Constraint Programming Marco Kuhlmann & Guido Tack Lecture 2 - - PowerPoint PPT Presentation

constraint programming
SMART_READER_LITE
LIVE PREVIEW

Constraint Programming Marco Kuhlmann & Guido Tack Lecture 2 - - PowerPoint PPT Presentation

Constraint Programming Marco Kuhlmann & Guido Tack Lecture 2 Today: History and Practice History and Practice Part I: Short historical overview where does CP come from? Part II: Constraint Programming with Gecode/J give you


slide-1
SLIDE 1

Constraint Programming

Marco Kuhlmann & Guido Tack Lecture 2

slide-2
SLIDE 2
slide-3
SLIDE 3

Today: History and Practice

slide-4
SLIDE 4

History and Practice

  • Part I: Short historical overview
  • where does CP come from?
  • Part II: Constraint Programming with Gecode/J
  • give you intuition about what's under the hood
  • scratch all topics we will discuss in this course
slide-5
SLIDE 5

Historical notes

1963 Sketchpad 1978 Alice

please ignore the scale...

1980 Chip 1986 CLP(R) constraint logic programming early research

slide-6
SLIDE 6

Historical notes

2005 Gecode Comet 1991 Mozart/Oz 1990 ILOG 1990 SICStus 1993 ECLiPSe

slide-7
SLIDE 7

Historical notes

2005 Gecode Comet 1991 Mozart/Oz 1990 ILOG 1990 SICStus 1993 ECLiPSe

?

slide-8
SLIDE 8

Constraint Logic Programming

foo(a). foo(b). g(X) :- X=[Y, Z], foo(Y), foo(Z).

slide-9
SLIDE 9

Constraint Logic Programming

foo(a). foo(b). g(X) :- X=[Y, Z], foo(Y), foo(Z). | ?- g(X). X = [a,a] ? ; X = [a,b] ? ; X = [b,a] ? ; X = [b,b]

slide-10
SLIDE 10

Constraint Logic Programming

foo(X) :- fd_domain(X, 1, 3). g(Y,Z) :- foo(Y), foo(Z), Y #< Z, fd_labeling([Y,Z]).

slide-11
SLIDE 11

Constraint Logic Programming

foo(X) :- fd_domain(X, 1, 3). g(Y,Z) :- foo(Y), foo(Z), Y #< Z, fd_labeling([Y,Z]). | ?- g(X,Y). X = 1 Y = 2 ? ; X = 1 Y = 3 ? ; X = 2 Y = 3

slide-12
SLIDE 12

Constraint Logic Programming

foo(X) :- fd_domain(X, 1, 3). g(Y,Z) :- foo(Y), foo(Z), Y #< Z, fd_labeling([Y,Z]). | ?- g(X,Y). X = 1 Y = 2 ? ; X = 1 Y = 3 ? ; X = 2 Y = 3

Model: constraint program = logic program = logical formula

slide-13
SLIDE 13

Constraint Logic Programming

foo(X) :- fd_domain(X, 1, 3). g(Y,Z) :- foo(Y), foo(Z), Y #< Z, fd_labeling([Y,Z]). | ?- g(X,Y). X = 1 Y = 2 ? ; X = 1 Y = 3 ? ; X = 2 Y = 3

Languages/Systems: GNU Prolog, BProlog, SICStus Prolog, ECLiPSe

slide-14
SLIDE 14

Concurrent Constraint Programming

slide-15
SLIDE 15

Concurrent Constraint Programming

  • Von-Neumann architecture: store values
  • operations: read and write
slide-16
SLIDE 16

Concurrent Constraint Programming

  • Von-Neumann architecture: store values
  • operations: read and write
  • cc architecture: store constraints
  • operations: ask and tell
  • communication through variables
slide-17
SLIDE 17

Concurrent Constraint Programming

  • Von-Neumann architecture: store values
  • operations: read and write
  • cc architecture: store constraints
  • operations: ask and tell
  • communication through variables
  • Languages/systems:
  • cc(FD), AKL, Mozart/Oz
slide-18
SLIDE 18

Concurrent Constraint Programming

  • Von-Neumann architecture: store values
  • operations: read and write
  • cc architecture: store constraints
  • operations: ask and tell
  • communication through variables
  • Languages/systems:
  • cc(FD), AKL, Mozart/Oz

