updated cplint

This commit is contained in:
Fabrizio Riguzzi 2011-10-22 16:33:04 +02:00
parent 90f8be5f0b
commit be449b3aef
51 changed files with 96393 additions and 980 deletions

View File

@ -705,6 +705,7 @@ all: startup.yss
@ENABLE_ZLIB@ @INSTALL_DLLS@ (cd packages/zlib; $(MAKE))
@ENABLE_CPLINT@ (cd packages/cplint/approx/simplecuddLPADs; $(MAKE))
@ENABLE_CPLINT@ (cd packages/cplint; $(MAKE))
@ENABLE_CPLINT@ (cd packages/cplint/slipcase; $(MAKE))
@ENABLE_CUDD@ (cd packages/ProbLog/simplecudd; $(MAKE))
@ENABLE_CUDD@ (cd packages/ProbLog/simplecudd_lfi; $(MAKE))
@ENABLE_JPL@ @INSTALL_DLLS@ (cd packages/jpl; $(MAKE))

905
configure vendored

File diff suppressed because it is too large Load Diff

View File

@ -164,7 +164,7 @@ AC_ARG_ENABLE(dlcompat,
use_dlcompat="$enableval", use_dlcompat=no)
AC_ARG_ENABLE(cplint,
[ --enable-cplint[=DIR] enable the cplint library using the glu library in DIR/lib],
[ --enable-cplint[=DIR] enable the cplint library using the CUDD library in DIR/lib],
yap_cv_cplint=$enableval, yap_cv_cplint=no)
AC_ARG_ENABLE(clpbn-bp,
@ -1412,22 +1412,13 @@ if test "$tabling" = "yes"
fi
if test ! "$yap_cv_cplint" = "no"
then
GLIBS=`pkg-config --libs glib-2.0`
GFLAGS=`pkg-config --cflags glib-2.0`
CPLINT_LIBS=${CPLINT_LIBS}$GLIBS
CPLINT_CFLAGS=" $GFLAGS "
CPLINT_LDFLAGS=" `echo $GLIBS | awk '{print $1}'` -L$pefic/lib/"
LDFLAGS=${LDFLAGS}" `echo $GLIBS | awk '{print $1}'` -L$prefix/lib/"
if test ! "$yap_cv_cplint" = "yes"
then
CPLINT_LDFLAGS+=" -L${yap_cv_cplint}/cudd -L${yap_cv_cplint}/mtr -L${yap_cv_cplint}/st -L${yap_cv_cplint}/util -L${yap_cv_cplint}/epd "
CPLINT_CFLAGS+=" -I${yap_cv_cplint}/include"
CPLINT_LIBS="-lcudd -lmtr -lst -lepd -lutil -lm "$CPLINT_LIBS
fi
#AC_SEARCH_LIBS([g_hash_table_new], [glib-2.0], [], [AC_MSG_ERROR([This package needs glib >=2.0.], [1])], [])
#AC_SEARCH_LIBS([Cudd_Init], [cudd], [], [AC_MSG_ERROR([This package needs CUDD.], [1])], [-lcudd -lmtr -lst -lutil -lepd -lm])
#AC_SEARCH_LIBS([pow], [m], [], [AC_MSG_ERROR([This package needs m.], [1])], [-lm])
ENABLE_CPLINT=""
ENABLE_CPLINT=""
if test "$target_os" = "cygwin" -o "$target_os" = "mingw32"
then
CPLINT_SHLIB_LD="gcc -shared ../../yap.dll"
@ -2201,6 +2192,7 @@ AC_CONFIG_FILES([packages/Dialect.defs])
AC_CONFIG_FILES([packages/CLPBN/Makefile])
AC_CONFIG_FILES([packages/cplint/Makefile])
AC_CONFIG_FILES([packages/cplint/approx/simplecuddLPADs/Makefile])
AC_CONFIG_FILES([packages/cplint/slipcase/Makefile])
AC_CONFIG_FILES([packages/meld/Makefile])
AC_CONFIG_FILES([packages/ProbLog/Makefile ])

View File

@ -146,7 +146,7 @@ CPLINT_DOCS=\
CPLINT_LEARNING_PROGRAMS=\
$(CPLINT_SRCDIR)/em \
$(CPLINT_SRCDIR)/rib
$(CPLINT_SRCDIR)/rib
all: $(SOBJS)
@ -177,7 +177,10 @@ install: all
$(INSTALL_PROGRAM) $(SOBJS) $(DESTDIR)$(YAPLIBDIR)
$(INSTALL_PROGRAM) approx/simplecuddLPADs/LPADBDD $(SHAREDIR)
cp -R $(CPLINT_LEARNING_PROGRAMS) $(DESTDIR)$(SHAREDIR)/cplint
cp $(CPLINT_SRCDIR)/em/*.pl $(DESTDIR)$(SHAREDIR)/cplint/
cp $(CPLINT_SRCDIR)/rib/*.pl $(DESTDIR)$(SHAREDIR)/cplint/
cp $(CPLINT_SRCDIR)/slipcase/*.pl $(DESTDIR)$(SHAREDIR)/cplint/
$(INSTALL_PROGRAM) slipcase/bddem.so $(DESTDIR)$(YAPLIBDIR)
installcheck:
for h in ${CPLINT_TEST_PROGRAMS}; do echo "t. halt." | $(BINDIR)/yap -l $$h; done

View File

@ -1,11 +1,10 @@
This directory contains the code of the LPAD anc CP-logic interpreter cplint
This directory contains the code of the cplint suite
All the code in this directory is distributed under the terms of the
The Artistic License 2.0, see the file Artistic.
COMPILATION:
This package requires CUDD and GLIB version 1.2.
This package requires CUDD
You can download CUDD from http://vlsi.colorado.edu/~fabio/CUDD/
You can download GLIB from http://www.gtk.org/. This is a standard Linux package
so it is easy to install using the package management software of your Linux
distribution
INSTALLATION:
Compile CUDD:

View File

@ -1,5 +1,5 @@
/*
LPAD and CP-Logic interpreter
LPAD and CP-Logic interpreter
Copyright (c) 2007, Fabrizio Riguzzi
@ -12,52 +12,68 @@ for the relative license.
#include "cuddInt.h"
#include "mtr.h"
#include "YapInterface.h"
#include <glib.h>
typedef struct
{
int var,value;
} factor;
{
int var,value;
} factor;
typedef struct
{
int nFact;
factor * factors;
} term;
{
int nFact;
factor * factors;
} term;
typedef struct
{
int nTerms;
term * terms;
} expr;
{
int nTerms;
term * terms;
} expr;
typedef struct
{
int nVal,nBit;
double * probabilities;
DdNode * * booleanVars;
} variable;
{
int nVal,nBit;
double * probabilities;
DdNode * * booleanVars;
} variable;
typedef struct
{
int nVar;
int nBVar;
variable * varar;
int * bVar2mVar;
} variables;
{
int nVar;
int nBVar;
variable * varar;
int * bVar2mVar;
} variables;
variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]);
typedef struct
{
DdNode *key;
double value;
} rowel;
typedef struct
{
int cnt;
rowel *row;
} tablerow;
variables createVars(YAP_Term t,DdManager * mgr, int create_dot,
char inames[1000][20]);
expr createExpression(YAP_Term t);
void init_my_predicates(void);
DdNode * retFunction(DdManager * mgr, expr expression,variables v);
DdNode * retTerm(DdManager * mgr,term t,variables v);
DdNode * retFactor(DdManager * mgr, factor f, variables v);
double Prob(DdNode *node, variables vars,GHashTable * nodes);
double Prob(DdNode *node, variables vars,tablerow * nodes);
double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v,
int mVarIndex,
variables vars,GHashTable * nodes, int comp);
int mVarIndex,
variables vars,tablerow * nodes, int comp);
tablerow* init_table(int nbvars);
double * get_value(tablerow *tab, DdNode *node);
void add_node(tablerow *tab, DdNode *node, double value);
void destroy_table(tablerow *tab, int nbvars);

View File

@ -1,20 +1,21 @@
/*
LPAD and CP-Logic interpreter
LPAD and CP-Logic interpreter
Copyright (c) 2007, Fabrizio Riguzzi
This package uses the library cudd, see http://vlsi.colorado.edu/~fabio/CUDD/
for the relative license.
This file contains the definition of Prob and ProbBool plus the functions
for building the BDD
This file contains the definition of Prob and ProbBool plus the functions
for building the BDD
*/
#include "cplint.h"
#include <stdlib.h>
int correctPosition(int index,variable v, DdNode * node,int posBVar);
@ -87,7 +88,8 @@ DdNode * retFactor(DdManager * mgr, factor f, variables vars)
node=Cudd_ReadOne(mgr);
Cudd_Ref(node);
/* booelan var with index 0 in v.booleanVars is the most significant */
do {
do
{
booleanVar=booleanVars[i];
bit=value & 01;
if (bit)
@ -96,22 +98,21 @@ DdNode * retFactor(DdManager * mgr, factor f, variables vars)
Cudd_Ref(tmp);
}
else
{
{
tmp=Cudd_bddAnd(mgr,node,Cudd_Not(booleanVar));
Cudd_Ref(tmp);
}
}
value=value>>1;
i--;
Cudd_RecursiveDeref(mgr,node);
node=tmp;
} while (i>=0);
return node;
}
double Prob(DdNode *node, variables vars, GHashTable * nodes)
double Prob(DdNode *node, variables vars, tablerow * nodes)
/* compute the probability of the expression rooted at node
nodes is used to store nodes for which the probability has alread been computed
so that it is not recomputed
@ -122,48 +123,45 @@ so that it is not recomputed
double res;
double * value_p;
DdNode *nodereg;
double *rp;
index=Cudd_NodeReadIndex(node);
comp=Cudd_IsComplement(node);
if (Cudd_IsConstant(node))
{
if (comp)
return 0.0;
else
return 1.0;
if (comp)
return 0.0;
else
return 1.0;
}
else
{
nodereg=Cudd_Regular(node);
value_p=g_hash_table_lookup(nodes,nodereg);
if (value_p!=NULL)
{
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,mVarIndex,vars,nodes,0);
rp=(double *)malloc(sizeof(double));
*rp=res;
g_hash_table_insert(nodes, nodereg, rp);
if (comp)
return 1-res;
else
return res;
}
}
{
nodereg=Cudd_Regular(node);
value_p=get_value(nodes,nodereg);
if (value_p!=NULL)
{
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,mVarIndex,vars,nodes,0);
add_node(nodes,nodereg,res);
if (comp)
return 1-res;
else
return res;
}
}
}
double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v,
int mVarIndex,
variables vars, GHashTable * nodes,int comp)
int mVarIndex,
variables vars, tablerow * nodes,int comp)
/* explores a group of binary variables making up the multivalued variable v */
{
DdNode *T,*F;
@ -180,15 +178,15 @@ variables vars, GHashTable * nodes,int comp)
{
p=probs[bits];
if (comp)
res=p*(1-Prob(node,vars,nodes));
else
res=p*Prob(node,vars,nodes);
res=p*(1-Prob(node,vars,nodes));
else
res=p*Prob(node,vars,nodes);
return res;
}
}
else
{
index=Cudd_NodeReadIndex(node);
index=Cudd_NodeReadIndex(node);
if (correctPosition(index,v,node,posBVar))
{
T = Cudd_T(node);
@ -197,23 +195,22 @@ variables vars, GHashTable * nodes,int comp)
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);
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,mVarIndex,vars,nodes,comp)+
ProbBool(node,bits,nBit-1,posBVar+1,v,mVarIndex,vars,nodes,comp);
ProbBool(node,bits,nBit-1,posBVar+1,v,mVarIndex,vars,nodes,comp);
return res;
}
}
@ -225,7 +222,57 @@ currently explored by ProbBool */
{
DdNode * bvar;
int ind;
bvar=v.booleanVars[posBVar];
ind=Cudd_NodeReadIndex(bvar);
return ind==index;
return ind==index;
}
double * get_value(tablerow *tab, DdNode *node) {
int i;
int index = Cudd_NodeReadIndex(node);
for(i = 0; i < tab[index].cnt; i++)
{
if (tab[index].row[i].key == node)
{
return &tab[index].row[i].value;
}
}
return NULL;
}
void destroy_table(tablerow *tab, int boolVars)
{
int i;
for (i = 0; i < boolVars; i++)
{
free(tab[i].row);
}
free(tab);
}
tablerow* init_table(int boolVars) {
int i;
tablerow *tab;
tab = (tablerow *) malloc(sizeof(rowel) * boolVars);
for (i = 0; i < boolVars; i++)
{
tab[i].row = NULL;
tab[i].cnt = 0;
}
return tab;
}
void add_node(tablerow *tab, DdNode *node, double value)
{
int index = Cudd_NodeReadIndex(node);
tab[index].row = (rowel *) realloc(tab[index].row,
(tab[index].cnt + 1) * sizeof(rowel));
tab[index].row[tab[index].cnt].key = node;
tab[index].row[tab[index].cnt].value = value;
tab[index].cnt += 1;
}

View File

@ -1,15 +1,15 @@
/*
LPAD and CP-Logic interpreter
LPAD and CP-Logic interpreter
Copyright (c) 2007, Fabrizio Riguzzi
This package uses the library cudd, see http://vlsi.colorado.edu/~fabio/CUDD/
for the relative license.
This file contains the functions for interfacing Yap and C
The arguments of the predicate compute_prob are parsed and translated into C data
structures
This file contains the functions for interfacing Yap and C
The arguments of the predicate compute_prob are parsed and translated into C data
structures
*/
#include "cplint.h"
@ -28,67 +28,61 @@ the array vars containing them
returns also the names of the variables to be used to save the ADD in dot format
*/
{
YAP_Term varTerm,probTerm;
int varIndex,nVal,i,b;
variable v;
char numberVar[10],numberBit[10];
double p;
variables vars;
vars.varar= (variable *) malloc(0* sizeof(variable));
vars.bVar2mVar=(int *) malloc(0* sizeof(int));
YAP_Term varTerm,probTerm;
int varIndex,nVal,i,b;
variable v;
char numberVar[10],numberBit[10];
double p;
variables vars;
vars.varar=NULL;
vars.bVar2mVar=NULL;
b=0;
vars.nVar=0;
varIndex=0;
while(YAP_IsPairTerm(t))
{
varTerm=YAP_HeadOfTerm(t);
varIndex=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));
varTerm=YAP_TailOfTerm(varTerm);
nVal=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));
varTerm=YAP_TailOfTerm(varTerm);
probTerm=YAP_HeadOfTerm(varTerm);
v.nVal=nVal;
v.nBit=(int)ceil(log(nVal)/log(2));
v.probabilities=(double *) malloc(nVal* sizeof(double));
v.booleanVars=(DdNode * *) malloc(v.nBit* sizeof(DdNode *));
for (i=0;i<nVal;i++)
{
p=YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm));
v.probabilities[i]=p;
probTerm=YAP_TailOfTerm(probTerm);
}
for (i=0;i<v.nBit;i++)
{
if (create_dot)
{
strcpy(inames[b+i],"X");
sprintf(numberVar,"%d",varIndex);
strcat(inames[b+i],numberVar);
strcat(inames[b+i],"_");
sprintf(numberBit,"%d",i);
strcat(inames[b+i],numberBit);
}
v.booleanVars[i]=Cudd_bddIthVar(mgr,b+i);
vars.bVar2mVar=(int *)realloc(vars.bVar2mVar,(b+i+1)*sizeof(int));
vars.bVar2mVar[b+i]=varIndex;
}
Cudd_MakeTreeNode(mgr,b,v.nBit,MTR_FIXED);
b=b+v.nBit;
vars.varar=(variable *) realloc(vars.varar,(varIndex+1)* sizeof(variable));
vars.varar[varIndex]=v;
t=YAP_TailOfTerm(t);
}
vars.nVar=varIndex+1;
vars.nBVar=b;
return vars;
b=0;
vars.nVar=0;
varIndex=0;
while(YAP_IsPairTerm(t))
{
varTerm=YAP_HeadOfTerm(t);
varIndex=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));
varTerm=YAP_TailOfTerm(varTerm);
nVal=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm));
varTerm=YAP_TailOfTerm(varTerm);
probTerm=YAP_HeadOfTerm(varTerm);
v.nVal=nVal;
v.nBit=(int)ceil(log(nVal)/log(2));
v.probabilities=(double *) malloc(nVal* sizeof(double));
v.booleanVars=(DdNode * *) malloc(v.nBit* sizeof(DdNode *));
for (i=0;i<nVal;i++)
{
p=YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm));
v.probabilities[i]=p;
probTerm=YAP_TailOfTerm(probTerm);
}
for (i=0;i<v.nBit;i++)
{
if (create_dot)
{
strcpy(inames[b+i],"X");
sprintf(numberVar,"%d",varIndex);
strcat(inames[b+i],numberVar);
strcat(inames[b+i],"_");
sprintf(numberBit,"%d",i);
strcat(inames[b+i],numberBit);
}
v.booleanVars[i]=Cudd_bddIthVar(mgr,b+i);
vars.bVar2mVar=(int *)realloc(vars.bVar2mVar,(b+i+1)*sizeof(int));
vars.bVar2mVar[b+i]=varIndex;
}
Cudd_MakeTreeNode(mgr,b,v.nBit,MTR_FIXED);
b=b+v.nBit;
vars.varar=(variable *) realloc(vars.varar,(varIndex+1)* sizeof(variable));
vars.varar[varIndex]=v;
t=YAP_TailOfTerm(t);
}
vars.nVar=varIndex+1;
vars.nBVar=b;
return vars;
}
expr createExpression(YAP_Term t)
@ -98,120 +92,121 @@ each factor is a couple (index of variable, index of value) obtained from a prol
two integers
*/
{
YAP_Term termTerm,factorTerm;
factor f;
int i,j;
term term1;
expr expression;
expression.terms=(term *)malloc(0 *sizeof(term));
i=0;
while(YAP_IsPairTerm(t))
{
term1.factors=(factor *)malloc(0 *sizeof(factor));
termTerm=YAP_HeadOfTerm(t);
j=0;
while(YAP_IsPairTerm(termTerm))
{
factorTerm=YAP_HeadOfTerm(termTerm);
f.var=YAP_IntOfTerm(YAP_HeadOfTerm(factorTerm));
f.value=YAP_IntOfTerm(YAP_HeadOfTerm(YAP_TailOfTerm(factorTerm)));
term1.factors=(factor *)realloc(term1.factors,(j+1)* sizeof(factor));
term1.factors[j]=f;
termTerm=YAP_TailOfTerm(termTerm);
j++;
}
term1.nFact=j;
expression.terms=(term *)realloc(expression.terms,(i+1)* sizeof(term));
expression.terms[i]=term1;
t=YAP_TailOfTerm(t);
i++;
}
expression.nTerms=i;
return(expression);
YAP_Term termTerm,factorTerm;
factor f;
int i,j;
term term1;
expr expression;
expression.terms=NULL;
i=0;
while(YAP_IsPairTerm(t))
{
term1.factors=NULL;
termTerm=YAP_HeadOfTerm(t);
j=0;
while(YAP_IsPairTerm(termTerm))
{
factorTerm=YAP_HeadOfTerm(termTerm);
f.var=YAP_IntOfTerm(YAP_HeadOfTerm(factorTerm));
f.value=YAP_IntOfTerm(YAP_HeadOfTerm(YAP_TailOfTerm(factorTerm)));
term1.factors=(factor *)realloc(term1.factors,(j+1)* sizeof(factor));
term1.factors[j]=f;
termTerm=YAP_TailOfTerm(termTerm);
j++;
}
term1.nFact=j;
expression.terms=(term *)realloc(expression.terms,(i+1)* sizeof(term));
expression.terms[i]=term1;
t=YAP_TailOfTerm(t);
i++;
}
expression.nTerms=i;
return(expression);
}
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];
GHashTable * nodes; /* hash table that associates nodes with their probability if already
computed, it is defined in glib */
arg1=YAP_ARG1;
arg2=YAP_ARG2;
arg3=YAP_ARG3;
arg4=YAP_ARG4;
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);
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_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));
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);
printf("%d",CUDD_REORDER_RANDOM_PIVOT);
*/
expression=createExpression(arg2);
expression=createExpression(arg2);
function=retFunction(mgr,expression,vars);
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);
/* 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=g_hash_table_new_full(NULL,NULL,NULL,free);
intBits=sizeof(unsigned int)*8;
prob=Prob(function,vars,nodes);
out=YAP_MkFloatTerm(prob);
g_hash_table_destroy(nodes);
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));
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));
}
void init_my_predicates()
@ -219,17 +214,17 @@ void init_my_predicates()
{
YAP_UserCPredicate("compute_prob",compute_prob,4);
}
FILE *
open_file(char *filename, const char *mode)
FILE * open_file(char *filename, const char *mode)
/* opens a file */
{
FILE *fp;
if ((fp = fopen(filename, mode)) == NULL) {
perror(filename);
exit(1);
}
return fp;
FILE *fp;
if ((fp = fopen(filename, mode)) == NULL)
{
perror(filename);
exit(1);
}
return fp;
}

