+ All Categories
Home > Documents > Constraint Programming Overview based on...

Constraint Programming Overview based on...

Date post: 22-Mar-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
185
DM841 Discrete Optimization Part I Lecture 2 Constraint Programming Overview based on Examples Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark
Transcript
Page 1: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

DM841

Discrete Optimization

Part I

Lecture 2Constraint Programming Overview

based on Examples

Marco Chiarandini

Department of Mathematics & Computer ScienceUniversity of Southern Denmark

Page 2: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Outline

1. An Initial Example

2. Constraint

3. Send More MoneyPoints to RememberModeling in MILP

2

Page 3: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Put a different number in each circle (1 to 8) such that adjacent circles cannot take consecutive numbers

Page 4: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Constraint Programming An Introduction

by example

Patrick Prosser with the help of Toby Walsh, Chris Beck,

Barbara Smith, Peter van Beek, Edward Tsang, ...

Page 5: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

A Puzzle

•  Place numbers 1 through 8 on nodes – Each number appears exactly once

?

?

?

?

?

?

? ?

– No connected nodes have consecutive numbers

You have 8 minutes!

Page 6: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Heuristic Search Which nodes are hardest to number?

?

?

?

?

?

?

? ?

Page 7: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Heuristic Search

?

?

?

?

?

?

? ?

Page 8: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Heuristic Search

?

?

?

?

?

?

? ?

Which are the least constraining values to use?

Page 9: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Heuristic Search

?

1

?

?

8

?

? ?

Values 1 and 8

Page 10: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Heuristic Search

?

1

?

?

8

?

? ?

Values 1 and 8

Symmetry means we don’t need to consider: 8 1

Page 11: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

We can now eliminate many values for other nodes

Page 12: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{1,2,3,4,5,6,7,8}

Page 13: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{2,3,4,5,6,7}

Page 14: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{3,4,5,6}

Page 15: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{3,4,5,6}

By symmetry

{3,4,5,6}

Page 16: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{3,4,5,6}

{3,4,5,6}

{1,2,3,4,5,6,7,8}

Page 17: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{3,4,5,6}

{3,4,5,6}

{2,3,4,5,6,7}

Page 18: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

Page 19: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{3,4,5,6}

By symmetry

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

Page 20: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{3,4,5,6}

{3,4,5,6,7}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{2,3,4,5,6}

Page 21: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

? ?

{3,4,5,6}

{3,4,5,6,7}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{2,3,4,5,6}

Value 2 and 7 are left in just one variable domain each

Page 22: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

2 7

{3,4,5,6}

{3,4,5,6,7}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{2,3,4,5,6}

And propagate …

Page 23: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

2 7

{3,4,5}

{3,4,5,6,7}

{3,4,5}

{3,4,5,6}

{3,4,5,6}

{2,3,4,5,6}

And propagate …

Page 24: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

2 7

{3,4,5}

{3,4,5,6,7}

{3,4,5}

{4,5,6}

{4,5,6}

{2,3,4,5,6}

And propagate …

Page 25: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

?

1

?

?

8

?

2 7

{3,4,5}

{3,4,5}

{4,5,6}

{4,5,6}

Guess a value, but be prepared to backtrack …

Page 26: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

?

?

8

?

2 7

{3,4,5}

{3,4,5}

{4,5,6}

{4,5,6}

Guess a value, but be prepared to backtrack …

Page 27: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

?

?

8

?

2 7

{3,4,5}

{3,4,5}

{4,5,6}

{4,5,6}

And propagate …

Page 28: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

?

?

8

?

2 7

{4,5}

{5,6}

{4,5,6}

And propagate …

Page 29: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

?

?

8

?

2 7

{4,5}

{5,6}

{4,5,6}

Guess another value …

Page 30: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

?

5

8

?

2 7

{4,5} {4,5,6}

Guess another value …

Page 31: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

?

5

8

?

2 7

{4,5} {4,5,6}

And propagate …

Page 32: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

?

5

8

?

2 7

{4} {4,6}

And propagate …

Page 33: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

4

5

8

?

2 7

{4} {4,6}

One node has only a single value left …

Page 34: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Inference/propagation

3

1

4

5

8

6

2 7

{6}

Page 35: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Solution

3

1

4

5

8

6

2 7

Page 36: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

The Core of Constraint Computation

•  Modelling – Deciding on variables/domains/constraints

•  Heuristic Search •  Inference/Propagation •  Symmetry •  Backtracking

Page 37: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Hardness

•  The puzzle is actually a hard problem – NP-complete

Page 38: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Constraint programming

•  Model problem by specifying constraints on acceptable solutions –  define variables and domains –  post constraints on these variables

•  Solve model –  choose algorithm

•  incremental assignment / backtracking search •  complete assignments / stochastic search

–  design heuristics

Page 39: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Example CSP •  Variable, vi for each node •  Domain of {1, …, 8} •  Constraints

–  All values used allDifferent(v1 v2 v3 v4 v5 v6 v7 v8) –  No consecutive numbers for

adjoining nodes |v1 - v2 | > 1 |v1 - v3 | > 1 …

?

?

?

?

?

?

? ?

Page 40: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Outline

1. An Initial Example

2. Constraint

3. Send More MoneyPoints to RememberModeling in MILP

3

Page 41: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Constraint Programming - in a nutshell

I Declarative description of problems withI Variables which range over (finite) sets of valuesI Constraints over subsets of variables which restrict possible value

combinationsI A solution is a value assignment which satisfies all constraints