x {3,4,5} y {3,4,5} x y y > 3

slide-19
SLIDE 19

Constraint Programming with Gecode/J

  • Quick reminder of last lecture
  • Walk-through for Send More Money
  • Some modeling techniques
  • Presentation of first graded lab
slide-20
SLIDE 20

x {3,4,5} y {3,4,5} x y y > 3

Computation Space

constraint store with connected propagators

slide-21
SLIDE 21

Search

slide-22
SLIDE 22

Search

choice

slide-23
SLIDE 23

Search

x=0 x≠0

branching

slide-24
SLIDE 24

Search

failure

x=0 x≠0

slide-25
SLIDE 25

Search

solution

x=0 x≠0

slide-26
SLIDE 26

Send More Money

  • variables: S,E,N,D,M,O,R,Y ∈ {0,...,9}
  • constraints: S ≠ 0, M ≠ 0

distinct(S,E,N,D,M,O,R,Y) 1000×S + 100×E + 10×N + D + 1000×M + 100×O + 10×R + E = 10000×M + 1000×O + 100×N + 10×E + Y

slide-27
SLIDE 27

Modelling in Gecode/J

  • Implement model as a script
  • declare variables
  • post constraints (create propagators)
  • define branching
  • Solve script
  • basic search strategy (DFS)
  • interactive, graphical search tool (Gist)
slide-28
SLIDE 28

Script: Overview

  • Inherit from class Space
  • Constructor
  • initialize variables
  • post propagators
  • define branching
  • Copy constructor
  • copy a space
  • Main function
  • invoke search engine
slide-29
SLIDE 29

Script: Structure

import static org.gecode.Gecode.*; import static org.gecode.GecodeEnumConstants.*; import org.gecode.*; public class Money extends Space { public VarArray<IntVar> letters; public Money() {...} public Money(Boolean share, Money money) {...} public static void main(String[] args) {...} }

slide-30
SLIDE 30

Script: Structure

import static org.gecode.Gecode.*; import static org.gecode.GecodeEnumConstants.*; import org.gecode.*; public class Money extends Space { public VarArray<IntVar> letters; public Money() {...} public Money(Boolean share, Money money) {...} public static void main(String[] args) {...} }

import all we need

slide-31
SLIDE 31

Script: Structure

import static org.gecode.Gecode.*; import static org.gecode.GecodeEnumConstants.*; import org.gecode.*; public class Money extends Space { public VarArray<IntVar> letters; public Money() {...} public Money(Boolean share, Money money) {...} public static void main(String[] args) {...} }

problem variables

slide-32
SLIDE 32

Script: Structure

import static org.gecode.Gecode.*; import static org.gecode.GecodeEnumConstants.*; import org.gecode.*; public class Money extends Space { public VarArray<IntVar> letters; public Money() {...} public Money(Boolean share, Money money) {...} public static void main(String[] args) {...} }

constructor

slide-33
SLIDE 33

Script: Structure

import static org.gecode.Gecode.*; import static org.gecode.GecodeEnumConstants.*; import org.gecode.*; public class Money extends Space { public VarArray<IntVar> letters; public Money() {...} public Money(Boolean share, Money money) {...} public static void main(String[] args) {...} }

copy constructor

slide-34
SLIDE 34

Script: Structure

import static org.gecode.Gecode.*; import static org.gecode.GecodeEnumConstants.*; import org.gecode.*; public class Money extends Space { public VarArray<IntVar> letters; public Money() {...} public Money(Boolean share, Money money) {...} public static void main(String[] args) {...} }

slide-35
SLIDE 35

Script: Constructor

