CSP 6
CSP 6
Adrian Groza
Outline
1 Constraint Reasoning
Example
Constraints programming represents one of the
closest approaches computer science has made to
the Holy Grail of programming: the user states the
problem, the computer solves it. Eugene C. Freuder,
Inaugural issue of the Constraints Journal, 1997.
Place numbers 1 through 8 on nodes
1 each number appears exactly once
2 no connected nodes have consecutive numbers
Constraint Reasoning Constraint Programming and Backtracking
Heuristic Search
”To succeed, try first where you are most likely to fail.”
”Deal with hard cases first: they can only get more difficult if you put them off.”
By simmetry
Constraint Reasoning Constraint Programming and Backtracking
By symmetry
Constraint Reasoning Constraint Programming and Backtracking
And propagate
Constraint Reasoning Constraint Programming and Backtracking
And propagate
Constraint Reasoning Constraint Programming and Backtracking
More propagation?
Constraint Reasoning Constraint Programming and Backtracking
A solution
Constraint Reasoning Constraint Programming and Backtracking
1 Model problem
specify in terms of constraints on acceptable solutions:
constraint satisfaction problem
define/choose constraint model: variables, domains,
constraints
2 Solve model
define/choose algorithm
define/choose heuristics
3 Verify and analyze solution
Constraint Reasoning Constraint Programming and Backtracking
Constraints Properties
A logical relation among several unknowns (variables)
May specify partial information: X > 2, ”the circle is inside
the square”
Non-directional: two variables X, Y can be used to infer a
constraint on X given a constraint on Y and vice versa:
X=Y+2
Declarative: specify what relationship must hold without
specifying a computational procedure to enforce that
relationship
Additive: the order of imposition of constraints does not
matter, all that matters, at the end is that the conjunction of
constraints is in effect
Rarely independent: typically constraints in the constraint
store share variables.
Constraint Reasoning Constraint Programming and Backtracking
A CSP is defined by
a set of variables: X , Y , Z ,...
a domain of values for each variable: X :: {1, 2}, Y :: {1, 2},
Z :: {1, 2}
a set of constraints between variables: X = Y , X 6= Z ,
Y >Z
A solution is an assignment of a value to each variable that
satisfies the constraints: X = 2, Y = 2, Z = 1
Given a CSP
Determine whether it has a solution or not (satisfiability)
Find any solution
Find all solutions
Find an optimal solution, given some cost function
Constraint Reasoning Constraint Programming and Backtracking
Puzzle
Map coloring
N-queen problem
Sudoku
Cryptarithmetic problem
Constraint Reasoning Constraint Programming and Backtracking
variables
v1 , ..., v8
domains
{1, ..., 8}
constraints
|v1 − v2 | = 6 1
|v1 − v3 | = 6 1
..
. |v7 − v8 | =
6 1
alldifferent(v1 , ..., v8 )
Constraint Reasoning Constraint Programming and Backtracking
Place n-queens on an n x n
board so that no pair of queens
attacks each other variables
x1 , x2 , x3 , x4
domains
{1, 2, 3, 4}
constraints
xi 6= xj and
|xi − xj | =
6 |i − j|
a solution
x1 ← 2, x2 ← 4, x3 ← 1, x4 ← 3
Constraint Reasoning Constraint Programming and Backtracking
Crypto-arithmetic problem
More Constraints?
S 6= 0, M 6= 0
M = 1, S = 9, O = 0,
N + R > 10
N = E + 1, E in 2..7
Constraints
N in 3..8, D, R, Y in 2..8
[S,E,N,D,M,O,R,Y] in 0..9
R in 3..8
alldifferent([S,E,N,D,M,O,R,Y])
N + R = 1E → E < 7
1000*S + 100*E + 10*N +
D+1000*M + 100*O + 10*R + R = 8 from N + R > 10,
E = 10000*M + 1000*O + N =E +1
100*N + 10*E + Y E in 2..6, N in 3..7
Constraint Reasoning Constraint Programming and Backtracking
Crypto-arithmetic problem
E+D = Y+10*C1
C1+N+R = E+10*C2
C2+E+O = N+10*C3
C3+S+M = 10*M+O
E,N,D,O,R,Y::{0,..,9}
S,M::{1,..,9}
C1,C2,C3::{0,1}
Constraint Reasoning Constraint Programming and Backtracking
SEND+MOST=MONEY
Money should be maximal
Constraint Reasoning Constraint Programming and Backtracking
Car assembly - 15 tasks: install axles (front and back), affix all
four wheels (right and left, front and back), tighten nuts for each
wheel, affix hubcaps, and inspect the final assembly, in
maximum 30 minutes.
Variables
X = {AxleF , AxleB , WheelRF , WheelLF , WheelRB , WheelLB , NutsRF ,
NutsLF , NutsRB , NutsLB , CapRF , CapLF , CapRB , CapLB , Inspect}
Precedence constraints: T1 + d1 ≤ T2
Axles have to be in place before the wheels are put on, and it
takes 10 minutes to install an axle,
AxleF + 10 ≤ WheelRF AxleF + 10 ≤ WheelLF
AxleB + 10 ≤ WheelRB AxleB + 10 ≤ WheelLB
Constraint Reasoning Constraint Programming and Backtracking
Global constraints
Involves an arbitrary number of constraints (but not necessarily all variables).
Global constraints
Outline
1 Constraint Reasoning
Backtracking
The basic uninformed algorithm for CSP.
Generate and Test Incrementally extends a partial solution
towards a complete solution
Probably the most general
problem solving method Algorithm:
Algorithm: Build a partial solution: a partial
consistent assignment
1 generate labelling
Extend consistently the partial
2 test satisfaction
solution: one new assigned variable
Drawbacks: blind generator, late each time
Constraint Reasoning Constraint Programming and Backtracking
Choose the variable with the fewest legal values (or most
constrained variable or “fail-first” heuristic)
Constraint Reasoning Constraint Programming and Backtracking
Degree heuristic
Constraint propagation
Constraint propagation
Weaknesses of Backtracking
Thrashing: throws away the reason of the conflict
Example: A, B, C, D, E :: 1..10, A > E: tries all the assignments for B,C,D
before finding that A 6= 1
The chronological backtracking backtracks to the Queen 5 and it finds
another column for this queen (column H). However, it is still impossible to
place the Queen 6
Solution: backjumping (jump to the source of the failure)
The closest queen that can resolve the conflict is Queen 4 because then
there is a chance that column D can be used for Queen 6.
Constraint Reasoning Constraint Programming and Backtracking
Weaknesses of Bactkracking
Redundant Work
Unnecessary constraint checks are repeated
Even if the conflicting values of variables they are not remembered
Constraint Reasoning Constraint Programming and Backtracking
Weaknesses of Backtracking
Forward Checking
Constraint Reasoning Constraint Programming and Backtracking