I Constraint propagation/reasoningI Removing inconsistent values for variablesI Detect failure if constraint can not be satisfiedI Interaction of constraints via shared variablesI Incomplete

I SearchI User controlled assignment of values to variablesI Each step triggers constraint propagation

I Different domains require/allow different methods

4

Page 42: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Constraint Programming

Constraint Programming: an alternative approach to imperative programmingand object oriented programming.

I Variables each with a finite set of possible values (domain)

I Constraint on a sequence of variables: a relationship on their domains

Constraint Satisfaction Problem: finite set of constraints

5

Page 43: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

CP

Constraint Programming = model (representation) +propagation (reasoning, inference) +search (reasoning, inference)

6

Page 44: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Basic Process

Problem

Human

Model

Constraint Solver/Search

Solution

Insight Centre for Data Analytics Slide 9June 20th, 2016

Page 45: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

More Realistic

Problem

Human

Model

Constraint Solver/Search

Hangs Solution Wrong Solution

Insight Centre for Data Analytics Slide 10June 20th, 2016

Page 46: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Dual Role of Model

• Allows Human to Express Problem• Close to Problem Domain• Constraints as Abstractions

• Allows Solver to Execute• Variables as Communication Mechanism• Constraints as Algorithms

Insight Centre for Data Analytics Slide 11June 20th, 2016

Page 47: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Modelling Frameworks

• MiniZinc (NICTA, Australia)

• NumberJack (Insight, Ireland)

• Essence (UK)

• Allow use of multiple back-end solvers

• Compile model into variants for each solver

• A priori solver independent model(CP, MIP, SAT)

Insight Centre for Data Analytics Slide 12June 20th, 2016

Page 48: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Framework Process

Problem

Human

Model

Compile/Reformulate

CP MIP SAT Other

Solution Solution Solution Solution

Insight Centre for Data Analytics Slide 13June 20th, 2016

Page 49: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Computational Models

Three main Computational Models to solve (combinatorial) constrainedoptimization problems:

I Mathematical Programming (LP, ILP, QP, SDP, ...)

I Constraint Programming (CSP as a model, SAT as a very special case)

I Local Search (... and Meta-heuristics)

I Others? Dynamic programming, dedicated algorithms, satisfiabilitymodulo theory, answer set programming, etc.

7

Page 50: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Modeling

Modeling:

1. identify:I parametersI variablesI domainsI constraintsI objective function

that formulate the problem

2. express what in point 1) in a way that allows the solution by availablesoftware

8

Page 51: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Variables

In MILP: real and integer (mostly binary) variables

In CP:

I finite domain integer (including Booleans),

I continuos with interval constraints

I structured domains: finite sets, multisets, graphs, ...

In LS: integer variables

9

Page 52: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Constraint Programming vs MILP

I In MILP we formulate problems as a set of linear inequalities

I In CP we describe substructures (so-called global constraints) andcombine them with various combinators.

I Substructures capture building blocks often (but not always)comptuationally tractable by special-purpose algorithms

I CP models can:I be solved by the constraint engineI be linearized and solved by their MIP solvers;I be translated in CNF and solved by SAT solvers;I be handled by local search

I In MILP the solver is often seen as a black-boxIn CP and LS solvers leave the user the task of programming the search.

I CP = model + propagation + searchconstraint propagation by domain filtering inferencesearch = backtracking or branch and bound or local search

10

Page 53: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Outline

1. An Initial Example

2. Constraint

3. Send More MoneyPoints to RememberModeling in MILP

11

Page 54: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Aims

I Example of Finite Domain Constraint Problem

I Models and Programs

I Constraint Propagation and Search

I Some Basic Constraints:linear arithmetic, alldifferent, disequality

I A Built-in search

I Visualizers for variables, constraints and search

12

Page 55: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Problem: Send + More = Money

Send + More = Money

You are asked to replace each letter by a different digit so that

S E N D +M O R E =

M O N E Y

is correct. Because S and M are the leading digits, they cannot be equal tothe 0 digit.

13

Page 56: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Modelling

1. Parameters

2. Variables (ie, solution representation)

3. Domains (ie, allowed values for the variables)

4. Constraints

Later Objective Function

14

Page 57: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Model

I Each character is a variable, which ranges over the values 0 to 9.

I An alldifferent constraint between all variables, which states that twodifferent variables must have different values. This is a very commonconstraint, which we will encounter in many other problems later on.

I Two disequality constraints (variable X must be different from value V )stating that the variables at the beginning of a number can not take thevalue 0.

I An arithmetic equality constraint linking all variables with the propercoefficients and stating that the equation must hold.

15

Page 58: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send More Money: CP model

SEND + MORE = MONEY

I Xi ∈ {0, . . . , 9} for all i ∈ I = {S ,E ,N,D,M,O,R,Y }

I Each letter takes a different digit 1 inequality constraint

alldifferent([X1,X2, . . . ,X8]).

(it substitutes 28 inequality constraints: Xi 6= Xj , i , j ∈ I , i 6= j)

I XM 6= 0, XS 6= 0

I Crypto constraint 1 equality constraint:103X1 +102X2 +10X3 +X4 +103X5 +102X6 +10X7 +X2 =

104X5 +103X6 +102X3 +10X2 +X8

16

Page 59: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

I This is one model, not the model of the problem

I Many possible alternatives

I Choice often depends on the constraint system availableConstraints availableReasoning attached to constraints

I Not always clear which is the best model

