rework examples

This commit is contained in:
Tiago Gomes 2012-12-11 23:06:09 +00:00
parent ccba2d4177
commit 4310e42562
8 changed files with 196 additions and 114 deletions

View File

@ -1,23 +1,45 @@
:- use_module(library(pfl)). :- use_module(library(pfl)).
%:- set_solver(lve). :- set_solver(hve).
%:- set_solver(hve). %:- set_solver(ve).
%:- set_solver(jt).
%:- set_solver(bdd).
%:- set_solver(bp). %:- set_solver(bp).
%:- set_solver(cbp). %:- set_solver(cbp).
%:- set_solver(gibbs).
%:- set_solver(lve).
%:- set_solver(lkc).
%:- set_solver(lbp).
:- yap_flag(write_strings, off). bayes burglary ; burglary_table ; [].
bayes burglary::[t,f] ; [0.001, 0.999] ; []. bayes earthquake ; earthquake_table ; [].
bayes earthquake::[t,f] ; [0.002, 0.998]; []. bayes alarm, burglary, earthquake ; alarm_table ; [].
bayes alarm::[t,f], burglary, earthquake ; bayes john_calls, alarm ; john_calls_table ; [].
[0.95, 0.94, 0.29, 0.001, 0.05, 0.06, 0.71, 0.999] ;
[].
bayes john_calls::[t,f], alarm ; [0.9, 0.05, 0.1, 0.95] ; []. bayes mary_calls, alarm ; mary_calls_table ; [].
bayes mary_calls::[t,f], alarm ; [0.7, 0.01, 0.3, 0.99] ; []. burglary_table(
[ 0.001,
0.999 ]).
earthquake_table(
[ 0.002,
0.998 ]).
alarm_table(
[ 0.95, 0.94, 0.29, 0.001,
0.05, 0.06, 0.71, 0.999 ]).
john_calls_table(
[ 0.9, 0.05,
0.1, 0.95 ]).
mary_calls_table(
[ 0.7, 0.01,
0.3, 0.99 ]).
% ?- john_calls(J), mary_calls(t). % ?- john_calls(J), mary_calls(t).

View File