public Money() { // Call superclass constructor super(); // Initialize variables letters = new VarArray<IntVar>(this, 8, IntVar.class, 0, 9); ... }

slide-36
SLIDE 36

Script: Constructor

... // Refer to the letters by name IntVar s = letters.get(0); IntVar e = letters.get(1); IntVar n = letters.get(2); IntVar d = letters.get(3); IntVar m = letters.get(4); IntVar o = letters.get(5); IntVar r = letters.get(6); IntVar y = letters.get(7); ...

slide-37
SLIDE 37

Script: Constructor

... // Initial letters non-zero rel(this, s, IRT_NQ, 0); rel(this, m, IRT_NQ, 0); // IRT: Integer relation type ...

slide-38
SLIDE 38

Posting Constraints

  • Defined in the class org.gecode.Gecode
  • accessed with import static
  • Check the documentation
  • All constraints take a space as first argument
  • where is the constraint to be installed?
  • Scripts are subclasses of Space!
slide-39
SLIDE 39

Linear equation constraints

  • Propagator for equations of the form
  • Specified as arrays

int[] a VarArray<IntVar> x

  • Supported relations:

IRT_EQ, IRT_NQ, IRT_LE, IRT_GR, IRT_LQ, IRT_GQ

n

  • i=1

aixi = d

slide-40
SLIDE 40

Script: Constructor

... // Post linear equation int a[]={ 1000, 100, 10, 1, 1000, 100, 10, 1,

  • 10000, -1000, -100, -10, -1};

VarArray<IntVar> x = new VarArray<IntVar>( s, e, n, d, m, o, r, e, m, o, n, e, y); linear(this, a, x, IRT_EQ, 0); ...

slide-41
SLIDE 41

Script: Constructor

... // Letters take distinct values distinct(this, letters); // Find values using first-fail branch(this, letters, BVAR_SIZE_MIN, BVAL_MIN); ...

slide-42
SLIDE 42

Branching

  • Choose variable
  • smallest domain size: BVAR_SIZE_MIN
  • smallest minimum: BVAR_MIN_MIN
  • given order: BVAR_NONE
  • Choose value
  • try smallest value: BVAL_MIN
  • split (lower first) BVAL_SPLIT_MIN
slide-43
SLIDE 43

Script: Copying

public Money(Boolean share, Money m) { super(share, m); letters = new VarArray<IntVar>(this, share, m.letters); }

slide-44
SLIDE 44

Script: Copying

public Money(Boolean share, Money m) { super(share, m); letters = new VarArray<IntVar>(this, share, m.letters); }

copy all variables you need for output!

slide-45
SLIDE 45

Script: Copying

public Money(Boolean share, Money m) { super(share, m); letters = new VarArray<IntVar>(this, share, m.letters); }

slide-46
SLIDE 46

Script: Copying

public Money(Boolean share, Money m) { super(share, m); letters = new VarArray<IntVar>(this, share, m.letters); }

copying of single variables also possible!

slide-47
SLIDE 47

Why copy? Search!

slide-48
SLIDE 48

Why copy? Search!

slide-49
SLIDE 49

Why copy? Search!

slide-50
SLIDE 50

Why copy? Search!

slide-51
SLIDE 51

Why copy? Search!

slide-52
SLIDE 52

Why copy? Search!

slide-53
SLIDE 53

Why copy? Search!

slide-54
SLIDE 54

Why copy? Search!

slide-55
SLIDE 55

Why copy? Search!

slide-56
SLIDE 56

Why copy? Search!

slide-57
SLIDE 57

Script

import static org.gecode.Gecode.*; import static org.gecode.GecodeEnumConstants.*; import org.gecode.*; public class Money extends Space { public VarArray<IntVar> letters; public Money() {...} public Money(Boolean share, Money money) {...} public static void main(String[] args) {...} }

slide-58
SLIDE 58

Solving

  • Hidden in Options class, but...
  • Search engines:
  • DFSSearch (depth first search)
  • BABSearch (branch-and-bound search)
  • Interactive search tool
  • Gist
slide-59
SLIDE 59

First Solution Search

public static void main(String[] args) { Money m = new Money(); DFSSearch s = new DFSSearch(m); Money sol = (Money) s.next(); if (sol != null) { System.out.println(sol.toString()); } }

slide-60
SLIDE 60

First Solution Search

public static void main(String[] args) { Money m = new Money(); DFSSearch s = new DFSSearch(m); Money sol = (Money) s.next(); if (sol != null) { System.out.println(sol.toString()); } }

create root space

slide-61
SLIDE 61

First Solution Search

public static void main(String[] args) { Money m = new Money(); DFSSearch s = new DFSSearch(m); Money sol = (Money) s.next(); if (sol != null) { System.out.println(sol.toString()); } }

create search engine

slide-62
SLIDE 62

First Solution Search

