ID2204: Constraint Programming Introduction & Overview · which value combinations satisfy...

Post on 30-Aug-2020

4 views 0 download

transcript

ID2204: Constraint Programming

Introduction & Overview

Lecture 01, 2015-03-23

Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology KTH – Royal Institute of Technology Sweden

2015-03-23

Lecture Overview

What is Constraint Programming?

Sudoku is Constraint Programming

... more later

Sudoku

...is Constraint Programming!

2015-03-23

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

2015-03-23

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

2015-03-23

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

2015-03-23

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

2015-03-23

Block Propagation

No field in block can take digits 3,6,8

8

6 3

2015-03-23

Block Propagation

No field in block can take digits 3,6,8 propagate to other fields in block

Rows and columns: likewise

1,2,4,5,7,9 8 1,2,4,5,7,9

1,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

2015-03-23

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

2015-03-23

Propagation

Prune digits from fields such that: digits distinct per rows, columns, blocks

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

2015-03-23

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

2015-03-23

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,3,6

2015-03-23

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

2015-03-23

Sudoku is Constraint Programming

Modelling: variables, values, constraints Solving: propagation, search

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

2015-03-23

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, ...

2015-03-23

Plan of Lecture

Introduction what is constraint programming? principles and applications

Overview course content course goal

Organizational

What Is Constraint

Programming?

2015-03-23

Running Example: SMM

Find distinct digits for letters, such that

SEND

+ MORE

= MONEY

2015-03-23

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

2015-03-23

Solving SMM

Find values for variables

such that all constraints satisfied

2015-03-23

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

2015-03-23

Important Concepts

Constraint store Propagator Constraint propagation

2015-03-23

Constraint Store

Maps variables to possible values stores basic constraints

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

2015-03-23

Constraint Store

Maps variables to possible values Others: finite sets, intervals, trees, ...

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

finite domain constraints

2015-03-23

Propagators

Implement (non-basic) constraints

distinct(x1,…,xn)

x + 2*y = z

2015-03-23

Propagators

Amplify store by constraint propagation

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

xy y>3

2015-03-23

Propagators

Amplify store by constraint propagation

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

xy y>3

2015-03-23

Propagators

Amplify store by constraint propagation

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

xy y>3

2015-03-23

Propagators

Amplify store by constraint propagation

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

xy y>3

2015-03-23

Propagators

Amplify store by constraint propagation

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

xy y>3

2015-03-23

Propagators

Amplify store by constraint propagation Disappear when done (subsumed, entailed)

no more propagation possible

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

xy y>3

2015-03-23

Propagators

Amplify store by constraint propagation Disappear when done (subsumed, entailed)

no more propagation possible

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

xy

2015-03-23

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

2015-03-23

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

2015-03-23

Important Concepts

Branching Exploration Branching heuristics Best-solution search

2015-03-23

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

2015-03-23

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

2015-03-23

Search: Exploration

Iterate propagation and branching Orthogonal: branching exploration Nodes:

Unsolved Failed Succeeded

2015-03-23

SMM: Unique Solution

SEND

+ MORE

= MONEY

9567

+ 1085

= 10652

2015-03-23

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

2015-03-23

SMM: Solution With First-fail

SEND

+ MORE

= MONEY

9567

+ 1085

= 10652

2015-03-23

Send Most Money (SMM++)

Find distinct digits for letters, such that

and MONEY maximal

SEND

+ MOST

= MONEY

2015-03-23

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

2015-03-23

Branch-and-bound Search

Find first solution

2015-03-23

Branch-and-bound Search

Explore with additional constraint

2015-03-23

Branch-and-bound Search

Explore with additional constraint

2015-03-23

Branch-and-bound Search

Guarantees better solutions

2015-03-23

Branch-and-bound Search

Guarantees better solutions

2015-03-23

Branch-and-bound Search

Last solution best

2015-03-23

Branch-and-bound Search

Proof of optimality

2015-03-23

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

2015-03-23

SMM++: Branch-and-bound

SEND

+ MOST

= MONEY

9782

+ 1094

= 10876

2015-03-23

SMM++: All Solution Search

SEND

+ MOST

= MONEY

9782

+ 1094

= 10876

Summary

2015-03-23

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

2015-03-23

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 …

2015-03-23

Draws on Variety of Techniques

Artificial intelligence basic idea, search, ...

Operations research scheduling, flow, ...

Algorithms graphs, matching, networks, ...

Programming languages programmability, extensionability, ...

2015-03-23

Essential Aspect

Compositional middleware for combining smart algorithmic problem substructures

components (propagators) scheduling graphs flows …

plus essential extra constraints

2015-03-23

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

2015-03-23

SMM: Strong Propagation

SEND

+ MORE

= MONEY

9567

+ 1085

= 10652

Scheduling Resources

Modelling Propagation Strong propagation

2015-03-23

Scheduling Resources: Problem

Tasks duration resource

Precedence constraints

determine order among two tasks

Resource constraints

at most one task per resource [disjunctive, non-preemptive scheduling]

2015-03-23

Scheduling: Bridge Example

Infamous: additional

side constraints!

2015-03-23

Scheduling: Solution

Start time for each task

All constraints satisfied

Earliest completion time minimal make-span

2015-03-23

Scheduling: Model

Variable for start-time of task a

start(a) Precedence constraint: a before b

start(a) + dur(a) start(b)

2015-03-23

Propagating Precedence

start(a){0,…,7} start(b){0,…,5}

a

b

a before b

2015-03-23

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}

2015-03-23

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

2015-03-23

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

2015-03-23

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)

2015-03-23

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

2015-03-23

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!

2015-03-23

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

2015-03-23

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

2015-03-23

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)

2015-03-23

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

2015-03-23

Content Overview

As to be expected, no surprises: applications

principles

pragmatics limitations

2015-03-23

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

2015-03-23

Principles Underlying CP

Models for propagation search

and their essential properties Different levels of consistency (propagation

strength) Different constraint domains

finite domains, finite sets, ...

2015-03-23

Strong Algorithmic Methods

Régin's distinct algorithm Edge-finding Integration

achieving required properties for propagation

2015-03-23

Relation to Other Techniques

Integer programming Local search Discussion of merits and weaknesses Hybrid approaches

2015-03-23

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.

2015-03-23

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

programming and how constraint programming relates to other methods (local search and integer programming)

Organizational

2015-03-23

Material

Lecture notes (slides) available before the lectures…

Additional material book excerpts scientific articles notes written by me Modeling and Programming with Gecode

2015-03-23

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

2015-03-23

Assignments

Four assignments each 5 bonus points if submitted in time one to three weeks for solving

Points only valid in this academic year!

2015-03-23

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 cschulte@kth.se

Both practical and principles

2015-03-23

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

2015-03-23

Contacting…

Christian Schulte cschulte@kth.se other options (urgent cases), see my homepage

2015-03-23

Summary

Constraint programming… …is exciting! …is fun! Understanding of principles and applications

necessary

Read the webpage https://www.kth.se/social/course/ID2204/

2015-03-23

Acknowledgments

I am grateful to Pierre Flener for helpful comments and bugreports on these slides