@ -1,9 +1,15 @@
:- use_module(library(pfl)). :- use_module(library(pfl)).
%:- set_solver(lve). :- set_solver(hve).
%:- set_solver(hve). %:- set_solver(ve).
%:- set_solver(jt).
%:- set_solver(bdd).
%:- set_solver(bp). %:- set_solver(bp).
%:- set_solver(cbp). %:- set_solver(cbp).
%:- set_solver(gibbs).
%:- set_solver(lve).
%:- set_solver(lkc).
%:- set_solver(lbp).
:- multifile people/2. :- multifile people/2.
:- multifile ev/1. :- multifile ev/1.
@ -14,74 +20,97 @@ people(p3, nyc).
people(p4, nyc). people(p4, nyc).
people(p5, nyc). people(p5, nyc).
ev(descn(p2, t)). ev(descn(p2, fits)).
ev(descn(p3, t)). ev(descn(p3, fits)).
ev(descn(p4, t)). ev(descn(p4, fits)).
ev(descn(p5, t)). ev(descn(p5, fits)).
bayes city_conservativeness(C)::[y,n] ; cons_table(C) ; [people(_,C)]. bayes city_conservativeness(C)::[high,low] ;
cons_table(C) ;
[people(_,C)].
bayes gender(P)::[m,f] ; gender_table(P) ; [people(P,_)]. bayes gender(P)::[male,female] ;
gender_table(P) ;
[people(P,_)].
bayes hair_color(P)::[t,f], city_conservativeness(C) ; hair_color_table(P) ; [people(P,C)]. bayes hair_color(P)::[dark,bright], city_conservativeness(C) ;
hair_color_table(P) ;
[people(P,C)].
bayes car_color(P)::[t,f], hair_color(P) ; car_color_table(P); [people(P,_)]. bayes car_color(P)::[dark,bright], hair_color(P) ;
car_color_table(P) ;
[people(P,_)].
bayes height(P)::[t,f], gender(P) ; height_table(P) ; [people(P,_)]. bayes height(P)::[tall,short], gender(P) ;
height_table(P) ;
[people(P,_)].
bayes shoe_size(P)::[t,f], height(P) ; shoe_size_table(P); [people(P,_)]. bayes shoe_size(P)::[big,small], height(P) ;
shoe_size_table(P) ;
[people(P,_)].
bayes guilty(P)::[y,n] ; guilty_table(P) ; [people(P,_)]. bayes guilty(P)::[y,n] ;
guilty_table(P) ;
[people(P,_)].
bayes descn(P)::[t,f], car_color(P), hair_color(P), height(P), guilty(P) ; descn_table(P) ; [people(P,_)]. bayes descn(P)::[fits,dont_fit], car_color(P),
hair_color(P), height(P), guilty(P) ;
descn_table(P) ;
[people(P,_)].
bayes witness(C)::[t,f], descn(Joe), descn(P2) ; wit_table ; [people(_,C), Joe=joe, P2=p2]. bayes witness(C), descn(Joe), descn(P2) ;
witness_table ;
% FIXME [people(_,C), Joe=joe, P2=p2].
%cons_table(amsterdam, [0.2, 0.8]) :- !.
cons_table(_, [0.8, 0.2]).
gender_table(_, [0.55, 0.45]). cons_table(amsterdam,
% special case for amsterdam: amsterdam is
% less conservative than other cities (is it?)
/* y */ [ 0.2,
/* n */ 0.8 ]) :- !. % FIXME
cons_table(_,
/* y */ [ 0.8,
/* n */ 0.2 ]).
gender_table(_,
/* male */ [ 0.55,
/* female */ 0.45 ]).
hair_color_table(_, hair_color_table(_,
/* conservative_city */ /* high low */
/* y n */ /* dark */ [ 0.05, 0.1,
[ 0.05, 0.1, /* bright */ 0.95, 0.9 ]).
0.95, 0.9 ]).
car_color_table(_, car_color_table(_,
/* t f */ /* dark bright */
[ 0.9, 0.2, /* dark */ [ 0.9, 0.2,
0.1, 0.8 ]). /* bright */ 0.1, 0.8 ]).
height_table(_, height_table(_,
/* m f */ /* male female */
[ 0.6, 0.4, /* tall */ [ 0.6, 0.4,
0.4, 0.6 ]). /* short */ 0.4, 0.6 ]).
shoe_size_table(_, shoe_size_table(_,
/* t f */ /* tall short */
[ 0.9, 0.1, /* big */ [ 0.9, 0.1,
0.1, 0.9 ]). /* small */ 0.1, 0.9 ]).
guilty_table(_, [0.23, 0.77]).
guilty_table(_,
/* yes */ [ 0.23,
/* no */ 0.77 ]).
descn_table(_, descn_table(_,
/* color, hair, height, guilt */ /* car_color(P), hair_color(P), height(P), guilty(P) */
/* ttttt tttf ttft ttff tfttt tftf tfft tfff ttttt fttf ftft ftff ffttt fftf ffft ffff */ /* fits */ [ 0.99, 0.5, 0.23, 0.88, 0.41, 0.3, 0.76, 0.87,
[ 0.99, 0.5, 0.23, 0.88, 0.41, 0.3, 0.76, 0.87, 0.44, 0.43, 0.29, 0.72, 0.23, 0.91, 0.95, 0.92, /* fits */ 0.44, 0.43, 0.29, 0.72, 0.23, 0.91, 0.95, 0.92,
0.01, 0.5, 0.77, 0.12, 0.59, 0.7, 0.24, 0.13, 0.56, 0.57, 0.71, 0.28, 0.77, 0.09, 0.05, 0.08]). /* dont_fit */ 0.01, 0.5, 0.77, 0.12, 0.59, 0.7, 0.24, 0.13,
/* dont_fit */ 0.56, 0.57, 0.71, 0.28, 0.77, 0.09, 0.05, 0.08 ]).
witness_table(
wit_table([0.2, 0.45, 0.24, 0.34, /* descn(Joe), descn(P2) */
0.8, 0.55, 0.76, 0.66]). /* t */ [ 0.2, 0.45, 0.24, 0.34,
/* f */ 0.8, 0.55, 0.76, 0.66 ]).
runall(G, Wrapper) :- runall(G, Wrapper) :-
@ -101,5 +130,5 @@ is_joe_guilty(Guilty) :-
guilty(joe, Guilty). guilty(joe, Guilty).
% ?- is_joe_guilty(Guilty) ?- is_joe_guilty(Guilty).

View File

@ -1,11 +1,14 @@
:- use_module(library(pfl)). :- use_module(library(pfl)).
%:- set_solver(lve). :- set_solver(hve).
%:- set_solver(hve). %:- set_solver(ve).
%:- set_solver(jt).
%:- set_solver(bp). %:- set_solver(bp).
%:- set_solver(cbp). %:- set_solver(cbp).
%:- set_solver(gibbs).
:- yap_flag(write_strings, off). %:- set_solver(lve).
%:- set_solver(lkc).
%:- set_solver(lbp).
:- multifile c/2. :- multifile c/2.
@ -25,9 +28,13 @@ c(p5,w1).
c(p5,w2). c(p5,w2).
c(p5,w3). c(p5,w3).
markov attends(P)::[t,f], hot(W)::[t,f] ; [0.2, 0.8, 0.8, 0.8] ; [c(P,W)]. markov attends(P), hot(W) ;
[0.2, 0.8, 0.8, 0.8] ;
[c(P,W)].
markov attends(P)::[t,f], series::[t,f] ; [0.501, 0.499, 0.499, 0.499] ; [c(P,_)]. markov attends(P), series ;
[0.501, 0.499, 0.499, 0.499] ;
[c(P,_)].
% ?- series(X). ?- series(X).

