Rework the learning examples

This commit is contained in:
Tiago Gomes 2012-12-12 15:16:30 +00:00
parent f128b6de7a
commit 108e310a0f
8 changed files with 127 additions and 137 deletions

View File

@ -6,17 +6,21 @@ bayes abi(K)::[h,m,l] ; abi_table ; [professor(K)].
bayes pop(K)::[h,m,l], abi(K) ; pop_table ; [professor(K)]. bayes pop(K)::[h,m,l], abi(K) ; pop_table ; [professor(K)].
bayes diff(C) :: [h,m,l] ; diff_table ; [course(C,_)]. bayes diff(C)::[h,m,l] ; diff_table ; [course(C,_)].
bayes int(S) :: [h,m,l] ; int_table ; [student(S)]. bayes int(S)::[h,m,l] ; int_table ; [student(S)].
bayes grade(C,S)::[a,b,c,d], int(S), diff(C) ; grade_table ; [registration(_,C,S)]. bayes grade(C,S)::[a,b,c,d], int(S), diff(C) ;
grade_table ;
[registration(_,C,S)].
bayes satisfaction(C,S)::[h,m,l], abi(P), grade(C,S) ; sat_table ; [reg_satisfaction(C,S,P)]. bayes satisfaction(C,S)::[h,m,l], abi(P), grade(C,S) ;
sat_table ;
[reg_satisfaction(C,S,P)].
bayes rat(C) :: [h,m,l], Sats ; avg ; [course_rat(C, Sats)]. bayes rat(C)::[h,m,l], Sats ; avg ; [course_rat(C, Sats)].
bayes rank(S) :: [a,b,c,d], Grades ; avg ; [student_ranking(S,Grades)]. bayes rank(S)::[a,b,c,d], Grades ; avg ; [student_ranking(S,Grades)].
grade(Key, Grade) :- grade(Key, Grade) :-
@ -30,19 +34,27 @@ reg_satisfaction(CKey, SKey, PKey) :-
course_rat(CKey, Sats) :- course_rat(CKey, Sats) :-
course(CKey, _), course(CKey, _),
setof(satisfaction(CKey,SKey), setof(satisfaction(CKey,SKey),
PKey^reg_satisfaction(CKey, SKey, PKey), PKey^reg_satisfaction(CKey, SKey, PKey),
Sats). Sats).
student_ranking(SKey, Grades) :- student_ranking(SKey, Grades) :-
student(SKey), student(SKey),
setof(grade(CKey,SKey), RKey^registration(RKey,CKey,SKey), Grades). setof(grade(CKey,SKey),
RKey^registration(RKey,CKey,SKey),
Grades).
:- ensure_loaded(tables). :- ensure_loaded(tables).
% convert to longer names % convert to longer names
professor_ability(P,A) :- abi(P, A). professor_ability(P,A) :- abi(P,A).
professor_popularity(P,A) :- pop(P, A). professor_popularity(P,A) :- pop(P,A).
course_difficulty(P,A) :- diff(P,A).
student_intelligence(P,A) :- int(P,A).
course_rating(C,X) :- rat(C,X).
registration_grade(R,A) :- registration_grade(R,A) :-
registration(R,C,S), registration(R,C,S),
@ -52,24 +64,14 @@ registration_satisfaction(R,A) :-
registration(R,C,S), registration(R,C,S),
satisfaction(C,S,A). satisfaction(C,S,A).
student_intelligence(P,A) :- int(P, A). registration_course(R,C) :- registration(R,C,_).
course_difficulty(P,A) :- diff(P, A). registration_student(R,S) :- registration(R,_,S).
registration_course(R,C) :-
registration(R, C, _).
registration_student(R,S) :-
registration(R, _, S).
course_rating(C,X) :- rat(C,X).
% %
% evidence % Evidence
% %
%abi(p0, h). %abi(p0, h).
%pop(p1, m). %pop(p1, m).
%pop(p2, h). %pop(p2, h).

View File

@ -1,5 +1,4 @@
/* Base file for school database. Supposed to be called from school_*.yap */
/* base file for school database. Supposed to be called from school_*.yap */
professor_key(Key) :- professor_key(Key) :-
professor(Key). professor(Key).
@ -67,5 +66,3 @@ student_ranking(Key, Rank) :-
:- ensure_loaded(tables). :- ensure_loaded(tables).

View File

@ -4702,5 +4702,3 @@ registration(r3457,c5,s1023).
registration(r3458,c37,s1023). registration(r3458,c37,s1023).
registration(r3459,c57,s1023). registration(r3459,c57,s1023).

View File

