+ All Categories
Home > Documents > Introduction to Constraint Programming Mohsen Salarrezaei Advanced Linear programming Course Sharif...

Introduction to Constraint Programming Mohsen Salarrezaei Advanced Linear programming Course Sharif...

Date post: 27-Dec-2015
Category:
Upload: cecily-carpenter
View: 222 times
Download: 1 times
Share this document with a friend
Popular Tags:
95
Introduction to Constraint Programming Mohsen Salarrezaei Advanced Linear programming Course Sharif University of Technology Autumn 2010
Transcript

Introduction to Constraint Programming

Mohsen SalarrezaeiAdvanced Linear programming CourseSharif University of Technology

Autumn 2010

Outline

• Introduction• Constraint propagation• Backtracking search• Some application• Global constraints• Symmetry• Modeling

The Tutorial

Goal: to provide an introduction What is constraint programming? What is it good for? How does it compare to integer programming? How easy is it to use? What is the underlying technology?

Combinatorial Optimization

Many, many practical applications Resource allocation, scheduling, routing

Properties Computationally difficult Technical and modeling expertise needed Experimental in nature Important ($$$) in practice

Many solution techniques Integer programming Specialized methods Local search/metaheuristics Constraint programming

Constraint programming Began in 1980s By computer programmers

Constraints Satisfaction Problem(CSP) Prolog III (Marseilles, France) CLP(R) CHIP (ECRC, Germany)

Application areas Scheduling, sequencing, resource and personnel allocation,

etc. etc. Active research area

Specialized conferences (CP, CP/AI-OR, …) Journal (Constraints) Companies

Comparison of CP/IP• Branch and Prune

– Prune: eliminate infeasible configurations– Branch: decompose into subproblems

• Prune– Carefully examine constraints to reduce possible variable values

• Branch– Use heuristics based on feasibility info

• Main focus:constraints and feasibility

• Branch and Bound– Bound: eliminate suboptimal solutions– Branch: decompose into subproblems

• Bound– Use (linear) relaxation of problem (+ cuts)

• Branch– Use information from relaxation

• Main focus: objective function and optimality

Illustrative artificial example

• Color a map of (part of) Europe: Belgium, Denmark, France, Germany, Netherlands, Luxembourg

• No two adjacent countries same color• Is four colors enough?

OPL example

enum Country {Belgium,Denmark,France,Germany,Netherlands,Luxembourg};

enum Colors {blue,red,yellow,gray};var Colors color[Country];

solve { color[France] <> color[Belgium]; color[France] <> color[Luxembourg]; color[France] <> color[Germany]; color[Luxembourg] <> color[Germany]; color[Luxembourg] <> color[Belgium]; color[Belgium] <> color[Netherlands]; color[Belgium] <> color[Germany]; color[Germany] <> color[Netherlands]; color[Germany] <> color[Denmark];};

• Variables non-numeric

• Constraints are non-linear

• Looks nothing like IP!

• Perfectly legal CP

Example constraint systems/languages

System Description

ILOG / OPL C++ class library

Comet Programming language & system

Eclipse Logic programming

Choco Java class library

HAL Logic programming

Oz Functional programming

Strength of CP’s Modelling

• Don’t need to linear constraints• Don’t need to special structure• Since there is no need for a linear relaxation,

the language can represent much more directly (no need for big-M IP formulations.

• It’s very very easy to modeling for everybody(don’t need to OR knowledge)

11

Integer constraints• Usual arithmetic operators:

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

• Usual logical operators:– , , , (or “if … then”)– e.g., if x = 1 then y = 2, x y z, (3x + 4y 7) (x*y = z)

• Global constraints:– alldifferent(x1, …, xn)– ordered([A,B,C,D])– cardinality(x1, …, xn, l, u)– minlist([A,B,C,D], 3)– occurrences(…)

more example: 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 7

6 1 9 5

9 8 6

8 6 3

4 8 3 1

7 2 6

6 2 8

4 1 9 5

8 7 9

Sudokuusing CP;int n=9;dvar int x[1..n][1..n] in 1..9;subject to{ forall(i in 1..n) allDifferent(all(j in 1..9)x[i][j]); forall(j in 1..n) allDifferent(all(i in 1..9)x[i][j]); forall(i in 1..3) forall(j in 1..3) allDifferent(all(k in 3*i-2..3*i,h in

3*j-2..3*j)x[k][h]);}

5 3 7

6 1 9 5

9 8 6

8 6 3

4 8 3 1

7 2 6

6 2 8

4 1 9 5

8 7 9

more example: n Queens

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

n Queens : Solution

using CP;int n=8;dvar int x[1..n]in 1..n;

subject to{ allDifferent(all(i in 1..n) x[i]); forall(i in 1..n-1) forall(j in i+1..n) abs(x[i]-x[j])!=j-i; }

Optimization with CP

1) Find a feasible solution x2) Z=c(x)3) Add constraint c(x)<z4)If found a feasible solution x Goto 2) Else Return last z

