constraint reasoning u motivation u eclipse - ic u eclipse – propia u eclipse - eplex

Post on 19-Dec-2015

232 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Constraint Reasoning

Motivation ECLiPSe - IC ECLiPSe – propia ECLiPSe - eplex

Overview

Constraint Checking, Strengthening and Relaxation Finite Domains Intervals Reified Constraints Propia Global Constraints Linear Constraints

Constraint Checking, Strengthening and Relaxation

What is constraint checking, and why is it useful? Map Colouring

What are “strengthening” and “relaxation” and why are they useful? Magic Squares

Martin Gardner’s Map

Minimal Criminal?

Martin Gardner’s Map

Constraint Checking for Software Engineering

Independence Constraint behaviour Search behaviour

Correctness “Solution” must satisfy all constraints

Generate and Test (Naive)

a b

c

d

coloured(A,B,C,D) :- label(A), label(B), label(C), label(D), ne(A,B), ne(A,C), ne(B,C), ne(A,D), ne(B,D), ne(C,D).

label(red).label(green).label(blue).

ne(A,B) :- A\=B.

This goal ordering searches 81 alternatives before failing.

Generate and Test (Optimal)

a b

c

d

coloured(A,B,C,D) :- label(A), label(B), ne(A,B), label(C), ne(A,C), ne(B,C), label(D), ne(A,D), ne(B,D), ne(C,D).

label(red).label(green).label(blue).

ne(A,B) :- A\=B.

Optimal interleaving searches 48 alternatives before failing.

State Constraints, then Encode Search

a b

c

d

coloured(A,B,C,D) :- ne(A,B), ne(A,C), ne(B,C), ne(A,D), ne(B,D), ne(C,D), label(A), label(B), label(C), label(D).

label(1).label(2).label(3).

ne(A,B) :-

This goal ordering searches 48 alternatives before failing.

suspend:(A=\=B)

Map Colouring

coloured(A,B,C,D,E,F,G,H) :- ne(A,B),ne(A,C),ne(A,D),ne(A,E),ne(A,H), ne(B,C),ne(B,F),ne(B,G), ne(C,H), ne(D,E), ne(E,F), ne(F,G), label(A),label(B),label(C),label(D ), label(E),label(F),label(G),label(H ).

label(1).label(2).label(3).

ne(A,B) :- suspend:(A=\=B)

This goal ordering searches 228 alternatives before failing.

a b

c

d e f g

h

Map Colouring

coloured(A,B,C,D,E,F,G,H) :- ne(A,B),ne(A,C),ne(A,D),ne(A,E),ne(A,H), ne(B,C),ne(B,F),ne(B,G), ne(C,H), ne(D,E), ne(E,F), ne(F,G), label(A),label(B),label(C),label( ), label(E),label(F),label(G),label( ).

label(1).label(2).label(3).

ne(A,B) :- suspend:(A=\=B)

Optimal goal ordering searches 48 alternatives before failing.

a b

c

d e f g

h

DDH

Colouring Gardner’s Map

Constraint on neighbouring regions suspend:(X=\=Y)

3 colours 70 backtracks (fail)

4 colours 40,000 backtracks(1st answer)

Constraint Checking for Software Engineering

Independence Constraint behaviour Search behaviour

Correctness “Solution” must satisfy all constraints

Correctness - CP Requirement

Every complete assignment must satisfy the constraints Every definable constraint must be checkable

Not just X=\=Y But also schedule([S1,…,Sn], [D1,…,Dn], Resources)

Purpose of Search Reach a state where “propagation” is a decision procedure Label all variables => constraint checking is a decision procedure

Constraint Checking and Relaxations

What is constraint checking, and why is it useful? Map Colouring

What are “strengthening” and “relaxation” and why are they useful? Magic Squares

Strengthening = Adding Theorems

Model Redundant constraints

Propagation Inferred constraints

Learning Nogoods

Magic Square

5

9

1

4

68

3

2

7

Constraints:

(1) Use only numbers between 1 and 9

(2) Each row, column and diagonal has the same sum

(3) Each cell is different

Generate and Test (Optimal)

5

X8

2

X7

31

4

X9

X6

Generate: Guess

Test:Check the constraints

Backtrack:If violated go back to

the last guess that still has more alternatives

Backtracks for all solutions: 21,801

