• No results found

Constraint programming is an instance of thedeclarative programmingparadigm. Like other declarative methods, in constraint programming the user only specifies the problem and not the solution method. Constraint programming systems are equipped with solvers that decide how to search for solutions to the given problem. A constraint satisfaction problem is a triple (V, D,C) in which V is a

set of variables,Dis a set of domains which map every variablev∈ V to a set

of valuesD(v), andCis a set of constraints which further limit values fromD

that can be assigned toV (F. Rossi et al. 2006).

Example 2. We have three items with weights 10, 15, and 20. Each item has

a value. The values of these three items are 5, 1, and 10. We want to pick two of these items while respecting the following conditions: 1) The sum of weights of selected items should not exceed 35, and 2) If we pick the first item, we can not select the third one.

To formulate this problem in constraint programming, we introduce discrete variables x1,x2, andx3 as indicators for selection of each of three items. We

also introduce the real variables which represents the total weight of selected

items. SoV ={x1, x2, x3, s}. The domain of discrete variables is the finite set

{0,1}, and the domain of continuous variable is the interval[0,45]. In other

CONSTRAINT PROGRAMMING 19

consists of following constraints:

x1+x2+x3= 2 (2.1)

s= 10x1+ 15x2+ 20x3 (2.2)

s≤35 (2.3)

(x1= 1)→(x3= 0) (2.4)

Constraint 2.1 reflects that we want to pick exactly two items. Constraint 2.2 specifies that sis sum of selected items. Constraints 2.3 and 2.4 reflect the two

conditions specified in the problem description1.

The two main operations of a constraint solving system are search and propagation. Search is the act of trying different values (or ranges of values)

for variables. For discrete variables, this is done by assigning values from the domain of that variable. For continuous variables, search is done by partitioning the domain into disjoint intervals. The intervals that have a size smaller than a predefined precision will not be partitioned anymore.

Propagation is the act of removing values (or ranges) from the domain of variables that given the domains of other variables and the model constraint, cannot be part of a solution. Given a constraint c defined over variables x1, . . . , xn with domainsD1, . . . , Dn, we define the set of admissible values for

variablex1 as:

{a1∈D1:∃a2∈D2, . . . ,anDn such thatC(a1, . . . , an) holds}

For continuous variables, the propagation mechanism computes a superset, namely the smallest interval enclosing this set.

Example 3. Consider a simple model consisting of three continuous variables x1, x2, andy with domains[a, b], [c, d], and[−∞,∞], respectively. Propagation

according to the constrainty=x1+x2 will reduce the domain of variabley to

[a+c, b+d]. If we replace this constraint byy=x1∗x2, the domain ofy will

be reduced to [min(ac, ad, bc, bd), max(ac, ad, bc, bd)].

If we reduce the domains of variables in a CSP in such a way that all constraints hold for each value in the domain of each variable, we have found a solution for that CSP.

1This problem can be modeled without using continuous variables. However, we have

20 BACKGROUND

Example 4 (Example 2 continued). For solving the problem formulated in

example 2, we start by assigning value 1 for x1. This assignment, together

with constraint 2.2 triggers a propagation step which reduces the domain ofs

to[10,45]. A similar propagation step based on constraint 2.4 removes value 1

from the domain ofx3. This in turn changes the domain ofs to[10,25]. We

continue by assigning value 0 to x2. As a result, the domain of s reduces to

[10,10]. At this point, all variables are assigned a value from their domain, and

all constraints hold for these values. This means that we have found a solution.

Note that in the previous example, we did not have to search for the value of

s, as its value was determined by propagation. In other words, our previous

assignments to other variables, dictated thatsshould be equal to 10.

Global constraints A global constraint is a constraint over a non-fixed number

of variables. Global constraint are often semantically redundant; meaning that they can be represented by a set of simpler constraints. However, a global constraint provides better access to the structure of the problem. A famous example is thealldifferent(x1, . . . , xn) constraint which enforces

that each pair of variables x1, . . . , xn should take different values. Consider alldifferent(x1, x2, x3). This constrain can be replaced by three inequality

constraints x1 6= x2, x1 =6 x3, x2 6= x3. Now consider a situation where the

domain of all three variables is {1,2}. The alldifferent constraint can

reason over these domains and issue a failure. However, the inequality constraints do not have access to this global view and can not detect the failure.

Constraint optimization Aconstraint optimizationproblem is a CSP together

with a functionf defined overV. The goal is to find a solutionS∗ such that f(S∗)≤f(S) for all solutionsS to the problem. This problem can be solved

using a CSP solver using a simple procedure. Initially a solutionS is obtained

by backtracking search. Then the constraintf(V)< Sis added to the constraint

setC. Solving this problem will give a solution with a smaller objective function.

These steps are repeated until no solution is found. The solution obtained last is optimal.