/
Constraint  Programming Pedro Meseguer Constraint  Programming Pedro Meseguer

Constraint Programming Pedro Meseguer - PowerPoint Presentation

miller
miller . @miller
Follow
342 views
Uploaded On 2022-06-18

Constraint Programming Pedro Meseguer - PPT Presentation

IIIACSIC Bellaterra Spain pedroiiiacsices 2 Overview Definitions Tree search backtracking Arc consistency Hybrids arc consistency tree search FC MAC ID: 920348

constraints arc constraint queens arc constraints queens constraint solution consistent search variables domains consistency domain model binary node tree

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Constraint Programming Pedro Meseguer" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Constraint Programming

Pedro Meseguer

IIIA-CSIC

Bellaterra, Spain

pedro@iiia.csic.es

Slide2

2

Overview

Definitions

Tree

search

:

backtracking

Arc

consistency

Hybrids

(

arc

consistency

+

tree

search

): FC, MAC

Modelling

Global

constraints

Soft

constraints

Branch

and

bound

Slide3

3

Some Definitions

Constraint Network (CN): (

X

,

D

,

C

)

X

= {

x

1

,

x

2

,…,

x

n

} variables

D

= {

d

1

,

d

2

,…,

d

n

} domains (finite)

C

= {

c

1

,

c

2

,…,

c

r

} constraints

c

C var

(

c

) = {

x

i

,

x

j

,…,

x

k

}

scope

rel

(

c

)

d

i

x

d

j

x .. x

d

k

permitted tuples

arity

(

c

)

=

|

var

(

c

)|

(

unary, binary, ternary

,…)

Constraint Satisfaction Problem (CSP):

CN solving: assig. satisfying every constraint

NP-complete task

Slide4

4

Example: n-queens

GOAL: Locate

n

queens in an

n

x

n

chessboard,

such that they do not attack each other

x

1

x

2

x

3x4

1 2 3 4

4-queens

Formulation:

Variables: one queen per row

Domains: available columns

Constraints:

different columns and different diagonals

x

i

xj | xi - xj |  | i - j |

x

1

x

2

x

3

x

4

Constraint Graph:

Slide5

5

Search Tree

State

space

:

explored

as a

tree

root

: empty

one

variable per

level

sucessors of a node: one sucessor per value of the level

variable

meaning: variable

value

x

a

b

c

Tree:

each branch defines an assignment

depth

n

(number of variables)

branching factor

d

(domain size)

Slide6

6

Search tree for 4-queens

x

1

x

2

x

3

x

4

1

2

3

4

(1,1,1,1)

(4,4,4,4)

(2,1,1,1)

(3,1,1,1)

(4,1,1,1)

Slide7

7

Backtracking Algorithm

Depth-first tree traversal (DFS)

At each node:

check every completely

assigned

constraint

if consistent, continue DFS

otherwise, prune current branch

continue DFS

Complexity: O(

d

n

)

Slide8

8

Backtracking on 4-queens

2

3

2

1

1

2

3

4

1

2

3

3

4

4

2

4

1

1

1

2

3

4

1

1

2

3

x

1

x

2

x

3

x

4

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

Q

solution

25 nodes

Slide9

9Inference

P’ is equivalent to P: Sol(P) = Sol(P’) P’ is presumably easier to solve than Psmaller search spaceconstraints are more explicit Inference can be: complete: produces the solution adaptive consistency incomplete: requires further search arc consistency

legal operations

on variables,

domains,

constraints

Inference:

P P’

Slide10

10

Incomplete Inference: Local Consistency

P constraint network of n variables:

Is P solvable

?

Simpler

problem

:

P

1i, P2j, P

3k

,…subnetworks

of P

of 1, 2,3,…variablesAre they

solvable?YES, but there are values that do not appear in any solution

they

can be removed from P

NO

,  P

has no solution

#vars in subnet: 1 2 3 …….

node arc

path consistency

consistency consistency

Empty domain: P has no solution !!

Slide11

11

Binary Arc Consistency (AC)

AC: Constraint Cij is directional arc consistent (i j) iff for all aDi there is b

