E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout...

42
E.N.S.E.I.R.B. March 2001 pyg / Constraint Logic Programming 1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes by Marc-Michel Corsini “Constraint Logic Programming: A Survey”, by Joxan Jaffar and Michael J. Maher, Journal of Logic Programming 1994: 19, 20: 503-581 “Constraint Logic Programming - An Informal Introduction”, by Thom Frühwirth, Alexander Herold, Volker Küchenhoff, Thierry le Provost, Pierre Lim, Eric Monfroy, Mark Wallace, technical report ECRC-93-5 “Constraint Logic Programming”, by Dick Fountain, Byte Magazine February 1995, © Mc Graw Hill “Algorithms for Constraint-Satisfaction Problems: A Survey”, by Vipin Kumar, AI magazine, Vol.13, N°1, Spring 1992: 32-44 Journées Francophones de Programmation en Logique, LaBRI, Université Bordeaux I, 25-27 mai 1994 CHIP V5 Reference Manual, © Cosytec SA
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    226
  • download

    0

Transcript of E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout...

Page 1: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 1

Constraint Logic Programming

handoutPaul Y Gloess

sources of inspiration include:

CLP notes by Marc-Michel Corsini

“Constraint Logic Programming: A Survey”, by Joxan Jaffar and Michael J. Maher, Journal of Logic Programming 1994: 19, 20: 503-581

“Constraint Logic Programming - An Informal Introduction”, by Thom Frühwirth, Alexander Herold, Volker Küchenhoff, Thierry le Provost, Pierre Lim, Eric Monfroy, Mark Wallace,

technical report ECRC-93-5

“Constraint Logic Programming”, by Dick Fountain, Byte Magazine February 1995, © Mc Graw Hill

“Algorithms for Constraint-Satisfaction Problems: A Survey”, by Vipin Kumar, AI magazine, Vol.13, N°1, Spring 1992: 32-44

Journées Francophones de Programmation en Logique, LaBRI, Université Bordeaux I, 25-27 mai 1994

CHIP V5 Reference Manual, © Cosytec SA

Page 2: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 2

Course Outlinemotivation for constraints

deal with arithmetics or boolean domainscombine efficiency and declarativeness

Constraint Logic ProgrammingCLP(X)

X = rational linear constraintsX = CHIP finite domainsX = boolean [or CHIP boolean]X = trees

LP = CLP(trees) or “pure Prolog revisited”constraint satisfiabilityCLP operational semantics

Constraint Satisfaction Problemnode and arc consistencygeneral backtracking algorithm

Generate and Test (GT)Standard Backtracking (SB)Forward Checking (FC)Look Ahead (LA)

comparison on the queen example

CHIP

Page 3: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 3

declarative programming

Constraint Solving(CS)

Logic Programming(LP)

Constraint Logic Programming(CLP)

Page 4: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 4

CLP examplesumto(N,S) S=1+…

+N

sumto(0, 0) .sumto(N, S) :- {N1, NS}, sumto(N-1,S-N).

{S3}, sumto(N, S).

{N=0, S=0};

{N=1, S=1};

{N=2, S=3};

nomore solution

sumto(N, N).

{N=0, S=0};

{N=1, S=1};

nomore solution

sumto(N+1, N);

no solution!

Page 5: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 5

CLP search tree

{S3}, sumto(N, S)

{S3, N=0,S=0} {S3, N1= N, S1=S, 1N1 S1}

sumto(N1-1, S1- N1){N=0,S=0}

{S3, N1= N, S1=S, 1N1S1

N1-1=0, S1-N1=0}{S3, N1= N, S1=S, 1N1S1

N2=N1-1, S2=S1- N1, 1N2S2}

sumto(N2-1, S2- N2)

{N=2, S=3}

{S3, N1= N, S1=S, 1N1S1

N2=N1-1, S2=S1- N1, 1N2S2

N2-1=0, S2-N2=0}

{S3, N1= N, S1=S, 1N1S1

N2=N1-1, S2=S1- N1, 1N2S2

N3=N2-1, S3=S2-N2, 1N3S3}