Redundant Constraint

X8

5

X7

91

2

X9

X6

Add (redundant) constraints:

The sum of each row, column and diagonal is 15.X5

Backtracks for all solutions: 2,377

How to choose redundant constraints

Experience with the problem

Run a theorem prover Mathematical insight

Strengthening = Adding Theorems

Model Redundant constraints

Propagation Inferred constraints

Learning Nogoods

Propagation

X8

5

X7

81

X4

X9

X6X5

X6 in 1..9X9 in 1..98+X6+X9 = 15

X9 in 1..6

1+X5+X9 = 158+X6+X9 = 15

X5-X6 = 7

1+X4+X7 = 158+X5+X7 = 15

X4-X5 = 7

X5-X6 = 7

X4-X5 = 7X4-X6 = 14

Propagation

X8

5

X7

81

X4

X9

X6X5

(1) Propagation should help construct solutions(2) The information should be compact(3) The information should be efficiently computable

Infer all consequences of current state:

Expressible in a fixed formeg Finite domains

Using a fixed kind of inferenceeg Gaussian elimination

Strengthening = Adding Theorems

Model Redundant constraints

Propagation Inferred constraints

Learning Nogoods

Learning

X8

5

X7

91

X4

X9

X6X5

Fail

Explanation:

X1=1, X9=<8, X1+X5+X9=15 => X5 >= 6X3=9, X7>=1, X3+X5+X7=15 => X5 =< 5

X3=9, X3 ≠ X9, X9=<9 => X9 =< 8

Black: From the modelRed: Inferred constraintsWhite: Search Choices

Nogood: X1=1 X3=9

Learning

X8

X2

X7

X3X1

X4

X9

X6X5

X1=1 & X3=9 always leads to failure:

not( X1=1 & X3=9 )

not abs(X3-X1) >= 5

“Explanation” of failure:Set of choices which, together with model, preclude all alternatives for some variable

Find “oldest” set of choices inconsistent with modelFind minimal set of choices inconsistent with modelFind minimal inconsistent set of constraints

Relaxation

Approximate model constraints Approximate inferred constraints Approximate learned nogoods

Propagation is Relaxing

X8

5

X7

81

X4

X9

X6X5

Relaxation:<X4,X7> є {<6,8>, <7,7>,<9,5>} => X4::{6,7,9}

(1) Propagation should help construct solutions“Easy” constraints (FD, linear)

(2) The information should be compactFD linear basis , convex hull

(3) The information should be efficiently computablePolynomial algorithms

Relaxation by Abstraction

X8

X7

X9

X6X5

low med high

med Abstract “sum” constraints

Relax “alldifferent” constraint

Each row/column/diagonal has: either a low, a medium and a high or a medium, a medium and a medium

Represent numbers 1 to 9by three values: low, medium, and high

Using the relaxation

high

low

med

lowmed

4 2 9

7

Use a solution to the relaxedproblem to guide search on thefull problem:

low => 1 or 2 or 3med => 4 or 5 or 6high => 7 or 8 or 9

Backtracks (on both relaxed and full problem) for all solutions: 495

Linear Relaxation

A solution for linear numeric constraints can be found by an efficient algorithm. Express magic square problem using only linear constraints.

Each row/column/diagonal: Xi+Xj+Xk = Sum

Introduce 9 (0/1) variables Bi1…Bi9 for each Xi:Bi1+Bi2*2 + … + Bi9*9 = XiBi1+Bi2 + … + Bi9 = 1

For each integer n between 1 and 9: B1n+B2n + … + B9n = 1

Linear Relaxation

5.0

8.0

2.0

5.0

5.08.0

2.0

1.99

7.99

Relax the constraint that the Xi and Bin are integers.Allow any “real” values between 1.0 .. 9.0 and 0.0 .. 1.0 respectively

Search by adding constraints:X3>=6.0After one search step:

5.0

9.0

1.0

4.0

6.08.0

3.0

2.0

7.0

Overview

Constraint Checking, Strengthening and Relaxation Finite Domains Intervals Reified Constraints Propia Global Constraints Linear Constraints

ECLiPSe Usage

Load the interval constraint library by using

:- lib(ic).

at the beginning of your code, or type

lib(ic).

at the top-level ECLiPSe prompt.

Functionality

