added cplint directory

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1966 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
rzf 2007-10-22 12:22:49 +00:00
parent ff4aa369be
commit 5a19adf881
33 changed files with 4083 additions and 0 deletions

27
cplint/Makefile Normal file
View File

@ -0,0 +1,27 @@
OBJECTS=cplint_yap.o cplint_Prob.o
.PHONY : clean
# -lutil -lst -lepd libutil.a /usr/local/bin/yap.dll \
cplint: $(OBJECTS)
gcc -mno-cygwin -shared -o cplint.dll $(OBJECTS) "C:\Software\BDD\cudd-2.4.1 mingw\cudd-2.4.1\cudd\libcudd.a" \
"C:\Software\BDD\cudd-2.4.1 mingw\cudd-2.4.1\mtr\libmtr.a" "C:\Software\BDD\cudd-2.4.1 mingw\cudd-2.4.1\st\libst.a" \
"C:\Software\BDD\cudd-2.4.1 mingw\cudd-2.4.1\util\libutil.a" "C:\Software\BDD\cudd-2.4.1 mingw\cudd-2.4.1\epd\libepd.a" \
"C:\Software\BDD\glu-2.1\libglu.a" "C:\Yap\bin\yap.dll" \
"C:\zipped\Glib windows\glib-2.12.11\bin\libglib-2.0-0.dll"
cplint_yap.o: cplint_yap.c cplint.h
gcc -mno-cygwin -I/usr/local \
-I/usr/include/glib-1.2 -I/lib/glib/include -I"c:/Yap/include" \
-I"C:\Software\BDD\Somenzi\glu-2.1\include" \
-I"C:\Software\BDD\cudd-2.4.1\cudd-2.4.1\include" \
-c -shared cplint_yap.c
cplint_Prob.o: cplint_Prob.c cplint.h
gcc -mno-cygwin -I/usr/local \
-I/usr/include/glib-1.2 -I/lib/glib/include -I"c:/Yap/include" \
-I/cygdrive/c/Software/BDD/Somenzi/glu-2.1/include \
-I"C:\Software\BDD\cudd-2.4.1\cudd-2.4.1\include" \
-c -shared cplint_Prob.c
clean:
rm $(OBJECTS) cpint.so

19
cplint/README Normal file
View File

@ -0,0 +1,19 @@
This directory contains the code of the LPAD anc CP-logic interpreter cplint
COMPILATION:
This package requires CUDD, GLU (a subpackage of VIS) and GLIB.
You can download CUDD from http://vlsi.colorado.edu/~fabio/
You can download GLU from http://vlsi.colorado.edu/~vis/getting_VIS_2.1.html
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
FEEDBACK:
Send feedback to:
Fabrizio Riguzzi
University of Ferrara
Dept. of Engineering
fabrizio.riguzzi@unife.it
http://www.ing.unife.it/Docenti/FabrizioRiguzzi/

80
cplint/cplint.h Normal file
View File

@ -0,0 +1,80 @@
/*
LPAD and CP-Logic interpreter
Copyright (c) 2007, Fabrizio Riguzzi
For the use of cudd:
Copyright (c) 1995-2004, Regents of the University of Colorado
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the University of Colorado nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "util.h"
#include "cuddInt.h"
#include "array.h"
#include "mtr.h"
#include "avl.h"
#include "Yap/YapInterface.h"
#include <glib.h>
typedef struct
{
int var,value;
} factor;
typedef struct
{
int nVal,nBit;
array_t * probabilities;
array_t * booleanVars;
} variable;
void createVars(array_t * vars, YAP_Term t,DdManager * mgr, array_t * bVar2mVar, char inames[1000][20]);
void createExpression(array_t * expression, YAP_Term t);
static int compute_prob(void);
void init_my_predicates();
int compare(char *a, char *b);
gint my_equal(gconstpointer v,gconstpointer v2);
guint my_hash(gconstpointer key);
void dealloc(gpointer key,gpointer value,gpointer user_data);
DdNode * retFunction(DdManager * mgr, array_t * expression,array_t * v);
DdNode * retTerm(DdManager * mgr,array_t *factors,array_t * v);
DdNode * retFactor(DdManager * mgr, factor f, array_t * v);
double Prob(DdNode *node, array_t * vars,array_t * bVar2mVar, GHashTable * nodes);
double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v,
array_t * vars,array_t * bVar2mVar, GHashTable * nodes);

722
cplint/cplint.yap Normal file
View File

@ -0,0 +1,722 @@
/*
LPAD and CP-Logic interpreter
Copyright (c) 2007, Fabrizio Riguzzi
*/
:-dynamic rule/4,def_rule/2,setting/2.
:-use_module(library(lists)).
:-use_module(library(ugraphs)).
:-load_foreign_files(['cplint'],[],init_my_predicates).
/* start of list of parameters that can be set by the user with
set(Parameter,Value) */
setting(epsilon_parsing,0.00001).
setting(savedot,false).
/* 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 */
s(GoalsList,Prob):-
solve(GoalsList,Prob).
solve(GoalsList,Prob):-
setof(Deriv,find_deriv(GoalsList,Deriv),LDup),
rem_dup_lists(LDup,L),
build_formula(L,Formula,[],Var),
var2numbers(Var,0,NewVar),
(setting(savedot,true)->
format("Variables: ~p~n",[Var]),
compute_prob(NewVar,Formula,Prob,1)
;
compute_prob(NewVar,Formula,Prob,0)
).
solve(GoalsList,0):-
\+ find_deriv(GoalsList,_Deriv).
find_deriv(GoalsList,Deriv):-
solve(GoalsList,[],DerivDup),
remove_duplicates(DerivDup,Deriv).
/* duplicate can appear in the C set because two different unistantiated clauses may become the
same clause when instantiated */
/* sc(Goals,Evidence,Prob) compute the conditional probability of the list of goals
Goals given the list of goals Evidence
Goals and Evidence can have variables, sc returns in backtracking all the solutions with their
corresponding probability
if it fails, the conditional probability is undefined
*/
sc(Goals,Evidence,Prob):-
solve_cond(Goals,Evidence,Prob).
solve_cond(Goals,Evidence,Prob):-
setof(DerivE,find_deriv(Evidence,DerivE),LDupE),
rem_dup_lists(LDupE,LE),
build_formula(LE,FormulaE,[],VarE),
var2numbers(VarE,0,NewVarE),
compute_prob(NewVarE,FormulaE,ProbE,0),
solve_cond_goals(Goals,LE,ProbGE),
Prob is ProbGE/ProbE.
solve_cond_goals(Goals,LE,ProbGE):-
setof(DerivGE,find_deriv_GE(LE,Goals,DerivGE),LDupGE),
rem_dup_lists(LDupGE,LGE),
build_formula(LGE,FormulaGE,[],VarGE),
var2numbers(VarGE,0,NewVarGE),
call_compute_prob(NewVarGE,FormulaGE,ProbGE).
solve_cond_goals(Goals,LE,0):-
\+ find_deriv_GE(LE,Goals,_DerivGE).
call_compute_prob(NewVarGE,FormulaGE,ProbGE):-
(setting(savedot,true)->
format("Variables: ~p~n",[NewVarGE]),
compute_prob(NewVarGE,FormulaGE,ProbGE,1)
;
compute_prob(NewVarGE,FormulaGE,ProbGE,0)
).
find_deriv_GE(LD,GoalsList,Deriv):-
member(D,LD),
solve(GoalsList,D,DerivDup),
remove_duplicates(DerivDup,Deriv).
/* solve(GoalsList,CIn,COut) takes a list of goals and an input C set
and returns an output C set
The C set is a list of triple (N,R,S) where
- N is the index of the head atom used, starting from 0
- R is the index of the non ground rule used, starting from 1
- S is the substitution of rule R, in the form of a list whose elements
are of the form 'VarName'=value
*/
solve([],C,C):-!.
solve([bagof(V,EV^G,L)|T],CIn,COut):-!,
list2and(GL,G),
bagof((V,C),EV^solve(GL,CIn,C),LD),
length(LD,N),
build_initial_graph(N,GrIn),
build_graph(LD,0,GrIn,Gr),
clique(Gr,Clique),
build_Cset(LD,Clique,L,[],C1),
remove_duplicates_eq(C1,C2),
solve(T,C2,COut).
solve([bagof(V,G,L)|T],CIn,COut):-!,
list2and(GL,G),
bagof((V,C),solve(GL,CIn,C),LD),
length(LD,N),
build_initial_graph(N,GrIn),
build_graph(LD,0,GrIn,Gr),
clique(Gr,Clique),
build_Cset(LD,Clique,L,[],C1),
remove_duplicates_eq(C1,C2),
solve(T,C2,COut).
solve([setof(V,EV^G,L)|T],CIn,COut):-!,
list2and(GL,G),
setof((V,C),EV^solve(GL,CIn,C),LD),
length(LD,N),
build_initial_graph(N,GrIn),
build_graph(LD,0,GrIn,Gr),
clique(Gr,Clique),
build_Cset(LD,Clique,L1,[],C1),
remove_duplicates(L1,L),
solve(T,C1,COut).
solve([setof(V,G,L)|T],CIn,COut):-!,
list2and(GL,G),
setof((V,C),solve(GL,CIn,C),LD),
length(LD,N),
build_initial_graph(N,GrIn),
build_graph(LD,0,GrIn,Gr),
clique(Gr,Clique),
build_Cset(LD,Clique,L1,[],C1),
remove_duplicates(L1,L),
solve(T,C1,COut).
solve([\+ H |T],CIn,COut):-!,
list2and(HL,H),
(setof(D,find_deriv(HL,D),LDup)->
rem_dup_lists(LDup,L),
choose_clauses(CIn,L,C1),
solve(T,C1,COut)
;
solve(T,CIn,COut)
).
solve([H|T],CIn,COut):-
builtin(H),!,
call(H),
solve(T,CIn,COut).
solve([H|T],CIn,COut):-
def_rule(H,B),
append(B,T,NG),
solve(NG,CIn,COut).
solve([H|T],CIn,COut):-
find_rule(H,(R,S,N),B,CIn),
solve_pres(R,S,N,B,T,CIn,COut).
solve_pres(R,S,N,B,T,CIn,COut):-
member_eq((N,R,S),CIn),!,
append(B,T,NG),
solve(NG,CIn,COut).
solve_pres(R,S,N,B,T,CIn,COut):-
append(CIn,[(N,R,S)],C1),
append(B,T,NG),
solve(NG,C1,COut).
build_initial_graph(N,G):-
listN(0,N,Vert),
add_vertices([],Vert,G).
build_graph([],_N,G,G).
build_graph([(_V,C)|T],N,GIn,GOut):-
N1 is N+1,
compatible(C,T,N,N1,GIn,G1),
build_graph(T,N1,G1,GOut).
compatible(_C,[],_N,_N1,G,G).
compatible(C,[(_V,H)|T],N,N1,GIn,GOut):-
(compatible(C,H)->
add_edges(GIn,[N-N1,N1-N],G1)
;
G1=GIn
),
N2 is N1 +1,
compatible(C,T,N,N2,G1,GOut).
compatible([],_C).
compatible([(N,R,S)|T],C):-
not_present_with_a_different_head(N,R,S,C),
compatible(T,C).
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).
build_Cset(_LD,[],[],C,C).
build_Cset(LD,[H|T],[V|L],CIn,COut):-
nth0(H,LD,(V,C)),
append(C,CIn,C1),
build_Cset(LD,T,L,C1,COut).
/* 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):-
rule(R,S,_,Head,Body),
member_head(H,Head,0,N),
not_already_present_with_a_different_head(N,R,S,C).
find_rule(H,(R,S,Number),Body,C):-
rule(R,S,_,uniform(H:1/_Num,_P,Number),Body),
not_already_present_with_a_different_head(Number,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),
instantiation_present_with_the_same_head(N,R,S,CIn),
choose_a_different_head(N,R,S,T,CIn,COut).
choose_a_different_head(N,R,S,D,CIn,COut):-
/* cases 1 and 2 of Select */
choose_a_head(N,R,S,CIn,C1),
choose_clauses(C1,D,COut).
choose_a_different_head(N,R,S,D,CIn,COut):-
/* case 3 of Select */
new_head(N,R,S,N1),
\+ already_present(N1,R,S,CIn),
choose_clauses([(N1,R,S)|CIn],D,COut).
/* 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(N,NH),
instantiation_present_with_the_same_head(N,R,S,T).
instantiation_present_with_the_same_head(N,R,S,[(NH,R,SH)|T]):-
\+ \+ S=SH,
N=NH,!,
dif(S,SH),
instantiation_present_with_the_same_head(N,R,S,T).
instantiation_present_with_the_same_head(N,R,S,[_H|T]):-
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):-
rule(R,S,Numbers,Head,_Body),
Head\=uniform(_,_,_),!,
nth0(N, Numbers, _Elem, Rest),
member(N1,Rest).
new_head(N,R,S,N1):-
rule(R,S,Numbers,uniform(_A:1/Tot,_L,_Number),_Body),
listN(0,Tot,Numbers),
nth0(N, Numbers, _Elem, Rest),
member(N1,Rest).
/* 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([],[]).
rem_dup_lists([H|T],T1):-
member_subset(H,T),!,
rem_dup_lists(T,T1).
rem_dup_lists([H|T],[H|T1]):-
rem_dup_lists(T,T1).
member_subset(E,[H|_T]):-
subset_my(H,E),!.
member_subset(E,[_H|T]):-
member_subset(E,T).
/* 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).
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([(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(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 */list_el(0,_P,[]):-!.
/* start of predicates for parsing an input file containing a program */
/* p(File) parses the file File.cpl. It can be called more than once without
exiting yap */
p(File):-
parse(File).
parse(File):-
atom_concat(File,'.cpl',FilePl),
open(FilePl,read,S),
read_clauses(S,C),
close(S),
retractall(rule(_,_,_,_,_)),
retractall(def_rule(_,_)),
process_clauses(C,1).
process_clauses([(end_of_file,[])],_N).
process_clauses([((H:-B),V)|T],N):-
H=uniform(A,P,L),!,
list2and(BL,B),
process_body(BL,V,V1),
remove_vars([P],V1,V2),
append(BL,[length(L,Tot),nth0(Number,L,P)],BL1),
append(V2,['Tot'=Tot],V3),
assertz(rule(N,V3,_NH,uniform(A:1/Tot,L,Number),BL1)),
N1 is N+1,
process_clauses(T,N1).
process_clauses([((H:-B),V)|T],N):-
H=(_;_),!,
list2or(HL1,H),
process_head(HL1,HL),
list2and(BL,B),
process_body(BL,V,V1),
length(HL,LH),
listN(0,LH,NH),
assertz(rule(N,V1,NH,HL,BL)),
N1 is N+1,
process_clauses(T,N1).
process_clauses([((H:-B),V)|T],N):-
H=(_:_),!,
list2or(HL1,H),
process_head(HL1,HL),
list2and(BL,B),
process_body(BL,V,V1),
length(HL,LH),
listN(0,LH,NH),
assertz(rule(N,V1,NH,HL,BL)),
N1 is N+1,
process_clauses(T,N1).
process_clauses([((H:-B),_V)|T],N):-!,
list2and(BL,B),
assert(def_rule(H,BL)),
process_clauses(T,N).
process_clauses([(H,V)|T],N):-
H=(_;_),!,
list2or(HL1,H),
process_head(HL1,HL),
length(HL,LH),
listN(0,LH,NH),
assertz(rule(N,V,NH,HL,[])),
N1 is N+1,
process_clauses(T,N1).
process_clauses([(H,V)|T],N):-
H=(_:_),!,
list2or(HL1,H),
process_head(HL1,HL),
length(HL,LH),
listN(0,LH,NH),
assertz(rule(N,V,NH,HL,[])),
N1 is N+1,
process_clauses(T,N1).
process_clauses([(H,_V)|T],N):-
assert(def_rule(H,[])),
process_clauses(T,N).
/* if the annotation in the head are not ground, the null atom is not added
and the eventual formulas are not evaluated */
process_head(HL,NHL):-
(ground_prob(HL)->
process_head_ground(HL,0,NHL)
;
NHL=HL
).
ground_prob([]).
ground_prob([_H:PH|T]):-
ground(PH),
ground_prob(T).
process_head_ground([H:PH],P,[H:PH1|Null]):-
PH1 is PH,
PNull is 1-P-PH1,
setting(epsilon_parsing,Eps),
EpsNeg is - Eps,
PNull > EpsNeg,
(PNull>Eps->
Null=['':PNull]
;
Null=[]
).
process_head_ground([H:PH|T],P,[H:PH1|NT]):-
PH1 is PH,
P1 is P+PH1,
process_head_ground(T,P1,NT).
/* setof must have a goal of the form B^G where B is a term containing the existential variables */
process_body([],V,V).
process_body([setof(A,B^_G,_L)|T],VIn,VOut):-!,
get_var(A,VA),
get_var(B,VB),
remove_vars(VA,VIn,V1),
remove_vars(VB,V1,V2),
process_body(T,V2,VOut).
process_body([setof(A,_G,_L)|T],VIn,VOut):-!,
get_var(A,VA),
remove_vars(VA,VIn,V1),
process_body(T,V1,VOut).
process_body([bagof(A,B^_G,_L)|T],VIn,VOut):-!,
get_var(A,VA),
get_var(B,VB),
remove_vars(VA,VIn,V1),
remove_vars(VB,V1,V2),
process_body(T,V2,VOut).
process_body([bagof(A,_G,_L)|T],VIn,VOut):-!,
get_var(A,VA),
remove_vars(VA,VIn,V1),
process_body(T,V1,VOut).
process_body([_H|T],VIn,VOut):-!,
process_body(T,VIn,VOut).
get_var_list([],[]).
get_var_list([H|T],[H|T1]):-
var(H),!,
get_var_list(T,T1).
get_var_list([H|T],VarOut):-!,
get_var(H,Var),
append(Var,T1,VarOut),
get_var_list(T,T1).
get_var(A,[A]):-
var(A),!.
get_var(A,V):-
A=..[_F|Args],
get_var_list(Args,V).
remove_vars([],V,V).
remove_vars([H|T],VIn,VOut):-
delete_var(H,VIn,V1),
remove_vars(T,V1,VOut).
delete_var(_H,[],[]).
delete_var(V,[VN=Var|T],[VN=Var|T1]):-
V\==Var,!,
delete_var(V,T,T1).
delete_var(_V,[_H|T],T).
read_clauses(S,[(Cl,V)|Out]):-
read_term(S,Cl,[variable_names(V)]),
(Cl=end_of_file->
Out=[]
;
read_clauses(S,Out)
).
listN(N,N,[]):-!.
listN(NIn,N,[NIn|T]):-
N1 is NIn+1,
listN(N1,N,T).
/* end of predicates for parsing an input file containing a program */
/* start of utility predicates */
list2or([X],X):-
X\=;(_,_),!.
list2or([H|T],(H ; Ta)):-!,
list2or(T,Ta).
list2and([X],X):-
X\=(_,_),!.
list2and([H|T],(H,Ta)):-!,
list2and(T,Ta).
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.
clique(Graph,Clique):-
vertices(Graph,Candidates),
extend_cycle(Graph,Candidates,[],[],Clique).
extend_cycle(G,[H|T],Not,CS,CSOut):-
neighbours(H, G, Neigh),
intersection(Neigh,T,NewCand),
intersection(Neigh,Not,NewNot),
extend(G,NewCand,NewNot,[H|CS],CSOut).
extend_cycle(G,[H|T],Not,CS,CSOut):-
extend_cycle(G,T,[H|Not],CS,CSOut).
extend(_G,[],[],CompSub,CompSub):-!.
extend(G,Cand,Not,CS,CSOut):-
extend_cycle(G,Cand,Not,CS,CSOut).
intersection([],_Y,[]).
intersection([H|T],Y,[H|Z]):-
member(H,Y),!,
intersection(T,Y,Z).
intersection([_H|T],Y,Z):-
intersection(T,Y,Z).
/* set(Par,Value) can be used to set the value of a parameter */
set(Parameter,Value):-
retract(setting(Parameter,_)),
assert(setting(Parameter,Value)).
/* end of utility predicates */

