Module on Constraints

51
9 January 2007 Constraints - Finite Domains 1 Module on Constraints The module consists of 4 courses depicted below and of 3 ECTS each. Course: Topics in Finite Domain Constraints Dates: 9 Jan – 25 Jan Lecturer: Pedro Barahona Course: Constraints over Sets and Optimisation Dates: 9 Jan – 25 Jan Lecturer: Francisco Azevedo Course: Constraints on Continuous Domains Dates: 6 Feb – 22 Feb Lecturer: Jorge Cruz Course: Fuzzy Constraints Dates: 6 Feb – 22 Feb Lecturer: João Moura Pires

description

Module on Constraints. The module consists of 4 courses depicted below and of 3 ECTS each. Course: Topics in Finite Domain Constraints Dates: 9 Jan – 25 Jan Lecturer: Pedro Barahona Course: Constraints over Sets and Optimisation Dates: 9 Jan – 25 Jan Lecturer: Francisco Azevedo - PowerPoint PPT Presentation

Transcript of Module on Constraints

Page 1: Module on Constraints

9 January 2007 Constraints - Finite Domains 1

Module on Constraints

­ The­module­consists­of­4­courses­depicted­below­and­of­3­ECTS­each.

Course:­Topics­in­Finite­Domain­Constraints Dates:­9­Jan­–­25­Jan Lecturer:­Pedro­Barahona­

Course:­Constraints­over­Sets­and­Optimisation­ Dates:­9­Jan­–­25­Jan Lecturer:­Francisco­Azevedo­

­Course:­Constraints­on­Continuous­Domains­ Dates:­6­Feb­–­22­Feb Lecturer:­Jorge­Cruz­

Course:­Fuzzy­Constraints­ Dates:­6­Feb­–­22­Feb Lecturer:­João­Moura­Pires­

Page 2: Module on Constraints

9 January 2007 Constraints - Finite Domains 2

Constraints – Finite Domains

Course:­Topics­in­Finite­Domain­Constraints

Implementation­ of­ constraint­ solvers:­ indexical­ constraints.­ Constructive­approach­ for­ combining­ constraints.­ Constructive­ disjunction,­ and­ cardinality­constraints.­ Global­ constraints:­ their­ specification­ and­ implementation.­ ­ ­Redundant­ constraints:­ advantages­ and­ disadvantages­ of­ their­ use­ in­scheduling,­planning­and­other­resource­management­applications.­

Course:­Constraints­over­Sets­and­Optimisation­

Representation­of­sets­and­multisets.­Set­variables­and­set­features­(maximum,­minimum­ and­ cardinality).­ Operations­ on­ sets­ (set­ union,­ intersection,­difference,­disjointness,­complement),­related­constraints­and­their­propagation.­Sets­ of­ sets:­ representation­ with­ union­ functions.­ Set­ constraint­ solvers:­Conjunto­and­Cardinal.­Applications:­Set­covering­and­partitioning,­timetabling,­digital­circuits.­Comparison­with­SAT.­Optimization.­Branch­and­bound­and­local­search.

Page 3: Module on Constraints

9 January 2007 Constraints - Finite Domains 3

Constraints – Finite Domains

Course:­Constraints­on­Continuous­Domains­

Representation­of­continuous­domains­(intervals,­boxes)­and­basic­operations.­Interval­analysis:­interval­arithmetic,­interval­extensions­of­real­functions­and­the­interval­ Newton­ method.­ Constraint­ propagation:­ narrowing­ functions­ and­constraint­projections.­Constraint­decomposition­method­and­Newton­constraint­method.­ Consistency­ criteria­ for­ continuous­ domains:­ local­ consistency­(interval,­hull­and­box)­and­higher­order­consistency­(3B,­bound­and­global­hull).­Introduction­to­differential­equations..

Course:­Fuzzy­Constraints­

Introduction­ to­ fuzzy­ sets­ theory­ and­ to­ Possibility­ Theory.­ Generalization­ of­CSP­ to­ FCSP.­ Fuzzy­ constraints­ and­ their­ dual­ interpretation.­ Min-FCSP.­Constraint­propagation­in­min-FCSP.­Solving­min-FCSP.­Refining­min-FCSP­by­discrimin­and­ leximin-FCSP.­Complexity­ issues.­V(alued)-CSP­and­S(emiring)-CSP­as­general­frameworks.­Special­cases­of­these­general­frameworks­(Min-FCSP,­Sigma-CSP,­Max-CSP­and­Lexicographic­CSP);­their­complexity.

Page 4: Module on Constraints

9 January 2007 Constraints - Finite Domains 4

Constraints – Finite Domains

Evaluation­

Final­Grade:­Average­of­the­4­modules

Type­of­Evaluation:­Typically­take­home­exam

Timetable:­Tuesdays­and­Thursdays,­10:00­–­12:00­

Room:­232-II

Page 5: Module on Constraints

9 January 2007 Constraints - Finite Domains 5

Constraints – Finite Domains

Constraint­Solvers

• Indexical­Constraints

• Bounds,­Node­and­Arc­Consistency

• Reified­Constraints

• Disjuctive­Constraints

• Ask­&­Tell­Constraint­Mechanisms

Page 6: Module on Constraints

9 January 2007 Constraints - Finite Domains 6

Indexical Constraints

­ A­key­ issue­ in­Constraint­(Logic)­Programming­system,­ is­ the­ implementation­

of­ the­ underlying­ Constraint­ Solvers.­ A­ number­ of­ features­ of­ these­ solvers­

must­be­taken­into­account:

The­ease­in­representing­domains­and­generic­constraints;

The­ possibility­ of­ controling­ constraint­ propagation­ in­ the­ ways­

appropriate­to­the­applications;

The­ possibility­ of­ combining­ simpler­ constraints­ into­ more­ complex­

constraints;