Outline

• Introduction• Constraint propagation• Backtracking search• Some application• Global constraints• Symmetry• Modeling

Place numbers 1 through 8 on nodes, where each number appears exactly once and no connected nodes have consecutive numbers

?

?

?

?

?

?

??

Acknowledgement: Patrick Prosser

Backtracking search

Which nodes are hardest to number?

?

?

?

?

?

?

??

Guess a value, but be prepared to backtrack

Backtracking search

?

?

?

?

?

?

??

Which nodes are hardest to number?

Backtracking search

Which are the least constraining values to use?

?

?

?

?

?

?

??

Values 1 & 8

Backtracking search

Symmetry means we don’t need to consider: 8 1

?

1

?

?

8

?

??

Inference/propagation

We can now eliminate many values for other nodes

?

1

?

?

8

?

??

{1,2,3,4,5,6,7,8}

Inference/propagation

By symmetry

?

1

?

?

8

?

??

{3,4,5,6}

{3,4,5,6}

Inference/propagation

?

1

?

?

8

?

??

{3,4,5,6}

{3,4,5,6}

{1,2,3,4,5,6,7,8}

Inference/propagation

By symmetry

?

1

?

?

8

?

??

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

Inference/propagation

?

1

?

?

8

?

??

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6,7} {2,3,4,5,6}

Inference/propagation

?

1

?

?

8

?

27

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

Inference/propagation

?

1

?

?

8

?

27

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

And propagate

Inference/propagation

?

1

?

?

8

?

27

{3,4,5}

{3,4,5}

{4,5,6}

{4,5,6}

Guess a value, but be prepared to backtrack

Inference/propagation

3

1

?

?

8

?

27

{3,4,5}

{4,5,6}

{4,5,6}

Guess a value, but be prepared to backtrack

Inference/propagation

3

1

?

?

8

?

27

{3,4,5}

{4,5,6}

{4,5,6}

And propagate

Inference/propagation

3

1

?

?

8

?

27

{4,5}

{5,6}

{4,5,6}

More propagation?

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

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

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

x

y

z

domain

{1, 2, 3}

{1, 2, 3}

{1, 2, 3}

C1: x < y

constraints

C2: y < z

Outline

• Introduction• Constraint propagation• Backtracking search• Some application• Global constraints• Symmetry• Modeling

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

Forward checking (FC) on 4-queens

Q

Q

Q

Q

Q

Q

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

Heuristics for backtracking algorithms

• Variable ordering (very important)– what variable to branch on next

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

Variable ordering

• Domain dependent heuristics• Domain independent heuristics• Static variable ordering

– fixed before search starts• Dynamic variable ordering

– chosen during search

Variable ordering: Possible goals

• Minimize the underlying search space– static ordering example:

• suppose x1, x2, x3, x4 with domain sizes 2, 4, 8, 16

• compare static ordering x1, x2, x3, x4 vs x4, x3, x2, x1

• Minimize expected depth of any branch• Minimize expected number of branches• Minimize size of search space explored by

backtracking algorithm– intractable to find “best” variable

Variable ordering: Basic idea

• Assign a heuristic value to a variable that estimates how difficult it is to find a satisfying value for that variable

• Principle: most likely to fail first– or don’t postpone the hard part

Others methods for solving CP

• Local search algorithms• The Min-Conflicts Heuristic(MCH)• The GSAT Algorithm• ….

• For study more methods see [1].

Outline

• Introduction• Constraint propagation• Backtracking search• Some application• Global constraints• Symmetry• Modeling

CP can hybrid with other OR’s method

• create a relaxation of the CP problem in the form of an OR model

