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, :- module(clpbn,
[{}/1,
clpbn_flag/2, clpbn_flag/2,
set_clpbn_flag/2, set_clpbn_flag/2,
clpbn_flag/3, clpbn_flag/3,
clpbn_key/2, clpbn_key/2,
clpbn_init_graph/1,
clpbn_init_solver/4, clpbn_init_solver/4,
clpbn_run_solver/3, clpbn_run_solver/3,
clpbn_finalize_solver/1,
pfl_init_solver/5, pfl_init_solver/5,
pfl_run_solver/3, pfl_run_solver/3,
clpbn_finalize_solver/1,
clpbn_init_graph/1,
probability/2, probability/2,
conditional_probability/3, conditional_probability/3,
use_parfactors/1, use_parfactors/1,
op( 500, xfy, with)]). op(500, xfy, with)
]).
:- use_module(library(atts)). :- use_module(library(atts)).
:- use_module(library(bhash)). :- use_module(library(bhash)).
:- use_module(library(lists)). :- use_module(library(lists)).
:- use_module(library(terms)). :- use_module(library(terms)).
:- use_module(library(maplist)). :- 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. :- attribute key/1, dist/2, evidence/1.
:- use_module('clpbn/ve', :- use_module('clpbn/ve',
[ve/3, [ve/3,
check_if_ve_done/1, check_if_ve_done/1,
@ -43,6 +39,39 @@
call_ve_ground_solver/6 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', :- use_module('clpbn/horus_ground',
[call_horus_ground_solver/6, [call_horus_ground_solver/6,
check_if_horus_ground_solver_done/1, check_if_horus_ground_solver_done/1,
@ -59,47 +88,8 @@
finalize_horus_lifted_solver/1 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', :- use_module('clpbn/dists',
[ [dist/4,
dist/4,
get_dist/4, get_dist/4,
get_evidence_position/3, get_evidence_position/3,
get_evidence_from_position/3, get_evidence_from_position/3,
@ -107,33 +97,47 @@
]). ]).
:- use_module('clpbn/evidence', :- use_module('clpbn/evidence',
[ [store_evidence/1,
store_evidence/1,
add_stored_evidence/2, add_stored_evidence/2,
incorporate_evidence/2, incorporate_evidence/2,
check_stored_evidence/2, check_stored_evidence/2,
put_evidence/2 put_evidence/2
]). ]).
:- use_module('clpbn/ground_factors',
[generate_network/5]).
:- use_module('clpbn/utils', :- use_module('clpbn/utils',
[ [sort_vars_by_key/3]).
sort_vars_by_key/3
]). :- use_module('clpbn/graphs',
[clpbn2graph/1]).
:- use_module('clpbn/graphviz', :- use_module('clpbn/graphviz',
[clpbn2gviz/4]). [clpbn2gviz/4]).
:- use_module(clpbn/ground_factors,
[generate_network/5]).
%
% avoid the overhead of using goal_expansion/2.
%
:- multifile user:term_expansion/2.
:- dynamic solver/1,output/1,use/1,suppress_attribute_display/1, parameter_softening/1, em_solver/1, use_parfactors/1. :- dynamic user:term_expansion/2.
solver(ve). :- dynamic
em_solver(bp). solver/1,
output/1,
use/1,
suppress_attribute_display/1,
parameter_softening/1,
em_solver/1,
use_parfactors/1.
:- meta_predicate probability(:,-), conditional_probability(:,:,-). :- meta_predicate probability(:,-), conditional_probability(:,:,-).
solver(ve).
em_solver(bp).
%output(xbif(user_error)). %output(xbif(user_error)).
%output(gviz(user_error)). %output(gviz(user_error)).
output(no). output(no).
@ -141,6 +145,7 @@ suppress_attribute_display(false).
parameter_softening(m_estimate(10)). parameter_softening(m_estimate(10)).
use_parfactors(off). use_parfactors(off).
clpbn_flag(Flag,Option) :- clpbn_flag(Flag,Option) :-
clpbn_flag(Flag, Option, Option). clpbn_flag(Flag, Option, Option).
@ -182,7 +187,7 @@ clpbn_flag(use_factors,Before,After) :-
dist(Dist, DistInfo, Key, Parents), dist(Dist, DistInfo, Key, Parents),
add_evidence(Var,Key,DistInfo,El) add_evidence(Var,Key,DistInfo,El)
% ,writeln({Var = Key with Dist}) % ,writeln({Var = Key with Dist})
. .
% %
% make sure a query variable is reachable by the garbage collector. % make sure a query variable is reachable by the garbage collector.
@ -332,7 +337,6 @@ write_out(bdd, GVars, AVars, DiffVars) :-
bdd(GVars, AVars, DiffVars). bdd(GVars, AVars, DiffVars).
write_out(bp, _GVars, _AVars, _DiffVars) :- write_out(bp, _GVars, _AVars, _DiffVars) :-
writeln('interface not supported any longer'). writeln('interface not supported any longer').
%bp(GVars, AVars, DiffVars).
write_out(gibbs, GVars, AVars, DiffVars) :- write_out(gibbs, GVars, AVars, DiffVars) :-
gibbs(GVars, AVars, DiffVars). gibbs(GVars, AVars, DiffVars).
write_out(bnt, GVars, AVars, DiffVars) :- write_out(bnt, GVars, AVars, DiffVars) :-
@ -530,6 +534,15 @@ user:term_expansion((A :- {}), ( :- true )) :- !, % evidence
clpbn_key(Var,Key) :- clpbn_key(Var,Key) :-
get_atts(Var, [key(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). % This is a routine to start a solver, called by the learning procedures (ie, em).
% LVs is a list of lists of variables one is interested in eventually marginalising out % LVs is a list of lists of variables one is interested in eventually marginalising out
@ -544,14 +557,19 @@ clpbn_init_solver(LVs, Vs0, VarsWithUnboundKeys, State) :-
clpbn_init_solver(gibbs, LVs, Vs0, VarsWithUnboundKeys, State) :- clpbn_init_solver(gibbs, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_gibbs_solver(LVs, Vs0, VarsWithUnboundKeys, State). init_gibbs_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(ve, LVs, Vs0, VarsWithUnboundKeys, State) :- clpbn_init_solver(ve, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_ve_solver(LVs, Vs0, VarsWithUnboundKeys, State). init_ve_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(bp, LVs, Vs0, VarsWithUnboundKeys, State) :- clpbn_init_solver(bp, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_horus_ground_solver(LVs, Vs0, VarsWithUnboundKeys, State). init_horus_ground_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(jt, LVs, Vs0, VarsWithUnboundKeys, State) :- clpbn_init_solver(jt, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_jt_solver(LVs, Vs0, VarsWithUnboundKeys, State). init_jt_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(bdd, LVs, Vs0, VarsWithUnboundKeys, State) :- clpbn_init_solver(bdd, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_bdd_solver(LVs, Vs0, VarsWithUnboundKeys, State). init_bdd_solver(LVs, Vs0, VarsWithUnboundKeys, State).
clpbn_init_solver(pcg, LVs, Vs0, VarsWithUnboundKeys, State) :- clpbn_init_solver(pcg, LVs, Vs0, VarsWithUnboundKeys, State) :-
init_pcg_solver(LVs, Vs0, VarsWithUnboundKeys, State). init_pcg_solver(LVs, Vs0, VarsWithUnboundKeys, State).
@ -560,7 +578,6 @@ clpbn_init_solver(pcg, LVs, Vs0, VarsWithUnboundKeys, State) :-
% Vs is the full graph % Vs is the full graph
% Ps are the probabilities on LVs. % Ps are the probabilities on LVs.
% %
%
clpbn_run_solver(LVs, LPs, State) :- clpbn_run_solver(LVs, LPs, State) :-
solver(Solver), solver(Solver),
clpbn_run_solver(Solver, LVs, LPs, State). clpbn_run_solver(Solver, LVs, LPs, State).
@ -583,6 +600,13 @@ clpbn_run_solver(bdd, LVs, LPs, State) :-
clpbn_run_solver(pcg, LVs, LPs, State) :- clpbn_run_solver(pcg, LVs, LPs, State) :-
run_pcg_solver(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).
% %
% This is a routine to start a solver, called by the learning procedures (ie, em). % This is a routine to start a solver, called by the learning procedures (ie, em).
@ -637,19 +661,6 @@ pfl_run_solver(LVs, LPs, State, cbp) :- !,
add_keys(Key1+V1,_Key2,Key1+V1). 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) :- probability(Goal, Prob) :-
findall(Prob, do_probability(Goal, [], Prob), [Prob]). findall(Prob, do_probability(Goal, [], Prob), [Prob]).

View File

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

View File

@ -1,40 +1,34 @@
%parfactor( :- module(pfl_ground_factors,
% [ability(P),grade(C,S), satisfaction(C,S,P)], [generate_network/5,
% \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 f/3
]). ]).
:- use_module(library(bhash), [ :- use_module(library(bhash),
b_hash_new/1, [b_hash_new/1,
b_hash_lookup/3, b_hash_lookup/3,
b_hash_insert/4, b_hash_insert/4,
b_hash_to_list/2]). b_hash_to_list/2
]).
:- use_module(library(lists), [ :- use_module(library(lists),
delete/3, [member/2]).
nth0/3,
member/2]).
:- use_module(library(maplist)). :- use_module(library(maplist)).
:- use_module(library(pfl), [ :- use_module(library(atts)).
factor/6,
:- use_module(library(pfl),
[factor/6,
defined_in_factor/2, defined_in_factor/2,
skolem/2]). skolem/2
]).
:- use_module(library(clpbn/aggregates), [ :- use_module(library(clpbn/aggregates),
avg_factors/5]). [avg_factors/5]).
:- use_module(library(clpbn/dists), [ :- use_module(library(clpbn/dists),
dist/4]). [dist/4]).
:- dynamic currently_defined/1, queue/1, f/4. :- dynamic currently_defined/1, queue/1, f/4.
@ -140,7 +134,8 @@ do_propagate(_K) :-
add_factor(factor(Type, Id, Ks, _, _Phi, Constraints), NKs) :- add_factor(factor(Type, Id, Ks, _, _Phi, Constraints), NKs) :-
% writeln(+Ks), % writeln(+Ks),
( Ks = [K,Els], var(Els) (
Ks = [K,Els], var(Els)
-> ->
% aggregate factor % aggregate factor
once(run(Constraints)), once(run(Constraints)),

View File

@ -34,7 +34,7 @@
:- use_module(library('clpbn/display'), :- use_module(library('clpbn/display'),
[clpbn_bind_vals/3]). [clpbn_bind_vals/3]).
:- use_module(library(clpbn/numbers)). :- use_module(library('clpbn/numbers')).
:- use_module(library(charsio), :- use_module(library(charsio),
[term_to_atom/2]). [term_to_atom/2]).

View File

@ -22,7 +22,7 @@
run_ve_ground_solver/3, run_ve_ground_solver/3,
call_ve_ground_solver/6]). call_ve_ground_solver/6]).
:- attribute size/1, all_diffs/1. :- use_module(library(atts)).
:- use_module(library(ordsets), :- use_module(library(ordsets),
[ord_union/3, [ord_union/3,
@ -73,6 +73,8 @@
:- use_module(library('clpbn/aggregates'), :- use_module(library('clpbn/aggregates'),
[check_for_agg_vars/2]). [check_for_agg_vars/2]).
:- attribute size/1, all_diffs/1.
% %
% uses a bipartite graph where bigraph(Vs, NFs, Fs) % uses a bipartite graph where bigraph(Vs, NFs, Fs)
% Vs=map variables to lists of factors % Vs=map variables to lists of factors

View File

@ -4,24 +4,21 @@
:- module(clpbn_em, [em/5]). :- module(clpbn_em, [em/5]).
:- use_module(library(lists),
[append/3,
delete/3]).
:- reexport(library(clpbn), :- reexport(library(clpbn),
[ [clpbn_flag/2,
clpbn_flag/2, clpbn_flag/3
clpbn_flag/3]). ]).
:- use_module(library(clpbn), :- use_module(library(clpbn),
[clpbn_init_graph/1, [clpbn_init_graph/1,
clpbn_init_solver/4, clpbn_init_solver/4,
clpbn_run_solver/3, clpbn_run_solver/3,
clpbn_finalize_solver/1,
pfl_init_solver/5, pfl_init_solver/5,
pfl_run_solver/3, pfl_run_solver/3,
clpbn_finalize_solver/1,
conditional_probability/3, conditional_probability/3,
clpbn_flag/2]). clpbn_flag/2
]).
:- use_module(library('clpbn/dists'), :- use_module(library('clpbn/dists'),
[get_dist_domain_size/2, [get_dist_domain_size/2,
@ -29,42 +26,49 @@
dist_new_table/2, dist_new_table/2,
get_dist_key/2, get_dist_key/2,
randomise_all_dists/0, randomise_all_dists/0,
uniformise_all_dists/0]). uniformise_all_dists/0
]).
:- use_module(library(clpbn/ground_factors), :- use_module(library('clpbn/ground_factors'),
[generate_network/5, [generate_network/5,
f/3]). f/3
]).
:- use_module(library(bhash), [ :- use_module(library('clpbn/utils'),
b_hash_new/1, [check_for_hidden_vars/3,
b_hash_lookup/3, sort_vars_by_key/3
b_hash_insert/4]). ]).
:- use_module(library('clpbn/learning/learn_utils'), :- use_module(library('clpbn/learning/learn_utils'),
[run_all/1, [run_all/1,
clpbn_vars/2, clpbn_vars/2,
normalise_counts/2, normalise_counts/2,
compute_likelihood/3, compute_likelihood/3,
soften_sample/2]). 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), :- use_module(library(lists),
[member/2]). [member/2]).
:- use_module(library(maplist)).
:- use_module(library(matrix),
[matrix_add/3,
matrix_to_list/2]).
:- use_module(library(rbtrees), :- use_module(library(rbtrees),
[rb_new/1, [rb_new/1,
rb_insert/4, rb_insert/4,
rb_lookup/3]). rb_lookup/3
]).
:- use_module(library(maplist)).
:- use_module(library('clpbn/utils'),
[
check_for_hidden_vars/3,
sort_vars_by_key/3]).
:- meta_predicate em(:,+,+,-,-), init_em(:,-). :- meta_predicate em(:,+,+,-,-), init_em(:,-).
@ -101,9 +105,9 @@ init_em(Items, State) :-
% randomise_all_dists, % randomise_all_dists,
% set initial values for distributions % set initial values for distributions
uniformise_all_dists, 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), !, clpbn:use_parfactors(on), !,
% get all variables to marginalise % get all variables to marginalise
run_examples(Items, Keys, Factors, EList), 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), generate_dists(Factors, EList, AllDists, AllDistInstances, MargKeys),
% setup solver, if necessary % setup solver, if necessary
pfl_init_solver(MargKeys, Keys, Factors, EList, SolverState). 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 % create the ground network
call_run_all(Items), call_run_all(Items),
% get all variables to marginalise % 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. % remove variables that do not have to do with this query.
different_dists(AllVars, AllDists, AllDistInstances, MargVars), different_dists(AllVars, AllDists, AllDistInstances, MargVars),
% setup solver by doing parameter independent work. % 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) :- run_examples(user:Exs, Keys, Factors, EList) :-
Exs = [_:_|_], !, Exs = [_:_|_], !,

View File

@ -1,10 +1,10 @@
% %
% This module defines PFL, the prolog factor language. % This module defines PFL, the Prolog Factor Language.
% %
% %
:- module(pfl, [ :- module(pfl,
op(550,yfx,@), [op(550,yfx,@),
op(550,yfx,::), op(550,yfx,::),
op(1150,fx,bayes), op(1150,fx,bayes),
op(1150,fx,markov), op(1150,fx,markov),
@ -24,7 +24,8 @@
set_clpbn_flag/2 as set_pfl_flag, set_clpbn_flag/2 as set_pfl_flag,
conditional_probability/3, conditional_probability/3,
pfl_init_solver/5, pfl_init_solver/5,
pfl_run_solver/3]). pfl_run_solver/3
]).
:- reexport(library(clpbn/horus), :- reexport(library(clpbn/horus),
[set_solver/1]). [set_solver/1]).
@ -32,7 +33,6 @@
:- reexport(library(clpbn/aggregates), :- reexport(library(clpbn/aggregates),
[avg_factors/5]). [avg_factors/5]).
:- ( % if clp(bn) has done loading, we're top-level :- ( % if clp(bn) has done loading, we're top-level
predicate_property(set_pfl_flag(_,_), imported_from(clpbn)) predicate_property(set_pfl_flag(_,_), imported_from(clpbn))
-> ->
@ -44,11 +44,13 @@
true true
). ).
:- use_module(library(atts)).
:- use_module(library(lists), :- use_module(library(lists),
[nth0/3, [nth0/3,
append/3, append/3,
member/2]). member/2
]).
:- dynamic factor/6, skolem_in/2, skolem/2, preprocess/3, evidence/2, id/1. :- dynamic factor/6, skolem_in/2, skolem/2, preprocess/3, evidence/2, id/1.