Merge branch 'master' of github.com:tacgomes/yap6.3

This commit is contained in:
Tiago Gomes 2012-05-24 11:59:29 +01:00
commit 7426dfb056
7 changed files with 148 additions and 147 deletions

View File

@ -42,19 +42,19 @@ CLPBN_PROGRAMS= \
$(CLPBN_SRCDIR)/aggregates.yap \
$(CLPBN_SRCDIR)/bdd.yap \
$(CLPBN_SRCDIR)/bnt.yap \
$(CLPBN_SRCDIR)/bp.yap \
$(CLPBN_SRCDIR)/connected.yap \
$(CLPBN_SRCDIR)/discrete_utils.yap \
$(CLPBN_SRCDIR)/display.yap \
$(CLPBN_SRCDIR)/dists.yap \
$(CLPBN_SRCDIR)/evidence.yap \
$(CLPBN_SRCDIR)/fove.yap \
$(CLPBN_SRCDIR)/gibbs.yap \
$(CLPBN_SRCDIR)/graphs.yap \
$(CLPBN_SRCDIR)/graphviz.yap \
$(CLPBN_SRCDIR)/ground_factors.yap \
$(CLPBN_SRCDIR)/hmm.yap \
$(CLPBN_SRCDIR)/horus.yap \
$(CLPBN_SRCDIR)/horus_ground.yap \
$(CLPBN_SRCDIR)/horus_lifted.yap \
$(CLPBN_SRCDIR)/jt.yap \
$(CLPBN_SRCDIR)/matrix_cpt_utils.yap \
$(CLPBN_SRCDIR)/pgrammar.yap \
@ -94,8 +94,16 @@ CLPBN_HMMER_EXAMPLES= \
$(CLPBN_EXDIR)/HMMer/score.yap
CLPBN_EXAMPLES= \
$(CLPBN_EXDIR)/burglary-alarm.fg \
$(CLPBN_EXDIR)/burglary-alarm.yap \
$(CLPBN_EXDIR)/burglary-alarm.uai \
$(CLPBN_EXDIR)/cg.yap \
$(CLPBN_EXDIR)/sprinkler.yap
$(CLPBN_EXDIR)/city.yap \
$(CLPBN_EXDIR)/comp_workshops.yap \
$(CLPBN_EXDIR)/social_domain1.yap \
$(CLPBN_EXDIR)/social_domain2.yap \
$(CLPBN_EXDIR)/sprinkler.yap \
$(CLPBN_EXDIR)/workshop_attrs.yap
install: $(CLBN_TOP) $(CLBN_PROGRAMS) $(CLPBN_PROGRAMS)

View File