17

Page 60: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send More Money: CP modelGecode-python

from gecode import *

s = space()letters = s.intvars(8,0,9)S,E,N,D,M,O,R,Y = letterss.rel(M,IRT_NQ,0)s.rel(S,IRT_NQ,0)s.distinct(letters)C = [1000, 100, 10, 1,

1000, 100, 10, 1,-10000, -1000, -100, -10, -1]

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

s.linear(C,X, IRT_EQ, 0)s.branch(letters, INT_VAR_SIZE_MIN, INT_VAL_MIN)for s2 in s.search():

print(s2.val(letters))

18

Page 61: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send More Money: CP modelMiniZinc

19

Page 62: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Program Sendmory

:- module(sendmory).:- export(sendmory/1).:- lib(ic).sendmory(L):-

L = [S,E,N,D,M,O,R,Y],L :: 0..9,alldifferent(L),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(L).

Insight Centre for Data Analytics Slide 21June 20th, 2016

Page 63: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Question

But how did the program come up with this solution?

20

Page 64: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Constraint Setup

I Domain Definition

I Alldifferent Constraint

I Disequality Constraints

I Equality Constraint

21

Page 65: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

The following slides are taken from H. Simonis: H. Simonis’ demo, slides33-134 and his tutorial at ACP2016.

22

Page 66: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Domain Definition

L = [S,E,N,D,M,O,R,Y],L :: 0..9,

[S,E ,N,D,M,O,R,Y ] ∈ {0..9}

Insight Centre for Data Analytics Slide 31June 20th, 2016

Page 67: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Domain Visualization

0 1 2 3 4 5 6 7 8 9SENDMORY

Insight Centre for Data Analytics Slide 32June 20th, 2016

Page 68: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Domain Visualization

Rows =Variables

0 1 2 3 4 5 6 7 8 9SENDMORY

Insight Centre for Data Analytics Slide 32June 20th, 2016

Page 69: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Domain Visualization

Columns = Values0 1 2 3 4 5 6 7 8 9

SENDMORY

Insight Centre for Data Analytics Slide 32June 20th, 2016

Page 70: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Domain Visualization

Cells= State

0 1 2 3 4 5 6 7 8 9SENDMORY

Insight Centre for Data Analytics Slide 32June 20th, 2016

Page 71: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Alldifferent Constraint

alldifferent(L),

• Built-in of ic library

• No initial propagation possible

• Suspends, waits until variables are changed

• When variable is fixed, remove value from domain ofother variables

• Forward checking

Insight Centre for Data Analytics Slide 33June 20th, 2016

Page 72: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Alldifferent Visualization

Uses the same representation as the domain visualizer

0 1 2 3 4 5 6 7 8 9SENDMORY

Insight Centre for Data Analytics Slide 34June 20th, 2016

Page 73: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Disequality Constraints

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

Remove value from domain

S ∈ {1..9},M ∈ {1..9}

Constraints solved, can be removed

Insight Centre for Data Analytics Slide 35June 20th, 2016

Page 74: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Domains after Disequality

0 1 2 3 4 5 6 7 8 9SENDMORY

Insight Centre for Data Analytics Slide 36June 20th, 2016

Page 75: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Equality Constraint

• Normalization of linear terms• Single occurence of variable• Positive coefficients

• Propagation

Insight Centre for Data Analytics Slide 37June 20th, 2016

Page 76: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

1000*S+ 100*E+ 10*N+ D+1000*M+ 100*O+ 10*R+ E

10000*M+ 1000*O+ 100*N+ 10*E+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 77: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

1000*S+ 100*E+ 10*N+ D+1000*M+ 100*O+ 10*R+ E

10000*M+ 1000*O+ 100*N+ 10*E+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 78: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

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

9000*M+ 1000*O+ 100*N+ 10*E+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 79: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

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

9000*M+ 1000*O+ 100*N+ 10*E+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 80: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

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

9000*M+ 900*O+ 100*N+ 10*E+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 81: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

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

9000*M+ 900*O+ 100*N+ 10*E+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 82: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

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

9000*M+ 900*O+ 90*N+ 10*E+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 83: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

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

9000*M+ 900*O+ 90*N+ 10*E+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 84: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Normalization

1000*S+ 91*E+ D+ 10*R

9000*M+ 900*O+ 90*N+ Y

Insight Centre for Data Analytics Slide 38June 20th, 2016

Page 85: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Simplified Equation

1000∗S+91∗E +10∗R+D = 9000∗M +900∗O+90∗N +Y

Insight Centre for Data Analytics Slide 39June 20th, 2016

Page 86: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation

1000 ∗ S1..9 + 91 ∗ E0..9 + 10 ∗ R0..9 + D0..9 =

9000 ∗M1..9 + 900 ∗O0..9 + 90 ∗ N0..9 + Y 0..9

Insight Centre for Data Analytics Slide 40June 20th, 2016

Page 87: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation

1000 ∗ S1..9 + 91 ∗ E0..9 + 10 ∗ R0..9 + D0..9︸ ︷︷ ︸

1000..9918

=

9000 ∗M1..9 + 900 ∗O0..9 + 90 ∗ N0..9 + Y 0..9︸ ︷︷ ︸

9000..89919

Insight Centre for Data Analytics Slide 40June 20th, 2016

Page 88: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation

1000 ∗ S1..9 + 91 ∗ E0..9 + 10 ∗ R0..9 + D0..9︸ ︷︷ ︸

9000..9918

=

