Constraint Programming: An Introduction

45
1 Constraint Programming: An Introduction Adapted by Cristian OLI from Peter Stuckey (199 Ho Chi Minh City

description

Constraint Programming: An Introduction. Ho Chi Minh City. Adapted by Cristian OLIVA from Peter Stuckey (1998). Definitions. A constraint C is of the form. where m 0 y c 1 ,c 2 ,…,c m are primitives constraints. There are two distinct constraints : TRUE and FALSE. - PowerPoint PPT Presentation

Transcript of Constraint Programming: An Introduction

Page 1: Constraint Programming: An Introduction

1

Constraint Programming: An

Introduction

Adapted by Cristian OLIVAfrom Peter Stuckey (1998)

Ho Chi Minh City

Page 2: Constraint Programming: An Introduction

2

Definitions A constraint C is of the form

1 2 mc c c

where m0 y c1,c2,…,cm are primitives constraints.

There are two distinct constraints : TRUE and FALSE

The empty conjunction of constraints is written as true.

The conjunction of two constraints C1 and C2 is written

1 2C C

Page 3: Constraint Programming: An Introduction

3

DefinitionsGiven a domain D and a constraint C, we can determine valuesof variables for which the constraint holds.

EXAMPLE 1. X=Y+Z

The assignment : 10, 7, 3X Y Z

Holds the constraint and for that the constraint is TRUE.

The assignment : 10, 4, 5X Y Z Doesn’t hold the constraint and then the constraint is FALSE.

Page 4: Constraint Programming: An Introduction

4

Definitions A set of pairs for a set V of variables is

an asignment of values from the domaine D to variables in V. Let be V={x1,x2,…,xn} and V'V, then (V') is :

