/* EMBLEM and SLIPCASE Copyright (c) 2011, Fabrizio Riguzzi and Elena Bellodi This package uses the library cudd, see http://vlsi.colorado.edu/~fabio/CUDD/ for the relative license. */ #include #include #include #include #include "cuddInt.h" #include "YapInterface.h" #define LOGZERO log(0.000001) #define CACHE_SLOTS 1 #define UNIQUE_SLOTS 1 typedef struct { int nVal,nRule; int firstBoolVar; } variable; typedef struct { DdNode *key; double value; } rowel; typedef struct { int cnt; rowel *row; } tablerow; tablerow * table; static variable * vars; static variable ** vars_ex; static int * bVar2mVar; static int ** bVar2mVar_ex; static double * sigma; static double ***eta; static double ***eta_temp; static double **arrayprob; static int *rules; static DdManager *mgr; static DdManager **mgr_ex; static int *nVars; static int *nVars_ex; static int nRules; double * probs; double * nodes_probs_ex; double ** probs_ex; static int * boolVars; static int * boolVars_ex; tablerow * nodesB; tablerow * nodesF; int ex,cycle; DdNode *** nodesToVisit; int * NnodesToVisit; double * example_prob; static int ret_prob(void); double Prob(DdNode *node,int comp_par); static int end_bdd(void); static int init_test(void); static int add_var(void); static int init(void); static int end(void); static int EM(void); static int Q(void); double ProbPath(DdNode *node, int comp_par); static int rec_deref(void); int indexMvar(DdNode *node); void Forward(DdNode *node); void GetForward(DdNode *node, double ForwProbPath); void UpdateForward(DdNode * node); double GetOutsideExpe(DdNode *root,double ex_prob); void Maximization(void); static double Expectation(DdNode **nodes_ex, int lenNodes); void init_my_predicates(void); FILE *open_file(char *filename, const char *mode); tablerow* init_table(int varcnt); double * get_value(tablerow *tab, DdNode *node); void add_or_replace_node(tablerow *tab, DdNode *node, double value); void add_node(tablerow *tab, DdNode *node, double value); void destroy_table(tablerow *tab,int varcnt); static int init(void) { int j,i; YAP_Term arg1,arg2,list; ex=0; cycle=0; arg1=YAP_ARG1; arg2=YAP_ARG2; nRules=YAP_IntOfTerm(arg1); vars_ex=NULL; nVars_ex=NULL; eta= (double ***) malloc(nRules * sizeof(double **)); eta_temp= (double ***) malloc(nRules * sizeof(double **)); rules= (int *) malloc(nRules * sizeof(int)); arrayprob=(double **) malloc(nRules * sizeof(double *)); probs_ex=NULL; bVar2mVar_ex=NULL; boolVars_ex=NULL; mgr_ex=NULL; nodes_probs_ex=NULL; list=arg2; for (j=0;jnVal=YAP_IntOfTerm(arg1); v->nRule=YAP_IntOfTerm(arg3); v->firstBoolVar=*boolVars; probs=(double *) realloc(probs,(((*boolVars+v->nVal-1)* sizeof(double)))); bVar2mVar=(int *) realloc(bVar2mVar,((*boolVars+v->nVal-1)* sizeof(int))); probTerm=arg2; p0=1; for (i=0;inVal-1;i++) { node=Cudd_bddIthVar(mgr,*boolVars+i); p=YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm)); bVar2mVar[*boolVars+i]=*nVars-1; probs[*boolVars+i]=p/p0; probTerm_temp=YAP_TailOfTerm(probTerm); probTerm=probTerm_temp; p0=p0*(1-p/p0); } *boolVars=*boolVars+v->nVal-1; rules[v->nRule]= v->nVal; out=YAP_MkIntTerm((YAP_Int)* nVars-1); return YAP_Unify(out,arg4); } static int equality(void) { YAP_Term arg1,arg2,arg3,out; int varIndex; int value; int i; variable v; DdNode * node, * tmp,*var; arg1=YAP_ARG1; //var arg2=YAP_ARG2; //value arg3=YAP_ARG3; //node varIndex=YAP_IntOfTerm(arg1); value=YAP_IntOfTerm(arg2); v=vars[varIndex]; i=v.firstBoolVar; tmp=Cudd_ReadOne(mgr); Cudd_Ref(tmp); node=NULL; for (i=v.firstBoolVar;i0.0) { for (j=0; j<*boolVars; j++) { T += sigma[j]; bVarIndex=Cudd_ReadInvPerm(mgr,j); if (bVarIndex==-1) { bVarIndex=j; } mVarIndex=bVar2mVar[bVarIndex]; eta_rule=eta_temp[vars[mVarIndex].nRule]; for (i=0; iea) && (ratio>er) && (cycle