9000 ∗M1..9 + 900 ∗O0..9 + 90 ∗ N0..9 + Y 0..9︸ ︷︷ ︸

9000..9918

Insight Centre for Data Analytics Slide 40June 20th, 2016

Page 89: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation

1000 ∗ S1..9 + 91 ∗ E0..9 + 10 ∗ R0..9 + D0..9︸ ︷︷ ︸

9000..9918

=

9000 ∗M1..9 + 900 ∗O0..9 + 90 ∗ N0..9 + Y 0..9︸ ︷︷ ︸

9000..9918

Deduction:M = 1,S = 9,O ∈ {0..1}

Insight Centre for Data Analytics Slide 40June 20th, 2016

Page 90: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation

1000 ∗ S1..9 + 91 ∗ E0..9 + 10 ∗ R0..9 + D0..9︸ ︷︷ ︸

9000..9918

=

9000 ∗M1..9 + 900 ∗O0..9 + 90 ∗ N0..9 + Y 0..9︸ ︷︷ ︸

9000..9918

Deduction:M = 1,S = 9,O ∈ {0..1}

Why? Skip

Insight Centre for Data Analytics Slide 40June 20th, 2016

Page 91: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Consider lower bound for S

1000 ∗ S1..9 + 91 ∗ E0..9 + 10 ∗ R0..9 + D0..9︸ ︷︷ ︸

9000..9918

= 9000 ∗M1..9 + 900 ∗O0..9 + 90 ∗ N0..9 + Y 0..9︸ ︷︷ ︸

9000..9918

• Lower bound of equation is 9000

• Rest of lhs (left hand side) (91 ∗ E0..9 + 10 ∗ R0..9 + D0..9)is atmost 918

• S must be greater or equal to 9000−9181000 = 8.082

• otherwise lower bound of equation not reached by lhs

• S is integer, therefore S ≥ d9000−9181000 e = 9

• S has upper bound of 9, so S = 9

Insight Centre for Data Analytics Slide 41June 20th, 2016

Page 92: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Consider upper bound of M

1000 ∗ S1..9 + 91 ∗ E0..9 + 10 ∗ R0..9 + D0..9︸ ︷︷ ︸

9000..9918

= 9000 ∗M1..9 + 900 ∗O0..9 + 90 ∗ N0..9 + Y 0..9︸ ︷︷ ︸

9000..9918

• Upper bound of equation is 9918

• Rest of rhs (right hand side) 900 ∗O0..9 + 90 ∗N0..9 + Y 0..9

is at least 0

• M must be smaller or equal to 9918−09000 = 1.102

• M must be integer, therefore M ≤ b9918−09000 c = 1

• M has lower bound of 1, so M = 1

Insight Centre for Data Analytics Slide 42June 20th, 2016

Page 93: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Consider upper bound of O

1000 ∗ S1..9 + 91 ∗ E0..9 + 10 ∗ R0..9 + D0..9︸ ︷︷ ︸

9000..9918

= 9000 ∗M1..9 + 900 ∗O0..9 + 90 ∗ N0..9 + Y 0..9︸ ︷︷ ︸

9000..9918

• Upper bound of equation is 9918

• Rest of rhs (right hand side) 9000 ∗ 1 + 90 ∗ N0..9 + Y 0..9 isat least 9000

• O must be smaller or equal to 9918−9000900 = 1.02

• O must be integer, therefore O ≤ b9918−9000900 c = 1

• O has lower bound of 0, so O ∈ {0..1}

Insight Centre for Data Analytics Slide 43June 20th, 2016

Page 94: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality: Result

0 1 2 3 4 5 6 7 8 9S - - - - - - - - Y

ENDM Y - - - - - - - -O 6 6 6 6 6 6 6 6

RY

Insight Centre for Data Analytics Slide 44June 20th, 2016

Page 95: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9S - - - - - - - - Y

ENDM Y - - - - - - - -O 6 6 6 6 6 6 6 6

RY

Insight Centre for Data Analytics Slide 45June 20th, 2016

Page 96: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9S Y

E |N |D |M Y

OR |Y |

Insight Centre for Data Analytics Slide 45June 20th, 2016

Page 97: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SE |N |D |M Y

O |R |Y |

Insight Centre for Data Analytics Slide 45June 20th, 2016

Page 98: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SENDMO Y

RY

Insight Centre for Data Analytics Slide 45June 20th, 2016

Page 99: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SE |N |D |MO Y

R |Y |

Insight Centre for Data Analytics Slide 45June 20th, 2016

Page 100: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SENDMORY

O = 0, [E ,R,D,N,Y ] ∈ {2..8}

Insight Centre for Data Analytics Slide 45June 20th, 2016

Page 101: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Waking the equality constraint

• Triggered by assignment of variables

• or update of lower or upper bound

Insight Centre for Data Analytics Slide 46June 20th, 2016

Page 102: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Removal of constants

1000 ∗ 9 + 91 ∗ E2..8 + 10 ∗ R2..8 + D2..8 =

9000 ∗ 1 + 900 ∗ 0 + 90 ∗ N2..8 + Y 2..8

Insight Centre for Data Analytics Slide 47June 20th, 2016

Page 103: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Removal of constants

1000 ∗ 9 + 91 ∗ E2..8 + 10 ∗ R2..8 + D2..8 =

9000 ∗ 1 + 900 ∗ 0 + 90 ∗ N2..8 + Y 2..8

Insight Centre for Data Analytics Slide 47June 20th, 2016

