Re: Re:search/6 inside bagof/3?

From: Karen E Petrie <scomkep_at_zeus.hud.ac.uk>
Date: Mon 04 Nov 2002 04:03:17 PM GMT
Message-Id: <200211041603.QAA15466@dvorak.hud.ac.uk>
Warwick & Zeynep,

I have some code which others may find useful, it doesn't actually store 
all the solutions (which may be a problem in zeyneps case?) But it will 
output each solution, with the number of backtracks to get there. I didn't 
actually write the original, it actually comes from one of the examples 
which I think Jochaim wrote? I have just modified it so that it works on 
any list, and added some backtracking stuff.

%VarsList is the list of variables to be searched over
%this code assumes that the modelling and constraints section proceds it
all_solutions(VarsList):-
	setval(solutions, 0),
	statistics(times, [T0|_]),
	(
	    init_backtracks,
	    labeling(VarsList),
	    getval(solutions, S),
	    Sol is (S + 1),
	    printf("\nSolution %d \n", [Sol]),
	    writeln(VarsList),
	    get_backtracks(B),
	    printf("Number of Backtracks %d \n", [B]),
	    incval(solutions),	    
	    fail
	;
	    true
	),
	get_backtracks(B),
	statistics(times, [T1|_]),
	T is T1-T0,
	getval(solutions, S),
	printf("\nFound %d solutions in %w s with %d backtracks",
		[S,T,B]).
		
labeling(AllVars) :-
        ( foreach(Var, AllVars) do
	    count_backtracks,
            indomain(Var)                      
        ).

		
:- local variable(backtracks), variable(deep_fail).

init_backtracks :-
        setval(backtracks,0).

get_backtracks(B) :-
        getval(backtracks,B).

count_backtracks :-
        setval(deep_fail,false).
count_backtracks :-
        getval(deep_fail,false),        % may fail
        setval(deep_fail,true),
        incval(backtracks),
	fail.
	
There are a few problems with this approach, the main one being that it 
leaves delayed goals hanging around, the other being that it doesn't let 
you use the built in search predicates you have to use your own! I 
actually find this quite convinent, as it lets me write my own heuristics, 
but I see that this could be annoying, under different circumstances.

Perhaps, something like this could be added to the search library? (or 
perhaps it is and I have missed it?)

Karen	
Received on Mon Nov 04 16:11:13 2002

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