Reorders and some reindentation

This commit is contained in:
Tiago Gomes 2012-12-17 11:53:57 +00:00
parent 9ff9be2f49
commit e35cf51476
7 changed files with 263 additions and 247 deletions

View File

@ -1,38 +1,34 @@
:- module(clpbn, [{}/1,
clpbn_flag/2,
set_clpbn_flag/2,
clpbn_flag/3,
clpbn_key/2,
clpbn_init_solver/4,
clpbn_run_solver/3,
pfl_init_solver/5,
pfl_run_solver/3,
clpbn_finalize_solver/1,
clpbn_init_graph/1,
probability/2,
conditional_probability/3,
use_parfactors/1,
op( 500, xfy, with)]).
:- module(clpbn,
[{}/1,
clpbn_flag/2,
set_clpbn_flag/2,
clpbn_flag/3,
clpbn_key/2,
clpbn_init_graph/1,
clpbn_init_solver/4,
clpbn_run_solver/3,
clpbn_finalize_solver/1,
pfl_init_solver/5,
pfl_run_solver/3,
probability/2,
conditional_probability/3,
use_parfactors/1,
op(500, xfy, with)
]).
:- use_module(library(atts)).
:- use_module(library(bhash)).
:- use_module(library(lists)).
:- use_module(library(terms)).
:- use_module(library(maplist)).
%
% avoid the overhead of using goal_expansion/2.
%
:- multifile
user:term_expansion/2.
:- dynamic
user:term_expansion/2.
:- attribute key/1, dist/2, evidence/1.
:- use_module('clpbn/ve',
[ve/3,
check_if_ve_done/1,
@ -43,6 +39,39 @@
call_ve_ground_solver/6
]).
:- use_module('clpbn/jt',
[jt/3,
init_jt_solver/4,
run_jt_solver/3
]).
:- use_module('clpbn/bdd',
[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
]).
:- use_module('clpbn/gibbs',
[gibbs/3,
check_if_gibbs_done/1,
init_gibbs_solver/4,
run_gibbs_solver/3
]).
%% :- use_module('clpbn/bnt',
%% [do_bnt/3,
%% check_if_bnt_done/1
%% ]).
:- use_module('clpbn/pgrammar',
[init_pcg_solver/4,
run_pcg_solver/3,
pcg_init_graph/0
]).
:- use_module('clpbn/horus_ground',
[call_horus_ground_solver/6,
check_if_horus_ground_solver_done/1,
@ -59,47 +88,8 @@
finalize_horus_lifted_solver/1
]).
:- use_module('clpbn/jt',
[jt/3,
init_jt_solver/4,
run_jt_solver/3
]).
:- use_module('clpbn/bdd',
[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
]).
%% :- use_module('clpbn/bnt',
%% [do_bnt/3,
%% check_if_bnt_done/1
%% ]).
:- use_module('clpbn/gibbs',
[gibbs/3,
check_if_gibbs_done/1,
init_gibbs_solver/4,
run_gibbs_solver/3
]).
:- use_module('clpbn/pgrammar',
[init_pcg_solver/4,
run_pcg_solver/3,
pcg_init_graph/0
]).
:- use_module('clpbn/graphs',
[
clpbn2graph/1
]).
:- use_module('clpbn/dists',
[
dist/4,
[dist/4,
get_dist/4,
get_evidence_position/3,
get_evidence_from_position/3,
@ -107,33 +97,47 @@
]).
:- use_module('clpbn/evidence',
[
store_evidence/1,
[store_evidence/1,
add_stored_evidence/2,
incorporate_evidence/2,
check_stored_evidence/2,
put_evidence/2
]).
:- use_module('clpbn/ground_factors',
[generate_network/5]).
:- use_module('clpbn/utils',
[
sort_vars_by_key/3
]).
[sort_vars_by_key/3]).
:- use_module('clpbn/graphs',
[clpbn2graph/1]).
:- use_module('clpbn/graphviz',
[clpbn2gviz/4]).
:- use_module(clpbn/ground_factors,
[generate_network/5]).
[clpbn2gviz/4]).
:- dynamic solver/1,output/1,use/1,suppress_attribute_display/1, parameter_softening/1, em_solver/1, use_parfactors/1.
%
% avoid the overhead of using goal_expansion/2.
%
:- multifile user:term_expansion/2.
:- dynamic user:term_expansion/2.
:- dynamic
solver/1,
output/1,
use/1,
suppress_attribute_display/1,
parameter_softening/1,
em_solver/1,
use_parfactors/1.
:- meta_predicate probability(:,-), conditional_probability(:,:,-).
solver(ve).
em_solver(bp).
:- meta_predicate probability(:,-), conditional_probability(:,:,-).
%output(xbif(user_error)).
%output(gviz(user_error)).
output(no).
@ -141,6 +145,7 @@ suppress_attribute_display(false).
parameter_softening(m_estimate(10)).
use_parfactors(off).
clpbn_flag(Flag,Option) :-
clpbn_flag(Flag, Option, Option).
@ -182,7 +187,7 @@ clpbn_flag(use_factors,Before,After) :-
dist(Dist, DistInfo, Key, Parents),
add_evidence(Var,Key,DistInfo,El)
% ,writeln({Var = Key with Dist})
.
.
%
% make sure a query variable is reachable by the garbage collector.
@ -331,8 +336,7 @@ write_out(jt, GVars, AVars, DiffVars) :-
write_out(bdd, GVars, AVars, DiffVars) :-
bdd(GVars, AVars, DiffVars).
write_out(bp, _GVars, _AVars, _DiffVars) :-
writeln('interface not supported any longer').
%bp(GVars, AVars, DiffVars).
writeln('interface not supported any longer').
write_out(gibbs, GVars, AVars, DiffVars) :-
gibbs(GVars, AVars, DiffVars).
write_out(bnt, GVars, AVars, DiffVars) :-
@ -427,7 +431,7 @@ find_var([_|DVars], V, Key, [_|DKeys]) :-
process_vars([], []).
process_vars([V|Vs], [K|Ks]) :-
process_var(V, K),
process_var(V, K),
process_vars(Vs, Ks).
process_var(V, K) :- get_atts(V, [key(K)]), !.
@ -529,6 +533,15 @@ user:term_expansion((A :- {}), ( :- true )) :- !, % evidence
clpbn_key(Var,Key) :-
get_atts(Var, [key(Key)]).
%
% only useful for probabilistic context free grammars
%
clpbn_init_graph(pcg) :- !,
pcg_init_graph.
clpbn_init_graph(_).
%
% This is a routine to start a solver, called by the learning procedures (ie, em).
@ -544,22 +557,26 @@ clpbn_init_solver(LVs, Vs0, VarsWithUnboundKeys, State) :-
clpbn_init_solver(gibbs, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_gibbs_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(ve, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_ve_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(bp, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_horus_ground_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(jt, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_jt_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(bdd, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_bdd_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(pcg, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_pcg_solver(LVs, Vs0, VarsWithUnboundKeys, State).
%
% LVs is the list of lists of variables to marginalise
% Vs is the full graph
% Ps are the probabilities on LVs.
%
% Ps are the probabilities on LVs.
%
clpbn_run_solver(LVs, LPs, State) :-
solver(Solver),
@ -582,6 +599,13 @@ clpbn_run_solver(bdd, LVs, LPs, State) :-
clpbn_run_solver(pcg, LVs, LPs, State) :-
run_pcg_solver(LVs, LPs, State).
clpbn_finalize_solver(State) :-
solver(bp), !,
functor(State, _, Last),
arg(Last, State, Info),
finalize_horus_ground_solver(Info).
clpbn_finalize_solver(_State).
%
@ -637,19 +661,6 @@ pfl_run_solver(LVs, LPs, State, cbp) :- !,
add_keys(Key1+V1,_Key2,Key1+V1).
%
% only useful for probabilistic context free grammars
%
clpbn_init_graph(pcg) :- !,
pcg_init_graph.
clpbn_init_graph(_).
clpbn_finalize_solver(State) :-
solver(bp), !,
functor(State, _, Last),
arg(Last, State, Info),
finalize_horus_ground_solver(Info).
clpbn_finalize_solver(_State).
probability(Goal, Prob) :-
findall(Prob, do_probability(Goal, [], Prob), [Prob]).
@ -693,20 +704,20 @@ variabilise_last([Arg1,Arg2|Args], Arg, Arg1.NArgs, V) :-
variabilise_last(Arg2.Args, Arg, NArgs, V).
match_probability(VPs, Goal, C, V, Prob) :-
match_probabilities(VPs, Goal, C, V, Prob).
match_probabilities(VPs, Goal, C, V, Prob).
match_probabilities([p(V0=C)=Prob|_], _, C, V, Prob) :-
V0 == V,
!.
V0 == V,
!.
match_probabilities([_|Probs], G, C, V, Prob) :-
match_probabilities(Probs, G, C, V, Prob).
match_probabilities(Probs, G, C, V, Prob).
goal_to_key(_:Goal, Skolem) :-
goal_to_key(Goal, Skolem).
goal_to_key(Goal, Skolem).
goal_to_key(Goal, Skolem) :-
functor(Goal, Na, Ar),
Ar1 is Ar-1,
functor(Skolem, Na, Ar1).
functor(Goal, Na, Ar),
Ar1 is Ar-1,
functor(Skolem, Na, Ar1).
:- use_parfactors(on) -> true ; assert(use_parfactors(off)).

View File

@ -12,6 +12,8 @@
:- use_module(library(maplist)).
:- use_module(library(atts)).
:- attribute posterior/4.

View File

@ -1,40 +1,34 @@
%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(pfl_ground_factors, [
generate_network/5,
f/3
:- module(pfl_ground_factors,
[generate_network/5,
f/3
]).
:- use_module(library(bhash), [
b_hash_new/1,
b_hash_lookup/3,
b_hash_insert/4,
b_hash_to_list/2]).
:- use_module(library(bhash),
[b_hash_new/1,
b_hash_lookup/3,
b_hash_insert/4,
b_hash_to_list/2
]).
:- use_module(library(lists), [
delete/3,
nth0/3,
member/2]).
:- use_module(library(lists),
[member/2]).
:- use_module(library(maplist)).
:- use_module(library(pfl), [
factor/6,
defined_in_factor/2,
skolem/2]).
:- use_module(library(atts)).
:- use_module(library(clpbn/aggregates), [
avg_factors/5]).
:- use_module(library(pfl),
[factor/6,
defined_in_factor/2,
skolem/2
]).
:- use_module(library(clpbn/dists), [
dist/4]).
:- use_module(library(clpbn/aggregates),
[avg_factors/5]).
:- use_module(library(clpbn/dists),
[dist/4]).
:- dynamic currently_defined/1, queue/1, f/4.
@ -59,20 +53,20 @@ generate_network(QueryVars, QueryKeys, Keys, Factors, EList) :-
% clean global stateq
%
init_global_search :-
retractall(queue(_)),
retractall(currently_defined(_)),
retractall(f(_,_,_)).
retractall(queue(_)),
retractall(currently_defined(_)),
retractall(f(_,_,_)).
pair_to_evidence(K-E, K=E).
include_evidence(V, Evidence0, Evidence) :-
clpbn:get_atts(V,[key(K),evidence(E)]), !,
(
b_hash_lookup(K, E1, Evidence0)
b_hash_lookup(K, E1, Evidence0)
->
(E \= E1 -> throw(clpbn:incompatible_evidence(K,E,E1)) ; Evidence = Evidence0)
(E \= E1 -> throw(clpbn:incompatible_evidence(K,E,E1)) ; Evidence = Evidence0)
;
b_hash_insert(Evidence0, K, E, Evidence)
b_hash_insert(Evidence0, K, E, Evidence)
).
include_evidence(_, Evidence, Evidence).
@ -82,11 +76,11 @@ static_evidence(Evidence0, Evidence) :-
include_static_evidence(K=E, Evidence0, Evidence) :-
(
b_hash_lookup(K, E1, Evidence0)
b_hash_lookup(K, E1, Evidence0)
->
(E \= E1 -> throw(incompatible_evidence(K,E,E1)) ; Evidence = Evidence0)
(E \= E1 -> throw(incompatible_evidence(K,E,E1)) ; Evidence = Evidence0)
;
b_hash_insert(Evidence0, K, E, Evidence)
b_hash_insert(Evidence0, K, E, Evidence)
).
@ -122,14 +116,14 @@ propagate.
do_propagate(K) :-
%writeln(-K),
\+ currently_defined(K),
( ground(K) -> assert(currently_defined(K)) ; true),
( ground(K) -> assert(currently_defined(K)) ; true),
(
defined_in_factor(K, ParFactor),
add_factor(ParFactor, Ks)
*->
true
defined_in_factor(K, ParFactor),
add_factor(ParFactor, Ks)
*->
true
;
throw(error(no_defining_factor(K)))
throw(error(no_defining_factor(K)))
),
member(K1, Ks),
\+ currently_defined(K1),
@ -139,25 +133,26 @@ do_propagate(_K) :-
propagate.
add_factor(factor(Type, Id, Ks, _, _Phi, Constraints), NKs) :-
% writeln(+Ks),
( Ks = [K,Els], var(Els)
->
% aggregate factor
once(run(Constraints)),
avg_factors(K, Els, 0.0, NewKeys, NewId),
NKs = [K|NewKeys]
;
run(Constraints),
NKs = Ks,
Id = NewId
% writeln(+Ks),
(
Ks = [K,Els], var(Els)
->
% aggregate factor
once(run(Constraints)),
avg_factors(K, Els, 0.0, NewKeys, NewId),
NKs = [K|NewKeys]
;
run(Constraints),
NKs = Ks,
Id = NewId
),
(
f(Type, NewId, NKs)
->
true
;
assert(f(Type, NewId, NKs))
).
f(Type, NewId, NKs)
->
true
;
assert(f(Type, NewId, NKs))
).
run([Goal|Goals]) :-
call(user:Goal),

View File

@ -21,7 +21,7 @@
cpp_run_ground_solver/3,
cpp_set_vars_information/2,
cpp_free_ground_network/1,
set_solver/1
set_solver/1
]).
:- use_module(library('clpbn/dists'),
@ -34,7 +34,7 @@
:- use_module(library('clpbn/display'),
[clpbn_bind_vals/3]).
:- use_module(library(clpbn/numbers)).
:- use_module(library('clpbn/numbers')).
:- use_module(library(charsio),
[term_to_atom/2]).

View File

@ -21,8 +21,8 @@
init_ve_ground_solver/5,
run_ve_ground_solver/3,
call_ve_ground_solver/6]).
:- attribute size/1, all_diffs/1.
:- use_module(library(atts)).
:- use_module(library(ordsets),
[ord_union/3,
@ -72,6 +72,8 @@
:- use_module(library('clpbn/aggregates'),
[check_for_agg_vars/2]).
:- attribute size/1, all_diffs/1.
%
% uses a bipartite graph where bigraph(Vs, NFs, Fs)

View File

@ -4,67 +4,71 @@
:- module(clpbn_em, [em/5]).
:- use_module(library(lists),
[append/3,
delete/3]).
:- reexport(library(clpbn),
[
clpbn_flag/2,
clpbn_flag/3]).
[clpbn_flag/2,
clpbn_flag/3
]).
:- use_module(library(clpbn),
[clpbn_init_graph/1,
clpbn_init_solver/4,
clpbn_run_solver/3,
pfl_init_solver/5,
pfl_run_solver/3,
clpbn_finalize_solver/1,
conditional_probability/3,
clpbn_flag/2]).
[clpbn_init_graph/1,
clpbn_init_solver/4,
clpbn_run_solver/3,
clpbn_finalize_solver/1,
pfl_init_solver/5,
pfl_run_solver/3,
conditional_probability/3,
clpbn_flag/2
]).
:- use_module(library('clpbn/dists'),
[get_dist_domain_size/2,
empty_dist/2,
dist_new_table/2,
get_dist_key/2,
randomise_all_dists/0,
uniformise_all_dists/0]).
[get_dist_domain_size/2,
empty_dist/2,
dist_new_table/2,
get_dist_key/2,
randomise_all_dists/0,
uniformise_all_dists/0
]).
:- use_module(library(clpbn/ground_factors),
[generate_network/5,
f/3]).
:- use_module(library(bhash), [
b_hash_new/1,
b_hash_lookup/3,
b_hash_insert/4]).
:- use_module(library('clpbn/ground_factors'),
[generate_network/5,
f/3
]).
:- use_module(library('clpbn/utils'),
[check_for_hidden_vars/3,
sort_vars_by_key/3
]).
:- use_module(library('clpbn/learning/learn_utils'),
[run_all/1,
clpbn_vars/2,
normalise_counts/2,
compute_likelihood/3,
soften_sample/2]).
[run_all/1,
clpbn_vars/2,
normalise_counts/2,
compute_likelihood/3,
soften_sample/2
]).
:- use_module(library(bhash),
[b_hash_new/1,
b_hash_lookup/3,
b_hash_insert/4
]).
:- use_module(library(matrix),
[matrix_add/3,
matrix_to_list/2
]).
:- use_module(library(lists),
[member/2]).
[member/2]).
:- use_module(library(rbtrees),
[rb_new/1,
rb_insert/4,
rb_lookup/3
]).
:- use_module(library(maplist)).
:- use_module(library(matrix),
[matrix_add/3,
matrix_to_list/2]).
:- use_module(library(rbtrees),
[rb_new/1,
rb_insert/4,
rb_lookup/3]).
:- use_module(library('clpbn/utils'),
[
check_for_hidden_vars/3,
sort_vars_by_key/3]).
:- meta_predicate em(:,+,+,-,-), init_em(:,-).
@ -101,9 +105,9 @@ init_em(Items, State) :-
% randomise_all_dists,
% set initial values for distributions
uniformise_all_dists,
setup_em_network(Items, Solver, State).
setup_em_network(Items, State).
setup_em_network(Items, Solver, state( AllDists, AllDistInstances, MargKeys, SolverState)) :-
setup_em_network(Items, state(AllDists, AllDistInstances, MargKeys, SolverState)) :-
clpbn:use_parfactors(on), !,
% get all variables to marginalise
run_examples(Items, Keys, Factors, EList),
@ -111,7 +115,7 @@ setup_em_network(Items, Solver, state( AllDists, AllDistInstances, MargKeys, Sol
generate_dists(Factors, EList, AllDists, AllDistInstances, MargKeys),
% setup solver, if necessary
pfl_init_solver(MargKeys, Keys, Factors, EList, SolverState).
setup_em_network(Items, Solver, state( AllDists, AllDistInstances, MargVars, SolverVars)) :-
setup_em_network(Items, state(AllDists, AllDistInstances, MargVars, SolverState)) :-
% create the ground network
call_run_all(Items),
% get all variables to marginalise
@ -121,7 +125,7 @@ setup_em_network(Items, Solver, state( AllDists, AllDistInstances, MargVars, Sol
% remove variables that do not have to do with this query.
different_dists(AllVars, AllDists, AllDistInstances, MargVars),
% setup solver by doing parameter independent work.
clpbn_init_solver(MargVars, AllVars, _, SolverVars).
clpbn_init_solver(MargVars, AllVars, _, SolverState).
run_examples(user:Exs, Keys, Factors, EList) :-
Exs = [_:_|_], !,
@ -232,9 +236,9 @@ all_dists([], _, []).
all_dists([V|AllVars], AllVars0, [i(Id, [V|Parents], Cases, Hiddens)|Dists]) :-
% V is an instance of Id
clpbn:get_atts(V, [dist(Id,Parents)]),
sort([V|Parents], Sorted),
sort([V|Parents], Sorted),
length(Sorted, LengSorted),
length(Parents, LengParents),
length(Parents, LengParents),
(
LengParents+1 =:= LengSorted
->

View File

@ -1,30 +1,31 @@
%
% This module defines PFL, the prolog factor language.
% This module defines PFL, the Prolog Factor Language.
%
%
:- module(pfl, [
op(550,yfx,@),
op(550,yfx,::),
op(1150,fx,bayes),
op(1150,fx,markov),
factor/6,
skolem/2,
defined_in_factor/2,
get_pfl_cpt/5, % given id and keys, return new keys and cpt
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
add_ground_factor/5 %add a new bayesian variable (for now)
]).
:- module(pfl,
[op(550,yfx,@),
op(550,yfx,::),
op(1150,fx,bayes),
op(1150,fx,markov),
factor/6,
skolem/2,
defined_in_factor/2,
get_pfl_cpt/5, % given id and keys, return new keys and cpt
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
add_ground_factor/5 %add a new bayesian variable (for now)
]).
:- reexport(library(clpbn),
[clpbn_flag/2 as pfl_flag,
set_clpbn_flag/2 as set_pfl_flag,
conditional_probability/3,
pfl_init_solver/5,
pfl_run_solver/3]).
pfl_run_solver/3
]).
:- reexport(library(clpbn/horus),
[set_solver/1]).
@ -32,24 +33,25 @@
:- reexport(library(clpbn/aggregates),
[avg_factors/5]).
:- ( % if clp(bn) has done loading, we're top-level
predicate_property(set_pfl_flag(_,_), imported_from(clpbn))
->
->
% we're using factor language
% set appropriate flag
set_pfl_flag(use_factors,on)
;
;
% we're within clp(bn), no need to do anything
true
).
).
:- use_module(library(atts)).
:- use_module(library(lists),
[nth0/3,
append/3,
member/2]).
member/2
]).
:- dynamic factor/6, skolem_in/2, skolem/2, preprocess/3, evidence/2, id/1.
user:term_expansion( bayes((Formula ; Phi ; Constraints)), pfl:factor(bayes,Id,FList,FV,Phi,Constraints)) :-