The IC library implements variables with integer or real domains basic equality, inequality and disequality constraints over

linear and nonlinear arithmetic expressions some “global” constraints for integer variables reified constraints search and optimisation facilities

Interval variables

IC variables have a domain attached

integer intervals : X{1..9}

integer intervals with holes: Y{[2,5..7]}

real intervals: Z{-0.5..3.5}

infinite intervals: W{-0.5..1.0Inf}

infinite integer intervals: V{-1.0Inf..3}

Interval variables

Vars :: Domain e.g. X :: 1..9 X #:: 1..9 Y :: [2,5..7] Y #:: [2,5..7] Z :: -0.5..3.5 Z $:: -0.5..3.5 W :: -0.5..1.0Inf W $:: -0.5..1.0Inf V :: -1.0Inf..3 V #:: -1.0Inf..3

Attaches an initial domain to a variable or intersects its old domain with the new one.

Type of bounds gives type of variable for :: (1.0Inf is considered type-neutral)

#:: always imposes integrality $:: never imposes integrality

The basic set of constraints

X #>= Y, Y #=< Z, X $>= Y, Y $=< Z Non-strict inequalities.

X #> Y, Y #< Z, X $> Y, Y $< Z Strict inequalities.

X #= Y, Y #\= Z, X $= Y, Y $\= Z Equality and disequality.

X,Y,Z can be expressions + - * / ^ abs sqr exp ln sin cos min max sum ...

“#” constraints impose integrality, “$” constraints do not

Basic search support

indomain(?Var) Instantiates Var to a value from its domain. Tries values from smallest to largest on backtracking. If X :: 1..3 then indomain(X) is the same as X=1 ; X=2 ; X=3

labeling(+VarList) Invokes indomain/1 on each variable in the list.

More sophisticated search covered in a later session

Colouring Gardner’s Map

Constraint on neighbouring regions suspend:(X=\=Y) ic:(X=\=Y)

3 colours 70 backtracks 3 backtracks (fail)

4 colours 40,000 backtracks 2000 backtracks(1st answer)

More built-in constraints

alldifferent(+List) All elements of the list are constrained to be pairwise

different.

integers(+List) All elements of the list are constrained to be integral.

reals(+List) All elements of the list are constrained to be real. (Note this doesn’t mean they can’t also be integral: this

is equivalent to List :: -1.0Inf..1.0Inf.)

Standard example

sendmore(Digits) :-

Digits = [S,E,N,D,M,O,R,Y],

Digits :: 0..9,

alldifferent(Digits),

S #\= 0, M #\= 0,

1000*S + 100*E + 10*N + D

+ 1000*M + 100*O + 10*R + E

#= 10000*M + 1000*O + 100*N + 10*E + Y,

labeling(Digits).

SEND+ MORE

= MONEY

For Magic Square: Matrices

?- dim(S, [3, 3]), BL is S[3, 1].

S = []([](_265, _266, _267), [](_261, _262, _263), [](BL, _258,_259))Yes (0.00s cpu)

_258

_266

BL

_267_265

_261

_259

_263_262

For Magic Square: Iteration

?- for(J, 1, 3) do writeln(J).

123Yes (0.00s cpu)

For Magic Square: Iteration Over Matrices

