practical constraints: a tutorial on modelling with constraints - CiteSeerX

3 downloads 131211 Views 139KB Size Report
approaches to efficient modelling with constraints in a tutorial-like form. ... consequence, to design models that exploits the ...... Proceedings of CP-AI-OR2001,.
CPDC’2003

PRACTICAL CONSTRAINTS: A TUTORIAL ON MODELLING WITH CONSTRAINTS ROMAN BARTÁK* Charles University, Faculty of Mathematics and Physics Malostranské námestí 2/25, 118 00 Praha 1, Czech Republic e-mail: [email protected] Abstract: Constraint programming provides a declarative approach to problem solving. The users just state the combinatorial (optimization) problems as constraint satisfaction problems and the underlying solver finds a solution for them. However, in practice, the situation is more complicated as there usually exist various ways how to describe the problem using variables, domains, and constraints. Moreover, the different models may lead to significantly different running times of the solvers. In fact, even a small change in the model may change the efficiency dramatically. This paper describes some known approaches to efficient modelling with constraints in a tutorial-like form.

Keywords: constraint satisfaction, problem modelling, applications

1

better behaviour of the solvers and, as a consequence, to design models that exploits the power of the solvers. The goal of this paper is to provide an overview of modelling techniques used to state problems as constraint satisfaction problems. Respecting what has been said in the previous paragraph, we first survey the mainstream constraint satisfaction technology. Then we make a short view to insides of some interesting constraints to explain their behaviour. In the rest of the paper we demonstrate some modelling techniques using several funny (seesaw), real-life (assignment problem), and hard (Golomb ruler) problems.

INTRODUCTION

“Constraint programming represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it.” [4] This nice quotation might convince users that designing a constraint model is an easy and straightforward task and that everything stated as a constraint satisfaction problem can be solved by the underlying constraint solver. This holds for simple or small problems but as soon as the problems are more complex, the role of constraint modelling is becoming more and more important. Basically, it means that various models of the same problem may lead to different solving times, quite often to significantly different times. Unfortunately, there does not exist (yet) any guide that can steer the user how to design a solvable model. This “feature” of constraint technology might be a bit depressing for novices. Nevertheless, there are many rules of thumb about designing models that would be probably good in the solving times. Moreover, we also believe that it is important to be aware of the insides of constraint satisfaction to understand *

2

CONSTRAINT SATISFACTION AT GLANCE

Constraint programming (CP) is a framework for solving combinatorial (optimization) problems. The basic idea is to model the problem as a set of variables with domains (the values for the variables) and a set of constraints restricting the possible combinations of the variables’ values

Supported by the Grant Agency of the Czech Republic under the contract no. 201/01/0942 and by the project LN00A056 of the Ministry of Education of the Czech Republic.

7

