ID2204: Constraint Programming
Introduction & Overview
Lecture 01, 2015-03-23
Christian Schulte
Software and Computer Systems
School of Information and Communication Technology KTH – Royal Institute of Technology
Lecture Overview
What is Constraint Programming?
Sudoku is Constraint Programming
... more later
Sudoku
...is Constraint Programming!
Sudoku
Assign blank fields digits such that:
digits distinct per rows, columns, blocks
9 2
2 5
9
7 3 6 2
6 9
7
4 9
1 8
6 3 4
6 8
1 8
Sudoku
Assign blank fields digits such that:
9 2
2 5
9
7 3 6 2
6 9
7
4 9
1 8
6 3 4
6 8
1 8
Sudoku
Assign blank fields digits such that:
digits distinct per rows, columns, blocks
9 2
2 5
9
7 3 6 2
6 9
7
4 9
1 8
6 3 4
6 8
1 8
Sudoku
Assign blank fields digits such that:
9 2
2 5
9
7 3 6 2
6 9
7
4 9
1 8
6 3 4
6 8
1 8
Block Propagation
No field in block can take digits 3,6,8
8
6 3
Block Propagation
No field in block can take digits 3,6,8
propagate to other fields in block
1,2,4,5,7,9
8
1,2,4,5,7,91,2,4,5,7,9
6 3
1,2,4,5,7,9 1,2,4,5,7,9 1,2,4,5,7,9
Propagation
Prune digits from fields such that:
digits distinct per rows, columns, blocks
9 2
2 5
9
7 3 6 2
6 9
7
4 9
1 8
6 3 4
6 8
1 8
1,2,3,4,5,6,7,8,9
Propagation
Prune digits from fields such that:
9 2
2 5
9
7 3 6
9 6
2
7
1 9 4
8 6 3
4
6 8
1 8
1,3,5,6,7,8
Propagation
Prune digits from fields such that:
digits distinct per rows, columns, blocks
9
2 9
5 2
7 3 6 2
6 9
7
4 9
1 8
6 3
8 6
4 1
8
1,3,6,7
Propagation
Prune digits from fields such that:
9 2
2 5
9
7 3 6 2
6 9
7
4 9
1 8
6 3 4
6 8
1 8
1,3,6
Iterated Propagation
Iterate propagation for rows, columns, blocks
What if no assignment: search... later
9 2
2 5
9
7 3 6 2
6 9
7
4 9
1 8
6 3 4
6 8
1 8
Sudoku is Constraint Programming
Modelling: variables, values, constraints
9 2
2 5
9
7 3 6 2
6 9
7
4 9
1 8
6 3 4
6 8
1 8
Variables: fields
take values: digits
maintain set of possible values
Constraints: distinct
relation among variables
Constraint Programming
Variable domains
finite domain integer, finite sets, multisets, intervals, ...
Constraints
distinct, arithmetic, scheduling, graphs, ...
Solving
propagation, branching, exploration, ...
Modelling
variables, values, constraints, heuristics, symmetries, ...
Plan of Lecture
Introduction
what is constraint programming?
principles and applications
Overview
course content
course goal
Organizational
What Is Constraint
Programming?
Running Example: SMM
Find distinct digits for letters, such that
SEND + MORE
= MONEY
Constraint Model for SMM
Variables:
S,E,N,D,M,O,R,Y {0,…,9}
Constraints:
distinct(S,E,N,D,M,O,R,Y)
1000×S+100×E+10×N+D + 1000×M+100×O+10×R+E
= 10000×M+1000×O+100×N+10×E+Y S0 M0
Solving SMM
Find values for variables
such that
all constraints satisfied
Finding a Solution
Compute with possible values
rather than enumerating assignments
Prune inconsistent values
constraint propagation
Search
branch: define search tree
explore: explore search tree for solution
Constraint Propagation
Important Concepts
Constraint store
Propagator
Constraint propagation
Constraint Store
Maps variables to possible values
stores basic constraints
x{3,4,5} y{3,4,5}
Constraint Store
Maps variables to possible values
Others: finite sets, intervals, trees, ...
x{3,4,5} y{3,4,5}
finite domain constraints
Propagators
Implement (non-basic) constraints distinct(x1,…,xn)
x + 2*y = z
Propagators
Amplify store by constraint propagation x{3,4,5} y{3,4,5}
xy y>3
Propagators
Amplify store by constraint propagation x{3,4,5} y{3,4,5}
xy y>3
Propagators
Amplify store by constraint propagation x{3,4,5} y{4,5}
xy y>3
Propagators
Amplify store by constraint propagation x{3,4,5} y{4,5}
xy y>3
Propagators
Amplify store by constraint propagation x{4,5} y{4,5}
xy y>3
Propagators
Amplify store by constraint propagation
Disappear when done (subsumed, entailed) x{4,5} y{4,5}
xy y>3
Propagators
Amplify store by constraint propagation
Disappear when done (subsumed, entailed)
no more propagation possible
x{4,5} y{4,5}
xy
Propagation for SMM
Results in store
S{9} E{4,…,7} N{5,…,8} D{2,…,8}
M{1} O{0} R{2,…,8} Y{2,…,8}
Propagation alone not sufficient!
create simpler sub-problems
branching
Constraints and Propagators
Constraints state relations among variables
which value combinations satisfy constraint
Propagators implement constraints
prune values in conflict with constraint
Constraint propagation drives propagators for several constraints
Search
Important Concepts
Branching
Exploration
Branching heuristics
Best-solution search
Search: Branching
Create subproblems with additional information
enable further constraint propagation x{4,5} y{4,5}
xy
x{4} y{4}
xy
x{5} y{4,5}
xy
x=4 x4
Example Branching Strategy
Pick variable x with at least two values
Pick value n from domain of x
Branch with
x=n and xn
Part of model
Search: Exploration
Iterate propagation and branching
Orthogonal: branching exploration Nodes:
SMM: Unique Solution
SEND + MORE
= MONEY 9567 + 1085
= 10652
Heuristics for Branching
Which variable
least possible values (first-fail)
application dependent heuristic
Which value
minimum, median, maximum
x=m or xm
split with median m
x<m or xm
Problem specific
SMM: Solution With First-fail
SEND + MORE
= MONEY 9567 + 1085
= 10652
Send Most Money (SMM++)
Find distinct digits for letters, such that
and MONEY maximal
SEND + MOST
= MONEY
Best Solution Search
Naïve approach:
compute all solutions
choose best
Branch-and-bound approach:
compute first solution
add “betterness” constraint to open nodes
next solution will be “better”
prunes search space
Branch-and-bound Search
Branch-and-bound Search
Explore with additional constraint
Branch-and-bound Search
Branch-and-bound Search
Guarantees better solutions
Branch-and-bound Search
Branch-and-bound Search
Last solution best
Branch-and-bound Search
Modelling SMM++
Constraints and branching as before
Order among solutions with constraints
so-far-best solution S,E,N,D,M,O,T,Y
current node S,E,N,D,M,O,T,Y
constraint added
10000×M+1000×O+100×N+10×E+Y
<
10000×M+1000×O+100×N+10×E+Y
SMM++: Branch-and-bound
SEND + MOST
= MONEY 9782 + 1094
= 10876
SMM++: All Solution Search
SEND + MOST
= MONEY 9782 + 1094
= 10876
Summary
Summary: Key Ideas and Principles
Modelling
variables with domain
constraints to state relations
branching strategy
solution ordering
Solving
constraint propagation
constraint branching
search tree exploration
applications
principles
Widely Applicable
Timetabling
Scheduling
Crew rostering
Resource allocation
Workflow planning and optimization
Gate allocation at airports
Sports-event scheduling
Railroad: track allocation, train allocation, schedules
Automatic composition of music
Genome sequencing
Frequency allocation
…
Draws on Variety of Techniques
Artificial intelligence
basic idea, search, ...
Operations research
scheduling, flow, ...
Algorithms
graphs, matching, networks, ...
Programming languages
programmability, extensionability, ...
Essential Aspect
Compositional middleware for combining
smart algorithmic
problem substructures
components (propagators)
scheduling
graphs
flows
…
plus
Principles
Models for constraint propagation
properties and guarantees
Strong constraint propagation
global constraints with strong algorithmic methods
mantra: search kills, search kills, search k…
Branching strategies
Exploration strategies
SMM: Strong Propagation
SEND + MORE
= MONEY 9567 + 1085
= 10652
Scheduling Resources
Modelling
Propagation
Strong propagation
Scheduling Resources: Problem
Tasks
duration
resource
Precedence constraints
determine order among two tasks
Resource constraints
at most one task per resource
Scheduling: Bridge Example
Infamous:
additional side
constraints!
Scheduling: Solution
Start time for each task
All constraints satisfied
Earliest completion time
minimal make-span
Scheduling: Model
Variable for start-time of task a start(a)
Precedence constraint: a before b start(a) + dur(a) start(b)
Propagating Precedence
start(a){0,…,7}
start(b){0,…,5}
a b
a before b
Propagating Precedence
start(a){0,…,7}
start(b){0,…,5}
a b
a
b
a before b
start(a){0,…,2}
start(b){3,…,5}
Scheduling: Model
Variable for start-time of task a start(a)
Precedence constraint: a before b start(a) + dur(a) start(b)
Resource constraint:
a before b or
b before a
Scheduling: Model
Variable for start-time of task a start(a)
Precedence constraint: a before b start(a) + dur(a) start(b)
Resource constraint:
start(a) + dur(a) start(b) or
b before a
Scheduling: Model
Variable for start-time of task a start(a)
Precedence constraint: a before b start(a) + dur(a) start(b)
Resource constraint:
start(a) + dur(a) start(b) or
start(b) + dur(b) start(a)
Reified Constraints
Use control variable b{0,1}
c b=1
Propagate
c holds propagate b=1
c holds propagate b=0
b=1 holds propagate c
b=0 holds propagate c
Reified Constraints
Use control variable b{0,1}
c b=1
Propagate
c holds propagate b=1
c holds propagate b=0
b=1 holds propagate c
b=0 holds propagate c
not easy!
Reification for Disjunction
Reify each precedence
[start(a) + dur(a) start(b)] b0=1 and
[start(b) + dur(b) start(a)] b1=1
Model disjunction b0 + b1 1
Model Is Too Naive
Local view
individual task pairs
O(n2) propagators for n tasks
Global view ("global" constraints)
all tasks on resource
single propagator
smarter algorithms possible
Example: Edge Finding
Find ordering among tasks (“edges”)
For each subset of tasks {a}B
assume: a before B
deduce information for a and B
assume: B before a
deduce information for a and B
join computed information
can be done in O(n2)
Summary
Modeling
easy but not always efficient
constraint combinators (reification)
global constraints
smart heuristics
More on constraint-based scheduling
Baptiste, Le Pape, Nuijten. Constraint-based Scheduling, Kluwer, 2001.
Course Overview
Content Overview
As to be expected, no surprises:
applications principles pragmatics limitations
Modeling with CP
Basic solving methods
constraint propagation
search
Typical techniques for modeling in different application areas
redundant constraints, symmetry elimination
Refining models by strong algorithmic methods
Heuristic search methods
Application to hard real-size problems
Principles Underlying CP
Models for
propagation
search
and their essential properties
Different levels of consistency (propagation strength)
Different constraint domains
finite domains, finite sets, ...
Strong Algorithmic Methods
Régin's distinct algorithm
Edge-finding
Integration
achieving required properties for propagation
Relation to Other Techniques
Integer programming
Local search
Discussion of merits and weaknesses
Hybrid approaches
Goals: Learning Outcomes
explain and apply basic modeling techniques for constraint
problems, including the selection of variables, constraints, and optimization criteria.
describe and apply depth-first search and branch-and-bound search for solving constraint problems.
describe and define constraint propagation, search branching, and search tree exploration
prove correctness, consistency, and completeness of propagators implementing constraints.
define and prove correctness of branching strategies.
describe optimizations of constraint propagation based on fixpoint reasoning.
Goals: Learning Outcomes
describe advanced modeling techniques, analyze combinatorial problems for the applicability of these techniques, and apply and combine them
techniques include: general symmetries, value and variable
symmetries, symmetry breaking with constraints, symmetry breaking during search, domination constraints, redundant constraints,
redundant modeling and channeling, using strong algorithmic techniques, and branching heuristics.
describe and apply Régin's algorithm for the distinct constraint as an example of strong constraint propagation
explain algorithms for the element constraint, linear constraints, and disjunctive scheduling constraints.
implement a simple propagation algorithm.
describe the main strength and weaknesses of constraint
Organizational
Material
Lecture notes (slides)
available before the lectures…
Additional material
book excerpts
scientific articles
notes written by me
Modeling and Programming with Gecode
How to Pass?
Pass exam
has 200 (3 hour exam) exam points
100 total points needed
grading scale linear (see www)
Total pts = exam pts + bonus pts
Bonus points
from assignments
at most 20 points
Assignments
Four assignments
each 5 bonus points if submitted in time
one to three weeks for solving
Points only valid in this academic year!
Assignment Tasks
Exploration tasks
small tryouts
need to be done in order to do…
Submission tasks
submit in time, get bonus points
do them, do them
submit to me by email [email protected]
Both practical and principles
Software: Gecode
Gecode C++ library
course web: links to Gecode page
Download and install
at least version 4.4.0 (available on Monday)
Start reading
“Modeling and Programming with Gecode”
at the beginning of Part M are reading instructions
Summary
Constraint programming…
…is exciting!
…is fun!
Understanding of principles and applications necessary
Read the webpage
https://www.kth.se/social/course/ID2204/
Acknowledgments
I am grateful to Pierre Flener for helpful
comments and bugreports on these slides