3 S = S3+2N3+3 6unsatisfiable!

{N=1,S=1}

Page 6: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 6

LP power versus CLP power

add(0, N, N) .add(s(M), N, s(MN)) :- add(M, N, MN).

add(M, N, K), add(M, N, s(K)).… loop for ever or stack overflow

LP

{M+N=K, M+N=K+1}.no solution!

CLP

Page 7: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 7

LP inefficiency versus CLP efficiency

• LP paradigmgenerate then test many unuseful branches explored

• CLP paradigmtest then generate cuts branches soon, avoiding exploration

Page 8: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 8

CLP operational semantics

inference rulesC, p(X1,,Xn) | literals

C Y1 X1,, Yn Xn C', h1,,hk | literals provided that

p(Y1,,Yn) : C',h1,,hk is a clause variant ,

C Y1 X1,, Yn Xn C' is satisfiable.

C, C

solution (projected on user variables)

provided that C is satisfiable.

C, literals

failure

provided that C is unsatisfiable.

Page 9: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 9

CLP program normalization

sumto(0, 0) .sumto(N, S) :- N1, NS, sumto(N-1,S-N).

sumto(N, S) :- {N=0, S=0}.sumto(N, S) :- {N1, NS, N’=N-1, S’=S-N},

sumto(N’,S’).

normalization

Predicate arguments become distinct variables.Predicate arguments become distinct variables.

Page 10: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 10

CLP(X)

• A basis B=F,P for constraints

• P: predicate symbols containing equality “=”;

• F: function symbols.

• A constraint language (a set of B-atoms).

• A B-semantics I of domain D with

• fI : Dn D, for each f in Fn;

• pI : Dn Bool, for each p in Pn;

• =I : DD Bool is “mathematical equality”.

An additional set LP of symbols is available to the user for defining predicates: symbols in LP are “uninterpreted”, have no semantics. [No uninterpreted function symbols are necessary, owing to normalization.]

Page 11: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 11

Constraint Satisfaction

main CLP(X) issue

X |= C

read: C is satisfiable in X

IX |= C

where: C is the existential closure of C

bydefinition

constraint[s] in LX

BX, LX, IX

Page 12: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 12

CLP(Qlin)rational linear constraints

• Constraint basis B=F,P with

• P = {=, <, , >, };

• F = {+, -, -1, *, /} Q

• Constraint language (linear arithmetics):

• X+3*Y > 50*Z - (3/4)*Y

• X*Y 3 not allowed (not linear)

• X/5 > Y/2 division by numbers OK

• X/Y = Z not allowed

• B-semantics I of domain Q with classical interpretation of predicate and function symbols.

Page 13: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 13

CLP(FD)CHIP Finite Domains

• Constraint basis B=F,P with

• P = {=, <, , >, , } {in[m,n] | m n, m,n Nat}

• F = {+, *} Nat

• Constraint language (linear integer arithmetics with each variable ranging in a finite domain):

• {X[1,5], Y [0,7], X 3, X+2*Y5, X+Y9}

• B-semantics I of domain NatZ interprets arithmetic operators and comparators as usual. The constraint X[m,n] is understood as mXn.

Page 14: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 14

CLP(Boolean)boolean constraints

• Constraint basis B=F,P with

• P = {=};

• F = {true, false, ,, , , }.

• Constraint language (boolean constraints):

• ((XY) X) = (Y Z)

• (XY) = true

• B-semantics I of domain Bool={true, false} with usual interpretation of boolean symbols.

Remark: since the domain of I is Bool, it is possible to let all boolean operators (symbols in F) belong to P as well.

Page 15: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 15

CLP(CHIP Boolean)CHIP boolean constraints

CHIP extends boolean constraints to arbitrary symbolic constants other than true and false. These constants are implicitely universally quantified.

Example:

((Xdrunk) alcoholic) = (X drunk)

Satisfiability of this constraint is defined by:

Boolean |= X D A ((XD) A) = (XD)

Page 16: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 16

CLP(Finite Trees)=