Dj such that (a, b)Rij

Constraint

C

ij

is AC iff it is directional AC in both

directions

A problem is AC iff every constraint is AC

Slide12

12AC: Example

a

b

c

a

b

c

X

i

X

j

a

b

c

X

k

Microstructure:

permitted

AC

AC

¬AC

AC

¬AC

¬AC

Slide13

13

Filtering by Arc Consistency

If for a Di there not exists b

Dj such that (a

,

b

)

R

ij

,a can be removed from Di (a will not be in any sol)Domain filtering:

Remove arc-inconsistent values

Until no changes

blue,red,

green

blue, green

blue

blue, red

X

2

X

3

X

4

X

1

Example:

Slide14

14Example: 3-queens

c

12 is not arc-consistent because value 2 of d1

c

21

is not arc-consistent

because value 2 of

d

2

c

32

is not arc-consistent

because value 2 of d

3

x

1

x

2

x

3

1 2 3

x

1

x

2

x

3

1 2 3

x

1

x

2

x

3

1 2 3

x

1

x

2

x

3

1 2 3

Slide15

15Constraint Propagation

AC

(c): procedure to make c arc consistent To make P arc-consistent, process each constraint ? c1 c2

… cr

c

1

c

2

cr

c

1

c2 …

cr c1 c2 … cr

AC

But

AC

(

c

) may render other constraints arc-inconsistent

To make

P

arc-consistent, iterate:

Apply AC on {c1,c2,…,cr } Until no changes in domains: fix point

Slide16

16Example: 3-queens

value 2 of

d3 was removed(to make c23 arc-consistent)

x

1

x

2

x

3

1 2 3

x

1

x

2

x

3

1 2 3

x

1

x

2

x

3

1 2 3

x

1

x

2

x

3

1 2 3

c

13

is not arc-consistent

because value 1 of

d

1

this makes

c

13

arc-inconsistent

x

1

x

2

x

3

1 2 3

c

13

is not arc-consistent

because value 3 of

d

1

x

1

x

2

x

3

1 2 3

domain

d

1

empty

no solution !!

Slide17

17Example: Equations on Integers

y

0

1 2 3 4 5 6 7 8 9

x

0

1 2 3 4 5 6 7 8 9

y

0

1 2 3 4 5 6 7 8 9

x

0

1 2 3 4 5 6 7 8 9

y

0

1 2 3 4 5 6 7 8 9

x

0

1 2 3 4 5 6 7 8 9

fix point

2x

+

4y

=

24

x

+

y

=

9

x

+

y

=

9

2x

+

4y

=

24

x

+

y

=

9

2x

+

4y

=

24

2x

+

4y

=

24

x

+

y

=

9

x

+

y

=

9

2x

+

4y

=

24

x

+

y

=

9

2x

+

4y

=

24

2x

+

4y

=

24

x

+

y

=

9

2x

+

4y

=

24

x

+

y

=

9

y

0

1 2 3 4 5 6 7 8 9

x

0

1 2 3 4 5 6 7 8 9

y

0

1 2 3 4 5 6 7 8 9

x

0

1 2 3 4 5 6 7 8 9

y

0

1 2 3 4 5 6 7 8 9

x

0

1 2 3 4 5 6 7 8 9

2x

+

4y

=

24

x

+

y

=

9

Slide18

18 Generalized Arc Consistency

c is arc-consistent iff: every possible value of every variable in var (c) appears in rel(c)

If

P

is arc-consistent

P

has solution

If

c

is

not arc-consistent because a D

x

:

a

will

not be in any solution

a can be removed:

Dx 

Dx

– {a}

if Dx

becomes empty, P has no

solution P is arc-consistent iff: every constraint is arc-consistent

inference incompleteinference !!

domain filtering

Slide19

19Forward CheckingFC is a combination of:

Search: backtracking

Inference: at each node, AC on constraints with assigned and unassigned variablesWhen a domain becomes empty : No solutions following current branch Prune current branch and backtrack Caution: Values removed by AC at level i, have to be restored when bactracking at level i or above