239
cplint/cplint_Prob.c Normal file
View File

@ -0,0 +1,239 @@
/*
LPAD and CP-Logic interpreter
Copyright (c) 2007, Fabrizio Riguzzi
For the use of cudd:
Copyright (c) 1995-2004, Regents of the University of Colorado
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the University of Colorado nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
This file contains the definition of Prob and ProbBool plus the functions
for building the BDD
*/
#include "cplint.h"
#include <stdlib.h>
DdNode * retFunction(DdManager * mgr,array_t *expression, array_t *v)
/* given an expression term1+term2+...+termn, returns the BDD that implements that function */
{
array_t * term;
DdNode * tNode, * tmp, *tmp1;
int i;
i=0;
tNode=Cudd_ReadLogicZero(mgr);
Cudd_Ref(tNode);
while(i<array_n(expression))
{
term=array_fetch(array_t * ,expression,i);
tmp=retTerm(mgr,term,v);
Cudd_Ref(tmp);
tmp1=Cudd_bddOr(mgr,tNode,tmp);
Cudd_Ref(tmp1);
Cudd_RecursiveDeref(mgr,tNode);
tNode=tmp1;
i++;
}
return tNode;
}
DdNode * retTerm(DdManager * mgr,array_t *term, array_t * v)
/* given a term V1=v1 and V2=v2 ... Vn=vn, returns the BDD that implements that function */
{
factor f;
DdNode * fNode, * tmp, *tmp1;
int i;
i=0;
fNode=Cudd_ReadOne(mgr);
Cudd_Ref(fNode);
while (i<array_n(term))
{
f=array_fetch(factor, term, i);
tmp=retFactor(mgr,f,v);
Cudd_Ref(tmp);
tmp1= Cudd_bddAnd(mgr,fNode,tmp);
Cudd_Ref(tmp1);
Cudd_RecursiveDeref(mgr,fNode);
fNode=tmp1;
i++;
}
return fNode;
}
DdNode * retFactor(DdManager * mgr, factor f, array_t * vars)
/* given a factor V=v, returns the BDD that implements that function */
{
int varIndex;
int value;
int i;
int bit;
variable v;
DdNode * node, *booleanVar, * tmp;
array_t * booleanVars;
varIndex=f.var;
value=f.value;
v=array_fetch(variable, vars, varIndex);
booleanVars=v.booleanVars;
i=v.nBit-1;
node=Cudd_ReadOne(mgr);
Cudd_Ref(node);
/* booelan var with index 0 in v.booleanVars is the most significant */
do {
booleanVar=array_fetch(DdNode *,booleanVars,i);
bit=value & 01;
if (bit)
{
tmp=Cudd_bddAnd(mgr,node,booleanVar);
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, array_t * vars,array_t * bVar2mVar, GHashTable * 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
*/
{
DdNode *T,*F;
int index,mVarIndex,nBit;
variable v;
double p,res;
double value;
double * value_p;
DdNode **key;
double *rp;
index=node->index;
if (Cudd_IsConstant(node))
{
value=node->type.value;
return value;
}
else
{
value_p=g_hash_table_lookup(nodes,&node);
if (value_p!=NULL)
{
return *value_p;
}
else
{
mVarIndex=array_fetch(int,bVar2mVar,index);
v=array_fetch(variable,vars,mVarIndex);
nBit=v.nBit;
res=ProbBool(node,0,nBit,0,v,vars,bVar2mVar,nodes);
key=(DdNode **)malloc(sizeof(DdNode *));
*key=node;
rp=(double *)malloc(sizeof(double));
*rp=res;
g_hash_table_insert(nodes, key, rp);
return res;
}
}
}
double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v,
array_t * vars,array_t * bVar2mVar, GHashTable * nodes)
/* explores a group of binary variables making up the multivalued variable v */
{
DdNode *T,*F;
double p,res;
array_t * probs;
double value;
probs=v.probabilities;
if (nBit==0)
{
if (bits>=array_n(probs))
return 0;
else
{
p=array_fetch(double,probs,bits);
res=p*Prob(node,vars,bVar2mVar,nodes);
return res;
}
}
else
{
if (correctPosition(node->index,v,node,posBVar))
{
T = node->type.kids.T;
F = node->type.kids.E;
bits=bits<<1;
res=ProbBool(T,bits+1,nBit-1,posBVar+1,v,vars,bVar2mVar,nodes)+
ProbBool(F,bits,nBit-1,posBVar+1,v,vars,bVar2mVar,nodes);
return res;
}
else
{
bits=bits<<1;
res=ProbBool(node,bits+1,nBit-1,posBVar+1,v,vars,bVar2mVar,nodes)+
ProbBool(node,bits,nBit-1,posBVar+1,v,vars,bVar2mVar,nodes);
return res;
}
}
}
int correctPosition(int index,variable v, DdNode * node,int posBVar)
/* returns 1 is the boolean variable with index posBVar is in the correct position
currently explored by ProbBool */
{
DdNode * bvar;
bvar=array_fetch(DdNode *,v.booleanVars,posBVar);
return bvar->index==index;
}

275
cplint/cplint_yap.c Normal file
View File

@ -0,0 +1,275 @@
/*
LPAD and CP-Logic interpreter
Copyright (c) 2007, Fabrizio Riguzzi
For the use of cudd:
Copyright (c) 1995-2004, Regents of the University of Colorado
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the University of Colorado nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
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"
#include <math.h>
#include <stdlib.h>
#include <string.h>
unsigned long dividend;
FILE *open_file (char *filename, const char *mode);
void reverse(char s[]);
void createVars(array_t * vars, YAP_Term t,DdManager * mgr, array_t * bVar2mVar, char inames[1000][20])
/* adds the boolean variables to the BDD and returns
an array_t containing them (array_t is defined in the util library of glu)
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;
b=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=array_alloc(double,0);
v.booleanVars=array_alloc(DdNode *,0);
for (i=0;i<nVal;i++)
{
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);
p=YAP_FloatOfTerm(YAP_HeadOfTerm(probTerm));
array_insert(double,v.probabilities,i,p);
probTerm=YAP_TailOfTerm(probTerm);
array_insert(DdNode *,v.booleanVars,i,Cudd_bddIthVar(mgr,b+i));
array_insert(int,bVar2mVar,b+i,varIndex);
}
Cudd_MakeTreeNode(mgr,b,nVal,MTR_FIXED);
b=b+nVal;
array_insert(variable,vars,varIndex,v);
t=YAP_TailOfTerm(t);
}
}
void createExpression(array_t * expression, YAP_Term t)
/* returns the expression as an array_t of terms (cubes) starting from the prolog lists of terms
each term is an array_t of factors obtained from a prolog list of factors
each factor is a couple (index of variable, index of value) obtained from a prolog list containing
two integers
*/
{
YAP_Term termTerm,factorTerm,tail;
factor f;
int i,j;
array_t * term;
i=0;
while(YAP_IsPairTerm(t))
{
term=array_alloc(factor,0);
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)));
array_insert(factor,term,j,f);
termTerm=YAP_TailOfTerm(termTerm);
j++;
}
array_insert(array_t *,expression,i,term);
t=YAP_TailOfTerm(t);
i++;
}
}
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;
array_t * variables,* expression, * term, * bVar2mVar;
DdNode * function, * add;
DdManager * mgr;
factor f;
variable v;
double p;
int var,val,nBVar,i,j,intBits;
FILE * file;
DdNode * array[1];
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 */
Cudd_ReorderingType * method;
arg1=YAP_ARG1;
arg2=YAP_ARG2;
arg3=YAP_ARG3;
arg4=YAP_ARG4;
mgr=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
variables=array_alloc(variable,0);
bVar2mVar=array_alloc(int,0);
createVars(variables,arg1,mgr,bVar2mVar,inames);
/* automatic variable reordering, default method CUDD_REORDER_SIFT used */
Cudd_AutodynEnable(mgr,CUDD_REORDER_SAME);
expression=array_alloc(array_t *,0);
createExpression(expression,arg2);
function=retFunction(mgr,expression,variables);
/* 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);
if (YAP_IntOfTerm(arg4))
/* if specified by the user, a dot file for the BDD is written to cpl.dot */
{
nBVar=array_n(bVar2mVar);
for(i=0;i<nBVar;i++)
names[i]=inames[i];
array[0]=add;
onames[0]="Out";
file = open_file("cpl.dot", "w");
Cudd_DumpDot(mgr,1,array,names,onames,file);
fclose(file);
}
nodes=g_hash_table_new(my_hash,my_equal);
intBits=sizeof(unsigned int)*8;
/* dividend is a global variable used by my_hash
it is equal to an unsigned int with binary representation 11..1 */
dividend=1;
for(j=1;j<intBits;j++)
{
dividend=(dividend<<1)+1;
}
out=YAP_MkFloatTerm(Prob(add,variables,bVar2mVar,nodes));
g_hash_table_foreach (nodes,dealloc,NULL);
g_hash_table_destroy(nodes);
Cudd_Quit(mgr);
array_free(variables);
array_free(bVar2mVar);
array_free(expression);
return(YAP_Unify(out,arg3));
}
/*
int compare(char *a, char *b)
{
int aval,bval;
aval=(int) *((DdNode **)a);
aval=(int) *((DdNode **)b);
if (aval<bval)
return -1;
else
if (aval>bval)
return 1;
else
return 0;
}
*/
void init_my_predicates()
/* function required by YAP for intitializing the predicates defined by a C function*/
{
YAP_UserCPredicate("compute_prob",compute_prob,4);
}
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;
}
void reverse(char s[])
/* reverses a string */
{
int i,c,j;
for (i=0,j=strlen(s)-1;i<j;i++,j--)
{
c=s[i];
s[i]=s[j];
s[j]=c;
}
}
gint my_equal(gconstpointer v,gconstpointer v2)
/* function used by GHashTable to compare two keys */
{
DdNode *a,*b;
a=*(DdNode **)v;
b=*(DdNode **)v2;
return (a==b);
}
guint my_hash(gconstpointer key)
/* function used by GHashTable to hash a key */
{
unsigned int h;
h=(unsigned int)((unsigned long) *((DdNode **)key) % dividend);
return h;
}
void dealloc(gpointer key,gpointer value,gpointer user_data)
{
free(key);
free(value);
}

546
cplint/doc/bib.bib Normal file
View File

