RE: Counting backtracks

From: Aminu, Farouk <>
Date: Thu 01 May 2003 12:29:22 PM GMT
Message-ID: <>
Dear Karen,

Thank you very much for yours below. I will make the necessary acknowledgements
whenever I use the code in a public way.

with regards,


Umaru Farouk Aminu
Department of Management Science,
Lancaster University,
LA1 4YX,
Tel: +44 1524 383619 (Home)
      +44 1524 593865 (School)
      +44 1524 844885 (Fax)

-----Original Message-----
From: Karen Petrie []
Sent: 30 April 2003 19:51
Subject: Re: [eclipse-users] Counting backtracks

Hi Farouk,

You wrote:
>I would like to count the number of backtracks done between successive 
>solutions until the 'last'. I would also like to know the number of 
>backtracks done between finding the optimum value and proving optimality. 
>Is there anyway I can do this in ECLiPSe?

Yes, there is a way to do it fairly easily if you are using the FD 
minimize predicate, as it rases the event handler 280 every time it 
outputs the optimal found to date. So all you need to do is set this even 
handler to also include a backtrack count. In my case I have only ever had 
it print out the value, but it should be fairly easy to output it to a 
file, if that was required.

Firstly, you need to write your own backtracking methods (this one 
actually comes from an example which Joachim Schimpf wrote, so all the 
credit goes to him):

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

init_backtracks :-

get_backtracks(B) :-
count_backtracks :-
count_backtracks :-
        getval(deep_fail,false),        % may fail
Then a search predicate which updates this count:

mylabeling(AllVars) :-
        ( foreach(Var, AllVars) do
            indomain(Var)                       % select value
Then you need to redefine the event handler:

write_info(_Error, Predicate):-
	arg(1, Predicate, Minimum),
	printf("Found a solution with cost %d \n", [Minimum]),
	printf("Backtracks to this point = %d \n \n", [B]).

%set event handler	
:- set_event_handler(280, write_info/2).	

Then all you do is call minimize on your labelling:

minimize(mylabeling(SearchVars), OptimalNumber)

I have attached an full example using 'The Peaceably Coexisting Armies of 
Queens' problem. Feel free to use any of the code, but I would appreciate 
an acknowledgment, if you use it in any public way.

One question I have though, is that I can not find an equivalent of the 
minimize predicate in the IC library, is it possible to do this with IC?

Received on Thu May 01 13:35:10 2003

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