1 1( ') {( , ), , ( , )}

p pi i i iV x t x t

where

1' { , , }

pi iV x x

Page 5: Constraint Programming: An Introduction

5

Definitions (V') is an instantiation of the variables

in V'. An instantiation (V'), where V'V, is a

partial solution if it satisfy all primitive constraints that contain the variables of V'.

An instantiation (V) is a solution if it satisfy the constraint C with domain D.

Page 6: Constraint Programming: An Introduction

6

Definitions A constraint C is satisfiable if it has at least

one solution. Otherwise, it is unsatisfiable.

Two constraints C1 and C2 are equivalent if they have the same set of solutions.

7 1X Y 1 7Y X

Page 7: Constraint Programming: An Introduction

7

Definitions The function primitivas takes a constraint.

1 2 mc c c and returns the set of primitive constraints {c1,c2,…,cm}

1 1( ... ) { ,..., }m mprimitivas c c c c

Page 8: Constraint Programming: An Introduction

8

Definitions Given a constraint C and given a domain

D,

Satisfaction : Does the constraint have a solution?

Solution : Give me a solution to the constraint if one exists.

Constraint solver is an algorithm that determine the satisfaction of a constraint. However, these algorithmsfind solutions too.

Page 9: Constraint Programming: An Introduction

9

Domains In general, constraints in Constraint

programming mainly operate on domains which are either discrete or continuous. The most known are : Linear arithmetics constraints Booleans constraints Sequence constraints Blocks world constraints Finite constraints domains Others…

Page 10: Constraint Programming: An Introduction

10

Finite Constraint Domains Constraint Satisfaction Problems (CSP). A CSP consist in :

A set of variables V={x1,x2,…,xn} For each variable xi there is a finite set of

possible values D(xi) (domain) A set of constraints C that restrict possible

values that a variable can take. A solution to the CSP is an instantiation

(V) that satisfy C.

Page 11: Constraint Programming: An Introduction

11

Constraint Satisfaction Problem ¿How to model an optimisation problem by

using CSP ?

It can be express as a sequence of CSP. For instance, we can create a variable that represents the objective value. We can add the following constraint:

Objective_function<objective_current_value

Page 12: Constraint Programming: An Introduction

Different kinds of constraint satisfaction techniques Some of the constraint satisfaction

techniques are: Chronological backtracking: Exponential time

complexity. Complete solver.

Polynomial time complexity. Incomplete Solver. Local consistency

Node consistency Arc Consistency Hyper-arc Consistency Bounds consistency

12

Page 13: Constraint Programming: An Introduction

13

Local Consistency Notion The arity of a primitive constraint is

the number of variables it involves.

:c X Y

•The arity of a CSP is equal to the arity of the highest arity constraint.

1X X Y

Page 14: Constraint Programming: An Introduction

14

Local Consistency Notion An instantiation (V') , where V'V, is

consistent if and only if (V') is a partial solution of CSP.

A solution to the CSP is a consistent instantiation (V).

Page 15: Constraint Programming: An Introduction

15

Node and Arc consistency Basic Idea: Find a CSP equivalent to the original

one with smaller domains. Key: examine 1 prim. constraint c, one at a time. Consistency-Node: (vars(c)={x}) remove the

values of the domain of x that no satisfy c Consistency-Arc: (vars(c)={x,y}) remove the

values of D(x) for which there is not a value in D(y) that satisfy c and vice-versa.

Page 16: Constraint Programming: An Introduction

16

Node consistent

A primitive constraint c es node consistent with domain D if |vars(c)| 1 or if vars(c) = {x} then for each d in D(x)d x is a solution of c.

A CSP is node consistent if each primitive constraint is node consistent with D.

Page 17: Constraint Programming: An Introduction

17

Node-consistent

Example 1: This CSP is node-consistent?X Y Y Z Z

D X D Y D Z

2

1 2 3 4( ) ( ) ( ) { , , , }

X Y Y Z Z

D X D Y D Z

2

1 2 3 4 1 2( ) ( ) { , , , }, ( ) { , }

NO!!!

Page 18: Constraint Programming: An Introduction

18

Achieving Node Consistency

18

Node_consistentNode_consistent(c,d) For each prim. Constraint c in C

D := node_consistent_primitivenode_consistent_primitive(c, D)

Return D Node_consistent_primitiveNode_consistent_primitive(c, D)

If |vars(c)| =1 thenLet {x} = vars(c)

Return D

D x d D x x d c( ): { ( )|{ } } is a solution of

Page 19: Constraint Programming: An Introduction

19

Arc Consistency

A primitive constraint c is arc consistent with domain D if |vars{c}| ≠2 or Vars(c) = {x,y} and for each d in d(x) there

exists e in d(y) such that

and similarly for y A CSP is arc consistent if each prim.

Constraint in it is arc consistent

{ , }x d y e c is a solution of

Page 20: Constraint Programming: An Introduction

20

Arc-Consistency

Example 2: This CSP is arc-consistent?

X Y Y Z Z

D X D Y D Z

2

1 2 3 4 1 2( ) ( ) { , , , }, ( ) { , }

NO!!!

Page 21: Constraint Programming: An Introduction

21

Achieving Arc Consistency

Arc_consistentArc_consistent(c,d) Repeat

W := dFor each prim. Constraint c in C

D := arc_consistent_primitivearc_consistent_primitive(c,d) Until W = D Return D

Page 22: Constraint Programming: An Introduction

22

Achieving Arc Consistency

Arc_consistent_primitiveArc_consistent_primitive(c, D) If |vars(c)| = 2 then

Return D Removes values which are not arc

consistent with c

D x d D x e D y

x d y e c

( ): { ( )| ( ),

{ , } }

exists

is a soln of

D y e D y d D x

x d y e c

( ): { ( )| ( ),

{ , } }

exists

is a soln of

Page 23: Constraint Programming: An Introduction

23

Using node and arc consistency. We can build constraint solvers using the

consistency methods Two important kinds of domain

False domain: some variable has empty domain

Valuation domain: each variable has a singleton domain

Page 24: Constraint Programming: An Introduction

24

Node and Arc Cons. Solver

Arc_solve(C,D) D := node_consistentnode_consistent(C,D) D := arc_consistentarc_consistent(C,D) if D is a false domain

return false if D is a valuation domain

return satisfiable(C,D) return unknown

Page 25: Constraint Programming: An Introduction

25

Example : Graph colouring

A

B

C

D

E

F

G

Page 26: Constraint Programming: An Introduction

26

Node and Arc Solver Example

Colouring graph: with constraints

FEEDFC

ECDCDB

CBCABA

A B C D E F G

Node consistent

yellowBredA

Page 27: Constraint Programming: An Introduction

27

A B C D E F G

Arc consistent

Node and Arc Solver Example

Colouring graph: with constraints

yellowBredA

FEEDFC

ECDCDB

CBCABA

Page 28: Constraint Programming: An Introduction

A B C D E F G

Arc-consistent

Node and Arc Solver Example

Colouring graph: with constraints

yellowBredA

FEEDFC

ECDCDB

CBCABA

Page 29: Constraint Programming: An Introduction

A B C D E F G

Arc-consistent

Reponse:

Unknown

Node and Arc Solver Example

Colouring graph: with constraints

yellowBredA

FEEDFC

ECDCDB

CBCABA

Page 30: Constraint Programming: An Introduction

30

Backtracking Constraint Solver We can combine consistency with the

backtracking solver. Apply node and arc consistency before

starting the backtracking solver and after each variable is given a value.

Page 31: Constraint Programming: An Introduction

31

A B C D E F G

Backtracking

Choice a variable whose domain has a cardinality >1, G

Add constraint G=red Apply consistency

Reponse: satisfiable (TRUE)

Backtracking Constraint Solver

Colouring graph: with constraints

yellowBredA

Page 32: Constraint Programming: An Introduction

32

Hyper-arc consistency What happens when we have primitive

constraints that contains more than two variables?

hyper-arc consistency: extending the arc consistency to a arbitrary number of variables.

Unfortunately determine the hyper-arc consistency is NP-hard.

What is the solution?

Page 33: Constraint Programming: An Introduction

33

Hyper-arc consistency A primitive constraint c is hyper-arc

consistent with domain D if for each variable x in c and domain assignment tD(X), there is an assignment t1,…,tk to the remaining variables in c such that tj

D(Xj) for 1j k and ((t1,X1),…(tk,Xk)) is a solution of c.

A CSP is hyper-arc consistent if each primitive constraint ci is hyper-arc consistent.

Page 34: Constraint Programming: An Introduction

34

Bounds Consistency arithmetic CSP: constraints are integer range: [l..u] represents the set of integers {l,

l+1, ..., u} idea use real number consistency and only

examine the endpoints (upper and lower bounds) of the domain of each variable

Define min(D,x) as minimum element in domain of x, similarly for max(D,x)

Page 35: Constraint Programming: An Introduction

35

Bounds consistency

A prim. constraint c is bounds consistent with domain D if for each var x in vars(c) exist real numbers d1, ..., dk for remaining

vars x1, ..., xk such that :

is a solution of c And similarly for

An arithmetic CSP is bounds consistent if all its primitive constraints are.

1 1{ min ( ), , }D k kx x x d x d

{ max ( )}Dx x

Page 36: Constraint Programming: An Introduction

36

Examples

X Y Z

D X D Y D Z

3 5

2 7 0 2 1 2( ) [ .. ], ( ) [ .. ], ( ) [ .. ]

What domain is bounds consistent?

Compare with the hyper-arc consistent domainD X D Y D Z( ) { , , }, ( ) { , , }, ( ) { , } 3 5 6 0 1 2 0 1

5 3Z X Y

5 2 7 3 0

10 7

Page 37: Constraint Programming: An Introduction

37

Achieving Bounds Consistency Given a current domain D we wish to

modify the endpoints of domains so the result is bounds consistent

propagation rules do this

Page 38: Constraint Programming: An Introduction

38

Achieving Bounds ConsistencyConsider the primitive constraint X = Y + Z which is equivalent to the three forms

X Y Z Y X Z Z X Y

Reasoning about minimum and maximum values: :

Propagation rules for the constraint X = Y + Z

min ( ) min ( )D DX Y Z max ( ) max ( )D DX Y Z

min ( ) max ( )D DY X Z max ( ) min ( )D DY X Z

min ( ) max ( )D DZ X Y max ( ) min ( )D DZ X Y

Page 39: Constraint Programming: An Introduction

39

Propagation rules for X=Y+Z

Bounds_consistency(D)

min : max{min ( ),min ( ) min ( )}D D DX X Y Z

max : min{max ( ),max ( ) max ( )}D D DX X Y Z

min max( ) : { ( ) | }x XD X d D X X d X

min : max{min ( ),min ( ) max ( )}D D DY Y X Z

max : min{max ( ),max ( ) min ( )}D D DY Y X Z

min max( ) : { ( ) | }y YD Y d D Y Y d Y

min : max{min ( ),min ( ) max ( )}D D DZ Z X Y

max : min{max ( ),max ( ) min ( )}D D DZ Z X Y min max( ) : { ( ) | }z ZD Z d D Z Z d Z

Return D

Page 40: Constraint Programming: An Introduction

40

Propagation rules for

Disequations give weak propagation rules, only when one side takes a fixed value that equals the minimum or maximum of the other is there propagation.

Y Z

D Y D Z

D Y D Z

D Y D Z D Y D Z

( ) [ .. ], ( ) [ .. ]

( ) [ .. ], ( ) [ .. ]

( ) [ .. ], ( ) [ .. ] ( ) [ .. ], ( ) [ .. ]

2 4 2 3

2 4 3 3

2 4 2 2 3 4 2 2

no propagation

no propagation

prop

Page 41: Constraint Programming: An Introduction

41

Alldifferent

alldifferent({V1,...,Vn}) holds when each variable V1,..,Vn takes a different value alldifferent({X, Y, Z}) is equivalent to

Arc consistent with domain

BUT there is no solution! specialized consistency for alldifferent can find it

X Y X Z Y Z

D X D Y D Z( ) { , }, ( ) { , }, ( ) { , } 1 2 1 2 1 2

Page 42: Constraint Programming: An Introduction

42

Other Complex Constraints

schedule n tasks with start times Si and durations Di needing resources Ri where L resources are available at each moment

array access if I = i, then X = Vi and if X ≠ Vi then I ≠ i

cumulative S S D D R R Ln n n([ , , ],[ , , ],[ , , ], )1 1 1

element I V V Xn( ,[ , , ], )1

Page 43: Constraint Programming: An Introduction

43

Optimization for CSP

Because domains are finite can use a solver to build a straightforward optimizer

retry_int_optretry_int_opt(C, D, f, best) D2 := int_solvint_solv(C,D) if D2 is a false domain then return best let sol be the solution corresponding to D2 return retry_int_optretry_int_opt(C /\ f < sol(f), D, f, sol)

Page 44: Constraint Programming: An Introduction

44

Optimization Backtracking

Since the solver may use backtrack search anyway combine it with the optimization

At each step in backtracking search, if best is the best solution so far add the constraint f < best(f)

Page 45: Constraint Programming: An Introduction

45

Branch and Bound Opt.

The previous methods,unlike simplex don't use the objective function to direct search

branch and bound optimization for (C,f) use simplex to find a real optimal, if solution is integer stop otherwise choose a var x with non-integer opt

value d and examine the problems

use the current best solution to constrain prob. ( , ) ( , )C x d f C x d f