1559
packages/cplint/doc/bib.bib Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 B

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -31,13 +31,10 @@ fabrizio.riguzzi@unife.it}
\texttt{cplint} is a suite of programs for reasoning with ICL \cite{DBLP:journals/ai/Poole97}, LPADs \cite{VenVer03-TR,VenVer04-ICLP04-IC} and CP-logic programs \cite{VenDenBru-JELIA06,DBLP:journals/tplp/VennekensDB09}. It contains programs both for inference and learning.
\section{Installation}
\texttt{cplint} is distributed in source code in the CVS version of Yap. It includes Prolog and C files. Download it by following the instruction in \url{http://www.ncc.up.pt/~vsc/Yap/downloads.html}.
\texttt{cplint} is distributed in source code in the source code development tree of Yap. It includes Prolog and C files. Download it by following the instruction in \url{http://www.ncc.up.pt/~vsc/Yap/downloads.html}.
\texttt{cplint} requires \href{http://vlsi.colorado.edu/~fabio/CUDD/}{CUDD} and glib-2.0.
\texttt{cplint} requires \href{http://vlsi.colorado.edu/~fabio/CUDD/}{CUDD}.
You can download CUDD from \url{ftp://vlsi.colorado.edu/pub/cudd-2.4.2.tar.gz}.
You can download glib-2.0 (version $\geq 2.0$) from \url{http://www.gtk.org/}. This is a standard GNU package
so it is easy to install it using the package management software of your Linux or Cygwin
distribution.
Compile CUDD:
\begin{enumerate}
@ -441,7 +438,7 @@ The files \texttt{*.uni} that are present for some of the examples are used by
\begin{itemize}
\item CEM (\texttt{cplint} EM): an implementation of EM for learning parameters that is based on \texttt{lpadsld.pl} \cite{RigDiM11-ML-IJ}
\item RIB (Relational Information Bottleneck): an algorithm for learning parameters based on the Information Bottleneck \cite{RigDiM11-ML-IJ}
\item EMBLEM (EM over Bdds for probabilistic Logic programs Efficient Mining): an implementation of EM for learning parameters that computes expectations directly on BDDs \cite{BelRig11-CILC11-NC,BelRig11-TR}
\item EMBLEM (EM over Bdds for probabilistic Logic programs Efficient Mining): an implementation of EM for learning parameters that computes expectations directly on BDDs \cite{BelRig11-IDA,BelRig11-CILC11-NC,BelRig11-TR}
\item SLIPCASE (Structure LearnIng of ProbabilistiC logic progrAmS with Em over bdds): an algorithm for learning the structure of program that is based on EMBLEM \cite{BelRig11-ILP11-IC}
\end{itemize}
@ -526,7 +523,12 @@ for specifying an input variable of type \texttt{<type>}, or
\begin{verbatim}
-<type>
\end{verbatim}
for specifying an output variable of type \texttt{<type>}.
for specifying an output variable of type \texttt{<type>}. or
\begin{verbatim}
<constant>
\end{verbatim}
for specifying a constant.
\subsection{Parameters}
In order to set the algorithms' parameters, you have to insert in \texttt{<stem>.l} commands of the form
\begin{verbatim}
@ -535,31 +537,70 @@ In order to set the algorithms' parameters, you have to insert in \texttt{<stem>
The available parameters are:
\begin{itemize}
\item \verb|depth| (values: integer or \verb|inf|, default value: 3): depth of derivations if \verb|depth_bound| is set to \verb|true|
\item \verb|single_var| (values: \verb|{true,false}|, default value: \verb|false|, valid for CEM only): if set to \verb|true|, there is a random variable for each clauses, instead of a separate random variable for each grounding of a clause
\item \verb|single_var| (values: \verb|{true,false}|, default value: \verb|false|, valid for CEM, EMBLEM and SLIPCASE): if set to \verb|true|, there is a random variable for each clauses, instead of a separate random variable for each grounding of a clause
\item \verb|sample_size| (values: integer, default value: 1000): total number of examples in case in which the models in the \verb|.kb| file contain a \verb|prob(P).| fact. In that case, one model corresponds to \verb|sample_size*P| examples
\item \verb|epsilon_em| (values: real, default value: 0.1, valid for CEM only): if the difference in the log likelihood in two successive EM iteration is smaller
\item \verb|epsilon_em| (values: real, default value: 0.1, valid for CEM, EMBLEM and SLIPCASE): if the difference in the log likelihood in two successive EM iteration is smaller
than \verb|epsilon_em|, then EM stops
\item \verb|epsilon_em_fraction| (values: real, default value: 0.01, valid for CEM only): if the difference in the log likelihood in two successive EM iteration is smaller
\item \verb|epsilon_em_fraction| (values: real, default value: 0.01, valid for CEM, EMBLEM and SLIPCASE): if the difference in the log likelihood in two successive EM iteration is smaller
than \verb|epsilon_em_fraction|*(-current log likelihood), then EM stops
\item \verb|random_restarts_number| (values: integer, default value: 1, valid for CEM only): number of random restarts
\item \verb|iter| (values: integer, defualt value: 1, valid for EMBLEM and SLIPCASE): maximum number of iteration of EM parameter learning. If set to -1, no maximum number of iterations is imposed
\item \verb|iterREF| (values: integer, defualt value: 1, valid for SLIPCASE):
maximum number of iteration of EM parameter learning for refinements. If set to -1, no maximum number of iterations is imposed.
\item \verb|random_restarts_number| (values: integer, default value: 1, valid for CEM, EMBLEM and SLIPCASE): number of random restarts of EM learning
\item \verb|random_restarts_REFnumber| (values: integer, default value: 1, valid for SLIPCASE): number of random restarts of EM learning for refinements
\item \verb|setrand| (values: rand(integer,integer,integer)): seed for the random functions, see Yap manual for allowed values
\item \verb|minimal_step| (values: [0,1], default value: 0.005, valid for RIB only): minimal increment of $\gamma$
\item \verb|maximal_step| (values: [0,1], default value: 0.1, valid for RIB only): maximal increment of $\gamma$
\item \verb|logsize_fraction| (values: [0,1], default value 0.9, valid for RIB only): RIB stops when $\mathbf{I}(CH,T;Y)$ is above \verb|logsize_fraction| times its maximum value ($\log |CH,T|$, see \cite{DBLP:journals/jmlr/ElidanF05})
\item \verb|delta| (values: negative integer, default value -10, valid for RIB only): value assigned to $\log 0$
\item \verb|epsilon_fraction| (values: integer, default value 100, valid for RIB only): in the computation of the step, the value of $\epsilon$ of \cite{DBLP:journals/jmlr/ElidanF05} is obtained as $\log |CH,T|\times$\verb|epsilon_fraction|
\item \verb|max_rules| (values: integer, default value: 6000, valid for RIB only): maximum number of ground rules. Used to set the size of arrays for storing internal statistics. Can be increased as much as memory allows.
\item \verb|minimal_step| (values: [0,1], default value: 0.005, valid for RIB): minimal increment of $\gamma$
\item \verb|maximal_step| (values: [0,1], default value: 0.1, valid for RIB): maximal increment of $\gamma$
\item \verb|logsize_fraction| (values: [0,1], default value 0.9, valid for RIB): RIB stops when $\mathbf{I}(CH,T;Y)$ is above \verb|logsize_fraction| times its maximum value ($\log |CH,T|$, see \cite{DBLP:journals/jmlr/ElidanF05})
\item \verb|delta| (values: negative integer, default value -10, valid for RIB): value assigned to $\log 0$
\item \verb|epsilon_fraction| (values: integer, default value 100, valid for RIB): in the computation of the step, the value of $\epsilon$ of \cite{DBLP:journals/jmlr/ElidanF05} is obtained as $\log |CH,T|\times$\verb|epsilon_fraction|
\item \verb|max_rules| (values: integer, default value: 6000, valid for RIB and SLIPCASE): maximum number of ground rules. Used to set the size of arrays for storing internal statistics. Can be increased as much as memory allows.
\item \verb|logzero| (values: negative real, default value $\log(0.000001)$, valid for SLIPCASE): value assigned to $\log 0$
\item \verb|examples| (values: \verb|atoms|,\verb|interpretations|, default value \verb|atoms|, valid for SLIPCASE): determines how BDDs are built: if set to \verb|interpretations|, a BDD for the conjunction of all the atoms for the target predicates in each interpretations is built.
If set to \verb|atoms|, a BDD is built for the conjunction of a group of atoms for the target predicates in each interpretations. The number of atoms in each group is determined by the parameter \verb|group|
\item \verb|group| (values: integer, default value: 1, valid for SLIPCASE): number of target atoms in the groups that are used to build BDDs
\item \verb|nax_iter| (values: integer, default value: 10, valid for SLIPCASE): number of interations of beam search
\item \verb|max_var| (values: integer, default value: 1, valid for SLIPCASE): maximum number of distinct variables in a clause
\item \verb|verbosity| (values: integer in [1,3], default value: 1): level of verbosity of the algorithms
\item \verb|beamsize| (values: integer, default value: 20, valid for SLIPCASE): size of the beam in SLIPCASE
\end{itemize}
\subsection{Commands}
To execute CEM, load \texttt{em.pl} and call:
To execute CEM, load \texttt{em.pl} with
\begin{verbatim}
?:- use_module(library('cplint/em')).
\end{verbatim}
and call:
\begin{verbatim}
?:- em(stem).
\end{verbatim}
To execute RIB, load \texttt{rib.pl} and call:
To execute RIB, load \texttt{rib.pl} with
\begin{verbatim}
?:- use_module(library('cplint/rib')).
\end{verbatim}
and call:
\begin{verbatim}
?:- ib_par(stem).
\end{verbatim}
To execute EMBLEM, load \texttt{slipcase.pl} with
\begin{verbatim}
?:- use_module(library('cplint/slipcase')).
\end{verbatim}
and call
\begin{verbatim}
?:- em(stem).
\end{verbatim}
To execute SLIPCASE, load \texttt{slipcase.pl} with
\begin{verbatim}
?:- use_module(library('cplint/slipcase')).
\end{verbatim}
and call
\begin{verbatim}
?:- sl(stem).
\end{verbatim}
\section{License}
\label{license}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 724 B

201
packages/cplint/em/Artistic Normal file
View File

@ -0,0 +1,201 @@
The Artistic License 2.0
Copyright (c) 2000-2006, The Perl Foundation.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
This license establishes the terms under which a given free software
Package may be copied, modified, distributed, and/or redistributed.
The intent is that the Copyright Holder maintains some artistic
control over the development of that Package while still keeping the
Package available as open source and free software.
You are always permitted to make arrangements wholly outside of this
license directly with the Copyright Holder of a given Package. If the
terms of this license do not permit the full use that you propose to
make of the Package, you should contact the Copyright Holder and seek
a different licensing arrangement.
Definitions
"Copyright Holder" means the individual(s) or organization(s)
named in the copyright notice for the entire Package.
"Contributor" means any party that has contributed code or other
material to the Package, in accordance with the Copyright Holder's
procedures.
"You" and "your" means any person who would like to copy,
distribute, or modify the Package.
"Package" means the collection of files distributed by the
Copyright Holder, and derivatives of that collection and/or of
those files. A given Package may consist of either the Standard
Version, or a Modified Version.
"Distribute" means providing a copy of the Package or making it
accessible to anyone else, or in the case of a company or
organization, to others outside of your company or organization.
"Distributor Fee" means any fee that you charge for Distributing
this Package or providing support for this Package to another
party. It does not mean licensing fees.
"Standard Version" refers to the Package if it has not been
modified, or has been modified only in ways explicitly requested
by the Copyright Holder.
"Modified Version" means the Package, if it has been changed, and
such changes were not explicitly requested by the Copyright
Holder.
"Original License" means this Artistic License as Distributed with
the Standard Version of the Package, in its current version or as
it may be modified by The Perl Foundation in the future.
"Source" form means the source code, documentation source, and
configuration files for the Package.
"Compiled" form means the compiled bytecode, object code, binary,
or any other form resulting from mechanical transformation or
translation of the Source form.
Permission for Use and Modification Without Distribution
(1) You are permitted to use the Standard Version and create and use
Modified Versions for any purpose without restriction, provided that
you do not Distribute the Modified Version.
Permissions for Redistribution of the Standard Version
(2) You may Distribute verbatim copies of the Source form of the
Standard Version of this Package in any medium without restriction,
either gratis or for a Distributor Fee, provided that you duplicate
all of the original copyright notices and associated disclaimers. At
your discretion, such verbatim copies may or may not include a
Compiled form of the Package.
(3) You may apply any bug fixes, portability changes, and other
modifications made available from the Copyright Holder. The resulting
Package will still be considered the Standard Version, and as such
will be subject to the Original License.
Distribution of Modified Versions of the Package as Source
(4) You may Distribute your Modified Version as Source (either gratis
or for a Distributor Fee, and with or without a Compiled form of the
Modified Version) provided that you clearly document how it differs
from the Standard Version, including, but not limited to, documenting
any non-standard features, executables, or modules, and provided that
you do at least ONE of the following:
(a) make the Modified Version available to the Copyright Holder
of the Standard Version, under the Original License, so that the
Copyright Holder may include your modifications in the Standard
Version.
(b) ensure that installation of your Modified Version does not
prevent the user installing or running the Standard Version. In
addition, the Modified Version must bear a name that is different
from the name of the Standard Version.
(c) allow anyone who receives a copy of the Modified Version to
make the Source form of the Modified Version available to others
under
(i) the Original License or
(ii) a license that permits the licensee to freely copy,
modify and redistribute the Modified Version using the same
licensing terms that apply to the copy that the licensee
received, and requires that the Source form of the Modified
Version, and of any works derived from it, be made freely
available in that license fees are prohibited but Distributor
Fees are allowed.
Distribution of Compiled Forms of the Standard Version
or Modified Versions without the Source
(5) You may Distribute Compiled forms of the Standard Version without
the Source, provided that you include complete instructions on how to
get the Source of the Standard Version. Such instructions must be
valid at the time of your distribution. If these instructions, at any
time while you are carrying out such distribution, become invalid, you
must provide new instructions on demand or cease further distribution.
If you provide valid instructions or cease distribution within thirty
days after you become aware that the instructions are invalid, then
you do not forfeit any of your rights under this license.
(6) You may Distribute a Modified Version in Compiled form without
the Source, provided that you comply with Section 4 with respect to
the Source of the Modified Version.
Aggregating or Linking the Package
(7) You may aggregate the Package (either the Standard Version or
Modified Version) with other packages and Distribute the resulting
aggregation provided that you do not charge a licensing fee for the
Package. Distributor Fees are permitted, and licensing fees for other
components in the aggregation are permitted. The terms of this license
apply to the use and Distribution of the Standard or Modified Versions
as included in the aggregation.
(8) You are permitted to link Modified and Standard Versions with
other works, to embed the Package in a larger work of your own, or to
build stand-alone binary or bytecode versions of applications that
include the Package, and Distribute the result without restriction,
provided the result does not expose a direct interface to the Package.
Items That are Not Considered Part of a Modified Version
(9) Works (including, but not limited to, modules and scripts) that
merely extend or make use of the Package, do not, by themselves, cause
the Package to be a Modified Version. In addition, such works are not
considered parts of the Package itself, and are not subject to the
terms of this license.
General Provisions
(10) Any use, modification, and distribution of the Standard or
Modified Versions is governed by this Artistic License. By using,
modifying or distributing the Package, you accept this license. Do not
use, modify, or distribute the Package, if you do not accept this
license.
(11) If your Modified Version has been derived from a Modified
Version made by someone other than you, you are nevertheless required
to ensure that your Modified Version complies with the requirements of
this license.
(12) This license does not grant you the right to use any trademark,
service mark, tradename, or logo of the Copyright Holder.
(13) This license includes the non-exclusive, worldwide,
free-of-charge patent license to make, have made, use, offer to sell,
sell, import and otherwise transfer the Package with respect to any
patent claims licensable by the Copyright Holder that are necessarily
infringed by the Package. If you institute patent litigation
(including a cross-claim or counterclaim) against any party alleging
that the Package constitutes direct or contributory patent
infringement, then this Artistic License to you shall terminate on the
date that such litigation is filed.
(14) Disclaimer of Warranty:
THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL
LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

23
packages/cplint/em/README Normal file
View File

@ -0,0 +1,23 @@
This folder contains an EM algorithm based on cplint
To execute it, load em.pl with
:- use_module(library('cplint/em')).
and run
:- em(filestem).
in a directory containing filestem.kb, filestem.bg and filestem.l.
You will find the output in filestem.rules.
See the files sa1 and sp1 in the folder as examples (they are the first fold of IMDB for target predicates sameauthor and sameperson)
In the .l file you should specify input and output (taget predicates) in the form
input(pred/arity).
output(pred/arity).
See also http://sites.unife.it/rib/

886
packages/cplint/em/em.pl Normal file
View File

@ -0,0 +1,886 @@
/*
CEM
Copyright (c) 2011, Fabrizio Riguzzi
*/
%:- set_prolog_flag(unknown,error).
%:- set_prolog_flag(profiling,on).
%:- set_prolog_flag(debug,on).
:- set_prolog_flag(discontiguous_warnings,on).
:- set_prolog_flag(single_var_warnings,on).
:- set_prolog_flag(unknown,fail).
%:-source.
%:-yap_flag(gc_trace,very_verbose).
:- use_module(inference,
[find_deriv_inf1/3]).
%:-consult(inference).
:-use_module(library(rbtrees)).
:-use_module(library(random)).
:-use_module(library(avl)).
:-use_module(library(lists)).
%:-use_module(library(lpadsld)).
:-load_foreign_files(['cplint'],[],init_my_predicates).
:-dynamic setting/2,rule/5.
setting(depth,3).
setting(single_var,false).
setting(sample_size,1000).
/* Total number of examples in case in which the models in the kb file contain
a prob(P). fact. In that case, one model corresponds to sample_size*P examples
*/
setting(equivalent_sample_size,100).
/* equivalent samaple size for computing the BD score of the network refinements
It is indicated with NPrime in the formulas on Heckerman, Geiger & Chickering
paper */
setting(epsilon_em,0.1).
setting(epsilon_em_fraction,0.01).
/* if the difference in log likelihood in two successive em iteration is smaller
than epsilon_em, then em stops */
setting(epsilon_sem,2).
setting(random_restarts_number,1).
/* number of random restarts of em */
setting(verbosity,1).
em(File):-
generate_file_names(File,FileKB,FileOut,FileL,FileLPAD),
reconsult(FileL),
load_models(FileKB,DB),
load_initial_model(FileLPAD,Model0),!,
set(verbosity,3),
statistics(cputime,[_,_]),
gen_ex(DB,[],DBE),
compute_parameters_EM(Model0,Model,SuffStats,CLL,DBE),
statistics(cputime,[_,CT]),
CTS is CT/1000,
format("Final CLL ~f~n",[CLL]),
format("Execution time ~f~n",[CTS]),
write_stats(user_output,SuffStats),
listing(setting/2),
format("Model:~n",[]),
write_model(Model,user_output),
open(FileOut,write,Stream),
format(Stream,"/* Final CLL ~f~n",[CLL]),
format(Stream,"Execution time ~f~n",[CTS]),
tell(Stream),
listing(setting/2),
write_stats(Stream,SuffStats),
format(Stream,"*/~n",[]),
write_model(Model,Stream),
told.
gen_ex([],DBE,DBE).
gen_ex([H|T],DB0,DB1):-
get_ouptut_atoms(O),
generate_goal(O,H,[],GL),
append(DB0,GL,DB2),
gen_ex(T,DB2,DB1).
cycle_head([],[],_NR,_S,_NH,_PG,_CSetList,_N):-!.
cycle_head([SSH0|T],[SSH1|T1],NR,S,NH,PG,CSetList,N):-
extract_relevant_C_sets(NR,S,NH,CSetList,CSL1),
(CSL1=[]->
SSH1 is SSH0
;
build_formula(CSL1,Formula,[],Var),
var2numbers(Var,0,NewVar),
compute_prob(NewVar,Formula,Prob,0),
SSH1 is SSH0 +Prob/PG*N
),
NH1 is NH+1,
cycle_head(T,T1,NR,S,NH1,PG,CSetList,N).
cycle_head_neg([],[],_NR,_S,_NH,_NA,_PG,_CSetList,_N):-!.
cycle_head_neg([SSH0|T],[SSH1|T1],NR,S,NH,NA,PG,CSetList,N):-
extract_relevant_C_sets_neg(NR,S,NH,NA,CSetList,CSL1),
(CSL1=[]->
SSH1 is SSH0%+0.001
;
build_formula(CSL1,Formula,[],Var),
var2numbers(Var,0,NewVar),
compute_prob(NewVar,Formula,Prob,0),
(Prob>1 ->write(cyc),write(Prob),write(NewVar),nl;true),
SSH1 is SSH0 +(1-Prob)/PG*N
),
NH1 is NH+1,
cycle_head_neg(T,T1,NR,S,NH1,NA,PG,CSetList,N).
extract_relevant_C_sets_neg(NR,S,NH,NA,CS,CS1):-
neg_choice(0,NA,NH,NR,S,C),
append(CS,C,CS1).
neg_choice(N,N,_NH,_NR,_S,[]):-!.
neg_choice(NH,NA,NH,NR,S,L):-!,
N1 is NH+1,
neg_choice(N1,NA,NH,NR,S,L).
neg_choice(N,NA,NH,NR,S,[[(N,NR,S)]|L]):-
N1 is N+1,
neg_choice(N1,NA,NH,NR,S,L).
extract_relevant_C_sets(_NR,_S,_NH,[],[]):-!.
extract_relevant_C_sets(NR,S,NH,[H|T],CS):-
member((NH1,NR,S),H),!,
extract_relevant_C_sets1(NR,S,NH,NH1,H,T,CS).
extract_relevant_C_sets(NR,S,NH,[H|T],[H1|CS]):-
append(H,[(NH,NR,S)],H1),
extract_relevant_C_sets(NR,S,NH,T,CS).
extract_relevant_C_sets1(NR,S,NH,NH1,_H,T,CS):-
NH1\=NH,!,
extract_relevant_C_sets(NR,S,NH,T,CS).
extract_relevant_C_sets1(NR,S,NH,_NH1,H,T,[H|CS]):-
extract_relevant_C_sets(NR,S,NH,T,CS).
/* EM start */
compute_parameters_EM([],[],SuffStats,-1e200,_DB):-!,
rb_new(SuffStats).
compute_parameters_EM(Model0,Model1,SuffStats1,CLL1,DB):-
setting(verbosity,Ver),
(Ver>0->
format("EM computation ~nInitial model:~n",[]),
write_model(Model0,user_output),
flush_output
;
true
),
(Ver>2->
format("Initial EM Iteration ~n",[]),
flush_output
;
true
),
randomize(Model0,ModelR),
em_iteration(ModelR,Model,SuffStats,CLL,DB),
(Ver>2->
format("CLL ~f~n",[CLL])
;
true
),
flush_output,
setting(random_restarts_number,N),
random_restarts(N,Model,SuffStats,CLL,Model1,SuffStats1,CLL1,DB),
(Ver>0->
format("Final CLL ~f~n",[CLL1]),
flush_output
;
true
).
random_restarts(1,Model,SS,CLL,Model,SS,CLL,_DB):-!.
random_restarts(N,Model0,SS0,CLL0,Model1,SS1,CLL1,DB):-
setting(verbosity,Ver),
(Ver>2->
setting(random_restarts_number,NMax),
Num is NMax-N+1,
format("Restart number ~d~n",[Num]),
flush_output
;
true
),
randomize(Model0,ModelR),
em_iteration(ModelR,ModelR1,SSR,CLLR,DB),
setting(verbosity,Ver),
(Ver>2->
format("CLL ~f~n",[CLLR])
;
true
),
N1 is N-1,
(CLLR>CLL0->
random_restarts(N1,ModelR1,SSR,CLLR,Model1,SS1,CLL1,DB)
;
random_restarts(N1,Model0,SS0,CLL0,Model1,SS1,CLL1,DB)
).
randomize([],[]):-!.
randomize([rule(N,V,NH,HL,BL,LogF)|T],[rule(N,V,NH,HL1,BL,LogF)|T1]):-
length(HL,L),
Int is 1.0/L,
randomize_head(Int,HL,0,HL1),
randomize(T,T1).
randomize_head(_Int,['':_],P,['':PNull1]):-!,
PNull is 1.0-P,
(PNull>=0.0->
PNull1 =PNull
;
PNull1=0.0
).
randomize_head(Int,[H:_|T],P,[H:PH1|NT]):-
PMax is 1.0-P,
random(0,PMax,PH1),
P1 is P+PH1,
randomize_head(Int,T,P1,NT).
em_iteration(Model0,ModelPar,SuffStats1,CLL1,DB):-
compute_CLL_stats(Model0,DB,CLL0,SuffStats0),
/* setting(verbosity,Ver),
(Ver>2->
format("EM Iteration numer ~d~nCLL ~f~n",[N,CLL0]),
write_stats(user_output,SuffStats0)
;
true
),*/
cycle_EM(Model0,SuffStats0,CLL0,ModelPar,SuffStats1,CLL1,DB,1).
cycle_EM(Model0,SuffStats0,CLL0,ModelPar,SuffStats,CLL,DB,N):-
m_step(Model0,SuffStats0,Model1),
compute_CLL_stats(Model1,DB,CLL1,SuffStats1),
setting(verbosity,Ver),
(Ver>2->
format("Iteration: ~d CLL ~f~n",[N,CLL1])
;
true
),
flush_output,
% write_stats(user_output,SuffStats1),
% statistics,
setting(epsilon_em,Epsilon_EM),
setting(epsilon_em_fraction,Epsilon_EM_Frac),
((CLL1-CLL0<Epsilon_EM;(CLL1-CLL0)< - CLL0*Epsilon_EM_Frac)->
ModelPar=Model1,
SuffStats=SuffStats1,
CLL=CLL1,!
;
N1 is N+1,!,
cycle_EM(Model1,SuffStats1,CLL1,ModelPar,SuffStats,CLL,DB,N1)
).
write_stats(S,SS):-
rb_visit(SS,Pairs),
format(S,"Suff stats~n",[]),
write_stats_list(S,Pairs).
write_stats_list(S,[]):-nl(S),nl(S),!.
write_stats_list(S,[R-d(D,N,I)|T]):-
format(S,"~d,~p,~f,~d~n",[R,D,N,I]),
write_stats_list(S,T).
m_step([],_SS,[]):-!.
m_step([rule(N,V,NH,HL,BL,LogF)|T],SS,[rule(N,V,NH,HL1,BL,LogF)|T1]):-
(rb_lookup(N,d(Distr,_NBT,_NI),SS)->
sum_list(Distr,NBT),
update_head(HL,Distr,NBT,HL1)
;
HL1=HL
),
m_step(T,SS,T1).
update_head([],[],_N,[]).
update_head([H:_P|T],[PU|TP],N,[H:P|T1]):-
P is PU/N,
update_head(T,TP,N,T1).
/* EM end */
/* Start of computation of log likelihood and sufficient stats */
compute_CLL_stats(Model,DB,CLL,SuffStats1):-
assert_model(Model),
compute_CLL_stats_examples(DB,CLL,SuffStats1),
retract_model.
assert_model([]):-!.
assert_model([rule(N,V,NH,HL,BL,_LogF)|T]):-
assert_rules(HL,0,HL,BL,NH,N,V),
assertz(rule_by_num(N,V,NH,HL,BL)),
assert_model(T).
retract_model:-
retractall(rule_by_num(_,_,_,_,_)),
retractall(rule(_,_,_,_,_,_,_,_)).
assert_rules([],_Pos,_HL,_BL,_Nh,_N,_V1):-!.
assert_rules(['':_P],_Pos,_HL,_BL,_Nh,_N,_V1):-!.
assert_rules([H:P|T],Pos,HL,BL,NH,N,V1):-
assertz(rule(H,P,Pos,N,V1,NH,HL,BL)),
Pos1 is Pos+1,
assert_rules(T,Pos1,HL,BL,NH,N,V1).
compute_CLL_stats_examples(DB,CLL,SuffStats1):-
rb_new(SuffStats0),
compute_CLL_stats_cplint(DB,0,CLL,SuffStats0,SuffStats1).
get_ouptut_atoms(O):-
findall((A/Ar),output((A/Ar)),O).
generate_goal([],_H,G,G):-!.
generate_goal([P/A|T],H,G0,G1):-
functor(Pred,P,A),
Pred=..[P|Rest],
Pred1=..[P,H|Rest],
findall(Pred1,call(Pred1),L),
findall(\+ Pred1,call(neg(Pred1)),LN),
append(G0,L,G2),
append(G2,LN,G3),
generate_goal(T,H,G3,G1).
compute_CLL_stats_cplint([],CLL,CLL,S,S):-!.
compute_CLL_stats_cplint([\+ H|T],CLL0,CLL1,Stats0,Stats1):-!,
setting(verbosity,V),
(V>3->
write(user_error,(\+ H)),nl(user_error),flush_output
;
true
),
s([H],CL,CSetList,PG),!,
(PG=:=1.0->
CLL2=CLL0,
Stats2=Stats0
;
(prob(H,P)->
setting(sample_size,NTot),
N is P*NTot
;
N=1
),
PG1 is 1-PG,
CLL2 is CLL0+log(PG1)*N,
collect_stats_cplint_neg(CL,PG1,CSetList,N,Stats0,Stats2)
),
compute_CLL_stats_cplint(T,CLL2,CLL1,Stats2,Stats1).
compute_CLL_stats_cplint([H|T],CLL0,CLL1,Stats0,Stats1):-
setting(verbosity,V),
(V>3->
write(user_error,H),nl(user_error),flush_output
;
true
),
s([H],CL,CSetList,PG),!,
(PG=0.0->
CLL2=CLL0,
Stats2=Stats0
;
(prob(H,P)->
setting(sample_size,NTot),
N is P*NTot
;
N=1
),
CLL2 is CLL0+log(PG)*N,
collect_stats_cplint(CL,PG,CSetList,N,Stats0,Stats2)
),
compute_CLL_stats_cplint(T,CLL2,CLL1,Stats2,Stats1).
s(GoalsList,GroundLpad,CSets,Prob):-
solve(GoalsList,GroundLpad,CSets,Prob).
solve(GoalsList,GroundLpad,LDup,Prob):-
setting(depth,D),
findall(Deriv,inference:find_deriv_inf1(GoalsList,D,Deriv),LDup),
(LDup=[]->
Prob=0.0,
GroundLpad=[]
;
append(LDup,L0),
remove_head(L0,L1),
remove_duplicates(L1,L2),
build_ground_lpad(L2,GroundLpad),
build_formula(LDup,Formula,[],Var),
var2numbers(Var,0,NewVar),
compute_prob(NewVar,Formula,Prob,0),
true
).
collect_stats_cplint([],_PG,_CSetList,_N,Stats,Stats):-!.
collect_stats_cplint([(R,S,Head,_Body)|T],PG,CSetList,N,Stats0,Stats1):-
(rb_lookup(R,d(Distr0,N1,NInst1),Stats0)->
cycle_head(Distr0,Distr,R,S,0,PG,CSetList,N),
N2 is N+N1,
rb_update(Stats0,R,d(Distr,N2,NInst1),Stats2)
;
length(Head,LH),
list0(0,LH,Distr0),
cycle_head(Distr0,Distr,R,S,0,PG,CSetList,N),
rb_insert(Stats0,R,d(Distr,N,1),Stats2)
),
collect_stats_cplint(T,PG,CSetList,N,Stats2,Stats1).
collect_stats_cplint_neg([],_PG,_CSetList,_N,Stats,Stats):-!.
collect_stats_cplint_neg([(R,S,Head,_Body)|T],PG,CSetList,N,Stats0,Stats1):-
length(Head,NA),
(rb_lookup(R,d(Distr0,N1,NInst1),Stats0)->
cycle_head_neg(Distr0,Distr,R,S,0,NA,PG,CSetList,N),
N2 is N+N1,
rb_update(Stats0,R,d(Distr,N2,NInst1),Stats2)
;
length(Head,LH),
list0(0,LH,Distr0),
cycle_head_neg(Distr0,Distr,R,S,0,NA,PG,CSetList,N),
rb_insert(Stats0,R,d(Distr,N,1),Stats2)
),
collect_stats_cplint_neg(T,PG,CSetList,N,Stats2,Stats1).
/* build_formula(LC,Formula,VarIn,VarOut) takes as input a set of C sets
LC and a list of Variables VarIn and returns the formula and a new list
of variables VarOut
Formula is of the form [Term1,...,Termn]
Termi is of the form [Factor1,...,Factorm]
Factorj is of the form (Var,Value) where Var is the index of
the multivalued variable Var and Value is the index of the value
*/
build_formula([],[],Var,Var,C,C).
build_formula([D|TD],[F|TF],VarIn,VarOut,C0,C1):-
length(D,NC),
C2 is C0+NC,
build_term(D,F,VarIn,Var1),
build_formula(TD,TF,Var1,VarOut,C2,C1).
build_formula([],[],Var,Var).
build_formula([D|TD],[F|TF],VarIn,VarOut):-
build_term(D,F,VarIn,Var1),
build_formula(TD,TF,Var1,VarOut).
build_term([],[],Var,Var).
build_term([(_,pruned,_)|TC],TF,VarIn,VarOut):-!,
build_term(TC,TF,VarIn,VarOut).
build_term([(N,R,S)|TC],[[NVar,N]|TF],VarIn,VarOut):-
(nth0_eq(0,NVar,VarIn,(R,S))->
Var1=VarIn
;
append(VarIn,[(R,S)],Var1),
length(VarIn,NVar)
),
build_term(TC,TF,Var1,VarOut).
/* nth0_eq(PosIn,PosOut,List,El) takes as input a List,
an element El and an initial position PosIn and returns in PosOut
the position in the List that contains an element exactly equal to El
*/
nth0_eq(N,N,[H|_T],El):-
H==El,!.
nth0_eq(NIn,NOut,[_H|T],El):-
N1 is NIn+1,
nth0_eq(N1,NOut,T,El).
/* var2numbers converts a list of couples (Rule,Substitution) into a list
of triples (N,NumberOfHeadsAtoms,ListOfProbabilities), where N is an integer
starting from 0 */
var2numbers([],_N,[]).
var2numbers([(R,S)|T],N,[[N,ValNumber,Probs]|TNV]):-
find_probs(R,S,Probs),
length(Probs,ValNumber),
N1 is N+1,
var2numbers(T,N1,TNV).
find_probs(R,S,Probs):-
rule_by_num(R,S,_N,Head,_Body),
get_probs(Head,Probs).
get_probs(uniform(_A:1/Num,_P,_Number),ListP):-
Prob is 1/Num,
list_el(Num,Prob,ListP).
get_probs([],[]).
get_probs([_H:P|T],[P1|T1]):-
P1 is P,
get_probs(T,T1).
list_el(0,_P,[]):-!.
list_el(N,P,[P|T]):-
N1 is N-1,
list_el(N1,P,T).
sum(_NS,[],[],[]):-!.
sum(NS,[H0|T0],[H1|T1],[H2|T2]):-
H2 is H0+H1*NS,
sum(NS,T0,T1,T2).
times(_NS,[],[]):-!.
times(NS,[H0|T0],[H1|T1]):-
H1 is H0*NS,
times(NS,T0,T1).
/* End of computation of log likelihood and sufficient stats */
/* Utility predicates */
generate_file_names(File,FileKB,FileOut,FileL,FileLPAD):-
generate_file_name(File,".kb",FileKB),
generate_file_name(File,".rules",FileOut),
generate_file_name(File,".cpl",FileLPAD),
generate_file_name(File,".l",FileL).
generate_file_name(File,Ext,FileExt):-
name(File,FileString),
append(FileString,Ext,FileStringExt),
name(FileExt,FileStringExt).
set(Parameter,Value):-
retract(setting(Parameter,_)),
assert(setting(Parameter,Value)).
load_initial_model(File,Model):-
open(File,read,S),
read_clauses(S,C),
close(S),
process_clauses(C,1,_N,[],Model).
process_clauses([(end_of_file,[])],N,N,Model,Model).
process_clauses([((H:-B),_V)|T],N,N2,Model0,Model1):-
H=(db(A)),!,
assert((A:-B)),
process_clauses(T,N,N2,Model0,Model1).
process_clauses([((H:-B),V)|T],N,N2,Model0,[rule(N,V1,NH,HL,BL,0)|Model1]):-
H=(_;_),!,
list2or(HL1,H),
process_head(HL1,HL,VI),
list2and(BL0,B),
add_int_atom(BL0,BL,VI),
length(HL,LH),
listN(0,LH,NH),
N1 is N+1,
(setting(single_var,true)->
V1=[]
;
V1=V
),
% assertz(rule(N,V,NH,HL,BL)),
process_clauses(T,N1,N2,Model0,Model1).
process_clauses([((H:-B),V)|T],N,N2,Model0,[rule(N,V1,NH,HL,BL,0)|Model1]):-
H=(_:_),!,
list2or(HL1,H),
process_head(HL1,HL,VI),
list2and(BL0,B),
add_int_atom(BL0,BL,VI),
length(HL,LH),
listN(0,LH,NH),
(setting(single_var,true)->
V1=[]
;
V1=V
),
N1 is N+1,
% assertz(rule(N,V1,NH,HL,BL)),
process_clauses(T,N1,N2,Model0,Model1).
process_clauses([((H:-B),V)|T],N,N2,Model0,[rule(N,V1,NH,HL,BL,0)|Model1]):-!,
process_head([H:1.0],HL,VI),
list2and(BL0,B),
add_int_atom(BL0,BL,VI),
length(HL,LH),
listN(0,LH,NH),
(setting(single_var,true)->
V1=[]
;
V1=V
),
N1 is N+1,
% assertz(rule(N,V1,NH,HL,BL)),
process_clauses(T,N1,N2,Model0,Model1).
process_clauses([(H,V)|T],N,N2,Model0,[rule(N,V1,NH,HL,[],0)|Model1]):-
H=(_;_),!,
list2or(HL1,H),
process_head(HL1,HL,_VI),
length(HL,LH),
listN(0,LH,NH),
(setting(single_var,true)->
V1=[]
;
V1=V
),
N1 is N+1,
% assertz(rule(N,V,NH,HL,[])),
process_clauses(T,N1,N2,Model0,Model1).
process_clauses([(H,V)|T],N,N2,Model0,[rule(N,V1,NH,HL,[],0)|Model1]):-
H=(_:_),!,
list2or(HL1,H),
process_head(HL1,HL,_VI),
length(HL,LH),
listN(0,LH,NH),
(setting(single_var,true)->
V1=[]
;
V1=V
),
N1 is N+1,
% assertz(rule(N,V,NH,HL,[])),
process_clauses(T,N1,N2,Model0,Model1).
process_clauses([(H,V)|T],N,N2,Model0,[rule(N,V1,NH,HL,[],0)|Model1]):-
process_head([H:1.0],HL,_VI),
length(HL,LH),
listN(0,LH,NH),
(setting(single_var,true)->
V1=[]
;
V1=V
),
N1 is N+1,
% assertz(rule(N,V,NH,HL,[])),
process_clauses(T,N1,N2,Model0,Model1).
/* if the annotation in the head are not ground, the null atom is not added
and the eventual formulas are not evaluated */
process_head([H:P|T],NHL,VI):-!,
process_head_prob([H:P|T],0.0,NHL,VI).
process_head(HL,NHL,VI):-
process_head_random(HL,0.0,NHL,VI).
process_head_random([],P,['':PNull1],_VI):-
PNull is 1.0-P,
(PNull>=0.0->
PNull1 =PNull
;
PNull1=0.0
).
process_head_random([H|T],P,[H1:PH1|NT],VI):-
add_int_atom([H],[H1],VI),
PMax is 1.0-P,
random(0,PMax,PH1),
P1 is P+PH1,
process_head_random(T,P1,NT,VI).
process_head_prob([H:PH],P,[H1:PH1,'':PNull1],VI):-
add_int_atom([H],[H1],VI),
PH1 is PH,
PNull is 1.0-P-PH1,
(PNull>=0.0->
PNull1 =PNull
;
PNull1=0.0
).
process_head_prob([H:PH|T],P,[H1:PH1|NT],VI):-
add_int_atom([H],[H1],VI),
PH1 is PH,
P1 is P+PH1,
process_head_prob(T,P1,NT,VI).
add_int_atom([],[],_VI).
add_int_atom([\+ H|T],[\+ H|T1],VI):-
inference:builtin(H),!,
add_int_atom(T,T1,VI).
add_int_atom([\+ H|T],[\+ H1|T1],VI):-!,
H=..[F|Args],
H1=..[F,VI|Args],
add_int_atom(T,T1,VI).
add_int_atom([H|T],[H|T1],VI):-
inference:builtin(H),!,
add_int_atom(T,T1,VI).
add_int_atom([H|T],[H1|T1],VI):-
H=..[F|Args],
H1=..[F,VI|Args],
add_int_atom(T,T1,VI).
/* predicates for reading in the program clauses */
read_clauses(S,Clauses):-
read_clauses_ground_body(S,Clauses).
read_clauses_ground_body(S,[(Cl,V)|Out]):-
read_term(S,Cl,[variable_names(V)]),
(Cl=end_of_file->
Out=[]
;
read_clauses_ground_body(S,Out)
).
listN(N,N,[]):-!.
listN(NIn,N,[NIn|T]):-
N1 is NIn+1,
listN(N1,N,T).
list0(N,N,[]):-!.
list0(NIn,N,[0|T]):-
N1 is NIn+1,
list0(N1,N,T).
/* end of predicates for parsing an input file containing a program */
load_models(File,ModulesList):-
open(File,read,Stream),
read_models(Stream,ModulesList),
close(Stream).
read_models(Stream,[Name1|Names]):-
read(Stream,begin(model(Name))),!,
(number(Name)->
name(Name,NameStr),
append("i",NameStr,Name1Str),
name(Name1,Name1Str)
;
Name1=Name
),
read_all_atoms(Stream,Name1),
read_models(Stream,Names).
read_models(_S,[]).
read_all_atoms(Stream,Name):-
read(Stream,At),
At \=end(model(_Name)),!,
(At=neg(Atom)->
Atom=..[Pred|Args],
Atom1=..[Pred,Name|Args],
assertz(neg(Atom1))
;
At=..[Pred|Args],
Atom1=..[Pred,Name|Args],
assertz(Atom1)
),
read_all_atoms(Stream,Name).
read_all_atoms(_S,_N).
list2or([],true):-!.
list2or([X],X):-
X\=;(_,_),!.
list2or([H|T],(H ; Ta)):-!,
list2or(T,Ta).
list2and([],true):-!.
list2and([X],X):-
X\=(_,_),!.
list2and([H|T],(H,Ta)):-!,
list2and(T,Ta).
write_model([],_Stream):-!.
write_model([rule(_N,_V,_NH,HL,BL,_LogF)|Rest],Stream):-
copy_term((HL,BL),(HL1,BL1)),
numbervars((HL1,BL1),0,_M),
write_disj_clause(Stream,(HL1:-BL1)),
format(Stream,".~n~n",[]),
write_model(Rest,Stream).
write_disj_clause(S,(H:-[])):-!,
write_head(S,H).
write_disj_clause(S,(H:-B)):-
write_head(S,H),
write(S,' :-'),
nl(S),
write_body(S,B).
write_head(S,[A:1.0|_Rest]):-!,
remove_int_atom(A,A1),
format(S,"~p",[A1]).
write_head(S,[A:P,'':_P]):-!,
remove_int_atom(A,A1),
format(S,"~p:~f",[A1,P]).
write_head(S,[A:P|Rest]):-
remove_int_atom(A,A1),
format(S,"~p:~f ; ",[A1,P]),
write_head(S,Rest).
write_body(S,[\+ A]):-!,
remove_int_atom(A,A1),
format(S,"\t\\+ ~p",[A1]).
write_body(S,[A]):-!,
remove_int_atom(A,A1),
format(S,"\t~p",[A1]).
write_body(S,[\+ A|T]):-!,
remove_int_atom(A,A1),
format(S,"\t\\+ ~p,~n",[A1]),
write_body(S,T).
write_body(S,[A|T]):-
remove_int_atom(A,A1),
format(S,"\t~p,~n",[A1]),
write_body(S,T).
remove_int_atom(A,A1):-
A=..[F,_|T],
A1=..[F|T].
build_ground_lpad([],[]):-!.
build_ground_lpad([(R,S)|T],[(R,S,Head,Body)|T1]):-
user:rule_by_num(R,S,_,Head,Body),
build_ground_lpad(T,T1).
remove_head([],[]).
remove_head([(_N,R,S)|T],[(R,S)|T1]):-
remove_head(T,T1).
append_all([],L,L):-!.
append_all([LIntH|IntT],IntIn,IntOut):-
append(IntIn,LIntH,Int1),
append_all(IntT,Int1,IntOut).

View File

@ -0,0 +1,361 @@
/*
CEM
Copyright (c) 2011, Fabrizio Riguzzi
*/
:- module(inference,[find_deriv_inf1/3]).
:-multifile setting/2.
:-dynamic setting/2.
%:-load_foreign_files(['cplint'],[],init_my_predicates).
:-use_module(library(lists)).
:-use_module(library(avl)).
/* start of list of parameters that can be set by the user with
set(Parameter,Value) */
setting(epsilon_parsing,0.00001).
setting(save_dot,false).
setting(ground_body,true).
/* available values: true, false
if true, both the head and the body of each clause will be grounded, otherwise
only the head is grounded. In the case in which the body contains variables
not appearing in the head, the body represents an existential event */
/* end of list of parameters */
/* s(GoalsLIst,Prob) compute the probability of a list of goals
GoalsLis can have variables, s returns in backtracking all the solutions with their
corresponding probability */
find_deriv_inf1(GoalsList,DB,Deriv):-
solve1(GoalsList,DB,[],Deriv).
find_deriv_inf(GoalsList,D,Deriv):-
solve(GoalsList,D,[],Deriv).
/* duplicate can appear in the C set because two different unistantiated clauses may become the
same clause when instantiated */
solve([],_D,C,C):-!.
solve([\+ H|T],DB,CIn,COut):-
functor(H,F,Args),
Args1 is Args-1,
user:input_cw(F/Args1),!,
call(user:neg(H)),
solve(T,DB,CIn,COut).
solve([\+ H |T],DB,CIn,COut):-!,
DB > 0,
DB1 is DB-1,
list2and(HL,H),
functor(H,F,Args),
Args1 is Args-1,
(user:input(F/Args1)->
call(user:neg(H))
;
\+ H
),
(setof(D,find_deriv_inf(HL,DB1,D),L)->
choose_clauses(CIn,L,C1),
solve(T,DB1,C1,COut)
;
solve(T,DB1,CIn,COut)
).
solve([H|T],D,CIn,COut):-
builtin(H),!,
call(H),
solve(T,D,CIn,COut).
solve([H|T],DB,CIn,COut):-
user:db(H),!,
call(user:H),
solve(T,DB,CIn,COut).
solve([H|T],DB,CIn,COut):-
functor(H,F,Args),
Args1 is Args-1,
user:input_cw(F/Args1),!,
call(user:H),
solve(T,DB,CIn,COut).
solve([H|T],D,CIn,COut):-
functor(H,F,Args),
Args1 is Args-1,
user:input(F/Args1),
call(user:H),
solve(T,D,CIn,COut).
solve([H|T],D,CIn,COut):-
user:def_rule(H,B),
append(B,T,NG),
solve(NG,D,CIn,COut).
solve([H|T],D,CIn,COut):-
D>=1,
find_rule(H,(R,S,N),B,CIn),
solve_pres(R,S,N,B,T,D,CIn,COut).
solve_pres(R,S,N,B,T,D,CIn,COut):-
member_eq((N,R,S),CIn),!,
append(B,T,NG),
D1 is D-1,
solve(NG,D1,CIn,COut).
solve_pres(R,S,N,B,T,D,CIn,COut):-
append(CIn,[(N,R,S)],C1),
append(B,T,NG),
D1 is D-1,
solve(NG,D1,C1,COut).
solve1([],_D,C,C):-!.
solve1([\+ H |T],DB,CIn,COut):-!,
DB>=1,
list2and(HL,H),
findall(D,find_deriv_inf1(HL,DB,D),L),
choose_clauses(CIn,L,C1),
solve(T,C1,COut).
solve1([H|T],D,CIn,COut):-
user:def_rule(H,B),
append(B,T,NG),
solve(NG,D,CIn,COut).
solve1([H|T],D,CIn,COut):-
D>=1,
find_rule(H,(R,S,N),B,CIn),
solve_pres(R,S,N,B,T,D,CIn,COut).
not_present_with_a_different_head(_N,_R,_S,[]).
not_present_with_a_different_head(N,R,S,[(N,R,S)|T]):-!,
not_present_with_a_different_head(N,R,S,T).
not_present_with_a_different_head(N,R,S,[(_N1,R,S1)|T]):-
S\=S1,!,
not_present_with_a_different_head(N,R,S,T).
not_present_with_a_different_head(N,R,S,[(_N1,R1,_S1)|T]):-
R\=R1,
not_present_with_a_different_head(N,R,S,T).
/* find_rule(G,(R,S,N),Body,C) takes a goal G and the current C set and
returns the index R of a disjunctive rule resolving with G together with
the index N of the resolving head, the substitution S and the Body of the
rule */
find_rule(H,(R,S,N),Body,C):-
user:rule(H,_P,N,R,S,_,_Head,Body),
not_already_present_with_a_different_head(N,R,S,C).
not_already_present_with_a_different_head(_N,_R,_S,[]).
not_already_present_with_a_different_head(N,R,S,[(N1,R,S1)|T]):-
not_different(N,N1,S,S1),!,
not_already_present_with_a_different_head(N,R,S,T).
not_already_present_with_a_different_head(N,R,S,[(_N1,R1,_S1)|T]):-
R\==R1,
not_already_present_with_a_different_head(N,R,S,T).
not_different(_N,_N1,S,S1):-
S\=S1,!.
not_different(N,N1,S,S1):-
N\=N1,!,
dif(S,S1).
not_different(N,N,S,S).
member_head(H,[(H:_P)|_T],N,N).
member_head(H,[(_H:_P)|T],NIn,NOut):-
N1 is NIn+1,
member_head(H,T,N1,NOut).
/* choose_clauses(CIn,LC,COut) takes as input the current C set and
the set of C sets for a negative goal and returns a new C set that
excludes all the derivations for the negative goals */
choose_clauses(C,[],C).
choose_clauses(CIn,[D|T],COut):-
member((N,R,S),D),
already_present_with_a_different_head(N,R,S,CIn),!,
choose_a_head(N,R,S,CIn,C1),
choose_clauses(C1,T,COut).
choose_clauses(CIn,[D|T],COut):-
member((N,R,S),D),
new_head(N,R,S,N1),
\+ already_present(N1,R,S,CIn),
impose_dif_cons(R,S,CIn),
choose_clauses([(N1,R,S)|CIn],T,COut).
impose_dif_cons(_R,_S,[]):-!.
impose_dif_cons(R,S,[(_NH,R,SH)|T]):-!,
dif(S,SH),
impose_dif_cons(R,S,T).
impose_dif_cons(R,S,[_H|T]):-
impose_dif_cons(R,S,T).
/* instantiation_present_with_the_same_head(N,R,S,C)
takes rule R with substitution S and selected head N and a C set
and asserts dif constraints for all the clauses in C of which RS
is an instantitation and have the same head selected */
instantiation_present_with_the_same_head(_N,_R,_S,[]).
instantiation_present_with_the_same_head(N,R,S,[(NH,R,SH)|T]):-
\+ \+ S=SH,!,
dif_head_or_subs(N,R,S,NH,SH,T).
instantiation_present_with_the_same_head(N,R,S,[_H|T]):-
instantiation_present_with_the_same_head(N,R,S,T).
dif_head_or_subs(N,R,S,NH,_SH,T):-
dif(N,NH),
instantiation_present_with_the_same_head(N,R,S,T).
dif_head_or_subs(N,R,S,N,SH,T):-
dif(S,SH),
instantiation_present_with_the_same_head(N,R,S,T).
/* case 1 of Select: a more general rule is present in C with
a different head, instantiate it */
choose_a_head(N,R,S,[(NH,R,SH)|T],[(NH,R,SH)|T]):-
S=SH,
dif(N,NH).
/* case 2 of Select: a more general rule is present in C with
a different head, ensure that they do not generate the same
ground clause */
choose_a_head(N,R,S,[(NH,R,SH)|T],[(NH,R,S),(NH,R,SH)|T]):-
\+ \+ S=SH, S\==SH,
dif(N,NH),
dif(S,SH).
choose_a_head(N,R,S,[H|T],[H|T1]):-
choose_a_head(N,R,S,T,T1).
/* select a head different from N for rule R with
substitution S, return it in N1 */
new_head(N,R,S,N1):-
user:rule_by_num(R,S,Numbers,Head,_Body),
Head\=uniform(_,_,_),!,
nth0(N, Numbers, _Elem, Rest),
member(N1,Rest).
already_present_with_a_different_head(N,R,S,[(NH,R,SH)|_T]):-
\+ \+ S=SH,NH \= N.
already_present_with_a_different_head(N,R,S,[_H|T]):-
already_present_with_a_different_head(N,R,S,T).
/* checks that a rule R with head N and selection S is already
present in C (or a generalization of it is in C) */
already_present(N,R,S,[(N,R,SH)|_T]):-
S=SH.
already_present(N,R,S,[_H|T]):-
already_present(N,R,S,T).
/* rem_dup_lists removes the C sets that are a superset of
another C sets further on in the list of C sets */
/* rem_dup_lists removes the C sets that are a superset of
another C sets further on in the list of C sets */
rem_dup_lists([],L,L).
rem_dup_lists([H|T],L0,L):-
(member_subset(H,T);member_subset(H,L0)),!,
rem_dup_lists(T,L0,L).
rem_dup_lists([H|T],L0,L):-
rem_dup_lists(T,[H|L0],L).
member_subset(E,[H|_T]):-
subset_my(H,E),!.
member_subset(E,[_H|T]):-
member_subset(E,T).
member_eq(A,[H|_T]):-
A==H,!.
member_eq(A,[_H|T]):-
member_eq(A,T).
subset_my([],_).
subset_my([H|T],L):-
member_eq(H,L),
subset_my(T,L).
remove_duplicates_eq([],[]).
remove_duplicates_eq([H|T],T1):-
member_eq(H,T),!,
remove_duplicates_eq(T,T1).
remove_duplicates_eq([H|T],[H|T1]):-
remove_duplicates_eq(T,T1).
builtin(_A is _B).
builtin(_A > _B).
builtin(_A < _B).
builtin(_A >= _B).
builtin(_A =< _B).
builtin(_A =:= _B).
builtin(_A =\= _B).
builtin(true).
builtin(false).
builtin(_A = _B).
builtin(_A==_B).
builtin(_A\=_B).
builtin(_A\==_B).
builtin(length(_L,_N)).
builtin(member(_El,_L)).
builtin(average(_L,_Av)).
builtin(max_list(_L,_Max)).
builtin(min_list(_L,_Max)).
builtin(nth0(_,_,_)).
builtin(nth(_,_,_)).
average(L,Av):-
sum_list(L,Sum),
length(L,N),
Av is Sum/N.
list2or([],true):-!.
list2or([X],X):-
X\=;(_,_),!.
list2or([H|T],(H ; Ta)):-!,
list2or(T,Ta).
list2and([],true):-!.
list2and([X],X):-
X\=(_,_),!.
list2and([H|T],(H,Ta)):-!,
list2and(T,Ta).

102
packages/cplint/em/sa1.cpl Normal file
View File

@ -0,0 +1,102 @@
sameauthor(A,B) :0.3 :- haswordauthor(A,word_a), haswordauthor(B,word_a).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_andrzej), haswordauthor(B,word_andrzej).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_auer), haswordauthor(B,word_auer).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_avrim), haswordauthor(B,word_avrim).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_bauer), haswordauthor(B,word_bauer).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_bianchi), haswordauthor(B,word_bianchi).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_blum), haswordauthor(B,word_blum).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_cesa), haswordauthor(B,word_cesa).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_cohen), haswordauthor(B,word_cohen).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_d), haswordauthor(B,word_d).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_david), haswordauthor(B,word_david).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_dietterich), haswordauthor(B,word_dietterich).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_druker), haswordauthor(B,word_druker).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_e), haswordauthor(B,word_e).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_ehrenfeucht), haswordauthor(B,word_ehrenfeucht).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_eric), haswordauthor(B,word_eric).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_feder), haswordauthor(B,word_feder).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_freund), haswordauthor(B,word_freund).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_fyoav), haswordauthor(B,word_fyoav).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_g), haswordauthor(B,word_g).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_goldman), haswordauthor(B,word_goldman).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_h), haswordauthor(B,word_h).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_harris), haswordauthor(B,word_harris).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_haussler), haswordauthor(B,word_haussler).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_helmbold), haswordauthor(B,word_helmbold).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_henry), haswordauthor(B,word_henry).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_j), haswordauthor(B,word_j).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_kautz), haswordauthor(B,word_kautz).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_kearns), haswordauthor(B,word_kearns).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_l), haswordauthor(B,word_l).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_lewis), haswordauthor(B,word_lewis).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_littlestone), haswordauthor(B,word_littlestone).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_m), haswordauthor(B,word_m).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_micahel), haswordauthor(B,word_micahel).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_michael), haswordauthor(B,word_michael).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_n), haswordauthor(B,word_n).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_nicolo), haswordauthor(B,word_nicolo).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_p), haswordauthor(B,word_p).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_r), haswordauthor(B,word_r).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_rivest), haswordauthor(B,word_rivest).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_robert), haswordauthor(B,word_robert).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_ronald), haswordauthor(B,word_ronald).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_s), haswordauthor(B,word_s).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_sally), haswordauthor(B,word_sally).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_shapire), haswordauthor(B,word_shapire).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_t), haswordauthor(B,word_t).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_tom), haswordauthor(B,word_tom).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_v), haswordauthor(B,word_v).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_w), haswordauthor(B,word_w).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_william), haswordauthor(B,word_william).
sameauthor(A,B) :0.3 :- haswordauthor(A,word_y), haswordauthor(B,word_y).

