PROLOG CLPFD How to express this via constraints?
clpfd, constraints, minimize, prolog, swi-prolog
Solution
Reification involves separate constraints (`#<==>/2`, `#==>/2` etc.), you can use them for example like:
example(Ls) :-
Ls = [X,Y],
Ls ins 1..2,
X #= 1 #<==> B1,
Y #= 1 #<==> B2,
X #= 2 #<==> B3,
Y #= 2 #<==> B4,
Cost #= max(B1*3 + B2*5, B3*3 + B4*5),
labeling([min(Cost)], Ls).
Sample query and its result:
?- example(Ls).
Ls = [1, 2] ;
Ls = [2, 1] ;
Ls = [1, 1] ;
Ls = [2, 2] ;
false.
Problem
Given the following sample of code: ``` example(Ls) :- Ls = [X,Y], Ls ins 1..2, Cost #= max((X #= 1)*3 + (Y #= 1)*5, (X #= 2)*3 + (Y #= 2)*5), labeling([minimize(Cost)], Ls). ``` The idea is to find the assignment to variables of Ls that minimizes Cost (in this simple example, it would be either X=1 and Y=2, or X=2 and Y=1). I am trying to use the fact that the constraint #=/2 is reifiable, which means reflecting their truth values into Boolean values represented by the integers 0 and 1. (taken from the manual http://www.swi-prolog.org/man/clpfd.html). However, it doesn't work. I get the following error: ``` ERROR: Domain error: `clpfd_expression' expected, found `_G3154#=1' ``` What would be an equivalent, correct version?