Reputation: 21
i am new to prolog and working my way through the FD solvers in it. I am exploring both SWI-Prolog and Gnu Prolog. I have a case where a constraint like below needs to be solved and one of the possible solutions needs to be picked at random based on a seed. Not sure how much of it is possible in Prolog.
Below is the equation i am trying to solve:
?- X #< 7 , X #> -10, 4*X+2*Y #< 8, Y #< 10, Y #>0, label([X]).
X = -9,
Y in 1..9 ;
X = -8,
Y in 1..9 ;
X = -7,
Y in 1..9 ;
X = -6,
Y in 1..9 ;
X = -5,
Y in 1..9 ;
X = -4,
Y in 1..9 ;
X = -3,
Y in 1..9 ;
X = -2,
Y in 1..7 ;
X = -1,
Y in 1..5 ;
X = 0,
Y in 1..3 ;
X = Y, Y = 1.
As you can see above, it gives me multiple solutions. For my application, I would be interested in choosing one of the data set from above based on a seed.
Is this possible in prolog? It would be great if you can help me with your suggestions.
Thanks, Venkat
Upvotes: 2
Views: 186
Reputation: 7229
You can specify random choice strategy for labeling variables. That's how to do it in ECLiPSe CLP Prolog:
:- lib(ic).
one_random_solution(X, Y) :-
X #< 7 , X #> -10, 4*X+2*Y #< 8, Y #< 10, Y #>0,
once search([X, Y], 0, input_order, indomain_random, complete, []).
And run with:
[eclipse]: seed(10), one_random_solution(X, Y).
X = -9
Y = 5
Yes (0.00s cpu)
Unfortunately, it looks like SWI-Prolog doesn't support random choice strategy for labeling.
Upvotes: 4