This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/clpbn/horus_ground.yap

144 lines
4.5 KiB
Plaintext
Raw Normal View History

2012-03-31 23:27:37 +01:00
/*******************************************************
2012-05-23 19:15:23 +01:00
Interface to Horus Ground Solvers. Used by:
- Variable Elimination
- Belief Propagation
- Counting Belief Propagation
2012-03-31 23:27:37 +01:00
********************************************************/
2012-05-23 19:15:23 +01:00
:- module(clpbn_horus_ground,
2012-05-23 21:23:03 +01:00
[call_horus_ground_solver/6,
check_if_horus_ground_solver_done/1,
2012-09-29 11:50:00 +01:00
init_horus_ground_solver/5,
run_horus_ground_solver/4,
2012-05-23 21:23:03 +01:00
finalize_horus_ground_solver/1
]).
2012-05-23 19:15:23 +01:00
:- use_module(horus,
2012-05-23 21:23:03 +01:00
[cpp_create_ground_network/4,
cpp_set_factors_params/2,
cpp_run_ground_solver/3,
cpp_set_vars_information/2,
2012-09-29 11:50:00 +01:00
cpp_free_ground_network/1,
set_solver/1
2012-05-23 19:15:23 +01:00
]).
:- use_module(library('clpbn/dists'),
[dist/4,
get_dist_domain/2,
get_dist_domain_size/2,
get_dist_params/2
]).
2012-08-09 04:16:37 +01:00
:- use_module(library('clpbn/ground_factors'),
2012-08-15 22:01:45 +01:00
[generate_network/5
2012-08-09 04:16:37 +01:00
]).
:- use_module(library('clpbn/display'),
2012-03-31 23:27:37 +01:00
[clpbn_bind_vals/3]).
2011-12-27 22:07:42 +00:00
:- use_module(library('clpbn/aggregates'),
2012-03-31 23:27:37 +01:00
[check_for_agg_vars/2]).
2012-09-23 13:25:15 +01:00
:- use_module(library(clpbn/numbers)).
:- use_module(library(charsio),
[term_to_atom/2]).
:- use_module(library(pfl),
2012-09-23 13:25:15 +01:00
[skolem/2
]).
2011-12-10 22:58:43 +00:00
2012-09-23 13:25:15 +01:00
:- use_module(library(maplist)).
:- use_module(library(lists)).
2011-12-10 22:58:43 +00:00
:- use_module(library(atts)).
2011-12-10 22:58:43 +00:00
2012-04-03 16:22:40 +01:00
:- use_module(library(bhash)).
2011-12-10 22:58:43 +00:00
2012-05-23 21:23:03 +01:00
call_horus_ground_solver(QueryVars, QueryKeys, AllKeys, Factors, Evidence, Output) :-
call_horus_ground_solver_for_probabilities([QueryKeys], AllKeys, Factors, Evidence, Solutions),
2012-07-03 19:48:13 +01:00
clpbn_bind_vals([QueryVars], Solutions, Output).
2012-09-23 13:25:15 +01:00
call_horus_ground_solver_for_probabilities(QueryKeys, AllKeys, Factors, Evidence, Solutions) :-
2012-07-03 19:48:13 +01:00
get_factors_type(Factors, Type),
2012-09-23 13:25:15 +01:00
keys_to_numbers(AllKeys, Factors, Evidence, Hash4, Id4, FactorIds, EvidenceIds),
2012-08-15 22:01:45 +01:00
%writeln(evidence:Evidence), writeln(''),
%writeln(evidenceIds:EvidenceIds), writeln(''),
2012-09-23 13:25:15 +01:00
%writeln(factorIds:FactorIds), writeln(''),
2012-07-03 19:48:13 +01:00
cpp_create_ground_network(Type, FactorIds, EvidenceIds, Network),
2012-09-23 13:25:15 +01:00
maplist(get_var_information, AllKeys, StatesNames),
maplist(term_to_atom, AllKeys, KeysAtoms),
%writeln(s1:KeysAtoms:KeysAtoms:StatesNames),
2012-07-03 19:48:13 +01:00
cpp_set_vars_information(KeysAtoms, StatesNames),
2012-08-13 15:23:36 +01:00
%writeln(network:(Type, FactorIds, EvidenceIds, Network)), writeln(''),
2012-08-09 04:16:37 +01:00
run_solver(ground(Network,Hash4,Id4), QueryKeys, Solutions),
2012-07-03 19:48:13 +01:00
cpp_free_ground_network(Network).
2012-08-09 04:16:37 +01:00
run_solver(ground(Network,Hash,Id), QueryKeys, Solutions) :-
%get_dists_parameters(DistIds, DistsParams),
%cpp_set_factors_params(Network, DistsParams),
2012-09-23 13:25:15 +01:00
lists_of_keys_to_ids(QueryKeys, QueryIds, Hash, _, Id, _),
2012-08-13 15:23:36 +01:00
%writeln(queryKeys:QueryKeys), writeln(''),
2012-09-23 13:25:15 +01:00
% writeln(queryIds:QueryIds), writeln(''),
2012-08-09 04:16:37 +01:00
cpp_run_ground_solver(Network, QueryIds, Solutions).
2012-04-05 18:38:56 +01:00
2012-08-15 22:01:45 +01:00
get_factors_type([f(bayes, _, _)|_], bayes) :- ! .
get_factors_type([f(markov, _, _)|_], markov) :- ! .
2012-09-29 11:50:00 +01:00
get_var_information(_:Key, Domain) :- !,
skolem(Key, Domain).
2012-09-23 13:25:15 +01:00
get_var_information(Key, Domain) :-
skolem(Key, Domain).
2012-04-18 03:03:29 +01:00
2012-05-23 21:23:03 +01:00
finalize_horus_ground_solver(bp(Network, _)) :-
cpp_free_ground_network(Network).
2012-09-29 11:50:00 +01:00
finalize_horus_ground_solver(horus(_, _, _, _)).
2012-07-03 19:48:13 +01:00
%
% QVars: all query variables?
%
%
2012-09-29 11:50:00 +01:00
init_horus_ground_solver(QueryKeys, AllKeys, Factors, Evidence, horus(QueryKeys, AllKeys, Factors, Evidence)).
2012-07-03 19:48:13 +01:00
%
% just call horus solver.
%
2012-09-29 11:50:00 +01:00
run_horus_ground_solver(_QueryVars, Solutions, horus(GKeys, Keys, Factors, Evidence) , Solver) :-
set_solver(Solver),
call_horus_ground_solver_for_probabilities(GKeys, Keys, Factors, Evidence, Solutions).
2012-05-23 21:23:03 +01:00
%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).
2012-03-22 11:40:24 +00:00
2011-12-27 22:07:42 +00:00
get_dists_parameters([],[]).
get_dists_parameters([Id|Ids], [dist(Id, Params)|DistsInfo]) :-
2012-03-31 23:27:37 +01:00
get_dist_params(Id, Params),
get_dists_parameters(Ids, DistsInfo).