From 65e0c3b2a2696013e02482eb96349dbee273304f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Tue, 3 Apr 2012 16:22:40 +0100 Subject: [PATCH] more PFL changes. --- packages/CLPBN/clpbn.yap | 23 ++++++++------ packages/CLPBN/clpbn/bp.yap | 42 +++++++++++++++++++++++++ packages/CLPBN/clpbn/fove.yap | 14 ++------- packages/CLPBN/clpbn/ground_factors.yap | 28 ++++++++++------- packages/CLPBN/clpbn/horus.yap | 2 +- 5 files changed, 76 insertions(+), 33 deletions(-) diff --git a/packages/CLPBN/clpbn.yap b/packages/CLPBN/clpbn.yap index 79185877d..8f330aed0 100644 --- a/packages/CLPBN/clpbn.yap +++ b/packages/CLPBN/clpbn.yap @@ -44,6 +44,7 @@ check_if_bp_done/1, init_bp_solver/4, run_bp_solver/3, + call_bp_ground/5, finalize_bp_solver/1 ]). @@ -118,7 +119,7 @@ [clpbn2gviz/4]). :- use_module(clpbn/ground_factors, - [generate_network/4]). + [generate_network/5]). :- dynamic solver/1,output/1,use/1,suppress_attribute_display/1, parameter_softening/1, em_solver/1, use_parfactors/1. @@ -230,6 +231,13 @@ clpbn_marginalise(V, Dist) :- % called by top-level % or by call_residue/2 % +project_attributes(GVars, _AVars0) :- + use_parfactors(on), + clpbn_flag(solver, Solver), Solver \= fove, !, + generate_network(GVars, GKeys, Keys, Factors, Evidence), + solver(Solver), + call_ground_solver(Solver, GKeys, Keys, Factors, Evidence, Answ), + writeln(Answ). project_attributes(GVars, AVars0) :- suppress_attribute_display(false), generate_vars(GVars, AVars0, AVars), @@ -250,14 +258,6 @@ project_attributes(GVars, AVars0) :- ). project_attributes(_, _). -generate_vars(GVars, _, _NewAVars) :- - use_parfactors(on), - clpbn_flag(solver, Solver), Solver \= fove, !, - generate_network(GVars, Keys, Factors, Evidence), - writeln(network(GVars, Keys, Factors, Evidence)), - halt. -generate_vars(_GVars, AVars, AVars). - clpbn_vars(AVars, DiffVars, AllVars) :- sort_vars_by_key(AVars,SortedAVars,DiffVars), incorporate_evidence(SortedAVars, AllVars). @@ -310,6 +310,11 @@ write_out(bnt, GVars, AVars, DiffVars) :- write_out(fove, GVars, AVars, DiffVars) :- fove(GVars, AVars, DiffVars). +% call a solver with keys, not actual variables +call_ground_solver(bp, GoalKeys, Keys, Factors, Evidence, Answ) :- + call_bp_ground(GoalKeys, Keys, Factors, Evidence, Answ). + + get_bnode(Var, Goal) :- get_atts(Var, [key(Key),dist(Dist,Parents)]), get_dist(Dist,_,Domain,CPT), diff --git a/packages/CLPBN/clpbn/bp.yap b/packages/CLPBN/clpbn/bp.yap index ece3c95e6..ba7908c0c 100644 --- a/packages/CLPBN/clpbn/bp.yap +++ b/packages/CLPBN/clpbn/bp.yap @@ -10,6 +10,7 @@ check_if_bp_done/1, init_bp_solver/4, run_bp_solver/3, + call_bp_ground/5, finalize_bp_solver/1 ]). @@ -32,6 +33,8 @@ :- use_module(library(clpbn/horus)). +:- use_module(library(lists)). + :- use_module(library(atts)). :- attribute id/1. @@ -52,6 +55,8 @@ :- use_module(library(charsio), [term_to_atom/2]). +:- use_module(library(bhash)). + :- use_module(horus, [create_ground_network/2, @@ -64,6 +69,43 @@ :- attribute id/1. +call_bp_ground(QueryKeys, AllKeys, Factors, Evidence, Solutions) :- + b_hash_new(Hash0), + keys_to_ids(AllKeys, 0, Hash0, Hash), + InvMap =.. [view|AllKeys], + list_of_keys_to_ids(QueryKeys, Hash, QueryVarsIds), + evidence_to_ids(Evidence, Hash, EvIds, EvIdNames), + factors_to_ids(Factors, Hash, FactorIds), + set_graphical_model(FactorIds, Network, InvMap, EvIdNames), + run_ground_solver(Network, QueryVarsIds, EvIds, Solutions), + free_bayesian_network(Network). + +keys_to_ids([], _, Hash, Hash). +keys_to_ids([Key|AllKeys], I0, Hash0, Hash) :- + b_hash_insert(Hash0, Key, I0, HashI), + I is I0+1, + keys_to_ids(AllKeys, I, HashI, Hash). + +list_of_keys_to_ids([], _, []). +list_of_keys_to_ids([Key|QueryKeys], Hash, [Id|QueryIds]) :- + b_hash_lookup(Key, Id, Hash), + list_of_keys_to_ids(QueryKeys, Hash, QueryIds). + +evidence_to_ids([], _, [], []). +evidence_to_ids([Key=V|QueryKeys], Hash, [Id=V|QueryIds], [Id=Name|QueryNames]) :- + b_hash_lookup(Key, Id, Hash), + pfl:skolem(Key,Dom), + nth0(V, Dom, Name), + evidence_to_ids(QueryKeys, Hash, QueryIds, QueryNames). + +factors_to_ids([], _, []). +factors_to_ids([f(markov, Keys, CPT)|Fs], Hash, [markov(Ids, CPT)|NFs]) :- + list_of_keys_to_ids(Keys, Hash, Ids), + factors_to_ids(Fs, Hash, NFs). +factors_to_ids([f(bayes, Keys, CPT)|Fs], Hash, [bayes(Ids, CPT)|NFs]) :- + list_of_keys_to_ids(Keys, Hash, Ids), + factors_to_ids(Fs, Hash, NFs). + bp([[]],_,_) :- !. bp([QueryVars], AllVars, Output) :- diff --git a/packages/CLPBN/clpbn/fove.yap b/packages/CLPBN/clpbn/fove.yap index 986b35087..967daae61 100644 --- a/packages/CLPBN/clpbn/fove.yap +++ b/packages/CLPBN/clpbn/fove.yap @@ -23,7 +23,7 @@ :- use_module(library(pfl), - [factor/5, + [factor/6, skolem/2, get_pfl_parameters/2 ]). @@ -72,19 +72,11 @@ get_parfactors(Factors) :- is_factor(pf(Id, Ks, Rs, Phi, Tuples)) :- -<<<<<<< HEAD - factor(_Type, Id, Ks, Vs, Table, Constraints), - get_ranges(Ks,Rs), - Table \= avg, - gen_table(Table, Phi), - all_tuples(Constraints, Vs, Tuples). -======= - factor(Id, Ks, Vs, Table, Constraints), + factor(_Type, Id, Ks, Vs, Table, Constraints), get_ranges(Ks,Rs), Table \= avg, gen_table(Table, Phi), all_tuples(Constraints, Vs, Tuples). ->>>>>>> 911b241ad663a911af52babcf5d702c5239b4350 get_ranges([],[]). @@ -124,7 +116,7 @@ get_observed_vars(V.AllAttVars, [K:E|ObservedVars]) :- ( clpbn:get_atts(V,[evidence(E)]) ; pfl:evidence(K,E) ), !, get_observed_vars(AllAttVars, ObservedVars). get_observed_vars(V.AllAttVars, ObservedVars) :- - clpbn:get_atts(V,[key(K)]), !, + clpbn:get_atts(V,[key(_K)]), !, get_observed_vars(AllAttVars, ObservedVars). diff --git a/packages/CLPBN/clpbn/ground_factors.yap b/packages/CLPBN/clpbn/ground_factors.yap index a84aa9e0f..0e3d93ce6 100644 --- a/packages/CLPBN/clpbn/ground_factors.yap +++ b/packages/CLPBN/clpbn/ground_factors.yap @@ -8,7 +8,7 @@ :- module(clpbn_ground_factors, [ - generate_network/4]). + generate_network/5]). :- use_module(library(bhash), [ b_hash_new/1, @@ -29,25 +29,29 @@ :- dynamic currently_defined/1, f/3. -generate_network(QueryVars, Keys, Factors, Evidence) :- +generate_network(QueryVars, QueryKeys, Keys, Factors, Evidence) :- attributes:all_attvars(AVars), - check_for_evidence(AVars, EVars), + check_for_evidence(AVars, EVars, Evidence), retractall(currently_defined(_)), retractall(f(_,_,_)), initialize_evidence(EVars), - findall(K, currently_defined(K), Evidence), + keys(QueryVars, QueryKeys), run_through_factors(QueryVars), run_through_factors(EVars), findall(K, currently_defined(K), Keys), - findall(f(FType,FKeys,FCPT), f(FType,FKeys,FCPT), Factors), - listing(f). + findall(f(FType,FKeys,FCPT), f(FType,FKeys,FCPT), Factors). -check_for_evidence(V.AVars, V.EVars) :- - clpbn:get_atts(V,[evidence(_E)]), !, - check_for_evidence(AVars, EVars). -check_for_evidence(_V.AVars, EVars) :- - check_for_evidence(AVars, EVars). -check_for_evidence([], []). +check_for_evidence(V.AVars, V.EVars, (K=E).Evidence) :- + clpbn:get_atts(V,[key(K),evidence(E)]), !, + check_for_evidence(AVars, EVars, Evidence). +check_for_evidence(_V.AVars, EVars, Evidence) :- + check_for_evidence(AVars, EVars, Evidence). +check_for_evidence([], [], []). + +keys([], []). +keys([Var|QueryVars], [Key|QueryKeys]) :- + clpbn:get_atts(Var,[key(Key)]), + keys(QueryVars, QueryKeys). run_through_factors([]). run_through_factors([Var|_QueryVars]) :- diff --git a/packages/CLPBN/clpbn/horus.yap b/packages/CLPBN/clpbn/horus.yap index 24f492fe2..d8daca6c9 100644 --- a/packages/CLPBN/clpbn/horus.yap +++ b/packages/CLPBN/clpbn/horus.yap @@ -25,7 +25,7 @@ patch_things_up :- warning :- format(user_error,"Horus library not installed: cannot use bp, fove~n.",[]). -:- catch(load_foreign_files(['horus'], [], init_predicates), _, patch_things_up) -> true ; warning. +:- catch(load_foreign_files([horus], [], init_predicates), _, patch_things_up) -> true ; warning.