The­transparency­of­the­implementation­(glass-box)­allowing­the­users­to­

acceed­ the­ primitive­ constraints­ in­ order­ to­ implement­ user­ specific­

constraints­and­variable­and­value­heuristics.­

Page 7: Module on Constraints

9 January 2007 Constraints - Finite Domains 7

Indexical Constraints

­ An­ adequate­ way­ of­ satisfying­ all­ these­ constraints­ has­ been­ the­ use­ of­

constraint­solvers­based­in­indexical­constraints.

­ This­ is,­ for­ example,­ the­ methodology­ adopted­ in­ the­ implementation­ of­

SICStus­(as­well­as­GNU­Prolog),­specifically­in­their­finite­domains­module.

­ Indexical­constraints­have­a­compact­and­integrated­approach­to­representing­

both­the­domains­and­the­constraints­of­the­problem­variables.­

­ To­ be­ useful,­ indexical­ constraints­ require­ that­ variables­ domains­ have­ an­

ordering.­ In­ fact,­SICStus­ requires­ that­ the­ domains­are­ finite­ subsets­ of­ the­

integers.

­ Since­most­constraints­are­arithmetic,­ constraint­propagation­aims­ in­general­

at­achieving­bounds­arc-consistency.

Page 8: Module on Constraints

9 January 2007 Constraints - Finite Domains 8

Indexical Constraints

Introductory example:

­ Let­ us­ consider­ the­ non-strict­ inequality­ constraint,­,­ applied­ to­ variables­A­

and­B­whose­initial­domains­are­respectively­2000­to­5000­and­1000­to­4000.­

In­SICStus­syntax

A in 2000..5000, B in 1000..4000, A #=< B

­ This­example­will­enable­an­analysis­of­the­advantages­and­disadvantages­of­

the­possible­implementation­methods,­namely­to­issues­such­as­

Maintaining­the­variables­domains;

Representing­the­constraints;

The­adequate­propagation­of­these­constraints

Page 9: Module on Constraints

9 January 2007 Constraints - Finite Domains 9

Indexical Constraints

A in 2000..5000, B in 1000..4000, A #=< B

­ Concerning­the­domains,­a­first­ issue­is­whether­to­adopt­a­representation­by­

intension­or­by­extension.­Notice­that­during­propagation­domains­may­only­be­

reduced,­they­never­increase!

­ The­representation­by­extension­maintains­explicitely,­ in­some­data­structure,­

all­current­values­ in­ the­domains.­As­domains­may­only­decrease,­such­data­

structure­may­be­static,­for­example­a­Boolean­vector­(bit­array).

­ Such­ option­ suffers­ from­ many­ disadvantages.­ For­ example­ detection­ of­

failures­ (i.e.­ empty­ domains)­ requires­ either­ the­ traversal­ of­ the­ whole­ data­

structure­(3000­memory­positions!),­or­the­maintenance­of­“bit­counters”.

Page 10: Module on Constraints

9 January 2007 Constraints - Finite Domains 10

Indexical Constraints

A in 2000..5000, B in 1000..4000, A #=< B

­ Given­ the­ importance­ of­ an­ effective­ detection­ of­ failures,­ and­ the­ memory­

overhead­(domain­values­may­usually­be­represented­in­a­more­compact­form)­

it­ is­ thus­ more­ adequate,­ for­ a­ general­ purpose­ solver,­ to­ adopt­ a­

representation­by­intension.

­ In­ the­beginning,­one­may­always­consider­explicit­ limits­ for­ the­domains,­all­

that­is­required­in­domains­declaration.­

­ Detection­of­ failure­ is­ then­very­simple:­the upper limit of a domain cannot

be less than the lower limit.

­ This­ representation­ is­most­efficient­when­domains­are­kept­convex­ (i.e.­with­

no­holes)­by­maintaining­simple­bounds­consistency.

Page 11: Module on Constraints

9 January 2007 Constraints - Finite Domains 11

Indexical Constraints

A in 2000..5000, B in 1000..4000, A #=< B

­ Most­ usual­ constraints­ maintain­ such­ convexity,­ namely­ inequality­ (and­

equality)­constraints­on­arithmetic­expressions.

­ Constraint­A #=< B­is­satisfiable­as­long­as­the­minimum­value­of­the­domain­

of­A­(in­short,­the­lower­bound­of­A)­is­not­greater­than­the­upper­bound­of­B.

­ On­ the­ other­ hand,­ no­ value­ from­ the­ domain­ of­A­ may­ be­ higher­ then­ the­

upper­bound­of­B,­i.e.­the­upper­bound­of­A­must­be­no­greater­than­the­upper­

bound­of­B.­Similarly,­ the­ lower­bound­of­B­must­be­not­ less­ than­ the­ lower­

bound­of­A).

A #=< BAB1000 5000

AB1000 5000

2000 4000

Page 12: Module on Constraints

9 January 2007 Constraints - Finite Domains 12

Indexical Constraints

A in 2000..5000, B in 1000..4000, A #=< B

­ As­ these­ operations­ are­ quite­ common­ (e.g.­ in­ numerical­ applications),­ it­ is­

necessary­ to­make­ the­ appropriate­ connection­ between­ these­ operations­ on­

the­upper/lower­bounds­and­ the­constraints­ that­enforce­ them­ to­achieve­ the­

adequate­propagation.

­ In­indexical­constraints,­this­link­is­achieved­by­specifying­domains­bounds­as­

a­function­of­other­bounds.

­ Constraints­ are­ thus­ represented­ by­ expressions­ on­ the­ variables­ bounds.­

Since­the­domains­can­only­be­reduced,­such­constraints­are­implemented­by­

representing­ the­ bounds­ of­ variables­ as­ a­ function­ of­ the­ bounds­ of­ other­

variables­and­max/min­operations­to­implement­intersection.

