constraint programming

65
Constraint Programming Peter van Beek Computer Science

Upload: yazid

Post on 24-Feb-2016

77 views

Category:

Documents


0 download

DESCRIPTION

Constraint Programming. Peter van Beek Computer Science. Outline. Introduction Constraint propagation Backtracking search Global constraints Symmetry Modeling. Some additional resources. “Handbook of Constraint Programming,” edited by F. Rossi, P. van Beek, T. Walsh. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Constraint Programming

Constraint ProgrammingPeter van BeekComputer Science

Page 2: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search

• Global constraints

• Symmetry

• Modeling

Page 3: Constraint Programming

Some additional resources

“Constraint-Based Local Search”, by Pascal Van Hentenryck and Laurent Michel

Integrated Methods for Optimization, by John N. Hooker

“Constraint Processing,” by Rina Dechter

“Principles of Constraint Programming,” by Krzysztof Apt

“Programming with Constraints,” by Kim Marriott, Peter J. Stuckey

“Handbook of Constraint Programming,” edited by F. Rossi, P. van Beek, T. Walsh

Page 4: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search

• Global constraints

• Symmetry

• Modeling

Page 5: Constraint Programming

What is constraint programming?• Idea: Solve a problem by stating constraints on acceptable solutions

• Advantages:• constraints often a natural part of problems

• especially true of difficult combinatorial problems

• once problem is modeled using constraints, wide selection of solution techniques available

Page 6: Constraint Programming

What is constraint programming?• Constraint programming is similar to mathematical programming

• declarative• user states the constraints• general purpose constraint solver, often based on backtracking search, is used to solve the

constraints

• Constraint programming is similar to computer programming• extensible

• user-defined constraints• allows user to program a strategy to search for a solution

Page 7: Constraint Programming

What is constraint programming?• Constraint programming is a collection of core techniques

• Modeling• deciding on variables/domains/constraints• improving the efficiency of a model

• Solving• local consistency

• constraint propagation• global constraints

• search• backtracking search• hybrid methods

Page 8: Constraint Programming

Constraint programming methodology• Constraint programming is a problem-solving methodology

• Model problem

• Solve model

• specify in terms of constraints on acceptable solutions• define/choose constraint model:

variables, domains, constraints

• define/choose search algorithm

• define/choose heuristics

ConstraintSatisfaction

Problem

Page 9: Constraint Programming

Constraint satisfaction problem (CSP)• A CSP is defined by:

• a set of variables {x1, …, xn}

• a set of values for each variable dom(x1), …, dom(xn)

• a set of constraints {C1, …, Cm}

• A solution to a CSP is a complete assignment to all the variables that satisfies the constraints

• Given a CSP:• determine whether it has a solution or not• find one solution• find all solutions• find an optimal solution, given some cost function

Page 10: Constraint Programming

Example domains and constraints• Reals, linear constraints

• 3x + 4y ≤ 7, 5x – 3y + z = 2• Guassian elimination, linear programming

• Integers, linear constraints• integer linear programming, branch-and-bound

• Boolean values, clauses

• Here: • finite domains• rich constraint languages • user-defined constraints• global constraints

Page 11: Constraint Programming

Constraint languages• Usual arithmetic operators:

• =, , , < , > , , + , , *, /, absolute value, exponentiation• e.g., 3x + 4y 7, 5x3 – x*y = 9

• Usual logical operators:• , , , • e.g., (x = 1) (y = 2), x y z, (3x + 4y 7) (x*y = z)

• Global constraints:• e.g., alldifferent(x1, …, xn)

• Table constraints

Page 12: Constraint Programming

Alldifferent constraint• Consists of:

• set of variables {x1, …, xn}

• Satisfied iff:• each of the variables is assigned

a different value

Page 13: Constraint Programming

Sudoku

Each Sudoku has a unique solution that can be reached logically without guessing.

Enter digits from 1 to 9 into the blank spaces. Every row must contain one of each digit. So must every column, as must every 3x3 square.

5 3 76 1 9 5

9 8 68 6 34 8 3 17 2 6

6 2 84 1 9 5

8 7 9

Page 14: Constraint Programming

Sudoku

x1 x2 x3 x4 x5 x6 x7 x8 x9

x10 x11 x1

2

x13 x14 …

x19 x20 x2

1

x28 …x37 …x46 …x55 …x64 …x73 …

5 3 76 1 9 5

9 8 68 6 34 8 3 17 2 6

6 2 84 1 9 5

8 7 9

Page 15: Constraint Programming

Sudoku

dom(xi) = {1, …, 9}, for all i = 1, …, 81