• Hybrid with branch-and-price algorithms uses CP for “column generation”

• Hybrid with Benders decomposition uses CP for “row generation”

computational results for combine CP and Branch and price

Problems that has been solved by hybrid op CP and branch and price

• Generalized Assignment Problem

• airline crew assignment• crew rostering• transit bus crew

scheduling• aircraft scheduling

• vehicle routing problem• network design• employee timetabling• physician scheduling• traveling tournament

problem

computational results for combine CP and Benders decomposition

Problems that has been solved by hybrid op CP and Benders decomposition

• Planning and Scheduling problem

• minimal perturbation scheduling problem

• circuit verification problems

• min-cost planning and disjunctive scheduling

• steel production scheduling

• batch scheduling in a chemical plant

• scheduling of computer processors

Other applicaton

• Use propagation stage in each node for solving MIP in B&B algorithm.

• Use CP for finding feasible neighberhood in Local search algorithms(Tabu search,SA,…)

• Use CP for convert of childerens in crossover operator in GA to feasible points.

• For finding initial solution in other OR methods.

Outline

• Introduction• Constraint propagation• Backtracking search• Some application• Global constraints• Symmetry• Modeling

Global constraints

• A global constraint is a constraint that can be specified over an arbitrary number of variables

• Advantages:– captures common

constraint patterns– efficient, special purpose

constraint propagation algorithms can be designed

Alldifferent constraint

• Consists of:– set of variables {x1, …, xn}

• Satisfied iff:– each of the variables is assigned a different

value

Knapsack constraint

• Consists of:– set of variables {x1, …, xn}

– a scalar value ci for each xi

– two scalar values L and U• Satisfied iff:

– L ci xi Ui =1

n

Knapsack: example of domain consistency

• Suppose knapsack constraint 80 27x1 + 37x2 + 45x3 + 53x4 82

where:

dom(x1) = {0, 1, 2, 3}

dom(x2) = {0, 1, 2, 3}

dom(x3) = {0, 1, 2, 3}

dom(x4) = {0, 1, 2, 3}

• Enforcing domain consistency yields

dom(x1) = {0, 1, 3}

dom(x2) = {0, 1}

dom(x3) = {0, 1}

dom(x4) = {0, 1}

• Example of a propagator that solves an NP-Complete problem using a pseudo-polynomial algorithm based on dynamic programming

Element constraint

• Consists of:• an array of variables x = [x1, …, xn]• an integer variable i • a variable y with arbitrary finite domain

• Satisfied iff:• xi = y

Assignment Problem• Assign four workers W1,W2,W3,W4 to four products such that each

worker works on one product and each product is produced by one worker. Effectiveness of production is given by the following table(e.g.,worker W1 produces P1 with effectivness 7) and the total effectiveness must be 19 at least.

Assignment Problem : Solution

Inverse constraint

Small Example: 10 cars in sequence. The order for assembly is 1, 2, ..., 10. A car must be painted within 3 positions of its assembly order. For instance, car 5 can be painted in positions 2 through 8 inclusive. Cars 1, 5, and 9 are red; 2, 6, and 10 are blue; 3 and 7 green; and 4 and 8 are yellow. Initial sequence 1, 2, ... 10 corresponds to color pattern RBGYRBGYRB and has 9 purgings. The sequence 2,1,5,3,7,4,8,6,10,9 corresponds to color pattern BRRGGYYBBR and has 5 purgings.

Constraint Program

int n=…;int rnge=…;int ncolor=…;range Slots 1..n;var Slots slot[1..n];var Slots revslot[1..n];int color[1..n]= …;minimize sum (j in 1..n-1) (color[revslot[j]] <> color[revslot[j+1]])

subject to { forall (i in Slots) i-rnge<=slot[i] <= i+rnge; /*Must be in range */ alldifferent(slot); /*must choose different slots */ forall (i in Slots) revslot[slot[i]] = i;};

Outline

• Introduction• Constraint propagation• Backtracking search• Some application• Global constraints• Symmetry• Modeling

Symmetry in constraint models

• Many constraint models contain symmetry–variables are “interchangeable”–values are “interchangeable”–variable-value symmetry

• As a result, when searching for a solution:–search tree contains many equivalent subtrees– if a subtree does not contain a solution, neither will

equivalent subtrees elsewhere in the tree– failing to recognize equivalent subtrees results in needless