LP with occur check

• Constraint basis B=F,P with

• P = {=};

• F = F0 F1 F2… Fn … .

• Constraint language (tree unification):

• f(g(X,X),U,U) = f(Y,Y,g(a,Z))

• X = f(X,Y)

• B-semantics I interprets function symbols as tree constructors:

• hI(1, …, n) = h

1 … n

Page 17: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 17

CLP(Finitely Branched Regular Trees)=

LP with no occur check

• Finite trees are replaced with finitely branched regular trees:

• A (possibly infinite) tree is finitely branched if each node has a finite number of successors;

• A (possibly infinite) tree is regular if it has a finite number of subtrees.

• Constraint language is the same:

• f(g(X,X),U,U) = f(Y,Y,g(a,Z))

• X = f(X,Y) becomes satisfiable!

• B-semantics I also interprets function symbols as tree constructors.

Page 18: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 18

LP rewritten in CLP style

append example

append(nil,L,L) .append(c(E,L), R, c(E, LR)) :- append(L, R, LR).

append(L,R,LR) :- {L=nil, R=LR}.append(L,R,LR) :- {L=c(E,L’), LR=c(E,LR’)},

append(L’, R, LR’).

normalization

Page 19: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 19

Constraint Satisfaction in X• Constraint language must be decidable:

– existence of algorithm for deciding whetherX |= C holds or not

• Decision algorithm must be efficient:– polynomial or linear on the average;– better: polynomial or linear in the worst case.

• Decision algorithm better be incremental:– inference adds new constraints to a satisfiable set: algorithm should just test

compatibility of the new ones with the old ones, not test satisfiability of whole set.

• Compromises are possible:– admit constraint languages (e.g., non linear constraints) with no algorithm;– wait until constraints become decidable (e.g., linear) by further instantiation of

variables.

Page 20: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 20

Delaying non linear Constraints

mult([R1,I1], [R2,I2], [R,I]) :- R = R1*R2 - I1*I2,I = R1*I2 + I1*R2.

complex multiplication

mult([1,2], [3,4], [R,I]);

{R=-5, I=10};nomore solution

mult([1,2], [X,Y], [A,B]);

{A=X-2*Y, B=Y+2*X};nomore solution

mult([R1,2], [R2,4], [-5,10]);

{R1=-0.5*R2+2.5, 3=R1*R2} maybe;

nomore solution

mult([R1,2], [R2,4], [-5,10]), (R1 =1; R1 =2; R1 =3);

{R1 =1, R2=3};

nomore solution

Page 21: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 21

k-Consistency Techniques• Goal:

– speed up constraint solving.

• Assumptions:– each variable ranges in a domain (usually finite);– any kind of constraints.

• Method:– try to narrow variable domains by detecting inconsistencies among domains of variable

subsets of size k;– propagate information about variables until domains become stable (cannot be reduced).

• Advantages and drawbacks:– efficient in practice;– very general: not specific of a kind of constraints (works even without a satisfiability decision

procedure);– not complete: no guarantee to detect unsatisfiability of the constraints until all variables are

instantiated.

Page 22: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 22

2-consistency scheduling example(2-consistency = arc-consistency)

time

Ti = start time of task iTi {1, 2, 3, 4, 5}

0 1 2 3 4 5time time time time time

T1 < T2

T1 < T3

T2 T3

T2 < T6

T3 < T5

T4 < T5

T5 < T6

T2

T1

T3 T5

T4

T6

Page 23: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 23

2-consistency scheduling example

T2

T1

T3 T5

T4

T6

12345

1234512345

12345

12345

12345

12345T2

T1

T3 T5

T4

T612345

12345

12345

12345

12345

T2

T1

T3 T5

T4

T6

12345

1234512345

12345

12345

12345

T2

T1

T3 T5

T4

T6

12345

1234512345

12345

12345

12345

Page 24: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 24

2-consistency scheduling examplelabelling T1 with 2

T2

T1

T3 T5

T4

T6

12345

1234512345

12345

12345

12345

12345T2

T1

T3 T5