View File

@ -1,5 +1,3 @@
professor_ability(p0,h). professor_ability(p0,h).
professor_ability(p1,h). professor_ability(p1,h).
professor_ability(p2,m). professor_ability(p2,m).
@ -1250,6 +1248,7 @@ registration_grade(r854,c).
registration_grade(r855,d). registration_grade(r855,d).
registration_grade(r856,c). registration_grade(r856,c).
registration_satisfaction(r0,h). registration_satisfaction(r0,h).
registration_satisfaction(r1,l). registration_satisfaction(r1,l).
registration_satisfaction(r2,h). registration_satisfaction(r2,h).
@ -2431,3 +2430,4 @@ student_ranking(s252,b).
student_ranking(s253,b). student_ranking(s253,b).
student_ranking(s254,b). student_ranking(s254,b).
student_ranking(s255,c). student_ranking(s255,c).

View File

@ -1,11 +1,15 @@
:- use_module(library(pfl)). :- use_module(library(pfl)).
%:- set_solver(lve). :- set_solver(hve).
%:- set_solver(hve). %:- set_solver(ve).
%:- set_solver(jt).
%:- set_solver(bdd).
%:- set_solver(bp). %:- set_solver(bp).
%:- set_solver(cbp). %:- set_solver(cbp).
%:- set_solver(gibbs).
:- yap_flag(write_strings, off). %:- set_solver(lve).
%:- set_solver(lkc).
%:- set_solver(lbp).
:- multifile people/1. :- multifile people/1.
@ -16,16 +20,19 @@ people(X,Y) :-
people(Y), people(Y),
X \== Y. X \== Y.
markov smokes(X)::[t,f]; [1.0, 4.0552]; [people(X)]. markov smokes(X) ; [1.0, 4.0552]; [people(X)].
markov cancer(X)::[t,f]; [1.0, 9.9742]; [people(X)]. markov cancer(X) ; [1.0, 9.9742]; [people(X)].
markov friends(X,Y)::[t,f] ; [1.0, 99.48432] ; [people(X,Y)]. markov friends(X,Y) ; [1.0, 99.48432] ; [people(X,Y)].
markov smokes(X)::[t,f], cancer(X)::[t,f] ; [4.48169, 4.48169, 1.0, 4.48169] ; [people(X)]. markov smokes(X), cancer(X) ;
[4.48169, 4.48169, 1.0, 4.48169] ;
[people(X)].
markov friends(X,Y)::[t,f], smokes(X)::[t,f], smokes(Y)::[t,f] ; markov friends(X,Y), smokes(X), smokes(Y) ;
[3.004166, 3.004166, 3.004166, 3.004166, 3.004166, 1.0, 1.0, 3.004166] ; [people(X,Y)]. [3.004166, 3.004166, 3.004166, 3.004166, 3.004166, 1.0, 1.0, 3.004166] ;
[people(X,Y)].
% ?- friends(p1,p2,X). % ?- friends(p1,p2,X).

View File

@ -1,11 +1,15 @@
:- use_module(library(pfl)). :- use_module(library(pfl)).
%:- set_solver(lve). :- set_solver(hve).
%:- set_solver(hve). %:- set_solver(ve).
%:- set_solver(jt).
%:- set_solver(bdd).
%:- set_solver(bp). %:- set_solver(bp).
%:- set_solver(cbp). %:- set_solver(cbp).
%:- set_solver(gibbs).
:- yap_flag(write_strings, off). %:- set_solver(lve).
%:- set_solver(lkc).
%:- set_solver(lbp).
:- multifile people/1. :- multifile people/1.
@ -16,16 +20,19 @@ people(X,Y) :-
people(Y). people(Y).
% X \== Y. % X \== Y.
markov smokes(X)::[t,f]; [1.0, 4.0552]; [people(X)]. markov smokes(X); [1.0, 4.0552]; [people(X)].
markov asthma(X)::[t,f]; [1.0, 9.9742] ; [people(X)]. markov asthma(X); [1.0, 9.9742] ; [people(X)].
markov friends(X,Y)::[t,f]; [1.0, 99.48432] ; [people(X,Y)]. markov friends(X,Y); [1.0, 99.48432] ; [people(X,Y)].
markov asthma(X)::[t,f], smokes(X)::[t,f]; [4.48169, 4.48169, 1.0, 4.48169] ; [people(X)]. markov asthma(X), smokes(X);
[4.48169, 4.48169, 1.0, 4.48169] ;
[people(X)].
markov asthma(X)::[t,f], friends(X,Y)::[t,f], smokes(Y)::[t,f]; markov asthma(X), friends(X,Y), smokes(Y);
[3.004166, 3.004166, 3.004166, 3.004166, 3.004166, 1.0, 1.0, 3.004166] ; [people(X,Y)]. [3.004166, 3.004166, 3.004166, 3.004166, 3.004166, 1.0, 1.0, 3.004166] ;
[people(X,Y)].
% ?- smokes(p1,t), smokes(p2,t), friends(p1,p2,X) % ?- smokes(p1,t), smokes(p2,t), friends(p1,p2,X)