Page 104: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Removal of constants

91 ∗ E2..8 + 10 ∗ R2..8 + D2..8 = 90 ∗ N2..8 + Y 2..8

Insight Centre for Data Analytics Slide 47June 20th, 2016

Page 105: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 1)

91 ∗ E2..8 + 10 ∗ R2..8 + D2..8︸ ︷︷ ︸

204..816

= 90 ∗ N2..8 + Y 2..8︸ ︷︷ ︸

182..728

Insight Centre for Data Analytics Slide 48June 20th, 2016

Page 106: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 1)

91 ∗ E2..8 + 10 ∗ R2..8 + D2..8 = 90 ∗ N2..8 + Y 2..8︸ ︷︷ ︸

204..728

Insight Centre for Data Analytics Slide 48June 20th, 2016

Page 107: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 1)

91 ∗ E2..8 + 10 ∗ R2..8 + D2..8 = 90 ∗ N2..8 + Y 2..8︸ ︷︷ ︸

204..728

N ≥ 3 = d204− 890

e,E ≤ 7 = b728− 2291

c

Insight Centre for Data Analytics Slide 48June 20th, 2016

Page 108: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 2)

91 ∗ E2..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N3..8 + Y 2..8

Insight Centre for Data Analytics Slide 49June 20th, 2016

Page 109: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 2)

91 ∗ E2..7 + 10 ∗ R2..8 + D2..8︸ ︷︷ ︸

204..725

= 90 ∗ N3..8 + Y 2..8︸ ︷︷ ︸

272..728

Insight Centre for Data Analytics Slide 49June 20th, 2016

Page 110: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 2)

91 ∗ E2..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N3..8 + Y 2..8︸ ︷︷ ︸

272..725

Insight Centre for Data Analytics Slide 49June 20th, 2016

Page 111: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 2)

91 ∗ E2..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N3..8 + Y 2..8︸ ︷︷ ︸

272..725

E ≥ 3 = d272− 8891

e

Insight Centre for Data Analytics Slide 49June 20th, 2016

Page 112: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 3)

91 ∗ E3..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N3..8 + Y 2..8

Insight Centre for Data Analytics Slide 50June 20th, 2016

Page 113: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 3)

91 ∗ E3..7 + 10 ∗ R2..8 + D2..8︸ ︷︷ ︸

295..725

= 90 ∗ N3..8 + Y 2..8︸ ︷︷ ︸

272..728

Insight Centre for Data Analytics Slide 50June 20th, 2016

Page 114: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 3)

91 ∗ E3..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N3..8 + Y 2..8︸ ︷︷ ︸

295..725

Insight Centre for Data Analytics Slide 50June 20th, 2016

Page 115: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 3)

91 ∗ E3..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N3..8 + Y 2..8︸ ︷︷ ︸

295..725

N ≥ 4 = d295− 890

e

Insight Centre for Data Analytics Slide 50June 20th, 2016

Page 116: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 4)

91 ∗ E3..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N4..8 + Y 2..8

Insight Centre for Data Analytics Slide 51June 20th, 2016

Page 117: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 4)

91 ∗ E3..7 + 10 ∗ R2..8 + D2..8︸ ︷︷ ︸

295..725

= 90 ∗ N4..8 + Y 2..8︸ ︷︷ ︸

362..728

Insight Centre for Data Analytics Slide 51June 20th, 2016

Page 118: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 4)

91 ∗ E3..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N4..8 + Y 2..8︸ ︷︷ ︸

362..725

Insight Centre for Data Analytics Slide 51June 20th, 2016

Page 119: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 4)

91 ∗ E3..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N4..8 + Y 2..8︸ ︷︷ ︸

362..725

E ≥ 4 = d362− 8891

e

Insight Centre for Data Analytics Slide 51June 20th, 2016

Page 120: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 5)

91 ∗ E4..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N4..8 + Y 2..8

Insight Centre for Data Analytics Slide 52June 20th, 2016

Page 121: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 5)

91 ∗ E4..7 + 10 ∗ R2..8 + D2..8︸ ︷︷ ︸

386..725

= 90 ∗ N4..8 + Y 2..8︸ ︷︷ ︸

362..728

Insight Centre for Data Analytics Slide 52June 20th, 2016

Page 122: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 5)

91 ∗ E4..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N4..8 + Y 2..8︸ ︷︷ ︸

386..725

Insight Centre for Data Analytics Slide 52June 20th, 2016

Page 123: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 5)

91 ∗ E4..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N4..8 + Y 2..8︸ ︷︷ ︸

386..725

N ≥ 5 = d386− 890

e

Insight Centre for Data Analytics Slide 52June 20th, 2016

Page 124: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 6)

91 ∗ E4..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N5..8 + Y 2..8

Insight Centre for Data Analytics Slide 53June 20th, 2016

Page 125: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 6)

91 ∗ E4..7 + 10 ∗ R2..8 + D2..8︸ ︷︷ ︸

386..725

= 90 ∗ N5..8 + Y 2..8︸ ︷︷ ︸

452..728

Insight Centre for Data Analytics Slide 53June 20th, 2016

Page 126: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 6)

91 ∗ E4..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N5..8 + Y 2..8︸ ︷︷ ︸

452..725

Insight Centre for Data Analytics Slide 53June 20th, 2016

Page 127: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality (Iteration 6)

91 ∗ E4..7 + 10 ∗ R2..8 + D2..8 = 90 ∗ N5..8 + Y 2..8︸ ︷︷ ︸

