Merge branch 'master' of yap.dcc.fc.up.pt:yap-6

This commit is contained in:
Vitor Santos Costa 2010-07-27 23:31:02 +01:00
commit d1d4150d68
3 changed files with 58 additions and 95 deletions

View File

@ -49,10 +49,6 @@ typedef struct
variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]); variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]);
expr createExpression(YAP_Term t); expr createExpression(YAP_Term t);
void init_my_predicates(void); void init_my_predicates(void);
int compare(char *a, char *b);
gint my_equal(gconstpointer v,gconstpointer v2);
guint my_hash(gconstpointer key);
void dealloc(gpointer key,gpointer value,gpointer user_data);
@ -63,4 +59,5 @@ DdNode * retFactor(DdManager * mgr, factor f, variables v);
double Prob(DdNode *node, variables vars,GHashTable * nodes); double Prob(DdNode *node, variables vars,GHashTable * nodes);
double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v, double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v,
variables vars,GHashTable * nodes); int mVarIndex,
variables vars,GHashTable * nodes, int comp);

View File

@ -117,52 +117,60 @@ nodes is used to store nodes for which the probability has alread been computed
so that it is not recomputed so that it is not recomputed
*/ */
{ {
int index,mVarIndex,nBit; int index,mVarIndex,nBit,comp;
variable v; variable v;
double res; double res;
double value;
double * value_p; double * value_p;
DdNode **key; DdNode *nodereg;
double *rp; double *rp;
index=node->index; index=Cudd_NodeReadIndex(node);
comp=Cudd_IsComplement(node);
if (Cudd_IsConstant(node)) if (Cudd_IsConstant(node))
{ {
value=node->type.value; if (comp)
return value; return 0.0;
else
return 1.0;
} }
else else
{ {
nodereg=Cudd_Regular(node);
value_p=g_hash_table_lookup(nodes,&node); value_p=g_hash_table_lookup(nodes,nodereg);
if (value_p!=NULL) if (value_p!=NULL)
{ {
return *value_p; if (comp)
return 1-*value_p;
else
return *value_p;
} }
else else
{ {
mVarIndex=vars.bVar2mVar[index]; mVarIndex=vars.bVar2mVar[index];
v=vars.varar[mVarIndex]; v=vars.varar[mVarIndex];
nBit=v.nBit; nBit=v.nBit;
res=ProbBool(node,0,nBit,0,v,vars,nodes); res=ProbBool(node,0,nBit,0,v,mVarIndex,vars,nodes,0);
key=(DdNode **)malloc(sizeof(DdNode *));
*key=node;
rp=(double *)malloc(sizeof(double)); rp=(double *)malloc(sizeof(double));
*rp=res; *rp=res;
g_hash_table_insert(nodes, key, rp); g_hash_table_insert(nodes, nodereg, rp);
return res; if (comp)
return 1-res;
else
return res;
} }
} }
} }
double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v, double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v,
variables vars, GHashTable * nodes) int mVarIndex,
variables vars, GHashTable * nodes,int comp)
/* explores a group of binary variables making up the multivalued variable v */ /* explores a group of binary variables making up the multivalued variable v */
{ {
DdNode *T,*F; DdNode *T,*F;
double p,res; double p,res;
double * probs; double * probs;
int comp1,comp2,index,indexF,mVarIndexF;
probs=v.probabilities; probs=v.probabilities;
if (nBit==0) if (nBit==0)
{ {
@ -171,28 +179,41 @@ variables vars, GHashTable * nodes)
else else
{ {
p=probs[bits]; p=probs[bits];
res=p*Prob(node,vars,nodes); if (comp)
res=p*(1-Prob(node,vars,nodes));
else
res=p*Prob(node,vars,nodes);
return res; return res;
} }
} }
else else
{ {
if (correctPosition(node->index,v,node,posBVar)) index=Cudd_NodeReadIndex(node);
if (correctPosition(index,v,node,posBVar))
{ {
T = node->type.kids.T; T = Cudd_T(node);
F = node->type.kids.E; F = Cudd_E(node);
bits=bits<<1; bits=bits<<1;
comp1=Cudd_IsComplement(F);
res=ProbBool(T,bits+1,nBit-1,posBVar+1,v,vars,nodes)+ res=ProbBool(T,bits+1,nBit-1,posBVar+1,v,mVarIndex,vars,nodes,comp);
ProbBool(F,bits,nBit-1,posBVar+1,v,vars,nodes); indexF=Cudd_NodeReadIndex(F);
if (Cudd_IsConstant(F))
mVarIndexF=-1;
else
mVarIndexF=vars.bVar2mVar[indexF];
if (mVarIndexF==mVarIndex)
comp2=(comp1 && !comp) || (!comp1 && comp);
else
comp2=comp;
res=res+ ProbBool(F,bits,nBit-1,posBVar+1,v,mVarIndex,vars,nodes,comp2);
return res; return res;
} }
else else
{ {
bits=bits<<1; bits=bits<<1;
res=ProbBool(node,bits+1,nBit-1,posBVar+1,v,vars,nodes)+ res=ProbBool(node,bits+1,nBit-1,posBVar+1,v,mVarIndex,vars,nodes,comp)+
ProbBool(node,bits,nBit-1,posBVar+1,v,vars,nodes); ProbBool(node,bits,nBit-1,posBVar+1,v,mVarIndex,vars,nodes,comp);
return res; return res;
} }
} }
@ -203,7 +224,8 @@ int correctPosition(int index,variable v, DdNode * node,int posBVar)
currently explored by ProbBool */ currently explored by ProbBool */
{ {
DdNode * bvar; DdNode * bvar;
int ind;
bvar=v.booleanVars[posBVar]; bvar=v.booleanVars[posBVar];
return bvar->index==index; ind=Cudd_NodeReadIndex(bvar);
return ind==index;
} }

View File

@ -18,10 +18,8 @@ for the relative license.
#include <string.h> #include <string.h>
unsigned long dividend;
FILE *open_file (char *filename, const char *mode); FILE *open_file (char *filename, const char *mode);
void reverse(char s[]);
static int compute_prob(void); static int compute_prob(void);
variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]) variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20])
@ -139,9 +137,9 @@ static int compute_prob(void)
YAP_Term out,arg1,arg2,arg3,arg4; YAP_Term out,arg1,arg2,arg3,arg4;
variables vars; variables vars;
expr expression; expr expression;
DdNode * function, * add; DdNode * function;
DdManager * mgr; DdManager * mgr;
int nBVar,i,j,intBits,create_dot; int nBVar,i,intBits,create_dot;
FILE * file; FILE * file;
DdNode * array[1]; DdNode * array[1];
double prob; double prob;
@ -179,7 +177,7 @@ static int compute_prob(void)
/* the BDD build by retFunction is converted to an ADD (algebraic decision diagram) /* the BDD build by retFunction is converted to an ADD (algebraic decision diagram)
because it is easier to interpret and to print */ because it is easier to interpret and to print */
add=Cudd_BddToAdd(mgr,function); //add=Cudd_BddToAdd(mgr,function);
//Cudd_PrintInfo(mgr,stderr); //Cudd_PrintInfo(mgr,stderr);
if (create_dot) if (create_dot)
@ -188,25 +186,17 @@ static int compute_prob(void)
nBVar=vars.nBVar; nBVar=vars.nBVar;
for(i=0;i<nBVar;i++) for(i=0;i<nBVar;i++)
names[i]=inames[i]; names[i]=inames[i];
array[0]=add; array[0]=function;
onames[0]="Out"; onames[0]="Out";
file = open_file("cpl.dot", "w"); file = open_file("cpl.dot", "w");
Cudd_DumpDot(mgr,1,array,names,onames,file); Cudd_DumpDot(mgr,1,array,names,onames,file);
fclose(file); fclose(file);
} }
nodes=g_hash_table_new(my_hash,my_equal); nodes=g_hash_table_new_full(NULL,NULL,NULL,free);
intBits=sizeof(unsigned int)*8; intBits=sizeof(unsigned int)*8;
/* dividend is a global variable used by my_hash prob=Prob(function,vars,nodes);
it is equal to an unsigned int with binary representation 11..1 */
dividend=1;
for(j=1;j<intBits;j++)
{
dividend=(dividend<<1)+1;
}
prob=Prob(add,vars,nodes);
out=YAP_MkFloatTerm(prob); out=YAP_MkFloatTerm(prob);
g_hash_table_foreach (nodes,dealloc,NULL);
g_hash_table_destroy(nodes); g_hash_table_destroy(nodes);
Cudd_Quit(mgr); Cudd_Quit(mgr);
for(i=0;i<vars.nVar;i++) for(i=0;i<vars.nVar;i++)
@ -223,22 +213,7 @@ static int compute_prob(void)
free(expression.terms); free(expression.terms);
return(YAP_Unify(out,arg3)); return(YAP_Unify(out,arg3));
} }
/*
int compare(char *a, char *b)
{
int aval,bval;
aval=(int) *((DdNode **)a);
aval=(int) *((DdNode **)b);
if (aval<bval)
return -1;
else
if (aval>bval)
return 1;
else
return 0;
}
*/
void init_my_predicates() void init_my_predicates()
/* function required by YAP for intitializing the predicates defined by a C function*/ /* function required by YAP for intitializing the predicates defined by a C function*/
{ {
@ -257,35 +232,4 @@ open_file(char *filename, const char *mode)
return fp; return fp;
} }
void reverse(char s[])
/* reverses a string */
{
int i,c,j;
for (i=0,j=strlen(s)-1;i<j;i++,j--)
{
c=s[i];
s[i]=s[j];
s[j]=c;
}
}
gint my_equal(gconstpointer v,gconstpointer v2)
/* function used by GHashTable to compare two keys */
{
DdNode *a,*b;
a=*(DdNode **)v;
b=*(DdNode **)v2;
return (a==b);
}
guint my_hash(gconstpointer key)
/* function used by GHashTable to hash a key */
{
unsigned int h;
h=(unsigned int)((unsigned long) *((DdNode **)key) % dividend);
return h;
}
void dealloc(gpointer key,gpointer value,gpointer user_data)
{
free(key);
free(value);
}