optim4ai

Logo

Optimisation for Artificial Intelligence, a 4-day course

View the Project on GitHub xoolive/optim4ai

Constraint programming

« Previous | Up ↑ | Next »

Definition

A constraint satisfaction problem consists of:

The objective is to find an instantiation of values to variables within their domains, which satisfies all the expressed constraints.

A problem of two variables $x, y \in {0, 1, 2}$ subject to the constraint $x \neq y$ is a constraint satisfaction problem.

Illustrative problems

\[\begin{alignat*}{12} &&&&&&\sf{}S&&\sf{}E&&\sf{}N&&\sf{}D\\ &&+&&&&\sf{}M&&\sf{}O&&\sf{}R&&\sf{}E\\ =&&&&\sf{}M&&\sf{}O&&\sf{}N&&\sf{}E&&\sf{}Y \end{alignat*}\]
Exercice:
Which digits can we use to replace the following letters so that the addition stands true?
Solution (Click to unfold) Each letter can be associated to a variable taking values between 0 and 9. All variables are linked by the following arithmetic constraint $\mathsf S \times 1000 + \mathsf E \times 100 + ... = ... + \mathsf E \times 10 + \mathsf Y$.

n queens

Exercice:
Place 8 queens on a board so that no two queens attack each other.
Solution (Click to unfold) Eight variables $q_i$ represent the position of the queen placed on the row labeled $i$. All variables must be different (not on the same column). Also, no two queens may be placed on the same ($\nearrow$ and $\searrow$) diagonal.

colouring

Exercice:
How to colour each region of the attached map, so that no two neighbouring regions hold the same colour? The problem consists in finding a colouring that minimises the number of colours used to paint the map.
Solution (Click to unfold) Each region is mapped to a colour (integer) taking values between 1 and n. A different ($\neq$) constraint is stated over all regions that are neighbouring.

You may recall the mathematical theorem stating that at most four colours are necessary for a proper colouring of such maps. We can use $n=4$ for a start (upper bound) and choose to decrease $n$ until no solution is found.

A more complicated—albeit general—approach would be to add an minimising criterion (optimisation) on the total number of colours used in a satisfying colouring.

The resolution process

Constraints as we state them do not necessarily apply to all variables which we use to define the problem.
Each constraint is associated with:

A common resolution process implements a depth-first search algorithm in a tree that is built on the fly:

backtrack

A word about arc-consistency

In the worst case, the tree is of an exponential size: efficient algorithms try to prune sub-trees as early as possible in the depth-first search process.

The basic backtracking algorithm we implemented on the toy problem shows several issues:

Important!
This step is called constraint propagation and is something we as humans are very capable at doing (unlike a search tree). In fine, Constraint programming is all about alternating hypotheses (branching) and constraint propagation (pruning).

A basic implementation of constraint propagation consists of maintaining arc-consistency, i.e. pruning the domains of variables which are not yet assigned but linked to assigned variables through binary constraints.

We can illustrate the benefits of this forward-checking approach with the 8-queen problem. When the first (red) queen is placed, we prune the domain of all the other queens (red dots).

nqueens step 1

Then, instead of trying position 1 and 2, the second (green) queen is directly placed on the first value of its subsequent domain, i.e. position 3.

nqueens step 2

When the third (blue) queen is placed, the domain of the sixth queen is reduced a single value (i.e. position 4);

nqueens step 3

however, the placement of the fourth (violet) queen eliminates all values of the domain of this queen. A backtrack can be triggered at that point.

nqueens step 4

Note that with this arc-consistency technique, the first backtrack occurs for instance $(1,3,5,2,\cdot,\cdot,\cdot,\cdot)$. Without this optimisation, we would have already backtracked 7 times before reaching this partial instantiation, and would still need to make several attempts/backtracks for queens #5 and #6 before realising they lead to no solution.

There are many algorithms designed to maintain arc-consistency and detect irrelevant sub-trees as soon as possible.

Symmetries

Consider the n-queen problem. A chessboard that is rotated by 90°, 180° or 270° yields queens positions that are different yet equivalent.

If the former chessboard is a solution to our problem, the latter also is. Conversely, if the former violates any constraint, the latter does so as well. A mirrored chessboard follows the same logics.

Formally speaking, a symmetry may be a permutation on the variables ($q_1 \rightleftharpoons q_4$) and/or on the values ($1 \rightleftharpoons 4$) of the problem.

symmetries

There are several ways to exploit the symmetries of a problem:

The bottom line when considering symmetries is to only try to exploit them if detecting them costs less than the speedup you can get from exploiting them.

« Previous | Up ↑ | Next »