alldifferent(x1, x2, x3, x4, x5, x6, x7, x8, x9)…alldifferent(x1, x10, x19, x28, x37, x46, x55, x64,

x73)…alldifferent(x1, x2, x3, x10, x11, x12, x19, x20,

x21)…x1 = 5, x2 = 3, x5 = 7, …, x81 = 9

5 3 76 1 9 5

9 8 68 6 34 8 3 17 2 6

6 2 84 1 9 5

8 7 9

Page 16: Constraint Programming

Example: Boolean satisfiability

Given a Boolean formula, does there exist a satisfying assignment

(x1 x2 x4) (x2 x4 x5) (x3

x4 x5)

Page 17: Constraint Programming

Constraint modelvariables: x1, x2 , x3 , x4 , x5

domains: {true, false}constraints: (x1 x2 x4) (x2 x4 x5) (x3 x4 x5)

(x1 x2 x4) (x2 x4 x5) (x3

x4 x5)

Page 18: Constraint Programming

Example: n-queens

Place n-queens on an n n board so that no pair of queens attacks each other

Page 19: Constraint Programming

Constraint model

4

3

2

1

x1 x2 x3 x4

variables: x1, x2 , x3 , x4

domains: {1, 2, 3, 4}constraints: x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1

Page 20: Constraint Programming

Example: 4-queens

A solutionx1 = 2x2 = 4x3 = 1x4 = 3

QQ

Q

Q

x1 x2 x3 x4

4

3

2

1

Page 22: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search

• Global constraints

• Soft constraints

• Symmetry

• Modeling

Page 23: Constraint Programming

Fundamental insight: Local consistency• A local inconsistency is an instantiation of some of the variables that

satisfies the relevant constraints but:• cannot be extended to one or more additional variables• so cannot be part of any solution

• Has led to:• definitions of conditions that characterize the level of local consistency of a

CSP• algorithms which enforce these levels of local consistency by removing

inconsistencies from the CSP• effective backtracking algorithms for finding solutions to a CSP that maintain

a level of local consistency during the search

Page 24: Constraint Programming

Enforcing local consistency: constraint propagation

• Here, focus on: Given a constraint, remove a value from the domain of a variable if it cannot be part of a solution according to that constraint

• Example of local consistency: arc consistency

Page 25: Constraint Programming

ac() : boolean1. Q all variable/constraint pairs (x, C) 2. while Q {} do3. select and remove a pair (x, C)

from Q4. if revise(x, C) then5. if dom(x) = {}6. return false7. else8. add pairs to Q9. return true

Generic arc consistency algorithm

revise(x, C) : boolean1. change false2. for each v dom(x) do3. if t C s.t. t[x] = v

then4. remove v from

dom(x)5. change true6. return change

Page 26: Constraint Programming

ac() : boolean1. Q all variable/constraint pairs (x, C) 2. while Q {} do3. select and remove a pair (x, C)

from Q4. if revise(x, C) then5. if dom(x) = {}6. return false7. else8. add pairs to Q9. return true

Generic arc consistency algorithm

revise(x, C) : boolean1. change false2. for each v dom(x) do3. if t C s.t. t[x] = v

then4. remove v from

dom(x)5. change true6. return change

variable xyz

domain {1, 2, 3}{1, 2, 3}{1, 2, 3}

C1: x < yconstraints

C2: y < z

Page 27: Constraint Programming

Improvements•Much work on efficient algorithms for table constraints

• Special purpose algorithms for global constraints (coming later)

Page 28: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search

• Global constraints

• Symmetry

• Modeling

Page 29: Constraint Programming

Constraint programming methodology• Model problem

• Solve model

• specify in terms of constraints on acceptable solutions• define/choose constraint model:

variables, domains, constraints

• define/choose search algorithm

• define/choose heuristics

Page 30: Constraint Programming

Backtracking search• CSPs often solved using backtracking search

• Many techniques for improving efficiency of a backtracking search algorithm

• branching strategies, constraint propagation, nogood recording, non-chronological backtracking (backjumping), heuristics for variable and value ordering, portfolios and restart strategies

Page 31: Constraint Programming

Backtracking search• A backtracking search is a depth-first traversal of a search tree

• search tree is generated as the search progresses• search tree represents alternative choices that may have to be examined in

order to find a solution• method of extending a node in the search tree is often called a branching

strategy

Page 32: Constraint Programming

Generic backtracking algorithmtreeSearch( i : integer ) : integer1. if all variables assigned a value then 2. return 0 // solution found3. x getNextVariable( )4. backtrackLevel i5. for each branching constraint bi do6. post( bi )7. if propagate( bi ) then8. backtrackLevel treeSearch( i + 1 )9. undo( bi )10. if backtrackLevel < i then11. return backtrackLevel12. backtrackLevel getBacktrackLevel()13. setNogoods()14. return backtrackLevel

Page 33: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies

• Global constraints