@ -1,30 +1,49 @@
int_table(_,T ,[h, m, l]) :- int_table(T). abi_table(
/* h */ [ 0.50,
/* m */ 0.40,
/* l */ 0.10 ]).
int_table([0.5, abi_table(_, T) :- abi_table(T).
0.4,
0.1]).
/* h h h m h l m h m m m l l h l m l l */ pop_table(
grade_table([ /* h m l */
0.2, 0.7, 0.85, 0.1, 0.2, 0.5, 0.01, 0.05,0.1 , /* h */ [ 0.9, 0.2, 0.01,
0.6, 0.25, 0.12, 0.3, 0.6,0.35,0.04, 0.15, 0.4 , /* m */ 0.09, 0.6, 0.09,
0.15,0.04, 0.02, 0.4,0.15,0.12, 0.5, 0.6, 0.4, /* l */ 0.01, 0.2, 0.9 ]).
0.05,0.01, 0.01, 0.2,0.05,0.03, 0.45, 0.2, 0.1 ]).
pop_table(_, T) :- pop_table(T).
diff_table(
/* h */ [ 0.25,
/* m */ 0.50,
/* l */ 0.25 ]).
dif_table(_, T) :- diff_table(T).
int_table(
/* h */ [ 0.5,
/* m */ 0.4,
/* l */ 0.1 ]).
int_table(_,T ,[h,m,l]) :- int_table(T).
grade_table(
/* h h h m h l m h m m m l l h l m l l */
/* a */ [ 0.2, 0.7, 0.85, 0.1, 0.2, 0.5, 0.01, 0.05, 0.1,
/* b */ 0.6, 0.25, 0.12, 0.3, 0.6, 0.35, 0.04, 0.15, 0.4,
/* c */ 0.15, 0.04, 0.02, 0.4, 0.15, 0.12, 0.5, 0.6, 0.4,
/* d */ 0.05, 0.01, 0.01, 0.2, 0.05, 0.03, 0.45, 0.2, 0.1 ]).
grade_table(I, D, grade_table(I, D,
/* h h h m h l m h m m m l l h l m l l */ p([a,b,c,d], T, [I,D])) :- grade_table(T).
p([a,b,c,d], T, [I,D])) :- grade_table(T).
sat_table( sat_table(
/* h a h b h c h d m a m b m c m d l a l b l c l d */ /* h a h b h c h d m a m b m c m d l a l b l c l d */
/*h*/ [0.98, 0.9, 0.8 , 0.6, 0.9, 0.4, 0.2, 0.01, 0.5, 0.2, 0.01, 0.01, /* h */ [ 0.98, 0.9, 0.8 , 0.6, 0.9, 0.4, 0.2, 0.01, 0.5, 0.2, 0.01, 0.01,
/*m*/ 0.01, 0.09,0.15, 0.3, 0.05, 0.4, 0.3, 0.04, 0.35, 0.3, 0.09, 0.01 , /* m */ 0.01, 0.09, 0.15, 0.3, 0.05, 0.4, 0.3, 0.04, 0.35, 0.3, 0.09, 0.01,
/*l*/ 0.01, 0.01,0.05, 0.1, 0.05, 0.2, 0.5, 0.95, 0.15, 0.5, 0.9, 0.98]). /* l */ 0.01, 0.01, 0.05, 0.1, 0.05, 0.2, 0.5, 0.95, 0.15, 0.5, 0.9, 0.98 ]).
/*
A: professor's ability;
B: student's grade (for course registration).
*/
satisfaction_table(A, G, p([h,m,l], T, [A,G])) :- sat_table(T). satisfaction_table(A, G, p([h,m,l], T, [A,G])) :- sat_table(T).
@ -35,22 +54,8 @@ satisfaction_table(A, G, p([h,m,l], T, [A,G])) :- sat_table(T).
% %
% add all and divide on the number of elements on the table! % add all and divide on the number of elements on the table!
% %
rating_prob_table([0.9,0.05,0.01, rating_prob_table(
0.09,0.9,0.09, [ 0.9, 0.05, 0.01,
0.01,0.05,0.9]). 0.09, 0.9, 0.09,
0.01, 0.05, 0.9 ]).
abi_table( [0.50, 0.40, 0.10]).
abi_table( _, T) :- abi_table(T).
pop_table( [0.9, 0.2, 0.01,
0.09, 0.6, 0.09,
0.01, 0.2, 0.9]).
pop_table(_, T) :- pop_table(T).
diff_table([0.25, 0.50, 0.25]).
dif_table(_, T) :- diff_table(T).

View File

