static int progress( void *instance, const lbfgsfloatval_t *local_x, const lbfgsfloatval_t *local_g, const lbfgsfloatval_t fx, const lbfgsfloatval_t xnorm, const lbfgsfloatval_t gnorm, const lbfgsfloatval_t step, int n, int k, int ls ) { YAP_Term call; YAP_Bool result; YAP_Int s1; YAP_Term t[8]; t[0] = YAP_MkFloatTerm(fx); t[1] = YAP_MkFloatTerm(xnorm); t[2] = YAP_MkFloatTerm(gnorm); t[3] = YAP_MkFloatTerm(step); t[4] = YAP_MkIntTerm(n); t[5] = YAP_MkIntTerm(k); t[6] = YAP_MkIntTerm(ls); t[7] = YAP_MkVarTerm(); call = YAP_MkApplTerm( fprogress8, 8, t); s1 = YAP_InitSlot(call); optimizer_status=OPTIMIZER_STATUS_CB_PROGRESS; result=YAP_CallProlog(call); optimizer_status=OPTIMIZER_STATUS_RUNNING; call = YAP_GetFromSlot( s1 ); if (result==FALSE) { printf("ERROR: Calling the progress call back function in YAP.\n"); // Goal did not succeed return FALSE; } if (YAP_IsIntTerm(YAP_ArgOfTerm(8,call))) { return YAP_IntOfTerm(YAP_ArgOfTerm(8,call)); } YAP_ShutdownGoal( TRUE ); fprintf(stderr, "ERROR: The progress call back function did not return an integer as last argument\n"); return 1; }
/** @pred optimizer_run(-F,-Status) Runs the optimization, _F is the best (minimal) function value and Status (int) is the status code returned by libLBFGS. Anything except 0 indicates an error, see the documentation of libLBFGS for the meaning. */ static int optimizer_run(void) { int ret = 0; YAP_Term t1 = YAP_ARG1; YAP_Term t2 = YAP_ARG2; YAP_Int s1, s2; lbfgsfloatval_t fx; lbfgsfloatval_t * tmp_x=x; if (optimizer_status == OPTIMIZER_STATUS_NONE) { printf("ERROR: Memory for parameter vector not initialized, please call optimizer_initialize/1 first.\n"); return FALSE; } if (optimizer_status != OPTIMIZER_STATUS_INITIALIZED) { printf("ERROR: Optimizer is running right now. Please wait till it is finished.\n"); return FALSE; } // both arguments have to be variables if (! YAP_IsVarTerm(t1) || ! YAP_IsVarTerm(t2)) { return FALSE; } s1 = YAP_InitSlot(t1); s2 = YAP_InitSlot(t2); optimizer_status = OPTIMIZER_STATUS_RUNNING; ret = lbfgs(n, x, &fx, evaluate, progress, NULL, ¶m); x=tmp_x; optimizer_status = OPTIMIZER_STATUS_INITIALIZED; YAP_Unify(YAP_GetFromSlot(s1),YAP_MkFloatTerm(fx)); YAP_Unify(YAP_GetFromSlot(s2),YAP_MkIntTerm(ret)); return TRUE; }
static YAP_Term get_term(list_cell cell) { if (strncmp(cell.type, "int", 3) == 0) return YAP_MkIntTerm(cell.int_val); else if (strncmp(cell.type, "double", 6) == 0) return YAP_MkFloatTerm(cell.double_val); return YAP_MkIntTerm(0); }
static YAP_Bool make_point (YAP_Float x, YAP_Float y, const char * functor_name, YAP_Term *term) { YAP_Term p[2]; YAP_Functor functor; if (functor_name == NULL) functor = YAP_MkFunctor (YAP_LookupAtom (","), 2); else functor = YAP_MkFunctor (YAP_LookupAtom (functor_name), 2); p[0] = YAP_MkFloatTerm (x); p[1] = YAP_MkFloatTerm (y); *term = YAP_MkApplTerm (functor, 2, p); return (TRUE); }
static int float_val(void) { YAP_Term in = YAP_ARG1; YAP_Term out = YAP_ARG2; char buffer[256]; YAP_StringToBuffer(in, buffer, BUFSIZE); YAP_Term res = YAP_MkFloatTerm(get_double(buffer)); return YAP_Unify(out, res); }
/* * returns the statistics */ static YAP_Bool mpi_stats(term_t YAP_ARG1,...){ fprintf(stderr,"%f %ld %ld %ld %ld %ld %ld\n",MPITIME,num_msgs_recv,bytes_recv,max_s_recv_msg,num_msgs_sent,bytes_sent,max_s_sent_msg); return (YAP_Unify(YAP_ARG1, YAP_MkFloatTerm((float)(MPITIME))) && YAP_Unify(YAP_ARG2, YAP_MkIntTerm((long)num_msgs_recv)) && YAP_Unify(YAP_ARG3, YAP_MkIntTerm((long)bytes_recv)) && YAP_Unify(YAP_ARG4, YAP_MkIntTerm((long)max_s_recv_msg)) && YAP_Unify(YAP_ARG5, YAP_MkIntTerm((long)num_msgs_sent)) && YAP_Unify(YAP_ARG6, YAP_MkIntTerm((long)bytes_sent)) && YAP_Unify(YAP_ARG7, YAP_MkIntTerm((long)max_s_sent_msg)) ); }
static int item2(YAP_Term tvar, YAP_Term titem, int offx, int offy) { mxArray *mat; int rows; int cols; int off; mat = get_array(tvar); rows = mxGetM(mat); cols = mxGetN(mat); off = MAT_ACCESS(offx,offy,rows,cols); if (!mat) return FALSE; if (mxIsInt32(mat)) { INT32_T *input = (INT32_T *)mxGetPr(mat); if (YAP_IsIntTerm(titem)) { input[off] = YAP_IntOfTerm(titem); } else if (YAP_IsFloatTerm(titem)) { input[off] = YAP_FloatOfTerm(titem); } else if (YAP_IsVarTerm(titem)) { return YAP_Unify(titem, YAP_MkIntTerm(input[off])); } else return FALSE; } else if (mxIsInt64(mat)) { INT64_T *input = (INT64_T *)mxGetPr(mat); if (YAP_IsIntTerm(titem)) { input[off] = YAP_IntOfTerm(titem); } else if (YAP_IsFloatTerm(titem)) { input[off] = YAP_FloatOfTerm(titem); } else if (YAP_IsVarTerm(titem)) { return YAP_Unify(titem, YAP_MkIntTerm(input[off])); } else return FALSE; } else if (mxIsCell(mat)) { if (YAP_IsVarTerm(titem)) { return YAP_Unify(titem, YAP_MkIntTerm((YAP_Int)mxGetCell(mat,off))); } else { mxArray *mat2 = get_array(titem); mxSetCell(mat,off, mat2); } } else if (mxIsDouble(mat)) { double *input = mxGetPr(mat); if (YAP_IsFloatTerm(titem)) { input[off] = YAP_FloatOfTerm(titem); } else if (YAP_IsIntTerm(titem)) { input[off] = YAP_IntOfTerm(titem); } else { return YAP_Unify(titem, YAP_MkFloatTerm(input[off])); } } else return FALSE; return cp_back(tvar, mat); }
static YAP_Bool ret_prob(void) { YAP_Term arg1, arg2, out; DdNode *node; arg1 = YAP_ARG1; arg2 = YAP_ARG2; node = (DdNode *)YAP_IntOfTerm(arg1); if (!Cudd_IsConstant(node)) { table = init_table(boolVars_ex[ex]); out = YAP_MkFloatTerm(Prob(node, 0)); destroy_table(table, boolVars_ex[ex]); } else { if (node == Cudd_ReadOne(mgr_ex[ex])) out = YAP_MkFloatTerm(1.0); else out = YAP_MkFloatTerm(0.0); } return (YAP_Unify(out, arg2)); }
static YAP_Bool paths(void) { double paths; YAP_Term arg1, arg2, out; DdNode *node; arg1 = YAP_ARG1; arg2 = YAP_ARG2; node = (DdNode *)YAP_IntOfTerm(arg1); paths = Cudd_CountPath(node); out = YAP_MkFloatTerm(paths); return (YAP_Unify(out, arg2)); }
static lbfgsfloatval_t evaluate( void *instance, const lbfgsfloatval_t *x, lbfgsfloatval_t *g_tmp, const int n, const lbfgsfloatval_t step ) { YAP_Term call; YAP_Term a1; YAP_Bool result; YAP_Int s1; YAP_Term t[3]; t[0] = YAP_MkVarTerm(); t[1] = YAP_MkIntTerm(n); t[2] = YAP_MkFloatTerm(step); call = YAP_MkApplTerm(fcall3, 3, t); g=g_tmp; s1 = YAP_InitSlot(call); optimizer_status=OPTIMIZER_STATUS_CB_EVAL; result=YAP_CallProlog(call); optimizer_status=OPTIMIZER_STATUS_RUNNING; if (result==FALSE) { printf("ERROR: Calling the evaluate call back function in YAP.\n"); // Goal did not succeed return FALSE; } call = YAP_GetFromSlot( s1 ); a1 = YAP_ArgOfTerm(1,call); if (YAP_IsFloatTerm(a1)) { YAP_ShutdownGoal( TRUE ); return (lbfgsfloatval_t) YAP_FloatOfTerm(a1); } else if (YAP_IsIntTerm(a1)) { YAP_ShutdownGoal( TRUE ); return (lbfgsfloatval_t) YAP_IntOfTerm(a1); } YAP_ShutdownGoal( TRUE ); fprintf(stderr, "ERROR: The evaluate call back function did not return a number as first argument.\n"); return 0; }
static int p_random(void) { double fli; long int t1, t2, t3; t1 = (a1 * 171) % 30269; t2 = (b1 * 172) % 30307; t3 = (c1 * 170) % 30323; fli = (t1/30269.0) + (t2/30307.0) + (t3/30323.0); a1 = t1; b1 = t2; c1 = t3; return(YAP_Unify(YAP_ARG1, YAP_MkFloatTerm(fli-(int)(fli)))); }
/* this has to be done carefully because we all need to transpose the matrix */ static YAP_Term cp_floats(int ndims, int *dims, double *input, int factor, int base, YAP_Term t) { int i; if (ndims == 1) for (i=dims[0]; i>0; i--) { t = YAP_MkPairTerm(YAP_MkFloatTerm(input[base+factor*(i-1)]),t); } else for (i=dims[0]; i>0; i--) { t = cp_floats(ndims-1, dims+1, input, factor*dims[0], base+factor*(i-1),t); } return t; }
static int optimizer_get_parameter( void ) { YAP_Term t1 = YAP_ARG1; YAP_Term t2 = YAP_ARG2; if (! YAP_IsAtomTerm(t1)) { return FALSE; } const char* name=YAP_AtomName(YAP_AtomOfTerm(t1)); if ((strcmp(name, "m") == 0)) { return YAP_Unify(t2,YAP_MkIntTerm(param.m)); } else if ((strcmp(name, "epsilon") == 0)) { return YAP_Unify(t2,YAP_MkFloatTerm(param.epsilon)); } else if ((strcmp(name, "past") == 0)) { return YAP_Unify(t2,YAP_MkIntTerm(param.past)); } else if ((strcmp(name, "delta") == 0)) { return YAP_Unify(t2,YAP_MkFloatTerm(param.delta)); } else if ((strcmp(name, "max_iterations") == 0)) { return YAP_Unify(t2,YAP_MkIntTerm(param.max_iterations)); } else if ((strcmp(name, "linesearch") == 0)) { return YAP_Unify(t2,YAP_MkIntTerm(param.linesearch)); } else if ((strcmp(name, "max_linesearch") == 0)) { return YAP_Unify(t2,YAP_MkIntTerm(param.max_linesearch)); } else if ((strcmp(name, "min_step") == 0)) { return YAP_Unify(t2,YAP_MkFloatTerm(param.min_step)); } else if ((strcmp(name, "max_step") == 0)) { return YAP_Unify(t2,YAP_MkFloatTerm(param.max_step)); } else if ((strcmp(name, "ftol") == 0)) { return YAP_Unify(t2,YAP_MkFloatTerm(param.ftol)); } else if ((strcmp(name, "gtol") == 0)) { return YAP_Unify(t2,YAP_MkFloatTerm(param.gtol)); } else if ((strcmp(name, "xtol") == 0)) { return YAP_Unify(t2,YAP_MkFloatTerm(param.xtol)); } else if ((strcmp(name, "orthantwise_c") == 0)) { return YAP_Unify(t2,YAP_MkFloatTerm(param.orthantwise_c)); } else if ((strcmp(name, "orthantwise_start") == 0)) { return YAP_Unify(t2,YAP_MkIntTerm(param.orthantwise_start)); } else if ((strcmp(name, "orthantwise_end") == 0)) { return YAP_Unify(t2,YAP_MkIntTerm(param.orthantwise_end)); } printf("ERROR: The parameter %s is unknown.\n",name); return FALSE; }
static int item1(YAP_Term tvar, YAP_Term titem, int off) { mxArray *mat; mat = get_array(tvar); if (!mat) return FALSE; if (mxIsInt32(mat)) { INT32_T *input = (INT32_T *)mxGetPr(mat); if (YAP_IsIntTerm(titem)) { input[off] = YAP_IntOfTerm(titem); } else if (YAP_IsFloatTerm(titem)) { input[off] = YAP_FloatOfTerm(titem); } else if (YAP_IsVarTerm(titem)) { return YAP_Unify(titem, YAP_MkIntTerm(input[off])); } else return FALSE; } else if (mxIsInt64(mat)) { INT64_T *input = (INT64_T *)mxGetPr(mat); if (YAP_IsIntTerm(titem)) { input[off] = YAP_IntOfTerm(titem); } else if (YAP_IsFloatTerm(titem)) { input[off] = YAP_FloatOfTerm(titem); } else if (YAP_IsVarTerm(titem)) { return YAP_Unify(titem, YAP_MkIntTerm(input[off])); } else return FALSE; } else if (mxIsCell(mat)) { if (YAP_IsVarTerm(titem)) { return YAP_Unify(titem, YAP_MkIntTerm((YAP_Int)mxGetCell(mat,off))); } else { mxArray *mat2 = get_array(titem); mxSetCell(mat,off, mat2); } } else if (mxIsDouble(mat)) { double *input = mxGetPr(mat); if (YAP_IsFloatTerm(titem)) { input[off] = YAP_FloatOfTerm(titem); } else if (YAP_IsIntTerm(titem)) { input[off] = YAP_IntOfTerm(titem); } else { return YAP_Unify(titem, YAP_MkFloatTerm(input[off])); } } else return FALSE; return cp_back(tvar, mat); }
/** @pred optimizer_get_g(+I,-G) Get the current value for `g[I]` (the partial derivative of _F_ with respect to `x[I]`). Only possible when the optimizer is initialized or running. */ static int get_g_value(void) { YAP_Term t1=YAP_ARG1; YAP_Term t2=YAP_ARG2; int i=0; if (optimizer_status != OPTIMIZER_STATUS_RUNNING && optimizer_status != OPTIMIZER_STATUS_CB_EVAL && optimizer_status != OPTIMIZER_STATUS_CB_PROGRESS) { printf("ERROR: optimizer_get_g/2 can only be called while the optimizer is running.\n"); return FALSE; } if (YAP_IsIntTerm(t1)) { i=YAP_IntOfTerm(t1); } else { return FALSE; } if (i<0 || i>=n) { return FALSE; } return YAP_Unify(t2,YAP_MkFloatTerm(g[i])); }
static int float_max_of_matrix(void) { Term t = YAP_ARG1; YAP_UInt dim = YAP_ArityOfFunctor(YAP_FunctorOfTerm(t)), i, pos; YAP_Float *mat = (YAP_Float *)malloc( dim*sizeof(YAP_Float) ), max; if (!mat) return FALSE; if (!YAP_ArgsToFloatArray(t, dim, mat)) return FALSE; max = mat[0]; pos = 0; for (i = 1; i < dim; i++) { if (mat[i] > max) { max = mat[i]; pos = i; } } return YAP_unify(YAP_ARG2, YAP_MkFloatTerm(min)) && YAP_unify(YAP_ARG3, YAP_MkIntTerm(pos)); }
/** @pred optimizer_get_x(+I,-X) Get the current value for `x[I]`. Only possible when the optimizer is initialized or running. */ static int get_x_value(void) { YAP_Term t1=YAP_ARG1; YAP_Term t2=YAP_ARG2; int i=0; if (optimizer_status==OPTIMIZER_STATUS_NONE) { printf("ERROR: set_x_value/2 can be called only when the optimizer is initialized.\n"); return FALSE; } if (YAP_IsIntTerm(t1)) { i=YAP_IntOfTerm(t1); } else { return FALSE; } if (i<0 || i>=n) { printf("ERROR: invalid index for set_x_value/2.\n"); return FALSE; } return YAP_Unify(t2,YAP_MkFloatTerm(x[i])); }
static YAP_Bool Q(void) { YAP_Term arg1, arg2, arg3, arg4, out, out1, term, nodesTerm, ruleTerm, tail, pair, compoundTerm; DdNode *node1, **nodes_ex; int r, lenNodes, i; double p1, p0, **eta_rule, CLL; arg1 = YAP_ARG1; arg2 = YAP_ARG2; arg3 = YAP_ARG3; arg4 = YAP_ARG4; nodesTerm = arg1; lenNodes = YAP_IntOfTerm(arg2); nodes_ex = (DdNode **)malloc(lenNodes * sizeof(DdNode *)); example_prob = (double *)malloc(lenNodes * sizeof(double)); for (i = 0; i < lenNodes; i++) { pair = YAP_HeadOfTerm(nodesTerm); node1 = (DdNode *)YAP_IntOfTerm(YAP_HeadOfTerm(pair)); nodes_ex[i] = node1; pair = YAP_TailOfTerm(pair); example_prob[i] = YAP_FloatOfTerm(YAP_HeadOfTerm(pair)); nodesTerm = YAP_TailOfTerm(nodesTerm); } for (r = 0; r < nRules; r++) { for (i = 0; i < rules[r] - 1; i++) { eta_rule = eta[r]; eta_rule[i][0] = 0; eta_rule[i][1] = 0; } } CLL = Expectation(nodes_ex, lenNodes); out = YAP_TermNil(); for (r = 0; r < nRules; r++) { tail = YAP_TermNil(); eta_rule = eta[r]; for (i = 0; i < rules[r] - 1; i++) { p0 = eta_rule[i][0]; p1 = eta_rule[i][1]; term = YAP_MkPairTerm(YAP_MkFloatTerm(p0), YAP_MkPairTerm(YAP_MkFloatTerm(p1), YAP_TermNil())); tail = YAP_MkPairTerm(term, tail); } ruleTerm = YAP_MkIntTerm(r); compoundTerm = YAP_MkPairTerm(ruleTerm, YAP_MkPairTerm(tail, YAP_TermNil())); out = YAP_MkPairTerm(compoundTerm, out); } free(nodes_ex); free(example_prob); out1 = YAP_MkFloatTerm(CLL); YAP_Unify(out1, arg4); return (YAP_Unify(out, arg3)); }
static YAP_Bool EM(void) { YAP_Term arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, out1, out2, out3, nodesTerm, ruleTerm, tail, pair, compoundTerm; DdNode *node1, **nodes_ex; int r, lenNodes, i, iter; long iter1; double CLL0 = -2.2 * pow(10, 10); //-inf double CLL1 = -1.7 * pow(10, 8); //+inf double p, p0, **eta_rule, ea, er; double ratio, diff; arg1 = YAP_ARG1; arg2 = YAP_ARG2; arg3 = YAP_ARG3; arg4 = YAP_ARG4; arg5 = YAP_ARG5; arg6 = YAP_ARG6; arg7 = YAP_ARG7; arg8 = YAP_ARG8; nodesTerm = arg1; ea = YAP_FloatOfTerm(arg2); er = YAP_FloatOfTerm(arg3); lenNodes = YAP_IntOfTerm(arg4); iter = YAP_IntOfTerm(arg5); nodes_ex = (DdNode **)malloc(lenNodes * sizeof(DdNode *)); nodes_probs_ex = (double *)malloc(lenNodes * sizeof(double)); example_prob = (double *)malloc(lenNodes * sizeof(double)); for (i = 0; i < lenNodes; i++) { pair = YAP_HeadOfTerm(nodesTerm); node1 = (DdNode *)YAP_IntOfTerm(YAP_HeadOfTerm(pair)); nodes_ex[i] = node1; pair = YAP_TailOfTerm(pair); example_prob[i] = YAP_FloatOfTerm(YAP_HeadOfTerm(pair)); nodesTerm = YAP_TailOfTerm(nodesTerm); } diff = CLL1 - CLL0; ratio = diff / fabs(CLL0); if (iter == -1) iter1 = 2147000000; else iter1 = iter; while ((diff > ea) && (ratio > er) && (cycle < iter1)) { cycle++; for (r = 0; r < nRules; r++) { for (i = 0; i < rules[r] - 1; i++) { eta_rule = eta[r]; eta_rule[i][0] = 0; eta_rule[i][1] = 0; } } CLL0 = CLL1; CLL1 = Expectation(nodes_ex, lenNodes); Maximization(); diff = CLL1 - CLL0; ratio = diff / fabs(CLL0); } out2 = YAP_TermNil(); for (r = 0; r < nRules; r++) { tail = YAP_TermNil(); p0 = 1; for (i = 0; i < rules[r] - 1; i++) { p = arrayprob[r][i] * p0; tail = YAP_MkPairTerm(YAP_MkFloatTerm(p), tail); p0 = p0 * (1 - arrayprob[r][i]); } tail = YAP_MkPairTerm(YAP_MkFloatTerm(p0), tail); ruleTerm = YAP_MkIntTerm(r); compoundTerm = YAP_MkPairTerm(ruleTerm, YAP_MkPairTerm(tail, YAP_TermNil())); out2 = YAP_MkPairTerm(compoundTerm, out2); } out3 = YAP_TermNil(); for (i = 0; i < lenNodes; i++) { out3 = YAP_MkPairTerm(YAP_MkFloatTerm(nodes_probs_ex[i]), out3); } YAP_Unify(out3, arg8); out1 = YAP_MkFloatTerm(CLL1); YAP_Unify(out1, arg6); free(nodes_ex); free(example_prob); free(nodes_probs_ex); return (YAP_Unify(out2, arg7)); }
static int compute_prob(void) /* this is the function that implements the compute_prob predicate used in pp.pl */ { YAP_Term out,arg1,arg2,arg3,arg4; variables vars; expr expression; DdNode * function; DdManager * mgr; int nBVar,i,intBits,create_dot; FILE * file; DdNode * array[1]; double prob; char * onames[1]; char inames[1000][20]; char * names[1000]; tablerow * nodes; arg1=YAP_ARG1; arg2=YAP_ARG2; arg3=YAP_ARG3; arg4=YAP_ARG4; mgr=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0); create_dot=YAP_IntOfTerm(arg4); vars=createVars(arg1,mgr,create_dot,inames); //Cudd_PrintInfo(mgr,stderr); /* automatic variable reordering, default method CUDD_REORDER_SIFT used */ //printf("status %d\n",Cudd_ReorderingStatus(mgr,&order)); //printf("order %d\n",order); Cudd_AutodynEnable(mgr,CUDD_REORDER_SAME); /* Cudd_AutodynEnable(mgr, CUDD_REORDER_RANDOM_PIVOT); printf("status %d\n",Cudd_ReorderingStatus(mgr,&order)); printf("order %d\n",order); printf("%d",CUDD_REORDER_RANDOM_PIVOT); */ expression=createExpression(arg2); function=retFunction(mgr,expression,vars); /* the BDD build by retFunction is converted to an ADD (algebraic decision diagram) because it is easier to interpret and to print */ //add=Cudd_BddToAdd(mgr,function); //Cudd_PrintInfo(mgr,stderr); if (create_dot) /* if specified by the user, a dot file for the BDD is written to cpl.dot */ { nBVar=vars.nBVar; for(i=0; i<nBVar; i++) names[i]=inames[i]; array[0]=function; onames[0]="Out"; file = open_file("cpl.dot", "w"); Cudd_DumpDot(mgr,1,array,names,onames,file); fclose(file); } nodes=init_table(vars.nBVar); intBits=sizeof(unsigned int)*8; prob=Prob(function,vars,nodes); out=YAP_MkFloatTerm(prob); destroy_table(nodes,vars.nBVar); Cudd_Quit(mgr); for(i=0; i<vars.nVar; i++) { free(vars.varar[i].probabilities); free(vars.varar[i].booleanVars); } free(vars.varar); free(vars.bVar2mVar); for(i=0; i<expression.nTerms; i++) { free(expression.terms[i].factors); } free(expression.terms); return(YAP_Unify(out,arg3)); }
static int compute_prob(void) /* this is the function that implements the compute_prob predicate used in pp.pl */ { YAP_Term out,arg1,arg2,arg3,arg4; array_t * variables,* expression, * bVar2mVar; DdNode * function, * add; DdManager * mgr; int nBVar,i,j,intBits,create_dot; FILE * file; DdNode * array[1]; char * onames[1]; char inames[1000][20]; char * names[1000]; GHashTable * nodes; /* hash table that associates nodes with their probability if already computed, it is defined in glib */ Cudd_ReorderingType order; arg1=YAP_ARG1; arg2=YAP_ARG2; arg3=YAP_ARG3; arg4=YAP_ARG4; mgr=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0); variables=array_alloc(variable,0); bVar2mVar=array_alloc(int,0); create_dot=YAP_IntOfTerm(arg4); createVars(variables,arg1,mgr,bVar2mVar,create_dot,inames); //Cudd_PrintInfo(mgr,stderr); /* automatic variable reordering, default method CUDD_REORDER_SIFT used */ //printf("status %d\n",Cudd_ReorderingStatus(mgr,&order)); //printf("order %d\n",order); Cudd_AutodynEnable(mgr,CUDD_REORDER_SAME); /* Cudd_AutodynEnable(mgr, CUDD_REORDER_RANDOM_PIVOT); printf("status %d\n",Cudd_ReorderingStatus(mgr,&order)); printf("order %d\n",order); printf("%d",CUDD_REORDER_RANDOM_PIVOT); */ expression=array_alloc(array_t *,0); createExpression(expression,arg2); function=retFunction(mgr,expression,variables); /* the BDD build by retFunction is converted to an ADD (algebraic decision diagram) because it is easier to interpret and to print */ add=Cudd_BddToAdd(mgr,function); //Cudd_PrintInfo(mgr,stderr); if (create_dot) /* if specified by the user, a dot file for the BDD is written to cpl.dot */ { nBVar=array_n(bVar2mVar); for(i=0;i<nBVar;i++) names[i]=inames[i]; array[0]=add; onames[0]="Out"; file = open_file("cpl.dot", "w"); Cudd_DumpDot(mgr,1,array,names,onames,file); fclose(file); } nodes=g_hash_table_new(my_hash,my_equal); intBits=sizeof(unsigned int)*8; /* dividend is a global variable used by my_hash it is equal to an unsigned int with binary representation 11..1 */ dividend=1; for(j=1;j<intBits;j++) { dividend=(dividend<<1)+1; } out=YAP_MkFloatTerm(Prob(add,variables,bVar2mVar,nodes)); g_hash_table_foreach (nodes,dealloc,NULL); g_hash_table_destroy(nodes); Cudd_Quit(mgr); array_free(variables); array_free(bVar2mVar); array_free(expression); return(YAP_Unify(out,arg3)); }