(Figure 1). Usually, the domains are finite and we are speaking about constraint satisfaction problems (CSP). The task is to find a valuation of the variables satisfying all the constraints, i.e., a feasible valuation. Sometimes, there is also an objective function defined over the problem variables. Then the task is to find a feasible valuation minimizing or maximizing the objective function. Such problems are called constraint satisfaction optimization problems (CSOP). Note that modelling problems using CS(O)P is natural because the constraints can capture arbitrary relations and various constraints can be easily combined within a single system. Opposite to frameworks like linear and integer programming, the constraints are not restricted to linear equalities and inequalities. The constraint can express arbitrary mathematical or logical formula, like (x2= 19,

Fig. 11. Initial domain pruning for the seesaw problem with the serialized constraint

4.2

P1

C

Assignment problems

labeling([ff],Sol).

The second studied problem is more real-life oriented than the seesaw problem. It belongs to the category of assignment problem like allocating ships to berths, planes to stands, crew to planes etc. In particular, we will describe a problem of assigning workers to products.

Fig. 14. A constraint model for the assignment problem

By running the above program we get four different assignments that satisfy the minimal profit

12

CPDC’2003 In many problems the role of variables and values can be swapped and it is the model designer who decides which model is more appropriate. In our assignment problem it may seem that both models are fully equivalent. However, somehow surprisingly the dual model requires a smaller number of choices to be explored to find all the solutions of the problem (11 vs. 15). The reason is that profit depends more on the product than on the worker. Thus, the profitability constraint propagates more for products than for workers. Figure 18 compares the initial pruning before the start of labelling for both primal and dual models.

constraint (Figure 15). The first two assignments have profit 19, the third assignment has profit 21 and the last assignment has profit 20. ?- assignment(X). X = X = X = X = no

[1,2,3,4] [2,1,3,4] [4,1,2,3] [4,1,3,2]

? ? ? ?

; ; ; ;

Fig. 15. All solutions of the assignment problem

Quite often the task is not to find a feasible solution but to find an optimal solution. Typically, the constraint solvers use branch and bound technique to find optimal solutions. The nice feature is that one does not need to change the constraint model to solve an optimisation problem. Only the standard labelling procedure is substituted by a procedure looking for optimal solutions. In practice, the value of the objective function is encoded into a variable and the system minimises or maximises the value of this variable. Figure 16 shows a change in the code necessary to solve the optimisation problem where the task is to find an assignment with maximal profit.

W1 W2 W3 W4

in in in in

1..4 1..4 1..4 1..4

P1 P2 P3 P4

in in in in

1..2 1..4 2..4 1..4

Fig. 18. Initial domain pruning for the assignment problem (left-primal model, right-dual model).

Determining the efficiency of different models is a difficult problem. Usually, the best model will be the one in which information is propagated first. To improve propagation, the primal and dual models can be combined into one model. In practice, it means that variables and constraints from both models are used together and special “channelling” constraints interconnect the models (SICStus Prolog provides the assignment constraint to interconnect the models). Figure 19 shows a constraint model where both primal and dual models are combined. Thanks to stronger domain pruning this model requires only 9 choices to be explored to find all the solutions of the problem

… EW1+EW2+EW3+EW4 #= E, maximize(labeling([ff],Sol),E). Fig. 16. A change of the constraint model to solve the optimisation problem

The branch and bound technique behind the maximize procedure will now find the optimal solution which is X=[4,1,2,3]. Let us now turn our attention back from optimisation to the original constraint model. We decided to use variables for workers and values for products. However, it is possible to swap the role of variables and values and to describe products by variables and workers assigned to the products as values for these variables. Figure 17 shows such a dual constraint model.

assignment(Workers):Workers= [W1,W2,W3,W4], domain(Workers,1,4), all_different(Workers), element(W1,[7,1,3,4],EW1), element(W2,[8,2,5,1],EW2), element(W3,[4,3,7,2],EW3), element(W4,[3,1,6,3],EW4), EW1+EW2+EW3+EW4 #>= 19, Products = [P1,P2,P3,P4],

assignment(Sol):Sol = [P1,P2,P3,P4],

domain(Products,1,4), all_different(Products), element(P1,[7,8,4,3],EP1), element(P2,[1,2,3,1],EP2), element(P3,[3,5,7,6],EP3), element(P4,[4,1,2,3],EP4), EP1+EP2+EP3+EP4 #>= 19,

domain(Sol,1,4), all_different(Sol), element(P1,[7,8,4,3],EP1), element(P2,[1,2,3,1],EP2), element(P3,[3,5,7,6],EP3), element(P4,[4,1,2,3],EP4), EP1+EP2+EP3+EP4 #>= 19,

assignment(Workers,Products),

labeling([ff],Sol).

labeling([ff],Workers).

Fig. 17. A dual model for the assignment problem

Fig. 19. A combined model for the assignment problem

13

CPDC’2003 Combining primal and dual models is an easy way how to improve domain pruning. As Figure 20 shows, this combination really helped to prune domains of the variables describing workers. W1 W2 W3 W4

in in in in

(1..2)\/{4} 1..4 2..4 2..4

P1 P2 P3 P4

in in in in

X1 = 0 X1