Slide20

20

Example: FC on 4-queens

2

2

3

4

4

1

1

3

x

1

x

2

x

3

x

4

x

1

x

2

x

3

x

4

1 2 3 4

x

1

x

2

x

3

x

4

1 2 3 4

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

Q

Q

solution

8 nodes

Slide21

21Maintaining Arc ConsistencyMAC is a combination of:

Search: backtracking

Inference: at each node, AC on all constraints Preprocess: subproblems are ACWhen a domain becomes empty : No solutions following current branch Prune current branch and backtrack Caution: Values removed by AC at level i, have to be restored when bactracking at level i or above

Slide22

22MAC vs FC: AC on futures

AC (2,3)

AC (3,4)

AC (2,3)

Q

Q

Q

Q

empty domain

Slide23

23Example: MAC on 4-queens

2

3

4

1

1

x

1

x

2

x

3

x

4

x

1

x

2

x

3

x

4

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

Q

x

1

x

2

x

3

x

4

1 2 3 4

Q

Q

Q

Q

Q

solution

5 nodes

Slide24

24Constraint ProgrammingCP: provides a platform for solving CSPs

proven useful in many real applications

Platform: set of common structures to reuse best known algorithms for propagation & solvingTwo stages: modelling solving

Slide25

25CP: Modelling

Modelling decisions

: select among alternatives the choice of the variables the choice of the domains how we state the constraintssearch space size

space reduction

Example: Map Colouring

variables: are regions or colours?

Any CSP can be modelled in different ways

Efficiency of algorithms can vary dramatically

No strong results are known

Formulating an effective model is not easy,

requires considerable skills in modelling

Slide26

26N-queens: Model 1

Variables

:

n

2

, one per cell, matrix B

n

x

n

Domains

: {0,1}, B[a,b]=0, no queen

B[a,b]=1, queen

Constraints

: If B[a,b] = 1 then

same row

B[_,b]=0

same column

B[a,_]=0

same diagonal

B[a+

d

,b+

d

]=0, B[a-d,b-d

]=0  same diagonal B[a-

d,b+d]=0, B[a+d,b-d]=0

Slide27

27N-queens: Model 2

Variables

: n, one per row Domains: {0,1,…,n-1}, queen column

Constraints: different columns

x

i

x

j  different diagonals | xi -

xj

| 

|

i - j |

x

1

x

2x3

x4

1 2 3 4

Different row constraint is included in the formulation!!

Slide28

28N-queens: Model 3

Variables

: n, one per row Domains: {0,1,…,n-1}, queen column

Constraints: different columns all-different(

x

1

,

x

2

,…,xn ) different diagonals | xi

-

xj

|

 | i -

j |

x

1

x2

x3

x4

1 2 3 4

Different row constraint is included in the formulation!!

Slide29

29N-queens Models

Model 1

Model 2

Model 3

Search space size

d

#vars

2

n2

4 65,536

1.27 E30

20 ERROR!!

n

n

256

1.00 E10

1.05 E26

n

n

256

1.00 E10

1.05 E26

Constraints

number

prunning

n rows

n columns

2(n-1) diagonals

n columns

2(n-1) diagonals

Equal model 1

1 all-diff

2(n-1) diagonals

More than model 2

Slide30

30Constraint FormulationsBinary (arity

2) : conceptually simple, easy to implement may generate weak formulationsNon-binary (arity > 2) : more complex constraints GAC: stronger (filter more) than AC on equivalent binary decompositionEquivalence: any non-binary CSP can be reformulated as a binary one

Slide31

31Global Constraints

Real-life constraints

: often complex, non-binary c is global iff: arity(c) > 2 c is logically equivalent to {c1,c2,…,ck } binary AC(c) prunes more than AC(c1,c2,…,

ck ) Propagation: specialized algorithms

exploit constraint semantics

decrease AC complexity

Slide32

32Example: all-different

3 binary constraints,

they are AC,no pruning

1 ternary

constraint

,

not

GAC

,GAC pruning ®

empty

domain

no solution

!!logicallyequivalent

Var:

