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/ground_factors.yap

140 lines
3.9 KiB
Plaintext
Raw Normal View History

2012-01-10 17:01:06 +00:00
%parfactor(
% [ability(P),grade(C,S), satisfaction(C,S,P)],
% \phi = [....],
% [P,C,S],
% [P \in [p1,p2,p4], C \in [c1,c3], S \in [s2,s3]]).
% [S \= s2])
:- module(clpbn_ground_factors, [
2012-04-03 16:22:40 +01:00
generate_network/5]).
2012-01-10 17:01:06 +00:00
:- use_module(library(bhash), [
b_hash_new/1,
b_hash_lookup/3,
b_hash_insert/4]).
2012-04-03 15:01:35 +01:00
:- use_module(library(lists), [
delete/3,
2012-04-03 17:12:58 +01:00
nth0/3,
2012-04-03 15:01:35 +01:00
member/2]).
2012-01-10 17:01:06 +00:00
:- use_module(library(pfl), [
2012-04-03 15:01:35 +01:00
factor/6,
defined_in_factor/2,
2012-01-10 17:01:06 +00:00
skolem/2]).
:- use_module(library(clpbn/dists), [
dist/4]).
2012-04-12 18:11:29 +01:00
:- dynamic currently_defined/1, f/4.
2012-01-10 17:01:06 +00:00
2012-04-04 08:29:20 +01:00
generate_network(QueryVars0, QueryKeys, Keys, Factors, Evidence) :-
2012-01-10 17:01:06 +00:00
attributes:all_attvars(AVars),
2012-04-03 17:12:58 +01:00
keys(QueryVars0, QueryKeys0),
check_for_evidence(AVars, EVars, QueryKeys0, QueryVars0, Evidence),
check_for_extra_bindings(QueryVars0, QueryVars, QueryKeys0, QueryKeys),
do_network(QueryVars, EVars, Keys, Factors).
do_network([], _, _, _) :- !.
do_network(QueryVars, EVars, Keys, Factors) :-
2012-04-03 15:01:35 +01:00
retractall(currently_defined(_)),
2012-04-12 18:11:29 +01:00
retractall(f(_,_,_,_)),
2012-04-03 15:01:35 +01:00
run_through_factors(QueryVars),
run_through_factors(EVars),
findall(K, currently_defined(K), Keys),
ground_all_keys(QueryVars, Keys),
ground_all_keys(EVars, Keys),
2012-04-12 18:11:29 +01:00
findall(f(FType,FId,FKeys,FCPT), f(FType,FId,FKeys,FCPT), Factors).
2012-04-03 16:22:40 +01:00
run_through_factors([]).
run_through_factors([Var|_QueryVars]) :-
clpbn:get_atts(Var,[key(K)]),
find_factors(K),
fail.
run_through_factors([_|QueryVars]) :-
run_through_factors(QueryVars).
ground_all_keys([], _).
ground_all_keys([V|GVars], AllKeys) :-
clpbn:get_atts(V,[key(Key)]),
\+ ground(Key), !,
2012-07-03 19:48:13 +01:00
writeln(g:Key),
member(Key, AllKeys),
ground_all_keys(GVars, AllKeys).
ground_all_keys([_V|GVars], AllKeys) :-
ground_all_keys(GVars, AllKeys).
2012-06-01 13:17:39 +01:00
2012-04-03 17:12:58 +01:00
%
% look for attributed vars with evidence (should also search the DB)
% verifiy if the evidence overlaps with query
% bind query if so.
%
check_for_evidence(V.AVars, V.EVars, QueryKeys, QueryVars, (K=E).Evidence) :-
2012-04-03 16:22:40 +01:00
clpbn:get_atts(V,[key(K),evidence(E)]), !,
2012-04-03 17:12:58 +01:00
check_for_evidence_in_query(K, QueryKeys, QueryVars, E),
check_for_evidence(AVars, EVars, QueryKeys, QueryVars, Evidence).
% ignore no evidence vars
check_for_evidence(_V.AVars, EVars, QueryKeys, QueryVars, Evidence) :-
check_for_evidence(AVars, EVars, QueryKeys, QueryVars, Evidence).
check_for_evidence([], [], _, _, []).
%
% do we still have free query variables?
%
check_for_extra_bindings([], [], [], []).
check_for_extra_bindings([V|QueryVars0], QueryVars, [_|QueryKeys0], QueryKeys) :-
nonvar(V),!,
check_for_extra_bindings(QueryVars0, QueryVars, QueryKeys0, QueryKeys).
check_for_extra_bindings([V|QueryVars0], [V|QueryVars], [K|QueryKeys0], [K|QueryKeys]) :-
check_for_extra_bindings(QueryVars0, QueryVars, QueryKeys0, QueryKeys).
check_for_evidence_in_query(Key, [Key|QueryKeys], [V|QueryVars], E) :- !,
skolem(Key, Dom),
nth0(E, Dom, Val),
V = Val,
check_for_evidence_in_query(Key, QueryKeys, QueryVars, E).
check_for_evidence_in_query(Key, [_|QueryKeys], [_|QueryVars], E) :-
check_for_evidence_in_query(Key, QueryKeys, QueryVars, E).
check_for_evidence_in_query(_Key, [], [], _E).
2012-04-03 16:22:40 +01:00
keys([], []).
keys([Var|QueryVars], [Key|QueryKeys]) :-
clpbn:get_atts(Var,[key(Key)]),
keys(QueryVars, QueryKeys).
2012-01-10 17:01:06 +00:00
2012-04-03 15:01:35 +01:00
initialize_evidence([]).
initialize_evidence([V|EVars]) :-
clpbn:get_atts(V, [key(K)]),
ground(K),
2012-04-03 15:01:35 +01:00
assert(currently_defined(K)),
initialize_evidence(EVars).
2012-01-10 17:01:06 +00:00
%
2012-04-03 15:01:35 +01:00
% gets key K, and collects factors that define it
find_factors(K) :-
2012-04-03 17:12:58 +01:00
\+ currently_defined(K),
( ground(K) -> assert(currently_defined(K)) ; true),
2012-04-03 15:01:35 +01:00
defined_in_factor(K, ParFactor),
add_factor(ParFactor, Ks),
member(K1, Ks),
\+ currently_defined(K1),
find_factors(K1).
2012-04-12 18:11:29 +01:00
add_factor(factor(Type, Id, Ks, _, Phi, Constraints), Ks) :-
F = f(Type, Id, Ks, CPT),
2012-04-12 17:17:21 +01:00
( is_list(Phi) -> CPT = Phi ; call(user:Phi, CPT) ),
2012-04-03 15:01:35 +01:00
run(Constraints),
2012-04-12 18:11:29 +01:00
\+ f(Type, Id, Ks, CPT),
2012-04-03 15:01:35 +01:00
assert(F).
run([Goal|Goals]) :-
call(user:Goal),
run(Goals).
run([]).