search

Example of value symmetry: 3-coloringvariables: v1, v2 , v3 , v4 , v5

domains: {1, 2, 3}

constraints: v1 v2

v1 v3

v2 v4

v3 v4

v3 v5

v4 v5

v2

v3

v1

v5

v4

Example of value symmetry: 3-coloring

A solution

v1 = 1

v2 = 2

v3 = 2

v4 = 1

v5 = 3

v2

v3

v1

v5

v4

Mapping

Example of value symmetry: 3-coloring

Another solution

v1 = 1

v2 = 2

v3 = 2

v4 = 1

v5 = 3

v2

v3

v1

v5

v4

Example of value symmetry: 3-coloringA partial non-solution

v1 = 1

v2 = 2

v3 = 3

v2

v3

v1

v5

v4Another partial non-solution

v1 = 1

v2 = 2

v3 = 3

And so on …

Example of variable-value symmetry: 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

Symmetries for 4-queens

21 3 4

5 6 7

10

9

8

11

12

13

14

15

16

913

5 1

14

10

6

11

15

2

7 3

16

12

8 4

15

16

14

13

12

11

10

78

9

6 5

4 3 2 1

84 12

16

3 7 11

62

15

10

14

1 5 9 13

14

13

15

16

9 10

11

65

12

7 8

1 2 3 4

34 2 1

8 7 6

11

12

5

10

9

16

15

14

13

12

16

8 4

15

11

7

10

14

3

6 2

13

9 5 1

51 9 13

2 6 10

73

14

11

15

4 8 12

16

horizontal axis vertical axis diagonal 1 diagonal 2

identity rotate 90 rotate 180 rotate 270

Example of variable-value symmetry: 4-queens

Q

x1 x2 x3 x4

4

3

2

1

A partial non-solution x1 = 1

Another partial non-solution x4 = 4

x1 = 1

x1 1

x4 = 4 x4 4

A formal definition of symmetry

• Let P be a CSP where– V = {x1, …, xn} is the set of variables

– D = dom(x1) dom(xn) is the set of values

• A (solution) symmetry of P is a permutation of the set V D that preserves the set of solutions of P– special cases:

• value ordering symmetry• variable ordering symmetry

Symmetries and permutations21 3 4

5 6 7

10

9

8

11

12

13

14 15 16

identity

( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 )

( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 )

( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 )

( 13 14 15 16 9 10 11 12 5 6 7 8 1 2 3 4 )

14

13

15

16

9 10

11

65

12

7 8

1 2 3 4

horizontal axis

Symmetries and permutations

21 3 4

5 6 7

10

9

8

11

12

13

14

15

16

identity

14

13

15

16

9 10

11

65

12

7 8

1 2 3 4

horizontal axis

x1 x2 x3 x4

4

3

2

1

x1 = 1 x1 = 1

x1 = 2 x1 = 2…

15

16

14

13

12

11

10

78

9

6 5

4 3 2 1

rotate 180

x1 = 1 x1 = 4

x1 = 2 x1 = 3…

x1 = 1 x4 = 4

x1 = 2 x4 = 3…

x1 x2 x3 x4

4

3

2

1

Mitigating symmetry in constraint models

• Reformulate the constraint model to reduce or eliminate symmetry– e.g., use set variables

• Break symmetry by adding constraints to model– leave at least one solution– eliminate some/all symmetric solutions and non-solutions

• Break symmetry during backtracking search algorithm– recognize and ignore some/all symmetric parts of the search tree dynamically while

searching

Breaking symmetry by adding constraints to model: 3-coloring

variables: v1, v2 , v3 , v4 , v5

domains: {1, 2, 3}

constraints: vi vj if vi and vj

are adjacent

v2

v3

v1

v5

v4

fixing colors in a single clique

Breaking symmetry by adding constraints to model: 4-queens

4

3

2

1

x1 x2 x3 x4

variables: x1, x2 , x3 , x4

domains: {1, 2, 3, 4}

constraints: xi xi | xi – xj | | i – j |

break horizontal symmetry by adding x1 ≤ 2break vertical symmetry by adding x2 ≤ x3

but …

Danger of adding symmetry breaking constraints

Q

Q

Q

Q

x1 x2 x3 x4

4

3

2

1

Q

Q

Q

Q

x1 x2 x3 x4

