Contents

Toggle## Constraint programming

Constraint Programming (PPC) makes it possible to solve Constraint Satisfaction (CSP) type problems. A CSP is a modeling problem in the form of a set of constraints posed on variables, each of these variables taking its values in a defined domain.

A CSP is defined by a triplet (X, D, C, R) such that:

- X = {X
_{1}, X_{2},…, X_{not}} the set of variables of the problem; - D a function which associates with each variable X
_{i}its domain (set of possible values) D (X_{i}); - C = C
_{1},VS_{2},…,VS_{k}} the set of constraints.

## Constraint Programming Solution

Solving a CSP consists of assigning values to variables, so that all the constraints are satisfied. For this, we need a precise vocabulary:

- An assignment is an instance of certain variables by values from their respective domains. We will denote by A = {(X
_{1}, V_{1}),…} Assignment which instantiates the variable X_{1}by the value V_{1}, etc; - An assignment is said to be total if it instantiates all the variables of the problem, otherwise it is said to be partial;
- An assignment A violates a constraint C
_{k}if all the variables of C_{k}are instantiated in A and if the relation defined by C_{k}is not verified by the assignment: - A total or partial assignment is consistent if it does not violate any constraint, otherwise it is said to be inconsistent;
- A CSP solution is a consistent total assignment.

In constraint programming, when a CSP has no solution, it is said to be overconstrained. In this case, we can find the total assignment that violates the fewest constraints possible. We then speak of max-CSP.

Another possibility in programming by constraints is to assign a weight to each constraint, one then seeks to minimize the weight of the violated constraints. We then speak of VCSP (valued). A max-CSP is a VCSP where all the constraints have the same weight.

When a CSP admits several different solutions, it is said to be under-constrained. If the solutions are not all equivalent, then it is possible to establish preferences between the solutions. We add a function that associates a numerical value to each solution, representing the quality of this solution. The objective is to maximize this objective function. We then speak of CSOP (constraint satisfaction optimization problem).

## Example of Constraint Programming: n-queens problem

A builder has land with 16 plots (4 rows and 4 columns) forming a chessboard. He wants to dispose of the wind turbines in such a way that they are not engaged. Two wind turbines are engaged if they are on the same row, the same column or the same diagonal.

### Constraint programming 1

The variables are the row and column positions of the wind turbines on the chessboard. We associate with each wind turbine i two variables L_{i} and C_{i} corresponding respectively to the row and the column on which to place the wind turbine. We have the following constraint programming modeling:

- Variables:
- X = {L
_{1}, THE_{2}, THE_{3}, THE_{4}, VS_{1}, VS_{2}, VS_{3}, VS_{4}}

- X = {L
- Areas :
- D (L
_{1}) = D (L_{2}) = D (L_{3}) = D (L_{4})={1,2,3,4} - D (C
_{1}) = D (C_{2}) = D (C_{3}) = D (C_{4})={1,2,3,4}

- D (L
- Constraints:
- VS
_{lig}= allDiff ({L_{1}, THE_{2}, THE_{3}, THE_{4}}) to have L values_{i}all different from each other - VS
_{collar}= allDiff ({C_{1}, VS_{2}, VS_{3}, VS_{4}}) to have C values_{i}all different from each other - VS
_{dm}= {C_{i}+ L_{i}! = C_{j}+ L_{j}/ for all i and j in {1,2,3,4} and i! = j} - VS
_{dd}= {C_{i}-THE_{i}! = C_{j}-THE_{j}/ for all i and j in {1,2,3,4} and i! = j} - It is the union of these together.

- VS

This modelization is naive since one considers all the constraints without looking for their meaning.

### Constraint programming 2

It is easy to notice that there will be at least one wind turbine per column of the chessboard (or row, etc.). The programming by constraints then consists in determining on which row is the wind turbine placed on column i, the variable will be noted X_{i} :

- Variables:
- X = {X
_{1}, X_{2}, X_{3}, X_{4}}

- X = {X
- Areas :
- D (X
_{1}) = D (X_{2}) = D (X_{3}) = D (X_{4})={1,2,3,4}

- D (X
- Constraints:
- VS
_{lig}= allDiff ({X_{1}, X_{2}, X_{3}, X_{4}}) to have X values_{i}all different from each other - VS
_{dm}= {X_{i}+ i! = X_{j}+ j / for all i and j in {1,2,3,4} and i! = j} - VS
_{dd}= {X_{i}-i! = X_{j}-j / for all i and j in {1,2,3,4} and i! = j} - It is the union of these together.

- VS

### Discussion

There are many other ways to model the problem. In this case, what is the best model? For that, there are three criteria making it possible to evaluate the relevance of the modeling: does it best model the problem? was the modeling trivial? does modeling allow the problem to be solved effectively?

## Resolution of a CSP

The resolution of a CSP is a Np-Complete problem in the general case. The resolution mechanism consists of repeating the following two steps:

- reduction of problems by filtering;
- browsing the search tree.

These two stages make use of notions such as the consistency of arcs or the propagation of constraints that we will not develop in this course. There are many solvers of a CSP such as for example the Google OR-tools library.