Top Banner
CP2014: Teaching CP
97

Teaching Constraint Programming, Patrick Prosser

Dec 18, 2014

Download

Education

Pierre Schaus

Teaching Constraint Programming
Patrick Prosser
Invited Talk CP2014 Lyon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Teaching Constraint Programming,  Patrick Prosser

CP2014: Teaching CP

Page 2: Teaching Constraint Programming,  Patrick Prosser
Page 3: Teaching Constraint Programming,  Patrick Prosser
Page 4: Teaching Constraint Programming,  Patrick Prosser
Page 5: Teaching Constraint Programming,  Patrick Prosser

Context

• Final Year • 120 credits (1 credit approx 10 hours)

• 30 credit project • 4 modules in semester 1 • 4 modules in semester 2

• A module is 10 credits • 1 compulsory module on Prof Issues

• Students select 4 modules from 8 in each semester • There is competition between staff for students • Students make choice after 2nd week • First lectures have to be as attractive as possible • CP(M)

• 30 lectures • 1 short assessed exercise (5%) • 1 long(ish) assessed exercise (15%) • Exam (80%)

• Students are motivated by “marks” ☹

Page 6: Teaching Constraint Programming,  Patrick Prosser

How  not  to  do  it

Page 7: Teaching Constraint Programming,  Patrick Prosser

Course  outlineLecture  1

Page 8: Teaching Constraint Programming,  Patrick Prosser

Variables  and  constraints,  and  how  to  represent  themLecture  2

Page 9: Teaching Constraint Programming,  Patrick Prosser

Backtracking  search,    implementationLectures  3  to  5

Page 10: Teaching Constraint Programming,  Patrick Prosser

A  bit  on  heuristicsLecture  6

Page 11: Teaching Constraint Programming,  Patrick Prosser

A  wee  bit  of  theory  (levels  of  consistency  etc)Lecture  7

Page 12: Teaching Constraint Programming,  Patrick Prosser

A  tiny  wee  bit  more  theory  (dual  and  hidden  variables)Lecture  8

Page 13: Teaching Constraint Programming,  Patrick Prosser

Ready  to  write  our  1st  constraint  programWeek  4

Page 14: Teaching Constraint Programming,  Patrick Prosser

Wooooosh!

Page 15: Teaching Constraint Programming,  Patrick Prosser

…  how  I  do  it  now

Page 16: Teaching Constraint Programming,  Patrick Prosser

Lecture  1

Page 17: Teaching Constraint Programming,  Patrick Prosser

Lecture  1

Stolen: BMS, TW, JCB

Page 18: Teaching Constraint Programming,  Patrick Prosser

Lecture  1

Handed out on paper, given 8 minutes, discussion …

Page 19: Teaching Constraint Programming,  Patrick Prosser

Lecture  2

Just  do  it!

Page 20: Teaching Constraint Programming,  Patrick Prosser

How many dominos are there?Lecture  2

Page 21: Teaching Constraint Programming,  Patrick Prosser

How many dominos are there?

How many pairs of numbers are there of the form

0 ≤ x ≤ y ≤ 6

Lecture  2

Page 22: Teaching Constraint Programming,  Patrick Prosser

Lecture  2

Page 23: Teaching Constraint Programming,  Patrick Prosser

Compile & Run

Lecture  2

Page 24: Teaching Constraint Programming,  Patrick Prosser

Lecture  2

Page 25: Teaching Constraint Programming,  Patrick Prosser

Stolen: BMS

Lecture  2

Page 26: Teaching Constraint Programming,  Patrick Prosser

Lecture  2

Page 27: Teaching Constraint Programming,  Patrick Prosser

Compile & Run

Lecture  2

Page 28: Teaching Constraint Programming,  Patrick Prosser

An exam question from AF2

Lecture  2

Page 29: Teaching Constraint Programming,  Patrick Prosser

Lecture  2

Page 30: Teaching Constraint Programming,  Patrick Prosser

Compile & Run

Lecture  2

Page 31: Teaching Constraint Programming,  Patrick Prosser

1st  observations  &  questions

• How do you know “How not to do it”? • Why are you using choco (jchoco I call it)? • Do you steal everything?

• do you tell the students you deal in stolen goods? • have you no sense of shame?

• You get students to use technology they don’t understand! • We would NEVER EVER do that!

• You write code in the lecture? Edit compile AND run? • what if something goes wrong? You’d look stupid, right?

• You put all of the code on the web?

Page 32: Teaching Constraint Programming,  Patrick Prosser

CPM!

Page 33: Teaching Constraint Programming,  Patrick Prosser
Page 34: Teaching Constraint Programming,  Patrick Prosser

search

Page 35: Teaching Constraint Programming,  Patrick Prosser
Page 36: Teaching Constraint Programming,  Patrick Prosser

Opening up the hood: part 1, search

So how does CP work?

BT: Chronological BackTracking !Thrashing! !FC: forward checking !Thrashing! !CBJ: Conflict-directed BackJumping !Thrashing!

Page 37: Teaching Constraint Programming,  Patrick Prosser

