Re: indomain/2 gives type error in version 5.5 (not before!)

From: Warwick Harvey <>
Date: Mon 02 Dec 2002 02:10:24 PM GMT
Message-ID: <>
On Sun, Dec 01, 2002 at 11:48:10PM -0500, Sebastian Sardina wrote:
> > In any event, it shouldn't be too hard to write your own indomain predicate
> > to do what you want.  In particular, you can use ::/2 to retrieve a
> > representation of the current domain of a variable, which you can then pass
> > to the nth_value/3 predicate provided by fd_search, which returns the Nth
> > value in the given domain (though if your domains are symbolic, ::/2 will
> > always return a simple list, which you can do with as you like).  Plug in a
> > random number (or whatever ordering you like) and "Bob's your uncle" as they
> > say...
> now I can write something like:
> my_random_choice(X) :- ........  < using nth_value/3 and random/1 >
> Now here is my big doubt:
> 	2) how can I make my random-choice procedure to repetitively select
> different random values for X without repeating? should
> my_random_choice/1 be responsible of doing that or search/6 itself takes
> care of this? 

Your predicate should take care of it.  The easiest way to do this is to (on
backtracking) remove the value tried so it can not be selected again, and
then just call your indomain predicate again.  E.g.:

indomain_random(X) :-
	% Find out how many domain elements we have to choose from.
	dvar_domain(X, Dom),
	dom_size(Dom, Size),
	% Get the domain elements.
	X :: L,
	% Choose one at random.
	Index is 1 + (random mod Size),
	nth_value(L, Index, Try),
	% Try assigning it.
	indomain_random(X, Try).

indomain_random(X, X).
indomain_random(X, Try) :-
	X #\= Try,

> > Once you have this predicate you can of course use it as the choice method
> > in a call to search/6; indeed, by providing your own selection and choice
> > methods, the variables don't even need to be FD variables: in the past I've
> > used fd_search's search/6 to solve problems involving sets.
> Yes right they don't even have to be FD variables. But should my
> my_random_choice/1 be as good and efficient as the indomain_random
> already provided for say integer finite domains? My program uses many
> types of variables, only some of them being symbolic. I want to apply
> search/6 randomly for every variable but I want a general and still
> efficient solution.

There will be some overhead for the meta-call (calling a predicate passed in
as a run-time parameter is less efficient than calling one known at compile
time), but I expect the impact of that is likely to be small in your case.

Incidentally, what are you using symbolic domains for?  (We have arguments
here as to whether they're useful or not, so hearing how and why they are
being used is of interest.)

Received on Mon Dec 02 14:11:33 2002

This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:19 PM GMT GMT