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

162 lines
3.2 KiB
Plaintext
Raw Normal View History

2012-01-10 17:01:06 +00:00
2012-12-17 11:53:57 +00:00
:- module(pfl_ground_factors,
[generate_network/5,
f/3
]).
2012-01-10 17:01:06 +00:00
2012-12-17 11:53:57 +00:00
:- use_module(library(bhash),
[b_hash_new/1,
b_hash_lookup/3,
b_hash_insert/4,
b_hash_to_list/2
]).
2012-01-10 17:01:06 +00:00
2012-12-17 11:53:57 +00:00
:- use_module(library(lists),
[member/2]).
2012-04-03 15:01:35 +01:00
2012-08-29 02:21:14 +01:00
:- use_module(library(maplist)).
2012-12-17 11:53:57 +00:00
:- use_module(library(atts)).
2012-01-10 17:01:06 +00:00
2012-12-17 11:53:57 +00:00
:- use_module(library(pfl),
[factor/6,
defined_in_factor/2,
skolem/2
]).
2012-12-17 11:53:57 +00:00
:- use_module(library(clpbn/aggregates),
[avg_factors/5]).
2012-09-23 13:25:15 +01:00
2012-12-17 11:53:57 +00:00
:- use_module(library(clpbn/dists),
[dist/4]).
2012-01-10 17:01:06 +00:00
2012-08-15 22:01:45 +01:00
:- dynamic currently_defined/1, queue/1, f/4.
2012-01-10 17:01:06 +00:00
2012-08-09 04:16:37 +01:00
%
% as you add query vars the network grows
% until you reach the last variable.
%
2012-08-15 22:01:45 +01:00
generate_network(QueryVars, QueryKeys, Keys, Factors, EList) :-
2012-08-09 04:16:37 +01:00
init_global_search,
attributes:all_attvars(AVars),
b_hash_new(Evidence0),
2012-08-29 22:36:46 +01:00
foldl(include_evidence,AVars, Evidence0, Evidence1),
static_evidence(Evidence1, Evidence),
2012-12-17 17:57:00 +00:00
b_hash_to_list(Evidence, EList0),
2012-08-29 22:36:46 +01:00
maplist(pair_to_evidence,EList0, EList),
maplist(queue_evidence, EList),
foldl(run_through_query(Evidence), QueryVars, [], QueryKeys),
2012-08-15 22:01:45 +01:00
propagate,
2012-08-09 04:16:37 +01:00
collect(Keys, Factors).
%
% clean global stateq
%
init_global_search :-
2012-12-17 11:53:57 +00:00
retractall(queue(_)),
retractall(currently_defined(_)),
retractall(f(_,_,_)).
2012-08-09 04:16:37 +01:00
2012-08-29 22:36:46 +01:00
pair_to_evidence(K-E, K=E).
2012-08-09 04:16:37 +01:00
2012-08-29 22:36:46 +01:00
include_evidence(V, Evidence0, Evidence) :-
2012-08-09 04:16:37 +01:00
clpbn:get_atts(V,[key(K),evidence(E)]), !,
(
2012-12-17 17:57:00 +00:00
b_hash_lookup(K, E1, Evidence0)
2012-08-09 04:16:37 +01:00
->
2012-12-17 17:57:00 +00:00
(E \= E1 -> throw(clpbn:incompatible_evidence(K,E,E1)) ; Evidence = Evidence0)
2012-08-09 04:16:37 +01:00
;
2012-12-17 17:57:00 +00:00
b_hash_insert(Evidence0, K, E, Evidence)
2012-08-29 22:36:46 +01:00
).
include_evidence(_, Evidence, Evidence).
static_evidence(Evidence0, Evidence) :-
findall(Sk=Var, pfl:evidence(Sk,Var), Evs),
foldl(include_static_evidence, Evs, Evidence0, Evidence).
2012-08-09 04:16:37 +01:00
2012-08-29 22:36:46 +01:00
include_static_evidence(K=E, Evidence0, Evidence) :-
2012-08-09 04:16:37 +01:00
(
2012-12-17 17:57:00 +00:00
b_hash_lookup(K, E1, Evidence0)
2012-08-09 04:16:37 +01:00
->
2012-12-17 17:57:00 +00:00
(E \= E1 -> throw(incompatible_evidence(K,E,E1)) ; Evidence = Evidence0)
2012-08-09 04:16:37 +01:00
;
2012-12-17 17:57:00 +00:00
b_hash_insert(Evidence0, K, E, Evidence)
2012-08-29 22:36:46 +01:00
).
2012-08-09 04:16:37 +01:00
2012-08-29 22:36:46 +01:00
queue_evidence(K=_) :-
2012-12-17 17:57:00 +00:00
queue_in(K).
2012-08-09 04:16:37 +01:00
2012-08-29 22:36:46 +01:00
run_through_query(Evidence, V, QueryKeys, QueryKeys) :-
2012-08-09 04:16:37 +01:00
clpbn:get_atts(V,[key(K)]),
2012-08-29 22:36:46 +01:00
b_hash_lookup(K, _, Evidence), !.
run_through_query(_Evidence, V, QueryKeys, [K|QueryKeys]) :-
2012-08-09 04:16:37 +01:00
clpbn:get_atts(V,[key(K)]),
2012-08-29 22:36:46 +01:00
queue_in(K).
2012-08-09 04:16:37 +01:00
collect(Keys, Factors) :-
2012-04-03 15:01:35 +01:00
findall(K, currently_defined(K), Keys),
2012-08-15 22:01:45 +01:00
findall(f(FType,FId,FKeys), f(FType,FId,FKeys), Factors).
2012-04-03 16:22:40 +01:00
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
2012-08-15 22:01:45 +01:00
queue_in(K) :-
queue(K), !.
queue_in(K) :-
% writeln(q+K),
2012-08-29 02:21:14 +01:00
assert(queue(K)),
fail.
queue_in(_).
2012-08-15 22:01:45 +01:00
propagate :-
retract(queue(K)),!,
do_propagate(K).
propagate.
do_propagate(K) :-
%writeln(-K),
2012-04-03 17:12:58 +01:00
\+ currently_defined(K),
2012-12-17 11:53:57 +00:00
( ground(K) -> assert(currently_defined(K)) ; true),
2012-08-15 22:01:45 +01:00
(
2012-12-17 17:57:00 +00:00
defined_in_factor(K, ParFactor),
add_factor(ParFactor, Ks)
2012-12-17 11:53:57 +00:00
*->
2012-12-17 17:57:00 +00:00
true
2012-08-15 22:01:45 +01:00
;
2012-12-17 17:57:00 +00:00
throw(error(no_defining_factor(K)))
2012-09-23 13:25:15 +01:00
),
2012-04-03 15:01:35 +01:00
member(K1, Ks),
\+ currently_defined(K1),
2012-08-15 22:01:45 +01:00
queue_in(K1),
fail.
2012-08-29 02:21:14 +01:00
do_propagate(_K) :-
2012-12-17 17:57:00 +00:00
propagate.
2012-04-03 15:01:35 +01:00
2012-08-29 22:36:46 +01:00
add_factor(factor(Type, Id, Ks, _, _Phi, Constraints), NKs) :-
2012-12-17 11:53:57 +00:00
% writeln(+Ks),
(
2012-12-17 17:57:00 +00:00
Ks = [K,Els], var(Els)
2012-12-17 11:53:57 +00:00
->
2012-12-17 17:57:00 +00:00
% aggregate factor
once(run(Constraints)),
avg_factors(K, Els, 0.0, NewKeys, NewId),
NKs = [K|NewKeys]
2012-12-17 11:53:57 +00:00
;
2012-12-17 17:57:00 +00:00
run(Constraints),
NKs = Ks,
Id = NewId
2012-08-29 22:36:46 +01:00
),
(
2012-12-17 17:57:00 +00:00
f(Type, NewId, NKs)
2012-12-17 11:53:57 +00:00
->
2012-12-17 17:57:00 +00:00
true
2012-12-17 11:53:57 +00:00
;
2012-12-17 17:57:00 +00:00
assert(f(Type, NewId, NKs))
2012-12-17 11:53:57 +00:00
).
2012-04-03 15:01:35 +01:00
run([Goal|Goals]) :-
call(user:Goal),
run(Goals).
run([]).