Search code examples
prologclpfdeclipse-clp

Print minimize steps prolog


I'm working on a prolog program (CLPFD) with ECLiPSe 6.1. The program has a lot of variables and i want to minimize a certain value but (obviously) the minimization steps takes a lot of time. How can i print each solution found during the minimize phase? The output prints only (for instance) Found a solution with cost 22003482 but not the solution itself. I would like to have:

Found a solution with cost 22003482
L = [[...],[...],...,[]] %L is the list of value to minimize

on each step of the minimization process.

I've tried this approach:

myLabeling([]).
myLabeling([H|T]):-
    labeling(H),
    myLabeling(T).

%code for the problem

%timeout(+Goal, ++TimeLimit, +TimeOutGoal)
timeout(minimize(myLabeling(AllNodesList),Result),TimeLimit,
    myLabeling(AllNodesList)).

where AllNodesList is a list of lists and TimeOutGoal is the goal to run when TimeLimit expires. This solution doesn't print the last solution found but the first non optimized solution found with labeling.

Any suggestions? Thanks.


Solution

  • You seem to be asking two different questions: (1) how to print every intermediate solution found during minimization, and (2) how to get the best solution in case of a timeout.

    To print every solution found during minimization, simply print the variables immediately after your labeling procedure succeeds:

    minimize( (labeling(Variables),writeln(solution(Variables))), Cost)
    

    To use a timeout, it is easiest is to use the bb_min/3 predicate from the branch_and_bound library. This is just like minimize/2, but accepts various options, including timeout:

    bb_min( labeling(Variables), Cost, bb_options{timeout:TimeLimit})
    

    This will abort the search after the given time, and bind Variables to the best solution found within the time limit.