Exemplo n.º 1
0
double GetOutsideExpe(DdNode *root, double ex_prob, int nex) {
  int i, j, mVarIndex, bVarIndex;
  double **eta_rule;
  double theta, rootProb, T = 0;

  sigma = (double *)malloc(boolVars_ex[nex] * sizeof(double));

  for (j = 0; j < boolVars_ex[nex]; j++) {
    sigma[j] = 0;
  }
  for (j = 0; j < nRules; j++) {
    for (i = 0; i < rules[j] - 1; i++) {
      eta_temp[j][i][0] = 0;
      eta_temp[j][i][1] = 0;
    }
  }
  rootProb = ProbPath(root, 0, nex);
  if (rootProb > 0.0) {
    for (j = 0; j < boolVars_ex[nex]; j++) {
      T += sigma[j];
      bVarIndex = Cudd_ReadInvPerm(mgr_ex[nex], j);
      if (bVarIndex == -1) {
        bVarIndex = j;
      }

      mVarIndex = bVar2mVar_ex[nex][bVarIndex];
      eta_rule = eta_temp[vars_ex[nex][mVarIndex].nRule];
      for (i = 0; i < vars_ex[nex][mVarIndex].nVal - 1; i++) {
        theta = probs_ex[nex][bVarIndex];
        eta_rule[i][0] = eta_rule[i][0] + T * (1 - theta);
        eta_rule[i][1] = eta_rule[i][1] + T * theta;
      }
    }

    for (j = 0; j < nRules; j++) {
      for (i = 0; i < rules[j] - 1; i++) {
        eta[j][i][0] = eta[j][i][0] + eta_temp[j][i][0] * ex_prob / rootProb;
        eta[j][i][1] = eta[j][i][1] + eta_temp[j][i][1] * ex_prob / rootProb;
      }
    }
  }
  free(sigma);
  return rootProb;
}
Exemplo n.º 2
0
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;
    }
  }
}
Exemplo n.º 3
0
int DumpDot(DdManager *dd, DdNode *add, rnum *vars, onum *orig_vars,   char ** lnames, FILE *fp) {
  int i;
  // first, we must apply an ordering in which all the orig_vars's 
  //corresponding binary variables are  together
  // so we just re-apply the original ordering
  // we'll also save the initial ordering so we can re-apply it after the dump.
  int *list = new int[2*numvars];
  int *orig_list = new int[2*numvars];
  for (i=0; i<numvars*2; i++) {
    // this is the original ordering
    list[i] = i;
    orig_list[i] = Cudd_ReadInvPerm(dd,i);
  }
  /*
  fprintf(stderr,"ordering passed into dumpdot is ");
  for (i=0; i<numvars*2; i++) 
    fprintf(stderr,"%d ",orig_list[i]);
  fprintf(stderr,"\nnew ordering is                 ");
  for (i=0; i<numvars*2; i++) 
    fprintf(stderr,"%d ",list[i]);
  fprintf(stderr,"\n");
  */
  int res = Cudd_ShuffleHeap(dd,list);
  

  // write out the header and global attributes
  fprintf(fp,"digraph \"DD\" {\n");
  fprintf(fp,"size = \"7.5,10\"\nratio=1.0;\ncenter = true;\nedge [dir = none];\n");

   // write the links to a different file name (temporary)
  // and the nodes to the original file
  int howmanynodes(0);
  int dagCount = Cudd_DagSize(add);
  int dagLeafCount = Cudd_CountLeaves(add);

  numbranches  = 0;

  numexpbranches = dagCount;
  branches = new DdNode*[numexpbranches];
  branchnodenames = new char*[numexpbranches];
  FILE *nodes_fp = fopen("/tmp/spudd.dat","w");
  DumpDoth(gbm,add,orig_vars,0,lnames,fp,nodes_fp,&howmanynodes);
  fclose(nodes_fp);
  //now transfer all of nodes_fp over to fp
  nodes_fp = fopen("/tmp/spudd.dat","r");
  char tmp;
  while (!feof(nodes_fp)) {
    tmp = fgetc(nodes_fp);
    //?? why??
    if (!feof(nodes_fp))
      fputc(tmp,fp);
  }
  fprintf(fp,"}\n");
  for (i=0; i<numbranches; i++)
    Cudd_RecursiveDeref(gbm,branches[i]);

  // reapply the original ordering
  res = Cudd_ShuffleHeap(dd,orig_list);

  delete [] branches;
  delete [] list;
  delete [] orig_list;
  return(1);
}