• Symmetry

• Modeling

Page 34: Constraint Programming

Branching strategies• A node p = {b1, …, bj} in the search tree is a set of branching

constraints, where bi, 1 ≤ i ≤ j, is the branching constraint posted at level i in search tree

• A node p is extended by posting a branching constraint• to ensure completeness, the constraints posted on all the branches from a

node must be mutually exclusive and exhaustive

p = {b1, …, bj}

p {bj+1} p {bj+1}1 k…

Page 35: Constraint Programming

Popular branching strategies• Running example: let x be the variable branched on, let dom(x) =

{1, …, 6}

• Enumeration (or d-way branching)• variable x is instantiated in turn to each value in its domain• e.g., x = 1 is posted along the first branch, x = 2 along second branch, …

• Binary choice points (or 2-way branching)• variable x is instantiated to some value in its domain• e.g., x = 1 is posted along the first branch, x 1 along second branch,

respectively

• Domain splitting• constraint posted splits the domain of the variable• e.g., x 3 is posted along the first branch, x > 3 along second branch,

respectively

Page 36: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies

• Global constraints

• Symmetry

• Modeling

Page 37: Constraint Programming

Constraint propagation• Effective backtracking algorithms for constraint programming

maintain a level of local consistency during the search; i.e., perform constraint propagation

• A generic scheme to maintain a level of local consistency in a backtracking search is to perform constraint propagation at each node in the search tree

• if any domain of a variable becomes empty, inconsistent so backtrack

Page 38: Constraint Programming

Constraint propagation• Backtracking search integrated with constraint propagation has two

important benefits1.removing inconsistencies during search can dramatically prune the search

tree by removing deadends and by simplifying the remaining sub-problem2. some of the most important variable ordering heuristics make use of the

information gathered by constraint propagation

Page 39: Constraint Programming

Maintaining a level of local consistency• Definitions of local consistency can be categorized by whether:

• only unary constraints need to be posted during constraint propagation; sometimes called domain filtering

• higher arity constraints may need to be posted

• In implementations of backtracking• domains represented extensionally• posting and retracting unary constraints can be done very efficiently• important that algorithms for enforcing a level of local consistency be

incremental

Page 40: Constraint Programming

Some backtracking algorithms• Chronological backtracking (BT)

• naïve backtracking: performs no constraint propagation, only checks a constraint if all of its variables have been instantiated; chronologically backtracks

• Forward checking (FC)• maintains arc consistency on all constraints with exactly one uninstantiated

variable; chronologically backtracks

• Maintaining arc consistency (MAC)• maintains arc consistency on all constraints with at least one uninstantiated

variable; chronologically backtracks

• Conflict-directed backjumping (CBJ)• backjumps; no constraint propagation

Page 41: Constraint Programming

Constraint model for 4-queens

4

3

2

1

x1 x2 x3 x4

variables: x1, x2 , x3 , x4

domains: {1, 2, 3, 4}constraints: x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1

Page 42: Constraint Programming

Search tree for 4-queens

x1

x2

x3

x4

x1=1

(1,1,1,1) (4,4,4,4)(2,4,1,3) (3,1,4,2)

x1= 4

Page 43: Constraint Programming

Chronological backtracking (BT)

Q

QQQQ

Q

Q

Q

Q

…Q

Page 44: Constraint Programming

Forward checking (FC)

4

3

2

1

x1 x2 x3 x4

Q

Enforce arc consistency on constraints with exactly one variable uninstantiated

{ x1 = 1}

x1 x2 |x1 – x2| 1 x1 x3 |x1 – x3| 2x1 x4 |x1 – x4| 3

constraints:

Page 45: Constraint Programming

Forward checking (FC) on 4-queens

Q

Q

Q

Q

Q

…Q

Page 46: Constraint Programming

Maintaining arc consistency (MAC)

4

3

2

1

x1 x2 x3 x4

Q

?

{ x1 = 1}

x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1

constraints:

Enforce arc consistency on constraints with at least one variable uninstantiated

Page 47: Constraint Programming

Maintaining arc consistency (MAC) on 4-queens

Q

QQ

Q

Q

Page 48: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies

• Global constraints

• Symmetry

• Modeling

Page 49: Constraint Programming

Non-chronological backtracking• Upon discovering a deadend, a backtracking algorithm must retract

some previously posted branching constraint• chronological backtracking: only the most recently posted branching

constraint is retracted• non-chronological backtracking: algorithm backtracks to and retracts the

closest branching constraint which bears some responsibility for the deadend

Page 50: Constraint Programming

4

3

2

1

{x1 = 1}

Conflict-directed backjumping (CBJ)

Q

Q{x1 = 1, x2 = 3}

x1

x2

x1

x2{x1 = 1, x2 = 3 , x4 = 2}