57926
packages/cplint/em/sa1.kb Normal file

File diff suppressed because it is too large Load Diff

17
packages/cplint/em/sa1.l Normal file
View File

@ -0,0 +1,17 @@
output(sameauthor/2).
input(sametitle/2).
input(samevenue/2).
input(author/2).
input(title/2).
input(venue/2).
input(haswordauthor/2).
input(haswordtitle/2).
input(haswordvenue/2).

View File

@ -0,0 +1,268 @@
/* Final CLL -6.445209
Execution time 39.202000
setting(depth_bound,true).
setting(depth,inf).
setting(single_var,true).
setting(sample_size,1000).
setting(equivalent_sample_size,100).
setting(epsilon_em,0.1).
setting(epsilon_em_fraction,0.01).
setting(epsilon_sem,2).
setting(random_restarts_number,1).
setting(verbosity,3).
Suff stats
1,[23.4908206261811,23.5091793738189],47.000000,1
2,[3.39086467470101,0.609135325298986],4.000000,1
3,[0.999999094618424,9.05381576345798e-07],1.000000,1
4,[0.768367876737893,0.231632123262107],1.000000,1
5,[1.0,0.0],1.000000,1
6,[2.84828911045281,10.1517108895472],13.000000,1
7,[3.99999999998435,1.56503698889962e-11],4.000000,1
8,[12.983279534159,0.0167204658409756],13.000000,1
10,[0.152431257895486,73.8475687421045],74.000000,1
11,[0.00870788648018717,20.9912921135198],21.000000,1
12,[24.9999999999997,3.33399974294944e-13],25.000000,1
13,[15.9999980910651,1.90893491658722e-06],16.000000,1
14,[25.4554988733455,0.54450112665451],26.000000,1
15,[19.9999999760067,2.39933335687255e-08],20.000000,1
16,[0.129766303992798,0.870233696007202],1.000000,1
17,[0.986807293994297,0.0131927060057027],1.000000,1
18,[50.0,0.0],50.000000,1
19,[2.1618207056,4.8381792944],7.000000,1
20,[2.45227441607431,1.54772558392569],4.000000,1
21,[40.9926219216609,0.00737807833914405],41.000000,1
22,[18.0241407877925,1.97585921220751],20.000000,1
23,[3.8141749972431,0.185825002756899],4.000000,1
24,[33.0,0.0],33.000000,1
25,[36.9999999149436,8.5056436989299e-08],37.000000,1
26,[7.9214828182491,1.0785171817509],9.000000,1
27,[0.599995367350068,30.4000046326499],31.000000,1
28,[48.999894203847,0.000105796152974403],49.000000,1
29,[141.999997152113,2.84788728515184e-06],142.000000,1
30,[15.3660357895144,9.63396421048556],25.000000,1
31,[36.0,0.0],36.000000,1
32,[0.551614828005467,0.448385171994533],1.000000,1
33,[49.988295006549,8.011704993451],58.000000,1
34,[0.381351770285117,0.618648229714883],1.000000,1
35,[12.0636240733763,0.936375926623662],13.000000,1
36,[8.92658418455131,0.073415815448688],9.000000,1
37,[0.964298970277856,0.0357010297221435],1.000000,1
38,[7.19128052926487,21.8087194707351],29.000000,1
39,[26.0670114688815,44.9329885311185],71.000000,1
40,[47.9999512860607,4.87139392888923e-05],48.000000,1
41,[4.76277270190422,4.23722729809578],9.000000,1
42,[1.24513461973704,3.75486538026296],5.000000,1
43,[19.4361776045177,2.56382239548234],22.000000,1
44,[2.25940297799937,0.740597022000632],3.000000,1
45,[81.9999280086304,7.19913695467996e-05],82.000000,1
46,[2.15974474331903,13.840255256681],16.000000,1
47,[0.95955961688869,0.0404403831113106],1.000000,1
48,[0.159066986286089,0.840933013713911],1.000000,1
51,[1.49504948167805,14.5049505183219],16.000000,1
*/
sameauthor(B,C):0.499788 :-
haswordauthor(B,word_a),
haswordauthor(C,word_a).
sameauthor(B,C):0.847716 :-
haswordauthor(B,word_andrzej),
haswordauthor(C,word_andrzej).
sameauthor(B,C):0.999997 :-
haswordauthor(B,word_auer),
haswordauthor(C,word_auer).
sameauthor(B,C):0.768368 :-
haswordauthor(B,word_avrim),
haswordauthor(C,word_avrim).
sameauthor(B,C) :-
haswordauthor(B,word_bauer),
haswordauthor(C,word_bauer).
sameauthor(B,C):0.219007 :-
haswordauthor(B,word_bianchi),
haswordauthor(C,word_bianchi).
sameauthor(B,C):1.000000 :-
haswordauthor(B,word_blum),
haswordauthor(C,word_blum).
sameauthor(B,C):0.998293 :-
haswordauthor(B,word_cesa),
haswordauthor(C,word_cesa).
sameauthor(B,C):0.905260 :-
haswordauthor(B,word_cohen),
haswordauthor(C,word_cohen).
sameauthor(B,C):0.002722 :-
haswordauthor(B,word_d),
haswordauthor(C,word_d).
sameauthor(B,C):0.000581 :-
haswordauthor(B,word_david),
haswordauthor(C,word_david).
sameauthor(B,C):1.000000 :-
haswordauthor(B,word_dietterich),
haswordauthor(C,word_dietterich).
sameauthor(B,C):1.000000 :-
haswordauthor(B,word_druker),
haswordauthor(C,word_druker).
sameauthor(B,C):0.979058 :-
haswordauthor(B,word_e),
haswordauthor(C,word_e).
sameauthor(B,C):1.000000 :-
haswordauthor(B,word_ehrenfeucht),
haswordauthor(C,word_ehrenfeucht).
sameauthor(B,C):0.129766 :-
haswordauthor(B,word_eric),
haswordauthor(C,word_eric).
sameauthor(B,C):0.984725 :-
haswordauthor(B,word_feder),
haswordauthor(C,word_feder).
sameauthor(B,C) :-
haswordauthor(B,word_freund),
haswordauthor(C,word_freund).
sameauthor(B,C):0.308832 :-
haswordauthor(B,word_fyoav),
haswordauthor(C,word_fyoav).
sameauthor(B,C):0.613069 :-
haswordauthor(B,word_g),
haswordauthor(C,word_g).
sameauthor(B,C):0.999733 :-
haswordauthor(B,word_goldman),
haswordauthor(C,word_goldman).
sameauthor(B,C):0.901207 :-
haswordauthor(B,word_h),
haswordauthor(C,word_h).
sameauthor(B,C):0.953544 :-
haswordauthor(B,word_harris),
haswordauthor(C,word_harris).
sameauthor(B,C) :-
haswordauthor(B,word_haussler),
haswordauthor(C,word_haussler).
sameauthor(B,C):1.000000 :-
haswordauthor(B,word_helmbold),
haswordauthor(C,word_helmbold).
sameauthor(B,C):0.880164 :-
haswordauthor(B,word_henry),
haswordauthor(C,word_henry).
sameauthor(B,C):0.019355 :-
haswordauthor(B,word_j),
haswordauthor(C,word_j).
sameauthor(B,C):0.999996 :-
haswordauthor(B,word_kautz),
haswordauthor(C,word_kautz).
sameauthor(B,C):1.000000 :-
haswordauthor(B,word_kearns),
haswordauthor(C,word_kearns).
sameauthor(B,C):0.614641 :-
haswordauthor(B,word_l),
haswordauthor(C,word_l).
sameauthor(B,C) :-
haswordauthor(B,word_lewis),
haswordauthor(C,word_lewis).
sameauthor(B,C):0.549479 :-
haswordauthor(B,word_littlestone),
haswordauthor(C,word_littlestone).
sameauthor(B,C):0.861836 :-
haswordauthor(B,word_m),
haswordauthor(C,word_m).
sameauthor(B,C):0.381352 :-
haswordauthor(B,word_micahel),
haswordauthor(C,word_micahel).
sameauthor(B,C):0.927971 :-
haswordauthor(B,word_michael),
haswordauthor(C,word_michael).
sameauthor(B,C):0.991404 :-
haswordauthor(B,word_n),
haswordauthor(C,word_n).
sameauthor(B,C):0.964253 :-
haswordauthor(B,word_nicolo),
haswordauthor(C,word_nicolo).
sameauthor(B,C):0.266344 :-
haswordauthor(B,word_p),
haswordauthor(C,word_p).
sameauthor(B,C):0.372048 :-
haswordauthor(B,word_r),
haswordauthor(C,word_r).
sameauthor(B,C):0.999998 :-
haswordauthor(B,word_rivest),
haswordauthor(C,word_rivest).
sameauthor(B,C):0.529197 :-
haswordauthor(B,word_robert),
haswordauthor(C,word_robert).
sameauthor(B,C):0.249027 :-
haswordauthor(B,word_ronald),
haswordauthor(C,word_ronald).
sameauthor(B,C):0.883441 :-
haswordauthor(B,word_s),
haswordauthor(C,word_s).
sameauthor(B,C):0.753109 :-
haswordauthor(B,word_sally),
haswordauthor(C,word_sally).
sameauthor(B,C):0.999998 :-
haswordauthor(B,word_shapire),
haswordauthor(C,word_shapire).
sameauthor(B,C):0.134984 :-
haswordauthor(B,word_t),
haswordauthor(C,word_t).
sameauthor(B,C):0.959560 :-
haswordauthor(B,word_tom),
haswordauthor(C,word_tom).
sameauthor(B,C):0.159067 :-
haswordauthor(B,word_v),
haswordauthor(C,word_v).
sameauthor(B,C):0.190517 :-
haswordauthor(B,word_w),
haswordauthor(C,word_w).
sameauthor(B,C):0.099437 :-
haswordauthor(B,word_william),
haswordauthor(C,word_william).
sameauthor(B,C):0.093441 :-
haswordauthor(B,word_y),
haswordauthor(C,word_y).

