void UpdateForward(DdNode *node, int nex) { int index, position, mVarIndex; DdNode *T, *E, *nodereg; variable v; double *value_p, *value_p_T, *value_p_F, p; if (Cudd_IsConstant(node)) { return; } else { index = Cudd_NodeReadIndex(node); mVarIndex = bVar2mVar_ex[nex][index]; v = vars_ex[nex][mVarIndex]; p = probs_ex[nex][index]; nodereg = Cudd_Regular(node); value_p = get_value(nodesF, nodereg); if (value_p == NULL) { printf("Error\n"); return; } else { T = Cudd_T(node); E = Cudd_E(node); if (!Cudd_IsConstant(T)) { value_p_T = get_value(nodesF, T); if (value_p_T != NULL) { *value_p_T = *value_p_T + *value_p * p; } else { add_or_replace_node(nodesF, Cudd_Regular(T), *value_p * p); index = Cudd_NodeReadIndex(T); position = Cudd_ReadPerm(mgr_ex[nex], index); nodesToVisit[position] = (DdNode **)realloc( nodesToVisit[position], (NnodesToVisit[position] + 1) * sizeof(DdNode *)); nodesToVisit[position][NnodesToVisit[position]] = T; NnodesToVisit[position] = NnodesToVisit[position] + 1; } } if (!Cudd_IsConstant(E)) { value_p_F = get_value(nodesF, Cudd_Regular(E)); if (value_p_F != NULL) { *value_p_F = *value_p_F + *value_p * (1 - p); } else { add_or_replace_node(nodesF, Cudd_Regular(E), *value_p * (1 - p)); index = Cudd_NodeReadIndex(E); position = Cudd_ReadPerm(mgr_ex[nex], index); nodesToVisit[position] = (DdNode **)realloc( nodesToVisit[position], (NnodesToVisit[position] + 1) * sizeof(DdNode *)); nodesToVisit[position][NnodesToVisit[position]] = E; NnodesToVisit[position] = NnodesToVisit[position] + 1; } } return; } } }
static void writeOrder(Biddy_Edge f, char *codes, unsigned int *order, unsigned int size) { char ch; #ifdef USE_CUDD int i, j; #endif int k; unsigned int ivar = 0; #ifdef USE_BIDDY /* TO DO: IMPLEMENT Biddy_ReadPerm() AND ITS INVERSE */ { Biddy_Edge tmp; tmp = Biddy_Support(f); while (!Biddy_IsConstant(tmp)) { /* In Biddy, the first user variable is at index [1] */ ivar = Biddy_GetTopVariable(tmp)-1; k = 3*(order[ivar]); ch = codes[k+2]; codes[k+2] = 0; printf("%s",&codes[k]); codes[k+2] = ch; tmp = Biddy_GetThen(tmp); if (!Biddy_IsConstant(tmp)) printf(","); else printf("\n"); }} #endif #ifdef USE_CUDD /* HOW TO AVOID THIS DOUBLE LOOP ??? */ for (i=0; i<size; i++) { /* In CUDD, the first user variable is at index [0] */ for (j=0; j<size; j++) { if (i == Cudd_ReadPerm(manager,j)) ivar = j; } k = 3 * (order[ivar]); ch = codes[k + 2]; codes[k + 2] = 0; printf("%s", &codes[k]); codes[k + 2] = ch; if (i != (size-1)) printf(","); else printf("\n"); } #endif }
double ProbPath(DdNode *node, int comp_par, int nex) { int index, mVarIndex, comp, pos, position, boolVarIndex; variable v; double res; double value, p, pt, pf, BChild0, BChild1, e0, e1; double *value_p, **eta_rule; DdNode *nodekey, *T, *F; comp = Cudd_IsComplement(node); comp = (comp && !comp_par) || (!comp && comp_par); if (Cudd_IsConstant(node)) { value = Cudd_V(node); if (comp) { return 0.0; } else { return 1.0; } } else { nodekey = Cudd_Regular(node); value_p = get_value(nodesB, nodekey); if (value_p != NULL) { return *value_p; } else { index = Cudd_NodeReadIndex(node); p = probs_ex[nex][index]; T = Cudd_T(node); F = Cudd_E(node); pf = ProbPath(F, comp, nex); pt = ProbPath(T, comp, nex); BChild0 = pf * (1 - p); BChild1 = pt * p; value_p = get_value(nodesF, nodekey); e0 = (*value_p) * BChild0; e1 = (*value_p) * BChild1; mVarIndex = bVar2mVar_ex[nex][index]; v = vars_ex[nex][mVarIndex]; pos = index - v.firstBoolVar; eta_rule = eta_temp[v.nRule]; eta_rule[pos][0] = eta_rule[pos][0] + e0; eta_rule[pos][1] = eta_rule[pos][1] + e1; res = BChild0 + BChild1; add_node(nodesB, nodekey, res); position = Cudd_ReadPerm(mgr_ex[nex], index); position = position + 1; boolVarIndex = Cudd_ReadInvPerm( mgr_ex[nex], position); // Returns the index of the variable currently // in the i-th position of the order. if (position < boolVars_ex[nex]) { sigma[position] = sigma[position] + e0 + e1; } if (!Cudd_IsConstant(T)) { index = Cudd_NodeReadIndex(T); position = Cudd_ReadPerm(mgr_ex[nex], index); sigma[position] = sigma[position] - e1; } if (!Cudd_IsConstant(F)) { index = Cudd_NodeReadIndex(F); position = Cudd_ReadPerm(mgr_ex[nex], index); sigma[position] = sigma[position] - e0; } return res; } } }