This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/clpbn/bp.yap

218 lines
5.7 KiB
Plaintext
Raw Normal View History

2012-03-31 23:27:37 +01:00
/*******************************************************
2012-03-31 23:27:37 +01:00
Belief Propagation and Variable Elimination Interface
2012-03-31 23:27:37 +01:00
********************************************************/
:- module(clpbn_bp,
[bp/3,
2012-03-22 11:40:24 +00:00
check_if_bp_done/1,
init_bp_solver/4,
run_bp_solver/3,
2012-04-03 16:22:40 +01:00
call_bp_ground/5,
finalize_bp_solver/1
]).
:- use_module(library('clpbn/dists'),
[dist/4,
get_dist_domain/2,
get_dist_domain_size/2,
get_dist_params/2
]).
:- use_module(library('clpbn/display'),
2012-03-31 23:27:37 +01:00
[clpbn_bind_vals/3]).
2012-03-22 11:40:24 +00:00
2011-12-27 22:07:42 +00:00
:- use_module(library('clpbn/aggregates'),
2012-03-31 23:27:37 +01:00
[check_for_agg_vars/2]).
2012-04-03 15:01:35 +01:00
:- use_module(library(clpbn/horus)).
2012-04-03 16:22:40 +01:00
:- use_module(library(lists)).
2012-03-22 11:40:24 +00:00
:- use_module(library(atts)).
:- attribute id/1.
2012-03-22 11:40:24 +00:00
%:- set_horus_flag(inf_alg, ve).
:- set_horus_flag(inf_alg, bn_bp).
%:- set_horus_flag(inf_alg, fg_bp).
%: -set_horus_flag(inf_alg, cbp).
2011-12-10 22:58:43 +00:00
2012-03-22 11:40:24 +00:00
:- set_horus_flag(schedule, seq_fixed).
%:- set_horus_flag(schedule, seq_random).
%:- set_horus_flag(schedule, parallel).
%:- set_horus_flag(schedule, max_residual).
2011-12-10 22:58:43 +00:00
2012-03-22 11:40:24 +00:00
:- set_horus_flag(accuracy, 0.0001).
2011-12-10 22:58:43 +00:00
2012-03-31 23:27:37 +01:00
:- use_module(library(charsio),
[term_to_atom/2]).
2011-12-10 22:58:43 +00:00
2012-04-03 16:22:40 +01:00
:- use_module(library(bhash)).
2011-12-10 22:58:43 +00:00
2012-03-31 23:27:37 +01:00
:- use_module(horus,
[create_ground_network/2,
set_bayes_net_params/2,
run_ground_solver/3,
set_extra_vars_info/2,
free_bayesian_network/1
]).
2011-12-10 22:58:43 +00:00
2012-03-31 23:27:37 +01:00
:- attribute id/1.
2012-04-03 16:22:40 +01:00
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),
2012-04-03 17:12:58 +01:00
init_graphical_model(FactorIds, Network, InvMap, EvIdNames),
2012-04-03 16:22:40 +01:00
run_ground_solver(Network, QueryVarsIds, EvIds, Solutions),
2012-04-03 17:12:58 +01:00
free_graphical_model(Network).
2012-04-03 16:22:40 +01:00
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) :-
2012-03-31 23:27:37 +01:00
init_bp_solver(_, AllVars, _, Network),
run_bp_solver([QueryVars], LPs, Network),
finalize_bp_solver(Network),
clpbn_bind_vals([QueryVars], LPs, Output).
2012-03-22 11:40:24 +00:00
init_bp_solver(_, AllVars0, _, bp(BayesNet, DistIds)) :-
2012-03-31 23:27:37 +01:00
%writeln('init_bp_solver'),
check_for_agg_vars(AllVars0, AllVars),
%writeln('clpbn_vars:'), print_clpbn_vars(AllVars),
assign_ids(AllVars, 0),
get_vars_info(AllVars, VarsInfo, DistIds0),
sort(DistIds0, DistIds),
create_ground_network(VarsInfo, BayesNet),
%get_extra_vars_info(AllVars, ExtraVarsInfo),
%set_extra_vars_info(BayesNet, ExtraVarsInfo),
%writeln(extravarsinfo:ExtraVarsInfo),
true.
2011-12-27 22:07:42 +00:00
2012-03-22 11:40:24 +00:00
run_bp_solver(QueryVars, Solutions, bp(Network, DistIds)) :-
2012-03-31 23:27:37 +01:00
%writeln('-> run_bp_solver'),
get_dists_parameters(DistIds, DistsParams),
2012-03-22 11:40:24 +00:00
set_bayes_net_params(Network, DistsParams),
2012-03-31 23:27:37 +01:00
vars_to_ids(QueryVars, QueryVarsIds),
run_ground_solver(Network, QueryVarsIds, Solutions).
2012-03-22 11:40:24 +00:00
2011-12-27 22:07:42 +00:00
2012-03-22 11:40:24 +00:00
finalize_bp_solver(bp(Network, _)) :-
free_bayesian_network(Network).
2011-12-27 22:07:42 +00:00
2012-03-22 11:40:24 +00:00
assign_ids([], _).
assign_ids([V|Vs], Count) :-
2012-03-31 23:27:37 +01:00
put_atts(V, [id(Count)]),
Count1 is Count + 1,
assign_ids(Vs, Count1).
2012-03-22 11:40:24 +00:00
get_vars_info([], [], []).
get_vars_info(V.Vs,
2012-03-31 23:27:37 +01:00
var(VarId,DS,Ev,PIds,DistId).VarsInfo,
DistId.DistIds) :-
clpbn:get_atts(V, [dist(DistId, Parents)]), !,
get_atts(V, [id(VarId)]),
get_dist_domain_size(DistId, DS),
get_evidence(V, Ev),
vars_to_ids(Parents, PIds),
get_vars_info(Vs, VarsInfo, DistIds).
get_evidence(V, Ev) :-
2012-03-31 23:27:37 +01:00
clpbn:get_atts(V, [evidence(Ev)]), !.
2011-05-20 23:56:12 +01:00
get_evidence(_V, -1). % no evidence !!!
2012-03-22 11:40:24 +00:00
vars_to_ids([], []).
vars_to_ids([L|Vars], [LIds|Ids]) :-
2012-03-31 23:27:37 +01:00
is_list(L), !,
vars_to_ids(L, LIds),
vars_to_ids(Vars, Ids).
2012-03-22 11:40:24 +00:00
vars_to_ids([V|Vars], [VarId|Ids]) :-
2012-03-31 23:27:37 +01:00
get_atts(V, [id(VarId)]),
vars_to_ids(Vars, Ids).
2012-03-22 11:40:24 +00:00
get_extra_vars_info([], []).
get_extra_vars_info([V|Vs], [v(VarId, Label, Domain)|VarsInfo]) :-
2012-03-31 23:27:37 +01:00
get_atts(V, [id(VarId)]), !,
clpbn:get_atts(V, [key(Key), dist(DistId, _)]),
term_to_atom(Key, Label),
get_dist_domain(DistId, Domain0),
numbers_to_atoms(Domain0, Domain),
get_extra_vars_info(Vs, VarsInfo).
2011-12-10 22:58:43 +00:00
get_extra_vars_info([_|Vs], VarsInfo) :-
2012-03-31 23:27:37 +01:00
get_extra_vars_info(Vs, VarsInfo).
get_dists_parameters([],[]).
get_dists_parameters([Id|Ids], [dist(Id, Params)|DistsInfo]) :-
2012-03-31 23:27:37 +01:00
get_dist_params(Id, Params),
get_dists_parameters(Ids, DistsInfo).
2012-03-22 11:40:24 +00:00
numbers_to_atoms([], []).
numbers_to_atoms([Atom|L0], [Atom|L]) :-
2012-03-31 23:27:37 +01:00
atom(Atom), !,
numbers_to_atoms(L0, L).
2012-03-22 11:40:24 +00:00
numbers_to_atoms([Number|L0], [Atom|L]) :-
2012-03-31 23:27:37 +01:00
number_atom(Number, Atom),
numbers_to_atoms(L0, L).
2012-03-22 11:40:24 +00:00
print_clpbn_vars(Var.AllVars) :-
2012-03-31 23:27:37 +01:00
clpbn:get_atts(Var, [key(Key),dist(DistId,Parents)]),
parents_to_keys(Parents, ParentKeys),
writeln(Var:Key:ParentKeys:DistId),
print_clpbn_vars(AllVars).
2012-03-22 11:40:24 +00:00
print_clpbn_vars([]).
2012-03-22 11:40:24 +00:00
parents_to_keys([], []).
parents_to_keys(Var.Parents, Key.Keys) :-
2012-03-31 23:27:37 +01:00
clpbn:get_atts(Var, [key(Key)]),
parents_to_keys(Parents, Keys).