LECTURE 6 CONSTRAINT PROGRAMMING & GLOBAL ...webia.lip6.fr/~codognet/PSAI/6-CP.pdfConstraint...

Post on 10-Mar-2021

11 views 0 download

Transcript of LECTURE 6 CONSTRAINT PROGRAMMING & GLOBAL ...webia.lip6.fr/~codognet/PSAI/6-CP.pdfConstraint...

LECTURE 6CONSTRAINT PROGRAMMING

& GLOBAL CONSTRAINTS

Historical Account of Constraint Programming

1973 1980 1990 2000

AI (C.S.P.)

AI languages

(Alice, Constraints)

Logic

Programming

Operation research

Theorem Proving

Constraint

(Logic)

Programming

Concurrent

Constraint LanguagesConcurrent Logic Languages

(Parlog, CP, GHC/KL1, …)

Extended Unification

(≠ , AC, …)

AI (CSP)

Operations

Research

Efficient

Constraint

Solvers

Industrial

applications

Constraint Programming

• In the 1990’s, Constraint Solving Techniques has been integrated in several programming paradigms after LP :

– OO (C++, Java),

– functional (Lisp,ML)

• Also in the 2000’s several specific modeling languages appeared: OPL, Comet, MiniZinc, ...

– Need for data + programming abstractions

• Therefore CP is the combination of:

– Host language (to state the constraints)

– Constraint Vocabulary (arithmetic, symbolic, global,…)

– Constraint Solver(s)

Modeling / Programming / Solving

• Modeling corresponds to state the problem with a certain number of constraints

• Programming consists in generating these constraints in a efficient and/or elegant way

• Solving amounts to find & generate a solution to the original problem

• Of course modeling, programming and solving are related

• A Constraint Programming system feature all 3

Modeling

• Modeling problems with constraints depends on the constraint vocabulary available

• e.g. only arithmetic constraints?

or also symbolic constraints such as all_different, etc ?

• Depends on the vocabulary, depends on the solver…

• Also :

– Different views of the problem lead to different models

– Different model can use different set of variables

• It is important to compare different models

• And sometime to combine different models

Example: the element constraint

• Syntax: element(I,L,X)

meaning that element I of list L to be equal to X

• Similar to arrays : L[i] = X

• Note that the constraint is a multi-directionalrelation

• Thus the values of I and/or X can be constrained

An example

Simple assignment problem:

four workers w1,w2,w3,w4

and four products p1,p2,p3,p4.

Assign workers to products to make profit >= 19

p p p p

w

w

w

w

1 2 3 4

1 7 1 3 4

2 8 2 5 1

3 4 3 7 2

4 3 1 6 3

Profit matrix:

Basic Operations Research Model(linear equations)

p p p p

w

w

w

w

1 2 3 4

1 7 1 3 4

2 8 2 5 1

3 4 3 7 2

4 3 1 6 3

16 Boolean variables Bij

meaning worker i is

assigned product j

19

363

2734

528

437

1:[1,4]i

1 :[1,4]i

44434241

34333231

24232221

14131211

4

1

4

1

P

BBBB

BBBB

BBBB

BBBB

P

B

B

j ij

j ij

11 prim.

constraints

28 choices to find

all four solutions

B23

Better Model(symbolic constraints)

Make use of disequalities

and symbolic constraints

Four variables

W1,W2,W3,W4

corresponding to workers

p p p p

w

w

w

w

1 2 3 4

1 7 1 3 4

2 8 2 5 1

3 4 3 7 2

4 3 1 6 3

19

