more PFL changes.

This commit is contained in:
Vítor Santos Costa 2012-04-03 16:22:40 +01:00
parent 6ccd458ea5
commit 65e0c3b2a2
5 changed files with 76 additions and 33 deletions

View File

@ -44,6 +44,7 @@
check_if_bp_done/1, check_if_bp_done/1,
init_bp_solver/4, init_bp_solver/4,
run_bp_solver/3, run_bp_solver/3,
call_bp_ground/5,
finalize_bp_solver/1 finalize_bp_solver/1
]). ]).
@ -118,7 +119,7 @@
[clpbn2gviz/4]). [clpbn2gviz/4]).
:- use_module(clpbn/ground_factors, :- 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. :- 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 % called by top-level
% or by call_residue/2 % 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) :- project_attributes(GVars, AVars0) :-
suppress_attribute_display(false), suppress_attribute_display(false),
generate_vars(GVars, AVars0, AVars), generate_vars(GVars, AVars0, AVars),
@ -250,14 +258,6 @@ project_attributes(GVars, AVars0) :-
). ).
project_attributes(_, _). 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) :- clpbn_vars(AVars, DiffVars, AllVars) :-
sort_vars_by_key(AVars,SortedAVars,DiffVars), sort_vars_by_key(AVars,SortedAVars,DiffVars),
incorporate_evidence(SortedAVars, AllVars). incorporate_evidence(SortedAVars, AllVars).
@ -310,6 +310,11 @@ write_out(bnt, GVars, AVars, DiffVars) :-
write_out(fove, GVars, AVars, DiffVars) :- write_out(fove, GVars, AVars, DiffVars) :-
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_bnode(Var, Goal) :-
get_atts(Var, [key(Key),dist(Dist,Parents)]), get_atts(Var, [key(Key),dist(Dist,Parents)]),
get_dist(Dist,_,Domain,CPT), get_dist(Dist,_,Domain,CPT),

View File

@ -10,6 +10,7 @@
check_if_bp_done/1, check_if_bp_done/1,
init_bp_solver/4, init_bp_solver/4,
run_bp_solver/3, run_bp_solver/3,
call_bp_ground/5,
finalize_bp_solver/1 finalize_bp_solver/1
]). ]).
@ -32,6 +33,8 @@
:- use_module(library(clpbn/horus)). :- use_module(library(clpbn/horus)).
:- use_module(library(lists)).
:- use_module(library(atts)). :- use_module(library(atts)).
:- attribute id/1. :- attribute id/1.
@ -52,6 +55,8 @@
:- use_module(library(charsio), :- use_module(library(charsio),
[term_to_atom/2]). [term_to_atom/2]).
:- use_module(library(bhash)).
:- use_module(horus, :- use_module(horus,
[create_ground_network/2, [create_ground_network/2,
@ -64,6 +69,43 @@
:- attribute id/1. :- 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([[]],_,_) :- !.
bp([QueryVars], AllVars, Output) :- bp([QueryVars], AllVars, Output) :-

View File

@ -23,7 +23,7 @@
:- use_module(library(pfl), :- use_module(library(pfl),
[factor/5, [factor/6,
skolem/2, skolem/2,
get_pfl_parameters/2 get_pfl_parameters/2
]). ]).
@ -72,19 +72,11 @@ get_parfactors(Factors) :-
is_factor(pf(Id, Ks, Rs, Phi, Tuples)) :- is_factor(pf(Id, Ks, Rs, Phi, Tuples)) :-
<<<<<<< HEAD factor(_Type, 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).
=======
factor(Id, Ks, Vs, Table, Constraints),
get_ranges(Ks,Rs), get_ranges(Ks,Rs),
Table \= avg, Table \= avg,
gen_table(Table, Phi), gen_table(Table, Phi),
all_tuples(Constraints, Vs, Tuples). all_tuples(Constraints, Vs, Tuples).
>>>>>>> 911b241ad663a911af52babcf5d702c5239b4350
get_ranges([],[]). get_ranges([],[]).
@ -124,7 +116,7 @@ get_observed_vars(V.AllAttVars, [K:E|ObservedVars]) :-
( clpbn:get_atts(V,[evidence(E)]) ; pfl:evidence(K,E) ), !, ( clpbn:get_atts(V,[evidence(E)]) ; pfl:evidence(K,E) ), !,
get_observed_vars(AllAttVars, ObservedVars). get_observed_vars(AllAttVars, ObservedVars).
get_observed_vars(V.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). get_observed_vars(AllAttVars, ObservedVars).

View File

@ -8,7 +8,7 @@
:- module(clpbn_ground_factors, [ :- module(clpbn_ground_factors, [
generate_network/4]). generate_network/5]).
:- use_module(library(bhash), [ :- use_module(library(bhash), [
b_hash_new/1, b_hash_new/1,
@ -29,25 +29,29 @@
:- dynamic currently_defined/1, f/3. :- dynamic currently_defined/1, f/3.
generate_network(QueryVars, Keys, Factors, Evidence) :- generate_network(QueryVars, QueryKeys, Keys, Factors, Evidence) :-
attributes:all_attvars(AVars), attributes:all_attvars(AVars),
check_for_evidence(AVars, EVars), check_for_evidence(AVars, EVars, Evidence),
retractall(currently_defined(_)), retractall(currently_defined(_)),
retractall(f(_,_,_)), retractall(f(_,_,_)),
initialize_evidence(EVars), initialize_evidence(EVars),
findall(K, currently_defined(K), Evidence), keys(QueryVars, QueryKeys),
run_through_factors(QueryVars), run_through_factors(QueryVars),
run_through_factors(EVars), run_through_factors(EVars),
findall(K, currently_defined(K), Keys), findall(K, currently_defined(K), Keys),
findall(f(FType,FKeys,FCPT), f(FType,FKeys,FCPT), Factors), findall(f(FType,FKeys,FCPT), f(FType,FKeys,FCPT), Factors).
listing(f).
check_for_evidence(V.AVars, V.EVars) :- check_for_evidence(V.AVars, V.EVars, (K=E).Evidence) :-
clpbn:get_atts(V,[evidence(_E)]), !, clpbn:get_atts(V,[key(K),evidence(E)]), !,
check_for_evidence(AVars, EVars). check_for_evidence(AVars, EVars, Evidence).
check_for_evidence(_V.AVars, EVars) :- check_for_evidence(_V.AVars, EVars, Evidence) :-
check_for_evidence(AVars, EVars). check_for_evidence(AVars, EVars, Evidence).
check_for_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([]).
run_through_factors([Var|_QueryVars]) :- run_through_factors([Var|_QueryVars]) :-

View File

@ -25,7 +25,7 @@ patch_things_up :-
warning :- warning :-
format(user_error,"Horus library not installed: cannot use bp, fove~n.",[]). 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.