Re: [eclipse-clp-users] Amount of variables that have been instantiated by enumeration or propagation

From: Joachim Schimpf <jschimpf_at_...153...>
Date: Mon, 12 Jan 2009 16:01:46 +1100
mauricio montecinos wrote:
> 
> I would like to know if there is a predefined predicate that allow me to 
> get the amount of variables that have been instantiated by enumeration 
> or propagation  

Presumably, you are looking for an efficient solution that does _not_ involve
checking all variables every time you ask for that information.

You could use the following code:

:- lib(ic).

delay is_var(X, _) if var(X).
is_var(_X, 0).

var_count(Term, Count) :-
	term_variables(Term, Xs),
	length(Xs, N),
	% create a boolean per variable, and sum them up
	length(Bs, N),
	Bs::0..1,
	ic_global:sumlist(Bs, Count),
	% set boolean to zero when var becomes instantiated
	( foreach(X,Xs), foreach(B,Bs) do is_var(X,B) ).


var_count/1 takes an arbitrary term containing variables, and creates a
finite domain variable that reflects the current number of variables in
its upper bound:

?- var_count([A, B, C, D, E, F], Count).
Count = Count{0 .. 6}
There are 13 delayed goals.
Yes (0.00s cpu)

When variables are instantiated, the count will immediately reflect this:

?- var_count([A, B, C, D, E, F], Count), A = 11, E = 55.
Count = Count{0 .. 4}
There are 9 delayed goals.
Yes (0.00s cpu)

You can use ic:get_max/2 to retrieve the current maximum. When all variables
are instantiated, the Count variable becomes instantiated to 0.  You can of
course modify this code to count the number of instantiations instead of
the remaining variables.


-- Joachim
Received on Mon Jan 12 2009 - 05:01:57 CET

This archive was generated by hypermail 2.3.0 : Tue Apr 16 2024 - 09:13:20 CEST