public static void main(String[] args) { Money m = new Money(); DFSSearch s = new DFSSearch(m); Money sol = (Money) s.next(); if (sol != null) { System.out.println(sol.toString()); } }

search

slide-63
SLIDE 63

All Solution Search

public static void main(String[] args) { Money m = new Money(); DFSSearch s = new DFSSearch(m); Money sol = (Money) s.next(); while (sol != null) { System.out.println(sol.toString()); sol = (Money) s.next(); } }

slide-64
SLIDE 64

Gecode/J Gist

  • Graphical search tree explorer
  • explore step by step
  • search next solution, search all solutions
  • "inspect" nodes (double-click)
  • hide failed subtrees
  • ...
  • Try it out!
slide-65
SLIDE 65

Invoking Gist

import org.gecode.gist.*; public static void main(String[] args) { Money m = new Money(); Gist g = new Gist(m); g.exploreOne(); // Also possible: g.exploreAll() }

slide-66
SLIDE 66

Best Solution Search

  • Reminder: SMM+

SEND + MOST = MONEY

  • Constraints:

Find distinct numbers such that the equation holds and MONEY is maximal

slide-67
SLIDE 67

SMM+: Order

  • Branch-and-bound principle:

after each solution, constrain remaining search to find only better solutions

  • Script provides method:

public void constrain(Space s) { ... }

  • Invoked on Space object to be constrained
  • Space s: so far best solution
  • Only use values from s! Never mix variables!
slide-68
SLIDE 68

SMM+: constrain method

IntVar money; public SMM() { ... int [] c = {10000,1000,100,10,1}; VarArray<IntVar> l = {m,o,n,e,y}; // pseudocode! linear(this, c, l, IRT_EQ, money); ... } public void constrain(Space bestSol) { SMM smm = (SMM) bestSol; rel(this, money, IRT_GR, smm.money.val()); }

slide-69
SLIDE 69

SMM+: main method

public static void main(String[] args) { SMM m = new SMM(); BABSearch s = new BABSearch(m); SMM best = null; SMM sol = (SMM) s.next(); while (sol != null) { best = sol; sol = (SMM) s.next(); } if (best != null) { System.out.println(best.toString()); } }

slide-70
SLIDE 70

SMM+: Gist

public static void main(String[] args) { SMM m = new SMM(); Gist g = new Gist(m, true); g.exploreAll(); }

slide-71
SLIDE 71

SMM+: Gist

public static void main(String[] args) { SMM m = new SMM(); Gist g = new Gist(m, true); g.exploreAll(); }

switch on b&b

slide-72
SLIDE 72

Solving: Summary

  • Non-interactive search:

DFSSearch, BABSearch

  • Interactive search:

Gist

  • Best solution search: provide constrain method
  • Search engines independent of script
slide-73
SLIDE 73

Some modeling techniques

  • Global constraints
  • Symmetry breaking
  • Reification
slide-74
SLIDE 74

Global constraints

  • Classic example:

x,y,z ∈ {1,2}, x ≠ y, x ≠ z, y ≠ z

  • No solution!
  • But: each individual constraint still satisfiable!

⇒ no propagation possible!

  • Solution: look at several constraints at once

distinct(x,y,z)

slide-75
SLIDE 75

Distinct

  • Remember last exercise for Sudoku?

"Experiment with different ... propagator strengths."

  • ICL_VAL: same as x ≠ y, x ≠ z, y ≠ z
  • ICL_BND: stronger
  • ICL_DOM: strongest possible propagation
slide-76
SLIDE 76

Grocery Puzzle

  • A kid buys four items in a grocery store.
  • Cashier: "That's €7,11.

Hold on! I multiplied instead of adding. Wow, the sum is also €7,11!"

  • What are the individual prices of the four items?
slide-77
SLIDE 77

Grocery Puzzle

  • Variables:

A,B,C,D ∈ {1, ..., 711} (price in cents)

  • Constraints:
  • A+B+C+D = 711
  • A*B*C*D = 711 * 100 * 100 * 100
slide-78
SLIDE 78

Grocery Puzzle

  • Branching:
  • Bad idea: try values one by one
  • Better: split domains
  • Typically good strategy for problems with arithmetic

constraints

  • Still:
slide-79
SLIDE 79

Grocery Puzzle: Symmetries

  • Many solutions are equivalent!
  • swap values of A,B,C,D
  • Let's order them:
  • A ≤ B ≤ C ≤ D
  • This is called

Symmetry Breaking

  • So let's see...
slide-80
SLIDE 80

Grocery Puzzle: Symmetries

  • Symmetry breaking also works on symmetric failure!
  • Example:
  • Suppose A,B,C have been assigned 1,2,3 by search.
  • We find no value for D such that we get a solution.
  • No need to try A=2, B=1, C=3!
  • Ruled out by A ≤ B ≤ C ≤ D
slide-81
SLIDE 81

Grocery Puzzle: More Symmetries

  • Observation: 711 has prime factor 79

711 = 79 × 9

  • So assume

A = 79 × X (for a "fresh" variable X)

  • Remove A ≤ B
  • And now...
slide-82
SLIDE 82

Reified constraints

  • Constraints are in a big conjuntion
  • How about disjunctive constraints?

A+B=C ∨ C=0

  • Solution: reify the constraints:

(A+B=C ⇔ b0) ∧ (C=0 ⇔ b1) ∧ (b0 ∨ b1 ⇔ true)

slide-83
SLIDE 83

Reified constraints

int[] ones = [1,1,-1]; VarArray<IntVar> xs = {a,b,c}; BoolVar b0(this); BoolVar b1(this); linear(this, ones, xs, IRT_EQ, 0, b0); rel(this, c, IRT_EQ, 0, b1); bool_or(this, b0, b1, true);

slide-84
SLIDE 84

What we've learned today

slide-85
SLIDE 85

What we've learned today

  • Use scripts to model constraint problems
slide-86
SLIDE 86

What we've learned today

  • Use scripts to model constraint problems
  • Posting a propagator installs it inside a space
slide-87
SLIDE 87

What we've learned today

  • Use scripts to model constraint problems
  • Posting a propagator installs it inside a space
  • We need copying for backtracking during search
slide-88
SLIDE 88

What we've learned today

  • Use scripts to model constraint problems
  • Posting a propagator installs it inside a space
  • We need copying for backtracking during search
  • Global constraints can provide stronger inferences
slide-89
SLIDE 89

What we've learned today

  • Use scripts to model constraint problems
  • Posting a propagator installs it inside a space
  • We need copying for backtracking during search
  • Global constraints can provide stronger inferences
  • Symmetry breaking is essential for some problems
slide-90
SLIDE 90

What we've learned today

  • Use scripts to model constraint problems
  • Posting a propagator installs it inside a space
  • We need copying for backtracking during search
  • Global constraints can provide stronger inferences
  • Symmetry breaking is essential for some problems
  • Reification allows for arbitrary Boolean combinations of

constraints

slide-91
SLIDE 91

Graded lab: temporal relations

  • Given:
  • a number of events
  • constraints between events:
  • a ends before b
  • a and b do not overlap
  • a happens during b
  • ...
slide-92
SLIDE 92

Graded lab: temporal relations

  • Given:
  • a number of events
  • constraints between events:
  • a ends before b
  • a and b do not overlap
  • a happens during b
  • ...
slide-93
SLIDE 93

Graded lab: temporal relations

  • Given:
  • a number of events
  • constraints between events:
  • a ends before b
  • a and b do not overlap
  • a happens during b
  • ...
slide-94
SLIDE 94

Graded lab: temporal relations

  • Given:
  • a number of events
  • constraints between events:
  • a ends before b
  • a and b do not overlap
  • a happens during b
  • ...
slide-95
SLIDE 95

Graded lab: temporal relations

  • Data structure:

enum Relation { PRECEDES, MEETS, OVERLAPS, DURING, STARTS, FINISHES, EQUALS}; class Item { String act1; Relation rel; String act2; } Vector<Vector<Item>> problem;

CNF

slide-96
SLIDE 96

Graded lab: temporal relations

A precedes B A meets B A overlaps B A during B A starts B A finishes B A equals B

slide-97
SLIDE 97

Graded lab: temporal relations

  • Your task:
  • create script that solves temporal relation problems
  • If you have too much time:
  • create visualization for the solutions
  • Submit
  • by Monday, April 30, 24:00 MESZ
  • by email to tack@ps.uni-sb.de
slide-98
SLIDE 98

Thanks for your attention! See you on Thursday.