@ -0,0 +1,546 @@
@Book{Lloyd,
author = {J. W. Lloyd},
title = {Foundations of Logic Programming},
edition = {2nd extended},
isbn = {3-540-18199-7},
publisher = {Springer-Verlag},
year = {1987},
}
@MISC{Str05-pc,
author = {J. Struyf},
year = {2005},
note = {Personal communication},
}
@MISC{Sri05-web,
author = {A. Srinivasan},
title = {Aleph},
year = {2004},
note = {http://web.comlab.ox.ac.uk/oucl/research/areas/machlearn/Aleph/aleph_toc.html},
}
@article{RaeDeh97-ML,
Author = "De Raedt, L. and Dehaspe, L.",
Title = "Clausal discovery",
Journal = "Machine Learning",
Year = 1997,
pages ={99--146},
number={2--3},
volume ={26}
}
@BOOK{GreVal85-book,
author = {S. Greco and P. Valabrega},
title = {Lezioni di Matematica: Algebra Lineare},
volume = {1},
publisher = {Levrotto e Bella},
year = {1985},
address = {Torino, Italy},
}
@BOOK{NiedeW97-book,
author = {S. Nienhuys-Cheng and R. de Wolf},
title = {Foundations of Inductive Logic Programming},
publisher = {Springer},
year = {1997},
number = {1228},
series = {Lecture Notes in Artificial Intelligence},
address = {Berlin, Germany},
}
@techreport{VenVer03-TR,
author = {J. Vennekens and S. Verbaeten},
title = {Logic Programs With Annotated Disjunctions},
year = {2003},
institution = {K. U. Leuven},
number = {CW386},
note = {\href{http://www.cs.kuleuven.ac.be/\%7Ejoost/techrep.ps}{http://www.cs.kuleuven.ac.be/\%7Ejoost/techrep.ps}},
}
@BOOK{LobMinRaj92-book,
author = {J. Lobo and J. Minker and A. Rajasekar},
title = {Foundations of Disjunctive Logic Programming},
publisher = {{MIT} Press},
year = {1992},
address = {Cambridge, Massachusetts},
}
@inProceedings{VenVer04-ICLP04-IC,
author = {J. Vennekens and S. Verbaeten and M. Bruynooghe},
title = {Logic Programs With Annotated Disjunctions},
booktitle = {The 20th International Conference on Logic Programming ({ICLP} 2004)},
year = {2004},
note={\href{http://www.cs.kuleuven.ac.be/\%7Ejoost/}{http://www.cs.kuleuven.ac.be/\%7Ejoost/}},
}
Institute for Artificial Intelligence, Vienna, TR-95-09, 1995.
@techreport{Hol03-TR,
author = {C. Holzbaur},
title = {{OFAI} clp(q,r) Manual, Edition 1.3.3 },
year = {1995},
institution = {Austrian Research Institute for Artificial Intelligence},
number = {TR-95-09},
address={Vienna},
}
@ARTICLE{NgSub92-InfCom-IJ,
author = {R. T. Ng and V. S. Subrahmanian},
title = {Probabilistic Logic Programming},
journal = {Information and Computation},
year = {1992},
volume = {101},
number = {2},
pages = {150--201},
}
@inProceedings{KerDeR01-ILP01-IC,
author = {K. Kersting and L. De Raedt},
title = {Towards Combining Inductive Logic Programming and Bayesian Networks},
booktitle = {Inductive Logic Programming ({ILP} 2001)},
year = {2001},
editor = {C. Rouveirol and M. Sebag},
publisher={Springer-Verlag},
series={{LNAI}},
number={2157}
}
@inProceedings{KerDeR00-ILP00-IC,
author = {K. Kersting and L. De Raedt},
title = {Bayesian Logic Programs},
booktitle = {Inductive Logic Programming ({ILP} 2000), Work in Progress Track},
year = {2000},
url={http://SunSITE.Informatik.RWTH-Aachen.DE/Publications/CEUR-WS/Vol-35/},
}
@techreport{KerDeR01-TR,
author = {K. Kersting and L. De Raedt},
title = {Bayesian Logic Programs},
year = {2001},
institution = {Institute for Computer Science, University of Freiburg},
number = {151},
address={Freiburg, Germany},
month ={April},
}
@incollection{GetFri01-BC,
author = {L. Getoor and N. Friedman and D. Koller and A. Pfeffer},
title = {Learning Probabilistic Relational Models},
booktitle = {Relational Data Mining},
publisher = {Springer-Verlag},
year = {2001},
editor ={Saso Dzeroski and Nada Lavrac},
address = {Berlin},
}
@ARTICLE{NgoHad97-TheoCS-IJ,
author = {L. Ngo and P. Haddaway},
title = {Answering Queries from Context-Sensitive Probabilistic
Knowledge Bases},
journal = {Theoretical Computer Science},
year = {1997},
volume = {171},
number = {1--2},
pages = {147-177},
}
@ARTICLE{Poo97-ArtInt-IJ,
author = {D. Poole},
title = {The {I}ndependent {C}hoice {L}ogic for Modelling Multiple Agents under
Uncertainty},
journal = {Artificial Intelligence},
year = {1997},
volume = {94},
number = {1--2},
pages = {7--56},
}
@article{mugg:mi17:slplearn,
TITLE = "Learning Stochastic Logic Programs",
AUTHOR = "S. H. Muggleton",
YEAR = 2000,
JOURNAL = "Electronic Transactions in Artificial Intelligence",
VOLUME = 4,
NUMBER = "041",
URL = "http://www.ida.liu.se/ext/epa/cis/2000/041/tcover.html" }
@inProceedings{Cus00-UAI00-IC,
author = {J. Cussens},
title = {Stochastic logic programs: Sampling, inference and applications},
booktitle = {Uncertainty in Artificial Intelligence ({UAI} 2000)},
year = {2000},
publisher={Morgan Kaufmann},
address= {San Francisco, CA},
pages= {115--122},
}
@inProceedings{Sat98-MD-IW,
author = {Sato, T.},
title = {Modeling scientific theories as PRISM programs},
booktitle = {{ECAI98} Workshop on Machine Discovery},
year = {1998},
pages= {37--45},
}
@inProceedings{Sat95-ICLP-IC,
author = { Sato, T.},
title = {A statistical learning method for logic programs with distribution semantics},
booktitle = {12th International Conference on Logic Programming ({ICLP} 1995)},
year = {1995},
pages= {715-729},
}
@inProceedings{SanPagQaz03-UAI-IC,
author = {Santos Costa, V. and D. Page and M. Qazi and J. Cussens},
title = {CLP($\mathcal{BN}$): Constraint Logic Programming for Probabilistic Knowledge},
booktitle = {Uncertainty in Artificial Intelligence ({UAI} 2003)},
year = {2003},
}
@inProceedings{Blo03-MRDM-IW,
author = {H. Blockeel},
title = {Prolog for First-Order Bayesian Networks: A Meta-intepreter Approach},
booktitle = {Multi-Relational Data Mining ({MRDM} 2003)},
year = {2003},
}
@INPROCEEDINGS{Ngo96-UAI-IC,
AUTHOR = "Ngo, L. ",
TITLE = "Probabilistic Disjunctive Logic Programming",
BOOKTITLE = "Uncertainty in Artificial Intelligence ({UAI} 1996)",
PUBLISHER = "Morgan Kaufmann Publishers",
ADDRESS = "San Francisco, CA",
YEAR = "1996",
PAGES = "397--404"
}
@article{Getoor+al:JMLR02,
author = "L. Getoor and N. Friedman and D. Koller and B.
Taskar",
journal = {Journal of Machine Learning Research},
title = "Learning probabilistic models of Relational Structure",
volume = 3,
month = {December},
pages = {679-707},
year = "2002",
}
@ARTICLE{well-founded,
author = {Van Gelder, A. and K. A. Ross and J. S. Schlipf},
title = {The Well-founded Semantics for General Logic Programs},
journal = {Journal of the {ACM}},
volume = {38},
number = {3},
pages = {620--650},
year = {1991}
}
@inProceedings{stable-models,
author = {M. Gelfond and V. Lifschitz},
title = {The Stable Model Semantics for Logic Programming},
booktitle = {Proceedings of the 5th Int. Conf. on Logic Programming},
editor = {R. Kowalski and K. A. Bowen},
year = 1988,
publisher = {{MIT} Press},
pages = {1070--1080}
}
@INCOLLECTION{Cla78,
author = {K. L. Clark},
title = {Negation as Failure},
booktitle= {Logic and Databases},
publisher = {Plenum Press},
year = 1978
}
@inproceedings{DeRKerKim-ILP06,
author = {De Raedt, L. and K. Kersting and A. Kimmig and K. Revoredo and H. Toivonen},
title = {Revising Probabilistic Prolog Programs},
booktitle = {Proceedings of the 16th International Conference on Inductive Logic Programming},
year = {2007},
publisher = {Springer},
number = {4455},
series = {LNAI},
}
@inproceedings{Rig-ILP06,
author={F. Riguzzi},
title={{ALLPAD}: Approximate Learning of Logic Programs with Annotated Disjunctions},
booktitle={Proceedings of the 16th International Conference on Inductive Logic Programming},
year={2007},
publisher={Springer},
number={4455},
series = {LNAI},
}
@inproceedings{DBLP:conf/ijcai/RaedtKT07,
author = {De Raedt, L. and
A. Kimmig and
H. Toivonen},
title = {ProbLog: A Probabilistic Prolog and Its Application in Link
Discovery.},
booktitle = {Proceedings of the 20th International Joint
Conference on Artificial Intelligence},
year = {2007},
pages = {2462-2467},
ee = {http://www.ijcai.org/papers07/Papers/IJCAI07-396.pdf},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@article{DBLP:journals/ngc/AptB91,
author = {K. R. Apt and
M. Bezem},
title = {Acyclic Programs.},
journal = {New Generation Comput.},
volume = {9},
number = {3/4},
year = {1991},
pages = {335-364},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@article{DBLP:journals/jlp/Fitting85a,
author = {M. Fitting},
title = {A Kripke-Kleene Semantics for Logic Programs.},
journal = {J. Log. Program.},
volume = {2},
number = {4},
year = {1985},
pages = {295-312},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@inproceedings{DBLP:conf/ki/HitzlerW02,
author = {P. Hitzler and
M. Wendt},
title = {The Well-Founded Semantics Is a Stratified Fitting Semantics.},
booktitle = {Annual
German Conference on AI, ({KI} 2002)},
year = {2002},
pages = {205-221},
publisher = {Springer},
series = {LNCS},
number = {2479},
ee = {http://link.springer.de/link/service/series/0558/bibs/2479/24790205.htm},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@inProceedings{FieBloRamBru-MRDM04,
author={D. Fierens and H. Blockeel and J. Ramon and M. Bruynooghe},
title={Logical Bayesian networks},
booktitle={Multi-Relational Data Mining ({MRDM} 2004)},
pages={19--30},
year={2004},
}
@inProceedings{Blo04-ILP04WIP-IC,
author = {H. Blockeel},
title = {Probabilistic logical models for Mendel's
experiments: An exercise},
booktitle = {Inductive Logic Programming ({ILP} 2004), Work in Progress Track},
year = {2004},
}
@techreport{Rig06-TR,
author = {F. Riguzzi},
title = {{ALLPAD}: Approximate Learning of Logic Programs With Annotated Disjunctions},
year = {2006},
institution = {University of Ferrara},
number = {CS-2006-01},
note = {http://www.ing.unife.it/aree\_ricerca/informazione/cs/technical\_reports/CS-2006-01.pdf},
}
@inproceedings{DBLP:conf/ismis/DehaspeR96,
author = {L. Dehaspe and
L. De Raedt},
title = {{DLAB}: A Declarative Language Bias Formalism.},
booktitle = {International Symposium on Methodologies for Intelligent Systems,
({ISMIS} 1996)},
editor = {Z. W. Ras and
M. Michalewicz},
year = {1996},
publisher = {Springer},
series = {LNCS},
number = {1079},
isbn = {3-540-61286-6},
pages = {613-622},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@MISC{Sri05-web-mlj,
author = {A. Srinivasan},
title = {Aleph},
year = {2004},
note = {http://web.comlab.ox.ac.uk/oucl/research/ areas/machlearn/Aleph/aleph\_toc.html},
}
@incollection{Prz88-Chapter,
author = {T. C. Przymusinski},
title = {On the declarative semantics of deductive databases and logic programs},
booktitle = {Foundations of deductive databases and logic programming},
year = {1988},
isbn = {0-934613-40-0},
pages = {193--216},
editor = {J. Minker},
publisher = {Morgan Kaufmann Publishers Inc.},
address = {San Francisco, CA, USA},
}
@article{DBLP:journals/ml/TurcotteMS01,
author = {M. Turcotte and
S. Muggleton and
M. J. E. Sternberg},
title = {The Effect of Relational Background Knowledge on Learning
of Protein Three-Dimensional Fold Signatures.},
journal = {Machine Learning},
volume = {43},
number = {1/2},
year = {2001},
pages = {81-95},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@inproceedings{DBLP:conf/psb/KerstingRKR03,
author = {K. Kersting and
T. Raiko and
S. Kramer and
L. De Raedt},
title = {Towards Discovering Structural Signatures of Protein Folds
Based on Logical Hidden Markov Models.},
booktitle = {Pacific Symposium on Biocomputing},
year = {2003},
pages = {192-203},
ee = {http://helix-web.stanford.edu/psb03/kersting.pdf},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@inproceedings{DBLP:conf/dis/StolleKR05,
author = {C. Stolle and
A. Karwath and
L. De Raedt},
title = {CLASSIC'CL: An Integrated ILP System.},
booktitle = {Discovery Science, ({DS} 2005)},
publisher = {Springer},
series = {LNCS},
number = {3735},
year = {2005},
ee = {http://dx.doi.org/10.1007/11563983_31},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@article{DBLP:journals/datamine/MannilaT97,
author = {H. Mannila and
H. Toivonen},
title = {Levelwise Search and Borders of Theories in Knowledge Discovery.},
journal = {Data Min. Knowl. Discov.},
volume = {1},
number = {3},
year = {1997},
pages = {241-258},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@inproceedings{DBLP:conf/ecml/KerstingG04,
author = {K. Kersting and
T. G{\"a}rtner},
title = {Fisher Kernels for Logical Sequences.},
booktitle = {Machine Learning, ({ECML} 2004)},
year = {2004},
publisher = {Springer},
series = {LNCS},
number = {3201},
pages = {205-216},
ee = {http://springerlink.metapress.com/openurl.asp?genre=article{\&}issn=0302-9743{\&}volume=3201{\&}spage=205},
bibsource = {DBLP, http://dblp.uni-trier.de}
}
@inproceedings{KerstingECML06,
author = {B. Gutmann and K. Kersting},
title = {{TildeCRF}: Conditional Random Fields for Logical Sequences.},
booktitle = {Machine Learning, ({ECML} 2006)},
year = {2006},
publisher = {Springer},
series = {LNCS},
}
@article{KerstingJAIR06,
author = {K. Kersting and L. De Raedt and T. Raik},
title = {Logical Hidden Markov Models.},
journal = {Journal of Artificial Intelligence Research},
volume = {25},
year = {2006},
pages = {425-456},
}
@inproceedings{Rig04-ILP04-IC-short,
author = "F. Riguzzi",
title = "Learning Logic Programs with Annotated Disjunctions",
booktitle = "Inductive Logic Programming, ({ILP} 2004)",
year = "2004",
month={September},
publisher={Springer},
series ={LNCS},
number = {3194},
pages={270--287},
url = {http://www.ing.unife.it/docenti/FabrizioRiguzzi/Papers/Rig-ILP04.pdf},
isbn={3-540-22941-8},
issn={0302-9743},
doi={10.1007/b10011},
}
@inproceedings{VenDenBru-JELIA06,
author = "J. Vennekens and M. Denecker and M. Bruynooghe",
title = "Representing Causal Information about a
Probabilistic Process",
booktitle = "10th European Conference on Logics in Artificial Intelligence, JELIA 2006",
year = "2006",
month={September},
publisher={Springer},
series ={LNAI},
}
@article{Bry-TC86,
author={R. E. Bryant},
title={Graph-based algorithms for boolean function manipulation},
journal={ IEEE Trans. on Computers},
volume={35},
number={8},
pages={677-691},
year={1986},
}
@inproceedings{MilDre-ISML02-IC,
title={On the construction of multiple-valued decision diagrams},
author={Miller, D. M. and Drechsler, R.},
booktitle={Proceedings 32nd IEEE International Symposium on
Multiple-Valued Logic},
year={2002},
pages={245-253},
}
@article{LauSpi-JRS88,
author={Lauritzen, S. and Spiegelhalter, D. J.},
year={1988},
title={Local Computations with Probabilities on Graphical Structures and Their Application to Expert Systems},
journal={Journal of the Royal Statistical Society},
volume={B, 50},
number={2},
pages={157-224},
}
@inproceedings{Rig-AIIA07-IC,
author={
Fabrizio Riguzzi },
title={A Top Down Interpreter for LPAD and CP-logic},
booktitle={10th Congress of the Italian Association for Artificial Intelligence},
year={2007},
publisher={Springer},
note={\href{http://www.ing.unife.it/docenti/FabrizioRiguzzi/Papers/Rig-AIIA07.pdf}{http://www.ing.unife.it/docenti/FabrizioRiguzzi/Papers/Rig-AIIA07.pdf}}
}
}
@inproceedings{Rig-RCRA07-IC,
author={
Fabrizio Riguzzi },
title={A Top Down Interpreter for LPAD and CP-logic},
booktitle={The 14th RCRA workshop
Experimental Evaluation of Algorithms for
Solving Problems with Combinatorial Explosion},
year={2007},
note={\href{http://pst.istc.cnr.it/RCRA07/articoli/P19-riguzzi-RCRA07.pdf}{http://pst.istc.cnr.it/RCRA07/articoli/P19-riguzzi-RCRA07.pdf}}
}
@unpublished{CP-logic-unp,
author={Joost Vennekens and Marc Denecker and Maurice Bruynooge},
title={Extending the Role of Causality in Probabilistic Modeling},
year={2006},
note={\href{http://www.cs.kuleuven.ac.be/\%7Ejoost/cplogic.pdf}{http://www.cs.kuleuven.ac.be/\%7Ejoost/cplogic.pdf}},
}

396
cplint/doc/manual.tex Normal file
View File

@ -0,0 +1,396 @@
% preambolo per doppia compilazione HTML/PDF
%\ifx\pdfoutput\undefined % compilazione htlatex
\documentclass{article}
\usepackage{graphicx}
\DeclareGraphicsExtensions{.png, .gif, .jpg}
\newcommand{\href}[2]{\Link[#1]{}{} #2 \EndLink}
\newcommand{\hypertarget}[2]{\Link[]{}{#1} #2 \EndLink}
\newcommand{\hyperlink}[2]{\Link[]{#1}{} #2 \EndLink}
%\else % compilazione pdflatex
%\documentclass[a4paper,12pt]{article}
%\usepackage[pdftex]{graphicx}
%\DeclareGraphicsExtensions{.pdf,.png,.jpg}
%\RequirePackage[hyperindex]{hyperref}
%\fi
%\documentclass{article}
\begin{document}
\title{\texttt{cplint} Version 1.0 Manual}
\author{Fabrizio Riguzzi\\
fabrizio.riguzzi@unife.it}
\maketitle
\section{Introduction}
\texttt{cplint} is an interpreter for LPADs \cite{VenVer03-TR,VenVer04-ICLP04-IC} and CP-logic programs \cite{VenDenBru-JELIA06,CP-logic-unp}. It is described in \cite{Rig-AIIA07-IC} and \cite{Rig-RCRA07-IC}. It is an adaptation of the interpreter for ProbLog \cite{DBLP:conf/ijcai/RaedtKT07}.
It was proved correct \cite{Rig-RCRA07-IC} for range restricted acyclic programs \cite{DBLP:journals/ngc/AptB91} without function symbols.
It is also able to deal with extensions of LPADs and CP-logic: the clause bodies can contain \texttt{setof} and \texttt{bagof}, the probabilities in the head may be depend on variables in the body and it is possible to specify a uniform distribution in the head with reference to a \texttt{setof} or \texttt{bagof} operator. These extended features have been introduced in order to represent CLP(BN) \cite{SanPagQaz03-UAI-IC} programs and PRM models \cite{Getoor+al:JMLR02}:
\texttt{setof} and \texttt{bagof} allow to express dependency of an attribute from an aggregate function of another attribute, as in CLP(BN) and PRM, while the possibility of specifying a uniform distribution allows the use of the reference uncertainty feature of PRM.
These extensions are work in progress: they have been implemented but there is no paper yet that describes the semantics of the extended language.
%For program with function symbols, the semantics of LPADs and CP-logic are not defined. However, the interpreter accepts programs with function symbols and, if it does not go into a loop, it returns an answer. What is the meaning of this answer is subject of current study.
\section{Installation}
\texttt{cplint} is distributed to be run an a Linux machine. It has been tested under Debian 4.0 and Suse 10.2. Please contact the author for versions for Windows. Please contact the author also for the complete source code.
Download and install Yap Prolog version 5.1.2 from \href{http://www.ncc.up.pt/\%7Evsc/Yap/}{http://www.ncc.up.pt/\%7Evsc/Yap/}.
Make sure libglib1.2 is installed in your machine. It is a standard Linux package so, if it is not installed, you can use your package manager to install it.
Download \texttt{cplint.zip} from \href{http://www.ing.unife.it/software/cplint/cplint.zip}{http://www.ing.unife.it/software/cplint/cplint.zip} and uncompress it.
By downloading \texttt{cplint.zip} you agree to the BSD-style license reported in section \ref{license}.
\section{Syntax}
Disjunction in the head is represented with a semicolon and atoms in the head are separated from probabilities by a colon. For the rest, the usual syntax of Prolog is used.
For example, the CP-logic clause
$$h_1:p_1\vee \ldots \vee h_n:p_n\leftarrow b_1,\dots,b_m ,\neg c_1,\ldots,\neg c_l$$
is represented by
\begin{verbatim}
h1:p1 ; ... ; hn:pn :- b1,...,bm,\+ c1,....,\+ cl
\end{verbatim}
No parentheses are necessary. The \texttt{pi} are numeric expressions that can involve
variables appearing in the body. It is up to the user to ensure that the numeric expressions are legal, i.e. that they sum up to less than one for every instantiation of the clause for which the body is true in an instance.
If the clause has an empty body, it can be represented like this
\begin{verbatim}
h1:p1 ; ... ;hn:pn.
\end{verbatim}
If the clause has a single head with probability 1, the annotation can be omitted and the clause takes the form of a normal prolog clause, i.e.
\begin{verbatim}
h1:- b1,...,bm,\+ c1,...,cl.
\end{verbatim}
stands for
\begin{verbatim}
h1:1 :- b1,...,bm,\+ c1,...,cl.
\end{verbatim}
The coin example of \cite{VenVer04-ICLP04-IC} is represented as (see file \texttt{coin.cpl})
\begin{verbatim}
heads(Coin):1/2 ; tails(Coin):1/2:-
toss(Coin),\+biased(Coin).
heads(Coin):0.6 ; tails(Coin):0.4:-
toss(Coin),biased(Coin).
fair(Coin):0.9 ; biased(Coin):0.1.
toss(coin).
\end{verbatim}
The first clause states that if we toss a coin that is not biased it has equal probability of landing heads and tails. The second states that if the coin is biased it is slightly more probable that it lands heads. The third states that the coin is fair with probability 0.9 and biased with probability 0.1 and the last clause states that we toss a coin with certainty.
\section{Commands}
The program must be stored in a text file with extension \texttt{.cpl}. Suppose you have stored the example above in file \texttt{coin.cpl}.
Suppose also that \texttt{coin.cpl} is in the directory \texttt{cplint} that you obtained when you uncompressed \texttt{cplint.zip}. In order to answer queries from this program, you run \texttt{yap} from directory \texttt{cplint} and then load the interpreter with the command
\begin{verbatim}
compile(cplint).
\end{verbatim}
If \texttt{coin.cpl} is not in directory \texttt{cplint} then you must use the command
\begin{verbatim}
compile('path_to_cplint/cplint.pl').
\end{verbatim}
from the directory where \texttt{coin.cpl} is.
In this case \verb|path_to_cplint| is the path to \texttt{cplint.pl}.
Then you must parse the source file \texttt{coin.cpl} with the command
\begin{verbatim}
p(coin).
\end{verbatim}
At this point you can pose query to the program. You have to use the predicate \texttt{s/2} (for solve) that takes as its first argument a conjunction of goals in the form of a list and returns the computed probability as its second argument. For example, the probability of the conjunction \texttt{head(coin), biased(coin)} can be asked with the query
\begin{verbatim}
s([head(coin),biased(coin)],P).
\end{verbatim}
For computing the probability of a conjunction given another conjunction you have to use the predicate \texttt{sc/3} (for solve conditional) that take takes as input the query conjunction as its first argument, the evidence conjunction as its second argument and returns the probability in its third argument.
For example, the probability of the query \texttt{heads(coin)} given the evidence \texttt{biased(coin)} can be asked with the query
\begin{verbatim}
sc([heads(coin)],[biased(coin)],P).
\end{verbatim}
The package contains also a program \texttt{semantics.pl} that computes the probability of queries by using directly the semantics of LPADs (i.e. by generating all the ground instances and then testing the query with each of them). This is provided for testing purposes only. After having compiled the program, you can use the same commands of \texttt{cplint.pl}. \texttt{semantics.pl} requires an extra file in the directory where \texttt{coin.cpl} is: a file with extension \texttt{.uni} (for universe) that contains, for each variable, the list of constants to which the variable can be instantiated. For example, in our case the current directory will contain a file \texttt{coin.uni} that is a Prolog file containing facts of the form
\begin{verbatim}
universe(VarList,ConstList).
\end{verbatim}
where \texttt{VarList} is a list of variables names (each must be included in single quotes) and \texttt{ConstList} is a list of constants. \texttt{semantics.pl} generates the grounding by instantiating in all possible ways the variables of \texttt{VarList} with the constants of \texttt{ConstList}. Note that the variables are identified by name, so a variable with the same name in two different clauses will be instantiated with the same constants.
You can test your installation of \texttt{cplint} by using the program \texttt{test.pl}:
compile \texttt{cplint.pl}, compile \texttt{test.pl} and exectue the query \texttt{t}.
A number of queries are executed against the example programs and the returned probabilities are checked: if \texttt{t.} succeeds, then \texttt{cplint} is working.
\section{Extensions}
In this section we will present the extensions to the syntax of LPADs and CP-logic programs that \texttt{cplint} can handle.
The first is the use of some standard Prolog predicates.
The bodies can contain the built-in predicates:
\begin{verbatim}
is/2
>/2
</2
>=/2
=</2
=:=/2
=\=/2
true/0
false/0
=/2
==/2
\=/2
\==/2
length/2
\end{verbatim}
The bodies can also contain the following
library predicates:
\begin{verbatim}
member/2
max_list/2
min_list/2
nth0/3
nth/3
\end{verbatim}
plus the predicate
\begin{verbatim}
average/2
\end{verbatim}
that, given a list of numbers, computes its arithmetic mean.
Moreover, the bodies can contain the predicates \texttt{setof/3} and \texttt{bagof/3} with the same meaning as in Prolog. Existential quantifiers are allowed in both, so for example the query
\begin{verbatim}
setof(Z, (term(X,Y))^foo(X,Y,Z), L).
\end{verbatim}
returns all the instantiations of \texttt{Z} such that there exists an instantiation of \texttt{X} and \texttt{Y} for which \texttt{foo(X,Y,Z)} is true.
An example of the use of \texttt{setof} and \texttt{bagof} is in the file \texttt{female.cpl}:
\begin{verbatim}
male(C):M/P ; female(C):F/P:-
person(C),
setof(Male,known_male(Male),LM),
length(LM,M),
setof(Female,known_female(Female),LF),
length(LF,F),
P is F+M.
person(f).
known_female(a).
known_female(b).
known_female(c).
known_male(d).
known_male(e).
\end{verbatim}
The disjunctive rule expresses the probability of a person of unknown sex of being male or female depending on the number of males and females that are known.
This is an example of the use of expressions in the probabilities in the head that depend on variables in the body. The probabilities are well defined because they always sum to 1 (unless \texttt{P} is 0).
Another use of \texttt{setof} and \texttt{bagof} is to have an attribute depend on an aggregate function of another attribute, similarly to what is done in PRM and CLP(BN).
So, in the classical school example (available in \texttt{student.cpl}) you can find the following
clauses:
\begin{verbatim}
student_rank(S,h):0.6 ; student_rank(S,l):0.4:-
bagof(G,R^(registr_stu(R,S),registr_gr(R,G)),L),
average(L,Av),Av>1.5.
student_rank(S,h):0.4 ; student_rank(S,l):0.6:-
bagof(G,R^(registr_stu(R,S),registr_gr(R,G)),L),
average(L,Av),Av =< 1.5.
\end{verbatim}
where \verb|registr_stu(R,S)| expresses that registration \texttt{R} refers to student \texttt{S} and \verb|registr_gr(R,G)| expresses that registration \texttt{R} reports grade \texttt{G} which is a natural number. The two clauses express a dependency of the rank of the student from the average of her grades.
Another extension has been introduced in order to be able to represent reference uncertainty of PRMs. Reference uncertainty means that the link structure of a relational model is not fixed but is uncertain: this is represented by having the instance referenced in a relationship be chosen uniformly from a set. For example, consider a domain modeling scientific papers: you have a single entity, paper, and a relationship, cites, between paper and itself that connects the citing paper to the cited paper. To represent the fact that the cited paper and the citing paper are selected uniformly from certain sets, the following clauses can be used (see file \verb|paper_ref_simple.cpl|):
\begin{verbatim}
uniform(cites_cited(C,P),P,L):-
bagof(Pap,paper_topic(Pap,theory),L).
uniform(cites_citing(C,P),P,L):-
bagof(Pap,paper_topic(Pap,ai),L).
\end{verbatim}
The first clauses states that the paper \texttt{P} cited in a citation \texttt{C} is selected uniformly from the set of all papers with topic theory.
The second clauses expresses that the citing paper is selected uniformly from the papers with
topic ai.
These clauses make use of the predicate
\begin{verbatim}
uniform(Atom,Variable,List)
\end{verbatim}
in the head, where \texttt{Atom} must contain \texttt{Variable}. The meaning is the following: the set of all the atoms obtained by instantiating \texttt{Variable} of \texttt{Atom} with a term taken from \texttt{List} is generated and the head is obtained by having a disjunct for each instantiation with probability $1/N$ where $N$ is the length of \texttt{List}.
A more elaborate example is present in file \verb|paper_ref.cpl|:
\begin{verbatim}
uniform(cites_citing(C,P),P,L):-
setof(Pap,paper(Pap),L).
cites_cited_group(C,theory):0.9 ; cites_cited_group(C,ai):0.1:-
cites_citing(C,P),paper_topic(P,theory).
cites_cited_group(C,theory):0.01;cites_cited_group(C,ai):0.99:-
cites_citing(C,P),paper_topic(P,ai).
uniform(cites_cited(C,P),P,L):-
cites_cited_group(C,T),bagof(Pap,paper_topic(Pap,T),L).
\end{verbatim}
where the cited paper depends on the topic of the citing paper. In particular, if the topic is theory, the cited paper is selected uniformly from the papers about theory with probability 0.9 and from the papers about ai with probability 0.1. if the topic is ai, the cited paper is selected uniformly from the papers about theory with probability 0.01 and from the papers about ai with probability 0.99.
PRMs take into account as well existence uncertainty, where the existence of instances is also probabilistic. For example, in the paper domain, the total number of citations may be unknown and a citation between any two paper may have a probability of existing. For example, a citation between two paper may be more probable if they are about the same topic:
\begin{verbatim}
cites(X,Y):0.005 :-
paper_topic(X,theory),paper_topic(Y,theory).
cites(X,Y):0.001 :-
paper_topic(X,theory),paper_topic(Y,ai).
cites(X,Y):0.003 :-
paper_topic(X,ai),paper_topic(Y,theory).
cites(X,Y):0.008 :-
paper_topic(X,ai),paper_topic(Y,ai).
\end{verbatim}
This is an example of a CP-logic program, because the probabilities in the head do not sum up to one.
The first clause states that, if the topic of a paper \texttt{X} is theory and of paper \texttt{Y} is theory, there is a probability of 0.005 that there is a citation from \texttt{X} to \texttt{Y}. The other clauses consider the remaining cases for the topics.
\section{Parameters}
The proof procedure has two parameters that can be set with the command
\begin{verbatim}
set(parameter,value).
\end{verbatim}
from the Yap prompt after having compiled \texttt{cplint.pl}.
The current value can be read with
\begin{verbatim}
setting(parameter,Value).
\end{verbatim}
from the Yap prompt.
Available parameters:
\begin{itemize}
\item
\verb|epsilon_parsing|: if (1 - the sum of the probabilities of all the head atoms) is smaller than
\verb|epsilon_parsing|
then the clause is considered as an LPAD clause, otherwise it is considered as a CP-logic
clause. Default value 0.00001
\item \texttt{savedot}: if true a graph representing the BDD is saved in the file \texttt{cpl.dot} in the current directory in dot format.
The variables names are of the form \verb|Xn_m| where \texttt{n} is the number of the multivalued
variable and \texttt{m} is the number of the binary variable. The correspondence of variables to
clauses can be evinced from the list printed on the screen of the form
\begin{verbatim}
Variables: [(2,[X=2,X1=1]),(2,[X=1,X1=0]),(1,[])]
\end{verbatim}
where the first element of each couple is the clause number of the input file (starting from 1).
In the example above variable \texttt{X0} corresponds to clause \texttt{2} with the substitutions \texttt{X=2,X1=1},
variable \texttt{X1} corresponds to clause \texttt{2} with the substitutions \texttt{X=1,X1=0} and
variable \texttt{X2} corresponds to clause \texttt{1} with the empty substitution.
You can view the graph with \texttt{graphviz} (\href{www.graphviz.org}{www.graphviz.org}) using the
command
\begin{verbatim}
dotty cpl.dot &
\end{verbatim}
\end{itemize}
\section{Content of the Directory}
\begin{itemize}
\item \texttt{cplint.pl}: Prolog code of the interpreter.
\item \verb|cplint.so|: dynamic library that includes CUDD. It is called by \texttt{cplint.pl}.
\item \texttt{semantics.pl}: Prolog program for computing the probability according to the semantics.
\item \texttt{test.pl}: Prolog program for testing the system.
\item Examples:
\begin{itemize}
\item \texttt{alarm.cpl}: representation of the Bayesian network in Figure 2 of
\cite{VenVer04-ICLP04-IC}.
\item \texttt{coin.cpl}: coin example from \cite{VenVer04-ICLP04-IC}.
\item \texttt{coin2.cpl}: coin example with two coins.
\item \texttt{dice.cpl}: dice example from \cite{VenVer04-ICLP04-IC}.
\item \verb|twosideddice.cpl,threesideddice.cpl| game with idealized dice with two or three sides. Used in the experiments in \cite{Rig-RCRA07-IC}.
\item \texttt{es.cpl}: first example in \cite{Rig-RCRA07-IC}.
\item \texttt{esapprox.cpl}: example showing the problems of approximate inference (see \cite{Rig-RCRA07-IC}).
\item \texttt{esrange.cpl}: example showing the problems with non range restricted programs (see \cite{Rig-RCRA07-IC}).
\item \texttt{female.cpl}: example showing the dependence of probabilities in the head from variables in the body (from \cite{VenVer04-ICLP04-IC}).
\item \texttt{mendel.cpl}: program describing the Mendelian rules of inheritance, taken from \cite{Blo04-ILP04WIP-IC}.
\item \verb|paper_ref.cpl,paper_ref_simple.cpl|: paper citations examples, showing reference uncertainty, inspired by \cite{Getoor+al:JMLR02}.
\item \verb|paper_ref_not.cpl|: paper citations example showing that negation can be used also for predicates defined by clauses with \texttt{uniform} in the head.
\item \texttt{school.cpl}: example inspired by the example \verb|school_32.yap| from the
source distribution of Yap in the \texttt{CLPBN} directory.
\item \verb|school_simple.cpl|: simplified version of \texttt{school.cpl}.
\item \verb|student.cpl|: student example from Figure 1.3 of \cite{GetFri01-BC}.
\end{itemize}
The files \texttt{*.uni} that are present for some of the examples are used by \texttt{semantics.pl}. Some of the example files contain in an initial comment some queries together with their result.
\item \verb|doc|: directory that contains this manual in pdf and html
\end{itemize}
\section{License}
\label{license}
Copyright (c) 2007, Fabrizio Riguzzi
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
\begin{itemize}
\item Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
\item Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
\item Neither the name of the University of Ferrara nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
\end{itemize}
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\vspace{3mm}
The program uses the library \href{http://vlsi.colorado.edu/\%7Efabio/}{CUDD} for manipulating BDDs.
For the use of CUDD, the following license must be accepted:
\vspace{3mm}
Copyright (c) 1995-2004, Regents of the University of Colorado
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
\begin{itemize}
\item
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
\item
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
\item
Neither the name of the University of Colorado nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
\end{itemize}
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
\bibliographystyle{plain}
\bibliography{bib}
\end{document}

View File

@ -0,0 +1,7 @@
burg(t):0.1; burg(f):0.9.
earthq(t):0.2; earthq(f):0.8.
alarm(t):-burg(t),earthq(t).
alarm(t):0.8 ; alarm(f):0.2:-burg(t),earthq(f).
alarm(t):0.8 ; alarm(f):0.2:-burg(f),earthq(t).
alarm(t):0.1 ; alarm(f):0.9:-burg(f),earthq(f).

17
cplint/examples/coin.cpl Normal file
View File

@ -0,0 +1,17 @@
/*
s([heads(C)],P).
C = coin
P = 0.51 ?;
no
s([tails(C)],P).
C = coin
P = 0.49 ?;
no
*/
heads(Coin): 1/2; tails(Coin) : 1/2:-toss(Coin),\+biased(Coin).
heads(Coin): 0.6 ; tails(Coin) : 0.4:-toss(Coin),biased(Coin).
fair(Coin):0.9 ; biased(Coin):0.1.
toss(coin).

0
cplint/examples/coin.uni Normal file
View File

23
cplint/examples/coin2.cpl Normal file
View File

@ -0,0 +1,23 @@
/*
?- s([heads(C)],P).
C = coin1
P = 0.51 ? ;
C = coin2
P = 0.51 ? ;
no
?- s([tails(C)],P).
C = coin1
P = 0.49 ? ;
C = coin2
P = 0.49 ? ;
no
s([tails(coin1)],P).
P = 0.49 ?
*/
heads(Coin): 0.5; tails(Coin) : 0.5:-toss(Coin),fair(Coin).
heads(Coin): 0.6 ; tails(Coin) : 0.4:-toss(Coin),biased(Coin).
fair(Coin):0.9 ; biased(Coin):0.1.
toss(coin1).
toss(coin2).

View File

@ -0,0 +1 @@
universe(['Coin'],[coin1,coin2]).

29
cplint/examples/dice.cpl Normal file
View File

@ -0,0 +1,29 @@
/*
s([on(0,1)],P).
P = 0.166666666666667 ?;
s([\+ on(0,6)],P).
P = 0.833333333333333 ?;
s([on(1,1)],P).
P = 0.138888888888889 ?
s([on(1,6)],P).
P = 0.138888888888889 ?
s([on(2,1)],P).
out of memory
s([on(0,1),on(1,1)],P).
P = 0.0277777777777778 ?
s([on(0,1),on(1,1),on(2,1)],P).
P = 0.00462962962962963 ?
*/
on(0,1):1/6;on(0,2):1/6;on(0,3):1/6;
on(0,4):1/6;on(0,5):1/6;on(0,6):1/6.
on(X,1):1/6;on(X,2):1/6;on(X,3):1/6;
on(X,4):1/6;on(X,5):1/6;on(X,6):1/6:-
X1 is X-1,X1>=0,on(X1,_),
\+ on(X1,6).

2
cplint/examples/dice.uni Normal file
View File

@ -0,0 +1,2 @@
universe(['X'],[1,2]).
universe(['X1'],[0,1]).

13
cplint/examples/es.cpl Normal file
View File

@ -0,0 +1,13 @@
/*
s([a],P).
Variables: [(1,[]),(2,[]),(3,[])]
P = 0.226 ?
*/
a:0.1.
b:0.3;c:0.6.
a:0.2:- \+ b.

View File

@ -0,0 +1,12 @@
/*
s([a],P).
Variables: [(1,[]),(2,[]),(3,[])]
P = 0.1719 ? ;
*/
a:0.1:-p(X).
p(1):0.9.
p(2):0.9.

View File

@ -0,0 +1,21 @@
/*
s([a(X)],P).
Variables: [(1,[]),(5,[]),(2,[]),(6,[])]
P = 0.2775
X = 1 ? ;
Variables: [(3,[]),(5,[]),(4,[]),(6,[])]
P = 0.36
X = 2 ? ;
*/
a(1):0.3:-p(1).
a(1):0.3:-p(2).
a(2):0.4:-p(1).
a(2):0.4:-p(2).
p(1):0.5.
p(2):0.5.

View File

@ -0,0 +1,26 @@
/*
s([female(f)],P).
P = 0.6 ?
s([male(f)],P).
P = 0.4 ?
*/
male(C):M/P;female(C):F/P:-
person(C),
setof(Male,known_male(Male),LM),
length(LM,M),
setof(Female,known_female(Female),LF),
length(LF,F),
P is F+M.
person(f).
known_female(a).
known_female(b).
known_female(c).
known_male(d).
known_male(e).

View File

@ -0,0 +1,38 @@
/*
this file can not be used with s.pl: it generates too many programs
s([cg(s,1,p)],P).
P = 0.75 ?
s([cg(s,1,w)],P).
P = 0.25 ?
s([cg(s,2,p)],P).
P = 0.25 ?
s([cg(s,2,w)],P).
P = 0.75 ?
s([cg(f,2,w)],P).
P = 0.5
s([cg(s,2,w)],P).
P = 0.75
*/
mother(m,s).
mother(mm,m).
mother(mf,f).
father(f,s).
father(fm,m).
father(ff,f).
cg(mm,1,p).
cg(mm,2,w).
cg(fm,1,p).
cg(fm,2,p).
cg(mf,1,w).
cg(mf,2,w).
cg(ff,1,w).
cg(ff,2,p).
cg(X,1,A):0.5 ; cg(X,1,B):0.5 :- mother(Y,X),cg(Y,1,A) , cg(Y,2,B).
cg(X,2,A):0.5 ; cg(X,2,B):0.5 :- father(Y,X),cg(Y,1,A) , cg(Y,2,B).

View File

@ -0,0 +1,51 @@
/*
?- s([cites_cited(c1,Pap)],P).
P = 0.181333333333333,
Pap = p1 ? ;
P = 0.181333333333333,
Pap = p2 ? ;
P = 0.228,
Pap = p3 ? ;
P = 0.181333333333333,
Pap = p4 ? ;
P = 0.228,
Pap = p5 ? ;
no
*/
paper(p1).
paper(p2).
paper(p3).
paper(p4).
paper(p5).
paper_topic(p1,theory).
paper_topic(p2,theory).
paper_topic(p4,theory).
paper_topic(p3,ai).
paper_topic(p5,ai).
cites(c1).
uniform(cites_citing(C,P),P,L):-
setof(Pap,paper(Pap),L).
cites_cited_group(C,theory):0.9;cites_cited_group(C,ai):0.1:-
cites_citing(C,P),paper_topic(P,theory).
cites_cited_group(C,theory):0.01;cites_cited_group(C,ai):0.99:-
cites_citing(C,P),paper_topic(P,ai).
uniform(cites_cited(C,P),P,L):-
cites_cited_group(C,T),bagof(Pap,paper_topic(Pap,T),L).

View File

@ -0,0 +1,41 @@
/*
s([\+ cites_cited(c1,p1)],P).
P = 0.7 ?
s([cites_citing(c1,p1)],P).
P = 0.14 ?
*/
paper(p1).
paper(p2).
paper(p3).
paper(p4).
paper(p5).
paper_topic(p1,theory).
paper_topic(p2,theory).
paper_topic(p4,theory).
paper_topic(p3,ai).
paper_topic(p5,ai).
cites(c1).
cites_cited_group(C,theory):0.9;cites_cited_group(C,ai):0.1.
uniform(cites_cited(C,P),P,L):-
cites_cited_group(C,T),bagof(Pap,paper_topic(Pap,T),L).
uniform(cites_citing(C,P),P,L):-
setof(Paper,paper(Paper),L),
cites_cited(C,Pap),
\+ cites_cited(C,p1).

View File

@ -0,0 +1,45 @@
/*
?- s([cites_cited(c1,Pap)],P).
P = 0.333333333333333,
Pap = p1 ? ;
P = 0.333333333333333,
Pap = p2 ? ;
P = 0.333333333333333,
Pap = p4 ? ;
no
?- Action (h for help): c
| s([cites_citing(c1,Pap)],P).
P = 0.5,
Pap = p3 ? ;
P = 0.5,
Pap = p5 ? ;
no
*/
paper(p1).
paper(p2).
paper(p3).
paper(p4).
paper(p5).
paper_topic(p1,theory).
paper_topic(p2,theory).
paper_topic(p4,theory).
paper_topic(p3,ai).
paper_topic(p5,ai).
cites(c1).
uniform(cites_cited(C,P),P,L):-
bagof(Pap,paper_topic(Pap,theory),L).
uniform(cites_citing(C,P),P,L):-
bagof(Pap,paper_topic(Pap,ai),L).

796
cplint/examples/school.cpl Normal file
View File

@ -0,0 +1,796 @@
/*
Yap run on school_32.yap
?- s([professor_ability(p0,X)],P).
P = 0.5,
X = h ? ;
P = 0.1,
X = l ? ;
P = 0.4,
X = m ? ;
no
?- s([professor_popularity(p0,X)],P).
P = 0.531,
X = h ? ;
P = 0.175,
X = l ? ;
P = 0.294,
X = m ? ;
no
ok
sc([professor_ability(p0,X)],[professor_popularity(p0,h)],P).
P = 0.847457627118644,
X = h ? ;
P = 0.00188323917137476,
X = l ? ;
P = 0.150659133709981,
X = m ?
ok
sc([professor_popularity(p0,X)],[professor_ability(p0,h)],P).
P = 0.9,
X = h ? ;
P = 0.01,
X = l ? ;
P = 0.09,
X = m ? ;
no
ok
?- s([registration_grade(r0,X)],P).
P = 0.06675,
X = 1 ? ;
P = 0.16575,
X = 2 ? ;
Action (";" for more choices, <return> for exit) ? ;
P = 0.356,
X = 3 ? ;
P = 0.4115,
X = 4 ?
no
ok
sc([registration_grade(r0,X)],[registration_course(r0,C), course_difficulty(C,h)],P).
C = c16,
P = 0.15,
X = 1 ? ;
C = c16,
P = 0.285,
X = 2 ? ;
C = c16,
P = 0.424,
X = 3 ? ;
C = c16,
P = 0.141,
X = 4 ? ;
no
ok (a=4, ...,d=1)
sc([registration_grade(r0,X)], [registration_course(r0,C), course_difficulty(C,h), registration_student(r0,S), student_intelligence(S,h)],P).
C = c16,
P = 0.05,
S = s0,
X = 1 ? ;
C = c16,
P = 0.15,
S = s0,
X = 2 ? ;
C = c16,
P = 0.6,
S = s0,
X = 3 ? ;
C = c16,
P = 0.2,
S = s0,
X = 4 ? ;
no
ok
sc([registration_grade(r0,X)],[registration_course(r0,C), course_difficulty(C,l), registration_student(r0,S), student_intelligence(S,h)],P).
C = c16,
P = 0.01,
S = s0,
X = 1 ? ;
C = c16,
P = 0.02,
S = s0,
X = 2 ? ;
C = c16,
P = 0.12,
S = s0,
X = 3 ? ;
C = c16,
P = 0.85,
S = s0,
X = 4 ? ;
no
ok
s([registration_satisfaction(r0,X)],P).
P = 0.15197525,
X = 1 ? ;
P = 0.15331025,
X = 2 ? ;
P = 0.6947145,
X = 3 ? ;
no
ok
sc([registration_satisfaction(r0,X)],[ registration_student(r0,S), student_intelligence(S,h)],P).
P = 0.0959225,
S = s0,
X = 1 ? ;
P = 0.124515,
S = s0,
X = 2 ? ;
P = 0.7795625,
S = s0,
X = 3 ? ;
no
ok (h=3, l=1)
sc([registration_satisfaction(r0,X)],[registration_grade(r0,4)],P).
P = 0.04,
X = 1 ? ;
P = 0.06,
X = 2 ? ;
P = 0.9,
X = 3 ? ;
in Yap:
?- registration_satisfaction(r0,X), registration_grade(r0,a).
% YAP: no handler for error error(domain_error(bayesian_domain),bind_clpbns(var(grade(r0),[a,b,c,d],[0.2,0.7,0.85,0.1,0.2,0.5,0.01,0.05,0.1,0.6,0.25,0.12,0.3,0.6,0.35,0.04,0.15,0.4,0.15,0.04,0.02,0.4,0.15,0.12,0.5,0.6,0.4,0.05,0.01,0.01,0.2,0.05,0.03,0.45,0.2,0.1],[_D11,_D12]),var(grade(r0),[a,b,c,d],[0.2,0.7,0.85,0.1,0.2,0.5,0.01,0.05,0.1,0.6,0.25,0.12,0.3,0.6,0.35,0.04,0.15,0.4,0.15,0.04,0.02,0.4,0.15,0.12,0.5,0.6,0.4,0.05,0.01,0.01,0.2,0.05,0.03,0.45,0.2,0.1],[_D13,_D12])))
sc([registration_satisfaction(r0,X)],[registration_grade(r0,1)],P).
P = 0.528,
X = 1 ? ;
P = 0.167,
X = 2 ? ;
P = 0.305,
X = 3 ? ;
no
?- sc([ registration_grade(r0,X)],[registration_satisfaction(r0,3)],P).
P = 0.0293052037923492,
X = 1 ? ;
P = 0.114760451955444,
X = 2 ? ;
P = 0.322837654892765,
X = 3 ? ;
P = 0.533096689359442,
X = 4 ? ;
no
?- registration_satisfaction(r0,X), registration_grade(r0,d).
% YAP: no handler for error error(domain_error(bayesian_domain),bind_clpbns(var(grade(r0),[a,b,c,d],[0.2,0.7,0.85,0.1,0.2,0.5,0.01,0.05,0.1,0.6,0.25,0.12,0.3,0.6,0.35,0.04,0.15,0.4,0.15,0.04,0.02,0.4,0.15,0.12,0.5,0.6,0.4,0.05,0.01,0.01,0.2,0.05,0.03,0.45,0.2,0.1],[_D11,_D12]),var(grade(r0),[a,b,c,d],[0.2,0.7,0.85,0.1,0.2,0.5,0.01,0.05,0.1,0.6,0.25,0.12,0.3,0.6,0.35,0.04,0.15,0.4,0.15,0.04,0.02,0.4,0.15,0.12,0.5,0.6,0.4,0.05,0.01,0.01,0.2,0.05,0.03,0.45,0.2,0.1],[_D13,_D12])))
sc([ registration_grade(r0,X)],[registration_satisfaction(r0,3)],P).
?- registration_satisfaction(r0,h), registration_grade(r0,X).
% YAP: no handler for error error(domain_error(bayesian_domain),bind_clpbns(var(grade(r0),[a,b,c,d],[0.2,0.7,0.85,0.1,0.2,0.5,0.01,0.05,0.1,0.6,0.25,0.12,0.3,0.6,0.35,0.04,0.15,0.4,0.15,0.04,0.02,0.4,0.15,0.12,0.5,0.6,0.4,0.05,0.01,0.01,0.2,0.05,0.03,0.45,0.2,0.1],[_D11,_D12]),var(grade(r0),[a,b,c,d],[0.2,0.7,0.85,0.1,0.2,0.5,0.01,0.05,0.1,0.6,0.25,0.12,0.3,0.6,0.35,0.04,0.15,0.4,0.15,0.04,0.02,0.4,0.15,0.12,0.5,0.6,0.4,0.05,0.01,0.01,0.2,0.05,0.03,0.45,0.2,0.1],[_D13,_D12])))
s([course_rating(c0,X)],P).
P = 0.5392099,
X = h ? ;
P = 0.2,
X = l ? ;
P = 0.2607901,
X = m ? ;
no
yap does not end, starts thrashing
sc([course_difficulty(c0,X)],[course_rating(c0,h)],P).
P = 0.235185778302661,
X = h ? ;
P = 0.259096503977393,
X = l ? ;
P = 0.505717717719945,
X = m ? ;
no
yap does not end, starts thrashing
s([course_difficulty(c0,X)],P).
P = 0.25,
X = h ? ;
P = 0.25,
X = l ? ;
P = 0.5,
X = m ? ;
no
ok
student_ranking(s0,X).
| ?- s([student_ranking(s0,X)],P).
P = 0.33105221125,
X = h ? ;
P = 0.204865630625,
X = l ? ;
P = 0.46408215812500003,
X = m ?
not checked with s
yap:
true ?
yes
sc([student_ranking(s0,X)],[student_intelligence(s0,h)],P).
P = 0.42922046875000003,
X = h ? ;
P = 0.20003319999999997,
X = l ? ;
P = 0.3707463312499999,
X = m ? ;
no
not checked with s
yap:
true
*/
professor_ability(Key,h):0.50;professor_ability(Key,m): 0.40;professor_ability(Key,l): 0.10.
professor_popularity(Key, h):0.9; professor_popularity(Key, m):0.09; professor_popularity(Key, l):0.01 :-
professor_ability(Key, h).
professor_popularity(Key, h):0.2; professor_popularity(Key, m):0.6; professor_popularity(Key, l):0.2:-
professor_ability(Key, m).
professor_popularity(Key, h):0.01; professor_popularity(Key, m):0.09; professor_popularity(Key, l):0.9 :-
professor_ability(Key,l).
registration_course(Key, CKey) :-
registration(Key, CKey, _).
registration_student(Key, SKey) :-
registration(Key, _, SKey).
registration_grade(Key, 4): 0.2;registration_grade(Key, 3): 0.6;registration_grade(Key, 2): 0.15 ;
registration_grade(Key, 1): 0.05 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, h) ,
course_difficulty(CKey, h).
registration_grade(Key, 4): 0.7;registration_grade(Key, 3): 0.25;registration_grade(Key, 2): 0.04 ;
registration_grade(Key, 1): 0.01 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, h) ,
course_difficulty(CKey, m).
registration_grade(Key, 4): 0.85;registration_grade(Key, 3): 0.12;registration_grade(Key, 2): 0.02 ;
registration_grade(Key, 1): 0.01 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, h) ,
course_difficulty(CKey, l).
registration_grade(Key, 4): 0.1;registration_grade(Key, 3): 0.3;registration_grade(Key, 2): 0.4 ;
registration_grade(Key, 1): 0.2 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, m) ,
course_difficulty(CKey, h).
registration_grade(Key, 4): 0.2;registration_grade(Key, 3): 0.6;registration_grade(Key, 2): 0.15 ;
registration_grade(Key, 1): 0.05 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, m) ,
course_difficulty(CKey, m).
registration_grade(Key, 4): 0.5;registration_grade(Key, 3): 0.35;registration_grade(Key, 2): 0.12;
registration_grade(Key, 1): 0.03 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, m) ,
course_difficulty(CKey, l).
registration_grade(Key, 4): 0.01;registration_grade(Key, 3): 0.04;registration_grade(Key, 2): 0.5 ;
registration_grade(Key, 1): 0.45 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, l) ,
course_difficulty(CKey, h).
registration_grade(Key, 4): 0.05;registration_grade(Key, 3): 0.15;registration_grade(Key, 2): 0.6 ;
registration_grade(Key, 1): 0.2 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, l) ,
course_difficulty(CKey, m).
registration_grade(Key, 4): 0.1;registration_grade(Key, 3): 0.4;registration_grade(Key, 2): 0.4 ;
registration_grade(Key, 1): 0.1 :-
registration(Key, CKey, SKey) ,
student_intelligence(SKey, l) ,
course_difficulty(CKey, l).
registration_satisfaction(Key, 3): 0.98 ; registration_satisfaction(Key, 2): 0.01 ;
registration_satisfaction(Key, 1): 0.01 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, h),
registration_grade(Key, 4).
registration_satisfaction(Key, 3): 0.9 ; registration_satisfaction(Key, 2): 0.09 ;
registration_satisfaction(Key, 1): 0.01 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, h),
registration_grade(Key, 3).
registration_satisfaction(Key, 3): 0.8 ; registration_satisfaction(Key, 2): 0.15 ;
registration_satisfaction(Key, 1): 0.05 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, h),
registration_grade(Key, 2).
registration_satisfaction(Key, 3): 0.6 ; registration_satisfaction(Key, 2): 0.3 ;
registration_satisfaction(Key, 1): 0.1 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, h),
registration_grade(Key, 1).
registration_satisfaction(Key, 3): 0.9 ; registration_satisfaction(Key, 2): 0.05 ;
registration_satisfaction(Key, 1): 0.05 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, m),
registration_grade(Key, 4).
registration_satisfaction(Key, 3): 0.4 ; registration_satisfaction(Key, 2): 0.4 ;
registration_satisfaction(Key, 1): 0.2 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, m),
registration_grade(Key, 3).
registration_satisfaction(Key, 3): 0.2 ; registration_satisfaction(Key, 2): 0.3 ;
registration_satisfaction(Key, 1): 0.5 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, m),
registration_grade(Key, 2).
registration_satisfaction(Key, 3): 0.01 ; registration_satisfaction(Key, 2): 0.04 ;
registration_satisfaction(Key, 1): 0.95 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, m),
registration_grade(Key, 1).
registration_satisfaction(Key, 3): 0.5 ; registration_satisfaction(Key, 2): 0.35 ;
registration_satisfaction(Key, 1): 0.15 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, l),
registration_grade(Key, 4).
registration_satisfaction(Key, 3): 0.2 ; registration_satisfaction(Key, 2): 0.3 ;
registration_satisfaction(Key, 1): 0.5 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, l),
registration_grade(Key, 3).
registration_satisfaction(Key, 3): 0.01 ; registration_satisfaction(Key, 2): 0.09 ;
registration_satisfaction(Key, 1): 0.9 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, l),
registration_grade(Key, 2).
registration_satisfaction(Key, 3): 0.01 ; registration_satisfaction(Key, 2): 0.01 ;
registration_satisfaction(Key, 1): 0.98 :-
registration(Key, CKey,_),
course(CKey, PKey),
professor_ability(PKey, l),
registration_grade(Key, 1).
course_rating(CKey, h):0.2; course_rating(CKey, m):0.2;course_rating(CKey, l):0.6:-
bagof(Sat, RKey^(registration_course(RKey,CKey), registration_satisfaction(RKey,Sat)), Sats),
average(Sats, Av),Av<1.
course_rating(CKey, h):0.2; course_rating(CKey, m):0.6;course_rating(CKey, l):0.2:-
bagof(Sat, RKey^(registration_course(RKey,CKey), registration_satisfaction(RKey,Sat)), Sats),
average(Sats, Av),Av<2,Av>=1.
course_rating(CKey, h):0.6; course_rating(CKey,m):0.2;course_rating(CKey, l):0.2:-
bagof(Sat, RKey^(registration_course(RKey,CKey), registration_satisfaction(RKey,Sat)), Sats),
average(Sats, Av),Av>=2.
course_difficulty(Key, h) :0.25 ; course_difficulty(Key, m): 0.5 ; course_difficulty(Key, l) : 0.25.
student_intelligence(Key, h): 0.5; student_intelligence(Key,m):0.4; student_intelligence(Key,l):0.1.
student_ranking(Key, h):0.2; student_ranking(Key, m):0.2;student_ranking(Key, l):0.6:-
bagof(Grade, CKey^(registration_student(CKey,Key),
registration_grade(CKey, Grade)), Grades),
average(Grades, Av),Av<1.5.
student_ranking(Key, h):0.2; student_ranking(Key, m):0.6;student_ranking(Key, l):0.2:-
bagof(Grade, CKey^(registration_student(CKey,Key),
registration_grade(CKey, Grade)), Grades),
average(Grades, Av),Av>=1.5,Av<3.5.
student_ranking(Key, h):0.6; student_ranking(Key, m):0.2;student_ranking(Key, l):0.2:-
bagof(Grade, CKey^(registration_student(CKey,Key),
registration_grade(CKey, Grade)), Grades),
average(Grades, Av),Av>=3.5.
professor(p0).
professor(p1).
professor(p2).
professor(p3).
professor(p4).
professor(p5).
professor(p6).
professor(p7).
professor(p8).
professor(p9).
professor(p10).
professor(p11).
professor(p12).
professor(p13).
professor(p14).
professor(p15).
professor(p16).
professor(p17).
professor(p18).
professor(p19).
professor(p20).
professor(p21).
professor(p22).
professor(p23).
professor(p24).
professor(p25).
professor(p26).
professor(p27).
professor(p28).
professor(p29).
professor(p30).
professor(p31).
course(c0,p24).
course(c1,p7).
course(c2,p16).
course(c3,p27).
course(c4,p25).
course(c5,p6).
course(c6,p28).
course(c7,p1).
course(c8,p29).
course(c9,p23).
course(c10,p17).
course(c11,p16).
course(c12,p11).
course(c13,p28).
course(c14,p13).
course(c15,p7).
course(c16,p21).
course(c17,p15).
course(c18,p8).
course(c19,p30).
course(c20,p1).
course(c21,p23).
course(c22,p11).
course(c23,p9).
course(c24,p0).
course(c25,p30).
course(c26,p15).
course(c27,p4).
course(c28,p26).
course(c29,p29).
course(c30,p31).
course(c31,p19).
course(c32,p5).
course(c33,p14).
course(c34,p14).
course(c35,p25).
course(c36,p21).
course(c37,p10).
course(c38,p2).
course(c39,p20).
course(c40,p3).
course(c41,p18).
course(c42,p9).
course(c43,p20).
course(c44,p17).
course(c45,p19).
course(c46,p6).
course(c47,p4).
course(c48,p12).
course(c49,p10).
course(c50,p2).
course(c51,p22).
course(c52,p31).
course(c53,p24).
course(c54,p0).
course(c55,p5).
course(c56,p22).
course(c57,p13).
course(c58,p18).
course(c59,p12).
course(c60,p27).
course(c61,p3).
course(c62,p8).
course(c63,p26).
student(s0).
student(s1).
student(s2).
student(s3).
student(s4).
student(s5).
student(s6).
student(s7).
student(s8).
student(s9).
student(s10).
student(s11).
student(s12).
student(s13).
student(s14).
student(s15).
student(s16).
student(s17).
student(s18).
student(s19).
student(s20).
student(s21).
student(s22).
student(s23).
student(s24).
student(s25).
student(s26).
student(s27).
student(s28).
student(s29).
student(s30).
student(s31).
student(s32).
student(s33).
student(s34).
student(s35).
student(s36).
student(s37).
student(s38).
student(s39).
student(s40).
student(s41).
student(s42).
student(s43).
student(s44).
student(s45).
student(s46).
student(s47).
student(s48).
student(s49).
student(s50).
student(s51).
student(s52).
student(s53).
student(s54).
student(s55).
student(s56).
student(s57).
student(s58).
student(s59).
student(s60).
student(s61).
student(s62).
student(s63).
student(s64).
student(s65).
student(s66).
student(s67).
student(s68).
student(s69).
student(s70).
student(s71).
student(s72).
student(s73).
student(s74).
student(s75).
student(s76).
student(s77).
student(s78).
student(s79).
student(s80).
student(s81).
student(s82).
student(s83).
student(s84).
student(s85).
student(s86).
student(s87).
student(s88).
student(s89).
student(s90).
student(s91).
student(s92).
student(s93).
student(s94).
student(s95).
student(s96).
student(s97).
student(s98).
student(s99).
student(s100).
student(s101).
student(s102).
student(s103).
student(s104).
student(s105).
student(s106).
student(s107).
student(s108).
student(s109).
student(s110).
student(s111).
student(s112).
student(s113).
student(s114).
student(s115).
student(s116).
student(s117).
student(s118).
student(s119).
student(s120).
student(s121).
student(s122).
student(s123).
student(s124).
student(s125).
student(s126).
student(s127).
student(s128).
student(s129).
student(s130).
student(s131).
student(s132).
student(s133).
student(s134).
student(s135).
student(s136).
student(s137).
student(s138).
student(s139).
student(s140).
student(s141).
student(s142).
student(s143).
student(s144).
student(s145).
student(s146).
student(s147).
student(s148).
student(s149).
student(s150).
student(s151).
student(s152).
student(s153).
student(s154).
student(s155).
student(s156).
student(s157).
student(s158).
student(s159).
student(s160).
student(s161).
student(s162).
student(s163).
student(s164).
student(s165).
student(s166).
student(s167).
student(s168).
student(s169).
student(s170).
student(s171).
student(s172).
student(s173).
student(s174).
student(s175).
student(s176).
student(s177).
student(s178).
student(s179).
student(s180).
student(s181).
student(s182).
student(s183).
student(s184).
student(s185).
student(s186).
student(s187).
student(s188).
student(s189).
student(s190).
student(s191).
student(s192).
student(s193).
student(s194).
student(s195).
student(s196).
student(s197).
student(s198).
student(s199).
student(s200).
student(s201).
student(s202).
student(s203).
student(s204).
student(s205).
student(s206).
student(s207).
student(s208).
student(s209).
student(s210).
student(s211).
student(s212).
student(s213).
student(s214).
student(s215).
student(s216).
student(s217).
student(s218).
student(s219).
student(s220).
student(s221).
student(s222).
student(s223).
student(s224).
student(s225).
student(s226).
student(s227).
student(s228).
student(s229).
student(s230).
student(s231).
student(s232).
student(s233).
student(s234).
student(s235).
student(s236).
student(s237).
student(s238).
student(s239).
student(s240).
student(s241).
student(s242).
student(s243).
student(s244).
student(s245).
student(s246).
student(s247).
student(s248).
student(s249).
student(s250).
student(s251).
student(s252).
student(s253).
student(s254).
student(s255).
registration(r0,c16,s0).
registration(r1,c10,s0).
registration(r2,c57,s0).
registration(r3,c22,s1).
registration(r4,c55,s1).
registration(r5,c27,s1).
registration(r6,c14,s2).
registration(r7,c52,s2).
registration(r8,c10,s2).
registration(r9,c47,s3).
registration(r10,c16,s3).
registration(r11,c62,s3).
registration(r12,c12,s4).
registration(r13,c11,s4).
registration(r14,c17,s4).
registration(r15,c52,s5).
registration(r16,c1,s5).
registration(r17,c35,s5).
registration(r18,c0,s6).
registration(r19,c7,s6).
registration(r20,c40,s6).

View File

@ -0,0 +1,52 @@
/*
?- s([registration_grade(r0,X)],P).
student_ranking(s0,X).
s([student_ranking(s0,h)],P).
P = 0,6646250000000005 ?
s([student_ranking(s0,l)],P).
P = 0,33537499999999987
ok, checked with s.pl
*/
registration_course(Key, CKey) :-
registration(Key, CKey, _).
registration_student(Key, SKey) :-
registration(Key, _, SKey).
registration_grade(Key, 4): 0.2;
registration_grade(Key, 1): 0.8 :-
registration(Key, CKey, SKey) ,
course_difficulty(CKey, h).
registration_grade(Key, 4): 0.85;
registration_grade(Key, 1): 0.15 :-
registration(Key, CKey, SKey) ,
course_difficulty(CKey, l).
course_difficulty(Cou, h) :0.5 ; course_difficulty(Cou, l) : 0.5.
student_ranking(Stu, h):0.2; student_ranking(Stu, l):0.8:-
bagof(Grade, CKey^(registration_student(CKey,Stu),
registration_grade(CKey, Grade)), Grades),
average(Grades, Av),Av<2.
student_ranking(Stu, h):0.8; student_ranking(Stu, l):0.2:-
bagof(Grade, CKey^(registration_student(CKey,Stu),
registration_grade(CKey, Grade)), Grades),
average(Grades, Av),Av>=2.
registration(r0,c16,s0).
registration(r1,c10,s0).

View File

@ -0,0 +1,3 @@
universe(['Stu'],[s0]).
universe(['Key'],[r0,r1]).
universe(['Cou'],[c16,c10]).

View File

@ -0,0 +1,95 @@
/*
s([student_rank(jane_doe,h)],P).
P = 0.465
s([student_rank(jane_doe,l)],P).
P = 0.535
s([course_rat(C,h)],P).
C = phil101,
P = 0.330656
s([course_rat(C,l)],P).
C = phil101,
P = 0.669344
*/
professor(gump).
%1
professor_teach_ab(P,h):0.2;professor_teach_ab(P,l):0.8:-
professor(P).
%2
professor_pop(P,h):0.7;professor_pop(P,l):0.3:-
professor_teach_ab(P,h).
%3
professor_pop(P,h):0.2;professor_pop(P,l):0.8:-
professor_teach_ab(P,l).
student(jane_doe).
%4
student_int(S,h):0.6;student_int(S,l):0.4:-
student(S).
%5
student_rank(S,h):0.6;student_rank(S,l):0.4:-
bagof(G,R^(registr_stu(R,S),registr_gr(R,G)),L),
average(L,Av),Av>1.5.
%6
student_rank(S,h):0.4;student_rank(S,l):0.6:-
bagof(G,R^(registr_stu(R,S),registr_gr(R,G)),L),
average(L,Av),Av =< 1.5.
registr(5639).
registr_cou(5639,phil101).
registr_stu(5639,jane_doe).
registr(5640).
registr_cou(5640,phil101).
registr_stu(5640,jane_doe).
%7
registr_gr(Reg,1):0.5;registr_gr(Reg,2):0.5:-
registr_stu(Reg,S),student_int(S,h),registr_cou(Reg,C),course_dif(C,h).
%8
registr_gr(Reg,1):0.2;registr_gr(Reg,2):0.8:-
registr_stu(Reg,S),student_int(S,h),registr_cou(Reg,C),course_dif(C,l).
%9
registr_gr(Reg,1):0.8;registr_gr(Reg,2):0.2:-
registr_stu(Reg,S),student_int(S,l),registr_cou(Reg,C),course_dif(C,h).
%10
registr_gr(Reg,1):0.5;registr_gr(Reg,2):0.5:-
registr_stu(Reg,S),student_int(S,l),registr_cou(Reg,C),course_dif(C,l).
%11
registr_sat(Reg,1):0.2;registr_sat(Reg,2):0.8:-
registr_gr(Reg,2),registr_cou(Reg,C),course_prof(C,P),
professor_teach_ab(P,h).
%12
registr_sat(Reg,1):0.4;registr_sat(Reg,2):0.6:-
registr_gr(Reg,2),registr_cou(Reg,C),course_prof(C,P),
professor_teach_ab(P,l).
%13
registr_sat(Reg,1):0.6;registr_sat(Reg,2):0.4:-
registr_gr(Reg,1),registr_cou(Reg,C),course_prof(C,P),
professor_teach_ab(P,h).
%14
registr_sat(Reg,1):0.8;registr_sat(Reg,2):0.2:-
registr_gr(Reg,1),registr_cou(Reg,C),course_prof(C,P),
professor_teach_ab(P,l).
course(phil101).
course_prof(phil101,gump).
%15
course_dif(C,h):0.5;course_dif(C,l):0.5:-
course(C).
%16
course_rat(C,h):0.8;course_rat(C,l):0.2:-
bagof(Stu,R^(registr_cou(R,C),registr_sat(R,Stu)),L),
average(L,Av),Av>1.5.
%17
course_rat(C,h):0.2;course_rat(C,l):0.8:-
bagof(Stu,R^(registr_cou(R,C),registr_sat(R,Stu)),L),
average(L,Av),Av=< 1.5.

View File

@ -0,0 +1,4 @@
universe(['S'],[jane_doe]).
universe(['P'],[gump]).
universe(['Reg'],[5639,5640]).
universe(['C'],[phil101]).

View File

@ -0,0 +1,39 @@
/*
s([on(0,1)],P).
P = 0.333333333333333 ?
s([on(1,1)],P).
P = 0.222222222222222
s([on(2,1)],P).
P = 0.148148147703704
s([on(3,1)],P).
% OUT OF DATABASE SPACE ERROR- Database crashed against Stacks
s([on(4,1)],P).
% OUT OF DATABASE SPACE ERROR- Database crashed against Stacks
s([on(5,1)],P).
% OUT OF DATABASE SPACE ERROR- Database crashed against Stacks
s([on(0,1),on(1,1)],P).
P = 0.111111111111111
s([on(0,1),on(1,1),on(2,1)],P).
P = 0.037037037037037
sc([on(1,1)],[on(0,1)],P).
P = 0.333333333333333
sc([on(2,1)],[on(0,1)],P).
P = 0.222222222222222
sc([on(3,1)],[on(0,1)],P).
P = 0.148148148148148
sc([on(2,1)],[on(1,1)],P).
P = 0.333333333333333 ?
*/
on(0,1):1/3;on(0,2):1/3;on(0,3):1/3.
on(X,1):1/3;on(X,2):1/3;on(X,3):1/3:-
X1 is X-1,X1>=0,
on(X1,Y),
\+ on(X1,3).

View File

@ -0,0 +1,3 @@
universe(['X'],[1,2]).
universe(['X1'],[0,1]).
universe(['Y'],[1,2,3]).

View File

@ -0,0 +1,21 @@
/*
s([on(3,1)],P).
P = 0.0625
sc([on(3,1)],[on(2,1)],P).
P = 0.5 ?
sc([on(4,1)],[on(2,1)],P).
P = 0.25
sc([on(10,1)],[on(2,1)],P).
P = 0.00390625
*/
on(0,1):0.5;on(0,2):0.5.
on(X,1):0.5;on(X,2):0.5:-
X1 is X-1,X1>=0,
on(X1,_),
\+ on(X1,2).

263
cplint/semantics.yap Normal file
View File

@ -0,0 +1,263 @@
/*
Program for computing the probability of a query directly according to the
semantics
Copyright (c) 2007, Fabrizio Riguzzi
*/
:-use_module(library(lists)).
:-dynamic setting/2.
:-set_prolog_flag(unknown,fail).
setting(epsilon,0.00001).
setting(test_builtins,false).
solve(GoalsList,Prob):-
s(GoalsList,Prob).
s(GoalsList,Prob):-
program_names(L),
list2and(GoalsList,Goals),
run_query(L,Goals,0,Prob).
run_query([],_G,P,P).
run_query([Prog|T],Goal,PIn,POut):-
elab_conj(Prog,Goal,Goal1),
call(Goal1),
prob(Prog,P),
P1 is PIn+P,
run_query(T,Goal,P1,POut).
run_query([Prog|T],Goal,PIn,POut):-
elab_conj(Prog,Goal,Goal1),
\+ call(Goal1),
run_query(T,Goal,PIn,POut).
p(File):-
atom_concat(File,'.uni',FileUni),
consult(FileUni),
atom_concat(File,'.cpl',FilePl),
open(FilePl,read,S),
read_clauses(S,C),
close(S),
process_clauses(C,ClausesVar),
instantiate(ClausesVar,[],Clauses),
assert(program(1)),
assert(program_names([])),
create_programs(Clauses).
create_programs(Clauses):-
create_single_program(Clauses,1,Program),
retract(program(N)),
number_codes(N,NC),
atom_codes(NA,NC),
atom_concat(p,NA,Name),
N1 is N+1,
assert(program(N1)),
format("Writing program ~d~n",[N]),
write_program(Name,Program),
retract(program_names(L)),
append(L,[Name],L1),
assert(program_names(L1)),
fail.
create_programs(_).
write_program(_Name,[]).
write_program(Name,[(H:-B)|T]):-
elab_conj(Name,H,H1),
elab_conj(Name,B,B1),
assertz((H1:-B1)),
write_program(Name,T).
elab_conj(_Name,true,true):-!.
elab_conj(Name,\+(B),\+(B1)):-!,
elab_conj(Name,B,B1).
elab_conj(Name,(BL,Rest),(BL1,Rest1)):-!,
elab_conj(Name,BL,BL1),
elab_conj(Name,Rest,Rest1).
elab_conj(Name,bagof(V,EV^G,L),bagof(V,EV^GL,L)):-!,
elab_conj(Name,G,GL).
elab_conj(Name,bagof(V,G,L),bagof(V,GL,L)):-!,
elab_conj(Name,G,GL).
elab_conj(Name,setof(V,EV^G,L),setof(V,EV^GL,L)):-!,
elab_conj(Name,G,GL).
elab_conj(Name,setof(V,G,L),setof(V,GL,L)):-!,
elab_conj(Name,G,GL).
elab_conj(Name,findall(V,G,L),findall(V,GL,L)):-!,
elab_conj(Name,G,GL).
elab_conj(_Name,A,A):-
bg(A),!.
elab_conj(_Name,A,A):-
builtin(A),!.
elab_conj(Name,Lit,Lit1):-
Lit\=(_,_),
Lit=..[Pred|Args],
Lit1=..[Pred,Name|Args].
create_single_program([],P,[(prob(P):-true)]).
create_single_program([r(H,B)|T],PIn,[(HA:-B)|T1]):-
member((HA:P),H),
P1 is PIn*P,
create_single_program(T,P1,T1).
instantiate([],C,C).
instantiate([r(V,H,B)|T],CIn,COut):-
findall(r(H,BOut),instantiate_clause(V,H,B,BOut),L),
append(CIn,L,C1),
instantiate(T,C1,COut).
check_body([],[]).
check_body([H|T],TOut):-
builtin(H),setting(test_builtins,true),!,
call(H),
check_body(T,TOut).
check_body([H|T],[H|TOut]):-
check_body(T,TOut).
instantiate_clause([],_H,B,BOut):-
list2and(BL,B),
check_body(BL,BLOut),
list2and(BLOut,BOut).
instantiate_clause([VarName=Var|T],H,BIn,BOut):-
universe(VarNames,U),
member(VarName,VarNames),
member(Var,U),
instantiate_clause(T,H,BIn,BOut).
instantiate_clause([VarName=_Var|T],H,BIn,BOut):-
\+ varName_present(VarName),!,
instantiate_clause(T,H,BIn,BOut).
varName_present(VarName):-
universe(VarNames,_U), member(VarName,VarNames).
process_clauses([(end_of_file,[])],[]).
process_clauses([((H:-B),V)|T],[r(V,HL,B)|T1]):-
H=(_;_),!,
list2or(HL1,H),
process_head(HL1,0,HL),
process_clauses(T,T1).
process_clauses([((H:-B),V)|T],[r(V,HL,B)|T1]):-
H=(_:_),!,
list2or(HL1,H),
process_head(HL1,0,HL),
process_clauses(T,T1).
process_clauses([((H:-B),V)|T],[r(V,[H:1],B)|T1]):-!,
process_clauses(T,T1).
process_clauses([(H,V)|T],[r(V,HL,true)|T1]):-
H=(_;_),!,
list2or(HL1,H),
process_head(HL1,0,HL),
process_clauses(T,T1).
process_clauses([(H,V)|T],[r(V,HL,true)|T1]):-
H=(_:_),!,
list2or(HL1,H),
process_head(HL1,0,HL),
process_clauses(T,T1).
process_clauses([(H,V)|T],[r(V,[H:1],true)|T1]):-
process_clauses(T,T1).
process_head([H:PH],P,[H:PH1|Null]):-
PH1 is PH,
PNull is 1-P-PH1,
setting(epsilon,Eps),
EpsNeg is - Eps,
PNull > EpsNeg,
(PNull>Eps->
Null=['':PNull]
;
Null=[]
).
process_head([H:PH|T],P,[H:PH1|NT]):-
PH1 is PH,
P1 is P+PH1,
process_head(T,P1,NT).
read_clauses(S,[(Cl,V)|Out]):-
read_term(S,Cl,[variable_names(V)]),
(Cl=end_of_file->
Out=[]
;
read_clauses(S,Out)
).
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).
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).
bg(member(_El,_L)).
bg(average(_L,_Av)).
bg(max_list(_L,_Max)).
bg(min_list(_L,_Max)).
average(L,Av):-
sum_list(L,Sum),
length(L,N),
Av is Sum/N.
/* set(Par,Value) can be used to set the value of a parameter */
set(Parameter,Value):-
retract(setting(Parameter,_)),
assert(setting(Parameter,Value)).

177
cplint/test.yap Normal file
View File

@ -0,0 +1,177 @@
/*
LPAD and CP-Logic interpreter test program
Copyright (c) 2007, Fabrizio Riguzzi
use
:-t.
to execute the test
*/
%:-use_module(library(cplint)).
:-compile(cplint).
epsilon(0.000001).
close_to(V,T):-
epsilon(E),
TLow is T-E,
THigh is T+E,
TLow<V,
V<THigh.
t:-
files(F),
statistics(runtime,[_,_]),
test_files(F),
statistics(runtime,[_,T]),
T1 is T /1000,
format("Time ~f secs~n",[T1]).
test_files([]).
test_files([H|T]):-
format("~a~n",[H]),
atom_concat('examples/',H,HN),
p(HN),
findall(A,test(A,H),L),
test_all(H,L),
test_files(T).
test_all(_F,[]).
test_all(F,[H|T]):-
format("~a ~p.~n",[F,H]),
call(H),
test_all(F,T).
files([paper_ref_not,paper_ref,female,esapprox,esrange,threesideddice,
mendel,student,school_simple,school,coin2,es]).
test((s([\+ cites_cited(c1,p1)],P),close_to(P,0.7)),paper_ref_not).
test((s([cites_citing(c1,p1)],P),close_to(P,0.14)),paper_ref_not).
test((s([cites_cited(c1,p1)],P),close_to(P,0.181333333)),paper_ref).
test((s([cites_cited(c1,p2)],P),close_to(P,0.181333333)),paper_ref).
test((s([cites_cited(c1,p4)],P),close_to(P,0.181333333)),paper_ref).
test((s([cites_cited(c1,p3)],P),close_to(P,0.228)),paper_ref).
test((s([cites_cited(c1,p5)],P),close_to(P,0.228)),paper_ref).
test((s([female(f)],P),close_to(P,0.6)),female).
test((s([male(f)],P),close_to(P,0.4)),female).
test((s([a],P),close_to(P,0.1719)),esapprox).
test((s([a(1)],P),close_to(P,0.2775)),esrange).
test((s([a(2)],P),close_to(P,0.36)),esrange).
test((s([on(0,1)],P),close_to(P,0.333333333333333)),threesideddice).
test((s([on(1,1)],P),close_to(P,0.222222222222222)),threesideddice).
test((s([on(2,1)],P),close_to(P,0.148148147703704)),threesideddice).
test((sc([on(2,1)],[on(0,1)],P),close_to(P,0.222222222222222)),threesideddice).
test((sc([on(2,1)],[on(1,1)],P),close_to(P,0.333333333333333)),threesideddice).
test((s([cg(s,1,p)],P),close_to(P,0.75)),mendel).
test((s([cg(s,1,w)],P),close_to(P,0.25)),mendel).
test((s([cg(s,2,p)],P),close_to(P,0.25)),mendel).
test((s([cg(s,2,w)],P),close_to(P,0.75)),mendel).
test((s([cg(f,2,w)],P),close_to(P,0.5)),mendel).
test((s([cg(s,2,w)],P),close_to(P,0.75)),mendel).
test((s([a],P),close_to(P,0.226)),es).
test((s([heads(coin1)],P),close_to(P,0.51)),coin2).
test((s([heads(coin2)],P),close_to(P,0.51)),coin2).
test((s([tails(coin1)],P),close_to(P,0.49)),coin2).
test((s([tails(coin2)],P),close_to(P,0.49)),coin2).
test((s([student_rank(jane_doe,h)],P),close_to(P,0.465)),student).
test((s([student_rank(jane_doe,l)],P),close_to(P,0.535)),student).
test((s([course_rat(phil101,h)],P),close_to(P,0.330656)),student).
test((s([course_rat(phil101,l)],P),close_to(P,0.669344)),student).
test((s([professor_ability(p0,h)],P),close_to(P,0.5)),school).
test((s([professor_ability(p0,m)],P),close_to(P,0.4)),school).
test((s([professor_ability(p0,l)],P),close_to(P,0.1)),school).
test((s([professor_popularity(p0,h)],P),close_to(P,0.531)),school).
test((s([professor_popularity(p0,l)],P),close_to(P,0.175)),school).
test((s([professor_popularity(p0,m)],P),close_to(P,0.294)),school).
test((sc([professor_ability(p0,h)],[professor_popularity(p0,h)],P),close_to(P,0.847457627118644)),school).
test((sc([professor_ability(p0,l)],[professor_popularity(p0,h)],P),close_to(P,0.00188323917137476)),school).
test((sc([professor_ability(p0,m)],[professor_popularity(p0,h)],P),close_to(P,0.150659133709981)),school).
test((sc([professor_popularity(p0,h)],[professor_ability(p0,h)],P),close_to(P,0.9)),school).
test((sc([professor_popularity(p0,l)],[professor_ability(p0,h)],P),close_to(P,0.01)),school).
test((sc([professor_popularity(p0,m)],[professor_ability(p0,h)],P),close_to(P,0.09)),school).
test(( s([registration_grade(r0,1)],P),close_to(P,0.06675)),school).
test(( s([registration_grade(r0,2)],P),close_to(P,0.16575)),school).
test(( s([registration_grade(r0,3)],P),close_to(P, 0.356)),school).
test(( s([registration_grade(r0,4)],P),close_to(P,0.4115)),school).
test((sc([registration_grade(r0,1)],[registration_course(r0,C), course_difficulty(C,h)],P),close_to(P,0.15)),school).
test((sc([registration_grade(r0,2)],[registration_course(r0,C), course_difficulty(C,h)],P),close_to(P,0.285)),school).
test((sc([registration_grade(r0,3)],[registration_course(r0,C), course_difficulty(C,h)],P),close_to(P,0.424)),school).
test((sc([registration_grade(r0,4)],[registration_course(r0,C), course_difficulty(C,h)],P),close_to(P,0.141)),school).
test((sc([registration_grade(r0,1)], [registration_course(r0,C), course_difficulty(C,h), registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.05)),school).
test((sc([registration_grade(r0,2)], [registration_course(r0,C), course_difficulty(C,h), registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.15)),school).
test((sc([registration_grade(r0,3)], [registration_course(r0,C), course_difficulty(C,h), registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.6)),school).
test((sc([registration_grade(r0,4)], [registration_course(r0,C), course_difficulty(C,h), registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.2)),school).
test((sc([registration_grade(r0,1)],[registration_course(r0,C), course_difficulty(C,l), registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.01)),school).
test((sc([registration_grade(r0,2)],[registration_course(r0,C), course_difficulty(C,l), registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.02)),school).
test((sc([registration_grade(r0,3)],[registration_course(r0,C), course_difficulty(C,l), registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.12)),school).
test((sc([registration_grade(r0,4)],[registration_course(r0,C), course_difficulty(C,l), registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.85)),school).
test((s([registration_satisfaction(r0,1)],P),close_to(P,0.15197525)),school).
test((s([registration_satisfaction(r0,2)],P),close_to(P,0.1533102)),school).
test((s([registration_satisfaction(r0,3)],P),close_to(P,0.6947145)),school).
test((sc([registration_satisfaction(r0,1)],[ registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.0959225)),school).
test((sc([registration_satisfaction(r0,2)],[ registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.124515)),school).
test((sc([registration_satisfaction(r0,3)],[ registration_student(r0,S), student_intelligence(S,h)],P),close_to(P,0.7795625)),school).
test((sc([registration_satisfaction(r0,1)],[registration_grade(r0,4)],P),close_to(P,0.04)),school).
test((sc([registration_satisfaction(r0,2)],[registration_grade(r0,4)],P),close_to(P,0.06)),school).
test((sc([registration_satisfaction(r0,3)],[registration_grade(r0,4)],P),close_to(P,0.9)),school).
test((sc([registration_satisfaction(r0,1)],[registration_grade(r0,1)],P),close_to(P,0.528)),school).
test((sc([registration_satisfaction(r0,2)],[registration_grade(r0,1)],P),close_to(P,0.167)),school).
test((sc([registration_satisfaction(r0,3)],[registration_grade(r0,1)],P),close_to(P,0.305)),school).
test((sc([ registration_grade(r0,1)],[registration_satisfaction(r0,3)],P),close_to(P,0.0293052037923492)),school).
test((sc([ registration_grade(r0,2)],[registration_satisfaction(r0,3)],P),close_to(P, 0.114760451955444)),school).
test((sc([ registration_grade(r0,3)],[registration_satisfaction(r0,3)],P),close_to(P,0.322837654892765)),school).
test((sc([ registration_grade(r0,4)],[registration_satisfaction(r0,3)],P),close_to(P,0.533096689359442)),school).
test((s([course_rating(c0,h)],P),close_to(P,0.5392099)),school).
test((s([course_rating(c0,l)],P),close_to(P, 0.2)),school).
test((s([course_rating(c0,m)],P),close_to(P,0.2607901)),school).
test((sc([course_difficulty(c0,h)],[course_rating(c0,h)],P),close_to(P,0.235185778302661)),school).
test((sc([course_difficulty(c0,l)],[course_rating(c0,h)],P),close_to(P,0.259096503977393)),school).
test((sc([course_difficulty(c0,m)],[course_rating(c0,h)],P),close_to(P,0.505717717719945)),school).
test((s([course_difficulty(c0,h)],P),close_to(P,0.25)),school).
test((s([course_difficulty(c0,l)],P),close_to(P,0.25)),school).
test((s([course_difficulty(c0,m)],P),close_to(P,0.5)),school).
test((s([student_ranking(s0,h)],P),close_to(P,0.6646250000000005)),school_simple).
test((s([student_ranking(s0,l)],P),close_to(P,0.33537499999999987)),school_simple).