improve memory usage when storing piointers to evidence facts.

This commit is contained in:
Vítor Santos Costa 2011-09-15 15:49:57 +01:00
parent cdd33b8c1a
commit 4855c3460a

View File

@ -26,7 +26,7 @@
:- dynamic :- dynamic
user:term_expansion/2. user:term_expansion/2.
:- attribute key/1, dist/2, evidence/1, starter/0. :- attribute key/1, dist/2, evidence/1.
:- use_module('clpbn/ve', :- use_module('clpbn/ve',
@ -163,14 +163,18 @@ clpbn_flag(parameter_softening,Before,After) :-
% %
% make sure a query variable is reachable by the garbage collector. % make sure a query variable is reachable by the garbage collector.
% %
% we use a mutable variable to avoid unnecessary trailing.
%
store_var(El) :- store_var(El) :-
catch(b_getval(clpbn_qvars,Q.Tail), _, init_clpbn_vars(El, Q, Tail)), nb_current(clpbn_qvars, Mutable), !,
Tail = [El|NewTail], get_mutable(Tail, Mutable),
b_setval(clpbn_qvars, [Q|NewTail]). update_mutable(El.Tail, Mutable).
store_var(El) :-
init_clpbn_vars(El).
init_clpbn_vars(El, Q, Tail) :- init_clpbn_vars(El) :-
Q = [El|Tail], create_mutable(El, Mutable),
b_setval(clpbn_qvars, [Q|Tail]). b_setval(clpbn_qvars, Mutable).
check_constraint(Constraint, _, _, Constraint) :- var(Constraint), !. check_constraint(Constraint, _, _, Constraint) :- var(Constraint), !.
check_constraint((A->D), _, _, (A->D)) :- var(A), !. check_constraint((A->D), _, _, (A->D)) :- var(A), !.
@ -273,17 +277,12 @@ get_bnode(Var, Goal) :-
get_dist(Dist,_,Domain,CPT), get_dist(Dist,_,Domain,CPT),
(Parents = [] -> X = tab(Domain,CPT) ; X = tab(Domain,CPT,Parents)), (Parents = [] -> X = tab(Domain,CPT) ; X = tab(Domain,CPT,Parents)),
dist_goal(X, Key, Goal0), dist_goal(X, Key, Goal0),
include_evidence(Var, Goal0, Key, Goali), include_evidence(Var, Goal0, Key, Goal).
include_starter(Var, Goali, Key, Goal).
include_evidence(Var, Goal0, Key, ((Key:-Ev),Goal0)) :- include_evidence(Var, Goal0, Key, ((Key:-Ev),Goal0)) :-
get_atts(Var, [evidence(Ev)]), !. get_atts(Var, [evidence(Ev)]), !.
include_evidence(_, Goal0, _, Goal0). include_evidence(_, Goal0, _, Goal0).
include_starter(Var, Goal0, Key, ((:-Key),Goal0)) :-
get_atts(Var, [starter]), !.
include_starter(_, Goal0, _, Goal0).
dist_goal(Dist, Key, (Key=NDist)) :- dist_goal(Dist, Key, (Key=NDist)) :-
term_variables(Dist, DVars), term_variables(Dist, DVars),
process_vars(DVars, DKeys), process_vars(DVars, DKeys),