452..725

N ≥ 5 = d452− 890

e,E ≥ 4 = d452− 8891

e

No further propagation at this point

Insight Centre for Data Analytics Slide 53June 20th, 2016

Page 128: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Domains after setup

0 1 2 3 4 5 6 7 8 9SENDMORY

Insight Centre for Data Analytics Slide 54June 20th, 2016

Page 129: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Outline

Problem

Program

Constraint Setup

SearchStep 1Step 2Further StepsSolution

Points to Remember

Insight Centre for Data Analytics Slide 55June 20th, 2016

Page 130: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

labeling built-in

labeling([S,E,N,D,M,O,R,Y])

• Try variable is order given

• Try values starting from smallest value in domain

• When failing, backtrack to last open choice

• Chronological Backtracking

• Depth First search

Insight Centre for Data Analytics Slide 56June 20th, 2016

Page 131: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Search Tree Step 1

Variable S already fixed

S

E9

Insight Centre for Data Analytics Slide 57June 20th, 2016

Page 132: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Step 2, Alternative E = 4

Variable E ∈ {4..7}, first value tested is 4

S

E4

9

Insight Centre for Data Analytics Slide 58June 20th, 2016

Page 133: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Assignment E = 4

0 1 2 3 4 5 6 7 8 9SE Y - - -NDMORY

Insight Centre for Data Analytics Slide 59June 20th, 2016

Page 134: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of E = 4, equality constraint

91 ∗ 4 + 10 ∗ R2..8 + D2..8 = 90 ∗ N5..8 + Y 2..8

Insight Centre for Data Analytics Slide 60June 20th, 2016

Page 135: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of E = 4, equality constraint

91 ∗ 4 + 10 ∗ R2..8 + D2..8︸ ︷︷ ︸

386..452

= 90 ∗ N5..8 + Y 2..8︸ ︷︷ ︸

452..728

Insight Centre for Data Analytics Slide 60June 20th, 2016

Page 136: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of E = 4, equality constraint

91 ∗ 4 + 10 ∗ R2..8 + D2..8 = 90 ∗ N5..8 + Y 2..8︸ ︷︷ ︸

452

Insight Centre for Data Analytics Slide 60June 20th, 2016

Page 137: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of E = 4, equality constraint

91 ∗ 4 + 10 ∗ R2..8 + D2..8 = 90 ∗ N5..8 + Y 2..8︸ ︷︷ ︸

452

N = 5,Y = 2,R = 8,D = 8

Insight Centre for Data Analytics Slide 60June 20th, 2016

Page 138: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Result of equality propagation

0 1 2 3 4 5 6 7 8 9SEN Y - - -D - - - - - - Y

MOR - - - - - - Y

Y Y - - - - - -

Insight Centre for Data Analytics Slide 61June 20th, 2016

Page 139: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SEN Y - - -D - - - - - - Y

MOR - - - - - - Y

Y Y - - - - - -

Insight Centre for Data Analytics Slide 62June 20th, 2016

Page 140: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SEN Y - -D - - - - - - Y

MOR - - - - - - Y

Y Y - - - - -

Alldifferent fails!

Insight Centre for Data Analytics Slide 62June 20th, 2016

Page 141: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Step 2, Alternative E = 5

Return to last open choice, E , and test next value

S

E4

N5

9

Insight Centre for Data Analytics Slide 63June 20th, 2016

Page 142: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Assignment E = 5

0 1 2 3 4 5 6 7 8 9SE - Y - -NDMORY

Insight Centre for Data Analytics Slide 64June 20th, 2016

Page 143: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SE - Y - -NDMORY

Insight Centre for Data Analytics Slide 65June 20th, 2016

Page 144: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SE Y

N |D |MOR |Y |

Insight Centre for Data Analytics Slide 65June 20th, 2016

Page 145: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SENDMORY

N 6= 5,N ≥ 6

Insight Centre for Data Analytics Slide 65June 20th, 2016

Page 146: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality

91 ∗ 5 + 10 ∗ R2..8 + D2..8 = 90 ∗ N6..8 + Y 2..8

Insight Centre for Data Analytics Slide 66June 20th, 2016

Page 147: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality

91 ∗ 5 + 10 ∗ R2..8 + D2..8︸ ︷︷ ︸

477..543

= 90 ∗ N6..8 + Y 2..8︸ ︷︷ ︸

542..728

Insight Centre for Data Analytics Slide 66June 20th, 2016

Page 148: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality

91 ∗ 5 + 10 ∗ R2..8 + D2..8 = 90 ∗ N6..8 + Y 2..8︸ ︷︷ ︸

542..543

Insight Centre for Data Analytics Slide 66June 20th, 2016

Page 149: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality

91 ∗ 5 + 10 ∗ R2..8 + D2..8 = 90 ∗ N6..8 + Y 2..8︸ ︷︷ ︸

542..543

N = 6,Y ∈ {2,3},R = 8,D ∈ {7..8}

Insight Centre for Data Analytics Slide 66June 20th, 2016

Page 150: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Result of equality propagation

0 1 2 3 4 5 6 7 8 9SEN Y - -D 6 6 6 6

MOR - - - - - Y

Y 6 6 6 6

Insight Centre for Data Analytics Slide 67June 20th, 2016

Page 151: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SEN Y - -D 6 6 6 6

MOR - - - - - Y

Y 6 6 6 6

Insight Centre for Data Analytics Slide 68June 20th, 2016

