Constraint programming is a declarative formalism that lets you describe conditions a solution should satisfy. This library provides CLP(FD), Constraint Logic Programming over Finite Domains. It can be used to model and solve various combinatorial problems such as planning, scheduling and allocation tasks.
Most predicates of this library are finite domain constraints, which are relations over integers. They generalise arithmetic evaluation of integer expressions in that propagation can proceed in all directions. This library also provides enumeration predicates, which let you systematically search for solutions on variables whose domains have become finite. A finite domain expression is one of:
an integer Given value a variable Unknown value -Expr Unary minus Expr + Expr Addition Expr * Expr Multiplication Expr - Expr Subtraction min(Expr,Expr) Minimum of two expressions max(Expr,Expr) Maximum of two expressions Expr mod Expr Remainder of integer division abs(Expr) Absolute value Expr / Expr Integer division
The most important finite domain constraints are:
Expr1 #>=
Expr2Expr1 is larger than or equal to Expr2 Expr1 #=<
Expr2Expr1 is smaller than or equal to Expr2 Expr1 #=
Expr2Expr1 equals Expr2 Expr1 #\=
Expr2Expr1 is not equal to Expr2 Expr1 #>
Expr2Expr1 is strictly larger than Expr2 Expr1 #<
Expr2Expr1 is strictly smaller than Expr2
The constraints #=/2, #\=/2, #</2, #>/2, #=</2, and #>=/2 can be reified, which means reflecting their truth values into Boolean values represented by the integers 0 and 1. Let P and Q denote reifiable constraints or Boolean variables, then:
#\
QTrue iff Q is false P #\/
QTrue iff either P or Q P #/\
QTrue iff both P and Q P #<==>
QTrue iff P and Q are equivalent P #==>
QTrue iff P implies Q P #<==
QTrue iff Q implies P
If a variable occurs at the place of a constraint that is being
reified, it is implicitly constrained to the Boolean values 0 and 1.
Therefore, the following queries all fail: ?-
#\
2. ?-
#\
#\
2. etc.
As an example of a constraint satisfaction problem, consider the cryptoarithmetic puzzle SEND + MORE = MONEY, where different letters denote distinct integers between 0 and 9. It can be modeled in CLP(FD) as follows:
:- use_module(library(clpfd)). puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]) :- Vars = [S,E,N,D,M,O,R,Y], Vars ins 0..9, all_different(Vars), S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E #= M*10000 + O*1000 + N*100 + E*10 + Y, M #> 0, S #> 0.
Sample query and its result:
?- puzzle(As+Bs=Cs). As = [9, _G10178, _G10181, _G10184], Bs = [1, 0, _G10199, _G10178], Cs = [1, 0, _G10181, _G10178, _G10223], _G10223 in 2..8, 1000*9+91*_G10178+ -90*_G10181+_G10184+ -9000*1+ -900*0+10*_G10199+ -1*_G10223#=0, all_different([_G10178, _G10181, _G10184, _G10199, _G10223, 0, 1, 9]), _G10199 in 2..8, _G10184 in 2..8, _G10181 in 5..8, _G10178 in 4..7.
Here, the constraint solver could deduce more stringent bounds for many variables. Labeling can be used to search for solutions:
?- puzzle(As+Bs=Cs), label(As), label(Bs). As = [9, 5, 6, 7], Bs = [1, 0, 8, 5], Cs = [1, 0, 6, 5, 2]
This library also provides reflection predicates (like fd_dom/2, fd_size/2 etc.) with which you can inspect a variable's current domain. Use call_residue_vars/2 and copy_term/3 to inspect residual goals and the constraints in which a variable is involved.
=<
I =<
Upper.
The atoms
inf and sup denote negative and positive infinity,
respectively.
\/
Domain2The variable selection strategy lets you specify which variable of Vars should be labeled next and is one of:
The value order is one of:
The branching strategy is one of:
#\=
V, where V is determined by the value ordering options. This is the
default.
#=<
M
and X #>
M, where M is the midpoint of the domain of X.
The order of solutions can be influenced with:
This generates solutions in ascending/descending order with respect to the evaluation of the arithmetic expression Expr. Labeling Vars must make Expr ground. To obtain the incomplete behaviour that other systems exhibit with "maximize(Expr)" and "minimize(Expr)", use once/1, e.g.:
once(labeling([max(Expr)], Vars))
If more than one option of a category is specified, the one occurring rightmost in the option list takes precedence over all others of that category. Labeling is always complete, always terminates, and yields no redundant solutions.
sum(List, #=<, 100)
=<
S_j or S_j +
D_j =<
S_i for all 1 =<
i <
j =<
n.