@ -1,21 +1,31 @@
% learn distribution for school database. /* Learn distribution for professor database. */
:- use_module(library(pfl)). :- use_module(library(pfl)).
:- use_module(library(clpbn/learning/em)). :- use_module(library(clpbn/learning/em)).
%:- clpbn:set_clpbn_flag(em_solver,gibbs).
%:- clpbn:set_clpbn_flag(em_solver,jt).
%:- clpbn:set_clpbn_flag(em_solver,hve).
:- clpbn:set_clpbn_flag(em_solver,ve).
%:- clpbn:set_clpbn_flag(em_solver,bp).
%:- clpbn:set_clpbn_flag(em_solver,bdd).
bayes abi(K)::[h,m,l] ; abi_table ; [professor(K)]. bayes abi(K)::[h,m,l] ; abi_table ; [professor(K)].
bayes pop(K)::[h,m,l], abi(K) ; pop_table ; [professor(K)]. bayes pop(K)::[h,m,l], abi(K) ; pop_table ; [professor(K)].
abi_table([0.3,0.3,0.4]). abi_table([0.3, 0.3, 0.4]).
pop_table([0.3,0.3,0.4,0.3,0.3,0.4,0.3,0.3,0.4]). pop_table([0.3, 0.3, 0.4, 0.3, 0.3, 0.4, 0.3, 0.3, 0.4]).
goal_list([/*abi(p0,h), goal_list([
/*
abi(p0,h),
abi(p1,m), abi(p1,m),
abi(p2,m), abi(p2,m),
abi(p3,m),*/ abi(p3,m),
*/
abi(p4,l), abi(p4,l),
pop(p5,h), pop(p5,h),
abi(p5,_), abi(p5,_),
@ -32,13 +42,6 @@ professor(p6).
professor(p7). professor(p7).
professor(p8). professor(p8).
%:- clpbn:set_clpbn_flag(em_solver,gibbs).
%:- clpbn:set_clpbn_flag(em_solver,jt).
:- clpbn:set_clpbn_flag(em_solver,hve).
:- clpbn:set_clpbn_flag(em_solver,ve).
%:- clpbn:set_clpbn_flag(em_solver,bp).
%:- clpbn:set_clpbn_flag(em_solver,bdd).
timed_main :- timed_main :-
statistics(runtime, _), statistics(runtime, _),
main(Lik), main(Lik),
@ -47,13 +50,5 @@ timed_main :-
main(Lik) :- main(Lik) :-
goal_list(L), goal_list(L),
% run_queries(L), em(L,0.01,10,_,Lik).
em(L,0.01,10,_,Lik).
run_queries([]).
run_queries(Q.L) :-
call(Q),
run_queries(L).

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,16 @@
% learn distribution for school database. /* Learn distribution for a sprinkler database. */
:- ['../sprinkler.pfl']. :- ['../sprinkler.pfl'].
:- use_module(library(clpbn/learning/em)). :- use_module(library(clpbn/learning/em)).
%:- clpbn:set_clpbn_flag(em_solver,gibbs).
%:- clpbn:set_clpbn_flag(em_solver,jt).
%:- clpbn:set_clpbn_flag(em_solver,hve).
:- clpbn:set_clpbn_flag(em_solver,bdd).
%:- clpbn:set_clpbn_flag(em_solver,bp).
%:- clpbn:set_clpbn_flag(em_solver,ve).
data(t,t,t,t). data(t,t,t,t).
data(_,t,_,t). data(_,t,_,t).
data(t,t,f,f). data(t,t,f,f).
@ -18,12 +25,7 @@ data(t,t,_,f).
data(t,f,f,t). data(t,f,f,t).
data(t,f,t,t). data(t,f,t,t).
%:- clpbn:set_clpbn_flag(em_solver,gibbs). :- dynamic id/1.
%:- clpbn:set_clpbn_flag(em_solver,jt).
:- clpbn:set_clpbn_flag(em_solver,hve).
:- clpbn:set_clpbn_flag(em_solver,bdd).
%:- clpbn:set_clpbn_flag(em_solver,bp).
%:- clpbn:set_clpbn_flag(em_solver,ve).
timed_main :- timed_main :-
statistics(runtime, _), statistics(runtime, _),
@ -33,19 +35,16 @@ timed_main :-
main(Lik) :- main(Lik) :-
findall(X,scan_data(X),L), findall(X,scan_data(X),L),
em(L,0.01,10,_,Lik). em(L,0.01,10,_,Lik).
scan_data(I:[wet_grass(W),sprinkler(S),rain(R),cloudy(C)]) :- scan_data(I:[wet_grass(W),sprinkler(S),rain(R),cloudy(C)]) :-
data(W, S, R, C), data(W, S, R, C),
new_id(I). new_id(I).
:- dynamic id/1.
new_id(I) :- new_id(I) :-
retract(id(I)), retract(id(I)),
I1 is I+1, I1 is I+1,
assert(id(I1)). assert(id(I1)).
id(0). id(0).

View File

@ -20,17 +20,17 @@ people(X,Y) :-
people(Y). people(Y).
% X \== Y. % X \== Y.
markov smokes(X); [1.0, 4.0552]; [people(X)]. markov smokes(X) ; [1.0, 4.0552]; [people(X)].
markov asthma(X); [1.0, 9.9742] ; [people(X)]. markov asthma(X) ; [1.0, 9.9742] ; [people(X)].
markov friends(X,Y); [1.0, 99.48432] ; [people(X,Y)]. markov friends(X,Y) ; [1.0, 99.48432] ; [people(X,Y)].
markov asthma(X), smokes(X); markov asthma(X), smokes(X) ;
[4.48169, 4.48169, 1.0, 4.48169] ; [4.48169, 4.48169, 1.0, 4.48169] ;
[people(X)]. [people(X)].
markov asthma(X), friends(X,Y), smokes(Y); markov asthma(X), friends(X,Y), smokes(Y) ;
[3.004166, 3.004166, 3.004166, 3.004166, 3.004166, 1.0, 1.0, 3.004166] ; [3.004166, 3.004166, 3.004166, 3.004166, 3.004166, 1.0, 1.0, 3.004166] ;
[people(X,Y)]. [people(X,Y)].