F, N, S

; Val: { }; Ctrs:

N

S ≠ F ≠

N

F

{ }

S

{ }

N { }

≠≠

all-different

F

{ }

S

{ }

N

{ }

Slide33

33Example: all-differentEnforcing arc-consistency:

n

variables, d values n(n-1)/2 binary constraints : O(n2 d2) 1 n-ary constraint: general purpose algorithm O(dn)specialized algorithm O(n2 d2)

Slide34

34CP Solving: Some Guidelines

Easy/hard problems:

hybrid search dynamic variable ordering: min domain / degree easy: FC / hard: MACOne solution/All solutions: one solution: hybrid search all solutions: hybrid search or complete inferenceFor specific problems (scheduling, routing…) check: formulation, global constraints heuristics, experiences

Slide35

35CP: Declarative Programming

Declarative Programming:

you declare Variables Domains Constraints and ask the SOLVER to find a solution!! SOLVER offers:Implementation for variables / domains / constraintsHybrid algorithm: backtracking + incomplete inferenceGlobal constraints + optimized AC propagationEmpty domain detectionEmbedded heuristics

Slide36

36

Hard vs Soft Constraints

[90’s]

Hard constraint:

it must be

always

satisfied:

physical constraints

if violated, the solution is invalid

Soft constraint:

it will be satisfied

if possible

:

preferences if violated, the solution is still valid

aggregation of violated constraintsClassical CSP + soft constraints: optimization problems narrow the set of solutions: user intention

Slide37

37

Soft CSP (Weighted Model = COP)

Soft CSP: (

X

,

D

,

C

)

X

set of n

variables

D variable domains

C

set of r soft constraints

c

Î

C c :

∏ di ® R

+ i var(c)

0 t satisfies completely c

c (

t) = (0, +¥) t satisfies / violates partially c

+¥ t

violates completely c c (

t) : cost associated with violation of c by t Goal: min ∑

c (t ) (NP-hard)

c C

Slide38

38

Example: 3-queens

GOAL:

Locate

3

queens

in

an

3

x

3

chessboard

,

such that the number of attacks is

minimum

x

1

x

2

x3

1 2 3

3-queens

Formulation:

Variables: one queen per row

Domains: available columns

Constraints:

different columns and different diagonals xi  xj | xi - xj |  | i - j |

Slide39

Constraints -> Cost Functions

39

x

1

x

2

1 3

3 1

x

1

x

3

1 2

2 1

2 3

3 2

x

2

x

3

1 3

3 1

3-queens:

CSP:

x

1

x2 1 1 1 1 2 1 1 3 0 2 1 1 2 2 1

2 3 1

3 1 0 3 2 1

3 3 1

COP:

x

1

x

3

1 1 1 1 2 0 1 3

1 2 1 0 2 2 1 2 3 0 3 1 1 3 2 0 3 3 1

x2 x3 1 1 1 1 2 1 1 3 0 2 1 1

2 2 1 2 3 1 3 1 0 3 2 1 3 3 1

Slide40

40

COP Search: Branch and Bound

Depth-first

tree

search

:

internal

node:

partial

assignment

leaf

: total assignment

At each node:Upper bound (UB): minimum

cost of

visited

leaves:

cost

of the current

best solution

Lower bound (LB)

: underestimation

of minimum cost

among

leaves below

current node Pruning: UB ≤ LBSimplest LB: LBsearch =

cost (t) = ∑ c (t) c

CP

2

n

current

node

1

Slide41

41

Search tree for 3-queens

x

1

x

2

x

3

1

2

3

(1,1,1)

(3,3,3)

(2,1,1)

(3,1,1)

Slide42

42

BnB for 3-queens

x

1

x

2

x

3

0

0

0

1 1 0

3

2

2 3 2 3

1

1 2 2 1 1

1 1 1

0 1 1

24 NODES

LB

UB

Slide43

43

BnB + soft AC* for 3-queens

x

1

x

2

x

3

C

= 1

3

2

1

9 NODES

C

= 1

C

= 1

C

= 2

C

= 2

C

= 1