Page 152: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SEND |MOR Y

Y

Insight Centre for Data Analytics Slide 68June 20th, 2016

Page 153: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SEND Y

MORY

Insight Centre for Data Analytics Slide 68June 20th, 2016

Page 154: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of alldifferent

0 1 2 3 4 5 6 7 8 9SENDMORY

D = 7

Insight Centre for Data Analytics Slide 68June 20th, 2016

Page 155: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality

91 ∗ 5 + 10 ∗ 8 + 7 = 90 ∗ 6 + Y 2..3

Insight Centre for Data Analytics Slide 69June 20th, 2016

Page 156: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality

91 ∗ 5 + 10 ∗ 8 + 7︸ ︷︷ ︸542

= 90 ∗ 6 + Y 2..3︸ ︷︷ ︸

542..543

Insight Centre for Data Analytics Slide 69June 20th, 2016

Page 157: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality

91 ∗ 5 + 10 ∗ 8 + 7 = 90 ∗ 6 + Y 2..3︸ ︷︷ ︸

542

Insight Centre for Data Analytics Slide 69June 20th, 2016

Page 158: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Propagation of equality

91 ∗ 5 + 10 ∗ 8 + 7 = 90 ∗ 6 + Y 2..3︸ ︷︷ ︸

542

Y = 2

Insight Centre for Data Analytics Slide 69June 20th, 2016

Page 159: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Last propagation step

0 1 2 3 4 5 6 7 8 9SENDMORY Y -

Insight Centre for Data Analytics Slide 70June 20th, 2016

Page 160: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Further Steps: Nothing more to do

S

E4

N5

9

Insight Centre for Data Analytics Slide 71June 20th, 2016

Page 161: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Further Steps: Nothing more to do

S

E4

N

D6

5

9

Insight Centre for Data Analytics Slide 71June 20th, 2016

Page 162: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Further Steps: Nothing more to do

S

E4

N

D

M7

6

5

9

Insight Centre for Data Analytics Slide 71June 20th, 2016

Page 163: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Further Steps: Nothing more to do

S

E4

N

D

M

O1

7

6

5

9

Insight Centre for Data Analytics Slide 71June 20th, 2016

Page 164: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Further Steps: Nothing more to do

S

E4

N

D

M

O

R0

1

7

6

5

9

Insight Centre for Data Analytics Slide 71June 20th, 2016

Page 165: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Further Steps: Nothing more to do

S

E4

N

D

M

O

R

Y8

0

1

7

6

5

9

Insight Centre for Data Analytics Slide 71June 20th, 2016

Page 166: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Further Steps: Nothing more to do

S

E4

N

D

M

O

R

Y2

8

0

1

7

6

5

9

Insight Centre for Data Analytics Slide 71June 20th, 2016

Page 167: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Complete Search Tree

S

E4

N

D

M

O

R

Y2

8

0

1

7

6

5 6 7

9

Insight Centre for Data Analytics Slide 72June 20th, 2016

Page 168: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Solution

9 5 6 7+ 1 0 8 51 0 6 5 2

Insight Centre for Data Analytics Slide 73June 20th, 2016

Page 169: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Outline

1. An Initial Example

2. Constraint

3. Send More MoneyPoints to RememberModeling in MILP

23

Page 170: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Points to Remember

I Constraint models are expressed by:variables + constraints + parameters

I Problems can have many different models, which can behave quitedifferently. Choosing the best model is an art.

I Constraints can take many different forms.

I Propagation deals with the interaction of variables and constraints:It removes some values that are inconsistent with a constraint from thedomain of a variable.

I Constraints only communicate via shared variables.

24

Page 171: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Points to Remember

I Propagation is data driven, and can be quite complex even for smallexamples.

I Propagation usually is not sufficient, search may be required to find asolution.

I The default search uses chronological depth-first backtracking,systematically exploring the complete search space.

I The search choices and propagation are interleaved,after every choice some more propagation may further reduce theproblem.

25

Page 172: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Applications

I Operation research (optimization problems)I Graphical interactive systems (to express geometrical correctness)I Molecular biology (DNA sequencing, 3D models of proteins)I FinanceI Circuit verificationI Elaboration of natural languages (construction of efficient parsers)I Scheduling of activitiesI Configuration problem in form compilationI Generation of coerent music programs [Anders and Miranda [2011]].I Data basesI ...I http://hsimonis.wordpress.com/

26

Page 173: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

ApplicationsDistribution of technology used at Google for optimization applicationsdeveloped by the operations research team

[Slide presented by Laurent Perron on OR-Tools at CP2013]27

Page 174: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

List of Contents

I Modeling with Finite Domain Integer VariablesI Introduction to GecodeI Overview on global constraintsI Notions of local consistencyI Constraint propagation algorithmsI Filtering algorithms for global constraintsI SearchI Set variablesI Symmetries

28

Page 175: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Outline

1. An Initial Example

2. Constraint

3. Send More MoneyPoints to RememberModeling in MILP

29

Page 176: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send More Money: ILP model 1

I xi ∈ {0, . . . , 9} for all i ∈ I = {S ,E ,N,D,M,O,R,Y }

I δij =