@ -1,5 +1,4 @@
:- module(clpbn, [{}/1,
clpbn_flag/2,
set_clpbn_flag/2,
@ -39,24 +38,22 @@
run_ve_solver/3
]).
:- use_module('clpbn/bp',
[bp/3,
check_if_bp_done/1,
init_bp_solver/4,
run_bp_solver/3,
call_bp_ground/6,
finalize_bp_solver/1
:- use_module('clpbn/horus_ground',
[call_horus_ground_solver/6,
check_if_horus_ground_solver_done/1,
init_horus_ground_solver/4,
run_horus_ground_solver/3,
finalize_horus_ground_solver/1
]).
:- use_module('clpbn/fove',
[fove/3,
check_if_fove_done/1,
init_fove_solver/4,
run_fove_solver/3,
finalize_fove_solver/1
:- use_module('clpbn/horus_lifted',
[call_horus_lifted_solver/3,
check_if_horus_lifted_solver_done/1,
init_horus_lifted_solver/4,
run_horus_lifted_solver/3,
finalize_horus_lifted_solver/1
]).
:- use_module('clpbn/jt',
[jt/3,
init_jt_solver/4,
@ -302,18 +299,19 @@ write_out(jt, GVars, AVars, DiffVars) :-
jt(GVars, AVars, DiffVars).
write_out(bdd, GVars, AVars, DiffVars) :-
bdd(GVars, AVars, DiffVars).
write_out(bp, GVars, AVars, DiffVars) :-
bp(GVars, AVars, DiffVars).
write_out(bp, _GVars, _AVars, _DiffVars) :-
writeln('interface not supported anymore').
%bp(GVars, AVars, DiffVars).
write_out(gibbs, GVars, AVars, DiffVars) :-
gibbs(GVars, AVars, DiffVars).
write_out(bnt, GVars, AVars, DiffVars) :-
do_bnt(GVars, AVars, DiffVars).
write_out(fove, GVars, AVars, DiffVars) :-
fove(GVars, AVars, DiffVars).
call_horus_lifted_solver(GVars, AVars, DiffVars).
% call a solver with keys, not actual variables
call_ground_solver(bp, GVars, GoalKeys, Keys, Factors, Evidence, Answ) :-
call_bp_ground(GVars, GoalKeys, Keys, Factors, Evidence, Answ).
call_horus_ground_solver(GVars, GoalKeys, Keys, Factors, Evidence, Answ).
get_bnode(Var, Goal) :-
@ -396,7 +394,7 @@ bind_clpbn(_, Var, _, _, _, _, []) :-
check_if_ve_done(Var), !.
bind_clpbn(_, Var, _, _, _, _, []) :-
use(bp),
check_if_bp_done(Var), !.
check_if_horus_ground_solver_done(Var), !.
bind_clpbn(_, Var, _, _, _, _, []) :-
use(jt),
check_if_ve_done(Var), !.
@ -471,7 +469,7 @@ clpbn_init_solver(gibbs, 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_bp_solver(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) :-
@ -497,7 +495,7 @@ clpbn_run_solver(ve, LVs, LPs, State) :-
run_ve_solver(LVs, LPs, State).
clpbn_run_solver(bp, LVs, LPs, State) :-
run_bp_solver(LVs, LPs, State).
run_horus_ground_solver(LVs, LPs, State).
clpbn_run_solver(jt, LVs, LPs, State) :-
run_jt_solver(LVs, LPs, State).
@ -518,7 +516,7 @@ clpbn_finalize_solver(State) :-
solver(bp), !,
functor(State, _, Last),
arg(Last, State, Info),
finalize_bp_solver(Info).
finalize_horus_ground_solver(Info).
clpbn_finalize_solver(_State).
probability(Goal, Prob) :-

View File

@ -1,22 +1,22 @@
/*******************************************************
Interface with C++
Horus Interface
********************************************************/
:- module(clpbn_horus,
[set_solver/1,
create_lifted_network/3,
create_ground_network/4,
set_parfactors_params/2,
set_factors_params/2,
run_lifted_solver/3,
run_ground_solver/3,
set_vars_information/2,
set_horus_flag/2,
free_parfactors/1,
free_ground_network/1
set_horus_flag/1,
cpp_create_lifted_network/3,
cpp_create_ground_network/4,
cpp_set_parfactors_params/2,
cpp_set_factors_params/2,
cpp_run_lifted_solver/3,
cpp_run_ground_solver/3,
cpp_set_vars_information/2,
cpp_set_horus_flag/2,
cpp_free_parfactors/1,
cpp_free_ground_network/1
]).
@ -25,7 +25,7 @@
patch_things_up :-
assert_static(clpbn_horus:set_horus_flag(_,_)).
assert_static(clpbn_horus:cpp_set_horus_flag(_,_)).
warning :-
@ -40,24 +40,27 @@ set_solver(ve) :- set_pfl_flag(solver,ve).
set_solver(jt) :- set_pfl_flag(solver,jt).
set_solver(gibbs) :- set_pfl_flag(solver,gibbs).
set_solver(fove) :- set_pfl_flag(solver,fove).
set_solver(hve) :- set_pfl_flag(solver,bp), set_horus_flag(inf_alg, ve).
set_solver(bp) :- set_pfl_flag(solver,bp), set_horus_flag(inf_alg, bp).
set_solver(cbp) :- set_pfl_flag(solver,bp), set_horus_flag(inf_alg, cbp).
set_solver(hve) :- set_pfl_flag(solver,bp), cpp_set_horus_flag(inf_alg, ve).
set_solver(bp) :- set_pfl_flag(solver,bp), cpp_set_horus_flag(inf_alg, bp).
set_solver(cbp) :- set_pfl_flag(solver,bp), cpp_set_horus_flag(inf_alg, cbp).
set_solver(S) :- throw(error('unknow solver ', S)).
%:- set_horus_flag(inf_alg, ve).
%:- set_horus_flag(inf_alg, bp).
%: -set_horus_flag(inf_alg, cbp).
:- set_horus_flag(schedule, seq_fixed).
%:- set_horus_flag(schedule, seq_random).
%:- set_horus_flag(schedule, parallel).
%:- set_horus_flag(schedule, max_residual).
set_horus_flag(K,V) :- cpp_set_horus_flag(K,V).
:- set_horus_flag(accuracy, 0.0001).
%:- cpp_set_horus_flag(inf_alg, ve).
%:- cpp_set_horus_flag(inf_alg, bp).
%: -cpp_set_horus_flag(inf_alg, cbp).
:- set_horus_flag(max_iter, 1000).
:- cpp_set_horus_flag(schedule, seq_fixed).
%:- cpp_set_horus_flag(schedule, seq_random).
%:- cpp_set_horus_flag(schedule, parallel).
%:- cpp_set_horus_flag(schedule, max_residual).
:- set_horus_flag(use_logarithms, false).
% :- set_horus_flag(use_logarithms, true).
:- cpp_set_horus_flag(accuracy, 0.0001).
:- cpp_set_horus_flag(max_iter, 1000).
:- cpp_set_horus_flag(use_logarithms, false).
% :- cpp_set_horus_flag(use_logarithms, true).

View File

@ -1,19 +1,27 @@
/*******************************************************
Belief Propagation and Variable Elimination Interface
Interface to Horus Ground Solvers. Used by:
- Variable Elimination
- Belief Propagation
- Counting Belief Propagation
********************************************************/
:- module(clpbn_bp,
[bp/3,
check_if_bp_done/1,
init_bp_solver/4,
run_bp_solver/3,
call_bp_ground/6,
finalize_bp_solver/1
:- module(clpbn_horus_ground,
[call_horus_ground_solver/6,
check_if_horus_ground_solver_done/1,
init_horus_ground_solver/4,
run_horus_ground_solver/3,
finalize_horus_ground_solver/1
]).
:- use_module(horus,
[cpp_create_ground_network/4,
cpp_set_factors_params/2,
cpp_run_ground_solver/3,
cpp_set_vars_information/2,
cpp_free_ground_network/1
]).
:- use_module(library('clpbn/dists'),
[dist/4,
@ -22,25 +30,20 @@
get_dist_params/2
]).
:- use_module(library('clpbn/display'),
[clpbn_bind_vals/3]).
:- use_module(library('clpbn/aggregates'),
[check_for_agg_vars/2]).
:- use_module(library(charsio),
[term_to_atom/2]).
:- use_module(library(pfl),
[skolem/2,
get_pfl_parameters/2
]).
:- use_module(library(lists)).
:- use_module(library(atts)).
@ -48,16 +51,7 @@
:- use_module(library(bhash)).
:- use_module(horus,
[create_ground_network/4,
set_factors_params/2,
run_ground_solver/3,
set_vars_information/2,
free_ground_network/1
]).
call_bp_ground(QueryVars, QueryKeys, AllKeys, Factors, Evidence, Output) :-
call_horus_ground_solver(QueryVars, QueryKeys, AllKeys, Factors, Evidence, Output) :-
b_hash_new(Hash0),
keys_to_ids(AllKeys, 0, Hash0, Hash),
get_factors_type(Factors, Type),
@ -70,23 +64,23 @@ call_bp_ground(QueryVars, QueryKeys, AllKeys, Factors, Evidence, Output) :-
%writeln(factorIds:FactorIds), writeln(''),
%writeln(evidence:Evidence), writeln(''),
%writeln(evidenceIds:EvidenceIds), writeln(''),
create_ground_network(Type, FactorIds, EvidenceIds, Network),
cpp_create_ground_network(Type, FactorIds, EvidenceIds, Network),
%get_vars_information(AllKeys, StatesNames),
%terms_to_atoms(AllKeys, KeysAtoms),
%set_vars_information(KeysAtoms, StatesNames),
%cpp_set_vars_information(KeysAtoms, StatesNames),
run_solver(ground(Network,Hash), QueryKeys, Solutions),
clpbn_bind_vals([QueryVars], Solutions, Output),
free_ground_network(Network).
cpp_free_ground_network(Network).
run_solver(ground(Network,Hash), QueryKeys, Solutions) :-
%get_dists_parameters(DistIds, DistsParams),
%set_factors_params(Network, DistsParams),
%cpp_set_factors_params(Network, DistsParams),
list_of_keys_to_ids(QueryKeys, Hash, QueryIds),
%writeln(queryKeys:QueryKeys), writeln(''),
%writeln(queryIds:QueryIds), writeln(''),
list_of_keys_to_ids(QueryKeys, Hash, QueryIds),
run_ground_solver(Network, [QueryIds], Solutions).
cpp_run_ground_solver(Network, [QueryIds], Solutions).
keys_to_ids([], _, Hash, Hash).
@ -114,7 +108,7 @@ factors_to_ids([f(_, DistId, Keys, CPT)|Fs], Hash, [f(Ids, Ranges, CPT, DistId)|
get_ranges([],[]).
get_ranges(K.Ks, Range.Rs) :- !,
get_ranges(K.Ks, Range.Rs) :- !,
skolem(K,Domain),
length(Domain,Range),
get_ranges(Ks, Rs).
@ -138,31 +132,34 @@ terms_to_atoms(K.Ks, Atom.As) :-
terms_to_atoms(Ks,As).
finalize_bp_solver(bp(Network, _)) :-
free_ground_network(Network).
finalize_horus_ground_solver(bp(Network, _)) :-
cpp_free_ground_network(Network).
bp([[]],_,_) :- !.
bp([QueryVars], AllVars, Output) :-
init_bp_solver(_, AllVars, _, Network),
run_bp_solver([QueryVars], LPs, Network),
finalize_bp_solver(Network),
clpbn_bind_vals([QueryVars], LPs, Output).
init_horus_ground_solver(_, _AllVars0, _, bp(_BayesNet, _DistIds)) :- !.
run_horus_ground_solver(_QueryVars, _Solutions, bp(_Network, _DistIds)) :- !.
init_bp_solver(_, AllVars0, _, bp(BayesNet, DistIds)) :-
%check_for_agg_vars(AllVars0, AllVars),
get_vars_info(AllVars0, VarsInfo, DistIds0),
sort(DistIds0, DistIds),
create_ground_network(VarsInfo, BayesNet),
true.
run_bp_solver(QueryVars, Solutions, bp(Network, DistIds)) :-
get_dists_parameters(DistIds, DistsParams),
set_factors_params(Network, DistsParams),
vars_to_ids(QueryVars, QueryVarsIds),
run_ground_solver(Network, QueryVarsIds, Solutions).
%bp([[]],_,_) :- !.
%bp([QueryVars], AllVars, Output) :-
% init_horus_ground_solver(_, AllVars, _, Network),
% run_horus_ground_solver([QueryVars], LPs, Network),
% finalize_horus_ground_solver(Network),
% clpbn_bind_vals([QueryVars], LPs, Output).
%
%init_horus_ground_solver(_, AllVars0, _, bp(BayesNet, DistIds)) :-
% %check_for_agg_vars(AllVars0, AllVars),
% get_vars_info(AllVars0, VarsInfo, DistIds0),
% sort(DistIds0, DistIds),
% cpp_create_ground_network(VarsInfo, BayesNet),
% true.
%
%
%run_horus_ground_solver(QueryVars, Solutions, bp(Network, DistIds)) :-
% get_dists_parameters(DistIds, DistsParams),
% cpp_set_factors_params(Network, DistsParams),
% vars_to_ids(QueryVars, QueryVarsIds),
% cpp_run_ground_solver(Network, QueryVarsIds, Solutions).
get_dists_parameters([],[]).

View File

@ -1,27 +1,31 @@
/*******************************************************
First Order Variable Elimination Interface
Interface to Horus Lifted Solvers. Used by:
- Lifted Variable Elimination
********************************************************/
:- module(clpbn_fove,
[fove/3,
check_if_fove_done/1,
init_fove_solver/4,
run_fove_solver/3,
finalize_fove_solver/1
:- module(clpbn_horus_lifted,
[call_horus_lifted_solver/3,
check_if_horus_lifted_solver_done/1,
init_horus_lifted_solver/4,
run_horus_lifted_solver/3,
finalize_horus_lifted_solver/1
]).
:- use_module(horus,
[cpp_create_lifted_network/3,
cpp_set_parfactors_params/2,
cpp_run_lifted_solver/3,
cpp_free_parfactors/1
]).
:- use_module(library('clpbn/display'),
[clpbn_bind_vals/3]).
:- use_module(library('clpbn/dists'),
[get_dist_params/2]).
:- use_module(library(pfl),
[factor/6,
skolem/2,
@ -29,30 +33,22 @@
]).
:- use_module(horus,
[create_lifted_network/3,
set_parfactors_params/2,
run_lifted_solver/3,
free_parfactors/1
]).
fove([[]], _, _) :- !.
fove([QueryVars], AllVars, Output) :-
init_fove_solver(_, AllVars, _, ParfactorList),
run_fove_solver([QueryVars], LPs, ParfactorList),
finalize_fove_solver(ParfactorList),
call_horus_lifted_solver([[]], _, _) :- !.
call_horus_lifted_solver([QueryVars], AllVars, Output) :-
init_horus_lifted_solver(_, AllVars, _, ParfactorList),
run_horus_lifted_solver([QueryVars], LPs, ParfactorList),
finalize_horus_lifted_solver(ParfactorList),
clpbn_bind_vals([QueryVars], LPs, Output).
init_fove_solver(_, AllAttVars, _, fove(ParfactorList, DistIds)) :-
init_horus_lifted_solver(_, AllAttVars, _, fove(ParfactorList, DistIds)) :-
get_parfactors(Parfactors),
get_dist_ids(Parfactors, DistIds0),
sort(DistIds0, DistIds),
get_observed_vars(AllAttVars, ObservedVars),
%writeln(parfactors:Parfactors:'\n'),
%writeln(evidence:ObservedVars:'\n'),
create_lifted_network(Parfactors,ObservedVars,ParfactorList).
cpp_create_lifted_network(Parfactors,ObservedVars,ParfactorList).
:- table get_parfactors/1.
@ -138,15 +134,15 @@ get_dists_parameters([Id|Ids], [dist(Id, Params)|DistsInfo]) :-
get_dists_parameters(Ids, DistsInfo).
run_fove_solver(QueryVarsAtts, Solutions, fove(ParfactorList, DistIds)) :-
run_horus_lifted_solver(QueryVarsAtts, Solutions, fove(ParfactorList, DistIds)) :-
get_query_vars(QueryVarsAtts, QueryVars),
%writeln(queryVars:QueryVars), writeln(''),
get_dists_parameters(DistIds, DistsParams),
%writeln(dists:DistsParams), writeln(''),
set_parfactors_params(ParfactorList, DistsParams),
run_lifted_solver(ParfactorList, QueryVars, Solutions).
cpp_set_parfactors_params(ParfactorList, DistsParams),
cpp_run_lifted_solver(ParfactorList, QueryVars, Solutions).
finalize_fove_solver(fove(ParfactorList, _)) :-
free_parfactors(ParfactorList).
finalize_horus_lifted_solver(fove(ParfactorList, _)) :-
cpp_free_parfactors(ParfactorList).

View File

@ -197,7 +197,6 @@ void readLiftedEvidence (
}
unsigned evidence = (unsigned) YAP_IntOfTerm (YAP_ArgOfTerm (2, pair));
bool found = false;
cout << "has evidence()" << endl;
for (unsigned i = 0; i < obsFormulas.size(); i++) {
if (obsFormulas[i].functor() == functor &&
obsFormulas[i].arity() == args.size() &&
@ -572,15 +571,15 @@ freeParfactors (void)
extern "C" void
init_predicates (void)
{
YAP_UserCPredicate ("create_lifted_network", createLiftedNetwork, 3);
YAP_UserCPredicate ("create_ground_network", createGroundNetwork, 4);
YAP_UserCPredicate ("run_lifted_solver", runLiftedSolver, 3);
YAP_UserCPredicate ("run_ground_solver", runGroundSolver, 3);
YAP_UserCPredicate ("set_parfactors_params", setParfactorsParams, 2);
YAP_UserCPredicate ("set_factors_params", setFactorsParams, 2);
YAP_UserCPredicate ("set_vars_information", setVarsInformation, 2);
YAP_UserCPredicate ("set_horus_flag", setHorusFlag, 2);
YAP_UserCPredicate ("free_parfactors", freeParfactors, 1);
YAP_UserCPredicate ("free_ground_network", freeGroundNetwork, 1);
YAP_UserCPredicate ("cpp_create_lifted_network", createLiftedNetwork, 3);
YAP_UserCPredicate ("cpp_create_ground_network", createGroundNetwork, 4);
YAP_UserCPredicate ("cpp_run_lifted_solver", runLiftedSolver, 3);
YAP_UserCPredicate ("cpp_run_ground_solver", runGroundSolver, 3);
YAP_UserCPredicate ("cpp_set_parfactors_params", setParfactorsParams, 2);
YAP_UserCPredicate ("cpp_cpp_set_factors_params", setFactorsParams, 2);
YAP_UserCPredicate ("cpp_set_vars_information", setVarsInformation, 2);
YAP_UserCPredicate ("cpp_set_horus_flag", setHorusFlag, 2);
YAP_UserCPredicate ("cpp_free_parfactors", freeParfactors, 1);
YAP_UserCPredicate ("cpp_free_ground_network", freeGroundNetwork, 1);
}

View File

@ -23,10 +23,10 @@ CC=@CC@
CXX=@CXX@
# normal
#CXXFLAGS= -std=c++0x @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) -D_YAP_NOT_INSTALLED_=1 -I$(srcdir) -I../../../.. -I$(srcdir)/../../../../include @CPPFLAGS@ -DNDEBUG
#CXXFLAGS= -std=c++0x @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) -D_YAP_NOT_INSTALLED_=1 -I$(srcdir) -I../../.. -I$(srcdir)/../../../include @CPPFLAGS@ -DNDEBUG
# debug
CXXFLAGS= -std=c++0x @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) -D_YAP_NOT_INSTALLED_=1 -I$(srcdir) -I../../../.. -I$(srcdir)/../../../../include @CPPFLAGS@ -g -O0 -Wextra
CXXFLAGS= -std=c++0x @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) -D_YAP_NOT_INSTALLED_=1 -I$(srcdir) -I../../.. -I$(srcdir)/../../../include @CPPFLAGS@ -g -O0 -Wextra
#