T4

T612345

12345

12345

12345

12345

T2

T1

T3 T5

T4

T6

12345

1234512345

12345

12345

12345

T2

T1

T3 T5

T4

T6

12345

1234512345

12345

12345

12345

Page 25: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 25

Given

problem variables X1, …, Xi, …, Xj, …, Xn ;

with domains D1, …, Di, …, Dj, …, Dn ;

problem constraints Ci,j(Xi, Xj), for 1i<jn ;

Result

an enumeration of all valuations

{X1=d1, …, Xn=dn}

such that

d1D1, …, dnDn

Ci,j(di, dj) = true, for 1i<jn.

General Backtracking Algorithm

for solving a binary CSP

Page 26: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 26

General Backtracking Algorithm

for solving a binary CSP(idea)Incrementally build valuation

extend {X1=d1, …, Xk=dk} ,

into {X1=d1, …, Xk=dk, Xk+1=dk+1},

by choosing dk+1 Dk+1 , when 0k<n,

so that some adequate criterion is satisfied, and then narrow the remaining domains Dk+2 , …, Dn accordingly.

Output valuation whenit is complete (k=n) [it must be a solution!].

Go back when0<k<n but valuation cannot be extended.

Stop whencannot go back anymore (k=0).

Page 27: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 27

Choice of dk+1: Adequate Criteria• Generate and Test (GT):

– when k<n-1, do not consider constraints;– when k=n-1, restrict choice of dn so that the extended valuation {X1=d1, …, Xn=dn}

satisfies all constraints. most inefficient strategy!

• Standard Backtracking (SB):– restrict choice of dk+1 so that newly extended valuation {X1=d1, …, Xk+1=dk+1}

satisfies constraints so far:

• Ci,j(di, dj) = true, for 1i<jk+1.

• Forward Checking (FC):– in addition to SB restriction, choose dk+1 so that:

• C1,(d1, X)… Ck+1,(dk+1,X) is satisfiable for k+1<n.

• Look Ahead (LA):– in addition to FC restriction, check satisfiability of:

• Ck+2,(Xk+2,X)…C-1,(X-1,X)

C,+1(X,X+1)C,n(X,Xn), for k+1<<n.

Page 28: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 28

4-queen problem

Place 4 queens so that no two queens are in attack.

1234

Q1 Q2 Q3 Q4

Qi: line number of queen in column i, for 1i4

Q1, Q2, Q3, Q4 Q1Q2, Q1Q3, Q1Q4,Q2Q3, Q2Q4,Q3Q4,Q1Q2-1, Q1Q2+1, Q1Q3-2, Q1Q3+2,Q1Q4-3, Q1Q4+3,Q2Q3-1, Q2Q3+1, Q2Q4-2, Q2Q4+2,Q3Q4-1, Q3Q4+1

Page 29: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 29

4-queen problem first solution

1234

Q1 Q2 Q3 Q4

There is a total of 256 valuations

GT algorithm will generate

64 valuations with Q1=1;

+ 48 valuations with Q1=2, 1Q23;

+ 3 valuations with Q1=2, Q2=4, Q3=1;

= 115 valuations to find first solution

Page 30: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 30

4-queen problem, SB algorithm

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

Page 31: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 31

4-queen problem, FC algorithm

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

Page 32: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 32

4-queen problem, LA algorithm

1234

Q1 Q2 Q3 Q4

=2

Q2Q3, Q2Q3-1, Q2Q3+1,Q2Q4, Q2Q4-2, Q2Q4+2

satisfiable in domains

=3

Q2Q3, Q2Q3-1, Q2Q3+1,Q3Q4, Q3Q4-1, Q3Q4+1

unsatisfiable in domains

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

1234

Page 33: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 33

typical structure of a CLP program

CHIP syntax used here

% Declare finite domain problem variables:

• [X, Y, Z]::1..10,

% Set up the constraints:

• 2*X + 3*Y + 2 #< Z,

% Search for a solution (labeling):

• indomain(X), indomain(Y), indomain(Z).

Page 34: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 34