View File

@ -0,0 +1,19 @@
sameperson(X,Y):0.2:-
movie(M,X),
movie(M,Y).
sameperson(X,Y):0.2:-
actor(X),
actor(Y),
workedunder(X,Z),
workedunder(Y,Z).
sameperson(X,Y):0.2:-
gender(X,Z),
gender(Y,Z).
sameperson(X,Y):0.2:-
director(X),
director(Y),
genre(X,Z),
genre(Y,Z).

1862
packages/cplint/em/sp1.kb Normal file

File diff suppressed because it is too large Load Diff

27
packages/cplint/em/sp1.l Normal file
View File

@ -0,0 +1,27 @@
output(sameperson/2).
input(gender/2).
input(genre/2).
input(actor/1).
input(director/1).
input(movie/2).
input(workedunder/2).
modeb(1,sameperson(-person, -person)).
modeb(1,workedunder(+person,-person)).
modeb(1,movie(-movie,+person)).
modeb(1,gender(+person,-gender)).
modeb(1,genre(+person,-genre)).
modeb(1,actor(+person)).
modeb(1,director(+person)).

View File

@ -0,0 +1,38 @@
/* Final CLL -239.829763
Execution time 39.458000
setting(sample_size,1000).
setting(equivalent_sample_size,100).
setting(epsilon_em,0.1).
setting(epsilon_em_fraction,0.01).
setting(epsilon_sem,2).
setting(random_restarts_number,1).
setting(inference,cplint).
setting(verbosity,3).
Suff stats
1,[161.602233313471,216.561766686529],378.000000,1
2,[85.4594299534467,432.807154859118],518.000000,1
3,[1.03319019220731,444.410117853292],445.000000,1
4,[23.1330023694224,18.8596251037636],42.000000,1
*/
sameperson(B,C):0.423997 :-
movie(D,B),
movie(D,C).
sameperson(B,C):0.168873 :-
actor(B),
actor(C),
workedunder(B,D),
workedunder(C,D).
sameperson(B,C):0.002385 :-
gender(B,D),
gender(C,D).
sameperson(B,C):0.553398 :-
director(B),
director(C),
genre(B,D),
genre(C,D).

392
packages/cplint/mcintyre.pl Normal file
View File