View File

@ -1,12 +1,15 @@
:- style_check(all).
:- ensure_loaded(library(pfl)). :- ensure_loaded(library(pfl)).
% 1. define domain of random variables :- set_solver(hve).
% not necessary if they are boolean. %:- set_solver(ve).
%:- set_solver(jt).
% 2. define parfactors %:- set_solver(bdd).
%:- set_solver(bp).
%:- set_solver(cbp).
%:- set_solver(gibbs).
%:- set_solver(lve).
%:- set_solver(lkc).
%:- set_solver(lbp).
bayes cloudy ; cloudy_table ; []. bayes cloudy ; cloudy_table ; [].
@ -16,18 +19,21 @@ bayes rain, cloudy ; rain_table ; [].
bayes wet_grass, sprinkler, rain ; wet_grass_table ; []. bayes wet_grass, sprinkler, rain ; wet_grass_table ; [].
cloudy_table(
[ 0.5,
0.5 ]).
% 3. define CPTs. sprinkler_table(
[ 0.5, 0.9,
0.5, 0.1 ]).
wet_grass_table([1.0,0.1,0.1,0.01, rain_table(
0.0,0.9,0.9,0.99]). [ 0.8, 0.2,
0.2, 0.8 ]).
sprinkler_table([0.5,0.9, wet_grass_table(
0.5,0.1]). [ 1.0, 0.1, 0.1, 0.01,
0.0, 0.9, 0.9, 0.99 ]).
rain_table([0.8,0.2,
0.2,0.8]).
cloudy_table([0.5,0.5]).
% ?- wet_grass(X).

View File

@ -1,29 +1,33 @@
:- use_module(library(pfl)). :- use_module(library(pfl)).
%:- set_solver(lve). :- set_solver(hve).
%:- set_solver(hve). %:- set_solver(ve).
%:- set_solver(jt).
%:- set_solver(bdd).
%:- set_solver(bp). %:- set_solver(bp).
%:- set_solver(cbp). %:- set_solver(cbp).
%:- set_solver(gibbs).
:- yap_flag(write_strings, off). %:- set_solver(lve).
%:- set_solver(lkc).
%:- set_solver(lbp).
:- multifile people/1. :- multifile people/1.
people @ 5. people @ 5.
markov attends(P)::[t,f], attr1::[t,f] ; [0.7, 0.3, 0.3, 0.3] ; [people(P)]. markov attends(P), attr1 ; [0.7, 0.3, 0.3, 0.3] ; [people(P)].
markov attends(P)::[t,f], attr2::[t,f] ; [0.7, 0.3, 0.3, 0.3] ; [people(P)]. markov attends(P), attr2 ; [0.7, 0.3, 0.3, 0.3] ; [people(P)].
markov attends(P)::[t,f], attr3::[t,f] ; [0.7, 0.3, 0.3, 0.3] ; [people(P)]. markov attends(P), attr3 ; [0.7, 0.3, 0.3, 0.3] ; [people(P)].
markov attends(P)::[t,f], attr4::[t,f] ; [0.7, 0.3, 0.3, 0.3] ; [people(P)]. markov attends(P), attr4 ; [0.7, 0.3, 0.3, 0.3] ; [people(P)].
markov attends(P)::[t,f], attr5::[t,f] ; [0.7, 0.3, 0.3, 0.3] ; [people(P)]. markov attends(P), attr5 ; [0.7, 0.3, 0.3, 0.3] ; [people(P)].
markov attends(P)::[t,f], attr6::[t,f] ; [0.7, 0.3, 0.3, 0.3] ; [people(P)]. markov attends(P), attr6 ; [0.7, 0.3, 0.3, 0.3] ; [people(P)].
markov attends(P)::[t,f], series::[t,f] ; [0.501, 0.499, 0.499, 0.499] ; [people(P)]. markov attends(P), series ; [0.501, 0.499, 0.499, 0.499] ; [people(P)].
% ?- series(X). % ?- series(X).