4321

)4],3,6,1,3[,4(

)3],2,7,3,4[,3(

)2],1,5,2,8[,2(

)1],4,3,1,7[,1(

])4,3,2,1([

P

WPWPWPWPP

WPWelement

WPWelement

WPWelement

WPWelement

WWWWntalldiffere

7 prim.

constraints

14 choices to find

all four solutions

W1

W2

W3

W4

1 2 3 4

Different Model(also with symbolic constraints)

Four variables

T1,T2,T3,T4

corresponding to products

p p p p

w

w

w

w

1 2 3 4

1 7 1 3 4

2 8 2 5 1

3 4 3 7 2

4 3 1 6 3

19

4321

)4],3,2,1,4[,4(

)3],6,7,5,3[,3(

)2],1,3,2,1[,2(

)1],3,4,8,7[,1(

])4,3,2,1([

P

TPTPTPTPP

TPTelement

TPTelement

TPTelement

TPTelement

TTTTntalldiffere

7 prim.

constraints

7 choices to find

all four solutions

1

2

3

4

T1 T2 T3 T4

Comparing Models

• Relative efficiency comes from

– more direct mapping to primary constraints

– fewer variables

– usually requires empirical evaluation

• Other criteria flexibility (new constraints)

– e.g. worker 3 works on product > worker 2

23222134

222133

2132

2431 0,0

BBBB

BBB

BB

BB

W W3 2 ?????

Combining Models

• Combine models by relating the variables and there values in each model

• e.g.. B13 = 1 means W1 = 3 means T3 = 1

• Combined models can gain more information through propagation

• Can use reified constraints:

(V1=D1) (V2=D2)

Combined Model

4321

)4],3,2,1,4[,4(

)3],6,7,5,3[,3(

)2],1,3,2,1[,2(

)1],3,4,8,7[,1(

])4,3,2,1([

TPTPTPTPP

TPTelement

TPTelement

TPTelement

TPTelement

TTTTntalldiffere

19

4321

)4],3,6,1,3[,4(

)3],2,7,3,4[,3(

)2],1,5,2,8[,2(

)1],4,3,1,7[,1(

])4,3,2,1([

P

WPWPWPWPP

WPWelement

WPWelement

WPWelement

WPWelement

WWWWntalldiffere

)4444(),4334(),4224(),4114(

),3443(),3333(),3223(),3113(

),2442(),2332(),2222(),2112(

),1441(),1331(),1221(),1111(

TWTWTWTW

TWTWTWTW

TWTWTWTW

TWTWTWTW

39 prim. constraints. 5 choices to find all solutions

Programming

• CSP Models can be huge and hard to write

• E.g. huge data from which to generate the constraints

• Need for:

– loops, recursion, etc

– data abstraction

– I/O (e.g. data from file)

– Sub-problems / sub-programs

• Thus including CSP facilities into a programming language

Constraint Programming Systems

• GNU Prolog: CLP language (see also Sictus Prolog, B-Prolog, …)

http://www.gprolog.org/

• Comet (Dynadec): CP modeling language + solver, also includes Local Search

http://dynadec.com/support/downloads/

• Gecode : CP library for C++

http://www.gecode.org/

• IBM ILOG CP CPLEX Optimizer: both OPL modeling language and also a CP library for C++, also include MIP (CPLEX) & some Local Search library

http://www-01.ibm.com/software/integration/optimization/cplex-

optimization-studio/

God save the Queens

• Place 8 queens on a chessboard

s.t. no two queens attack each other

• Modeling with constraints :

– 8 variables {Q1,...,Q8}

value of Qi = column of queen on line i

– Domains : {1,...,8}

– constraints :

i [1,8] j [1,8] s.t. j > i

Qi Qj Qi Qj + i -j Qi Qj - i + j

alternatively : all_different({Q1,...,Q8}),

all_different({Q1+1,...,Q8+8}),

all_different({Q1-1,...,Q8-8}),

GNU Prolog

queens(N,L):-

fd_set_vector_max(N),

length(L,N),

fd_domain(L,1,N),

safe(L),

fd_labelingff(L).

safe([]).

safe([X|L]):-

noattack(L,X,1),

safe(L).

noattack([],_,_).

noattack([Y|L],X,I):-

diff(X,Y,I),

I1 #= I+1,

noattack(L,X,I1).

diff(X,Y,I):-

X#\=Y,

X#\=Y+I,

X+I#\=Y.

GeCode(basic version)

class Queens : public Script {

public:

IntVarArray q;

Queens(const SizeOptions& opt)

: q(*this,opt.size(),0,opt.size()-1) {

const int n = q.size();

for (int i = 0; i<n; i++)

for (int j = i+1; j<n; j++) {

rel(*this, q[i] != q[j]);

rel(*this, q[i]+i != q[j]+j);

rel(*this, q[i]-i != q[j]-j);

}

branch(*this, q, INT_VAR_SIZE_MIN, INT_VAL_MIN);

}

Queens(bool share, Queens& s) : Script(share,s) {

q.update(*this, share, s.q);

}

virtual Space*

copy(bool share) {

return new Queens(share,*this);

}

Comet(basic version)

import cotfd;

Solver<CP> cp();

int n = 15;

range S = 1..n;

var<CP>{int} Q[i in S,j in S](cp,0..1);

solve<cp>{

forall(i in S){

cp.post(sum(j in S) Q[i,j] ==1 );

cp.post(sum(j in S) Q[j,i] ==1 );

}

forall(i in S){

cp.post(sum(j in 1..i) Q[j,j+(n-i)] <= 1);

cp.post(sum(j in 1..i) Q[j+(n-i),j] <= 1);

cp.post(sum(j in 1..i) Q[-j+i+1,j] <= 1);

cp.post(sum(j in 1..i) Q[n-j+1,n-i+j] <= 1);

}

}

using{

forall(i in S,j in S){

try<cp> cp.post(Q[i,j]==0); | cp.post(Q[i,j]==1);

}

}

forall(i in S)

cout << all(j in S) Q[n-i+1,j] << endl;

cout << cp.getNFail() << endl;

IBM ILOG Solver (basic version)

OPL Studio – IBM Ilog Solver(using alldifferent constraints)

int n << "number of queens:";

range Domain 1..n;

var Domain queens[Domain];

solve {

alldifferent(queens);

forall(ordered i,j in Domain) {

abs(queens[i]-queens[j])<> abs(i-j) ;

};

};

Mini Zinc

array [1..n] of var 1..n: q;

predicate

noattack(int: i, int: j, var int: qi, var int: qj) =

qi != qj /\

qi + i != qj + j /\

qi - i != qj - j;

constraint

forall (i in 1..n, j in i+1..n) (

noattack(i, j, q[i], q[j])

);

solve satisfy;

output [

"queens:",

show(q), "\n"];

• Modeling Language developed by NICTA (Australia)

• http://www.minizinc.org/

• now “standard” in Constraint Programming community

• Intermediate language between “Zinc” and “FlatZinc”

Global Constraints

• What does it mean to be “global” ?

• No real definition of a global constraint but:

– Involves several variables (more global view of subproblem)

– Has some specific propagation/filtering mechanism

(more efficient than basic AC or BC)

– Somehow a global contraint “regroups” several basic constraints and treat them “together”

• Basic example: all_different(X1,…Xn)

(to be detailed later)

Global Constraints (2)

[from Van Hentenryck 2009]

Global Constraints (3)

[from Van Hentenryck 2009]

Global Constraint Catalogue

• A unified catalogue regrouping many GC, their definition, semantics and filtering algorithms

• Why those constraints ?

– Found to be useful for modeling applications

– some level of abstraction

• more than 325 different Global Constraints !

• http://www.emn.fr/z-info/sdemasse/gccat/

Global Constraints & dedicated filtering algorithms (from [van Hoeve 2010])

An Example of Specialized Filtering : all_different constraint

1. all_different(X1,…Xn) replaced by n(n+1)/2 disequations

not much filtering, e.g. X Y, Y Z, Z X with domains {0,1}

2. Reasoning with number of values in the union of domains of variables

ok for previous example, but ...

all_different(X,Y,Z,T) with Dx=Dy=Dz={1,2} and DT={2,3,4,5} ...??

3. Same, but consider all subsets of variables

4. Graph algorithm [Regin 1994]:

Maximal bipartite matching

(here: size 3)

X Y Z T

1 2 3 4 5

Filtering for all_different

[from Van Hoeve 2010]

Filtering for all_different (2)

[from Van Hoeve 2010]

Filtering for all_different (3)

[from Van Hoeve 2010]

Another version of all_different

nx

xx

j

n

,,1

),,(entall_differ 1

Convex hull relaxation [Hooker, Williams & Yan 2001]

(strongest possible linear relaxation)

Jjj

n

jj

nJnJJJx

nnx

|| with ,,1 all),1|(|||

)1(

2

1

12

1

For n = 4:

1,,,

3,3,3,3,3,3

6,6,6,6

10

4321

434232413121

432431421321

4321

xxxx

xxxxxxxxxxxx

xxxxxxxxxxxx

xxxx

The sequence constraint

[from Van Hoeve 2010]

Example: Nurse Roistering in hospital

The sequence constraint (2)

[from Van Hoeve 2010]

The sequence constraint (3)

[from Van Hoeve 2010]Best algorithm for this type of filtering is O(n³)

Cumulative Scheduling

• From disjunctive to cumulative scheduling

• Capacity x duration = energy

– example [2,4] :

or

Example

• cumulative resource problem example:

• Scheduling problem with precedence

– Maximal capacity of 5 (shared resource)

– The 6 tasks have to fit in a box of maximal height = 5

– Here, makespan = 13

The cumulative constraint

• Cumulative([S1,..,Sn], ([D1,..,Dn], ([R1,..,Rn], C)

– Si = start time of task i

– Di = duration of task i

– Ri = resource usage of task i (per time unit)

– C = resource capacity (limit)

• Example:

cumulative([A,B,C,D,E,F], [2,6,2,2,5,6], [1,2,4,2,2,2],5)

The cumulative constraint (2)

• Specialized filtering can be done by considering compulsory parts

• Basic idea for one task:

• Then combine for all tasks

The cumulative constraint (3)

• More efficient (but more complex) filtering schemes than checking compulsory part only are possible

• Edge-Finding:

– Identify pairs (S,i) such that task i cannot precede (resp. follow) any task in subset S in any feasible schedule

– Update the earliest starting date (resp. latest finishing date) accordingly

• Problem: perform edge-finding efficiently

• Best algorithm is in polynomial time: O(n²)