2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- module(clpbn,
|
2012-12-17 14:50:12 +00:00
|
|
|
[{}/1,
|
|
|
|
clpbn_flag/2,
|
|
|
|
set_clpbn_flag/2,
|
2012-12-17 23:14:54 +00:00
|
|
|
set_solver/1,
|
2012-12-17 21:19:47 +00:00
|
|
|
set_em_solver/1,
|
2012-12-17 14:50:12 +00:00
|
|
|
clpbn_flag/3,
|
|
|
|
clpbn_key/2,
|
2012-12-17 17:57:00 +00:00
|
|
|
clpbn_init_graph/1,
|
2012-12-17 14:50:12 +00:00
|
|
|
clpbn_init_solver/4,
|
|
|
|
clpbn_run_solver/3,
|
|
|
|
pfl_init_solver/5,
|
|
|
|
pfl_run_solver/3,
|
2012-12-17 23:59:52 +00:00
|
|
|
pfl_end_solver/1,
|
2012-12-17 14:50:12 +00:00
|
|
|
probability/2,
|
|
|
|
conditional_probability/3,
|
|
|
|
use_parfactors/1,
|
|
|
|
op(500, xfy, with)
|
|
|
|
]).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
:- use_module(library(atts)).
|
2012-12-17 11:53:57 +00:00
|
|
|
|
2012-08-08 00:33:51 +01:00
|
|
|
:- use_module(library(bhash)).
|
2012-12-17 11:53:57 +00:00
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
:- use_module(library(lists)).
|
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- use_module(library(terms)).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- use_module(library(maplist)).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2014-10-19 11:27:53 +01:00
|
|
|
:- yap_flag(arithmetic_exceptions,false).
|
|
|
|
|
2011-09-15 15:49:57 +01:00
|
|
|
:- attribute key/1, dist/2, evidence/1.
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2011-05-21 00:27:25 +01:00
|
|
|
:- use_module('clpbn/ve',
|
2012-12-17 14:50:12 +00:00
|
|
|
[ve/3,
|
|
|
|
check_if_ve_done/1,
|
|
|
|
init_ve_solver/4,
|
|
|
|
run_ve_solver/3,
|
|
|
|
init_ve_ground_solver/5,
|
|
|
|
run_ve_ground_solver/3,
|
|
|
|
call_ve_ground_solver/6
|
|
|
|
]).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
|
|
|
:- use_module('clpbn/jt',
|
2012-12-17 14:50:12 +00:00
|
|
|
[jt/3,
|
|
|
|
init_jt_solver/4,
|
|
|
|
run_jt_solver/3
|
|
|
|
]).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-03-22 19:10:15 +00:00
|
|
|
:- use_module('clpbn/bdd',
|
2012-12-17 14:50:12 +00:00
|
|
|
[bdd/3,
|
|
|
|
init_bdd_solver/4,
|
|
|
|
run_bdd_solver/3,
|
|
|
|
init_bdd_ground_solver/5,
|
|
|
|
run_bdd_ground_solver/3,
|
|
|
|
call_bdd_ground_solver/6
|
|
|
|
]).
|
2012-03-22 19:10:15 +00:00
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
:- use_module('clpbn/gibbs',
|
2012-12-17 14:50:12 +00:00
|
|
|
[gibbs/3,
|
|
|
|
check_if_gibbs_done/1,
|
|
|
|
init_gibbs_solver/4,
|
|
|
|
run_gibbs_solver/3
|
|
|
|
]).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2009-05-26 16:49:04 +01:00
|
|
|
:- use_module('clpbn/pgrammar',
|
2012-12-17 23:14:54 +00:00
|
|
|
[pcg_init_graph/0,
|
|
|
|
init_pcg_solver/4,
|
|
|
|
run_pcg_solver/3
|
2012-12-17 14:50:12 +00:00
|
|
|
]).
|
2009-05-26 16:49:04 +01:00
|
|
|
|
2013-01-10 22:10:22 +00:00
|
|
|
:- use_module('clpbn/horus',
|
|
|
|
[set_horus_flag/2]).
|
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- use_module('clpbn/horus_ground',
|
2012-12-17 14:50:12 +00:00
|
|
|
[call_horus_ground_solver/6,
|
|
|
|
check_if_horus_ground_solver_done/1,
|
|
|
|
init_horus_ground_solver/5,
|
|
|
|
run_horus_ground_solver/3,
|
2012-12-18 12:11:45 +00:00
|
|
|
end_horus_ground_solver/1
|
2012-12-17 14:50:12 +00:00
|
|
|
]).
|
2012-12-17 11:53:57 +00:00
|
|
|
|
|
|
|
:- use_module('clpbn/horus_lifted',
|
2012-12-17 14:50:12 +00:00
|
|
|
[call_horus_lifted_solver/3,
|
|
|
|
check_if_horus_lifted_solver_done/1,
|
|
|
|
init_horus_lifted_solver/4,
|
|
|
|
run_horus_lifted_solver/3,
|
2012-12-18 12:11:45 +00:00
|
|
|
end_horus_lifted_solver/1
|
2012-12-17 14:50:12 +00:00
|
|
|
]).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 23:29:21 +00:00
|
|
|
%% :- use_module('clpbn/bnt',
|
|
|
|
%% [do_bnt/3,
|
|
|
|
%% check_if_bnt_done/1
|
|
|
|
%% ]).
|
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
:- use_module('clpbn/dists',
|
2012-12-17 14:50:12 +00:00
|
|
|
[dist/4,
|
|
|
|
get_dist/4,
|
|
|
|
get_evidence_position/3,
|
|
|
|
get_evidence_from_position/3,
|
|
|
|
additive_dists/6
|
|
|
|
]).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
|
|
|
:- use_module('clpbn/evidence',
|
2012-12-17 14:50:12 +00:00
|
|
|
[store_evidence/1,
|
|
|
|
add_stored_evidence/2,
|
|
|
|
incorporate_evidence/2,
|
|
|
|
check_stored_evidence/2,
|
|
|
|
put_evidence/2
|
|
|
|
]).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- use_module('clpbn/ground_factors',
|
2012-12-17 14:50:12 +00:00
|
|
|
[generate_network/5]).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
|
|
|
:- use_module('clpbn/utils',
|
2012-12-17 14:50:12 +00:00
|
|
|
[sort_vars_by_key/3]).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- use_module('clpbn/graphs',
|
2012-12-17 14:50:12 +00:00
|
|
|
[clpbn2graph/1]).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- use_module('clpbn/graphviz',
|
2012-12-17 14:50:12 +00:00
|
|
|
[clpbn2gviz/4]).
|
2012-01-10 17:01:06 +00:00
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
%
|
|
|
|
% avoid the overhead of using goal_expansion/2.
|
|
|
|
%
|
|
|
|
:- multifile user:term_expansion/2.
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- dynamic user:term_expansion/2.
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
:- dynamic
|
|
|
|
solver/1,
|
2012-12-17 23:14:54 +00:00
|
|
|
em_solver/1,
|
2012-12-17 11:53:57 +00:00
|
|
|
suppress_attribute_display/1,
|
|
|
|
parameter_softening/1,
|
2012-12-17 23:14:54 +00:00
|
|
|
use_parfactors/1,
|
|
|
|
output/1,
|
|
|
|
use/1.
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2011-11-30 13:04:13 +00:00
|
|
|
:- meta_predicate probability(:,-), conditional_probability(:,:,-).
|
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
|
2012-12-19 18:25:47 +00:00
|
|
|
solver(hve).
|
|
|
|
em_solver(hve).
|
2009-02-16 12:23:29 +00:00
|
|
|
suppress_attribute_display(false).
|
|
|
|
parameter_softening(m_estimate(10)).
|
2012-01-10 17:01:06 +00:00
|
|
|
use_parfactors(off).
|
2012-12-17 23:14:54 +00:00
|
|
|
output(no).
|
|
|
|
%output(xbif(user_error)).
|
|
|
|
%output(gviz(user_error)).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 23:14:54 +00:00
|
|
|
ground_solver(ve).
|
|
|
|
ground_solver(hve).
|
|
|
|
ground_solver(jt).
|
|
|
|
ground_solver(bdd).
|
|
|
|
ground_solver(bp).
|
|
|
|
ground_solver(cbp).
|
|
|
|
ground_solver(gibbs).
|
|
|
|
|
|
|
|
lifted_solver(lve).
|
|
|
|
lifted_solver(lkc).
|
|
|
|
lifted_solver(lbp).
|
2012-12-17 11:53:57 +00:00
|
|
|
|
2012-12-17 23:14:54 +00:00
|
|
|
|
|
|
|
clpbn_flag(Flag, Option) :-
|
2009-02-16 12:23:29 +00:00
|
|
|
clpbn_flag(Flag, Option, Option).
|
|
|
|
|
|
|
|
set_clpbn_flag(Flag,Option) :-
|
|
|
|
clpbn_flag(Flag, _, Option).
|
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(solver,Before,After) :- !,
|
2009-02-16 12:23:29 +00:00
|
|
|
retract(solver(Before)),
|
|
|
|
assert(solver(After)).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(em_solver,Before,After) :- !,
|
2009-02-16 12:23:29 +00:00
|
|
|
retract(em_solver(Before)),
|
|
|
|
assert(em_solver(After)).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(bnt_solver,Before,After) :- !,
|
2009-02-16 12:23:29 +00:00
|
|
|
retract(bnt:bnt_solver(Before)),
|
|
|
|
assert(bnt:bnt_solver(After)).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(bnt_path,Before,After) :- !,
|
2009-02-16 12:23:29 +00:00
|
|
|
retract(bnt:bnt_path(Before)),
|
|
|
|
assert(bnt:bnt_path(After)).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(bnt_model,Before,After) :- !,
|
2009-02-16 12:23:29 +00:00
|
|
|
retract(bnt:bnt_model(Before)),
|
|
|
|
assert(bnt:bnt_model(After)).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(suppress_attribute_display,Before,After) :- !,
|
2009-02-16 12:23:29 +00:00
|
|
|
retract(suppress_attribute_display(Before)),
|
|
|
|
assert(suppress_attribute_display(After)).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(parameter_softening,Before,After) :- !,
|
2009-02-16 12:23:29 +00:00
|
|
|
retract(parameter_softening(Before)),
|
|
|
|
assert(parameter_softening(After)).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(use_factors,Before,After) :- !,
|
2012-01-10 17:01:06 +00:00
|
|
|
retract(use_parfactors(Before)),
|
|
|
|
assert(use_parfactors(After)).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(output,Before,After) :- !,
|
2012-12-17 23:14:54 +00:00
|
|
|
retract(output(Before)),
|
|
|
|
assert(output(After)).
|
|
|
|
|
2013-01-10 23:38:46 +00:00
|
|
|
clpbn_flag(HorusOption, _Before, After) :-
|
|
|
|
set_horus_flag(HorusOption, After).
|
2013-01-10 22:10:22 +00:00
|
|
|
|
2012-12-17 23:14:54 +00:00
|
|
|
set_solver(Solver) :-
|
|
|
|
set_clpbn_flag(solver,Solver).
|
|
|
|
|
2012-12-17 21:19:47 +00:00
|
|
|
set_em_solver(Solver) :-
|
2012-12-17 23:14:54 +00:00
|
|
|
set_clpbn_flag(em_solver,Solver).
|
2012-12-17 21:19:47 +00:00
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
{_} :-
|
|
|
|
solver(none), !.
|
2012-12-17 23:14:54 +00:00
|
|
|
{ Var = Key with Dist } :-
|
2009-02-16 12:23:29 +00:00
|
|
|
put_atts(El,[key(Key),dist(DistInfo,Parents)]),
|
|
|
|
dist(Dist, DistInfo, Key, Parents),
|
2009-05-02 20:11:54 +01:00
|
|
|
add_evidence(Var,Key,DistInfo,El)
|
|
|
|
% ,writeln({Var = Key with Dist})
|
2012-12-17 11:53:57 +00:00
|
|
|
.
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2011-05-21 00:15:20 +01:00
|
|
|
%
|
|
|
|
% make sure a query variable is reachable by the garbage collector.
|
|
|
|
%
|
2011-09-15 15:49:57 +01:00
|
|
|
% we use a mutable variable to avoid unnecessary trailing.
|
|
|
|
%
|
2012-12-20 23:19:10 +00:00
|
|
|
store_var(El) :-
|
|
|
|
nb_current(clpbn_qvars, Mutable),
|
2011-09-20 09:54:30 +01:00
|
|
|
nonvar(Mutable), !,
|
2011-09-15 15:49:57 +01:00
|
|
|
get_mutable(Tail, Mutable),
|
|
|
|
update_mutable(El.Tail, Mutable).
|
2012-12-20 23:19:10 +00:00
|
|
|
store_var(El) :-
|
2012-12-17 17:57:00 +00:00
|
|
|
init_clpbn_vars(El).
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2011-09-15 15:49:57 +01:00
|
|
|
init_clpbn_vars(El) :-
|
|
|
|
create_mutable(El, Mutable),
|
|
|
|
b_setval(clpbn_qvars, Mutable).
|
2011-05-21 00:15:20 +01:00
|
|
|
|
2012-12-17 23:14:54 +00:00
|
|
|
check_constraint(Constraint, _, _, Constraint) :-
|
|
|
|
var(Constraint), !.
|
|
|
|
check_constraint((A->D), _, _, (A->D)) :-
|
|
|
|
var(A), !.
|
2009-02-16 12:23:29 +00:00
|
|
|
check_constraint((([A|B].L)->D), Vars, NVars, (([A|B].NL)->D)) :- !,
|
|
|
|
check_cpt_input_vars(L, Vars, NVars, NL).
|
|
|
|
check_constraint(Dist, _, _, Dist).
|
|
|
|
|
|
|
|
check_cpt_input_vars([], _, _, []).
|
|
|
|
check_cpt_input_vars([V|L], Vars, NVars, [NV|NL]) :-
|
|
|
|
replace_var(Vars, V, NVars, NV),
|
|
|
|
check_cpt_input_vars(L, Vars, NVars, NL).
|
|
|
|
|
|
|
|
replace_var([], V, [], V).
|
|
|
|
replace_var([V|_], V0, [NV|_], NV) :- V == V0, !.
|
|
|
|
replace_var([_|Vars], V, [_|NVars], NV) :-
|
|
|
|
replace_var(Vars, V, NVars, NV).
|
|
|
|
|
|
|
|
add_evidence(V,Key,Distinfo,NV) :-
|
|
|
|
nonvar(V), !,
|
|
|
|
get_evidence_position(V, Distinfo, Pos),
|
|
|
|
check_stored_evidence(Key, Pos),
|
2011-05-21 00:19:53 +01:00
|
|
|
store_var(NV),
|
2009-02-16 12:23:29 +00:00
|
|
|
clpbn:put_atts(NV,evidence(Pos)).
|
|
|
|
add_evidence(V,K,_,V) :-
|
2012-01-10 17:01:06 +00:00
|
|
|
add_stored_evidence(K,V),
|
|
|
|
store_var(V).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
|
|
|
clpbn_marginalise(V, Dist) :-
|
|
|
|
attributes:all_attvars(AVars),
|
|
|
|
project_attributes([V], AVars),
|
2010-06-30 16:54:58 +01:00
|
|
|
clpbn_display:get_atts(V, posterior(_,_,Dist,_)).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
|
|
|
%
|
|
|
|
% called by top-level
|
|
|
|
% or by call_residue/2
|
|
|
|
%
|
2012-10-03 09:50:50 +01:00
|
|
|
project_attributes(GVars0, _AVars0) :-
|
2012-04-03 16:22:40 +01:00
|
|
|
use_parfactors(on),
|
2012-12-17 23:14:54 +00:00
|
|
|
clpbn_flag(solver, Solver),
|
|
|
|
ground_solver(Solver),
|
2012-10-03 09:50:50 +01:00
|
|
|
generate_network(GVars0, GKeys, Keys, Factors, Evidence),
|
|
|
|
b_setval(clpbn_query_variables, f(GVars0,Evidence)),
|
|
|
|
simplify_query(GVars0, GVars),
|
2012-12-17 17:57:00 +00:00
|
|
|
(
|
2012-12-20 23:19:10 +00:00
|
|
|
GKeys = []
|
2012-12-17 17:57:00 +00:00
|
|
|
->
|
2012-10-03 09:50:50 +01:00
|
|
|
GVars0 = [V|_],
|
|
|
|
clpbn_display:put_atts(V, [posterior([],[],[],[])])
|
|
|
|
;
|
|
|
|
call_ground_solver(Solver, GVars, GKeys, Keys, Factors, Evidence)
|
2012-12-17 17:57:00 +00:00
|
|
|
).
|
2012-04-03 17:12:58 +01:00
|
|
|
project_attributes(GVars, AVars) :-
|
2009-02-16 12:23:29 +00:00
|
|
|
suppress_attribute_display(false),
|
|
|
|
AVars = [_|_],
|
|
|
|
solver(Solver),
|
|
|
|
( GVars = [_|_] ; Solver = graphs), !,
|
2012-11-22 12:27:33 +00:00
|
|
|
% we don't pass query variables in this way
|
|
|
|
b_setval(clpbn_query_variables, none),
|
2009-02-16 12:23:29 +00:00
|
|
|
clpbn_vars(AVars, DiffVars, AllVars),
|
|
|
|
get_clpbn_vars(GVars,CLPBNGVars0),
|
|
|
|
simplify_query_vars(CLPBNGVars0, CLPBNGVars),
|
2011-05-21 00:27:25 +01:00
|
|
|
(output(xbif(XBifStream)) -> clpbn2xbif(XBifStream,ve,AllVars) ; true),
|
2009-02-16 12:23:29 +00:00
|
|
|
(output(gviz(XBifStream)) -> clpbn2gviz(XBifStream,sort,AllVars,GVars) ; true),
|
|
|
|
(
|
2012-12-17 17:57:00 +00:00
|
|
|
Solver = graphs
|
2009-02-16 12:23:29 +00:00
|
|
|
->
|
2012-12-17 17:57:00 +00:00
|
|
|
write_out(Solver, [[]], AllVars, DiffVars)
|
2009-02-16 12:23:29 +00:00
|
|
|
;
|
2012-12-17 17:57:00 +00:00
|
|
|
write_out(Solver, [CLPBNGVars], AllVars, DiffVars)
|
2009-02-16 12:23:29 +00:00
|
|
|
).
|
|
|
|
project_attributes(_, _).
|
|
|
|
|
2012-10-03 09:50:50 +01:00
|
|
|
%
|
|
|
|
% check for query variables with evidence
|
|
|
|
%
|
|
|
|
simplify_query([V|GVars0], GVars) :-
|
|
|
|
get_atts(V, [evidence(_)]), !,
|
|
|
|
simplify_query(GVars0, GVars).
|
|
|
|
simplify_query([V|GVars0], GVars) :-
|
|
|
|
get_atts(V, [key(K)]),
|
|
|
|
pfl:evidence(K, _), !,
|
|
|
|
simplify_query(GVars0, GVars).
|
|
|
|
simplify_query([V|GVars0], [V|GVars]) :-
|
|
|
|
simplify_query(GVars0, GVars).
|
|
|
|
simplify_query([], []).
|
|
|
|
|
2012-06-01 13:17:39 +01:00
|
|
|
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).
|
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
clpbn_vars(AVars, DiffVars, AllVars) :-
|
|
|
|
sort_vars_by_key(AVars,SortedAVars,DiffVars),
|
|
|
|
incorporate_evidence(SortedAVars, AllVars).
|
|
|
|
|
2012-01-10 17:01:06 +00:00
|
|
|
get_clpbn_vars([V|GVars],[V|CLPBNGVars]) :-
|
|
|
|
get_atts(V, [key(_)]), !,
|
|
|
|
get_clpbn_vars(GVars,CLPBNGVars).
|
|
|
|
get_clpbn_vars([_|GVars],CLPBNGVars) :-
|
|
|
|
get_clpbn_vars(GVars,CLPBNGVars).
|
2009-02-16 12:23:29 +00:00
|
|
|
get_clpbn_vars([],[]).
|
|
|
|
|
|
|
|
simplify_query_vars(LVs0, LVs) :-
|
|
|
|
sort(LVs0,LVs1),
|
|
|
|
get_rid_of_ev_vars(LVs1,LVs).
|
|
|
|
|
|
|
|
%
|
|
|
|
% some variables might already have evidence in the data-base.
|
|
|
|
%
|
|
|
|
get_rid_of_ev_vars([],[]).
|
|
|
|
get_rid_of_ev_vars([V|LVs0],LVs) :-
|
|
|
|
clpbn:get_atts(V, [dist(Id,_),evidence(Pos)]), !,
|
|
|
|
get_evidence_from_position(Ev, Id, Pos),
|
|
|
|
clpbn_display:put_atts(V, [posterior([],Ev,[],[])]), !,
|
|
|
|
get_rid_of_ev_vars(LVs0,LVs).
|
|
|
|
get_rid_of_ev_vars([V|LVs0],[V|LVs]) :-
|
|
|
|
get_rid_of_ev_vars(LVs0,LVs).
|
|
|
|
|
|
|
|
|
2012-12-17 23:14:54 +00:00
|
|
|
% Call a solver with keys, not actual variables
|
|
|
|
call_ground_solver(ve, GVars, GoalKeys, Keys, Factors, Evidence) :- !,
|
|
|
|
call_ve_ground_solver(GVars, GoalKeys, Keys, Factors, Evidence, _Answ).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
2012-12-17 23:14:54 +00:00
|
|
|
call_ground_solver(hve, GVars, GoalKeys, Keys, Factors, Evidence) :- !,
|
2013-01-10 21:51:45 +00:00
|
|
|
clpbn_horus:set_horus_flag(ground_solver, hve),
|
2012-08-08 00:33:51 +01:00
|
|
|
call_horus_ground_solver(GVars, GoalKeys, Keys, Factors, Evidence, _Answ).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
2012-09-24 14:36:30 +01:00
|
|
|
call_ground_solver(bdd, GVars, GoalKeys, Keys, Factors, Evidence) :- !,
|
|
|
|
call_bdd_ground_solver(GVars, GoalKeys, Keys, Factors, Evidence, _Answ).
|
2012-12-17 23:14:54 +00:00
|
|
|
|
|
|
|
call_ground_solver(bp, GVars, GoalKeys, Keys, Factors, Evidence) :- !,
|
|
|
|
clpbn_horus:set_horus_flag(ground_solver, bp),
|
|
|
|
call_horus_ground_solver(GVars, GoalKeys, Keys, Factors, Evidence, _Answ).
|
|
|
|
|
|
|
|
call_ground_solver(cbp, GVars, GoalKeys, Keys, Factors, Evidence) :- !,
|
|
|
|
clpbn_horus:set_horus_flag(ground_solver, cbp),
|
|
|
|
call_horus_ground_solver(GVars, GoalKeys, Keys, Factors, Evidence, _Answ).
|
|
|
|
|
2012-08-08 00:33:51 +01:00
|
|
|
call_ground_solver(Solver, GVars, _GoalKeys, Keys, Factors, Evidence) :-
|
2012-12-17 23:14:54 +00:00
|
|
|
% fall back to traditional solver
|
2012-08-08 00:33:51 +01:00
|
|
|
b_hash_new(Hash0),
|
2012-12-20 23:19:10 +00:00
|
|
|
foldl(gvar_in_hash, GVars, Hash0, HashI),
|
2012-08-29 02:21:14 +01:00
|
|
|
foldl(key_to_var, Keys, AllVars, HashI, Hash1),
|
|
|
|
foldl(evidence_to_v, Evidence, _EVars, Hash1, Hash),
|
2012-09-23 13:25:15 +01:00
|
|
|
%writeln(Keys:AllVars),
|
2012-08-29 02:21:14 +01:00
|
|
|
maplist(factor_to_dist(Hash), Factors),
|
2012-08-08 00:33:51 +01:00
|
|
|
% evidence
|
|
|
|
retract(use_parfactors(on)),
|
|
|
|
write_out(Solver, [GVars], AllVars, _),
|
|
|
|
assert(use_parfactors(on)).
|
|
|
|
|
2012-12-17 23:14:54 +00:00
|
|
|
|
|
|
|
% do nothing if we don't have query variables to compute.
|
2013-01-09 17:49:07 +00:00
|
|
|
write_out(_, GVars, AVars, _) :-
|
|
|
|
maplist(bound_varl(AVars), GVars), !.
|
|
|
|
|
2012-12-17 23:14:54 +00:00
|
|
|
write_out(_, [], _, _) :- !.
|
|
|
|
|
|
|
|
write_out(graphs, _, AVars, _) :- !,
|
|
|
|
clpbn2graph(AVars).
|
|
|
|
|
|
|
|
write_out(ve, GVars, AVars, DiffVars) :- !,
|
|
|
|
ve(GVars, AVars, DiffVars).
|
|
|
|
|
|
|
|
write_out(jt, GVars, AVars, DiffVars) :- !,
|
|
|
|
jt(GVars, AVars, DiffVars).
|
|
|
|
|
|
|
|
write_out(bdd, GVars, AVars, DiffVars) :- !,
|
|
|
|
bdd(GVars, AVars, DiffVars).
|
|
|
|
|
|
|
|
write_out(gibbs, GVars, AVars, DiffVars) :- !,
|
|
|
|
gibbs(GVars, AVars, DiffVars).
|
|
|
|
|
|
|
|
write_out(lve, GVars, AVars, DiffVars) :- !,
|
|
|
|
clpbn_horus:set_horus_flag(lifted_solver, lve),
|
|
|
|
call_horus_lifted_solver(GVars, AVars, DiffVars).
|
|
|
|
|
|
|
|
write_out(lkc, GVars, AVars, DiffVars) :- !,
|
|
|
|
clpbn_horus:set_horus_flag(lifted_solver, lkc),
|
|
|
|
call_horus_lifted_solver(GVars, AVars, DiffVars).
|
|
|
|
|
|
|
|
write_out(lbp, GVars, AVars, DiffVars) :- !,
|
|
|
|
clpbn_horus:set_horus_flag(lifted_solver, lbp),
|
|
|
|
call_horus_lifted_solver(GVars, AVars, DiffVars).
|
|
|
|
|
|
|
|
write_out(bnt, GVars, AVars, DiffVars) :- !,
|
|
|
|
do_bnt(GVars, AVars, DiffVars).
|
|
|
|
|
|
|
|
write_out(Solver, _, _, _) :-
|
2012-12-20 23:34:53 +00:00
|
|
|
format("Error: solver '~w' is unknown.", [Solver]),
|
2012-12-17 23:14:54 +00:00
|
|
|
fail.
|
|
|
|
|
2013-01-09 17:49:07 +00:00
|
|
|
bound_varl(AVars, L) :-
|
|
|
|
maplist(bound_var(AVars), L).
|
|
|
|
|
|
|
|
bound_var(_AVars,V) :-
|
|
|
|
var(V), !,
|
|
|
|
get_atts(V, [key(K)]),
|
|
|
|
( pfl:evidence(K, Ev) -> true ; get_atts(V, [key(K), evidence(Ev)]) ),
|
|
|
|
pfl:skolem(K,D),
|
|
|
|
once(nth0(Ev,D,V)).
|
|
|
|
bound_var(_AVars, _V).
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-08 00:33:51 +01:00
|
|
|
%
|
2012-08-29 02:21:14 +01:00
|
|
|
% convert a PFL network (without constraints)
|
2012-08-08 00:33:51 +01:00
|
|
|
% into CLP(BN) for evaluation
|
|
|
|
%
|
2012-08-29 02:21:14 +01:00
|
|
|
gvar_in_hash(V, Hash0, Hash) :-
|
2012-08-08 00:33:51 +01:00
|
|
|
get_atts(V, [key(K)]),
|
2012-12-20 23:19:10 +00:00
|
|
|
b_hash_insert(Hash0, K, V, Hash).
|
2012-08-08 00:33:51 +01:00
|
|
|
|
2012-08-29 02:21:14 +01:00
|
|
|
key_to_var(K, V, Hash0, Hash0) :-
|
|
|
|
b_hash_lookup(K, V, Hash0), !.
|
|
|
|
key_to_var(K, V,Hash0, Hash) :-
|
|
|
|
put_atts(V, [key(K)]),
|
|
|
|
b_hash_insert(Hash0, K, V, Hash).
|
2012-08-08 00:33:51 +01:00
|
|
|
|
2012-08-29 02:21:14 +01:00
|
|
|
evidence_to_v(K=E, V, Hash0, Hash0) :-
|
2012-08-08 00:33:51 +01:00
|
|
|
b_hash_lookup(K, V, Hash0), !,
|
2012-08-29 02:21:14 +01:00
|
|
|
clpbn:put_atts(V,[evidence(E)]).
|
|
|
|
evidence_to_v(K=E, V, Hash0, Hash) :-
|
|
|
|
b_hash_insert(Hash0, K, V, Hash),
|
|
|
|
clpbn:put_atts(V,[evidence(E)]).
|
2012-08-08 00:33:51 +01:00
|
|
|
|
2012-08-29 02:21:14 +01:00
|
|
|
factor_to_dist(Hash, f(bayes, Id, Ks)) :-
|
|
|
|
maplist(key_to_var(Hash), Ks, [V|Parents]),
|
2012-08-08 00:33:51 +01:00
|
|
|
Ks =[Key|_],
|
|
|
|
pfl:skolem(Key, Domain),
|
2013-04-07 15:51:20 +01:00
|
|
|
pfl:get_pfl_parameters(Id, Ks, CPT),
|
2012-08-08 00:33:51 +01:00
|
|
|
dist(p(Domain,CPT,Parents), DistInfo, Key, Parents),
|
2012-08-29 02:21:14 +01:00
|
|
|
put_atts(V,[dist(DistInfo,Parents)]).
|
2012-04-03 16:22:40 +01:00
|
|
|
|
2012-08-29 02:21:14 +01:00
|
|
|
key_to_var(Hash, K, V) :-
|
|
|
|
b_hash_lookup(K, V, Hash).
|
2012-04-03 16:22:40 +01:00
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
get_bnode(Var, Goal) :-
|
|
|
|
get_atts(Var, [key(Key),dist(Dist,Parents)]),
|
|
|
|
get_dist(Dist,_,Domain,CPT),
|
|
|
|
(Parents = [] -> X = tab(Domain,CPT) ; X = tab(Domain,CPT,Parents)),
|
|
|
|
dist_goal(X, Key, Goal0),
|
2011-09-15 15:49:57 +01:00
|
|
|
include_evidence(Var, Goal0, Key, Goal).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
|
|
|
include_evidence(Var, Goal0, Key, ((Key:-Ev),Goal0)) :-
|
|
|
|
get_atts(Var, [evidence(Ev)]), !.
|
|
|
|
include_evidence(_, Goal0, _, Goal0).
|
|
|
|
|
|
|
|
dist_goal(Dist, Key, (Key=NDist)) :-
|
|
|
|
term_variables(Dist, DVars),
|
|
|
|
process_vars(DVars, DKeys),
|
|
|
|
my_copy_term(Dist,DVars, NDist,DKeys).
|
|
|
|
|
|
|
|
my_copy_term(V, DVars, Key, DKeys) :-
|
|
|
|
find_var(DVars, V, Key, DKeys).
|
|
|
|
my_copy_term(A, _, A, _) :- atomic(A), !.
|
|
|
|
my_copy_term(T, Vs, NT, Ks) :-
|
|
|
|
T =.. [Na|As],
|
|
|
|
my_copy_terms(As, Vs, NAs, Ks),
|
|
|
|
NT =.. [Na|NAs].
|
|
|
|
|
|
|
|
my_copy_terms([], _, [], _).
|
|
|
|
my_copy_terms([A|As], Vs, [NA|NAs], Ks) :-
|
|
|
|
my_copy_term(A, Vs, NA, Ks),
|
|
|
|
my_copy_terms(As, Vs, NAs, Ks).
|
|
|
|
|
|
|
|
find_var([V1|_], V, Key, [Key|_]) :- V1 == V, !.
|
|
|
|
find_var([_|DVars], V, Key, [_|DKeys]) :-
|
|
|
|
find_var(DVars, V, Key, DKeys).
|
|
|
|
|
|
|
|
process_vars([], []).
|
|
|
|
process_vars([V|Vs], [K|Ks]) :-
|
2012-12-17 11:53:57 +00:00
|
|
|
process_var(V, K),
|
2009-02-16 12:23:29 +00:00
|
|
|
process_vars(Vs, Ks).
|
|
|
|
|
2012-12-20 23:19:10 +00:00
|
|
|
process_var(V, K) :- get_atts(V, [key(K)]), !.
|
2009-02-16 12:23:29 +00:00
|
|
|
% oops: this variable has no attributes.
|
|
|
|
process_var(V, _) :- throw(error(instantiation_error,clpbn(attribute_goal(V)))).
|
|
|
|
|
|
|
|
%
|
2012-12-20 23:19:10 +00:00
|
|
|
% unify a CLPBN variable with something.
|
2009-02-16 12:23:29 +00:00
|
|
|
%
|
2009-09-10 00:02:09 +01:00
|
|
|
verify_attributes(Var, T, Goal) :-
|
2009-02-16 12:23:29 +00:00
|
|
|
get_atts(Var, [key(Key),dist(Dist,Parents)]), !,
|
|
|
|
/* oops, someone trying to bind a clpbn constrained variable */
|
2009-09-10 00:02:09 +01:00
|
|
|
bind_clpbn(T, Var, Key, Dist, Parents, Goal).
|
2009-02-16 12:23:29 +00:00
|
|
|
verify_attributes(_, _, []).
|
|
|
|
|
|
|
|
|
2009-09-10 00:02:09 +01:00
|
|
|
bind_clpbn(T, Var, _, _, _, do_not_bind_variable([put_evidence(T,Var)])) :-
|
|
|
|
nonvar(T),
|
|
|
|
!.
|
|
|
|
bind_clpbn(T, Var, Key, Dist, Parents, []) :- var(T),
|
2009-05-02 20:11:54 +01:00
|
|
|
get_atts(T, [key(Key1),dist(Dist1,Parents1)]),
|
2009-05-26 16:49:04 +01:00
|
|
|
(
|
2012-12-17 17:57:00 +00:00
|
|
|
bind_clpbns(Key, Dist, Parents, Key1, Dist1, Parents1)
|
2009-05-02 20:11:54 +01:00
|
|
|
->
|
2012-12-17 17:57:00 +00:00
|
|
|
(
|
|
|
|
get_atts(T, [evidence(Ev1)]) ->
|
|
|
|
bind_evidence_from_extra_var(Ev1,Var)
|
|
|
|
;
|
|
|
|
get_atts(Var, [evidence(Ev)]) ->
|
|
|
|
bind_evidence_from_extra_var(Ev,T)
|
|
|
|
;
|
|
|
|
true
|
|
|
|
)
|
2009-02-16 12:23:29 +00:00
|
|
|
;
|
2012-12-17 17:57:00 +00:00
|
|
|
fail
|
2009-05-02 20:11:54 +01:00
|
|
|
).
|
2009-09-10 00:02:09 +01:00
|
|
|
bind_clpbn(_, Var, _, _, _, _, []) :-
|
2011-05-21 00:27:25 +01:00
|
|
|
use(ve),
|
|
|
|
check_if_ve_done(Var), !.
|
2011-05-17 12:00:33 +01:00
|
|
|
bind_clpbn(_, Var, _, _, _, _, []) :-
|
2012-12-17 23:14:54 +00:00
|
|
|
use(hve),
|
2012-05-23 21:23:03 +01:00
|
|
|
check_if_horus_ground_solver_done(Var), !.
|
2009-09-10 00:02:09 +01:00
|
|
|
bind_clpbn(_, Var, _, _, _, _, []) :-
|
2009-02-16 12:23:29 +00:00
|
|
|
use(jt),
|
2011-05-21 00:27:25 +01:00
|
|
|
check_if_ve_done(Var), !.
|
2012-03-22 19:10:15 +00:00
|
|
|
bind_clpbn(_, Var, _, _, _, _, []) :-
|
|
|
|
use(bdd),
|
|
|
|
check_if_bdd_done(Var), !.
|
2012-12-17 23:14:54 +00:00
|
|
|
bind_clpbn(_, Var, _, _, _, _, []) :-
|
|
|
|
use(bp),
|
|
|
|
check_if_horus_ground_solver_done(Var), !.
|
|
|
|
bind_clpbn(_, Var, _, _, _, _, []) :-
|
|
|
|
use(cbp),
|
|
|
|
check_if_horus_ground_solver_done(Var), !.
|
|
|
|
bind_clpbn(_, Var, _, _, _, _, []) :-
|
|
|
|
use(bnt),
|
|
|
|
check_if_bnt_done(Var), !.
|
2009-09-10 00:02:09 +01:00
|
|
|
bind_clpbn(T, Var, Key0, _, _, _, []) :-
|
2009-02-16 12:23:29 +00:00
|
|
|
get_atts(Var, [key(Key)]), !,
|
|
|
|
(
|
|
|
|
Key = Key0 -> true
|
|
|
|
;
|
2012-12-17 17:57:00 +00:00
|
|
|
% let us not loose whatever we had.
|
2009-09-10 00:02:09 +01:00
|
|
|
put_evidence(T,Var)
|
2009-02-16 12:23:29 +00:00
|
|
|
).
|
|
|
|
|
|
|
|
fresh_attvar(Var, NVar) :-
|
|
|
|
get_atts(Var, LAtts),
|
|
|
|
put_atts(NVar, LAtts).
|
|
|
|
|
|
|
|
% I will now allow two CLPBN variables to be bound together.
|
2012-12-17 23:14:54 +00:00
|
|
|
% bind_clpbns(Key, Dist, Parents, Key, Dist, Parents).
|
2012-12-17 17:57:00 +00:00
|
|
|
bind_clpbns(Key, Dist, Parents, Key1, Dist1, Parents1) :-
|
2009-02-16 12:23:29 +00:00
|
|
|
Key == Key1, !,
|
2009-05-02 20:11:54 +01:00
|
|
|
get_dist(Dist,_Type,_Domain,_Table),
|
|
|
|
get_dist(Dist1,_Type1,_Domain1,_Table1),
|
2012-03-25 21:40:29 +01:00
|
|
|
Dist = Dist1,
|
|
|
|
Parents = Parents1.
|
2009-02-16 12:23:29 +00:00
|
|
|
bind_clpbns(Key, _, _, _, Key1, _, _, _) :-
|
|
|
|
Key\=Key1, !, fail.
|
|
|
|
bind_clpbns(_, _, _, _, _, _, _, _) :-
|
|
|
|
format(user_error, 'unification of two bayesian vars not supported~n', []).
|
|
|
|
|
|
|
|
same_parents([],[]).
|
|
|
|
same_parents([P|Parents],[P1|Parents1]) :-
|
|
|
|
same_node(P,P1),
|
|
|
|
same_parents(Parents,Parents1).
|
|
|
|
|
|
|
|
same_node(P,P1) :- P == P1, !.
|
|
|
|
same_node(P,P1) :-
|
|
|
|
get_atts( P,[key(K)]),
|
|
|
|
get_atts(P1,[key(K)]),
|
|
|
|
P = P1.
|
|
|
|
|
|
|
|
|
|
|
|
bind_evidence_from_extra_var(Ev1,Var) :-
|
|
|
|
get_atts(Var, [evidence(Ev0)]), !,
|
|
|
|
Ev0 = Ev1.
|
|
|
|
bind_evidence_from_extra_var(Ev1,Var) :-
|
|
|
|
put_atts(Var, [evidence(Ev1)]).
|
|
|
|
|
2012-12-17 17:57:00 +00:00
|
|
|
user:term_expansion((A :- {}), ( :- true )) :- !, % evidence
|
2009-02-16 12:23:29 +00:00
|
|
|
prolog_load_context(module, M),
|
|
|
|
store_evidence(M:A).
|
|
|
|
|
|
|
|
clpbn_key(Var,Key) :-
|
|
|
|
get_atts(Var, [key(Key)]).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
|
|
|
|
2012-12-17 11:53:57 +00:00
|
|
|
%
|
|
|
|
% only useful for probabilistic context free grammars
|
|
|
|
%
|
|
|
|
clpbn_init_graph(pcg) :- !,
|
|
|
|
pcg_init_graph.
|
|
|
|
clpbn_init_graph(_).
|
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
|
|
|
|
%
|
|
|
|
% This is a routine to start a solver, called by the learning procedures (ie, em).
|
|
|
|
% LVs is a list of lists of variables one is interested in eventually marginalising out
|
|
|
|
% Vs0 gives the original graph
|
|
|
|
% AllDiffs gives variables that are not fully constrainted, ie, we don't fully know
|
|
|
|
% the key. In this case, we assume different instances will be bound to different
|
|
|
|
% values at the end of the day.
|
|
|
|
%
|
|
|
|
clpbn_init_solver(LVs, Vs0, VarsWithUnboundKeys, State) :-
|
2011-05-17 12:00:33 +01:00
|
|
|
solver(Solver),
|
2009-02-16 12:23:29 +00:00
|
|
|
clpbn_init_solver(Solver, LVs, Vs0, VarsWithUnboundKeys, State).
|
|
|
|
|
2011-05-21 00:27:25 +01:00
|
|
|
clpbn_init_solver(ve, LVs, Vs0, VarsWithUnboundKeys, State) :-
|
|
|
|
init_ve_solver(LVs, Vs0, VarsWithUnboundKeys, State).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
clpbn_init_solver(jt, LVs, Vs0, VarsWithUnboundKeys, State) :-
|
|
|
|
init_jt_solver(LVs, Vs0, VarsWithUnboundKeys, State).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2012-03-22 19:10:15 +00:00
|
|
|
clpbn_init_solver(bdd, LVs, Vs0, VarsWithUnboundKeys, State) :-
|
|
|
|
init_bdd_solver(LVs, Vs0, VarsWithUnboundKeys, State).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2012-12-17 23:29:21 +00:00
|
|
|
clpbn_init_solver(gibbs, LVs, Vs0, VarsWithUnboundKeys, State) :-
|
|
|
|
init_gibbs_solver(LVs, Vs0, VarsWithUnboundKeys, State).
|
|
|
|
|
2009-05-26 16:49:04 +01:00
|
|
|
clpbn_init_solver(pcg, LVs, Vs0, VarsWithUnboundKeys, State) :-
|
|
|
|
init_pcg_solver(LVs, Vs0, VarsWithUnboundKeys, State).
|
2009-02-16 12:23:29 +00:00
|
|
|
|
|
|
|
%
|
|
|
|
% LVs is the list of lists of variables to marginalise
|
|
|
|
% Vs is the full graph
|
2012-12-20 23:19:10 +00:00
|
|
|
% Ps are the probabilities on LVs.
|
2009-02-16 12:23:29 +00:00
|
|
|
%
|
|
|
|
clpbn_run_solver(LVs, LPs, State) :-
|
2011-05-17 12:00:33 +01:00
|
|
|
solver(Solver),
|
2009-02-16 12:23:29 +00:00
|
|
|
clpbn_run_solver(Solver, LVs, LPs, State).
|
|
|
|
|
2011-05-21 00:27:25 +01:00
|
|
|
clpbn_run_solver(ve, LVs, LPs, State) :-
|
|
|
|
run_ve_solver(LVs, LPs, State).
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2009-02-16 12:23:29 +00:00
|
|
|
clpbn_run_solver(jt, LVs, LPs, State) :-
|
|
|
|
run_jt_solver(LVs, LPs, State).
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2012-03-22 19:10:15 +00:00
|
|
|
clpbn_run_solver(bdd, LVs, LPs, State) :-
|
|
|
|
run_bdd_solver(LVs, LPs, State).
|
|
|
|
|
2012-12-17 23:29:21 +00:00
|
|
|
clpbn_run_solver(gibbs, LVs, LPs, State) :-
|
|
|
|
run_gibbs_solver(LVs, LPs, State).
|
|
|
|
|
2009-05-26 16:49:04 +01:00
|
|
|
clpbn_run_solver(pcg, LVs, LPs, State) :-
|
|
|
|
run_pcg_solver(LVs, LPs, State).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2012-12-15 00:13:53 +00:00
|
|
|
%
|
|
|
|
% This is a routine to start a solver, called by the learning procedures (ie, em).
|
|
|
|
%
|
2012-12-15 16:11:03 +00:00
|
|
|
pfl_init_solver(QueryKeys, AllKeys, Factors, Evidence, State) :-
|
2012-12-17 18:50:36 +00:00
|
|
|
em_solver(Solver),
|
2012-12-17 21:03:13 +00:00
|
|
|
(lifted_solver(Solver) ->
|
|
|
|
format("Error: you cannot use a lifted solver for learning.", [Solver]), fail
|
|
|
|
;
|
|
|
|
true
|
|
|
|
),
|
|
|
|
(ground_solver(Solver) ->
|
|
|
|
true
|
|
|
|
;
|
2012-12-20 23:34:53 +00:00
|
|
|
format("Error: solver '~w' is unknown.", [Solver]), fail
|
2012-12-17 21:03:13 +00:00
|
|
|
),
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_init_solver(Solver, QueryKeys, AllKeys, Factors, Evidence, State).
|
2012-12-15 16:11:03 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_init_solver(ve, QueryKeys, AllKeys, Factors, Evidence, State) :- !,
|
2012-12-15 00:29:03 +00:00
|
|
|
init_ve_ground_solver(QueryKeys, AllKeys, Factors, Evidence, State).
|
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_init_solver(hve, QueryKeys, AllKeys, Factors, Evidence, State) :- !,
|
2013-01-10 21:51:45 +00:00
|
|
|
clpbn_horus:set_horus_flag(ground_solver, hve),
|
2012-12-15 00:29:03 +00:00
|
|
|
init_horus_ground_solver(QueryKeys, AllKeys, Factors, Evidence, State).
|
2012-12-15 00:13:53 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_init_solver(bdd, QueryKeys, AllKeys, Factors, Evidence, State) :- !,
|
2012-12-17 21:03:13 +00:00
|
|
|
init_bdd_ground_solver(QueryKeys, AllKeys, Factors, Evidence, State).
|
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_init_solver(bp, QueryKeys, AllKeys, Factors, Evidence, State) :- !,
|
2012-12-17 17:57:00 +00:00
|
|
|
clpbn_horus:set_horus_flag(ground_solver, bp),
|
2012-12-15 00:16:06 +00:00
|
|
|
init_horus_ground_solver(QueryKeys, AllKeys, Factors, Evidence, State).
|
2012-12-15 00:13:53 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_init_solver(cbp, QueryKeys, AllKeys, Factors, Evidence, State) :- !,
|
2012-12-17 17:57:00 +00:00
|
|
|
clpbn_horus:set_horus_flag(ground_solver, cbp),
|
2012-12-15 00:16:06 +00:00
|
|
|
init_horus_ground_solver(QueryKeys, AllKeys, Factors, Evidence, State).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_init_solver(Solver, _, _, _, _, _) :-
|
2012-12-20 23:34:53 +00:00
|
|
|
format("Error: solver '~w' can't be used for learning.", [Solver]),
|
2012-12-17 21:03:13 +00:00
|
|
|
fail.
|
|
|
|
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2012-12-15 16:11:03 +00:00
|
|
|
pfl_run_solver(LVs, LPs, State) :-
|
2012-12-17 18:50:36 +00:00
|
|
|
em_solver(Solver),
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_run_solver(Solver, LVs, LPs, State).
|
2012-12-15 00:13:53 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_run_solver(ve, LVs, LPs, State) :- !,
|
2012-09-29 11:50:00 +01:00
|
|
|
run_ve_ground_solver(LVs, LPs, State).
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_run_solver(hve, LVs, LPs, State) :- !,
|
2012-12-17 21:03:13 +00:00
|
|
|
run_horus_ground_solver(LVs, LPs, State).
|
2012-12-15 00:13:53 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_run_solver(bdd, LVs, LPs, State) :- !,
|
2012-09-29 11:50:00 +01:00
|
|
|
run_bdd_ground_solver(LVs, LPs, State).
|
2012-12-15 00:13:53 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_run_solver(bp, LVs, LPs, State) :- !,
|
2012-12-15 15:56:05 +00:00
|
|
|
run_horus_ground_solver(LVs, LPs, State).
|
2012-12-17 17:57:00 +00:00
|
|
|
|
2013-01-15 12:27:21 +00:00
|
|
|
pfl_run_solver(cbp, LVs, LPs, State) :- !,
|
2012-12-15 15:56:05 +00:00
|
|
|
run_horus_ground_solver(LVs, LPs, State).
|
2012-09-29 11:50:00 +01:00
|
|
|
|
2012-12-17 23:59:52 +00:00
|
|
|
pfl_end_solver(State) :-
|
|
|
|
(em_solver(hve) ; em_solver(bp) ; em_solver(cbp)),
|
2012-12-18 12:11:45 +00:00
|
|
|
end_horus_ground_solver(State).
|
2012-12-17 23:59:52 +00:00
|
|
|
pfl_end_solver(_State).
|
|
|
|
|
2012-09-29 11:50:00 +01:00
|
|
|
|
2009-05-02 20:11:54 +01:00
|
|
|
add_keys(Key1+V1,_Key2,Key1+V1).
|
|
|
|
|
2011-11-30 13:04:13 +00:00
|
|
|
|
|
|
|
probability(Goal, Prob) :-
|
|
|
|
findall(Prob, do_probability(Goal, [], Prob), [Prob]).
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2012-10-02 14:26:42 +01:00
|
|
|
conditional_probability(Goal, ListOfGoals, Prob) :-
|
|
|
|
\+ ground(Goal),
|
|
|
|
throw(error(ground(Goal),conditional_probability(Goal, ListOfGoals, Prob))).
|
2011-11-30 13:04:13 +00:00
|
|
|
conditional_probability(Goal, ListOfGoals, Prob) :-
|
|
|
|
\+ ground(ListOfGoals), !,
|
|
|
|
throw(error(ground(ListOfGoals),conditional_probability(Goal, ListOfGoals, Prob))).
|
|
|
|
conditional_probability(Goal, ListOfGoals, Prob) :-
|
|
|
|
findall(Prob, do_probability(Goal, ListOfGoals, Prob), [Prob]).
|
|
|
|
|
|
|
|
do_probability(Goal, ListOfGoals, Prob) :-
|
|
|
|
evidence_to_var(Goal, C, NGoal, V),
|
|
|
|
call_residue(run( ListOfGoals, NGoal), Vars), !,
|
2012-10-02 14:26:42 +01:00
|
|
|
match_probability(Vars, NGoal, C, V, Prob).
|
2011-11-30 13:04:13 +00:00
|
|
|
|
|
|
|
run(ListOfGoals,Goal) :-
|
|
|
|
do(ListOfGoals),
|
|
|
|
call(Goal).
|
|
|
|
|
|
|
|
do(M:ListOfGoals) :-
|
|
|
|
do(ListOfGoals, M).
|
|
|
|
do([]).
|
|
|
|
|
|
|
|
do([], _M).
|
|
|
|
do(G.ListOfGoals, M) :-
|
|
|
|
M:G,
|
|
|
|
do(ListOfGoals, M).
|
|
|
|
|
|
|
|
evidence_to_var(M:Goal, C, M:VItem, V) :- !,
|
|
|
|
evidence_to_var(Goal, C, VItem, V).
|
|
|
|
evidence_to_var(Goal, C, VItem, V) :-
|
|
|
|
Goal =.. [L|Args],
|
|
|
|
variabilise_last(Args, C, NArgs, V),
|
|
|
|
VItem =.. [L|NArgs].
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2011-11-30 13:04:13 +00:00
|
|
|
variabilise_last([Arg], Arg, [V], V).
|
|
|
|
variabilise_last([Arg1,Arg2|Args], Arg, Arg1.NArgs, V) :-
|
|
|
|
variabilise_last(Arg2.Args, Arg, NArgs, V).
|
|
|
|
|
2012-10-02 14:26:42 +01:00
|
|
|
match_probability(VPs, Goal, C, V, Prob) :-
|
2012-12-17 11:53:57 +00:00
|
|
|
match_probabilities(VPs, Goal, C, V, Prob).
|
2012-10-02 14:26:42 +01:00
|
|
|
|
|
|
|
match_probabilities([p(V0=C)=Prob|_], _, C, V, Prob) :-
|
2012-12-17 11:53:57 +00:00
|
|
|
V0 == V,
|
|
|
|
!.
|
2012-10-03 09:50:50 +01:00
|
|
|
match_probabilities([_|Probs], G, C, V, Prob) :-
|
2012-12-17 11:53:57 +00:00
|
|
|
match_probabilities(Probs, G, C, V, Prob).
|
2012-10-02 14:26:42 +01:00
|
|
|
|
|
|
|
goal_to_key(_:Goal, Skolem) :-
|
2012-12-17 11:53:57 +00:00
|
|
|
goal_to_key(Goal, Skolem).
|
2012-10-02 14:26:42 +01:00
|
|
|
goal_to_key(Goal, Skolem) :-
|
2012-12-17 11:53:57 +00:00
|
|
|
functor(Goal, Na, Ar),
|
|
|
|
Ar1 is Ar-1,
|
|
|
|
functor(Skolem, Na, Ar1).
|
2011-11-30 13:04:13 +00:00
|
|
|
|
2012-04-03 15:01:35 +01:00
|
|
|
:- use_parfactors(on) -> true ; assert(use_parfactors(off)).
|
2011-11-30 13:04:13 +00:00
|
|
|
|