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,
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,
clpbn_finalize_solver/1,
clpbn_init_graph/1,
probability/2,
conditional_probability/3,
use_parfactors/1,
op( 500, xfy, with)]).
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]).
%
% 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).
em_solver(bp).
:- 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).
%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.
@ -332,7 +337,6 @@ 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).
write_out(gibbs, GVars, AVars, DiffVars) :-
gibbs(GVars, AVars, DiffVars).
write_out(bnt, GVars, AVars, DiffVars) :-
@ -530,6 +534,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).
% 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) :-
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).
@ -560,7 +578,6 @@ clpbn_init_solver(pcg, LVs, Vs0, VarsWithUnboundKeys, State) :-
% Vs is the full graph
% Ps are the probabilities on LVs.
%
%
clpbn_run_solver(LVs, LPs, State) :-
solver(Solver),
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) :-
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).
@ -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]).

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,
:- module(pfl_ground_factors,
[generate_network/5,
f/3
]).
:- use_module(library(bhash), [
b_hash_new/1,
:- use_module(library(bhash),
[b_hash_new/1,
b_hash_lookup/3,
b_hash_insert/4,
b_hash_to_list/2]).
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,
:- use_module(library(atts)).
:- use_module(library(pfl),
[factor/6,
defined_in_factor/2,
skolem/2]).
skolem/2
]).
:- use_module(library(clpbn/aggregates), [
avg_factors/5]).
:- use_module(library(clpbn/aggregates),
[avg_factors/5]).
:- use_module(library(clpbn/dists), [
dist/4]).
:- use_module(library(clpbn/dists),
[dist/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) :-
% writeln(+Ks),
( Ks = [K,Els], var(Els)
(
Ks = [K,Els], var(Els)
->
% aggregate factor
once(run(Constraints)),

View File

@ -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

@ -22,7 +22,7 @@
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,
@ -73,6 +73,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)
% Vs=map variables to lists of factors

View File

@ -4,24 +4,21 @@
:- 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,
clpbn_finalize_solver/1,
pfl_init_solver/5,
pfl_run_solver/3,
clpbn_finalize_solver/1,
conditional_probability/3,
clpbn_flag/2]).
clpbn_flag/2
]).
:- use_module(library('clpbn/dists'),
[get_dist_domain_size/2,
@ -29,42 +26,49 @@
dist_new_table/2,
get_dist_key/2,
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,
f/3]).
f/3
]).
:- use_module(library(bhash), [
b_hash_new/1,
b_hash_lookup/3,
b_hash_insert/4]).
:- 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]).
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]).
:- 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]).
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(:,-).
@ -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 = [_:_|_], !,

View File

@ -1,10 +1,10 @@
%
% This module defines PFL, the prolog factor language.
% This module defines PFL, the Prolog Factor Language.
%
%
:- module(pfl, [
op(550,yfx,@),
:- module(pfl,
[op(550,yfx,@),
op(550,yfx,::),
op(1150,fx,bayes),
op(1150,fx,markov),
@ -24,7 +24,8 @@
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,7 +33,6 @@
:- 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))
->
@ -44,11 +44,13 @@
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.