Page 13: Module on Constraints

9 January 2007 Constraints - Finite Domains 13

Indexical Constraints

A in 2000..5000, B in 1000..4000, A #=< B

­ Posting­the­constraint­A #=< B­changes­the­initial­domains­of­variables­A­and­

B

Domain­of­A­­ 2000 .. 5000 inf .. max(B)­

­...­i.e.­ max(A) = min(5000, max(B)) = max(B)

Domain­of­B­ 1000 .. 4000 min(A) .. sup­

...­i.e.­ min(B) = max(1000, min(A)) = min(A)

The­constraint­is­thus­implicitely­maintained­as­

A in 2000 .. 4000 inf ..max(B)­

B in 2000 .. 4000­­ min(A) ..sup­

A #< BAB1000 5000

AB1000 5000

2000 4000

Page 14: Module on Constraints

9 January 2007 Constraints - Finite Domains 14

Indexical Constraints

A in 2000..5000, B in 1000..4000, A #=< B

­ The­implicit­constraint­representation

o A in 2000 .. 4000 inf ..max(B)­

o B in 2000 .. 4000­­ min(A) ..sup

also­ allows­ the­ intended­ propagation.­ Whenever­ the­ upper­ bound­ of­ B­

changes­ (i.e.­decreases!),­such­condition­ is­propagated­ to­variable­A,­whose­

upper­bound­also­decreases­(from­5000­to­4000).­Comparison­with­the­ lower­

bound­of­A­(2000)­detects­when­the­domain­of­A­becomes­empty­!

­ Propagation­ from­A­ to­B­ is­ similar.­ Any­ change­ in­ the­ lower­ bound­ of­A­ is­

propagated­ to­ the­ lower­ bound­ of­B.­ Changes­ in­A’s­ upper­ bound­ and­B’s­

lower­bound­are­not­propagated.

Page 15: Module on Constraints

9 January 2007 Constraints - Finite Domains 15

Indexical Constraints

­ Propagation­may­be­observed­in­the­introduction­of­a­new­variable­C,­

constraining­B,­­

C in 3000..3500, B #= C

0 A in 2000 .. 4000 inf ..max(B)­

B in 2000 .. 4000­­ min(A) ..sup

­ Upon­posting­the­domain­of­C

1 C in 3000 .. 3500

­ Upon­posting­the­constraint­B #= C

2a C in 3000 .. 3500 min(B) .. max(B)­

2b B in 2000 .. 4000 [min(C)..max(C) ­ min(A)..sup)]

2000 .. 4000 max(min(C),min(A)) .. min(max(C),sup)

3000 .. 3500 max(min(C),min(A)) .. max(C)

2c A in 2000 .. 3500 inf ..max(B)­

­ Simplifications­are­made,­ if­possible.­The­upper­bound­of­B­ is­simplified,­but­

not­ its­ lower­ bound­ (the­ greater­ of­ the­ lower­ bounds­ of­ A­ and­ C­ is­ not­

determined­yet).

Page 16: Module on Constraints

9 January 2007 Constraints - Finite Domains 16

Indexical Constraints

­ Of­ course,­ the­ user­ must­ not­ be­ concerned,­ in­ general,­ with­ this­ low­ level­

implementation.

­ The­ most­ usual­ constraints,­ concerning­ the­ usual­ arithmetic­ operations­ and­

relational­operations,­are­directly­compiled­in­indexical­constraints.

­ For­ example,­ constraint­ ­A + B #= C­ is­ compiled­ in­ the­ following­ indexical­

constraints

C in min(A)+min(B) .. max(A)+max(B)

A in min(C)-max(B) .. max(C)-min(B)

B in min(C)-max(A) .. max(C)-min(A)

­ Notice­ the­ monotonic­ nature­ of­ domain­ operations.­ ­ The­ lower­ bound­ of­ A­

increases­with­ the­ increase­of­ the­ lower­bound­of­C­and­ the­decrease­of­ the­

upper­bound­of­B.

Page 17: Module on Constraints

9 January 2007 Constraints - Finite Domains 17

Indexical Constraints

­ The­ previous­ examples­ show­ that­ the­ type­ of­ consistency­ maintained­ by­