@ -0,0 +1,392 @@
/*
LPAD and CP-Logic reasoning suite
File mcintyre.pl
Monte Carlo inference for LPADs
Copyright (c) 2011, Fabrizio Riguzzi
*/
:-dynamic rule_n/1,setting/2.
:-use_module(library(random)).
:-use_module(library(lists)).
:- set_prolog_flag(unknown,fail).
:-source.
rule_n(1).
setting(epsilon_parsing, 0.0001).
setting(compiling,true).
/* values: true, failse */
/* k
*
* numnber of samples
*
* Default value: 1000
*/
setting(k, 1000).
/* min_error
* ---------
* This parameter shows the threshold for the probability interval.
*
* Default value: 0.01
*/
setting(min_error, 0.01).
s(Goals, Samples, CPUTime, WallTime, Lower, Prob, Upper):-
solve(Goals, Samples, CPUTime, WallTime, Lower, Prob, Upper).
solve(Goals, Samples, CPUTime, WallTime, Lower, Prob, Upper) :-
% Retrieving functional parameters...
setting(k, K),
setting(min_error, MinError),
% Resetting the clocks...
statistics(cputime,[_,_]),
statistics(walltime,[_,_]),
% Performing resolution...
montecarlo_cycle(0, 0, Goals, K, MinError, Samples, Lower, Prob, Upper),
% Taking elapsed times...
statistics(cputime,[_,CT1]),
CPUTime is CT1/1000,
statistics(walltime,[_,WT1]),
WallTime is WT1/1000.
/* montecarlo(Count, Success, Goals, K, MinError, Samples, Lower, Prob, Upper)
* ---------------------------------------------------------------------------
* This tail recursive predicate solves the problem currently in memory with a
* Monte Carlo approach.
* It requires the number of samples and successes so far (Count and Success),
* the desired list of Goals to fulfil, the number K of samples to consider at
* once and the threshold MinError for the binomial proportion confidence
* interval.
* It returns the total number of Samples considered, the Lower and Upper ends
* of the the binomial proportion confidence interval and the extimated Prob.
*
* INPUT
* - Count: number of samples considered so far.
* - Success: number of successfull samples considered so far.
* - Goals: list of goals to fulfil.
* - K: number of samples to consider at once.
* - MinError: threshold for the binomial proportion confidence interval.
*
* OUTPUT
* - Samples: total number of samples considered.
* - Lower: lower end of the the binomial proportion confidence interval.
* - Prob: extimated probability.
* - Upper: upper end of the the binomial proportion confidence interval.
*
* NB: This method is based on the binomial proportion confidence interval and
* the Brown's rule of thumb to avoid the case the sample proportion is
* exactly 0.0 or 1.0 and doesn't make use of BDDs.
*/
montecarlo_cycle(N0, S0, Goals, K, MinError, Samples, Lower, Prob, Upper):-!,
montecarlo(K,N0, S0, Goals, N, S),
P is S / N,
D is N - S,
Semi is 1.95996 * sqrt(P * (1 - P) / N),
Int is 2 * Semi,
/* N * P > 5; N * S / N > 5; S > 5
* N (1 - P) > 5; N (1 - S / N) > 5; N (N - S) / N > 5; N - S > 5
*/
%format("Batch: samples ~d positive ~d interval ~f~n",[N,S,Int]),
%flush_output,
%((S > 5, D > 5,
((Int < MinError; Int =:= 0) ->
Samples is N,
Lower is P - Semi,
Prob is P,
Upper is P + Semi
;
montecarlo_cycle(N, S, Goals, K, MinError, Samples, Lower, Prob, Upper)
).
montecarlo(0,N,S , _Goals,N,S):-!.
montecarlo(K1,Count, Success, Goals,N1,S1):-
abolish_all_tables,
eraseall(exp),
(Goals->
Valid=1
;
Valid=0
),
N is Count + 1,
S is Success + Valid,
K2 is K1-1,
montecarlo(K2,N, S, Goals, N1,S1).
member_eq(Item, [Head|_Tail]) :-
Item==Head, !.
member_eq(Item, [_Head|Tail]) :-
member_eq(Item, Tail).
list2and([X], X) :-
X\= (_, _) , !.
list2and([H|T], (H, Ta)) :- !,
list2and(T, Ta).
list2or([X], X) :-
X \= ( _ ; _ ) , !.
list2or([H|T], (H ; Ta)) :- !,
list2or(T, Ta).
get_var_n(R,S,Probs,V):-
(v(R,S,V)->
true
;
length(Probs,L),
add_var(L,Probs,V),
assert(v(R,S,V))
).
sample_head(_HeadList,R,VC,NH):-
recorded(exp,(R,VC,NH),_),!.
sample_head(HeadList,R,VC,NH):-
sample(HeadList,NH),
recorda(exp,(R,VC,NH),_).
generate_rules_fact([],_HeadList,_VC,_R,_Probs,_N,[]).
generate_rules_fact([(Head:_P1),(null:_P2)],_HeadList,VC,R,Probs,N,[Clause]):-!,
(Clause=(Head:-(sample_head(Probs,R,VC,NH),NH=N))).
generate_rules_fact([(Head:_P)|T],HeadList,VC,R,Probs,N,[Clause|Clauses]):-
(Clause=(Head:-(sample_head(Probs,R,VC,NH),NH=N))),
N1 is N+1,
generate_rules_fact(T,HeadList,VC,R,Probs,N1,Clauses).
generate_clause(Head,Body,_HeadList,VC,R,Probs,_BDDAnd,N,_Builtin,Clause):-
(ground(VC)->
Clause=(Head:-(sample_head(Probs,R,VC,NH),NH=N,Body))
;
Clause=(Head:-(Body,sample_head(Probs,R,VC,NH),NH=N))
).
generate_rules([],_Body,_HeadList,_VC,_R,_Probs,_BDDAnd,_N,_Builtin,[]).
generate_rules([(Head:_P1),(null:_P2)],Body,HeadList,VC,R,Probs,BDDAnd,N,Builtin,[Clause]):-!,
generate_clause(Head,Body,HeadList,VC,R,Probs,BDDAnd,N,Builtin,Clause).
generate_rules([(Head:_P)|T],Body,HeadList,VC,R,Probs,BDDAnd,N,Builtin,[Clause|Clauses]):-
generate_clause(Head,Body,HeadList,VC,R,Probs,BDDAnd,N,Builtin,Clause),
N1 is N+1,
generate_rules(T,Body,HeadList,VC,R,Probs,BDDAnd,N1,Builtin,Clauses).
/* set(Par,Value) can be used to set the value of a parameter */
set(Parameter,Value):-
retract(setting(Parameter,_)),
assert(setting(Parameter,Value)).
extract_vars(Variable, Var0, Var1) :-
var(Variable), !,
(member_eq(Variable, Var0) ->
Var1 = Var0;
append(Var0, [Variable], Var1)).
extract_vars(Term, Var0, Var1) :-
Term=..[_F|Args],
extract_vars_list(Args, Var0, Var1).
extract_vars_list([], Var, Var).
extract_vars_list([Term|Tail], Var0, Var1) :-
extract_vars(Term, Var0, Var),
extract_vars_list(Tail, Var, Var1).
difference([],_,[]).
difference([H|T],L2,L3):-
member_eq(H,L2),!,
difference(T,L2,L3).
difference([H|T],L2,[H|L3]):-
difference(T,L2,L3).
process_head(HeadList, GroundHeadList) :-
ground_prob(HeadList), !,
process_head_ground(HeadList, 0, GroundHeadList).
process_head(HeadList, HeadList).
/* process_head_ground([Head:ProbHead], Prob, [Head:ProbHead|Null])
* ----------------------------------------------------------------
*/
process_head_ground([Head:ProbHead], Prob, [Head:ProbHead|Null]) :-!,
ProbLast is 1 - Prob - ProbHead,
setting(epsilon_parsing, Eps),
EpsNeg is - Eps,
ProbLast > EpsNeg,
(ProbLast > Eps ->
Null = [null:ProbLast];
Null = []).
process_head_ground([Head:ProbHead|Tail], Prob, [Head:ProbHead|Next]) :-
ProbNext is Prob + ProbHead,
process_head_ground(Tail, ProbNext, Next).
ground_prob([]).
ground_prob([_Head:ProbHead|Tail]) :-
ground(ProbHead), % Succeeds if there are no free variables in the term ProbHead.
ground_prob(Tail).
get_probs([], []).
get_probs([_H:P|T], [P1|T1]) :-
P1 is P,
get_probs(T, T1).
builtin(_A is _B).
builtin(_A > _B).
builtin(_A < _B).
builtin(_A >= _B).
builtin(_A =< _B).
builtin(_A =:= _B).
builtin(_A =\= _B).
builtin(true).
builtin(false).
builtin(_A = _B).
builtin(_A==_B).
builtin(_A\=_B).
builtin(_A\==_B).
builtin(length(_L, _N)).
builtin(member(_El, _L)).
builtin(average(_L, _Av)).
builtin(max_list(_L, _Max)).
builtin(min_list(_L, _Max)).
builtin(nth0(_, _, _)).
builtin(nth(_, _, _)).
builtin(eraseall(_Id)).
builtin(recordzifnot(_Id, _Item, _)).
parse(FileIn,FileOut):-
open(FileIn,read,SI),
read_clauses(SI,C),
close(SI),
process_clauses(C,[],C1),
open(FileOut,write,SO),
write_clauses(C1,SO),
close(SO).
process_clauses([end_of_file],C,C).
process_clauses([H|T],C0,C1):-
(expand_term(H,H1)->
true
;
H1=H
),
(H1=[_|_]->
append(C0,H1,C2)
;
append(C0,[H1],C2)
),
process_clauses(T,C2,C1).
read_clauses(S,[Cl|Out]):-
read_term(S,Cl,[]),
(Cl=end_of_file->
Out=[]
;
read_clauses(S,Out)
).
write_clauses([],_).
write_clauses([H|T],S):-
write(S,H),
write(S,'.'),
nl(S),
write_clauses(T,S).
sample(HeadList, HeadId) :-
random(Prob),
sample(HeadList, 0, 0, Prob, HeadId), !.
sample([HeadProb|Tail], Index, Prev, Prob, HeadId) :-
Succ is Index + 1,
Next is Prev + HeadProb,
(Prob =< Next ->
HeadId = Index;
sample(Tail, Succ, Next, Prob, HeadId)).
get_next_rule_number(R):-
retract(rule_n(R)),
R1 is R+1,
assert(rule_n(R1)).
user:term_expansion((Head :- Body), Clauses):-
% disjunctive clause with more than one head atom
setting(compiling,true),
Head = (_;_), !,
list2or(HeadListOr, Head),
process_head(HeadListOr, HeadList),
get_next_rule_number(R),
get_probs(HeadList,Probs),
extract_vars((Head:-Body),[],VC),
generate_rules(HeadList,Body,HeadList,VC,R,Probs,_BDDAnd,0,_Builtin,Clauses).
user:term_expansion((Head :- Body), Clauses) :-
% disjunctive clause with a single head atom
setting(compiling,true),
((Head:-Body) \= ((user:term_expansion(_,_) ):- _ )),
Head = (H:_), !,
list2or(HeadListOr, Head),
process_head(HeadListOr, HeadList),
get_next_rule_number(R),
get_probs(HeadList,Probs),
extract_vars((Head:-Body),[],VC),
generate_clause(H,Body,HeadList,VC,R,Probs,_BDDAnd,0,_Builtin,Clauses).
user:term_expansion(Head,Clauses) :-
% disjunctive fact with more than one head atom
setting(compiling,true),
Head = (_;_),!,
list2or(HeadListOr, Head),
process_head(HeadListOr, HeadList),
extract_vars((Head),[],VC),
get_next_rule_number(R),
get_probs(HeadList,Probs),
generate_rules_fact(HeadList,HeadList,VC,R,Probs,0,Clauses).
user:term_expansion(Head,Clause) :-
% disjunctive fact with a single head atom
setting(compiling,true),
(Head \= ((term_expansion(_,_)) :- _ )),
Head = (Head1:_), !,
list2or(HeadListOr, Head),
process_head(HeadListOr, HeadList),
get_probs(HeadList,Probs),
extract_vars((Head),[],VC),
get_next_rule_number(R),
(Clause=(Head1:-(sample_head(Probs,R,VC,NH),NH=0))).

View File

@ -0,0 +1,201 @@
The Artistic License 2.0
Copyright (c) 2000-2006, The Perl Foundation.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
This license establishes the terms under which a given free software
Package may be copied, modified, distributed, and/or redistributed.
The intent is that the Copyright Holder maintains some artistic
control over the development of that Package while still keeping the
Package available as open source and free software.
You are always permitted to make arrangements wholly outside of this
license directly with the Copyright Holder of a given Package. If the
terms of this license do not permit the full use that you propose to
make of the Package, you should contact the Copyright Holder and seek
a different licensing arrangement.
Definitions
"Copyright Holder" means the individual(s) or organization(s)
named in the copyright notice for the entire Package.
"Contributor" means any party that has contributed code or other
material to the Package, in accordance with the Copyright Holder's
procedures.
"You" and "your" means any person who would like to copy,
distribute, or modify the Package.
"Package" means the collection of files distributed by the
Copyright Holder, and derivatives of that collection and/or of
those files. A given Package may consist of either the Standard
Version, or a Modified Version.
"Distribute" means providing a copy of the Package or making it
accessible to anyone else, or in the case of a company or
organization, to others outside of your company or organization.
"Distributor Fee" means any fee that you charge for Distributing
this Package or providing support for this Package to another
party. It does not mean licensing fees.
"Standard Version" refers to the Package if it has not been
modified, or has been modified only in ways explicitly requested
by the Copyright Holder.
"Modified Version" means the Package, if it has been changed, and
such changes were not explicitly requested by the Copyright
Holder.
"Original License" means this Artistic License as Distributed with
the Standard Version of the Package, in its current version or as
it may be modified by The Perl Foundation in the future.
"Source" form means the source code, documentation source, and
configuration files for the Package.
"Compiled" form means the compiled bytecode, object code, binary,
or any other form resulting from mechanical transformation or
translation of the Source form.
Permission for Use and Modification Without Distribution
(1) You are permitted to use the Standard Version and create and use
Modified Versions for any purpose without restriction, provided that
you do not Distribute the Modified Version.
Permissions for Redistribution of the Standard Version
(2) You may Distribute verbatim copies of the Source form of the
Standard Version of this Package in any medium without restriction,
either gratis or for a Distributor Fee, provided that you duplicate
all of the original copyright notices and associated disclaimers. At
your discretion, such verbatim copies may or may not include a
Compiled form of the Package.
(3) You may apply any bug fixes, portability changes, and other
modifications made available from the Copyright Holder. The resulting
Package will still be considered the Standard Version, and as such
will be subject to the Original License.
Distribution of Modified Versions of the Package as Source
(4) You may Distribute your Modified Version as Source (either gratis
or for a Distributor Fee, and with or without a Compiled form of the
Modified Version) provided that you clearly document how it differs
from the Standard Version, including, but not limited to, documenting
any non-standard features, executables, or modules, and provided that
you do at least ONE of the following:
(a) make the Modified Version available to the Copyright Holder
of the Standard Version, under the Original License, so that the
Copyright Holder may include your modifications in the Standard
Version.
(b) ensure that installation of your Modified Version does not
prevent the user installing or running the Standard Version. In
addition, the Modified Version must bear a name that is different
from the name of the Standard Version.
(c) allow anyone who receives a copy of the Modified Version to
make the Source form of the Modified Version available to others
under
(i) the Original License or
(ii) a license that permits the licensee to freely copy,
modify and redistribute the Modified Version using the same
licensing terms that apply to the copy that the licensee
received, and requires that the Source form of the Modified
Version, and of any works derived from it, be made freely
available in that license fees are prohibited but Distributor
Fees are allowed.
Distribution of Compiled Forms of the Standard Version
or Modified Versions without the Source
(5) You may Distribute Compiled forms of the Standard Version without
the Source, provided that you include complete instructions on how to
get the Source of the Standard Version. Such instructions must be
valid at the time of your distribution. If these instructions, at any
time while you are carrying out such distribution, become invalid, you
must provide new instructions on demand or cease further distribution.
If you provide valid instructions or cease distribution within thirty
days after you become aware that the instructions are invalid, then
you do not forfeit any of your rights under this license.
(6) You may Distribute a Modified Version in Compiled form without
the Source, provided that you comply with Section 4 with respect to
the Source of the Modified Version.
Aggregating or Linking the Package
(7) You may aggregate the Package (either the Standard Version or
Modified Version) with other packages and Distribute the resulting
aggregation provided that you do not charge a licensing fee for the
Package. Distributor Fees are permitted, and licensing fees for other
components in the aggregation are permitted. The terms of this license
apply to the use and Distribution of the Standard or Modified Versions
as included in the aggregation.
(8) You are permitted to link Modified and Standard Versions with
other works, to embed the Package in a larger work of your own, or to
build stand-alone binary or bytecode versions of applications that
include the Package, and Distribute the result without restriction,
provided the result does not expose a direct interface to the Package.
Items That are Not Considered Part of a Modified Version
(9) Works (including, but not limited to, modules and scripts) that
merely extend or make use of the Package, do not, by themselves, cause
the Package to be a Modified Version. In addition, such works are not
considered parts of the Package itself, and are not subject to the
terms of this license.
General Provisions
(10) Any use, modification, and distribution of the Standard or
Modified Versions is governed by this Artistic License. By using,
modifying or distributing the Package, you accept this license. Do not
use, modify, or distribute the Package, if you do not accept this
license.
(11) If your Modified Version has been derived from a Modified
Version made by someone other than you, you are nevertheless required
to ensure that your Modified Version complies with the requirements of
this license.
(12) This license does not grant you the right to use any trademark,
service mark, tradename, or logo of the Copyright Holder.
(13) This license includes the non-exclusive, worldwide,
free-of-charge patent license to make, have made, use, offer to sell,
sell, import and otherwise transfer the Package with respect to any
patent claims licensable by the Copyright Holder that are necessarily
infringed by the Package. If you institute patent litigation
(including a cross-claim or counterclaim) against any party alleging
that the Package constitutes direct or contributory patent
infringement, then this Artistic License to you shall terminate on the
date that such litigation is filed.
(14) Disclaimer of Warranty:
THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL
LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -0,0 +1,25 @@
This folder cotains RIB, a parameter learning algorithm based on the information bottleneck
To execute rib, load ib.pl with
:- use_module(library('cplint/rib')).
and run
:- ib_par(filestem).
in a directory containing filestem.kb, filestem.bg and filestem.l.
You will find the output in filestem.rules.
The folder contains the following examples
sp1 and sp1npn (first fold of IMDB for target predicate sameperson)
hmml2: hidden markov model
In the .l file you should specify input and output (taget predicates) in the form
input(pred/arity).
output(pred/arity).
See also http://sites.unife.it/rib/

View File

@ -0,0 +1,25 @@
failure(L) :- hmmf(L,[r0],C,2), C >= 1000.
success(L) :- hmmf(L,[r0],C,2), C<1000.
hmmf([p|L],S,C1,N):0.4 ; hmmf([s|L],S,C2,N):0.6 :- N>0,
S=[r0|_],
tr(S,R2),
N2 is N-1,
hmmf(L,[R2|S],C,N2),
C1 is C+900,
C2 is C+400.
hmmf([s|L],S,C1,N):0.5 ; hmmf([h|L],S,C2,N):0.5 :- N>0,
S=[r1|_],
tr(S,R2),
N2 is N-1,
hmmf(L,[R2|S],C,N2),
C1 is C+500,
C2 is C+400.
hmmf([],S,0,0).
tr([r0|T],r0): 0.7 ; tr([r0|T],r1) : 0.3.
tr([r1|T],r0): 0.3 ; tr([r1|T],r1) : 0.7.

1434
packages/cplint/rib/hmml2.kb Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
output(success/1).
output(failure/1).
unseen(hmmf/4).
unseen(tr/2).
:-set(depth_bound,inf).
:-set(minimal_step,0.005).
:-set(maximal_step,0.1).

View File

@ -0,0 +1,44 @@
/* Execution time 72.732000 LogSize 26.931472 Final I 14.929189
setting(setrand,rand(1230,45,123)).
setting(logsize_fraction,0.9).
setting(delta,-10).
setting(epsilon_fraction,100).
setting(max_rules,6000).
setting(depth_bound,inf).
setting(minimal_step,0.005).
setting(maximal_step,0.1).
setting(sample_size,478).
setting(verbosity,3).
*/
failure(B) :-
hmmf(B,[r0],C,2),
C>=1000.
success(B) :-
hmmf(B,[r0],C,2),
C<1000.
hmmf([p|B],C,D,E):0.280793 ; hmmf([s|B],C,F,E):0.719207 :-
E>0,
C=[r0|G],
tr(C,H),
I is E-1,
hmmf(B,[H|C],J,I),
D is J+900,
F is J+400.
hmmf([s|B],C,D,E):0.550416 ; hmmf([h|B],C,F,E):0.449584 :-
E>0,
C=[r1|G],
tr(C,H),
I is E-1,
hmmf(B,[H|C],J,I),
D is J+500,
F is J+400.
hmmf([],B,0,0).
tr([r0|B],r0):0.459350 ; tr([r0|B],r1):0.540650.
tr([r1|B],r0):0.508370 ; tr([r1|B],r1):0.491630.

View File

@ -0,0 +1,710 @@
/*
RIB
Copyright (c) 2011, Fabrizio Riguzzi and Nicola di Mauro
*/
:- module(inference_ib,
[build_network/5,
get_prob/3,get_CL/3,remove_head/2,build_ground_lpad/2,find_ground_atoms/3,get_atoms/2,
find_atoms_head/3,find_deriv_inf1/2]).
:-load_foreign_files(['cplint'],[],init_my_predicates).
:-multifile setting/2.
:-dynamic rule/4,def_rule/4,setting/2.
:-use_module(library(lists)).
/* start of list of parameters that can be set by the user with
set(Parameter,Value) */
setting(epsilon_parsing,0.00001).
setting(save_dot,false).
setting(ground_body,true).
/* available values: true, false
if true, both the head and the body of each clause will be grounded, otherwise
only the head is grounded. In the case in which the body contains variables
not appearing in the head, the body represents an existential event */
/* end of list of parameters */
/* s(GoalsLIst,Prob) compute the probability of a list of goals
GoalsLis can have variables, s returns in backtracking all the solutions with their
corresponding probability */
get_atoms([],[]):-!.
get_atoms([\+ H|T],T1):-
builtin(H),!,
get_atoms(T,T1).
get_atoms([H|T],T1):-
builtin(H),!,
get_atoms(T,T1).
get_atoms([\+ H|T],[H1|T1]):-!,
H=..[P,_|Rest],
H1=..[P|Rest],
get_atoms(T,T1).
get_atoms([H|T],[H1|T1]):-
H=..[P,_|Rest],
H1=..[P|Rest],
get_atoms(T,T1).
lookup_gvars([],_AV,[]):-!.
lookup_gvars([\+ H|T],AV,[HV|T1]):- !,
avl_lookup(H,HV,AV),
lookup_gvars(T,AV,T1).
lookup_gvars([H|T],AV,[HV|T1]):-
avl_lookup(H,HV,AV),
lookup_gvars(T,AV,T1).
get_prob(ChoiceVar,Vars,Distr):-
clpbn:call_solver([ChoiceVar], Vars),
clpbn_display:get_atts(ChoiceVar, [posterior(_Vs,_Vals,Distr,_AllDiffs)]).
get_CL(Goal,Vars,CL):-
clpbn:call_solver([Goal], Vars),
clpbn_display:get_atts(Goal, [posterior(_Vs,_Vals,[_P,CL],_AllDiffs)]).
remove_head([],[]).
remove_head([d(R,S)|T],[d(R,S)|T1]):-
%write(n),
remove_head(T,T1).
remove_head([(_N,R,S)|T],[(R,S)|T1]):-
%write(n),
remove_head(T,T1).
append_all([],L,L):-!.
append_all([LIntH|IntT],IntIn,IntOut):-
append(IntIn,LIntH,Int1),
append_all(IntT,Int1,IntOut).
process_goals([],[],[]):-!.
process_goals([H|T],[HG|TG],[HV|TV]):-
H=..[F,HV|Rest],
HG=..[F|Rest],
process_goals(T,TG,TV).
build_ground_lpad([],[]):-!.
build_ground_lpad([(R,S)|T],[(R,S,Head,Body)|T1]):-
user:rule_by_num(R,S,_,Head,Body),
build_ground_lpad(T,T1).
add_ev([],_AV):-!.
add_ev([\+ H|T],AV):-!,
H=..[F,_|R],
H1=..[F|R],
avl_lookup(H1,V,AV),
clpbn:put_atts(V,evidence(0)),
add_ev(T,AV).
add_ev([H|T],AV):-
H=..[F,_|R],
H1=..[F|R],
avl_lookup(H1,V,AV),
clpbn:put_atts(V,evidence(1)),
add_ev(T,AV).
lookup_gvars([],_AV,[],S,S):-!.
lookup_gvars([\+ H|T],AV,[HV|T1],Sign0,Sign2):- !,
avl_lookup(H,HV,AV),
clpbn:get_atts(HV, [key(K)]),
avl_insert(K,f,Sign0,Sign1),
lookup_gvars(T,AV,T1,Sign1,Sign2).
lookup_gvars([H|T],AV,[HV|T1],Sign0,Sign2):-
avl_lookup(H,HV,AV),
clpbn:get_atts(HV, [key(K)]),
avl_insert(K,t,Sign0,Sign1),
lookup_gvars(T,AV,T1,Sign1,Sign2).
build_table_conj(R,Table):-
build_col_conj(R,t,f,[],Row1),
build_col_conj(R,t,t,Row1,Table).
build_col_conj([],Tr,Final,Row0,Row1):-!,
(Tr=Final->
append(Row0,[0.999],Row1)
;
append(Row0,[0.001],Row1)
).
build_col_conj([\+_H|RP],Tr,Final,Row0,Row2):-!,
build_col_conj(RP,Tr,Final,Row0,Row1),
build_col_conj(RP,f,Final,Row1,Row2).
build_col_conj([_H|RP],Tr,Final,Row0,Row2):-
build_col_conj(RP,f,Final,Row0,Row1),
build_col_conj(RP,Tr,Final,Row1,Row2).
build_table_atoms(H,R,Table):-
build_col(H,R,f,f,[],Row1),
build_col(H,R,t,f,Row1,Table).
build_col(_A,[],Tr,Found,Row0,Row1):-!,
(Tr=Found->
append(Row0,[0.999],Row1)
;
append(Row0,[0.001],Row1)
).
build_col(A,[(_N,_S,H)|RP],Tr,Found,Row0,Row1):-
build_col_cycle(A,H,RP,Tr,Found,Row0,Row1).
build_col_cycle(_A,[],_RP,_Tr,_Found,Row,Row).
build_col_cycle(A,[A:_P|T],RP,Tr,Found,Row0,Row2):-!,
build_col(A,RP,Tr,t,Row0,Row1),
build_col_cycle(A,T,RP,Tr,Found,Row1,Row2).
build_col_cycle(A,[_|T],RP,Tr,Found,Row0,Row2):-
build_col(A,RP,Tr,Found,Row0,Row1),
build_col_cycle(A,T,RP,Tr,Found,Row1,Row2).
parents([],_CV,[]):-!.
parents([(N,S,_H)|T],CV,[V|T1]):-
avl_lookup(ch(N,S),V,CV),
parents(T,CV,T1).
find_rules_with_atom(_A,[],[]):-!.
find_rules_with_atom(A,[(N,S,Head,_Body)|T],[(N,S,Head)|R]):-
member(A:_P,Head),!,
find_rules_with_atom(A,T,R).
find_rules_with_atom(A,[_H|T],R):-
find_rules_with_atom(A,T,R).
build_table([P],L,Row):-!,
build_col(L,t,P,0.999,Row).
build_table([HP|TP],L,Tab):-
build_col(L,t,HP,0.001,Row),
append(Row,Row1,Tab),
build_table(TP,L,Row1).
build_col([],t,HP,_PNull,[HP]):-!.
build_col([],f,_HP,PNull,[PNull]):-!.
build_col([\+ _H|T],Truth,P,PNull,Row):-!,
build_col(T,Truth,P,PNull,Row1),
append(Row1,Row2,Row),
build_col(T,f,P,PNull,Row2).
build_col([_H|T],Truth,P,PNull,Row):-
build_col(T,f,P,PNull,Row1),
append(Row1,Row2,Row),
build_col(T,Truth,P,PNull,Row2).
get_parents([],_AV,[]):-!.
get_parents([\+ H|T],AV,[V|T1]):-!,
avl_lookup(H,V,AV),
get_parents(T,AV,T1).
get_parents([H|T],AV,[V|T1]):-!,
avl_lookup(H,V,AV),
get_parents(T,AV,T1).
choice_vars([],Tr,Tr,[]):-!.
choice_vars([(N,S,_H,_B)|T],Tr0,Tr1,[NV|T1]):-
avl_insert(ch(N,S),NV,Tr0,Tr2),
choice_vars(T,Tr2,Tr1,T1).
atom_vars([],Tr,Tr,[]):-!.
atom_vars([H|T],Tr0,Tr1,[VH|VT]):-
avl_insert(H,VH,Tr0,Tr2),
atom_vars(T,Tr2,Tr1,VT).
find_ground_atoms([],GA,GA):-!.
find_ground_atoms([(_N,_S,Head,_Body)|T],GA0,GA1):-
find_atoms_head(Head,AtH,_P),
append(GA0,AtH,GA3),
find_ground_atoms(T,GA3,GA1).
find_atoms_body([],[]):-!.
find_atoms_body([\+H|T],[H|T1]):-!,
find_atoms_body(T,T1).
find_atoms_body([H|T],[H|T1]):-
find_atoms_body(T,T1).
find_atoms_head([],[],[]).
find_atoms_head(['':P],[''],[P]):-!.
find_atoms_head([H:P|T],[H1|TA],[P|TP]):-
H=..[F,_|R],
H1=..[F|R],
find_atoms_head(T,TA,TP).
find_deriv_inf1(GoalsList,DB,Deriv):-
solve1(GoalsList,DB,[],Deriv).
find_deriv_inf(GoalsList,Deriv):-
solve(GoalsList,[],Deriv).
find_deriv_inf(GoalsList,DB,Deriv):-
solve(GoalsList,DB,[],Deriv).
/* duplicate can appear in the C set because two different unistantiated clauses may become the
same clause when instantiated */
solve1([],_DB,C,C):-!.
solve1([\+ H |T],DB,CIn,COut):-!,
DB > 0,
DB1 is DB-1,
list2and(HL,H),
(setof(D,find_deriv_inf1(HL,DB1,D),L)->
choose_clauses(CIn,L,C1),
solve(T,DB1,C1,COut)
;
solve(T,DB1,CIn,COut)
).
solve1([H|T],DB,CIn,COut):-
DB>0,
DB1 is DB-1,
user:def_rule(H,B,N,S),
append(B,T,NG),
append(CIn,[d(N,S)],CIn1),
solve(NG,DB1,CIn1,COut).
solve1([H|T],DB,CIn,COut):-
DB>0,
DB1 is DB-1,
find_rule(H,(R,S,N),B,CIn),
solve_pres(R,S,N,B,T,DB1,CIn,COut).
solve([],_DB,C,C):-!.
solve([\+ H|T],DB,CIn,COut):-
builtin(H),!,
call(\+ H),
solve(T,DB,CIn,COut).
solve([\+ H|T],DB,CIn,COut):-
functor(H,F,Args),
Args1 is Args-1,
user:input_cw(F/Args1),!,
call(user:neg(H)),
solve(T,DB,CIn,COut).
solve([\+ H|T],DB,CIn,COut):-
functor(H,F,Args),
Args1 is Args-1,
user:input(F/Args1),
call(user:neg(H)),
solve(T,DB,CIn,COut).
solve([\+ H |T],DB,CIn,COut):-!,
DB > 0,
DB1 is DB-1,
list2and(HL,H),
functor(H,F,Args),
Args1 is Args-1,
(user:input(F/Args1)->
call(user:neg(H))
;
true
),
(setof(D,find_deriv_inf(HL,DB1,D),L)->
choose_clauses(CIn,L,C1),
solve(T,DB1,C1,COut)
;
solve(T,DB1,CIn,COut)
).
solve([H|T],DB,CIn,COut):-
builtin(H),!,
call(H),
solve(T,DB,CIn,COut).
solve([H|T],DB,CIn,COut):-
user:db(H),!,
call(user:H),
solve(T,DB,CIn,COut).
solve([H|T],DB,CIn,COut):-
DB>0,
DB1 is DB-1,
user:def_rule(H,B,N,S),
append(B,T,NG),
append(CIn,[d(N,S)],CIn1),
solve(NG,DB1,CIn1,COut).
solve([H|T],DB,CIn,COut):-
functor(H,F,Args),
Args1 is Args-1,
user:input_cw(F/Args1),!,
call(user:H),
solve(T,DB,CIn,COut).
solve([H|T],DB,CIn,COut):-
functor(H,F,Args),
Args1 is Args-1,
user:input(F/Args1),
call(user:H),
solve(T,DB,CIn,COut).
solve([H|T],DB,CIn,COut):-
DB>0,
DB1 is DB-1,
find_rule(H,(R,S,N),B,CIn),
solve_pres(R,S,N,B,T,DB1,CIn,COut).
solve_pres(R,S,N,B,T,DB,CIn,COut):-
member_eq((N,R,S),CIn),!,
append(B,T,NG),
solve(NG,DB,CIn,COut).
solve_pres(R,S,N,B,T,DB,CIn,COut):-
append(CIn,[(N,R,S)],C1),
append(B,T,NG),
solve(NG,DB,C1,COut).
not_present_with_a_different_head(_N,_R,_S,[]).
not_present_with_a_different_head(N,R,S,[(N,R,S)|T]):-!,
not_present_with_a_different_head(N,R,S,T).
not_present_with_a_different_head(N,R,S,[(_N1,R,S1)|T]):-
S\=S1,!,
not_present_with_a_different_head(N,R,S,T).
not_present_with_a_different_head(N,R,S,[(_N1,R1,_S1)|T]):-
R\=R1,
not_present_with_a_different_head(N,R,S,T).
/* find_rule(G,(R,S,N),Body,C) takes a goal G and the current C set and
returns the index R of a disjunctive rule resolving with G together with
the index N of the resolving head, the substitution S and the Body of the
rule */
find_rule(H,(R,S,N),Body,C):-
user:rule(H,_P,N,R,S,_,_Head,Body),
not_already_present_with_a_different_head(N,R,S,C).
not_already_present_with_a_different_head(_N,_R,_S,[]).
not_already_present_with_a_different_head(N,R,S,[d(_R,_S1)|T]):-!,
not_already_present_with_a_different_head(N,R,S,T).
not_already_present_with_a_different_head(N,R,S,[(N1,R,S1)|T]):-
not_different(N,N1,S,S1),!,
not_already_present_with_a_different_head(N,R,S,T).
not_already_present_with_a_different_head(N,R,S,[(_N1,R1,_S1)|T]):-
R\==R1,
not_already_present_with_a_different_head(N,R,S,T).
not_different(_N,_N1,S,S1):-
S\=S1,!.
not_different(N,N1,S,S1):-
N\=N1,!,
dif(S,S1).
not_different(N,N,S,S).
member_head(H,[(H:_P)|_T],N,N).
member_head(H,[(_H:_P)|T],NIn,NOut):-
N1 is NIn+1,
member_head(H,T,N1,NOut).
/* choose_clauses(CIn,LC,COut) takes as input the current C set and
the set of C sets for a negative goal and returns a new C set that
excludes all the derivations for the negative goals */
choose_clauses(C,[],C).
choose_clauses(CIn,[D|T],COut):-
member((N,R,S),D),
already_present_with_a_different_head(N,R,S,CIn),!,
choose_a_head(N,R,S,CIn,C1),
choose_clauses(C1,T,COut).
choose_clauses(CIn,[D|T],COut):-
member((N,R,S),D),
new_head(N,R,S,N1),
\+ already_present(N1,R,S,CIn),
impose_dif_cons(R,S,CIn),
choose_clauses([(N1,R,S)|CIn],T,COut).
impose_dif_cons(_R,_S,[]):-!.
impose_dif_cons(R,S,[(_NH,R,SH)|T]):-!,
dif(S,SH),
impose_dif_cons(R,S,T).
impose_dif_cons(R,S,[_H|T]):-
impose_dif_cons(R,S,T).
/* instantiation_present_with_the_same_head(N,R,S,C)
takes rule R with substitution S and selected head N and a C set
and asserts dif constraints for all the clauses in C of which RS
is an instantitation and have the same head selected */
instantiation_present_with_the_same_head(_N,_R,_S,[]).
instantiation_present_with_the_same_head(N,R,S,[(NH,R,SH)|T]):-
\+ \+ S=SH,!,
dif_head_or_subs(N,R,S,NH,SH,T).
instantiation_present_with_the_same_head(N,R,S,[_H|T]):-
instantiation_present_with_the_same_head(N,R,S,T).
dif_head_or_subs(N,R,S,NH,_SH,T):-
dif(N,NH),
instantiation_present_with_the_same_head(N,R,S,T).
dif_head_or_subs(N,R,S,N,SH,T):-
dif(S,SH),
instantiation_present_with_the_same_head(N,R,S,T).
/* case 1 of Select: a more general rule is present in C with
a different head, instantiate it */
choose_a_head(N,R,S,[(NH,R,SH)|T],[(NH,R,SH)|T]):-
S=SH,
dif(N,NH).
/* case 2 of Select: a more general rule is present in C with
a different head, ensure that they do not generate the same
ground clause */
choose_a_head(N,R,S,[(NH,R,SH)|T],[(NH,R,S),(NH,R,SH)|T]):-
\+ \+ S=SH, S\==SH,
dif(N,NH),
dif(S,SH).
choose_a_head(N,R,S,[H|T],[H|T1]):-
choose_a_head(N,R,S,T,T1).
/* select a head different from N for rule R with
substitution S, return it in N1 */
new_head(N,R,S,N1):-
user:rule_by_num(R,S,Numbers,Head,_Body),
Head\=uniform(_,_,_),!,
nth0(N, Numbers, _Elem, Rest),
member(N1,Rest).
already_present_with_a_different_head(N,R,S,[(NH,R,SH)|_T]):-
\+ \+ S=SH,NH \= N.
already_present_with_a_different_head(N,R,S,[_H|T]):-
already_present_with_a_different_head(N,R,S,T).
/* checks that a rule R with head N and selection S is already
present in C (or a generalization of it is in C) */
already_present(N,R,S,[(N,R,SH)|_T]):-
S=SH.
already_present(N,R,S,[_H|T]):-
already_present(N,R,S,T).
/* rem_dup_lists removes the C sets that are a superset of
another C sets further on in the list of C sets */
/* rem_dup_lists removes the C sets that are a superset of
another C sets further on in the list of C sets */
rem_dup_lists([],L,L).
rem_dup_lists([H|T],L0,L):-
(member_subset(H,T);member_subset(H,L0)),!,
rem_dup_lists(T,L0,L).
rem_dup_lists([H|T],L0,L):-
rem_dup_lists(T,[H|L0],L).
member_subset(E,[H|_T]):-
subset_my(H,E),!.
member_subset(E,[_H|T]):-
member_subset(E,T).
member_eq(A,[H|_T]):-
A==H,!.
member_eq(A,[_H|T]):-
member_eq(A,T).
subset_my([],_).
subset_my([H|T],L):-
member_eq(H,L),
subset_my(T,L).
remove_duplicates_eq([],[]).
remove_duplicates_eq([H|T],T1):-
member_eq(H,T),!,
remove_duplicates_eq(T,T1).
remove_duplicates_eq([H|T],[H|T1]):-
remove_duplicates_eq(T,T1).
builtin(_A is _B).
builtin(_A > _B).
builtin(_A < _B).
builtin(_A >= _B).
builtin(_A =< _B).
builtin(_A =:= _B).
builtin(_A =\= _B).
builtin(true).
builtin(false).
builtin(_A = _B).
builtin(_A==_B).
builtin(_A\=_B).
builtin(_A\==_B).
builtin(length(_L,_N)).
builtin(member(_El,_L)).
builtin(average(_L,_Av)).
builtin(max_list(_L,_Max)).
builtin(min_list(_L,_Max)).
builtin(nth0(_,_,_)).
builtin(nth(_,_,_)).
average(L,Av):-
sum_list(L,Sum),
length(L,N),
Av is Sum/N.
list2or([],true):-!.
list2or([X],X):-
X\=;(_,_),!.
list2or([H|T],(H ; Ta)):-!,
list2or(T,Ta).
list2and([],true):-!.
list2and([X],X):-
X\=(_,_),!.
list2and([H|T],(H,Ta)):-!,
list2and(T,Ta).
/* predicates for building the formula to be converted into a BDD */
/* build_formula(LC,Formula,VarIn,VarOut) takes as input a set of C sets
LC and a list of Variables VarIn and returns the formula and a new list
of variables VarOut
Formula is of the form [Term1,...,Termn]
Termi is of the form [Factor1,...,Factorm]
Factorj is of the form (Var,Value) where Var is the index of
the multivalued variable Var and Value is the index of the value
*/
build_formula([],[],Var,Var,C,C).
build_formula([D|TD],[F|TF],VarIn,VarOut,C0,C1):-
length(D,NC),
C2 is C0+NC,
% reverse(D,D1),
D=D1,
build_term(D1,F,VarIn,Var1),
build_formula(TD,TF,Var1,VarOut,C2,C1).
build_formula([],[],Var,Var).
build_formula([D|TD],[F|TF],VarIn,VarOut):-
build_term(D,F,VarIn,Var1),
build_formula(TD,TF,Var1,VarOut).
build_term([],[],Var,Var).
build_term([(_,pruned,_)|TC],TF,VarIn,VarOut):-!,
build_term(TC,TF,VarIn,VarOut).
build_term([(N,R,S)|TC],[[NVar,N]|TF],VarIn,VarOut):-
(nth0_eq(0,NVar,VarIn,(R,S))->
Var1=VarIn
;
append(VarIn,[(R,S)],Var1),
length(VarIn,NVar)
),
build_term(TC,TF,Var1,VarOut).
/* nth0_eq(PosIn,PosOut,List,El) takes as input a List,
an element El and an initial position PosIn and returns in PosOut
the position in the List that contains an element exactly equal to El
*/
nth0_eq(N,N,[H|_T],El):-
H==El,!.
nth0_eq(NIn,NOut,[_H|T],El):-
N1 is NIn+1,
nth0_eq(N1,NOut,T,El).
/* var2numbers converts a list of couples (Rule,Substitution) into a list
of triples (N,NumberOfHeadsAtoms,ListOfProbabilities), where N is an integer
starting from 0 */
var2numbers([],_N,[]).
var2numbers([(R,S)|T],N,[[N,ValNumber,Probs]|TNV]):-
find_probs(R,S,Probs),
length(Probs,ValNumber),
N1 is N+1,
var2numbers(T,N1,TNV).
find_probs(R,S,Probs):-
user:rule_by_num(R,S,_N,Head,_Body),
get_probs(Head,Probs).
get_probs(uniform(_A:1/Num,_P,_Number),ListP):-
Prob is 1/Num,
list_el(Num,Prob,ListP).
get_probs([],[]).
get_probs([_H:P|T],[P1|T1]):-
P1 is P,
get_probs(T,T1).
list_el(0,_P,[]):-!.
list_el(N,P,[P|T]):-
N1 is N-1,
list_el(N1,P,T).
/* end of predicates for building the formula to be converted into a BDD */

2607
packages/cplint/rib/rib.pl Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,47 @@
sameperson_pos(X,Y):0.05:-
movie(M,X),
movie(M,Y).
sameperson_pos(X,Y):0.3:-
actor(X),
actor(Y),
workedunder(X,Z),
workedunder(Y,Z).
sameperson_pos(X,Y):0.5:-
director(X),
director(Y),
genre(X,Z),
genre(Y,Z).
sameperson_neg(X,Y):0.05:-
movie(M,X),
movie(M,Y).
sameperson_neg(X,Y):0.3:-
actor(X),
actor(Y),
workedunder(X,Z),
workedunder(Y,Z).
sameperson_neg(X,Y):0.5:-
director(X),
director(Y),
genre(X,Z),
genre(Y,Z).
sameperson(X,Y):0.3:-
\+ sameperson_pos(X,Y),
sameperson_neg(X,Y).
sameperson(X,Y):0.3:-
\+ sameperson_pos(X,Y),
\+ sameperson_neg(X,Y).
sameperson(X,Y):0.3:-
sameperson_pos(X,Y),
sameperson_neg(X,Y).
sameperson(X,Y):0.3:-
sameperson_pos(X,Y),
\+ sameperson_neg(X,Y).

11186
packages/cplint/rib/sp1.kb Normal file

File diff suppressed because it is too large Load Diff

39
packages/cplint/rib/sp1.l Normal file
View File

@ -0,0 +1,39 @@
output(sameperson/2).
input(gender/2).
input(genre/2).
input(actor/1).
input(director/1).
input(movie/2).
input(workedunder/2).
unseen(sameperson_pos/2).
unseen(sameperson_neg/2).
modeb(1,sameperson(-person, -person)).
modeb(1,sameperson_pos(-person, -person)).
modeb(1,sameperson_neg(-person, -person)).
modeb(1,workedunder(+person,-person)).
modeb(1,movie(-movie,+person)).
modeb(1,gender(+person,-gender)).
modeb(1,genre(+person,-genre)).
modeb(1,actor(+person)).
modeb(1,director(+person)).
:- set(minimal_step,0.005).
:- set(maximal_step,0.1).

View File

@ -0,0 +1,72 @@
/* Execution time 40.627000 LogSize 28.339593 Final I 5.235925
setting(setrand,rand(1230,45,123)).
setting(cll_sample_size,1000).
setting(equivalent_sample_size,100).
setting(large_equivalent_sample_size,10000).
setting(large_equivalent_sample_size_factor,100).
setting(equivalent_sample_size_factor,0.25).
setting(epsilon_em,0.1).
setting(epsilon_sem,2).
setting(random_restarts_number,10).
setting(inference,cplint).
setting(new_head_atoms_zero_prob,false).
setting(depth_bound,4).
setting(logsize_fraction,0.9).
setting(maximum_instantiations,20).
setting(delta,-10).
setting(epsilon_fraction,100).
setting(max_rules,6000).
setting(max_var,2).
setting(minimal_step,0.005).
setting(maximal_step,0.1).
setting(sample_size,840).
setting(verbosity,3).
*/
sameperson_pos(B,C):0.500393 :-
movie(D,B),
movie(D,C).
sameperson_pos(B,C):0.485303 :-
actor(B),
actor(C),
workedunder(B,D),
workedunder(C,D).
sameperson_pos(B,C):0.556762 :-
director(B),
director(C),
genre(B,D),
genre(C,D).
sameperson_neg(B,C):0.414274 :-
movie(D,B),
movie(D,C).
sameperson_neg(B,C):0.524811 :-
actor(B),
actor(C),
workedunder(B,D),
workedunder(C,D).
sameperson_neg(B,C):0.682189 :-
director(B),
director(C),
genre(B,D),
genre(C,D).
sameperson(B,C):0.377641 :-
\+ sameperson_pos(B,C),
sameperson_neg(B,C).
sameperson(B,C):0.040523 :-
\+ sameperson_pos(B,C),
\+ sameperson_neg(B,C).
sameperson(B,C):0.784373 :-
sameperson_pos(B,C),
sameperson_neg(B,C).
sameperson(B,C):0.461804 :-
sameperson_pos(B,C),
\+ sameperson_neg(B,C).

View File

@ -0,0 +1,19 @@
sameperson(X,Y):0.2:-
movie(M,X),
movie(M,Y).
sameperson(X,Y):0.2:-
actor(X),
actor(Y),
workedunder(X,Z),
workedunder(Y,Z).
sameperson(X,Y):0.2:-
gender(X,Z),
gender(Y,Z).
sameperson(X,Y):0.2:-
director(X),
director(Y),
genre(X,Z),
genre(Y,Z).

11186
packages/cplint/rib/sp1npn.kb Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,30 @@
output(sameperson/2).
input(gender/2).
input(genre/2).
input(actor/1).
input(director/1).
input(movie/2).
input(workedunder/2).
modeb(1,sameperson(-person, -person)).
modeb(1,workedunder(+person,-person)).
modeb(1,movie(-movie,+person)).
modeb(1,gender(+person,-gender)).
modeb(1,genre(+person,-genre)).
modeb(1,actor(+person)).
modeb(1,director(+person)).
:- set(maximal_step,0.1).
:- set(minimal_step,0.005).

View File

@ -0,0 +1,44 @@
/* Execution time 133.240000 LogSize 13.169796 Final I 2.364892
setting(setrand,rand(1230,45,123)).
setting(cll_sample_size,1000).
setting(equivalent_sample_size,100).
setting(large_equivalent_sample_size,10000).
setting(large_equivalent_sample_size_factor,100).
setting(equivalent_sample_size_factor,0.25).
setting(epsilon_em,0.1).
setting(epsilon_sem,2).
setting(random_restarts_number,10).
setting(inference,cplint).
setting(new_head_atoms_zero_prob,false).
setting(depth_bound,4).
setting(logsize_fraction,0.9).
setting(maximum_instantiations,20).
setting(delta,-10).
setting(epsilon_fraction,100).
setting(max_rules,5000).
setting(max_var,2).
setting(maximal_step,0.1).
setting(minimal_step,0.005).
setting(sample_size,840).
setting(verbosity,3).
*/
sameperson(B,C):0.507542 :-
movie(D,B),
movie(D,C).
sameperson(B,C):0.566586 :-
actor(B),
actor(C),
workedunder(B,D),
workedunder(C,D).
sameperson(B,C):0.355943 :-
gender(B,D),
gender(C,D).
sameperson(B,C):0.853257 :-
director(B),
director(C),
genre(B,D),
genre(C,D).

View File

@ -0,0 +1,201 @@
The Artistic License 2.0
Copyright (c) 2000-2006, The Perl Foundation.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
This license establishes the terms under which a given free software
Package may be copied, modified, distributed, and/or redistributed.
The intent is that the Copyright Holder maintains some artistic
control over the development of that Package while still keeping the
Package available as open source and free software.
You are always permitted to make arrangements wholly outside of this
license directly with the Copyright Holder of a given Package. If the
terms of this license do not permit the full use that you propose to
make of the Package, you should contact the Copyright Holder and seek
a different licensing arrangement.
Definitions
"Copyright Holder" means the individual(s) or organization(s)
named in the copyright notice for the entire Package.
"Contributor" means any party that has contributed code or other
material to the Package, in accordance with the Copyright Holder's
procedures.
"You" and "your" means any person who would like to copy,
distribute, or modify the Package.
"Package" means the collection of files distributed by the
Copyright Holder, and derivatives of that collection and/or of
those files. A given Package may consist of either the Standard
Version, or a Modified Version.
"Distribute" means providing a copy of the Package or making it
accessible to anyone else, or in the case of a company or
organization, to others outside of your company or organization.
"Distributor Fee" means any fee that you charge for Distributing
this Package or providing support for this Package to another
party. It does not mean licensing fees.
"Standard Version" refers to the Package if it has not been
modified, or has been modified only in ways explicitly requested
by the Copyright Holder.
"Modified Version" means the Package, if it has been changed, and
such changes were not explicitly requested by the Copyright
Holder.
"Original License" means this Artistic License as Distributed with
the Standard Version of the Package, in its current version or as
it may be modified by The Perl Foundation in the future.
"Source" form means the source code, documentation source, and
configuration files for the Package.
"Compiled" form means the compiled bytecode, object code, binary,
or any other form resulting from mechanical transformation or
translation of the Source form.
Permission for Use and Modification Without Distribution
(1) You are permitted to use the Standard Version and create and use
Modified Versions for any purpose without restriction, provided that
you do not Distribute the Modified Version.
Permissions for Redistribution of the Standard Version
(2) You may Distribute verbatim copies of the Source form of the
Standard Version of this Package in any medium without restriction,
either gratis or for a Distributor Fee, provided that you duplicate
all of the original copyright notices and associated disclaimers. At
your discretion, such verbatim copies may or may not include a
Compiled form of the Package.
(3) You may apply any bug fixes, portability changes, and other
modifications made available from the Copyright Holder. The resulting
Package will still be considered the Standard Version, and as such
will be subject to the Original License.
Distribution of Modified Versions of the Package as Source
(4) You may Distribute your Modified Version as Source (either gratis
or for a Distributor Fee, and with or without a Compiled form of the
Modified Version) provided that you clearly document how it differs
from the Standard Version, including, but not limited to, documenting
any non-standard features, executables, or modules, and provided that
you do at least ONE of the following:
(a) make the Modified Version available to the Copyright Holder
of the Standard Version, under the Original License, so that the
Copyright Holder may include your modifications in the Standard
Version.
(b) ensure that installation of your Modified Version does not
prevent the user installing or running the Standard Version. In
addition, the Modified Version must bear a name that is different
from the name of the Standard Version.
(c) allow anyone who receives a copy of the Modified Version to
make the Source form of the Modified Version available to others
under
(i) the Original License or
(ii) a license that permits the licensee to freely copy,
modify and redistribute the Modified Version using the same
licensing terms that apply to the copy that the licensee
received, and requires that the Source form of the Modified
Version, and of any works derived from it, be made freely
available in that license fees are prohibited but Distributor
Fees are allowed.
Distribution of Compiled Forms of the Standard Version
or Modified Versions without the Source
(5) You may Distribute Compiled forms of the Standard Version without
the Source, provided that you include complete instructions on how to
get the Source of the Standard Version. Such instructions must be
valid at the time of your distribution. If these instructions, at any
time while you are carrying out such distribution, become invalid, you
must provide new instructions on demand or cease further distribution.
If you provide valid instructions or cease distribution within thirty
days after you become aware that the instructions are invalid, then
you do not forfeit any of your rights under this license.
(6) You may Distribute a Modified Version in Compiled form without
the Source, provided that you comply with Section 4 with respect to
the Source of the Modified Version.
Aggregating or Linking the Package
(7) You may aggregate the Package (either the Standard Version or
Modified Version) with other packages and Distribute the resulting
aggregation provided that you do not charge a licensing fee for the
Package. Distributor Fees are permitted, and licensing fees for other
components in the aggregation are permitted. The terms of this license
apply to the use and Distribution of the Standard or Modified Versions
as included in the aggregation.
(8) You are permitted to link Modified and Standard Versions with
other works, to embed the Package in a larger work of your own, or to
build stand-alone binary or bytecode versions of applications that
include the Package, and Distribute the result without restriction,
provided the result does not expose a direct interface to the Package.
Items That are Not Considered Part of a Modified Version
(9) Works (including, but not limited to, modules and scripts) that
merely extend or make use of the Package, do not, by themselves, cause
the Package to be a Modified Version. In addition, such works are not
considered parts of the Package itself, and are not subject to the
terms of this license.
General Provisions
(10) Any use, modification, and distribution of the Standard or
Modified Versions is governed by this Artistic License. By using,
modifying or distributing the Package, you accept this license. Do not
use, modify, or distribute the Package, if you do not accept this
license.
(11) If your Modified Version has been derived from a Modified
Version made by someone other than you, you are nevertheless required
to ensure that your Modified Version complies with the requirements of
this license.
(12) This license does not grant you the right to use any trademark,
service mark, tradename, or logo of the Copyright Holder.
(13) This license includes the non-exclusive, worldwide,
free-of-charge patent license to make, have made, use, offer to sell,
sell, import and otherwise transfer the Package with respect to any
patent claims licensable by the Copyright Holder that are necessarily
infringed by the Package. If you institute patent litigation
(including a cross-claim or counterclaim) against any party alleging
that the Package constitutes direct or contributory patent
infringement, then this Artistic License to you shall terminate on the
date that such litigation is filed.
(14) Disclaimer of Warranty:
THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL
LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -0,0 +1,53 @@
#
# default base directory for YAP installation
# (EROOT for architecture-dependent files)
#
prefix = @prefix@
ROOTDIR = $(prefix)
EROOTDIR = ${prefix}
#
# where the binary should be
#
BINDIR = $(EROOTDIR)/bin
#
# where YAP should look for libraries
#
LIBDIR=$(EROOTDIR)/lib/Yap
DESTDIR=$(prefix)/share/Yap
#
#
CC=@CC@
CFLAGS= @SHLIB_CFLAGS@ $(YAP_EXTRAS) $(DEFS) -I$(srcdir)/../../../include @CPPFLAGS@
#
#
# You shouldn't need to change what follows.
#
INSTALL=@INSTALL@
INSTALL_DATA=@INSTALL_DATA@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
SHELL=/bin/sh
RANLIB=ranlib
SHLIB_CFLAGS=-shared -fPIC
SHLIB_SUFFIX=.so
CWD=$(PWD)
srcdir=@srcdir@
CPLINT_CFLAGS=@CPLINT_CFLAGS@
CPLINT_LDFLAGS=@CPLINT_LDFLAGS@
CPLINT_LIBS=@CPLINT_LIBS@
#
default: bddem.so
bddem.so: bddem.o
$(CC) -shared -export-dynamic $(CPLINT_LDFLAGS) bddem.o $(CPLINT_LIBS) -o bddem.so -Wl,-R,$(YAPLIBDIR) -Wl,-R,$(LIBDIR)
bddem.o : $(srcdir)/bddem.c
$(CC) -c $(CFLAGS) $(CPLINT_CFLAGS) $(srcdir)/bddem.c -o bddem.o
clean:
rm -f *.o bddem.so
install: default
$(INSTALL_PROGRAM) bddem.so $(DESTDIR)

View File

@ -0,0 +1,24 @@
This folder cotains EMBLEM, an EM parameter learning algorithm, and SLIPCASE, a structure learning algorithm
To execute EMBLEM, load slicpase.pl with
:- use_module(library('cplint/slipcase')).
and run
:- em(filestem).
in a directory containing filestem.kb, filestem.bg and filestem.l.
You will find the output in filestem.rules.
To execute SLIPCASE, load slicpase.pl with
:- use_module(library('cplint/slipcase')).
and run
:- sl(filestem).
in a directory containing filestem.kb, filestem.bg and filestem.l.
You will find the output in filestem.rules.
See also http://sites.unife.it/ml/emblem and http://sites.unife.it/ml/slipcase

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,458 @@
/*
EMBLEM and SLIPCASE
Copyright (c) 2011, Fabrizio Riguzzii, Nicola di Mauro and Elena Bellodi
*/
:- use_module(library(terms)).
:- use_module(library(lists)).
:- set_prolog_flag(discontiguous_warnings,on).
:- set_prolog_flag(single_var_warnings,on).
theory_revisions_op(Theory,TheoryRevs):-
setof(RevOp, Theory^revise_theory(Theory,RevOp), TheoryRevs),!.
theory_revisions_op(_Theory,[]).
theory_revisions(Theory,TheoryRevs):-
theory_revisions_op(Theory,TheoryRevs1),
apply_operators(TheoryRevs1,Theory,TheoryRevs).
apply_operators([],_Theory,[]).
apply_operators([add(Rule)|RestOps],Theory,[NewTheory|RestTheory]) :-
append(Theory, [Rule], NewTheory),
apply_operators(RestOps,Theory,RestTheory).
apply_operators([add_body(Rule1,Rule2,_A)|RestOps],Theory,[NewTheory|RestTheory]) :-
delete_matching(Theory,Rule1,Theory1),
append(Theory1, [Rule2], NewTheory),
apply_operators(RestOps,Theory,RestTheory).
apply_operators([remove_body(Rule1,Rule2,_A)|RestOps],Theory,[NewTheory|RestTheory]) :-
delete_matching(Theory,Rule1,Theory1),
append(Theory1, [Rule2], NewTheory),
apply_operators(RestOps,Theory,RestTheory).
apply_operators([add_head(Rule1,Rule2,_A)|RestOps],Theory,[NewTheory|RestTheory]) :-
delete_matching(Theory,Rule1,Theory1),
append(Theory1, [Rule2], NewTheory),
apply_operators(RestOps,Theory,RestTheory).
apply_operators([remove_head(Rule1,Rule2,_A)|RestOps],Theory,[NewTheory|RestTheory]) :-
delete_matching(Theory,Rule1,Theory1),
append(Theory1, [Rule2], NewTheory),
apply_operators(RestOps,Theory,RestTheory).
apply_operators([remove(Rule)|RestOps],Theory,[NewTheory|RestTheory]) :-
delete_matching(Theory,Rule,NewTheory),
apply_operators(RestOps,Theory,RestTheory).
revise_theory(Theory,Ref):-
specialize_theory(Theory,Ref).
revise_theory(Theory,Ref):-
generalize_theory(Theory,Ref).
generalize_theory(Theory,Ref):-
Theory \== [],
choose_rule(Theory,Rule),
generalize_rule(Rule,Ref).
generalize_theory(Theory,Ref):-
length(Theory,LT),
setting(max_rules,MR),
LT<MR,
add_rule(Ref).
generalize_rule(Rule,Ref):-
generalize_head(Rule,Ref).
generalize_rule(Rule,Ref):-
generalize_body(Rule,Ref).
add_rule(add(rule(ID,Head,[]))):-
findall(HL , modeh(_,HL), HLS),
length(HLS,L),
L1 is L+1,
P is 1/L1,
generate_head(HLS,P,Head),
get_next_rule_number(ID).
generate_head([H|_T],_P,[H1:0.5,'':0.5]):-
H=..[Pred|Args],
take_const(Args,Args1),
H1=..[Pred|Args1].
generate_head([_H|T],P,Head):-
generate_head(T,P,Head).
take_const([],[]).
take_const([+A|T],[_V|T1]):-
atom(A),!,
take_const(T,T1).
take_const([-A|T],[_V|T1]):-
atom(A),!,
take_const(T,T1).
take_const([A|T],[A|T1]):-
take_const(T,T1).
generalize_head(Rule,Ref):-
Rule = rule(ID,LH,BL),
generalize_head1(LH,LH1,NewAt),
Ref = add_head(Rule,rule(ID,LH1,BL),NewAt).
generalize_head1(LH,LH1,NH):-
findall(HL , modeh(_,HL), HLS),
generalize_head2(HLS,LH,LH1,NH).
generalize_head2([X|_R],LH,LH1,PH) :-
X =.. [P|A],
length(A,LA),
length(A1,LA),
PH =.. [P|A1],
\+ member(PH:_, LH),
(setting(new_head_atoms_zero_prob,true)->
delete_matching(LH,'':PNull,LH0),
append(LH0,[PH:0.0,'':PNull],LH1)
;
length(LH,NH),
add_to_head(LH,NH,PH,LH1)
).
generalize_head2([_X|R],LH,LH1) :-
generalize_head2(R,LH,LH1).
add_to_head(['':PN],NH,At,[At:PA,'':PN1]):-!,
PN1 is PN*NH/(NH+1),
PA is 1/(NH+1).
add_to_head([H:PH|T],NH,At,[H:PH1|T1]):-
PH1 is PH*NH/(NH+1),
add_to_head(T,NH,At,T1).
get_module_var(LH,Module):-
member(H:_,LH),!,
H=..[_F,Module|_].
generalize_body(Rule,Ref):-
Rule = rule(ID,LH,BL),
delete_one(BL,BL1,A),
remove_prob(LH,LH1),
delete(LH1,'',LH2),
linked_clause(BL1,LH2),
Ref = remove_body(Rule,rule(ID,LH,BL1),A).
specialize_theory(Theory,Ref):-
Theory \== [],
choose_rule(Theory,Rule),
specialize_rule(Rule,SpecRule,Lit),
Ref = add_body(Rule,SpecRule,Lit).
specialize_rule(Rule,SpecRule,Lit):-
findall(BL , modeb(_,BL), BLS),
specialize_rule(BLS,Rule,SpecRule,Lit).
specialize_rule([Lit|_RLit],Rule,SpecRul,SLit):-
Rule = rule(ID,LH,BL),
remove_prob(LH,LH1),
append(LH1,BL,ALL),
specialize_rule1(Lit,ALL,SLit),
append(BL,[SLit],BL1),
lookahead(SLit,LLit1),
specailize_rule_la(LLit1,LH1,BL1,BL2),
append(LH1,BL2,ALL2),
extract_fancy_vars(ALL2,Vars1),
length(Vars1,NV),
setting(max_var,MV),
NV=<MV,
SpecRul = rule(ID,LH,BL2).
specialize_rule([Lit|_RLit],Rule,SpecRul,SLit):-
Rule = rule(ID,LH,BL),
remove_prob(LH,LH1),
append(LH1,BL,ALL),
specialize_rule1(Lit,ALL,SLit),
append(BL,[SLit],BL1),
append(LH1,BL1,ALL1),
extract_fancy_vars(ALL1,Vars1),
length(Vars1,NV),
setting(max_var,MV),
NV=<MV,
SpecRul = rule(ID,LH,BL1).
specialize_rule([_|RLit],Rule,SpecRul,Lit):-
specialize_rule(RLit,Rule,SpecRul,Lit).
specailize_rule_la([],_LH1,BL1,BL1).
specailize_rule_la([Lit1|T],LH1,BL1,BL3):-
copy_term(Lit1,Lit2),
modeb(_,Lit2),
append(LH1,BL1,ALL1),
specialize_rule1(Lit2,ALL1,SLit1),
append(BL1,[SLit1],BL2),
specailize_rule_la(T,LH1,BL2,BL3).
remove_prob(['':_P],[]):-!.
remove_prob([X:_],[X]):-!.
remove_prob([X:_|R],[X|R1]):-
remove_prob(R,R1).
specialize_rule1(Lit,Lits,SpecLit):-
Lit =.. [Pred|Args],
exctract_type_vars(Lits,TypeVars0),
remove_duplicates(TypeVars0,TypeVars),
take_var_args(Args,TypeVars,Args1),
SpecLit =.. [Pred|Args1],
\+ member_eq(SpecLit,Lits).
convert_to_input_vars([],[]):-!.
convert_to_input_vars([+T|RT],[+T|RT1]):-
!,
convert_to_input_vars(RT,RT1).
convert_to_input_vars([-T|RT],[+T|RT1]):-
convert_to_input_vars(RT,RT1).
member_eq(X,[Y|_List]) :-
X == Y.
member_eq(X,[_|List]) :-
member_eq(X,List).
remove_eq(X,[Y|R],R):-
X == Y,
!.
remove_eq(X,[_|R],R1):-
remove_eq(X,R,R1).
linked_clause(X):-
linked_clause(X,[]).
linked_clause([],_).
linked_clause([L|R],PrevLits):-
term_variables(PrevLits,PrevVars),
input_variables(L,InputVars),
linked(InputVars,PrevVars),!,
linked_clause(R,[L|PrevLits]).
linked([],_).
linked([X|R],L) :-
member_eq(X,L),
!,
linked(R,L).
input_variables(\+ LitM,InputVars):-
!,
LitM=..[P|Args],
length(Args,LA),
length(Args1,LA),
Lit1=..[P|Args1],
copy_term(LitM,Lit0),
modeb(_,Lit1),
Lit1 =.. [P|Args1],
convert_to_input_vars(Args1,Args2),
Lit2 =.. [P|Args2],
input_vars(Lit0,Lit2,InputVars).
input_variables(LitM,InputVars):-
LitM=..[P|Args],
length(Args,LA),
length(Args1,LA),
Lit1=..[P|Args1],
modeb(_,Lit1),
input_vars(LitM,Lit1,InputVars).
input_variables(LitM,InputVars):-
LitM=..[P|Args],
length(Args,LA),
length(Args1,LA),
Lit1=..[P|Args1],
modeh(_,Lit1),
input_vars(LitM,Lit1,InputVars).
input_vars(Lit,Lit1,InputVars):-
Lit =.. [_|Vars],
Lit1 =.. [_|Types],
input_vars1(Vars,Types,InputVars).
input_vars1([],_,[]).
input_vars1([V|RV],[+_T|RT],[V|RV1]):-
!,
input_vars1(RV,RT,RV1).
input_vars1([_V|RV],[_|RT],RV1):-
input_vars1(RV,RT,RV1).
exctract_type_vars([],[]).
exctract_type_vars([Lit|RestLit],TypeVars):-
Lit =.. [Pred|Args],
length(Args,L),
length(Args1,L),
Lit1 =.. [Pred|Args1],
take_mode(Lit1),
type_vars(Args,Args1,Types),
exctract_type_vars(RestLit,TypeVars0),
!,
append(Types,TypeVars0,TypeVars).
take_mode(Lit):-
modeh(_,Lit),!.
take_mode(Lit):-
modeb(_,Lit),!.
take_mode(Lit):-
mode(_,Lit),!.
type_vars([],[],[]).
type_vars([V|RV],[+T|RT],[V=T|RTV]):-
!,
type_vars(RV,RT,RTV).
type_vars([V|RV],[-T|RT],[V=T|RTV]):-atom(T),!,
type_vars(RV,RT,RTV).
type_vars([_V|RV],[_T|RT],RTV):-
type_vars(RV,RT,RTV).
take_var_args([],_,[]).
take_var_args([+T|RT],TypeVars,[V|RV]):-
!,
member(V=T,TypeVars),
take_var_args(RT,TypeVars,RV).
take_var_args([-T|RT],TypeVars,[_V|RV]):-
atom(T),
take_var_args(RT,TypeVars,RV).
take_var_args([-T|RT],TypeVars,[V|RV]):-
member(V=T,TypeVars),
take_var_args(RT,TypeVars,RV).
take_var_args([T|RT],TypeVars,[T|RV]):-
T\= + _,(T\= - _; T= - A,number(A)),
take_var_args(RT,TypeVars,RV).
choose_rule(Theory,Rule):-
member(Rule,Theory).
add_rule(Theory,add(rule(ID,H,[]))):-
new_id(ID),
findall(HL , modeh(_,HL), HLS),
length(HLS,NH),
P is 1/(NH+1),
add_probs(HLS,H,P),
\+ member(rule(_,H,[]),Theory).
add_rule(Theory,TheoryGen):-
findall(HL , modeh(_,HL), HLS),
add_rule(HLS,Theory,TheoryGen).
add_rule([X|_R],Theory,TheoryGen) :-
new_id(ID),
X =.. [P|A],
length(A,LA),
length(A1,LA),
PH =.. [P|A1],
TheoryGen = add(rule(ID,[PH:0.5,'':0.5],[])),
\+ member(rule(_,[PH:_,'':_],[]),Theory).
add_rule([_X|R],Theory,TheoryGen) :-
add_rule(R,Theory,TheoryGen).
add_probs([],['':P],P):-!.
add_probs([H|T],[H:P|T1],P):-
add_probs(T,T1,P).
extract_fancy_vars(List,Vars):-
term_variables(List,Vars0),
fancy_vars(Vars0,1,Vars).
fancy_vars([],_,[]).
fancy_vars([X|R],N,[NN2=X|R1]):-
name(N,NN),
append([86],NN,NN1),
name(NN2,NN1),
N1 is N + 1,
fancy_vars(R,N1,R1).
delete_one([X|R],R,X).
delete_one([X|R],[X|R1],D):-
delete_one(R,R1,D).
remove_last([_X],[]) :-
!.
remove_last([X|R],[X|R1]):-
remove_last(R,R1).
delete_matching([],_El,[]).
delete_matching([El|T],El,T1):-!,
delete_matching(T,El,T1).
delete_matching([H|T],El,[H|T1]):-
delete_matching(T,El,T1).

View File

@ -0,0 +1,850 @@
/*
EMBLEM and SLIPCASE
Copyright (c) 2011, Fabrizio Riguzzi and Elena Bellodi
*/
:-use_module(library(lists)).
:-use_module(library(random)).
:-use_module(library(system)).
:-dynamic setting/2,last_id/1, rule/5.
:-[revise].
setting(epsilon_em,0.0001).
setting(epsilon_em_fraction,0.00001).
setting(eps,0.0001).
setting(eps_f,0.00001).
/* if the difference in log likelihood in two successive em iteration is smaller
than epsilon_em, then em stops */
setting(epsilon_sem,2).
/* number of random restarts of em */
setting(random_restarts_REFnumber,1).
setting(random_restarts_number,1).
setting(iterREF,-1).
setting(iter,-1).
setting(examples,atoms).
setting(group,1).
setting(d,1).
setting(verbosity,1).
setting(logzero,log(0.000001)).
setting(initial_clauses_modeh,1).
setting(max_iter,10).
setting(max_var,5).
setting(max_rules,10).
setting(beamsize,20).
sl(File):-
generate_file_names(File,FileKB,FileIn,FileBG,FileOut,FileL),
reconsult(FileL),
load_models(FileKB,DB),
statistics(walltime,[_,_]),
(file_exists(FileBG)->
set(compiling,on),
load(FileBG,_ThBG,RBG),
set(compiling,off),
generate_clauses(RBG,_RBG1,0,[],ThBG),
assert_all(ThBG)
;
true
),
(file_exists(FileIn)->
set(compiling,on),
load(FileIn,_Th1,R1),
set(compiling,off)
;
deduct(DB,[],InitialTheory),
length(InitialTheory,_LI), %-LI=number of rules of the theory
remove_duplicates(InitialTheory,Th0),
length(Th0,_LI1),
set(compiling,on),
process_clauses(Th0,[],_Th1,[],R1), %+Th0: rules Head:-body with prob. in the head,-_Th1: rules in the same form as Th0 but with bdds one/and+getvarn+equality, R1: same theory of the form rule(NR,[head atoms with prob. (with null),[body atoms].
set(compiling,off)
),
write('Initial theory'),nl,
write_rules(R1,user_output),
learn_struct(DB,R1,R2,CLL2),
learn_params(DB,R2,R,CLL),
statistics(walltime,[_,WT]),
WTS is WT/1000,
format("~nRefinement CLL ~f - CLL after EMBLEM ~f~n",[CLL2,CLL]),
format("Total execution time ~f~n~n",[WTS]),
write_rules(R,user_output),
listing(setting/2),
open(FileOut,write,Stream),
format(Stream,'/* SLIPCASE Final CLL ~f~n',[CLL]),
format(Stream,'Execution time ~f~n',[WTS]),
tell(Stream),
listing(setting/2),
format(Stream,'*/~n~n',[]),
told,
open(FileOut,append,Stream1),
write_rules(R,Stream1),
close(Stream1).
learn_struct(DB,R1,R,CLL1):- %+R1:initial theory of the form [rule(NR,[h],[b]],...], -R:final theory of the same form, -CLL1
generate_clauses(R1,R2,0,[],Th1),
assert_all(Th1),
assert_all(R2),!,
findall(R-HN,(rule(R,HL,_BL),length(HL,HN)),L),
keysort(L,LS),
get_heads(LS,LSH),
length(LSH,NR),
init(NR,LSH),
retractall(v(_,_,_)),
length(DB,NEx),
(setting(examples,atoms)->
setting(group,G),
derive_bdd_nodes_groupatoms(DB,NEx,G,[],Nodes,0,CLL0,LE,[]),! % 1 bdd for group of facts (for example if group=1)
;
derive_bdd_nodes(DB,NEx,[],Nodes,0,CLL0),! % 1 bdd for model
),
setting(random_restarts_number,N),
format("~nInitial CLL ~f~n~n",[CLL0]),
random_restarts(N,Nodes,CLL0,CLL,initial,Par,LE), %output:CLL,Par
format("CLL after EMBLEM = ~f~n",[CLL]),
retract_all(Th1),
retract_all(R2),!,
end, %frees all variables
update_theory(R2,Par,R3),
write('updated Theory'),nl,
write_rules(R3,user_output), %definite rules without probabilities in the head are not written
setting(max_iter,M),
cycle_struct([(R3,CLL)],DB,R3,R,M,CLL,-inf,CLL1).
em(File):-
generate_file_names(File,FileKB,FileIn,FileBG,FileOut,FileL),
reconsult(FileL),
load_models(FileKB,DB),
(file_exists(FileBG)->
set(compiling,on),
load(FileBG,_ThBG,RBG),
set(compiling,off),
generate_clauses(RBG,_RBG1,0,[],ThBG),
assert_all(ThBG)
;
true
),
set(compiling,on),
load(FileIn,_TH,R0),
set(compiling,off),
set(verbosity,3),
statistics(cputime,[_,_]),
learn_params(DB,R0,R,CLL),
statistics(cputime,[_,CT]),
CTS is CT/1000,
format("EM: Final CLL ~f~n",[CLL]),
format("Execution time ~f~n~n",[CTS]),
write_rules(R,user_output),
listing(setting/2),
open(FileOut,write,Stream),
format(Stream,'/* EMBLEM Final CLL ~f~n',[CLL]),
format(Stream,'Execution time ~f~n',[CTS]),
tell(Stream),
listing(setting/2),
format(Stream,'*/~n~n',[]),
told,
open(FileOut,append,Stream1),
write_rules(R,Stream1),
close(Stream1).
learn_params(DB,R0,R,CLL):-
generate_clauses(R0,R1,0,[],Th0),
assert_all(Th0),
assert_all(R1),!,
findall(R-HN,(rule(R,HL,_BL),length(HL,HN)),L),
keysort(L,LS),
get_heads(LS,LSH),
length(LSH,NR),
init(NR,LSH),
retractall(v(_,_,_)),
length(DB,NEx),
(setting(examples,atoms)->
setting(group,G),
derive_bdd_nodes_groupatoms(DB,NEx,G,[],Nodes,0,CLL0,LE,[]),!
;
derive_bdd_nodes(DB,NEx,[],Nodes,0,CLL0),!
),
setting(random_restarts_number,N),
random_restarts(N,Nodes,CLL0,CLL,initial,Par,LE), %computes new parameters Par
end,
retract_all(Th0),
retract_all(R1),!,
update_theory(R1,Par,R). %replaces in R1 the probabilities Par and outputs R
update_theory(R,initial,R):-!.
update_theory([],_Par,[]).
update_theory([def_rule(H,B)|T0],Par,[def_rule(H,B)|T]):-!,
update_theory(T0,Par,T).
update_theory([(H:-B)|T0],Par,[(H:-B)|T]):-!,
update_theory(T0,Par,T).
update_theory([rule(N,_H,_B)|T0],Par,T):-
member([N,[1.0|_T]],Par),!,
update_theory(T0,Par,T).
update_theory([rule(N,H,B)|T0],Par,[rule(N,H1,B)|T]):-
member([N,P],Par),!,
reverse(P,P1),
update_head_par(H,P1,H1),
update_theory(T0,Par,T).
update_head_par([],[],[]).
update_head_par([H:_P|T0],[HP|TP],[H:HP|T]):-
update_head_par(T0,TP,T).
cycle_struct([],_DB,R,R,_M,S,_SP,S):-!.
cycle_struct(_B,_DB,R,R,_M,S,SP,S):-
setting(eps,Eps),
setting(eps_f,EpsF),
(
(S-SP)<Eps
;
(X is -S*EpsF,
Y is S-SP,
Y<X)
),
!.
cycle_struct(_Beam,_DB,R,R,0,S,_SP,S):-!.
cycle_struct([(RH,_ScoreH)|T],DB,R0,R,M,Score0,SP0,Score):-
format("Iteration ~d",[M]),nl,nl,
theory_revisions(RH,LR),!, %+R1=rule(NR,[head],[body]), -LR:list of lists, each correponding to a different revised theory
length(LR,NR),%NR:number of different revised theories
write('Number of revisions '),write(NR),nl,
score_refinements(LR,T,T1,1,NR,DB,R0,Score0,SP0,R3,S3,SP),
%-SP, -R3: the best score SP and refined theory R3 (in the form: rule(NR,[head],[body])), from the set of theories generated in revise.pl
write('Best refinement:'),nl,
write_rules(R3,user_output),
M1 is M-1,%decreases the number of max_iter M
format("~nBest score (CLL) ~f~n~n",[S3]),
cycle_struct(T1,DB,R3,R,M1,S3,SP,Score).
score_refinements([],B,B,_N,_NR,_DB,R,S,SP,R,S,SP).
score_refinements([R1|T],B0,B,Nrev,NRef,DB,R0,S0,SP0,R,S,SP):- %scans the list of revised theories; returns S,R, the best (highest) score and revised theory R,after the comparisons at the end
format('Score ref. ~d of ~d~n',[Nrev,NRef]),
write_rules(R1,user_output),
generate_clauses(R1,R2,0,[],Th1),
assert_all(Th1),
assert_all(R2),!,
findall(RN-HN,(rule(RN,HL,_BL),length(HL,HN)),L),
keysort(L,LS),
get_heads(LS,LSH),
length(LSH,NR),
init(NR,LSH),
retractall(v(_,_,_)),
length(DB,NEx),
(setting(examples,atoms)->
setting(group,G),
derive_bdd_nodes_groupatoms(DB,NEx,G,[],Nodes,0,CLL0,LE,[]),!
;
derive_bdd_nodes(DB,NEx,[],Nodes,0,CLL0),!
),
setting(random_restarts_REFnumber,N),
random_restarts_ref(N,Nodes,CLL0,CLL,initial,Par,LE),
end,
update_theory(R2,Par,R3),
write('Updated refinement'),nl,
write_rules(R3,user_output),
Score = CLL,
write('Score (CLL) '),write(Score),nl,nl,nl,
retract_all(Th1),
retract_all(R2),!,
/*compares the score and theory found so far with the latest refinement R1 and associated score*/
(Score>S0->
R4=R3,
S4=Score,
SP1=S0
;
R4=R0,
S4=S0,
SP1=SP0
),
setting(beamsize,BS),
insert_in_order(B0,(R3,Score),BS,B1),
Nrev1 is Nrev+1,
score_refinements(T,B1,B,Nrev1,NRef,DB,R4,S4,SP1,R,S,SP).
insert_in_order([],C,BeamSize,[C]):-
BeamSize>0,!.
insert_in_order(Beam,_New,0,Beam):-!.
insert_in_order([(Th1,Heuristic1)|RestBeamIn],(Th,Heuristic),BeamSize,BeamOut):-
Heuristic>Heuristic1,!,
% larger heuristic, insert here
NewBeam=[(Th,Heuristic),(Th1,Heuristic1)|RestBeamIn],
length(NewBeam,L),
(L>BeamSize->
nth(L,NewBeam,_Last,BeamOut)
;
BeamOut=NewBeam
).
insert_in_order([(Th1,Heuristic1)|RestBeamIn],(Th,Heuristic),BeamSize,
[(Th1,Heuristic1)|RestBeamOut]):-
BeamSize1 is BeamSize -1,
insert_in_order(RestBeamIn,(Th,Heuristic),BeamSize1,
RestBeamOut).
remove_int_atom_list([],[]).
remove_int_atom_list([A|T],[A1|T1]):-
A=..[F,_|Arg],
A1=..[F|Arg],
remove_int_atom_list(T,T1).
remove_int_atom(A,A1):-
A=..[F,_|T],
A1=..[F|T].
get_heads([],[]).
get_heads([_-H|T],[H|TN]):-
get_heads(T,TN).
derive_bdd_nodes([],_E,Nodes,Nodes,CLL,CLL).
derive_bdd_nodes([H|T],E,Nodes0,Nodes,CLL0,CLL):-
get_output_atoms(O),
generate_goal(O,H,[],GL),
(prob(H,P)->
CardEx is P*E
;
CardEx is 1.0
),
init_bdd,
one(One),
get_node_list(GL,One,BDD,CardEx),
ret_prob(BDD,HP),
(HP=:=0.0->
setting(logzero,LZ),
CLL1 is CLL0+LZ*CardEx
;
CLL1 is CLL0+log(HP)*CardEx
),
end_bdd,
append(Nodes0,[[BDD,CardEx]],Nodes1),
derive_bdd_nodes(T,E,Nodes1,Nodes,CLL1,CLL).
get_node_list([],BDD,BDD,_CE).
get_node_list([H|T],BDD0,BDD,CE):-
get_node(H,BDD1),
and(BDD0,BDD1,BDD2),
get_node_list(T,BDD2,BDD,CE).
derive_bdd_nodes_groupatoms([],_E,_G,Nodes,Nodes,CLL,CLL,LE,LE).
derive_bdd_nodes_groupatoms([H|T],E,G,Nodes0,Nodes,CLL0,CLL,LE0,LE):- %[H|T] models
get_output_atoms(O),
generate_goal(O,H,[],GL),
length(GL,NA),
(prob(H,P)->
CardEx is P*E/NA
;
CardEx is 1.0/NA
),
get_node_list_groupatoms(GL,BDDs,CardEx,G,CLL0,CLL1,LE0,LE1),
append(Nodes0,BDDs,Nodes1),
derive_bdd_nodes_groupatoms(T,E,G,Nodes1,Nodes,CLL1,CLL,LE1,LE).
get_node_list_groupatoms([],[],_CE,_Gmax,CLL,CLL,LE,LE).
get_node_list_groupatoms([H|T],[[BDD,CE1]|BDDT],CE,Gmax,CLL0,CLL,LE0,LE):-
init_bdd,
one(One),
get_bdd_group([H|T],T1,Gmax,G,One,BDD,CE,LE0,LE1), %output=BDD,CLL
CE1 is CE*(Gmax-G),
ret_prob(BDD,HP),
end_bdd,
(HP =:=0.0->
setting(logzero,LZ),
CLL2 is CLL0+LZ*CE1
;
CLL2 is CLL0+log(HP)*CE1
),
get_node_list_groupatoms(T1,BDDT,CE,Gmax,CLL2,CLL,LE1,LE).
get_bdd_group([],[],G,G,BDD,BDD,_CE,LE,LE):-!.
get_bdd_group(T,T,0,0,BDD,BDD,_CE,LE,LE):- !.
get_bdd_group([H|T],T1,Gmax,G1,BDD0,BDD,CE,[H|LE0],LE):-
get_node(H,BDD1), %creates bdd for atomo H
and(BDD0,BDD1,BDD2),
G is Gmax-1,
get_bdd_group(T,T1,G,G1,BDD2,BDD,CE,LE0,LE).
/* EM start */
random_restarts(0,_Nodes,CLL,CLL,Par,Par,_LE):-!.
random_restarts(N,Nodes,CLL0,CLL,Par0,Par,LE):-
setting(verbosity,Ver),
(Ver>2->
setting(random_restarts_number,NMax),
Num is NMax-N+1,
format("Restart number ~d~n~n",[Num]),
flush_output
;
true
),
randomize,
setting(epsilon_em,EA),
setting(epsilon_em_fraction,ER),
length(Nodes,L),
setting(iter,Iter),
em(Nodes,EA,ER,L,Iter,CLLR,Par1),
setting(verbosity,Ver),
(Ver>2->
format("Random_restart: CLL ~f~n",[CLLR])
;
true
),
N1 is N-1,
(CLLR>CLL0->
random_restarts(N1,Nodes,CLLR,CLL,Par1,Par,LE)
;
random_restarts(N1,Nodes,CLL0,CLL,Par0,Par,LE)
).
random_restarts_ref(0,_Nodes,CLL,CLL,Par,Par,_LE):-!.
random_restarts_ref(N,Nodes,CLL0,CLL,Par0,Par,LE):-
setting(verbosity,Ver),
(Ver>2->
setting(random_restarts_REFnumber,NMax),
Num is NMax-N+1,
format("Restart number ~d~n~n",[Num]),
flush_output
;
true
),
setting(epsilon_em,EA),
setting(epsilon_em_fraction,ER),
length(Nodes,L),
setting(iterREF,Iter),
em(Nodes,EA,ER,L,Iter,CLLR,Par1),
setting(verbosity,Ver),
(Ver>2->
format("Random_restart: CLL ~f~n",[CLLR])
;
true
),
N1 is N-1,
(CLLR>CLL0->
random_restarts_ref(N1,Nodes,CLLR,CLL,Par1,Par,LE)
;
random_restarts_ref(N1,Nodes,CLL0,CLL,Par0,Par,LE)
).
randomize([],[]):-!.
randomize([rule(N,V,NH,HL,BL,LogF)|T],[rule(N,V,NH,HL1,BL,LogF)|T1]):-
length(HL,L),
Int is 1.0/L,
randomize_head(Int,HL,0,HL1),
randomize(T,T1).
randomize_head(_Int,['':_],P,['':PNull1]):-!,
PNull is 1.0-P,
(PNull>=0.0->
PNull1 =PNull
;
PNull1=0.0
).
randomize_head(Int,[H:_|T],P,[H:PH1|NT]):-
PMax is 1.0-P,
random(0,PMax,PH1),
P1 is P+PH1,
randomize_head(Int,T,P1,NT).
update_head([],[],_N,[]):-!.
update_head([H:_P|T],[PU|TP],N,[H:P|T1]):-
P is PU/N,
update_head(T,TP,N,T1).
/* EM end */
/* utilities */
generate_file_names(File,FileKB,FileIn,FileBG,FileOut,FileL):-
generate_file_name(File,".kb",FileKB),
generate_file_name(File,".cpl",FileIn),
generate_file_name(File,".rules",FileOut),
generate_file_name(File,".bg",FileBG),
generate_file_name(File,".l",FileL).
generate_file_name(File,Ext,FileExt):-
name(File,FileString),
append(FileString,Ext,FileStringExt),
name(FileExt,FileStringExt).
load_models(File,ModulesList):- %carica le interpretazioni, 1 alla volta
open(File,read,Stream),
read_models(Stream,ModulesList),
close(Stream).
read_models(Stream,[Name1|Names]):-
read(Stream,begin(model(Name))),!,
(number(Name)->
name(Name,NameStr),
append("i",NameStr,Name1Str),
name(Name1,Name1Str)
;
Name1=Name
),
read_all_atoms(Stream,Name1),
read_models(Stream,Names).
read_models(_S,[]).
read_all_atoms(Stream,Name):-
read(Stream,At),
At \=end(model(_Name)),!,
(At=neg(Atom)->
Atom=..[Pred|Args],
Atom1=..[Pred,Name|Args],
assertz(neg(Atom1))
;
(At=prob(Pr)->
assertz(prob(Name,Pr))
;
At=..[Pred|Args],
Atom1=..[Pred,Name|Args],
assertz(Atom1)
)
),
read_all_atoms(Stream,Name).
read_all_atoms(_S,_N).
write_param(initial,S):-!,
format("~nInitial parameters~n",[]),
findall(rule(R,H,B),rule(R,H,B),LDis),
findall(rule(d,[H:1.0],B),def_rule(H,B),LDef),
append(LDis,LDef,L),
write_model(L,S).
write_param(L,S):-
reverse(L,L1),
write_par(L1,S).
write_par([],S):-
findall(rule(d,[H:1.0],B),def_rule(H,B),L),
write_model(L,S).
write_par([[N,P]|T],S):-
rule(N,HL0,BL),
reverse(P,PR),
new_par(PR,HL0,HL),
copy_term((HL,BL),(HL1,BL1)),
numbervars((HL1,BL1),0,_M),
write_disj_clause(S,(HL1:-BL1)),
write_par(T,S).
write_rules([],_S).
write_rules([rule(_N,HL,BL)|T],S):-
copy_term((HL,BL),(HL1,BL1)),
numbervars((HL1,BL1),0,_M),
write_disj_clause(S,(HL1:-BL1)),
write_rules(T,S).
new_par([],[],[]).
new_par([HP|TP],[Head:_|TO],[Head:HP|TN]):-
new_par(TP,TO,TN).
write_model([],_Stream):-!.
write_model([rule(_N,HL,BL)|Rest],Stream):-
copy_term((HL,BL),(HL1,BL1)),
numbervars((HL1,BL1),0,_M),
write_disj_clause(Stream,(HL1:-BL1)),
write_model(Rest,Stream).
write_disj_clause(S,(H:-[])):-!,
write_head(S,H),
format(S,".~n~n",[]).
write_disj_clause(S,(H:-B)):-
write_head(S,H),
write(S,' :-'),
nl(S),
write_body(S,B).
write_head(S,[A:1.0|_Rest]):-!,
format(S,"~p",[A]).
write_head(S,[A:P,'':_P]):-!,
format(S,"~p:~g",[A,P]).
write_head(S,[A:P]):-!,
format(S,"~p:~g",[A,P]).
write_head(S,[A:P|Rest]):-
format(S,"~p:~g ; ",[A,P]),
write_head(S,Rest).
write_body(S,[A]):-!,
format(S,"\t~p.~n~n",[A]).
write_body(S,[A|T]):-
format(S,"\t~p,~n",[A]),
write_body(S,T).
list2or([],true):-!.
list2or([X],X):-
X\=;(_,_),!.
list2or([H|T],(H ; Ta)):-!,
list2or(T,Ta).
list2and([],true):-!.
list2and([X],X):-
X\=(_,_),!.
list2and([H|T],(H,Ta)):-!,
list2and(T,Ta).
deduct([],Th,Th).
deduct([M|T],InTheory0,InTheory):-
get_head_atoms(O),
generate_head(O,M,[],HL),
generate_body(HL,InTheory1),
append(InTheory0,InTheory1,InTheory2),
deduct(T,InTheory2,InTheory).
get_head_atoms(O):-
findall(A,modeh(_,A),O).
generate_head([],_M,HL,HL):-!.
generate_head([A|T],M,H0,H1):-
functor(A,F,N),
functor(F1,F,N),
F1=..[F|Arg],
Pred1=..[F,M|Arg],
findall((A,Pred1),call(neg(Pred1)),L),
setting(initial_clauses_modeh,IC), %IC: represents how many samples are extracted from the list L of example
sample(IC,L,L1), %+IC,L, -L1
append(H0,L1,H2),
generate_head(T,M,H2,H1).
sample(0,_List,[]):-!.
sample(N,List,List):-
length(List,L),
L=<N,!.
sample(N,List,[El|List1]):-
length(List,L),
random(0,L,Pos),
nth0(Pos,List,El,Rest),
N1 is N-1,
sample(N1,Rest,List1).
generate_body([],[]):-!.
generate_body([(A,H)|T],[(Head:0.5:-Body)|CL0]):-
findall((R,B),modeb(R,B),BL),
A=..[F|ArgsTypes],
H=..[F,M|Args],
setting(d,D),
cycle_modeb(ArgsTypes,Args,[],[],BL,a,[],BLout0,D,M),
remove_duplicates(BLout0,BLout),
variabilize((H:-BLout),CLV), %+(Head):-Bodylist; -CLV:(Head):-Bodylist with variables _num in place of constants
copy_term((H:-BLout),CLa),
numbervars(CLa,0,_N1),
copy_term(CLV,CLav),
numbervars(CLav,0,_N1v),
CLV=(Head1:-BodyList1),
remove_int_atom(Head1,Head),
remove_int_atom_list(BodyList1,BodyList),
list2and(BodyList,Body),
generate_body(T,CL0).
variabilize((H:-B),(H1:-B1)):-
variabilize_list([H|B],[H1|B1],[],_AS,_M).
variabilize_list([],[],A,A,_M).
variabilize_list([H|T],[H1|T1],A0,A,M):-
H=..[F,_M|Args],
variabilize_args(Args,Args1,A0,A1),
H1=..[F,M|Args1],
variabilize_list(T,T1,A1,A,M).
variabilize_args([],[],A,A).
variabilize_args([C|T],[V|TV],A0,A):-
member(C/V,A0),!,
variabilize_args(T,TV,A0,A).
variabilize_args([C|T],[V|TV],A0,A):-
variabilize_args(T,TV,[C/V|A0],A).
cycle_modeb(ArgsTypes,Args,ArgsTypes,Args,_BL,L,L,L,_,_M):-!.
cycle_modeb(_ArgsTypes,_Args,_ArgsTypes1,_Args1,_BL,_L,L,L,0,_M):-!.
cycle_modeb(ArgsTypes,Args,_ArgsTypes0,_Args0,BL,_L0,L1,L,D,M):-
find_atoms(BL,ArgsTypes,Args,ArgsTypes1,Args1,L1,L2,M),
D1 is D-1,
cycle_modeb(ArgsTypes1,Args1,ArgsTypes,Args,BL,L1,L2,L,D1,M).
find_atoms([],ArgsTypes,Args,ArgsTypes,Args,L,L,_M).
find_atoms([(R,H)|T],ArgsTypes0,Args0,ArgsTypes,Args,L0,L1,M):-
H=..[F|ArgsT],
findall(A,instantiate_query(ArgsT,ArgsTypes0,Args0,F,M,A),L),
call_atoms(L,[],At),
remove_duplicates(At,At1),
(R = '*' ->
R1= +inf
;
R1=R
),
sample(R1,At1,At2),
extract_output_args(At2,ArgsT,ArgsTypes0,Args0,ArgsTypes1,Args1),
append(L0,At2,L2),
find_atoms(T,ArgsTypes1,Args1,ArgsTypes,Args,L2,L1,M).
call_atoms([],A,A).
call_atoms([H|T],A0,A):-
findall(H,H,L),
append(A0,L,A1),
call_atoms(T,A1,A).
extract_output_args([],_ArgsT,ArgsTypes,Args,ArgsTypes,Args).
extract_output_args([H|T],ArgsT,ArgsTypes0,Args0,ArgsTypes,Args):-
H=..[_F,_M|ArgsH],
add_const(ArgsH,ArgsT,ArgsTypes0,Args0,ArgsTypes1,Args1),
extract_output_args(T,ArgsT,ArgsTypes1,Args1,ArgsTypes,Args).
add_const([],[],ArgsTypes,Args,ArgsTypes,Args).
add_const([_A|T],[+_T|TT],ArgsTypes0,Args0,ArgsTypes,Args):-!,
add_const(T,TT,ArgsTypes0,Args0,ArgsTypes,Args).
add_const([A|T],[-Type|TT],ArgsTypes0,Args0,ArgsTypes,Args):-
(already_present(ArgsTypes0,Args0,A,Type)->
ArgsTypes1=ArgsTypes0,
Args1=Args0
;
ArgsTypes1=[+Type|ArgsTypes0],
Args1=[A|Args0]
),
add_const(T,TT,ArgsTypes1,Args1,ArgsTypes,Args).
already_present([+T|_TT],[C|_TC],C,T):-!.
already_present([_|TT],[_|TC],C,T):-
already_present(TT,TC,C,T).
instantiate_query(ArgsT,ArgsTypes,Args,F,M,A):-
instantiate_input(ArgsT,ArgsTypes,Args,ArgsB),
A=..[F,M|ArgsB].
instantiate_input([],_AT,_A,[]).
instantiate_input([-_Type|T],AT,A,[_V|TA]):-!,
instantiate_input(T,AT,A,TA).
instantiate_input([+Type|T],AT,A,[H|TA]):-
find_val(AT,A,+Type,H),
instantiate_input(T,AT,A,TA).
find_val([T|_TT],[A|_TA],T,A).
find_val([_T|TT],[_A|TA],T,A):-
find_val(TT,TA,T,A).
get_output_atoms(O):-
findall((A/Ar),output((A/Ar)),O).
generate_goal([],_H,G,G):-!.
generate_goal([P/A|T],H,G0,G1):-
functor(Pred,P,A),
Pred=..[P|Rest],
Pred1=..[P,H|Rest],
findall(Pred1,call(Pred1),L),
findall(\+ Pred1,call(neg(Pred1)),LN),
append(G0,L,G2),
append(G2,LN,G3),
generate_goal(T,H,G3,G1).
:-[inference_sl].