From 71a62307f449b706d60fb4ac09f858092630f81b Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 1 Jun 2012 13:17:39 +0100 Subject: [PATCH] CLP(BN) stuff. --- packages/CLPBN/clpbn.yap | 14 +++++++++----- packages/CLPBN/clpbn/display.yap | 4 ++-- packages/CLPBN/clpbn/dists.yap | 3 +-- packages/CLPBN/clpbn/ground_factors.yap | 19 +++++++++++-------- packages/CLPBN/clpbn/horus.yap | 17 +++++++---------- packages/CLPBN/examples/School/school_32.yap | 10 ++++++++-- packages/CLPBN/pfl.yap | 1 + 7 files changed, 39 insertions(+), 29 deletions(-) diff --git a/packages/CLPBN/clpbn.yap b/packages/CLPBN/clpbn.yap index db017a6d4..9761d08d4 100644 --- a/packages/CLPBN/clpbn.yap +++ b/packages/CLPBN/clpbn.yap @@ -235,11 +235,7 @@ project_attributes(GVars, _AVars0) :- use_parfactors(on), clpbn_flag(solver, Solver), Solver \= fove, !, generate_network(GVars, GKeys, Keys, Factors, Evidence), - (ground(GVars) -> - true - ; - call_ground_solver(Solver, GVars, GKeys, Keys, Factors, Evidence, _Avars0) - ). + call_ground_solver(Solver, GVars, GKeys, Keys, Factors, Evidence, _Avars0). project_attributes(GVars, AVars) :- suppress_attribute_display(false), AVars = [_|_], @@ -259,6 +255,14 @@ project_attributes(GVars, AVars) :- ). project_attributes(_, _). +match([], _Keys). +match([V|GVars], Keys) :- + clpbn:get_atts(V,[key(GKey)]), + member(GKey, Keys), ground(GKey), !, + match(GVars, Keys). +match([_V|GVars], Keys) :- + match(GVars, Keys). + clpbn_vars(AVars, DiffVars, AllVars) :- sort_vars_by_key(AVars,SortedAVars,DiffVars), incorporate_evidence(SortedAVars, AllVars). diff --git a/packages/CLPBN/clpbn/display.yap b/packages/CLPBN/clpbn/display.yap index f37814a5b..0e96a2e7b 100644 --- a/packages/CLPBN/clpbn/display.yap +++ b/packages/CLPBN/clpbn/display.yap @@ -46,11 +46,11 @@ clpbn_bind_vals([Vs|MoreVs],[Ps|MorePs],AllDiffs) :- clpbn_bind_vals2([],_,_) :- !. % simple case, we want a distribution on a single variable. -bind_vals([V],Ps) :- +clpbn_bind_vals2([V],Ps,AllDiffs) :- use_parfactors(on), !, clpbn:get_atts(V, [key(K)]), pfl:skolem(K,Vals), - put_atts(V, posterior([V], Vals, Ps)). + put_atts(V, posterior([V], Vals, Ps, AllDiffs)). % complex case, we want a joint distribution, do it on a leader. % should split on cliques ? clpbn_bind_vals2(Vs,Ps,AllDiffs) :- diff --git a/packages/CLPBN/clpbn/dists.yap b/packages/CLPBN/clpbn/dists.yap index d3d1141e0..830a44c18 100644 --- a/packages/CLPBN/clpbn/dists.yap +++ b/packages/CLPBN/clpbn/dists.yap @@ -245,8 +245,7 @@ get_dist_domain_size(Id, DSize) :- get_dist_domain(Id, Domain) :- recorded(clpbn_dist_db, db(Id, _, _, _, Domain, _, _), _), !. -get_dist_domain(avg(Domain), Domain) :- - recorded(clpbn_dist_db, db(Id, _, _, _, Domain, _, _), _), !. +get_dist_domain(avg(Domain), Domain). get_dist_key(Id, Key) :- use_parfactors(on), !, diff --git a/packages/CLPBN/clpbn/ground_factors.yap b/packages/CLPBN/clpbn/ground_factors.yap index b90037a4a..488f889de 100644 --- a/packages/CLPBN/clpbn/ground_factors.yap +++ b/packages/CLPBN/clpbn/ground_factors.yap @@ -41,11 +41,22 @@ do_network([], _, _, _) :- !. do_network(QueryVars, EVars, Keys, Factors) :- retractall(currently_defined(_)), retractall(f(_,_,_,_)), +writeln(keys:Keys), run_through_factors(QueryVars), run_through_factors(EVars), findall(K, currently_defined(K), Keys), +writeln(keys2:Keys), findall(f(FType,FId,FKeys,FCPT), f(FType,FId,FKeys,FCPT), Factors). +match([], _Keys). +match([V|GVars], Keys) :- + clpbn:get_atts(V,[key(GKey)]), !, + member(GKey, Keys), ground(GKey), + match(GVars, Keys). +match([_V|GVars], Keys) :- + match(GVars, Keys). + + % % look for attributed vars with evidence (should also search the DB) % verifiy if the evidence overlaps with query @@ -85,14 +96,6 @@ keys([Var|QueryVars], [Key|QueryKeys]) :- clpbn:get_atts(Var,[key(Key)]), keys(QueryVars, QueryKeys). -run_through_factors([]). -run_through_factors([Var|_QueryVars]) :- - clpbn:get_atts(Var,[key(K)]), - find_factors(K), - fail. -run_through_factors([_|QueryVars]) :- - run_through_factors(QueryVars). - initialize_evidence([]). initialize_evidence([V|EVars]) :- clpbn:get_atts(V, [key(K)]), diff --git a/packages/CLPBN/clpbn/horus.yap b/packages/CLPBN/clpbn/horus.yap index e88178cf8..a8aa7adbd 100644 --- a/packages/CLPBN/clpbn/horus.yap +++ b/packages/CLPBN/clpbn/horus.yap @@ -20,9 +20,6 @@ ]). -:- use_module(library(pfl), - [set_pfl_flag/2]). - patch_things_up :- assert_static(clpbn_horus:set_horus_flag(_,_)). @@ -33,13 +30,13 @@ warning :- :- catch(load_foreign_files([horus], [], init_predicates), _, patch_things_up) -> true ; warning. -set_solver(ve) :- set_pfl_flag(solver,ve). -set_solver(jt) :- set_pfl_flag(solver,jt). -set_solver(gibbs) :- set_pfl_flag(solver,gibbs). -set_solver(fove) :- set_pfl_flag(solver,fove). -set_solver(hve) :- set_pfl_flag(solver,bp), set_horus_flag(inf_alg, ve). -set_solver(bp) :- set_pfl_flag(solver,bp), set_horus_flag(inf_alg, bp). -set_solver(cbp) :- set_pfl_flag(solver,bp), set_horus_flag(inf_alg, cbp). +set_solver(ve) :- pfl:set_pfl_flag(solver,ve). +set_solver(jt) :- pfl:set_pfl_flag(solver,jt). +set_solver(gibbs) :- pfl:set_pfl_flag(solver,gibbs). +set_solver(fove) :- pfl:set_pfl_flag(solver,fove). +set_solver(hve) :- pfl:set_pfl_flag(solver,bp), set_horus_flag(inf_alg, ve). +set_solver(bp) :- pfl:set_pfl_flag(solver,bp), set_horus_flag(inf_alg, bp). +set_solver(cbp) :- pfl:set_pfl_flag(solver,bp), set_horus_flag(inf_alg, cbp). set_solver(S) :- throw(error('unknow solver ', S)). diff --git a/packages/CLPBN/examples/School/school_32.yap b/packages/CLPBN/examples/School/school_32.yap index 50a61cfef..56980939e 100644 --- a/packages/CLPBN/examples/School/school_32.yap +++ b/packages/CLPBN/examples/School/school_32.yap @@ -1,3 +1,5 @@ + + /* total_professors(32). @@ -11,11 +13,15 @@ total_students(256). :- style_check(all). -:- yap_flag(unknown,error). - :- yap_flag(write_strings,on). :- ensure_loaded(parschema). +:- yap_flag(unknown,error). +%:- clpbn_horus:set_solver(fove). +%:- clpbn_horus:set_solver(hve). +:- clpbn_horus:set_solver(bp). +%:- clpbn_horus:set_solver(cbp). + :- ensure_loaded(school32_data). diff --git a/packages/CLPBN/pfl.yap b/packages/CLPBN/pfl.yap index 8be97aed8..49e4da970 100644 --- a/packages/CLPBN/pfl.yap +++ b/packages/CLPBN/pfl.yap @@ -131,6 +131,7 @@ interface_predicate(Sk) :- append(SKAs, [Var], ESKAs), ESk =.. ESKAs, assert(preprocess(ESk, Sk, Var)), + % transform from PFL to CLP(BN) call assert_static((user:ESk :- evidence(Sk,Ev) -> Ev = Var; var(Var) -> insert_atts(Var,Sk) ;