Constraint Processing and Programming
Introductory Exemple
Javier Larrosa
Combinatorial Problem Solving
• Science and Engineering are full of Combinatorial
Problems
Solving algorithms are exponentially expensive
A lot of tricks can be applied to improve performance
• Naive Approach
For each problem, design and implement an efficient algorithm
Place numbers 1 through 8 on nodes
• each number appears exactly once
• no connected nodes have consecutive numbers
?
?
?
?
?
?
?
?
Note the graph symmetry
Acknowledgement: Patrick Prosser
Heuristic Search
?
?
?
?
?
?
?
?
Guess a value, but be prepared to backtrack
Which nodes are hardest to number?
Heuristic Search
?
?
?
?
?
?
?
?
Which nodes are hardest to number?
Heuristic Search
?
?
?
?
?
?
?
?
Which are the least constraining values to use?
Values 1 & 8
Heuristic Search
?
?
1
?
?
8
?
?
Symmetry means we don’t need to consider: 8 1
Inference/propagation
{1,2,3,4,5,6,7,8}
?
?
1
?
?
8
?
?
We can now eliminate many values for other nodes
Inference/propagation
{3,4,5,6}
?
?
1
By symmetry
?
{3,4,5,6}
?
8
?
?
Inference/propagation
{3,4,5,6}
?
{1,2,3,4,5,6,7,8}
?
?
1
?
{3,4,5,6}
8
?
?
Inference/propagation
{3,4,5,6}
?
{3,4,5,6}
?
?
1 8
By symmetry
?
{3,4,5,6}
?
{3,4,5,6}
?
Inference/propagation
{3,4,5,6}
?
{3,4,5,6}
?
{3,4,5,6,7} ?
1 8
?
{3,4,5,6}
?
{3,4,5,6}
?
{2,3,4,5,6}
Inference/propagation
{3,4,5,6}
?
{3,4,5,6}
?
7 1 8
?
{3,4,5,6}
?
{3,4,5,6}
2
Inference/propagation
{3,4,5,6}
?
{3,4,5,6}
?
7 1 8
?
{3,4,5,6}
And propagate
?
{3,4,5,6}
2
Inference/propagation
{3,4,5}
?
{4,5,6}
?
7 1
?
{3,4,5}
8
?
{4,5,6}
2
Inference/propagation
3
1 7
By symmetry
?
{3,4,5}
{4,5,6}
?
8
?
{4,5,6}
2
Inference/propagation
3
1 7
?
{3,4,5}
And propagate
{4,5,6}
?
8
?
{4,5,6}
2
Inference/propagation
7
?
{4,5}
More propagation?
3
1
{5,6}
?
8
?
{4,5,6}
2
Inference/propagation
7
3
1
4
A solution
5
8
6
2
The Declarative Approach to Combinatorial
Problem Solving
• Declarative Approach
1. Model the problem using a well-defined language
2. Solve the problem using general-purpose techniques
Constraint Programming and SAT solving follow this approach.
Constraint programming methodology
• Model problem
• specify in terms of constraints on acceptable solutions
• define/choose constraint model: variables, domains, constraints
• Solve model
• define/choose algorithm
• define/choose heuristics
• Verify and analyze solution
Constraint programming methodology
• Model problem
• specify in terms of constraints on acceptable solutions
• define/choose constraint model: variables, domains, constraints
• Solve model
• define/choose algorithm
• define/choose heuristics
• Verify and analyze solution
Constraint
Satisfaction
Problem
• A CSP is defined by
• a set of variables
• a domain of values for each variable
• a set of constraints between variables
• A solution is
• an assignment of a value to each variable that satisfies the constraints
Given a CSP
• Determine whether it has a solution or not
• Find any solution
• Find all solutions
• Find an optimal solution, given some cost function
Constraint model for puzzle variables v
1
, …, v
8 domains
{1, …, 8} constraints
| v
1
– v
2
|
1
| v
1
– v
3
|
1
…
| v
7
– v
8
|
1 alldifferent( v
1
, …, v
8
)
?
?
?
?
?
?
?
?
Example: instruction scheduling
Given a basic-block of code and a single-issue pipelined processor, find the minimum schedule
(a + b) + c
Example: evaluate (a + b) + c instructions
A r1
a
B r2
b
C r3
c
D r1
r1 + r2
E r1
r1 + r3
A dependency DAG
B
3
D
3
C
1 3
E
Example: evaluate (a + b) + c non-optimal schedule
A r1
a
B r2
b nop nop
D r1
r1 + r2
C r3
c nop nop
E r1
r1 + r3
A dependency DAG
B
3
D
1
3
E
3
C
Example: evaluate (a + b) + c optimal schedule
A r1
a
B r2
b
C r3
c nop
D r1
r1 + r2
E r1
r1 + r3
A dependency DAG
B
3
D
3
1 3
C
E
Constraint model variables
A, B, C, D, E domains
{1, …, m } constraints
D
A + 3
D
B + 3
E
C + 3
E
D + 1 alldifferent(A, B, C, D, E)
A dependency DAG
B
3 3
D C
1 3
E
Example: Graph coloring
Given k colors, does there exist a coloring of the nodes such that adjacent nodes are assigned different colors
Example: 3-coloring variables: v
1
, v
2
, v
3
, v
4
, v
5 domains:
{1, 2, 3} constraints: v i
v j if v i and v are adjacent j v
1 v
3 v
2 v
4 v
5
Example: 3-coloring
A solution v
1 v
2 v v v
3
4
5
1
2
2
1
3 v
1 v
3 v
2 v
4 v
5
Example: n -queens
Place n -queens on an n
n board so that no pair of queens attacks each other
Constraint model variables: x
1
, x
2
, x
3
, x
4 domains:
{1, 2, 3, 4} constraints: x i
x j and
| x i
x j
|
| i j |
3
4
1
2 x
1 x
2 x
3 x
4
Example: 4-queens
A solution x
1
2 x
2
4 x
3
1 x
4
3
4 x
1
3
1
2
Q x
2 x
3
Q
Q x
4
Q
Constraint programming methodology
• Model problem
• specify in terms of constraints on acceptable solutions
• define/choose constraint model: variables, domains, constraints
• Solve model
• define/choose algorithm
• define/choose heuristics
• Verify and analyze solution
Constraint programming methodology
• Model problem
• specify in terms of constraints on acceptable solutions
• define/choose constraint model: variables, domains, constraints
• Solve model
• define/choose algorithm
• define/choose heuristics
• Verify and analyze solution
Application areas
• Paradigm of choice for many hard combinatorial problems
• scheduling
• planning
• vehicle routing
• configuration
• bioinformatics
• …
Commercial applications