Merge branch 'master' of yap.dcc.fc.up.pt:yap-6
This commit is contained in:
commit
d1d4150d68
@ -49,10 +49,6 @@ typedef struct
|
||||
variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]);
|
||||
expr createExpression(YAP_Term t);
|
||||
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 ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v,
|
||||
variables vars,GHashTable * nodes);
|
||||
int mVarIndex,
|
||||
variables vars,GHashTable * nodes, int comp);
|
||||
|
@ -117,52 +117,60 @@ nodes is used to store nodes for which the probability has alread been computed
|
||||
so that it is not recomputed
|
||||
*/
|
||||
{
|
||||
int index,mVarIndex,nBit;
|
||||
int index,mVarIndex,nBit,comp;
|
||||
variable v;
|
||||
double res;
|
||||
double value;
|
||||
double * value_p;
|
||||
DdNode **key;
|
||||
DdNode *nodereg;
|
||||
double *rp;
|
||||
|
||||
index=node->index;
|
||||
index=Cudd_NodeReadIndex(node);
|
||||
comp=Cudd_IsComplement(node);
|
||||
if (Cudd_IsConstant(node))
|
||||
{
|
||||
value=node->type.value;
|
||||
return value;
|
||||
if (comp)
|
||||
return 0.0;
|
||||
else
|
||||
return 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
value_p=g_hash_table_lookup(nodes,&node);
|
||||
nodereg=Cudd_Regular(node);
|
||||
value_p=g_hash_table_lookup(nodes,nodereg);
|
||||
if (value_p!=NULL)
|
||||
{
|
||||
return *value_p;
|
||||
if (comp)
|
||||
return 1-*value_p;
|
||||
else
|
||||
return *value_p;
|
||||
}
|
||||
else
|
||||
{
|
||||
mVarIndex=vars.bVar2mVar[index];
|
||||
v=vars.varar[mVarIndex];
|
||||
nBit=v.nBit;
|
||||
res=ProbBool(node,0,nBit,0,v,vars,nodes);
|
||||
key=(DdNode **)malloc(sizeof(DdNode *));
|
||||
*key=node;
|
||||
res=ProbBool(node,0,nBit,0,v,mVarIndex,vars,nodes,0);
|
||||
rp=(double *)malloc(sizeof(double));
|
||||
*rp=res;
|
||||
g_hash_table_insert(nodes, key, rp);
|
||||
return res;
|
||||
g_hash_table_insert(nodes, nodereg, rp);
|
||||
if (comp)
|
||||
return 1-res;
|
||||
else
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 */
|
||||
{
|
||||
DdNode *T,*F;
|
||||
double p,res;
|
||||
double * probs;
|
||||
|
||||
int comp1,comp2,index,indexF,mVarIndexF;
|
||||
|
||||
probs=v.probabilities;
|
||||
if (nBit==0)
|
||||
{
|
||||
@ -171,28 +179,41 @@ variables vars, GHashTable * nodes)
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (correctPosition(node->index,v,node,posBVar))
|
||||
index=Cudd_NodeReadIndex(node);
|
||||
if (correctPosition(index,v,node,posBVar))
|
||||
{
|
||||
T = node->type.kids.T;
|
||||
F = node->type.kids.E;
|
||||
T = Cudd_T(node);
|
||||
F = Cudd_E(node);
|
||||
bits=bits<<1;
|
||||
|
||||
res=ProbBool(T,bits+1,nBit-1,posBVar+1,v,vars,nodes)+
|
||||
ProbBool(F,bits,nBit-1,posBVar+1,v,vars,nodes);
|
||||
comp1=Cudd_IsComplement(F);
|
||||
res=ProbBool(T,bits+1,nBit-1,posBVar+1,v,mVarIndex,vars,nodes,comp);
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
bits=bits<<1;
|
||||
res=ProbBool(node,bits+1,nBit-1,posBVar+1,v,vars,nodes)+
|
||||
ProbBool(node,bits,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,mVarIndex,vars,nodes,comp);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -203,7 +224,8 @@ int correctPosition(int index,variable v, DdNode * node,int posBVar)
|
||||
currently explored by ProbBool */
|
||||
{
|
||||
DdNode * bvar;
|
||||
|
||||
int ind;
|
||||
bvar=v.booleanVars[posBVar];
|
||||
return bvar->index==index;
|
||||
ind=Cudd_NodeReadIndex(bvar);
|
||||
return ind==index;
|
||||
}
|
||||
|
@ -18,10 +18,8 @@ for the relative license.
|
||||
#include <string.h>
|
||||
|
||||
|
||||
unsigned long dividend;
|
||||
|
||||
FILE *open_file (char *filename, const char *mode);
|
||||
void reverse(char s[]);
|
||||
static int compute_prob(void);
|
||||
|
||||
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;
|
||||
variables vars;
|
||||
expr expression;
|
||||
DdNode * function, * add;
|
||||
DdNode * function;
|
||||
DdManager * mgr;
|
||||
int nBVar,i,j,intBits,create_dot;
|
||||
int nBVar,i,intBits,create_dot;
|
||||
FILE * file;
|
||||
DdNode * array[1];
|
||||
double prob;
|
||||
@ -179,7 +177,7 @@ static int compute_prob(void)
|
||||
|
||||
/* 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);
|
||||
//add=Cudd_BddToAdd(mgr,function);
|
||||
//Cudd_PrintInfo(mgr,stderr);
|
||||
|
||||
if (create_dot)
|
||||
@ -188,25 +186,17 @@ static int compute_prob(void)
|
||||
nBVar=vars.nBVar;
|
||||
for(i=0;i<nBVar;i++)
|
||||
names[i]=inames[i];
|
||||
array[0]=add;
|
||||
array[0]=function;
|
||||
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);
|
||||
nodes=g_hash_table_new_full(NULL,NULL,NULL,free);
|
||||
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;
|
||||
}
|
||||
prob=Prob(add,vars,nodes);
|
||||
prob=Prob(function,vars,nodes);
|
||||
out=YAP_MkFloatTerm(prob);
|
||||
g_hash_table_foreach (nodes,dealloc,NULL);
|
||||
g_hash_table_destroy(nodes);
|
||||
Cudd_Quit(mgr);
|
||||
for(i=0;i<vars.nVar;i++)
|
||||
@ -223,22 +213,7 @@ static int compute_prob(void)
|
||||
free(expression.terms);
|
||||
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()
|
||||
/* 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;
|
||||
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user