add more support for FOVE.

This commit is contained in:
Vítor Santos Costa
2012-03-22 19:10:15 +00:00
parent 7d4f83ca40
commit a338b95d3f
14 changed files with 533 additions and 1243 deletions

View File

@@ -3,26 +3,42 @@
%
%
:- module(clpbn_parfactors, [
:- module(pfl, [
factor/5,
skolem/2,
get_pfl_parameters/2, % given id return par factor parameter
new_pfl_parameters/2, % given id set new parameters
get_first_pvariable/2, % given id get firt pvar (useful in bayesian)
get_factor_pvariable/2, % given id get any pvar
op(550,yfx,::),
op(1150,fx,bayes),
op(1150,fx,markov),
op(1150,fx,parfactor)]).
:- use_module(library(lists),
[nth0/3,
append/3]).
append/3,
member/2]).
:- dynamic factor/5, skolem_in/2, skolem/2, preprocess/3, evidence/2, id/1.
:- reexport(library(clpbn),
[clpbn_flag/3,
set_clpbn_flag/2]).
[clpbn_flag/2,
clpbn_flag/2 as pfl_flag,
set_clpbn_flag/2,
set_clpbn_flag/2 as set_pfl_flag]).
:- set_clpbn_flag(use_factors,on).
:- set_pfl_flag(use_factors,on).
user:term_expansion( bayes((Formula ; Phi ; Constraints)), clpbn_parfactors:factor(Id,FList,FV,Phi,Constraints)) :-
:- pfl_not_clpbn.
user:term_expansion( bayes((Formula ; Phi ; Constraints)), pfl:factor(Id,FList,FV,Phi,Constraints)) :-
!,
term_variables(Formula, FreeVars),
FV =.. [fv|FreeVars],
new_id(Id),
process_args(Formula, Id, 0, _, FList, []).
user:term_expansion( markov((Formula ; Phi ; Constraints)), pfl:factor(Id,FList,FV,Phi,Constraints)) :-
!,
term_variables(Formula, FreeVars),
FV =.. [fv|FreeVars],
@@ -32,7 +48,7 @@ user:term_expansion( Goal, [] ) :-
preprocess(Goal, Sk,Var), !,
(ground(Goal) -> true ; throw(error('non ground evidence',Goal))),
% prolog_load_context(module, M),
assert(evidence(Sk,Var)).
assert(pfl:evidence(Sk,Var)).
id(0).
@@ -78,7 +94,10 @@ interface_predicate(Sk) :-
ESk =.. ESKAs,
assert(preprocess(ESk, Sk, Var)),
assert_static((user:ESk :-
var(Var) -> insert_atts(Var,Sk) ; add_evidence(Sk,Var) )
evidence(Sk,Ev) -> Ev = Var;
var(Var) -> insert_atts(Var,Sk) ;
add_evidence(Sk,Var)
)
).
insert_atts(Var,Sk) :-
@@ -90,3 +109,36 @@ add_evidence(Sk,Var) :-
clpbn:put_atts(_V,[key(Sk),evidence(E)]).
get_pfl_parameters(Id,Out) :-
factor(Id,_FList,_FV,Phi,_Constraints),
writeln(factor(Id,_FList,_FV,_Phi,_Constraints)),
( is_list(Phi) -> Out = Phi ; call(user:Phi, Out) ).
new_pfl_parameters(Id, NewPhi) :-
retract(factor(Id,FList,FV,_Phi,Constraints)),
assert(factor(Id,FList,FV,NewPhi,Constraints)),
fail.
new_pfl_parameters(_Id, _NewPhi).
get_pfl_factor_sizes(Id, DSizes) :-
factor(Id, FList, _FV, _Phi, _Constraints),
get_sizes(FList, DSizes).
get_sizes([], []).
get_sizes(Key.FList, Sz.DSizes) :-
skolem(Key, Domain),
length(Domain, Sz),
get_sizes(FList, DSizes).
% only makes sense for bayesian networks
get_first_pvariable(Id,Var) :-
factor(Id,Var._FList,_FV,_Phi,_Constraints).
% only makes sense for bayesian networks
get_factor_pvariable(Id,Var) :-
factor(Id,FList,_FV,_Phi,_Constraints),
member(Var, FList).