Constraint Programming Marco Kuhlmann & Guido Tack Lecture 1 - - PowerPoint PPT Presentation

constraint programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Constraint Programming

Marco Kuhlmann & Guido Tack Lecture 1

slide-2
SLIDE 2

Welcome!

slide-3
SLIDE 3

Where am I?

  • Constraint Programming
  • advanced course
  • 6 credit points
  • lecture (2 hours) + lab (2 hours)
  • WWW: http://www.ps.uni-sb.de/
slide-4
SLIDE 4

This lecture

  • constraint programming
  • what it is – fundamental concepts
  • why it matters – applications
  • how it works – showcase examples
  • organisation
slide-5
SLIDE 5

Constraint Programming

slide-6
SLIDE 6

S E N D M O R E + M O N E Y

Send More Money

slide-7
SLIDE 7

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

slide-8
SLIDE 8

Ridiculous!

slide-9
SLIDE 9

Specialised algorithms

  • advantages
  • may be highly efficient
  • offer deep insights into the problem
  • disadvantages
  • may take years to develop
  • cannot be easily adapted
slide-10
SLIDE 10

Enter Constraint Programming

slide-11
SLIDE 11

Constraint programming is a problem-solving technique for combinatorial problems that works by incorporating constraints into a programming environment. (after Apt 2003)

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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]?

slide-14
SLIDE 14

Subsets & constraints

seq[0] > seq[1] seq[0] + seq[1] = seq[2]

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Constraint Satisfaction Problems (CSPs) simple formal model for combinatorial problems

slide-17
SLIDE 17

CSPs: Ingredients

  • finite set of problem variables, x
  • associated domains dom(x)
  • finite set of constraints

intensional

  • r extensional
slide-18
SLIDE 18

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

slide-19
SLIDE 19

How to integrate constraints into a programming environment?

slide-20
SLIDE 20

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/
slide-21
SLIDE 21

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

slide-22
SLIDE 22

Constraint Propagation

slide-23
SLIDE 23

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
slide-24
SLIDE 24

distinguish two sorts of constraints: basic constraints and non-basic constraints

slide-25
SLIDE 25

x {3,4,5} y {3,4,5}

Constraint Store

basic constraints

slide-26
SLIDE 26

Propagators

  • implement non-basic constraints
  • translate into basic constraints
  • subscribe to variables in the store
  • get notified about changes
slide-27
SLIDE 27

Propagators

y > 3 x y

amplify store

y {3,4,5} x {3,4,5} y {4,5} x {4,5}

gets notified

slide-28
SLIDE 28

x {3,4,5} y {3,4,5} x y y > 3

Computation Space

constraint store with connected propagators

slide-29
SLIDE 29

Important concepts

  • constraint store

stores basic constraints

  • propagator

implements non-basic constraint

  • computation space

constraint store + propagators

slide-30
SLIDE 30

Branching

slide-31
SLIDE 31

x {4,5} y {4,5} x y

Bad news

propagation is not enough!

slide-32
SLIDE 32

Stable spaces

  • solution

for each x, dom(x) is a singleton

  • failure

there is an x with dom(x) empty

  • choice
slide-33
SLIDE 33

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

slide-34
SLIDE 34

Search tree

choice solution

slide-35
SLIDE 35

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

slide-36
SLIDE 36

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
slide-37
SLIDE 37

Homework

Show that the branching strategy can have a massive impact

  • n the size of the search tree.
slide-38
SLIDE 38

Search

slide-39
SLIDE 39

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

slide-40
SLIDE 40

Static search strategies

  • explore the search tree
  • standard search strategies
  • depth-first search
  • iterative deepening
  • A* search
slide-41
SLIDE 41

Dynamic search

  • add new constraints during search
  • dynamic search strategies
  • iterative best-solution search
  • branch-and-bound search

best solution search

slide-42
SLIDE 42

Best Solution Search

  • class of combinatorial structures σ
  • objective function obj : σ → N
  • find a structure s such that obj(s) is
  • ptimal among all structures σ
slide-43
SLIDE 43

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

slide-44
SLIDE 44

S E N D M O S T + M O N E Y

Send Most Money

MONEY should be maximal

slide-45
SLIDE 45

SMM+ – B & B

unexplored subtree add betterness constraint add betterness constraint best solution first solution

slide-46
SLIDE 46

SMM+

slide-47
SLIDE 47

SMM+

slide-48
SLIDE 48

What this course will be about

slide-49
SLIDE 49

Architecture

  • propagation:

prune impossible values

  • branching:

divide the problem into smaller parts

  • search:

interleave propagation and branching to find solutions

slide-50
SLIDE 50

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
slide-51
SLIDE 51

Applications

  • timetabling
  • crew rostering
  • gate allocation at airports
  • sports league scheduling
  • natural language processing
slide-52
SLIDE 52

Scheduling resources

  • tasks

duration, used resources

  • precedence constraints

task a must precede task b

  • resource constraints

at most one task per resource

slide-53
SLIDE 53

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

slide-54
SLIDE 54

Constraint Programming

  • can be used to tackle hard

combinatorial problems

  • combines various interesting

methodologies and techniques

  • applications are ubiquitous

knowledgeable people are not!

slide-55
SLIDE 55

Constraint Programming

  • compute with possible values

lower bound, upper bound

  • prune inconsistent values

guessing as last resort

  • factorise the problem

inferences + heuristics + search

slide-56
SLIDE 56

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

slide-57
SLIDE 57

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
slide-58
SLIDE 58

Caveat

  • CP is established …
  • international conferences
  • many results & applications
  • … our tools are not (yet)
  • some might not work (as expected)
  • some might be uncomfortable
slide-59
SLIDE 59

Organisation

slide-60
SLIDE 60

Literature

  • Krzysztof R. Apt:

Principles of Constraint Programming Cambridge University Press, 2003

  • Christian Schulte:

Programming Constraint Services Springer-Verlag, 2002

slide-61
SLIDE 61

Lectures

  • 12 lectures in total
  • last lecture on July 14
  • no lectures on
  • May 5 (Ascension)
  • May 26 (Corpus Christi)
slide-62
SLIDE 62

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
slide-63
SLIDE 63

Exam

  • at the end of the term
  • determines 60% of final grade
  • written or oral (to be determined)
  • re-exam will be oral
slide-64
SLIDE 64

Contact & Support

  • mailing list

subscribe on web site

  • office hours

Wednesdays, 14–15, room 45.517

  • during & after the lectures
slide-65
SLIDE 65

Constraint Programming

  • problem-solving technique
  • interleaves inferences & heuristics
  • combines various methodologies
  • is fun!
slide-66
SLIDE 66

Thanks for your attention!

slide-67
SLIDE 67

Backup Slides

slide-68
SLIDE 68

Eight Queens

slide-69
SLIDE 69

Problem specification

Place 8 queens on a chessboard such that no two queens attack each other.

slide-70
SLIDE 70

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
slide-71
SLIDE 71

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

slide-72
SLIDE 72

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

slide-73
SLIDE 73

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

Eight Queens in Alice

slide-74
SLIDE 74

Homework

Generalise the Eight Queens problem so that n queens are placed on an n-times-n chessboard.