?- dim(S,[3,3]), (for(J,1,3),param(S) do S[J,1] #= I+1, S[J,2] #= I+2, S[J,3] #= I+3 ).

S = []([](2, 3, 4), [](3, 4, 5), [](4, 5, 6))Yes (0.00s cpu)

Magic Square

:- lib(ic).

square(S) :- dim(S,[3,3]), term_variables(S,Vars), Vars::1..9, alldifferent(Vars), ( for(J,1,3), param(S) do S[J,1]+S[J,2]+S[J,3] #= 15, S[1,J]+S[2,J]+S[3,J] #= 15 ), S[1,1]+S[2,2]+S[3,3] #= 15, S[1,3]+S[2,2]+S[3,1] #= 15, labeling(Vars).

Magic Square

S = []([](8, 3, 4), [](1, 5, 9), [](6, 7, 2))Yes (0.00s cpu, solution 1, maybe more)

[eclipse]: square(S)

An arithmetic puzzle

Is there a positive number which when divided by 3 gives a remainder of 1; when divided by 4 gives a remainder of 2; when divided by 5 gives a remainder of 3;

and when divided by 6 gives a remainder of 4?

(express the constraints with multiplications rather than divisions)

model(X) :- ic:integers([A,B,C,D]), ic:(X > 0), ic:(X =:= A*3 + 1), ic:(X =:= B*4 + 2), ic:(X =:= C*5 + 3), ic:(X =:= D*6 + 4).

An arithmetic puzzle

model(X) :- ic:(X > 0), ic:(X =:= A*3 + 1), ic:(X =:= B*4 + 2), ic:(X =:= C*5 + 3), ic:(X =:= D*6 + 4).

?- model(X).X = X{0.0 .. 1.0Inf}There are 5 delayed goals.Yes (0.00s cpu)

What went wrong??

An arithmetic puzzle

model(X) :-

ic:integers([A,B,C,D]),

ic:(X > 0),

ic:(X =:= A*3 + 1),

ic:(X =:= B*4 + 2),

ic:(X =:= C*5 + 3),

ic:(X =:= D*6 + 4).

?- model(X).

X = X{58 .. 1.0Inf}

Delayed goals:

ic:(-3*A{19..1.0Inf} + X{58..1.0Inf} =:= 1)

ic:(-4*B{14..1.0Inf} + X{58..1.0Inf} =:= 2)

ic:(-5*C{11..1.0Inf} + X{58..1.0Inf} =:= 3)

ic:(X{58..1.0Inf} - 6*D{9..1.0Inf} =:= 4)

Yes

An arithmetic puzzle

?- model(X).X = X{58 .. 1.0Inf}Delayed goals: ic:(-3*A{19..1.0Inf} + X{58..1.0Inf} =:= 1) ic:(-4*B{14..1.0Inf} + X{58..1.0Inf} =:= 2) ic:(-5*C{11..1.0Inf} + X{58..1.0Inf} =:= 3) ic:(X{58..1.0Inf} - 6*D{9..1.0Inf} =:= 4)Yes

?- model(X), labeling([X]).X = 58 More? (;) X = 118 More? (;) X = 178 More? (;) ...

Overview

Constraint Checking, Strengthening and Relaxation Finite Domains Intervals Reified Constraints Propia Global Constraints Linear Constraints

Interval Arithmetic and Constraints

Interval Arithmetic

Real values often can’t be represented exactly by floating point numbers

Calculations introduce rounding errors

Problems: Is the result really a solution? Were solutions missed?

Example

X is sqrt(2), Y is X*X. X = 1.4142135623730951 Y = 2.0000000000000004

Interval Arithmetic

Solution: Represent each real value by a pair of floating

point bounds Arithmetic performed on intervals, with

appropriate rounding A ground interval expresses that the exact real

value lies somewhere between its bounds

The bounded real data type

Written lwb__upb, where lwb and upb are the lower and upper floating point bounds, respectively (e.g. 0.12__0.16)

Not usually entered directly: normally occur as result of computation

breal/1 tests whether a term is a bounded real

breal/2 converts other numeric types to bounded reals

breal_min/2, breal_max/2 and breal_bounds/3 can

be used to obtain the floating point bounds of a bounded real

Example

X is sqrt(2), Y is X*X. X = 1.4142135623730951 Y = 2.0000000000000004

X is sqrt(breal(2)), Y is X*X. X = 1.4142135623730949__1.4142135623730954 Y = 1.9999999999999996__2.0000000000000013

The bounded real data type

?- Y is float(1) / 10,

X is Y + Y + Y + Y + Y + Y + Y + Y + Y + Y.

X = 0.99999999999999989

Y = 0.1

Yes

?- Y is breal(1) / 10,

X is Y + Y + Y + Y + Y + Y + Y + Y + Y + Y.

X = 0.99999999999999911__1.0000000000000009

Y = 0.099999999999999992__0.10000000000000002

Yes

Interval Arithmetic

Problem: Arithmetic comparison now only partial E.g. is 0.12__0.16 = 0.13__0.15? > ? < ?

Solution: Leave incomparable comparisons as delayed goals Presence of delayed goals indicates that the solution is a

“candidate” only User decides if delayed goals indicate a problem

Example

X = 2.5__3.5, Z is X+5 X = 2.5__3.5 Z = 7.5__8.5

X=2.5__3.5, Y = 3.0__4.0, X+5>Y X = 2.5__3.5 Y = 3.0__4.0

X=2.5__3.5, Y=3.0__4.0, X-5 > Y Fail

X=2.5__3.5, Y=3.0__4.0, X > Y X = 2.5__3.5 Y = 3.0__4.0 Delayed Goal: 2.5__3.5 > 3.0__4.0

IC for real variables

IC’s general constraints ($=/2, $=</2, etc.) work for:real variablesinteger variablesa mix of both

Propagation is performed using safe arithmetic Integrality preserved where possible

Two kinds of intervals: “Ground” interval

Approximates a single (ground) real value Bounds never change Semantics: Y=lwb__upb

X. lwb ≤ X ≤ upb Y=X

“Variable” interval Approximates the domain of a real variable Bounds can be updated Semantics: lwb ≤ Y ≤ upb

Interval Arithmetic

Semantics

“Ground Interval” Y $= 2.99__3.01, Y $>= 4.

Fails Y $= 2.99__3.01, Y $>= 2.

Y = 2.99__3.01 Y $= 2.99__3.01, Y $>= 3.

Y = 2.99__3.01 Delayed goal: (0 $=< -0.009__0.01) Semantics: Y=[2.99,3.01] Y≥3

Interval Constraint Y :: 2.99..3.01, Y $>= 3

Y = Y{3.0 .. 3.01} Semantics: 3.0 ≤ Y ≤ 3.01

Example

?- 3*X $= 10.0, 6*Y $= 20.0, X $= Y.

X = 3.333333333333333__3.3333333333333335Y = 3.333333333333333__3.3333333333333335There is 1 delayed goal.Yes (0.00s cpu)

Delayed Goal:ic : (0 =:= -4.44e-16__4.44e-16)

Solving real constraints

IC provides two methods for solving real constraints

locate/2,3 good when there are a finite

number of discrete solutions Works by splitting domains

squash/3 good for refining bounds on a

continuous feasible region Works by trying to prove parts of domains infeasible

Not using locate…

Find the intersection of two circles

?- 4 $= X^2 + Y^2, 4 $= (X - 1)^2 + (Y - 1)^2).

X = X{-1.000000000000002 .. 2.0000000000000013}Y = Y{-1.000000000000002 .. 2.0000000000000013}

There are 12 delayed goals.Yes

Using locate

?- 4 $= X^2 + Y^2,

4 $= (X - 1)^2 + (Y – 1)^2,

locate([X, Y], 1e-5).

X = X{-0.82287566035527082 .. -0.822875644848199}

Y = Y{1.8228756448481993 .. 1.8228756603552705}

There are 12 delayed goals.

More ? ;

X = X{1.8228756448481993 .. 1.8228756603552705}

Y = Y{-0.82287566035527082 .. -0.822875644848199}

There are 12 delayed goals.

Yes

IC Example (modelling, abstraction)

A

C B7

George is contemplating buying a farm which is a very strange shape, comprising a large triangular lake with a square field on each side.

The area of the lake is exactly seven acres. The area of each field is an exact whole number of acres.

What is the smallest possible total area of the three fields?

IC Example - Model

farm(F, A, B, C) :-

[A, B, C] :: 0.0..1.0Inf, % The 3 sides of the lake

triangle_area(A, B, C, 7), % The lake area is 7

[F, FA, FB, FC] :: 1..1.0Inf, % The square areas are integral

square_area(A, FA),

square_area(B, FB),

square_area(C, FC),

F #= FA+FB+FC,

% Avoid symmetric solutions

FA $>= FB, FB $>= FC.

A

C B7

IC Example - Abstraction

triangle_area(A, B, C, Area) :- S $>= 0, S $= (A+B+C)/2, Area $= sqrt(S*(S-A)*(S-B)*(S-C)).

square_area(A, Area) :- Area $= sqr(A).

A

C B7

IC Example - Search

solve(F, A, B, C) :- farm(F, A, B, C), % the model indomain(F), % ensure that solution is minimal locate([A, B, C], 0.01).

?- solve(F, A, B, C).F = 50A = A{4.4721359549995787 .. 4.4721359549995805}B = B{4.12310562561766 .. 4.1231056256176615}C = C{3.6055512754639887 .. 3.6055512754639896}There are 23 delayed goals.More?

A

C B7

Overview

Constraint Checking, Strengthening and Relaxation Finite Domains Intervals Reified Constraints Propia Global Constraints Linear Constraints

Reified constraints

Basic IC constraints revisited

X #= Y

X #>= Y

X #< Y

X #\= Y etc.

#=(X, Y)

#>=(X, Y)

#<(X, Y)

#\=(X, Y)

etc.

#=(X, Y, B)

#>=(X, Y, B)

#<(X, Y, B)

#\=(X, Y, B)

etc.

Boolean variables B indicate truth of constraint

Reified constraints

X #> Y #>(X, Y, B)X #= Y #=(X, Y, B)

B=1 if the constraint is satisfied (entailed) B=0 if the constraint is false (disentailed) B{0..1} while unknown

B can be set to 1 to enforce the constraint 0 to enforce its negation

Disjunctive constraints via reified constraints

no_overlap(S1, D1, S2, D2) :-

#>=(S2, S1+D1, B), #<(S1, S2+D2, B).

1

1

2

2

B=1

B=0

2 Fail

1

1 2 B{0..1}

Constraint connectives

neg C Negation of constraint C

C1 and C2 C1 and C2

C1 or C2 C1 or C2

C1 => C2 C1 implies C2

E.g. X #= 0 => Y #> 0

Embedding reified constraints

Constraints can appear in other expressions Evaluate to their reified boolean Sometimes the easiest way to reify a constraint

B #= (X #>= Y + 2)

evaluated as: #>=(X,(Y+2),B)

1 #= (X1 #< Y1 + (X2 #=< Y2))

evaluated as:

#<(X1,(Y1+B2),B1), #=<(X2,Y2,B2), B1#=1

This is how the constraint connectives (and, or, etc.) are actually implemented

82

Remember – Resource ProfilesS1

S2S3

E1E2

E3

No. ofResourcesRequired

Time

321

Activities

321

S1 S2S3

Potentialresourcebottleneckpoints

Resource Profile

For each time point t represent the number of executing jobs by Rt

Each job j has a start time Sj and an end time Ej For each time point t and job j introduce a boolean Btj

which is true (i.e.1) if job j is executing at time t.

Rt #= Btj1 + Btj2 + … + Btjn

Constraint defining Rt:

Constraint defining B4j:

Btj #= (Sj<4 and Ej>4)

84

Remember – Resource ProfilesS1

S2S3

E1E2

E3

No. ofResourcesRequired

Time

321

Activities

321

S1 S2S3

Potentialresourcebottleneckpoints

Resource Start Times Profileprofile(S1-E1-P1,S2-E2-P2,S3-E3-P3) :- B12 #= (S1>=S2 and S1<E2), B13 #= (S1>=S3 and S1<E3), P1 #= 1 + B12 + B13,

B21 #= (S2>=S1 and S2<E1), B23 #= (S2>=S3 and S2<E3), P2 #= B21 + 1 + B23,

B31 #= (S3>=S1 and S3<E1), B32 #= (S3>=B2 and S3<E2), P3 #= B31 + B32 + 1. ?- S1::1..3,S2::3..5,S3::5..7,

profile(S1-5-P1,S2-8-P2,S3-9-P3).S1 = S1{1 .. 3}P1 = P1{[1, 2]}S2 = S2{3 .. 5}P2 = P2{1 .. 3}S3 = S3{5 .. 7}P3 = 2There are 8 delayed goals.Yes (0.00s cpu)

Disjunctive constraints via reified constraints (II)

Note that the following are all equivalent:

#>=(S2, S1+D1, B), #<(S1, S2+D2, B)

(S2 #>= S1+D1) #= (S1 #< S2+D2)

S2 #>= S1+D1 or S1 #>= S2+D2

Reified Constraints and Real Variables

?- X $= 1.1, 3.0 * Y $= 3.3, '$=<'(X, Y, B).

X = 1.1__1.1Y = 1.0999999999999999__1.1B = B{[0, 1]}There is 1 delayed goal.Yes (0.00s cpu)

The constraint is sometimes not “decided” when it should be

Overview

Constraint Checking, Strengthening and Relaxation Finite Domains Intervals Reified Constraints Propia Global Constraints Linear Constraints

#

Propia

Transforming Constraint Specification into Constraint Behaviour

Motivation for Propia

Encode constraints in readable form Set up model without leaving choice points Support constraint propagation

NB: Never introduce choice points in constraint setupWhy not?•repeatedly setting up constraints on different branches•partially blind searching

The noclash Example

noclash(S,T) (T S+5) (S T)Specification:

Encoding as ECLiPSe clauses:

noclash(S,T) :- T #>= S+5.noclash(S,T) :- S #>= T.

The noclash procedure in ECLiPSe

noclash(S,T) :- T #>= S+5.noclash(S,T) :- S #>= T.

This program leaves a choice point

?- S::1..10, noclash(S,6).

S = 1 ;

S = S{[6..10]}

noclash with Propia

noclash(S,T) :- T #>= S+5.noclash(S,T) :- S #>= T.

S{[1, 6..10]}

This program does not create any choice points

?- S::1..10, (noclash(S,6) infers most).

Choice Points and Efficiency Experiment

sum(Products,Raw1,Raw2,Profit) :- ( foreach(Item,Products), foreach(R1,R1List), foreach(R2,R2List), foreach(P,PList) do product(Item,R1,R2,P) ), Raw1 #= sum(R1List), Raw2 #= sum(R2List), Profit #= sum(PList).

product( 101,1,19,1). product( 102,2,17,2). product( 103,3,15,3). product( 104,4,13,4). product( 105,10,8,5). product( 106,16,4,4).product( 107,17,3,3). product( 108,18,2,2). product( 109,19,1,1).

£11pin19nuts

£22pins17nuts

£PR1pinsR2nuts

Raw1 = 1 + 2 + R1Raw2 = 19 + 17 + R2Profit= 1 + 2 + P

Products:

Choice Points and Efficiency

product_plan(Products) :- length(Products,9), Raw1 #=< 95, Raw2 #=< 95, Profit #>= 40, sum(Products,Raw1,Raw2,Profit), labeling(Products).

sum(Products,Raw1,Raw2,Profit) :- ( foreach(Item,Products), foreach(R1,R1List), foreach(R2,R2List), foreach(P,PList) do product(Item,R1,R2,P) ), Raw1 #= sum(R1List), Raw2 #= sum(R2List), Profit #= sum(PList).

infers most

33 secs

Time for all 13801 solutions:

Time to first solution:

1 hour

95>= R1+R1+R1+R1+R1+R1+R1+R1+R195>= R2+R2+R2+R2+R2+R2+R2+R2+R240=< P +P +P +P +P +P +P +P +P

Propia

Annotations Most Specific Generalisation Algorithm

Propia’s infers Annotation

Propia annotation says what you want to infer: Goal infers consistent

Fail as soon as inconsistency can be proven

Goal infers unique Instantiate as soon as unique solution exists

Goal infers ic Maintain finite domains covering all solutions

Goal infers most Use strongest available representation covering all solutions

Annotation Resultsp(1,1).p(1,3).p(1,4).

?- p(X,Y) infers consistent.No information extracted?- p(X,3) infers unique.X=1?- p(X,Y) infers most.X=1 (assuming ic is not loaded)?- p(X,Y) infers most.X=1,Y{[1,3,4]} (assuming ic is loaded)?- p(X,Y) infers ic.X=1,Y{[1,3,4]} (ic must be loaded)

More “global” Consistency

pp(X,Y,Z) :- p(X,Y), p(Y,Z).

?- pp(X,Y,Z) infers most.X=1, Y=1, Z{[1,3,4]}

C1C5

C4

C2

C3 C6

C7

C8Problem

Constraints

(C1,C4,C5) infers most

Path Consistency?

For each triple X,Y,Z define: pXYZ(X,Y,Z) :- pXY(X,Y), pYZ(Y,Z), pZX(Z,X).

Use infers ic to achieve path consistency: solve(A,B,…,X,Y,Z) :- pABC(A,B,C) infers ic, … pXYZ(X,Y,Z) infers ic, labeling([A,B,…,Y,Z]).

Does this maintain path consistency? Doesn’t remove tuples from binary constraints Weak propagation on triples that share 2 variables

Propia Algorithm

Query ?- p(X,2,Z)

Answers p(1,2,3). p(3,2,4). p(3,2,3).

Query?- p(X,2,Z) infers most

Result “Most specific generalisation” of

{p(1,2,3), p(3,2,4), p(3,2,3)} p(X{1,3},2,Z{3,4})

102

Most Specific Generalisation

The Opposite of Unification!

[1,A] [1,1]

[1,X]

[1,1] [2,2]

[X,X]

“most specific generalisations”

103

Most Specific Generalisation over Finite Domains

1 2

X::{[1,2]}

f(1,2) f(2,4)

f(X::{1,2},Y::{2,4})

“most specific generalisations”

104

Naïve Propia Algorithm

Goal infers most

Find all solutions to Goal, and put them in a setFind the most specific generalisation of all the terms in the set

member(X,[1,2,3]) infers most

Find all solutions to member(X,[1,2,3]): {1,2,3}

Find the most specific generalisation of {1,2,3}: X::{1,2,3}

105

Propia Algorithm

Goal infers most

Find one solution S to Goal The current most specific generalisation MSG = S

Repeat Find a solution NewS to Goal

which is NOT an instance of MSG Find the most specific generalisation NewMSG

of MSG and NewS MSG := NewMSG

until no such solution remains

106

Example - without finite domains

p(1,2).p(1,3).p(2,3).p(2,4).

p(X,Y) infers most

MSG:1st Iteration: p(1,2)2nd Iteration: p(1,_)3rd Iteration: p(_,_)No more iterations

107

Propia Algorithm for Arc Consistency

p(1,2).p(1,3).p(2,3).p(2,4).

This algorithm only works for predicates defined by ground facts

p(X,Y) infers ic

is implemented as:

element(I,[1,1,2,2],X),element(I,[2,3,3,4],Y).

Global Consistency

Reasoning on Combinations of Constraints Propia can apply consistency techniques to combinations of constraints Propia can be applied to program-defined predicates These predicates combine sets of constraints selected in advance of program

execution

Overview

Constraint Checking, Strengthening and Relaxation Finite Domains Intervals Reified Constraints Propia Global Constraints Linear Constraints

Modelling

Built-in constraints X #> Y

Abstraction before(task(Si,Di), task(Sj,Dj)) :- Si+Di #=< Sj.

Conjunction between(X,Y,Z) :- X #< Y, Y #< Z.

Disjunction neighbour(X,Y) :- ( X #= Y+1 or Y #= X+1 ).

Iteration not_among(X, L) :- ( foreach(Y,L),param(X) do X #\= Y ).

Recursion not_among(X, []). not_among(X, [Y|Ys]) :- X #\= Y, not_among(X, Ys).

Propagation in IC

Broken down into Binary Constraints W+X >= Y+Z becomes W+X = A, Y+Z=B, A >= B

Propagation on Bounds only?- Y::1..3, X #= 2*Y X = X{2 .. 6}Y = Y{1 .. 3}There is 1 delayed goal.Yes (0.00s cpu)

Propagation on bounds decides simple temporal constraints X >= Y+ <Constant>

No propagation decides disequations Conjunctions of disequations are NP complete

N-ary constraints in lib(ic)

S #= sum(List) Sum of N variables or sub-expressions.

X #= min(List) Smallest of N variables or sub-expressions.

X #= max(List) Largest of N variables or sub-expressions.

alldifferent(List) All elements of the list are pairwise different.

Global constraints

Constraints involving many variables Do more “global” reasoning Based on IC primitives

:- lib(ic).

Available in the libraries :- lib(ic_global). :- lib(ic_cumulative). :- lib(ic_edge_finder). :- lib(ic_edge_finder3).

Different constraint behaviours

lib(ic) implementation of alldifferent/1

?- [A,B,C]::1..3, D::1..5, ic:alldifferent([A,B,C,D]).

A = A{1 .. 3}

B = B{1 .. 3}

C = C{1 .. 3}

D = D{1 .. 5}

Delayed goals:

outof(A{1 .. 3}, [], [B{1 .. 3}, C{1 .. 3}, D{1 .. 5}])

outof(B{1 .. 3}, [A{1 .. 3}], [C{1 .. 3}, D{1 .. 5}])

outof(C{1 .. 3}, [B{1 .. 3}, A{1 .. 3}], [D{1 .. 5}])

outof(D{1 .. 5}, [C{1 .. 3}, B{1 .. 3}, A{1 .. 3}], [])

Yes

top related