CHIP constraint syntax• Linear rational constraints:

• comparators: ^<, ^<=, ^>, ^>=, ^=, ^\= ;• operators: +, -, *, /

• Finite domain (integer) linear constraints:• comparators: #<, #<=, #>, #>=, #=, #\= ;• operators: +, -, *.

• Arithmetic (passive) constraints (delayed):• comparators: <, <=, >, >=, =:=, =\= ;• operators: +, -, *, /.

• Regular tree constraints (unification):• comparators: =, \=.

• Boolean constraints:• predicates: &=, &\=, and/3, nand/3, or/3, nor/3, xor/3, not/2 ;• operators: #, ! , 0, 1, symbols .

Page 35: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 35

alldifferent(+List)

CHIP constraint• Summary:Holds if all elements of List are different.

• +List should be a list of:• non negative integers C ;• finite domain variables X;• terms of the form X+C

• alldifferent could have been defined:alldifferent([ ]) .

alldifferent([E|L]) :- notmember(E, L),

alldifferent(L).

notmember(E, [ ]) .

notmember(E, [F | L]) :- E #\= F,

notmember(E, L).

Page 36: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 36

element(+Index, +List, -Value)

CHIP constraint• Summary:Holds if Value is the Indexth element of List:

List[Index] = Value

• +Index should be:• a non-negative integer;• or a domain variable.

• -Value should be:• a free variable;• or a domain variable;• or a non-negative integer.

an implementation of “arrays”

CHIP also maintains close interaction between Index and Value domains

an implementation of “arrays”

CHIP also maintains close interaction between Index and Value domains

Page 37: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 37

labeling variableswith CHIPCHIP provides great user control over variable labeling:

what problem variable should be instantiated first?

in what order should a problem variable be bound to its successive domain values?

Builtin CHIP labeling predicates:

indomain/1;

indomain/2;

delete/5;

[labeling/4.]

Good labeling is tightly related to efficiency! Good labeling is tightly related to efficiency!

Page 38: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 38

indomain/2(+X, +Method)

CHIP predicate• Summary:

Instantiates X to a value in its domain using Method method.

• +X should be one of:• a non-negative integer ;• a domain variable.

• +Method should be one:• min;• max;• middle;• a positive integer.

Page 39: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 39

delete/5(-Selected, +List, -Rest, +Arg, +Method)

CHIP predicate• Summary:Selects an element Selected from a list List depending on the Method strategy, yielding Rest as the remainder of the list.

• -Selected, -Rest:• a free variable.

• +List should be one of:• a list on non-negative integers and domain variables (when Arg=0);• a list of compound terms.

• +Arg:• a positive integer.

• +Method should be one of• first_fail; most_constrained;• smallest; largest; max_regret.

Page 40: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 40

labeling example in CHIP

first fail strategy

label([ ]) .

label([E|L]) :- delete(V, [E|L], R, 0, first_fail),indomain(V),label(R).

label([ ]) .

label([E|L]) :- delete(V, [E|L], R, 0, first_fail),indomain(V),label(R).

Page 41: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 41

min_max/2(+Goal, +C)min_max/4(+Goal, +C, +Lower, +Upper)

min_max/6(+Goal, +C, +Lower, +Upper, +Percent, +Timeout)• Summary:

Find solutions with lower and lower cost.

• +Goal:• a CHIP query.

• +C:• a domain variable or list of FD linear terms.

• +Lower, +Upper:• a range for Cost.

• +Percent:• integer: each solution improve by Percent%.

• +Timeout:• integer: time in seconds allowed for search.

Page 42: E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming1 Constraint Logic Programming handout Paul Y Gloess sources of inspiration include: CLP notes.

E.N.S.E.I.R.B. March 2001pyg / Constraint Logic Programming 42

min_max examples

% Minimize the maximum of a list:…length(L, M),L::1..N,alldifferent(L),…min_max(label(L), L),…

% Minimize the sum of a list:…sum_up(L, 0, Sum),Cost :: 0..100000,Cost #= Sum,min_max(label(L), Sum),…