2004-07-15 17:23:44 +01:00
|
|
|
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
:- module(clpbn, [{}/1,
|
|
|
|
clpbn_flag/2,
|
|
|
|
set_clpbn_flag/2,
|
|
|
|
clpbn_flag/3]).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
|
|
|
:- use_module(library(atts)).
|
|
|
|
:- use_module(library(lists)).
|
|
|
|
:- use_module(library(terms)).
|
|
|
|
|
|
|
|
:- op( 500, xfy, with).
|
|
|
|
|
|
|
|
%
|
|
|
|
% avoid the overhead of using goal_expansion/2.
|
|
|
|
%
|
|
|
|
:- multifile
|
|
|
|
user:term_expansion/2.
|
|
|
|
|
|
|
|
:- dynamic
|
|
|
|
user:term_expansion/2.
|
|
|
|
|
|
|
|
:- multifile
|
|
|
|
user:term_expansion/2.
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
:- attribute key/1, dist/3, evidence/1, done/1, starter/0.
|
2004-07-15 17:23:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
:- use_module('clpbn/bnt', [dump_as_bnt/2,
|
|
|
|
check_if_bnt_done/1
|
|
|
|
]).
|
|
|
|
|
2004-11-16 16:38:09 +00:00
|
|
|
:- use_module('clpbn/vel', [vel/3,
|
2004-07-15 17:23:44 +01:00
|
|
|
check_if_vel_done/1
|
|
|
|
]).
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
:- use_module('clpbn/graphs', [
|
|
|
|
clpbn2graph/1
|
2004-07-15 17:23:44 +01:00
|
|
|
]).
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
:- use_module('clpbn/evidence', [
|
|
|
|
store_evidence/1,
|
|
|
|
incorporate_evidence/2
|
|
|
|
]).
|
|
|
|
|
|
|
|
:- dynamic solver/1,output/1.
|
|
|
|
|
|
|
|
solver(vel).
|
|
|
|
|
|
|
|
%output(xbif(user_error)).
|
2004-12-20 21:44:58 +00:00
|
|
|
%output(gviz(user_error)).
|
|
|
|
output(no).
|
2004-12-16 06:07:07 +00:00
|
|
|
|
|
|
|
clpbn_flag(Flag,Option) :-
|
|
|
|
clpbn_flag(Flag, Option, Option).
|
|
|
|
|
|
|
|
set_clpbn_flag(Flag,Option) :-
|
|
|
|
clpbn_flag(Flag, _, Option).
|
|
|
|
|
|
|
|
clpbn_flag(output,Before,After) :-
|
|
|
|
retract(output(Before)),
|
|
|
|
assert(output(After)).
|
|
|
|
clpbn_flag(solver,Before,After) :-
|
|
|
|
retract(solver(Before)),
|
|
|
|
assert(solver(After)).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
|
|
|
{Var = Key with Dist} :-
|
2004-12-16 06:07:07 +00:00
|
|
|
put_atts(El,[key(Key),dist(Domain,Table,Parents)]),
|
|
|
|
extract_dist(Dist, Table, Parents, Domain),
|
2004-07-15 17:23:44 +01:00
|
|
|
add_evidence(Var,El).
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
extract_dist(V, Tab.Inps, Domain) :- var(V), !,
|
2004-07-15 17:23:44 +01:00
|
|
|
V = p(Domain, Tab, Inps).
|
2004-12-16 06:07:07 +00:00
|
|
|
extract_dist(p(Domain, Tab, Inps), Tab, Inps, Domain).
|
|
|
|
extract_dist(p(Domain, Tab), Tab, [], Domain).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
|
|
|
check_constraint(Constraint, _, _, Constraint) :- var(Constraint), !.
|
2004-11-16 16:38:09 +00:00
|
|
|
check_constraint((A->D), _, _, (A->D)) :- var(A), !.
|
|
|
|
check_constraint((([A|B].L)->D), Vars, NVars, (([A|B].NL)->D)) :- !,
|
2004-07-15 17:23:44 +01:00
|
|
|
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,NV) :-
|
|
|
|
nonvar(V), !,
|
|
|
|
clpbn:put_atts(NV,evidence(V)).
|
|
|
|
add_evidence(V,V).
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
%
|
2004-07-15 17:23:44 +01:00
|
|
|
% called by top-level
|
2004-12-16 06:07:07 +00:00
|
|
|
% or by call_residue/2
|
2004-07-15 17:23:44 +01:00
|
|
|
%
|
|
|
|
project_attributes(GVars, AVars) :-
|
|
|
|
AVars = [_|_], !,
|
|
|
|
sort_vars_by_key(AVars,SortedAVars,DiffVars),
|
|
|
|
get_clpbn_vars(GVars,CLPBNGVars),
|
2004-12-16 06:07:07 +00:00
|
|
|
solver(Solver),
|
|
|
|
incorporate_evidence(SortedAVars, AllVars),
|
|
|
|
write_out(Solver,CLPBNGVars, AllVars, DiffVars).
|
2004-07-15 17:23:44 +01:00
|
|
|
project_attributes(_, _).
|
|
|
|
|
|
|
|
get_clpbn_vars([],[]).
|
|
|
|
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).
|
|
|
|
|
|
|
|
sort_vars_by_key(AVars,SortedAVars, UnifiableVars) :-
|
|
|
|
get_keys(AVars, KeysVars),
|
|
|
|
keysort(KeysVars, KVars),
|
|
|
|
merge_same_key(KVars, SortedAVars, [], UnifiableVars).
|
|
|
|
|
|
|
|
get_keys([], []).
|
|
|
|
get_keys([V|AVars], [K-V|KeysVars]) :-
|
|
|
|
get_atts(V, [key(K)]),
|
|
|
|
get_keys(AVars, KeysVars).
|
|
|
|
|
|
|
|
merge_same_key([], [], _, []).
|
|
|
|
merge_same_key([K1-V1,K2-V2|Vs], SortedAVars, Ks, UnifiableVars) :-
|
|
|
|
K1 == K2, !, V1 = V2,
|
|
|
|
merge_same_key([K1-V1|Vs], SortedAVars, Ks, UnifiableVars).
|
|
|
|
merge_same_key([K1-V1,K2-V2|Vs], [V1|SortedAVars], Ks, [K1|UnifiableVars]) :-
|
|
|
|
(in_keys(K1, Ks) ; \+ \+ K1 = K2), !,
|
|
|
|
add_to_keys(K1, Ks, NKs),
|
|
|
|
merge_same_key([K2-V2|Vs], SortedAVars, NKs, UnifiableVars).
|
|
|
|
merge_same_key([K-V|Vs], [V|SortedAVars], Ks, UnifiableVars) :-
|
|
|
|
add_to_keys(K, Ks, NKs),
|
|
|
|
merge_same_key(Vs, SortedAVars, NKs, UnifiableVars).
|
|
|
|
|
|
|
|
in_keys(K1,[K|_]) :- \+ \+ K1 = K, !.
|
|
|
|
in_keys(K1,[_|Ks]) :-
|
|
|
|
in_keys(K1,Ks).
|
|
|
|
|
|
|
|
add_to_keys(K1, Ks, Ks) :- ground(K1), !.
|
|
|
|
add_to_keys(K1, Ks, [K1|Ks]).
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
write_out(vel, GVars, AVars, DiffVars) :-
|
2004-07-15 17:23:44 +01:00
|
|
|
vel(GVars, AVars, DiffVars).
|
2004-12-16 06:07:07 +00:00
|
|
|
write_out(bnt, GVars, AVars, _) :-
|
2004-07-15 17:23:44 +01:00
|
|
|
dump_as_bnt(GVars, AVars).
|
2004-12-16 06:07:07 +00:00
|
|
|
write_out(graphs, _, AVars, _) :-
|
|
|
|
clpbn2graph(AVars).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
|
|
|
get_bnode(Var, Goal) :-
|
2004-12-16 06:07:07 +00:00
|
|
|
get_atts(Var, [key(Key),dist(A,B,C)]),
|
|
|
|
(C = [] -> X = tab(A,B) ; X = tab(A,B,C)),
|
2004-07-15 17:23:44 +01:00
|
|
|
dist_goal(X, Key, Goal0),
|
|
|
|
include_evidence(Var, Goal0, Key, Goali),
|
|
|
|
include_starter(Var, Goali, Key, Goal).
|
|
|
|
|
2004-11-16 16:38:09 +00:00
|
|
|
include_evidence(Var, Goal0, Key, ((Key:-Ev),Goal0)) :-
|
2004-07-15 17:23:44 +01:00
|
|
|
get_atts(Var, [evidence(Ev)]), !.
|
|
|
|
include_evidence(_, Goal0, _, Goal0).
|
|
|
|
|
2004-11-16 16:38:09 +00:00
|
|
|
include_starter(Var, Goal0, Key, ((:-Key),Goal0)) :-
|
2004-07-15 17:23:44 +01:00
|
|
|
get_atts(Var, [starter]), !.
|
|
|
|
include_starter(_, 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]) :-
|
|
|
|
process_var(V, K),
|
|
|
|
process_vars(Vs, Ks).
|
|
|
|
|
|
|
|
process_var(V, K) :- get_atts(V, [key(K)]), !.
|
|
|
|
% oops: this variable has no attributes.
|
|
|
|
process_var(V, _) :- throw(error(instantiation_error,clpbn(attribute_goal(V)))).
|
|
|
|
|
|
|
|
%
|
|
|
|
% unify a CLPBN variable with something.
|
|
|
|
%
|
|
|
|
verify_attributes(Var, T, Goals) :-
|
2004-12-16 06:07:07 +00:00
|
|
|
get_atts(Var, [key(Key),dist(Domain,Table,Parents)]), !,
|
2004-07-15 17:23:44 +01:00
|
|
|
/* oops, someone trying to bind a clpbn constrained variable */
|
|
|
|
Goals = [],
|
2004-12-16 06:07:07 +00:00
|
|
|
bind_clpbn(T, Var, Key, Domain, Table, Parents).
|
2004-07-15 17:23:44 +01:00
|
|
|
verify_attributes(_, _, []).
|
|
|
|
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
bind_clpbn(T, Var, Key, Domain, Table, Parents) :- var(T),
|
|
|
|
get_atts(T, [key(Key1),dist(Doman1,Table1,Parents1)]), !,
|
|
|
|
bind_clpbns(Key, Domain, Table, Parents, Key1, Doman1, Table1, Parents1),
|
2004-12-05 05:01:45 +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).
|
2004-07-15 17:23:44 +01:00
|
|
|
bind_clpbn(_, Var, _, _) :-
|
|
|
|
use(bnt),
|
|
|
|
check_if_bnt_done(Var), !.
|
|
|
|
bind_clpbn(_, Var, _, _) :-
|
|
|
|
use(vel),
|
|
|
|
check_if_vel_done(Var), !.
|
|
|
|
bind_clpbn(T, Var, Key0, _) :-
|
|
|
|
get_atts(Var, [key(Key0)]), !,
|
|
|
|
(
|
|
|
|
Key = Key0 -> true
|
|
|
|
;
|
|
|
|
format(user_error, "trying to force evidence ~w through unification with key ~w~n",[T, Key])
|
|
|
|
).
|
|
|
|
|
|
|
|
fresh_attvar(Var, NVar) :-
|
|
|
|
get_atts(Var, LAtts),
|
|
|
|
put_atts(NVar, LAtts).
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
bind_clpbns(Key, Domain, Table, Parents, Key1, Domain1, Table1, Parents1) :-
|
|
|
|
Key == Key1, !,
|
|
|
|
( Domain == Domain1, Table == Table1, Parents == Parents1 -> true ; throw(error(domain_error(bayesian_domain),bind_clpbns))).
|
|
|
|
bind_clpbns(_, _, _, _, _, _, _, _) :-
|
2004-07-15 17:23:44 +01:00
|
|
|
format(user_error, "unification of two bayesian vars not supported~n").
|
|
|
|
|
2004-12-05 05:01:45 +00:00
|
|
|
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)]).
|
|
|
|
|
2004-07-15 17:23:44 +01:00
|
|
|
user:term_expansion((A :- {}), ( :- true )) :- !, % evidence
|
|
|
|
prolog_load_context(module, M),
|
2004-12-16 06:07:07 +00:00
|
|
|
store_evidence(M:A).
|
2004-07-15 17:23:44 +01:00
|
|
|
|