consistency

Page 38: Teaching Constraint Programming,  Patrick Prosser
Page 39: Teaching Constraint Programming,  Patrick Prosser

Opening up the hood: part 2, arc-consistency

So how does CP work?

Arc-consistency Definition (aka 2-consistency) Covered 1-2-3-Consistency !Algorithms AC3 AC4/6/2001 AC5

Is AC a decision procedure?

Page 40: Teaching Constraint Programming,  Patrick Prosser

modelling

Page 41: Teaching Constraint Programming,  Patrick Prosser

We modelled some problems

number partitioning

jobshop schedulingmagic square

n-queens

bin packingknight’s tour

Ramsey number

Orthogonal Latin Squares

Crystal Maze

graph colouring

m-queens

Talent Scheduling

Crossword Puzzle

Page 42: Teaching Constraint Programming,  Patrick Prosser

What are

the d

ecisio

n vari

ables?

Are there any symmetries?

Is there a variable/value ordering heuristic?

Is there another model?

We modelled some problems

How does search go?

Redundant constr

aints?

Dual or Hidden Variable encoding?

Page 43: Teaching Constraint Programming,  Patrick Prosser

phase transition phenomena

Page 44: Teaching Constraint Programming,  Patrick Prosser

Where are the hard problems?

Constrainedness? Who cares?

In optimisation, as a heuristic, in solving problems

Page 45: Teaching Constraint Programming,  Patrick Prosser

heuristics

Page 46: Teaching Constraint Programming,  Patrick Prosser

Variable and value ordering heuristics

Static, topological properties !Fail-first, and it’s realisation !Constrainedness !Dual viewpoint heuristics !Promise !Regret !Domain Specific (example JSSP slack-based, Warnsdorff)

Page 47: Teaching Constraint Programming,  Patrick Prosser

specialised constraints

Page 48: Teaching Constraint Programming,  Patrick Prosser

specialised constraints

• max • sub-tour elimination • Knight’s tour • … • the glass box

Page 49: Teaching Constraint Programming,  Patrick Prosser

local search

Page 50: Teaching Constraint Programming,  Patrick Prosser

Search againLocal search (aka neighbourhood search)

HC, SA, TS, GLS, GA, ACO, …

Why we need them !!Problems in using them incompleteness, move operators, evaluation/fitness functions, tuning parameters, !Problems in using them in CP

aka meta-heuristics

Page 51: Teaching Constraint Programming,  Patrick Prosser

Search againLimited Discrepancy Search

lds

• motivation for LDS • when might we use it? • when should we not use it?

Page 52: Teaching Constraint Programming,  Patrick Prosser

change of representation

Page 53: Teaching Constraint Programming,  Patrick Prosser

dual and hidden variable encoding

change of representation

Page 54: Teaching Constraint Programming,  Patrick Prosser

Levels of consistency

Page 55: Teaching Constraint Programming,  Patrick Prosser

• Node Consistency (NC) • Arc-consistency (AC) • Path Consistency (PC) • Generalised arc-consistency (GAC) • Bounds consistency • Inverse Path Consistency (IPC aka PIC) • Singleton Arc-consistency (SAC) • … and others

consistency

Page 56: Teaching Constraint Programming,  Patrick Prosser

Mechanics of choco

Page 57: Teaching Constraint Programming,  Patrick Prosser

Mechanics of choco

Page 58: Teaching Constraint Programming,  Patrick Prosser

Mechanics of choco

Page 59: Teaching Constraint Programming,  Patrick Prosser

CP for a design problem

Using CP as an intelligent data base !Not solving a problem, but using CP as an “active” representation !Explanations via quickXplain

Page 60: Teaching Constraint Programming,  Patrick Prosser

theory

• csp <v,c,d> and it’s complexity • search (bt, fc, cbj, …) • thrashing • arc-consistency • levels of consistency • heuristics • local search & lds • dual & hidden variables • sat • phase transition phenomena & constrainedness

summary

Page 61: Teaching Constraint Programming,  Patrick Prosser

practice

number partitioning

jobshop schedulingmagic square

n-queens

bin packingknight’s tour

Talent Scheduling Orthogonal Latin Squares

Crystal Maze

graph colouring

Modelling & Solving Problemssummary

Page 62: Teaching Constraint Programming,  Patrick Prosser

practice

• variables (enumerated/bound/setsVar) and their domains • constraints (neq, allDiff, ifOnlyIf, …) • decision variables • what propagation might take place • size of the encoding/model (how it scales with problem size) • heuristics (dynamic/static, variable/value) • size of the search/state space • how search goes (example was knight’s tour) • alternative models (dual? hidden? values as variables?) • optimisation (with maximise/minimise, seqn of decision problems) • dealing with conflicts (soft constraints & penalties) • symmetry breaking (ramsey, bin packing, …) • redundant constraints (magic square, MOLS …) • what will make problems hard to solve and what will make them easy?

Modelling & Solving Problemssummary

Page 63: Teaching Constraint Programming,  Patrick Prosser