indexical­ constraints­ in­ constraints­of­ equality­ (­#= )­ ­ and­ inequality,­ strict­ or­

not,­(­#< , #=< , #> and #>=­)­is,­by­default,­bounds­(or­interval)­consistency.

­ The­ default­ compilation­ of­ the­ constraints­ only­ affects­ the­ bounds­ of­ the­

domains­of­the­variables.

­ Nevertheless,­ there­ is­ the­ possibility­ of­ imposing­ other­ types­ of­ consistency­

adequate­ for­ many­ other­ constraints,­ namely­ node­ consistency­ and­ arc­

consistency.

­ These­types­of­consistency­have­only­interest­when­it­is­important­to­consider­

concave­ domains­ (with­ “holes”),­ as­ for­ example­ in­ the­ queens­ and­ graph­

colouring­problems.

Page 18: Module on Constraints

9 January 2007 Constraints - Finite Domains 18

Indexical Constraints

­ Concavities­ are­ possibly­ introduced­ by­ disequality­ constraints­ (#\=).­ Let­ us­

consider,­for­example,­

A in 1..9, B in 3..5, A #\= B

­ This­ disequality­ constraint­ has­ to­ be­ represented­ through­ set­ complement­

operations,­not­intersection.­Adopting­SICStus­syntax

A in \dom(B) ­and­­­­­­­­B in \dom(A)­

­ This­operation­raises­problems­regarding­monotonicity,­since­when­the­domain­

of­one­variable­decreases­the­domains­of­the­other­would­increase!

• A in 1 .. 9 \dom(B)

• B in 3 .. 5­­ \dom(A)

Page 19: Module on Constraints

9 January 2007 Constraints - Finite Domains 19

Indexical Constraints

A in 1..9, B in 3..5, A #\= B

A in 1 .. 9 \dom(B) ; B in 3 .. 5 \dom(A)

­ For­ this­ reason,­ complement­ operations­ are­ “frozen”­ until­ the­ domains­ to­ be­

complemented­are­reduced­to­singletons.

­ This­actually­implements­node­consistency,­the­most­useful­criterion­to­handle­

disequality­constraints­(in­isolation).

­ When­the­domain­of­B­becomes­a­singleton,­B = 3­for­example,­the­domain­of­

A becomes­concave,­being­represented­by­set­union.­In­­SICStus­syntax

?- A in 1..9, B in 3..5, A #\= B, B #=3, fd_dom(B,S).

B = 3, S = {3}, A in(1..2)\/(4..9) ?

Page 20: Module on Constraints

9 January 2007 Constraints - Finite Domains 20

Indexical Constraints

­ Arc­consistency­requires­ that­whenever­ the­domain­of­a­variable­(not­only­ its­

bounds)­ is­ changed,­ a­ check­ is­ made­ on­ whether­ the­ other­ variables­ still­

maintain­support.

­ (Generalised)­ Arc­ consistency­ is­ thus­ implemented­ by­ specifying­ in­ the­

indexical­constraints­the­domain­of­the­other­variables.­

­ For­example,­in­SICStus­syntax,­if­it­is­required­to­maintain­arc­consistency­on­

the­ previous­ sum­ constraint,­ the­ constraint­ can­ be­ specified­ by­ the­ user­ by­

means­of­the­following­FD­predicate­

sum(A,B,C)+:

A in dom(C) - dom(B),

B in dom(C) - dom(A),

C in dom(A) + dom(B).

Page 21: Module on Constraints

9 January 2007 Constraints - Finite Domains 21

Indexical Constraints

­ User­ defined­ constraints­ in­ SICStus­ (by­ means­ of­ FD­ predicates)­ is­ thus­

similar­ to­ the­ predicate­ definitions­ in­ Prolog,­ with­ a­ different­ “neck”­ operator­

(“+:”­rather­than­the­Prolog­operator­“:-”).­

­ There­ are­ however­ some­ differences,­ namely­ there­ are­ no­ alternative­

“clauses”,­i.e.­no­backtracking­in­constraint­definitions.

­ Using­the­previous­definition­we­would­get,

?- A in 1..6, A#\=3,A#\=4,A#\=5, B in 1..2, sum(A,B,C).

A in(1..2) \/ {6}, B in 1..2, C in(2..4)\/(7..8) ?

% A+B#= C C in 1..8

?- C in 1..9, C#\=5,C#\=6,C#\=7, B in 0..2, sum(A,B,C).

C in(1..4)\/(8..9), B in 0..2, A in(-1..4)\/(6..9) ?

% A+B#= C A in -1..9

Page 22: Module on Constraints

9 January 2007 Constraints - Finite Domains 22

Specifying Indexical Constraints

­ Indexical­ expressions­ may­ only­ appear­ in­ the­ “body”­ of­ FD­ predicate­

definitions.­ Such­ limitation­ is­ due­ to­ the­ different­ execution­ mechanisms­

between­ the­host­ language­ (Prolog)­and­ the­FD­module­ in­SICStus.­ ­ In­ fact,­

given­the­sum­definition

sum(A,B,C)+: A in dom(C) - dom(B), B in dom(C) - dom(A), C in dom(A) + dom(B).

it­is­intended­that,­in­the­FD­module,­the­domains­of­A/B/C­are­re-evaluated­by­

updates­on­the­domains­of­C,B/C,A/A,B.

­ In­ contrast­ with­ Prolog­ execution,­ the­ primitives­ appearing­ in­ indexical­

expressions­(e.g.­dom,­max,­min,­etc)­should­be­regarded­as­reactive agents­

(to­changes).

Page 23: Module on Constraints

9 January 2007 Constraints - Finite Domains 23

Specifying Indexical Constraints - Terms

­ In­addition­to­the­primitives­already­examplified,­indexical­expressions­may­be­

formed­with­FD­terms.­Being­X­a­domain­variable­and­D(X)­its­current­domain,­

the­basic­FD­terms­are

min(X)­ minimum­of­D(X)­

max(X) maximum­of­D(X)­

card(X) cardinality­of­D(X)­

­X value­(integer)­of­X.­­The­expression­is­only­

evaluated­when­X­is­instantiated.

I an­integer

inf minus­infinity

sup­ plus­infinity

Page 24: Module on Constraints

9 January 2007 Constraints - Finite Domains 24

Specifying Indexical Constraints - Terms

­ FD­ terms,­ basic­ or­ not,­ may­ be­ combined­ through­ arithmetic­ operators­ into­

compound­ FD­ terms.­ Denoting­ by­ T1/T2­ arbitrary­ FD­ terms,­ and­ by­

D(T1)/D(T2)­ their­ current­ “domains”,­ the­ following­ ­ compound­ terms­may­ be­

formed­(and­evaluated­by­interval­arithmetic)

-T1­ I-negation­of­D(T1)­

T1+T2 I-sum­of­D(T1)­and­D(T2)­

T1-T2 I-difference­of­D(T1)­and­D(T2)

T1*T2 I-product­of­D(T1)­and­D(T2),­D(T2)­>=­0

T1/>T2­ D(T1)­div­D(T2),­with­outbound­rounding,­

T1/<T2 with­D(T2)­>=­0

T1 mod T2 D(T1)­mod­D(T2)

Page 25: Module on Constraints

9 January 2007 Constraints - Finite Domains 25

Specifying Indexical Constraints - Ranges

­ FD­ terms­ are­ used­ in­ indexical­ constraints­ to­ define­ ranges­ for­ the­ domain­

variables,­that­must­be­intersected­with­the­current­domains­of­these­variables.­

In­general,­indexical­constraints­take­the­form

X in R

where­R­is­a­range­defined­by­the­following­grammar:

­ The­basic ranges­are­the­following

dom(X)­ D(X)­

{T1,...,Tn} set­of­terms­Ti

T1 .. T2­ interval­bound­by­terms­Ti

where­ X­ denotes­ a­ domain­ variable­ and­ Ti­ an­ FD­ term.­ Other­ terms­ may­

subsequently­be­obtained­by­composition.

Page 26: Module on Constraints

9 January 2007 Constraints - Finite Domains 26

Specifying Indexical Constraints - Ranges

­ Denoting­by­Ri­a­range­and­by­D(Ri)­the­corresponding­domain,­the­following­

operators­are­used­to­compose­terms

R1/\R2­ intersection­of­­D(R1)­and­D(R2)­

R1\/R2­ union­of­D(R1)­e­D(R2)

\R1 complement­of­D(R1)­

R1+R2 (T2) I-sum­of­D(R1)­and­D(R2)­(or­D(T2))­

-R1 I-negation­of­D(R1)

R1-R2 e R1-T2­ I-difference­of­D(R1)­and­D(R2)­(D(T2))

R1 mod R2 (T2)­ I-mod­­of­D(R1)­and­D(R2)­(D(T2))­

R1 ? R2­ if­R1­\=­­then­D(R2)­else­

unionof(X,R1,R2) union­of­D(Sk):­each­Sk­ is­obtained­ from­R2­

replacing­X­for­the­k-th­element­of­R1.

Page 27: Module on Constraints

9 January 2007 Constraints - Finite Domains 27

Indexical Constraints – Example (1)

Example 1:­­­adjust(X,Z,K,N)

­ For­variables­X­and­Z,­with­domain­1­to­N,­make­X­to­be­shifted­from­Z­by­a­

value­K­(0=<­K­<­N),­with­rewrapping.

­ This­ kind­ of­ adjust­ enables­ that,­ while­ enumerating­ Z­ in­ the­ “standard”­

increasing­order,­X­is­enumerated­with­the­“shifted”­ordering.

­ For­example,­let­us­assume­N­=­15­and­K­=­5,­i.e.­that­

while­Z­is­enumerated­with­the­standard­increasing­order

X­is­enumerated­with­the­order­shifted­by­5

X 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5

Z 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 28: Module on Constraints

9 January 2007 Constraints - Finite Domains 28

Indexical Constraints – Example (1)

Example 1:­­­adjust(X,Z,K,N)

­ Notice­that­X=((Z+K+N-1)mod­N)+1­and­Z=((X-K+N-1)mod­N)+1.­For­example,­

for­Z­=­7­­X­=­12­as­intended

X = (7+5+15-1) mod 15 +1 = 26 mod 15 +1 = 11 +1 = 12

Z = (12-5+15-1) mod 15 + 1= 21 mod 15 + 1= 6 +1 = 7

­ Hence,­ we­ may­ formulate­ the­ correspondence­ between­ X­ and­ Z­ with­ the­

following­specification

Solution 1:­­­­­­adjust(X,Z,K,N)+:

X in ((dom(Z)+K+N-1) mod N)+1,

Z in ((dom(X)-K+N-1) mod N)+1.

X 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5

Z 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 29: Module on Constraints

9 January 2007 Constraints - Finite Domains 29

Indexical Constraints – Example (1)

Example 1:­­­adjust(X,Z,K,N)

­ In­fact,­the­mod­operation­is­unnecessary,­if­X­and­Z­are­defined­over­1..N.­All­

that­is­required­is,­for­each­value­Z­to­add­K.­Since­this­may­result­above­the­

upper­bound­of­X,­sometimes­N­must­be­subtracted.­

­ For­example:­­

­ for­Z­=­7­ X1­=­­7­+­5­=­12­­­and­­X2­=­­7­+­5­-­15­=-3

­­­­ for­Z­=­11­ X1­=­11­+­5­=­16­­and­­­X2­=­11­+­5­-­15­=­­1

X 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5

Z 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 30: Module on Constraints

9 January 2007 Constraints - Finite Domains 30

Indexical Constraints – Example (1)

­ By­using­the­union­of­the­two­intervals,­one­resulting­from­simply­adding­K­and­

the­other­by­adding­K­and­subtracting­N,­the­adequate­value­of­X­is­thus­

obtained.­This­leads­to­the­simpler­formulation.

Solution 2:­­adjust(X,Z,K,N)+:

X in (dom(Z)+ K) \/ (dom(Z)+ K - N),

Z in (dom(X)- K) \/ (dom(X)- K + N).

Left as Exercise:­Enumerate­Y­from­centre­to­the­“edges”­as­below.

Y 15 13 11 9 7 5 3 1 2 4 6 8 10 12 14

Z 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 31: Module on Constraints

9 January 2007 Constraints - Finite Domains 31

Indexical Constraints – Example (2)

Example 2:­­­no_attack(L1,C1,L2,C2)

­ Two­ queens,­ placed­ in­ rows­ L1­ and­ L2­ (constants)­ and­ columns­ C1­ e­ C2­

(domain­variables)­should­not­attack­each­other,­i.e.­

C1 \= C2 and L1+C1 \= L2+C2 and L1+C1 \= L2+C2.

Solution 1:­­no_attack(L1,C1,L2,C2)+:

C1 in \({C2} \/ {C2+L2-L1} \/ {C2+L1-L2}),

C2 in \({C1} \/ {C1+L1-L2} \/ {C1+L2-L1}).

­ In­such­approach,­as­ the­domain­variables­appear­ in­a­ ­negative­ “context”­ (\

{C})­ the­ constraint­ is­ frozen­ until­ C­ is­ instantiated.­ Hence,­ this­ formulation­

guarantees­the­maintenance­of­node consistency.­

­ In­alternative,­one­may­specify­arc consistency.

Page 32: Module on Constraints

9 January 2007 Constraints - Finite Domains 32

Indexical Constraints – Example (2)

Example 2:­­­no_attack(L1,C1,L2,C2)

Solution 2:­no_attack(L1,C1,L2,C2)+:

C1 in unionof(Y, dom(C2), \({Y} \/ {Y+L2-L1} \/ {Y+L1-L2})),

C2 in unionof(X, dom(C1), \({X} \/ {X+L1-L2} \/ {X+L2-L1})).

­ Here,­we­get­arc­consistency­by­allowing­a­queen­to­be­in­a­position­that­is­not­

under­ the­ attack­ of­ at­ least­ one­ possible­ position­ of­ the­ other­ queen,­ which­

supports­the­former.

­ For­example,­the­domain­of­C1­is­obtained­by­the­union­of­all­values­that­are­

“safe”­ for­ some­ value­ in­ the­ domain­ of­C2,­ through­ a­ range­ expression­ that­

takes­in­turn­all­values­of­the­domain­of­C2,­renaming­them­as­Y

­C1 in unionof(Y,dom(C2),...).

Page 33: Module on Constraints

9 January 2007 Constraints - Finite Domains 33

Indexical Constraints – Example (2)

Example 2:­­­no_attack(L1,C1,L2,C2)

­ The­ third­ approach­ below­ optimises­ arc­ consistency­ in­ this­ problem.­ Taking­

into­account­that­a­position­in­a­queen­is­always­supported­by­a­queen­with­4­

or­ more­ positions­ available,­ arc­ consistency­ is­ only­ evaluated­ when­ the­

cardinality­of­the­domain­drops­below­4.

Solution 3:­ no_attack(L1,C1,L2,C2)+:

C1 in ( 4..card(C2)) ? (inf..sup) \/

unionof(Y,dom(C2),\({Y} \/ {Y+L2-L1} \/ {Y+L1-

L2})),

C2 in (4..card(C1)) ? (inf..sup) \/

unionof(X,dom(C1),\({X} \/ {X+L1-L2} \/ {X+L2-L1})).

­ In­ fact,­ C1­ is­ constrained­ to­ be­ either­ inf..sup­ (when­ the­ cardinality­ of­ C2­

(card(C2)­is­>=4)­or­unionof(Y,... ), otherwise.

Page 34: Module on Constraints

9 January 2007 Constraints - Finite Domains 34

Disjunctive Constraints

­ In­ general,­ the­ execution­ of­ a­ CLP­ problem­ includes­ a­ search­ phase­ with­

backtracking­ on­ the­ enumeration­ of­ the­ variables­ when­ the­ constraints­ and­

their­ propagation­ is­ not­ sufficient­ to­ eliminate­ redundant­ values­ from­ the­

variables­domains.

­ This­ backtracking­ occurs­ in­ the­ enumeration­ phase,­when­all­ the­ constraints­

are­already­posted,­i.e.­There­is­no­backtracking­on­constraint­posting.

­ However,­when­constraints­are­specified­by­intension,­the­most­natural­way­to­

combine­them­is­through­the­disjunction­of­the­different­alternatives.

­ One­must­then­identify­the­best­way­to­specify­disjunction.

Page 35: Module on Constraints

9 January 2007 Constraints - Finite Domains 35

Disjunctive Constraints

­ Example:­

Given­two­tasks,­T1­and­T2,­with­starting­times­S1­and­S2­and­duration­D1­and­

D2,­garantee­that­they­do­not­overlap.

­ A­natural­implemention­of­this­constraint­is­through­the­equivalent­disjunction

T2­does­not­start­before­the­end­of­T1;­­­­or

T1­does­not­start­before­the­end­of­T2

­ The­specification­of­ this­constraint,­namely­ the­disjunction­of­ the­alternatives,­

may­ use­ the­ usual­ disjunction­ of­ Prolog­ clauses­ (and­ the­ associated­

backtracking)

disjoint(S1,S2,D1,D2) :- S1 + D1 #=< S2.

disjoint(S1,S2,D1,D2) :- S2 + D2 #=< S1.

Page 36: Module on Constraints

9 January 2007 Constraints - Finite Domains 36

Disjunctive Constraints

­ Problem(Vars):-

Declaration of Variables and Domains,

Specification of Constraints,

Labelling of the Variables.

­ This­ formulation­ raises­ the­ problem­ of­ interaction­ between­ backtrack­

mechanisms­made­either

On­the­phase­of­specification­and­posting­of­constraints;­and

On­the­enumeration­phase­of­the­variables.­

­ With­k­ non-overlaping­ tasks,­ there­are­k*(k-1)/2­ combinations­of­precedence­

between­ pairs­ of­ tasks.­With­50­ tasks,­k = 50,­ there­will­ be­50*49/2 = 1225­

constraints­and­21225­ways­of­combining­them­(in­fact­only­50! are distinct).

Page 37: Module on Constraints

9 January 2007 Constraints - Finite Domains 37

Disjunctive Constraints

­

­­­­­­­Problem(Vars):-

Declaration of Variables and Domains,

Specification of Constraints,

Labelling of the Variables.

­ Hence­the­two­alternatives:

Either­1­single­problem­with­k­variables,­with­complexity­O(dk)­;­or

a­model­with­alternative­“problems”­on­ the­same­variables­ leading­ to­k!­

problems­on­these­variables,­with­complexity­O(k! dk).

­ Although­each­of­ the­k!­problems­ in­ the­second­model­are­more­constrained­

than­ the­ single­ one­ of­ the­ first­ model,­ the­ huge­ number­ of­ problems­

corresponding­ to­ an­ ordering­ of­ the­ task­ makes­ this­ formulation­ very­

inneficient.

Page 38: Module on Constraints

9 January 2007 Constraints - Finite Domains 38

Reified Constraints

­ A­better­formulation­uses­a­single­specification­of­all­the­constraint­alternatives,­

avoiding­the­backtracking­of­multiple­constraint­definitions.

­ Such­formulation­can­be­obtained­through­constraints reification.

­ The­ basic­ idea­ of­ constraint­ reification­ is­ to­ associate­ its­ satisfaction­ to­ a­

boolean­variable­0/1,­and­make­this­variable­accessible­to­the­program­level.

­ For­example­associating­constraints­C1­and­C2­to­boolean­variables­­B1­and­B2,­

the­disjunction­of­these­constraints­may­be­specified­by­means­of­constraint

B1 + B2 #>= 1

Page 39: Module on Constraints

9 January 2007 Constraints - Finite Domains 39

Reified Constraints

­ Once­defined­this­basic­meta-level­mechanism­of­reified­constraints,­its­scope­

can­be­enlarged­ for­other­ types­of­combinations­of­constraints,­namely­ those­

requiring­some­countings.

­ For­example,­if­from­the­20­constraints­C1,..., C20­at­least­10­must­be­satisfied,­

instead­of­specifying,­

C1020­=­184 756­alternative­combinations­of­10­out­of­20­of­the­C1­to­C20­

constraints

the­only­additional­specifications­required­are

the­20­constraints,­C1­to­C20

the­constraints­reification­to­Boolean­variables­B1­to­B20

the­“counting”­constraint­B1 + B2 + ... + B20 #>= 10

Page 40: Module on Constraints

9 January 2007 Constraints - Finite Domains 40

Reified Constraints - Ask & Tell Mechanisms

­ To­ efficiently­ exploit­ the­ correspondance­ between­ a­ constraint­ C­ and­ a­

Boolean­variable­B,­Ask & Tell­mechanisms­must­be­defined:

­ Tell­mechanisms

Tell(C)­­­-­Post­the­constraint

Tell(~C)­-­Post­the­“negation”­of­the­constraint

­ Ask­mechanisms

Ask(C)­­­-­Check­the­entailment­of­the­constraint

Ask(~C)­-­Check­the­entailment­of­its­negation.

­ For­ example,­ to­ impose­ that­ one­ and­ only­ one­ of­ constraints­C1­ and­C2­ is­

satisfied­all­the­4­above­situations­must­be­implemented.

Page 41: Module on Constraints

9 January 2007 Constraints - Finite Domains 41

Reified Constraints - Ask & Tell Mechanisms

­ Ask(C1) / Ask(C2)­succeeds

Once­ detected­ the­ satisfaction­ of­ one­ of­ the­ constraints,­ the­ negation­ of­ the­

other­constraint­must­be­posted,­i.e.­tell(~R2) / tell(~R1).

­ Ask(~C1) / Ask(~C2)­succeeds

Once­ detected­ the­ satisfaction­ of­ the­ negation­ of­ one­ of­ the­ constraints,­ the­

other­constraint­must­be­posted,­i.e.­tell(C2) / tell(C1).

­ It­is­thus­important­to­see­how­reified­constraints,­and­the­underlying­Ask&Tell­

mechanisms­may­be­specified,­not­only­ in­built-in­predefined­constraints,­but­

also­for­user­defined­constraints.

Page 42: Module on Constraints

9 January 2007 Constraints - Finite Domains 42

Specification of Ask & Tell Mechanisms

­ In­ SICStus­ the­ association­ between­ constraints­ and­ boolean­ variables­ is­

specified­through­the­built-in­operator­“#<=>”

C #<=> B.

where­B­is­the­boolean­variable­and­C­the­constraint.

­ Some­built-in­constraints­are­pre-defined.­Such­is­the­case­of­linear­numerical­

constraints.­As­an­application,­tasks­non-overlapping­as­described­before­can­

be­specified­with­no­alternative­clauses­as

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

(S1 + D1 #=< S2) #<=> B1, % T1 before T2

(S2 + D2 #=< S1) #<=> B2, % T2 before T1

B1 + B2 #>= 1.

Page 43: Module on Constraints

9 January 2007 Constraints - Finite Domains 43

Specification of Ask & Tell Mechanisms

­ For­constraints­defined­by­the­user,­the­appropriate­Ask & Tell­conditions­must­

also­be­defined.

­ For­ instance,­ let­us­consider­the­constraint­of­difference­between­two­domain­

variables.

­ The­two­Tell­mechanisms­are­imposed­by­propagating­indexical­specifications,­

that­ include­both­ the­positive­definitions­ (‘+:’),­ already­ known,­as­well­ as­ the­

negative­definitions­(‘-:’)

’x\\=y’(X,Y) +: ’x\\=y’(X,Y) -:

X in \{Y}, X in dom(Y),

Y in \{X}. Y in dom(X).

Page 44: Module on Constraints

9 January 2007 Constraints - Finite Domains 44

Specification of Ask & Tell Mechanisms

­ The­Ask­mechanisms­are­imposed­by­checking indexicals,­both­positive­(‘+?’)

’x\\=y’(X,Y)+?

X in \dom(Y).

and­negative­(‘-?’)

’x\\=y’(X,Y) -?

X in {Y}.

­ The­ intended­ behaviour­ to­ propagating­ and­ checking­ indexicals,­ imposes­

some­ constraints­ on­ the­ expressions­ that­ can­ be­ used­ on­ the­ “body”­ of­ the­

definitions,­as­well­as­to­their­execution.­

­ Some­of­these­limitations­are­presented­next.

Page 45: Module on Constraints

9 January 2007 Constraints - Finite Domains 45

Ask & Tell Propagation

Propagating Indexicals (Trigger)

A­Propagating­Indexical­X in R­is­scheduled­for­execution­whenever

It­becomes­monotonic­for­the­first­time­

It­is­rescheduled­whenever,­for­another­variable­Y­appearing­in­R

o The­domain­of­Y­is­changed­and­Y­appears­in­R­in­the­form­card(Y)­

ou­dom(Y);

o The­ upper/lower­ bounds­ of­Y­ change­ and­Y­ appears­ in­R­ in­ the­

form­max(Y)/min(Y).

Page 46: Module on Constraints

9 January 2007 Constraints - Finite Domains 46

Ask & Tell Propagation

Propagating Indexicals (Result)

­ Given­some­variable­X­ referred­to­ in­an­ indexical­constraint,­and­denoting­by­

M(X)­the­value­of­the­constraint­in­the­current­state­of­memory­and­by­I(X)­the­

interval­between­the­lower­and­upper­bounds­of­X,­then

If­M(X)­is­disjoint­from­I(X),­there­is­a­contradiction.

If­ I(X)­ is­ within­ M(X),­ there­ are­ no­ values­ in­ the­ current­ domain­ of X­

incompatible­ with­ the­ indexical­ constraints,­ whose­ execution­ is­

suspended,­until­the­situation­becomes­“ground”.

Otherwise,­ the­ indexical­ constraint­ is­ added­ to­ the­ constraints­ over­X,­

whose­domain­will­be­pruned.

Page 47: Module on Constraints

9 January 2007 Constraints - Finite Domains 47

Ask & Tell Propagation

Propagating Indexicals: Examples

­ Example 1:­ Constraint­ X in \{Y} in­ the­ positive­ propagating­ indexical­ is­

suspended­until­it­gets­monotonic,­i.e.­until­Y­gets­instantiated.

In­ this­ case,­ the­ constraint­ is­ either­ contradictory,­ or­ is­ propagated,­

pruning­the­value­of­Y­from­the­domain­of­X.

­ Example 2:­Constraint­X in dom(Y),­ in­ the­negative­propagating­ indexical­ is­

inherently­monotonic.­

Whilst­Y­has­values­in­its­domain,­X is­getting­prunned.­

If­ the­ domains­ of X­ and­ Y­ become­ disjunct,­ then­ the­ negation­ of­ the­

constraint­of­difference­fails!­

When­ the­domain­of­Y­gets­ground,­ then­so­does­ the­domain­of­X­and­

the­constraint­succeeds.

Page 48: Module on Constraints

9 January 2007 Constraints - Finite Domains 48

Ask & Tell Propagation

Checking Indexicals (Trigger)

­ A­ checking­ indexical­ X in R­ is­ triggered­ for­ execution­ in­ the­ following­

conditions

It­becomes­anti-monotonic­for­the­first­time

It­is­rescheduled­whenever,­

o The­domain­of­X has­been­pruned­or­got­ground;

o The­ domain­ of­ another­ variable­ Y,­ appearing­ in­ R­ in­ the­ form­

card(Y)­or­dom(Y),­is­pruned;

o The­upper/lower­ bounds­of­ another­ variable­Y,­ ­ appearing­ in­R in­

the­form­max(Y)/min(Y),­is­pruned.

Page 49: Module on Constraints

9 January 2007 Constraints - Finite Domains 49

Ask & Tell Propagation

Checking Indexicals (Result)

­ Given­some­variable­X­referrred­to­in­an­indexical­constraint,­and­denoting­by­

M(X)­the­value­of­the­constraint­in­the­current­state­of­memory­and­by­I(X)­the­

interval­between­the­lower­and­upper­bounds­of­X,­then

If­ I(X)­ is­ contained­ in­M(X),­ then­no­ value­of­X­may­ turn­ the­ constraint­

false,­ as­ M(X)­ may­ only­ “increase­ size”­ (anti-monotonic).­ Hence­ the­

constraint­is­entailed.

If­ I(X)­ is­disjoint­ from­M(X)­and­M(X)­ is­ground­ then­the­constraint­may­

no­longer­be­satisfied,­and­is­­disentailed.

Otherwise,­the­checking­indexical­is­suspended.

Page 50: Module on Constraints

9 January 2007 Constraints - Finite Domains 50

Ask & Tell Propagation

Checking Indexicals: Examples

­ Example 1:­ Constraint­ X in \dom(Y) in­ the­ positive­ checking­ indexical­ is­

inherently­anti-monotonic­(the­values­of­\dom(Y)may­only­increase­as­dom(Y)­

decreases­during­execution).­

Hence,­the­positive­checking­indexical­succeeds­as­soon­as­the­domain­

of­X­has­no­elements­in­common­with­the­domain­of­Y.

­ Example 2:­Constraint­X in {Y},­ in­ the­negative­checking­ indexical­only­gets­

anti-monotonic­when­Y­becomes­ground­to­some­value.­

Whilst­X­has­this­value­in­its­domain­the­constraint­suspends.­

If­X­only­has­this­value­in­its­domain­the­negative­checking­succeeds,­i.e.­

the­constraint­of­difference­fails.

Page 51: Module on Constraints

9 January 2007 Constraints - Finite Domains 51

Ask & Tell - Example

Example: Strict Inequality Constraints

­ As­can­be­checked­easily,­the­following­definitions­of­propagating­and­

checking­indexicals­are­adequate­to­reify­a­constraint­of­strict­inequality­of­the­

form­

’x\\<y’(X,Y) #<=> B

’x\\<y’(X,Y)+: X in inf .. max(Y)-1, Y in min(X)+1 .. sup.

’x\\<y’(X,Y)-: % x>=y

X in min(Y) .. sup,

Y in inf .. max(X).

’x\\<y’(X,Y)+? X in inf .. min(Y)-1, Y in max(X)+1 .. sup.

’x\\<y’(X,Y)-? X in max(Y).. sup,

Y in inf .. min(X).