4

3

2

1

adding x2 ≤ x3 removes this solution

adding x1 ≤ 2 removes this solution

Breaking symmetry during backtracking search

• Let g() be a permutation• Let p be a node in the search tree

– a set of assignments and branching constraints• Suppose node p is to be extended by x = v• Post the constraint:

(p g(p) x v) g(x v)

Breaking symmetry during backtracking search: 4-queens

x1 = 1

x1 1

x4 = 4 x4 4

General form: (p g(p) x v) g(x v)

p

Here (p is empty): (x v) g(x v)

So, post: (x1 1) (x1 4 x4 1 x4 4)

Outline

• Introduction• Constraint propagation• Backtracking search• Some application• Global constraints• Symmetry• Modeling

Unfortunately…

• Often easy to state a model• Much harder is to design an efficient model

given a solver– even harder still to design an efficient model +

solver + heuristics

Importance of the constraint model

“In integer programming, formulating a ‘good’ model is of crucial importance to solving the model.”

G. L. Nemhauser and L. A. Wolsey Handbook in OR & MS, 1989

“Same for constraint programming.”

Helmut Simonis, expert CP modeler

Measures for comparing models

• How easy is it to• write down,• understand,• modify, debug,• communicate?

• How computationally difficult is it to solve?

Computational difficulty?

• What is a good model depends on algorithm• Choice of variables defines search space• Choice of constraints defines

– how search space can be reduced – how search can be guided

Computational complexity

• How hard is it to solve a CSP instance?• Class of all CSP instances is NP-hard

– if CSPs can be solved efficiently (polynomially), then so can Boolean satisfiability, set covering, partition, traveling salesperson problem, graph coloring, …

– so unlikely efficient general-purpose algorithms exists

Should we give up?

“While a method for computing the solutions to NP-complete problems using a reasonable amount of time remains undiscovered, computer scientists and programmers still frequently encounter NP-complete problems. An expert programmer should be able to recognize an NP-complete problem so that he or she does not unknowingly waste time trying to solve a problem which so far has eluded generations of computer scientists.”

Wikipedia, 2009

An exponential curve

size of instance

time

(sec

onds

)

Another exponential curve

Instances that arise in practice

Acce

ptab

le

solv

ing

time

Improving model efficiency

• Reformulate the model– change the denotation of the variables

• Given a model:– add redundant variables– add redundant constraints– add symmetry-breaking

Reformulate the model

• Change the denotation of the variables– i.e., what does assigning a value to a variable

mean in terms of the original problem?• Example: 4-queens

– xi = j means place the queen in column i, row j

– xi = j means place the queen in row i, column j– x = [i, j] means place the queen in row i, column j– xij = 0 means there is no queen in row i, column

j

Adding redundant (auxiliary) variables

• Variables that are abstractions of other variables– e.g, decision variables

suppose x has domain {1,…,10} add Boolean variable to represent decisions

(x < 5), (x 5)• Variables that represent constraints (reified constraints)

– e.g., associate a decision variable x with a constraint so that x takes the value 1 if the constraint is satisfied and 0 otherwise

suppose there is the constraint: (y1 + d1 ≤ y2 ) (y2 + d2 ≤ y1)

add Boolean variable to represent (y1 + d1 ≤ y2 )

Adding redundant constraints

• Improve computational efficiency of model by adding “right” constraints

– dead-ends encountered earlier in search process

• Three methods:– apply a local consistency enforcing algorithm before solving– learn constraints (nogoods) while solving– add hand-crafted constraints during modeling

• Can often be explained as projections of conjunctions of a subset of the existing constraints

Refrences

[1] F.Rossi,P.Van Beek,T.Walsh,“FOUNDATIONS OF ARTIFICIAL INTELLIGENCE”,Handbook of constraint programming, Elsevier,2006.

[2] Krzysztof R. Apt,” Principles of Constraint Programming”, Cambridge University Press 2003

[3] M. Milano, P. Van hentenryck,“hybrid optimization”, Springer Optimization and Its Applications, VOLUME 45,2010.

[4]Ian P.Gent, Karen E.Petrie, Jean-Franc¸oisPuget,” Symmetry in Constraint Programming”, Handbook of Constraint Programming,2006.

[5]www.book2down.com/Handbook+of+Constraint+Programming-pdf-6.html


Recommended