Rhythm

Once  we  are  up  to  speed,  an  ideal  week  goes  like  this  !• lecture  on  new  theory  • a  new  problem  • Model  and  solve  

• “Performance  CP”  • Consider  alternative  models  • Symmetry  breaking  • When  do  problems  get  hard

Page 64: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problems

Page 65: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problems

The  usual  stuff  int  x  =  3;  int  y  =  5;  int  z  =  Math.max(x,y);

Page 66: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problems

The  usual  stuff  int  x  =  3;  int  y  =  5;  int  z  =  Math.max(x,y);

Our  stuff  Model  model            =  new  CPModel();  IntegerVariable  x  =  makeIntVar(“x”,0,3);  IntegerVariable  y  =  makeIntVar(“y”,0,5);  IntegerVariable  z  =  makeIntVar(“z”,0,4);  model.addConstraint(eq(z,max(x,y)));

Page 67: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problems

The  usual  stuff  int  x  =  3;  int  y  =  5;  int  z  =  Math.max(x,y);

Our  stuff  Model  model            =  new  CPModel();  IntegerVariable  x  =  makeIntVar(“x”,0,3);  IntegerVariable  y  =  makeIntVar(“y”,0,5);  IntegerVariable  z  =  makeIntVar(“z”,0,4);  model.addConstraint(eq(z,max(x,y)));

Breaking  the  mental  model  of  variables  in  a  programming  language

Page 68: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problems

The  usual  stuff  int  x  =  3;  int  y  =  5;  int  z  =  Math.max(x,y);

Our  stuff  Model  model            =  new  CPModel();  IntegerVariable  x  =  makeIntVar(“x”,0,3);  IntegerVariable  y  =  makeIntVar(“y”,0,5);  IntegerVariable  z  =  makeIntVar(“z”,0,4);  model.addConstraint(eq(z,max(x,y)));

• Post  x  >  0  and  propagate  • Post  z  >  0  and  propagate  • Post  z  <  3  and  propagate

Page 69: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problems

Almost  anything  to  do  with  implication  is  mind  blowing  (or  maybe  I  am  a  bad  teacher)

Page 70: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problemsarrays

Page 71: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problemsarrays

Pick  off  the  ith  row:  easy

Page 72: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problemsarrays

Pick  off  the  diagonal:  easy

Page 73: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problemsarrays

Pick  off  the  ith  column:  hard

Page 74: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problemsarrays

Pick  off  the  diagonal:  hard

Page 75: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problems

Code  comprehension

Page 76: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problemsCode  comprehension

Given  n,  what  does  f1  return?  Suggest  a  name  for  f1.

Page 77: Teaching Constraint Programming,  Patrick Prosser

Where are the really hard problemsCode  comprehension

Given  n  and  r,  what  does  f2  return?  Suggest  a  name  for  f2?

Page 78: Teaching Constraint Programming,  Patrick Prosser

Assessed  exercises

Page 79: Teaching Constraint Programming,  Patrick Prosser

Assessed  exercises

• 1st  exercise  is  sudoku  (5%)  • To  get  them  to  download  and  use  choco

Page 80: Teaching Constraint Programming,  Patrick Prosser
Page 81: Teaching Constraint Programming,  Patrick Prosser
Page 82: Teaching Constraint Programming,  Patrick Prosser

1D Sudoku!

Page 83: Teaching Constraint Programming,  Patrick Prosser
Page 84: Teaching Constraint Programming,  Patrick Prosser
Page 85: Teaching Constraint Programming,  Patrick Prosser

Read in 9 rows of 9 numbers Jeez. If I knew it was this hard I never would have attempted it

Page 86: Teaching Constraint Programming,  Patrick Prosser
Page 87: Teaching Constraint Programming,  Patrick Prosser

The floggings will continue until morale

improves

Page 88: Teaching Constraint Programming,  Patrick Prosser

Assessed  exercises

Exercise  2  (15%)    has  been    !• Car  sequencing  • Talent  scheduling  • Meeting  Scheduling  • Maximum  Clique  

Page 89: Teaching Constraint Programming,  Patrick Prosser

Assessed  exercises

Exercise  2  (15%)    has  been    !• Car  sequencing  • Talent  scheduling  • Meeting  Scheduling  • Maximum  Clique  

I  think  that  was  the  best  

Page 90: Teaching Constraint Programming,  Patrick Prosser

So?

Page 91: Teaching Constraint Programming,  Patrick Prosser

Jump in!

Page 92: Teaching Constraint Programming,  Patrick Prosser

Steal

Page 93: Teaching Constraint Programming,  Patrick Prosser

You can’t teach it all

Page 94: Teaching Constraint Programming,  Patrick Prosser

The hard bits might not be where you expect them to be.

Page 95: Teaching Constraint Programming,  Patrick Prosser

Have fun

Page 96: Teaching Constraint Programming,  Patrick Prosser

… and remember,

your students are not just your students

CP(M)CP(M)

CP(M)

CP(M

)

Page 97: Teaching Constraint Programming,  Patrick Prosser