Lec13.ppt

Lecture 13:
Modeling in
Constraint Programming
© J. Christopher Beck 2008
1
Outline

Introduction



The Crystal Maze
Heuristics, Propagation, B&B
Modeling in CP
© J. Christopher Beck 2008
2
Readings


P Ch 5.5, D.2, D.3
B.M. Smith,
“Modelling”, The
Handbook of
Constraint
Programming,
2006.
© J. Christopher Beck 2008
3
You have
5 minutes!
Crystal Maze

Place the numbers 1 through 8 in the
nodes such that:
Each number appears exactly once
– No connected
?
?
nodes have
consecutive
numbers
?
?
?

© J. Christopher Beck 2008
?
?
?
4
Modeling



Each node  a variable
{1, …, 8}  values in the domain of
each variable
No consecutive numbers  a constraint


(vi, vj)  |vi – vj| > 1
All values used  all-different
constraint
© J. Christopher Beck 2008
5
Heuristic Search
{1, 2, 3, 4, 5, 6, 7, 8}
?
© J. Christopher Beck 2008
?
?
1?
8?
?
?
?
6
Inference/Propagation
{1, 2, 3, 4, 5, 6, 7, 8}
?
?
?
1?
8?
?
?
?
{1, 2, 3, 4, 5, 6, 7, 8}
© J. Christopher Beck 2008
7
Inference/Propagation
{3, 4, 5, 6}
4?
{3, 4, 5, 6}
6?
{3, 4, 5, 6, 7}
7?
{2, 3, 4, 5, 6}
1?
8?
3?
5?
{3,
4,
5,
6}
© J. Christopher Beck 2008
{3, 4, 5, 6}
2?
8
Generic CP Algorithm
Start
Solution?
Propagators
Success
Dead-end?
Backtrack
Technique
Nothing to
retract?
Failure
© J. Christopher Beck 2008
Make
Heuristic
Decision
Assert
Commitment
9
Questions?
With CP You Are …

Guaranteed to find a solution if one
exists


But you need to have enough time!


If not you can prove there is no solution
On average, for many problems you can
find a solution within a reasonable time
CP is commercially successful for
scheduling and other applications
© J. Christopher Beck 2008
10
The Core of CP

Modeling


Heuristic search



How to branch
How much effort to find a good branch
Inference/propagation


How to represent the problem
How much effort
Backtracking
© J. Christopher Beck 2008
11
Constraint Satisfaction
Problem (CSP)

Given:




V, a set of variables {v0, v1, …, vn}
D, a set of domains {D0, D1, …, Dn}
C, a set of constraints {c0, c1, …, cm}
Each constraint, ci, has a scope
ci(v0, v2, v4, v117, …), the variables that
it constrains
© J. Christopher Beck 2008
12
Constraint Satisfaction
Problem (CSP)

A constraint, ci, is a mapping from the
elements of the Cartesian product of
the domains of the variables in its scope
to {T,F}

ci(v0, v2, v4, v117, …) maps:
(D0 X D2 X D4 X D117 X … )  {T,F}
A constraint is satisfied if the
assignment of the variables in its scope
are
mapped
to
T
13
© J. Christopher Beck 2008

Constraint Satisfaction
Problem (CSP)

In a solution to a CSP:


each variable is assigned a value from its
domain: vi = di, di є Di
each constraint is satisfied
© J. Christopher Beck 2008
14
Variables & Constraints

Variables can be anything



colors, times, countries, …
often integer valued or binary
Constraints are not limited to linear
constraints


extensional: list of acceptable valuecombinations
intensional: mathematical expression
© J. Christopher Beck 2008
15
Examples



V = {v1, v2, …, vn}
Di = {1, 2, …, n}
all-diff(v1, v2, …, vn)



each of the variables must take on a
different value
v1 > v 2 + v 4
v4 = v5 * v6
© J. Christopher Beck 2008
16
n-Queens Problem

© J. Christopher Beck 2008
Place n-Queens on
the chess board so
that no queen can
attack any other
17
n-Queens Problem


© J. Christopher Beck 2008
Variables?
Constraints?
18
n-Queens Problem

© J. Christopher Beck 2008
How about a
different model?
19
Why Do We Care About
Modeling?



You need to represent your problem!
As in MIP/LP different models are
harder or easier to solve
We will return to CP modeling (in a big
way) in a few weeks
© J. Christopher Beck 2008
20