Constraint Programming Marco Kuhlmann & Guido Tack Lecture 1 - - PowerPoint PPT Presentation
Constraint Programming Marco Kuhlmann & Guido Tack Lecture 1 - - PowerPoint PPT Presentation
Constraint Programming Marco Kuhlmann & Guido Tack Lecture 1 Welcome! Where am I? Constraint Programming advanced course 6 credit points lecture (2 hours) + lab (2 hours) WWW: http://www.ps.uni-sb.de/ This lecture
Welcome!
Where am I?
- Constraint Programming
- advanced course
- 6 credit points
- lecture (2 hours) + lab (2 hours)
- WWW: http://www.ps.uni-sb.de/
This lecture
- constraint programming
- what it is – fundamental concepts
- why it matters – applications
- how it works – showcase examples
- organisation
Constraint Programming
S E N D M O R E + M O N E Y
Send More Money
Generate & Test
- generate
all possible combinations
- f all possible values
for all letters in the puzzle
- test
for each potential solution, whether it satisfies the equation
Ridiculous!
Specialised algorithms
- advantages
- may be highly efficient
- offer deep insights into the problem
- disadvantages
- may take years to develop
- cannot be easily adapted
Enter Constraint Programming
Constraint programming is a problem-solving technique for combinatorial problems that works by incorporating constraints into a programming environment. (after Apt 2003)
Combinatorial problems
- combinatorial structures
characterised in terms of a finite set of variables and a finite value for each variable
- combinatorial problem
question about properties
- f a class of combinatorial structures
subsets
Example
- combinatorial structures:
permutations of the primes between 1 and 10
- combinatorial problems:
How many such structures are there … without any additional properties? … where seq[0] > seq[1]? … where seq[0] + seq[1] = seq[2]?
Subsets & constraints
seq[0] > seq[1] seq[0] + seq[1] = seq[2]
subsets
Combinatorial problems
- combinatorial structures
characterised in terms of a finite set of variables and a finite value for each variable
- combinatorial problem
question about properties
- f a class of combinatorial structures
constraints
Constraint Satisfaction Problems (CSPs) simple formal model for combinatorial problems
CSPs: Ingredients
- finite set of problem variables, x
- associated domains dom(x)
- finite set of constraints
intensional
- r extensional
CSPs: Terminology
- variable assignment:
total function that maps each x to an element in dom(x)
- solution to a CSP:
variable assignment such that all constraints are satisfied
How to integrate constraints into a programming environment?
Meet Alice
- Alice is a better Standard ML:
it supports concurrent and distributed programming.
- Alice features the GeCoDE library,
the bleeding edge of research in constraint systems.
- WWW: http://www.ps.uni-sb.de/alice/
fun example1 space = let val dom = domain [2, 3, 5, 7] val sequence = fdtermVec (space, 4, dom) val seq0 = Vector.sub (sequence, 0) val seq1 = Vector.sub (sequence, 1) val seq2 = Vector.sub (sequence, 2) in distinct (space, sequence); (* seq[0] > seq[1] *) post (space, seq0 `> seq1); (* seq[0] + seq[1] = seq[2] *) post (space, seq0 `+ seq1 `= seq2); branch (space, sequence) end
Constraints in Alice
Constraint Propagation
Example
problem variables: x, y domains:
- dom(x) = {3,4,5},
- dom(y) = {3,4,5}
constraints:
- x ≥ y,
- y > 3
x = 4, y = 4 x = 5, y = 4 x = 5, y = 5 finite sets
- f integers
distinguish two sorts of constraints: basic constraints and non-basic constraints
x {3,4,5} y {3,4,5}
Constraint Store
basic constraints
Propagators
- implement non-basic constraints
- translate into basic constraints
- subscribe to variables in the store
- get notified about changes
Propagators
y > 3 x y
amplify store
y {3,4,5} x {3,4,5} y {4,5} x {4,5}
gets notified
x {3,4,5} y {3,4,5} x y y > 3
Computation Space
constraint store with connected propagators
Important concepts
- constraint store
stores basic constraints
- propagator
implements non-basic constraint
- computation space
constraint store + propagators
Branching
x {4,5} y {4,5} x y
Bad news
propagation is not enough!
Stable spaces
- solution
for each x, dom(x) is a singleton
- failure
there is an x with dom(x) empty
- choice
x {4,5} y {4,5} x y x {4} x {4} x {4} x {5} y {4,5} x y x y y {4,5} y {4}
Branching
stable space domain split
Search tree
choice solution
Branching heuristics
- naive heuristic
- pick some x with dom(x) > 1
- pick value k from dom(x)
- branch with x ∈ {n} and x ∉ {n}
- first-fail heuristic:
pick x with dom(x) minimal
Branching strategy
- variable selection
- any variable
- minimal/maximal current domain
- value selection (for the left branch)
- maximal/minimal/medial element
- lower half/upper half of the domain
Homework
Show that the branching strategy can have a massive impact
- n the size of the search tree.
Search
Search
- propagation and branching
induce a search tree
- orthogonal issue:
in what order are the nodes of that tree constructed?
- different problems require
different search strategies
Static search strategies
- explore the search tree
- standard search strategies
- depth-first search
- iterative deepening
- A* search
Dynamic search
- add new constraints during search
- dynamic search strategies
- iterative best-solution search
- branch-and-bound search
best solution search
Best Solution Search
- class of combinatorial structures σ
- objective function obj : σ → N
- find a structure s such that obj(s) is
- ptimal among all structures σ
Best Solution Search
- naive approach:
compute all solutions & choose best
- branch-and-bound approach:
compute first solution, s add ‘better-than-s’ constraint compute next solution, and iterate
prunes the search tree
S E N D M O S T + M O N E Y
Send Most Money
MONEY should be maximal
SMM+ – B & B
unexplored subtree add betterness constraint add betterness constraint best solution first solution
SMM+
SMM+
What this course will be about
Architecture
- propagation:
prune impossible values
- branching:
divide the problem into smaller parts
- search:
interleave propagation and branching to find solutions
What you will learn
- how to model combinatorial problems
- how to solve them using CP
- how to write new propagators
- how to program new search strategies
- how to apply CP to practical problems
Applications
- timetabling
- crew rostering
- gate allocation at airports
- sports league scheduling
- natural language processing
Scheduling resources
- tasks
duration, used resources
- precedence constraints
task a must precede task b
- resource constraints
at most one task per resource
First assignment
- install the tools:
http://www.ps.uni-sb.de/alice/
- warm-up in programming Alice
- model and solve some simple
constraint satisfaction problems
Constraint Programming
- can be used to tackle hard
combinatorial problems
- combines various interesting
methodologies and techniques
- applications are ubiquitous
knowledgeable people are not!
Constraint Programming
- compute with possible values
lower bound, upper bound
- prune inconsistent values
guessing as last resort
- factorise the problem
inferences + heuristics + search
What CP is not
- no efficiency miracle
- exponential problems remain
exponential
- If you have polynomial algorithms,
use them, and forget about CP!
- no replacement, but a complement
to other programming paradigms
What you should bring
- broad interest in computer science
- theory and formal models
- practice and programming
- self-initiative
- try! explore! do!
- ask questions, and answer them
Caveat
- CP is established …
- international conferences
- many results & applications
- … our tools are not (yet)
- some might not work (as expected)
- some might be uncomfortable
Organisation
Literature
- Krzysztof R. Apt:
Principles of Constraint Programming Cambridge University Press, 2003
- Christian Schulte:
Programming Constraint Services Springer-Verlag, 2002
Lectures
- 12 lectures in total
- last lecture on July 14
- no lectures on
- May 5 (Ascension)
- May 26 (Corpus Christi)
Labs
- explorative labs
- get familiar with the concepts
- get familiar with the tools
- graded labs
- four medium-sized projects
- determine 40% of your final grade
Exam
- at the end of the term
- determines 60% of final grade
- written or oral (to be determined)
- re-exam will be oral
Contact & Support
- mailing list
subscribe on web site
- office hours
Wednesdays, 14–15, room 45.517
- during & after the lectures
Constraint Programming
- problem-solving technique
- interleaves inferences & heuristics
- combines various methodologies
- is fun!
Thanks for your attention!
Backup Slides
Eight Queens
Problem specification
Place 8 queens on a chessboard such that no two queens attack each other.
History of Eight Queens
- originally proposed in 1842
by Max Bazzel, a chess player
- studied by various mathematicians
- George Pólya even studied it
- n donut-shaped chessboards
Model
- problem variables: row[0], … , row[7]
- dom(row[k]) = {0,…,7}
- assign row[k] = i iff
the queen in the k-th column should be placed in the i-th row
Constraints
- no two queens in the same column:
by construction
- no two queens in the same row:
distinct(row[0], … , row[7])
- no two queens in the same diagonal:
row[i] + (j - i) ≠ row[j] and row[i] – (j - i) ≠ row[j], for all 0 ≤ i < j ≤ 7
fun queens space = let val row = Linear.fdtermVec (space, 8, [0`#7]) in Linear.distinct (space, row); List.app (fn (i, j) => let val rowi = Vector.sub (row, i) val rowj = Vector.sub (row, j) in post (space, rowi `+ (`j `- `i) `<> rowj); post (space, rowi `– (`j `- `i) `<> rowj) end) (triangle 8); Linear.branch (space, row, FD.B_SIZE_MIN, FD.B_MED); row end