Q

x3

x1 x3 x4x2

Page 51: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies

• Global constraints

• Symmetry

• Modeling

Page 52: Constraint Programming

Nogood recording• One of the most effective techniques known for improving the

performance of backtracking search on a CSP is to add redundant (implied) constraints

• a constraint is redundant if set of solutions does not change when constraint is added

• Three methods:• add hand-crafted constraints during modeling• apply a consistency algorithm before solving• learn constraints while solving

nogood recording

Page 53: Constraint Programming

Nogood recording• A nogood is a set of assignments and branching constraints that is

not consistent with any solution• i.e., there does not exist a solutionan assignment of a value to each variable

that satisfies all the constraintsthat also satisfies all the assignments and branching constraints in the nogood

Page 54: Constraint Programming

Example nogoods: 4-queens• Set of assignments {x1 = 1, x2 = 3} is a

nogood• to rule out the nogood, the redundant

constraint (x1 = 1 x2 = 3)

could be recorded, which is just x1 1 x2 3

• recorded constraints can be checked and propagated just like the original constraints

• But {x1 = 1, x2 = 3} is not a minimal nogood

4

3

2

1

Q

Qx1 x3 x4x2

Page 55: Constraint Programming

Discovering nogoods• Discover nogoods when:

• during backtracking search when current set of assignments and branching constraints fails

• during backtracking search when nogoods have been discovered for every branch• set of assignments {x1 = 1, x2 = 1} is a nogood• set of assignments {x1 = 1, x2 = 2} is a nogood• set of assignments {x1 = 1, x2 = 3} is a nogood• set of assignments {x1 = 1, x2 = 4} is a nogood• {x1 = 1} is a nogood

• Tricky when:• backtracking algorithm maintains a level of local consistency • in the presence of global constraints

• Standard in SAT solvers

• Currently not yet widely used for solving general CSPs

Page 56: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies

• Global constraints

• Symmetry

• Modeling

Page 57: Constraint Programming

Heuristics for backtracking algorithms• Variable ordering (very important)

• what variable to branch on next• examples:

• impact-based (measure reduction in search space)• conflict-based (measure how often a variable appears in a failed constraint)• activity-based (measure how often the domain of a variable is reduced)

• Value ordering (can be important)• given a choice of variable, what order to try values• example:

• choose first the values that are most likely to succeed

Page 58: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies

• Global constraints

• Symmetry

• Modeling

Page 59: Constraint Programming

Portfolios• Observation: Backtracking

algorithms can be quite brittle, performing well on some instances but poorly on other similar instances

• Portfolios of algorithms have been proposed and shown to improve performance

We are the last Dodos on the planet, so I’ve put all of our eggs safely into

this basket…

Page 60: Constraint Programming

Portfolios: Definitions• Given a set of backtracking algorithms and a time

deadline d, a portfolio P for a single processor is a sequence of pairs,

• A restart strategy portfolio is a portfolio where,

Page 61: Constraint Programming

Restart strategy portfolio• Randomize backtracking algorithm

• randomize selection of a value• randomize selection of a variable

• A restart strategy (t1, t2, t3, …, tm) is a sequence• idea: randomized backtracking algorithm is run

for t1 steps. If no solution is found within that cutoff, the algorithm is restarted and run for t2

steps, and so on

Page 62: Constraint Programming

Restart strategies• Let f(t) be the probability a randomized

backtracking algorithm A on instance x stops after taking exactly t steps

• f(t) is called the runtime distribution of algorithm A on instance x

• Given the runtime distribution of an instance, the optimal restart strategy for that instance

is given by (t*, t*, t*, …), for some fixed cutoff t*

• A fixed cutoff strategy is an example of a non-universal strategy: designed to work on a particular instance

Page 63: Constraint Programming

Universal restart strategies• Non-universal strategies are open to

catastrophic failure

• In contrast to non-universal strategies, universal strategies are designed to be

used on any instance

• Luby strategy

• Walsh strategy

(1, r, r2, r3, …), r > 1

(1, 1, 2, 1, 1, 2, 4, 1, 1, 2, 1, 1, 2, 4, 8, 1, …)

grows exponentially

grows linearly

Page 64: Constraint Programming

Summary: backtracking search• CSPs often solved using backtracking search

• Many techniques for improving efficiency of a backtracking search algorithm

• branching strategies, constraint propagation, nogood recording, non-chronological backtracking (backjumping), heuristics for variable and value ordering, portfolios and restart strategies

• Best combinations of these techniques give robust backtracking algorithms that can routinely solve large, hard instances that are of practical importance

Page 65: Constraint Programming

Outline• Introduction

• Constraint propagation

• Backtracking search

• Global constraints

• Symmetry

• Modeling