{0 if xi < xj

1 if xj < xi

I Crypto constraint:103x1 +102x2 +10x3 +x4 +103x5 +102x6 +10x7 +x2 =

104x5 +103x6 +102x3 +10x2 +x8

I Each letter takes a different digit:

xi − xj − 10δij ≤ −1, for all i , j , i < jxj − xi + 10δij ≤ 9, for all i , j , i < j

30

Page 177: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send More Money: ILP model 2

I xi ∈ {0, . . . , 9} for all i ∈ I = {S ,E ,N,D,M,O,R,Y }I yij ∈ {0, 1} for all i ∈ I , j ∈ J = {0, . . . , 9}I Crypto constraint:

103x1 +102x2 +10x3 +x4 +103x5 +102x6 +10x7 +x2 =

104x5 +103x6 +102x3 +10x2 +x8

I Each letter takes a different digit:∑

j∈J

yij = 1, ∀i ∈ I ,

i∈I

yij ≤ 1, ∀j ∈ J,

xi =∑

j∈J

jyij , ∀i ∈ I .

31

Page 178: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send More Money: ILP model

The quality of these formulations depends on both the tightness of the LPrelaxations and the number of constraints and variables (compactness)

I Which of the two models is tighter?project out all extra variables in the LP so that the polytope for LP is inthe space of the x variables. By linear comb. of constraints:

Model 1

−1 ≤ xi − xj ≤ 10− 1

Model 2∑j∈J

xj ≥|J| (|J| − 1)

2, ∀J ⊂ I ,

∑j∈J

xj ≤|J| (2k − |J|) + 1

2, ∀J ⊂ I .

I Can you find the convex hull of this problem?Williams and Yan [2001] prove that model 2 is facet defining

Suppose we want to maximize MONEY, how strong is the upper boundobtained with this formulation? How to obtain a stronger upper bound?

32

Page 179: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send More Money: ILP model (revisited)

I xi ∈ {0, . . . , 9} for all i ∈ I = {S ,E ,N,D,M,O,R,Y }I Crypto constraint:

103x1 +102x2 +10x3 +x4 +103x5 +102x6 +10x7 +x2 =

104x5 +103x6 +102x3 +10x2 +x8

I Each letter takes a different digit:

j∈J

xj ≥|J| (|J| − 1)

2, ∀J ⊂ I ,

j∈J

xj ≤|J| (2k − |J|) + 1

2, ∀J ⊂ I .

But exponentially many!

33

Page 180: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send More Money: CP model (revisited)

I Xi ∈ {0, . . . , 9} for all i ∈ I = {S ,E ,N,D,M,O,R,Y }

I

103X1 +102X2 +10X3 +X4 +103X5 +102X6 +10X7 +X2 =

104X5 +103X6 +102X3 +10X2 +X8

I

alldifferent([X1,X2, . . . ,X8]).

I Redundant constraints (5 equality constraints)X4 + X2 = 10 r1 + X8,

X3 + X7 + r1 = 10 r2 + X2,

X2 + X6 + r2 = 10 r3 + X3,

X1 + X5 + r3 = 10 r4 + X6,

+r4 = X5.

Can we do better? Can we propagate something?

34

Page 181: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Send Most Money: CP modelGecode-python

Optimization version:

max∑

i∈I ′CiXi , I ′ = {M,O,N,E ,Y }

from gecode import *

s = space()letters = s.intvars(8,0,9)S,E,N,D,M,O,T,Y = letterss.rel(M,IRT_NQ,0)s.rel(S,IRT_NQ,0)s.distinct(letters)C = [1000, 100, 10, 1,

1000, 100, 10, 1,-10000, -1000, -100, -10, -1]

X = [S,E,N,D,M,O,S,T,M,O,N,E,Y]

s.linear(C,X,IRT_EQ,0)money = s.intvar(0,99999)s.linear([10000,1000,100,10,1],[M,O,N,E,Y], IRT_EQ, money)s.maximize(money)s.branch(letters, INT_VAR_SIZE_MIN, INT_VAL_MIN)for s2 in s.search():

print(s2.val(money), s2.val(letters))

39

Page 182: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Strengths

I CP is excellent to explore highly constrained combinatorial spaces quickly

I Math programming is particulary good at deriving lower bounds

I LS is particualry good at derving upper bounds

40

Page 183: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Differences

I MILP modelsI impose modelling rules: linear inequalities and objectivesI emphasis on tightness and compactness of LP, strength of bounds

(remove dominated constraints)

I CP modelsI a large variety of algorithms communicating with each other: global

constraintsI more expressivenessI emphasis on exploiting substructres, include redundant constraints

41

Page 184: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

Resume

I Constraint Satisfaction Problem

I Modelling in CP

I Examples, Send More Money, Sudoku

42

Page 185: Constraint Programming Overview based on Examplesmarco/Teaching/AY2016-2017/DM841/Slides/dm841-p1-lec2.pdfConstraint Programming An Introduction by example Patrick Prosser with the

References

Anders T. and Miranda E.R. (2011). Constraint programming systems for modelingmusic theories and composition. ACM Comput. Surv., 43(4), pp. 30:1–30:38.

Hooker J.N. (2011). Hybrid modeling. In Hybrid Optimization, edited by P.M.Pardalos, P. van Hentenryck, and M. Milano, vol. 45 of Optimization and ItsApplications, pp. 11–62. Springer New York.

Smith B.M. (2006). Modelling. In Handbook of Constraint Programming, edited byF. Rossi, P. van Beek, and T. Walsh, chap. 11, pp. 377–406. Elsevier.

Williams H. and Yan H. (2001). Representations of the all_different predicate ofconstraint satisfaction in integer programming. INFORMS Journal onComputing, 13(2), pp. 96–103.

43


Recommended