/* EMBLEM and SLIPCASE Copyright (c) 2013, 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 "config.h" #if HAVE_CUDD_CUDDINT_H #include "cudd/cuddInt.h" #else #include "cuddInt.h" #endif #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_ex; static int ** bVar2mVar_ex; static double * sigma; static double ***eta; static double ***eta_temp; static double **arrayprob; static int *rules; static DdManager **mgr_ex; static int *nVars_ex; static int nRules; double * nodes_probs_ex; double ** probs_ex; static int * boolVars_ex; tablerow * nodesB; tablerow * nodesF; int ex,cycle; DdNode *** nodesToVisit; int * NnodesToVisit; double * example_prob; static YAP_Bool ret_prob(void); double Prob(DdNode *node,int comp_par); static YAP_Bool end_bdd(void); static YAP_Bool init_test(void); static YAP_Bool add_var(void); static YAP_Bool init(void); static YAP_Bool end(void); static YAP_Bool EM(void); static YAP_Bool Q(void); double ProbPath(DdNode *node, int comp_par, int nex); static YAP_Bool rec_deref(void); int indexMvar(DdNode *node); void Forward(DdNode *node, int nex); void GetForward(DdNode *node, double ForwProbPath); void UpdateForward(DdNode * node, int nex); double GetOutsideExpe(DdNode *root,double ex_prob, int nex); 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 YAP_Bool 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_ex[ex]; probs_ex[ex]=(double *) realloc(probs_ex[ex],(((boolVars_ex[ex]+v->nVal-1)* sizeof(double)))); bVar2mVar_ex[ex]=(int *) realloc(bVar2mVar_ex[ex],((boolVars_ex[ex]+v->nVal-1)* sizeof(int))); probTerm=arg2; p0=1; for (i=0;inVal-1;i++) { node=Cudd_bddIthVar(mgr_ex[ex],boolVars_ex[ex]+i); p=YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm)); bVar2mVar_ex[ex][boolVars_ex[ex]+i]=nVars_ex[ex]-1; probs_ex[ex][boolVars_ex[ex]+i]=p/p0; probTerm_temp=YAP_TailOfTerm(probTerm); probTerm=probTerm_temp; p0=p0*(1-p/p0); } boolVars_ex[ex]=boolVars_ex[ex]+v->nVal-1; rules[v->nRule]= v->nVal; out=YAP_MkIntTerm((YAP_Int) nVars_ex[ex]-1); return YAP_Unify(out,arg4); } static YAP_Bool equality(void) { YAP_Term arg1,arg2,arg3,out; int varIndex; int value; int i; variable v; DdNode * node, * tmp,*var; arg1=YAP_ARG1; arg2=YAP_ARG2; arg3=YAP_ARG3; varIndex=YAP_IntOfTerm(arg1); value=YAP_IntOfTerm(arg2); v=vars_ex[ex][varIndex]; i=v.firstBoolVar; tmp=Cudd_ReadOne(mgr_ex[ex]); Cudd_Ref(tmp); node=NULL; for (i=v.firstBoolVar;i0.0) { for (j=0; jea) && (ratio>er) && (cycle