constraint programming toby walsh unsw and nicta

62
Constraint Programming Toby Walsh UNSW and NICTA

Upload: marissa-coleman

Post on 01-Apr-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Constraint Programming Toby Walsh UNSW and NICTA

Constraint Programming

Toby WalshUNSW and NICTA

Page 2: Constraint Programming Toby Walsh UNSW and NICTA

Overview

• Constraint satisfaction– What is a constraint satisfaction problem? (aka “CSP”)– Constraint propagation (aka “inference”)– Search algorithms for solving CSPs

• Modelling problems using CSPs– Some case studies– Auxiliary variables– Symmetry– Implied constraints– Dual models

Page 3: Constraint Programming Toby Walsh UNSW and NICTA

Resources

• Course links– http://www.cse.unsw.edu.au/~tw/cp.html

• Benchmark problems– www.csplib.org

• Constraints solvers– Logic programming languages: ECLIPSE, BProlog– Functional languages: FaCiLe (OCaml), …– Imperative languages: Choco (Java), …

Page 4: Constraint Programming Toby Walsh UNSW and NICTA

Constraint programming

• Ongoing “dream” of declarative programming– State the constraints

• No two exams at the same time• Only 5 shifts in one week• Parcel picked up between 10am-noon

– Black box solver finds a solution• Just like magic!

• Paradigm of choice for many hard combinatorial problems– Scheduling, assignment, routing, …

Page 5: Constraint Programming Toby Walsh UNSW and NICTA

Constraints are everywhere!

• No meetings before 10am• Network traffic < 100

Gbytes/sec• PCB width < 21cm• Salary > 45k Euros• No two exams at the same time• Only 5 shifts in one week• Parcel picked up before 10am…

Page 6: Constraint Programming Toby Walsh UNSW and NICTA

Constraint satisfaction

• Constraint satisfaction problem (CSP) consists of:– Set of variables– Each variable has set of values

• Usually assume finite domain• {true,false}, {red,blue,green}, [0,10], …

– Set of constraints

• Goal– Find assignment of values to variables to satisfy all the

constraints

Page 7: Constraint Programming Toby Walsh UNSW and NICTA

Example CSP

• Sudoku– Variable for each square– Domain: numbers 1 to 9– Constraints– Row:

• AllDifferent(X1,..X9)• AllDifferent(X10,..X18)• …

– Column• AllDifferent(X1,X10,..)• …

– Small square• AllDifferent(X1,..,X3,X10,..X12,..)

Page 8: Constraint Programming Toby Walsh UNSW and NICTA

Example CSP

• Course timetabling– Variable for each course

X1, X2 ..

– Domain are possible times for course

Wed9am, Fri10am, ..

– Constraints:X1 \= Wed9amCapacity constraints:

atmost(3,[X1,X2..],Wed9am)Lecturer constraints:

alldifferent([X1,X5,…])

Page 9: Constraint Programming Toby Walsh UNSW and NICTA

Constraint optimization

• CSP + objective function– E.g. objective is Profit = Income - Costs

• Find assignment of values to variables that:– Satisfies constraints– Maximizes (minimizes) objective

• Often solved as sequence of satisfaction problems

Profit > 0, Profit > Ans1, Profit > Ans2, …

Page 10: Constraint Programming Toby Walsh UNSW and NICTA

Constraint programming v. Logic programming

• Constraints declaratively specify problem– Logic programming natural approach

Assert constraints, call “labelling” strategy (backtracking search predicate)

• But can also define constraint satisfaction or optimization within an imperative of functional language• Popular toolkits in C++, Java, CAML, …

Page 11: Constraint Programming Toby Walsh UNSW and NICTA

Constraints

• Constraints– Scope

• list of variables to which constraint applies

– Relation specifying allowed values (goods)• Extensionally specified:

– (X=red, Y=blue) or (X=red, Y=green) or – (X=blue, Y=red) or (X=blue, Y=green) or– (X=green, Y=red) or (X=green, Y=blue)

• Intensionally specified:– X =/= Y– X + 2Y - Z < 5– Alldifferent(X,Y,Z)– AtLeast(1,[X,Y,Z],{red})

Page 12: Constraint Programming Toby Walsh UNSW and NICTA

Binary v non-binary

• Binary constraint– Scope covers 2 variables– E.g. not-equals constraint: X1 =/= X2.– E.g. ordering constraint: X1 < X2

• Non-binary constraint– Scope covers 3 or more variables– E.g. alldifferent(X1,X2,X3).– E.g. tour(X1,X2,X3,X4).

“Non-binary constraints” usually do not include unary constraints!

Page 13: Constraint Programming Toby Walsh UNSW and NICTA

Some non-binary examples

• Timetabling– Variables: Lecture1, Lecture2, …– Values: time1, time2, …– Constraint that lectures taught by same lecturer do not

conflict:alldifferent(Lecture1,Lecture5,…).

Page 14: Constraint Programming Toby Walsh UNSW and NICTA

Some non-binary examples

• Scheduling– Variables: Job1. Job2, …– Values: machine1, machine2, …– Constraint on number of jobs on each machine:

atmost(2,[Job1,Job2,…],machine1),atmost(1,[Job1,Job2,…],machine2).

Page 15: Constraint Programming Toby Walsh UNSW and NICTA

Why use non-binary constraints?

• Binary constraints are NP-complete– Any non-binary constraint can be represented using

binary constraints– E.g. alldifferent(X1,X2,X3) is “equivalent” to X1 =/= X2,

X1 =/= X3, X2 =/= X3

• In theory therefore they’re not needed– But in practice, they are!

Page 16: Constraint Programming Toby Walsh UNSW and NICTA

Modelling with non-binary constraints

• Benefits include:– Compact, declarative specifications

(discussed next)

– Efficient constraint propagation(discussed second)

Page 17: Constraint Programming Toby Walsh UNSW and NICTA

Modelling with non-binary constraints

Consider writing your own alldifferent constraint:

alldifferent([]).alldifferent([Head|Tail]):-

onediff(Head,Tail),alldifferent(Tail).

onediff(El,[]).

onediff(El,[Head|Tail]):-El #\= Head,

onediff(El,Tail).

Page 18: Constraint Programming Toby Walsh UNSW and NICTA

Constraint solvers

• Two main approaches– Systematic, tree search algorithms– Local search or repair based procedures

• Other more exotic possibilities– Hybrid algorithms– Quantum algorithms

Page 19: Constraint Programming Toby Walsh UNSW and NICTA

Systematic solvers

• Tree search– Assign value to variable– Deduce values that must be removed from

future/unassigned variables• Propagation to ensure some level of consistency

– If future variable has no values, backtrack else repeat

• Number of choices– Variable to assign next, value to assign

Some important refinements like nogood learning, non-chronological backtracking, …

Page 20: Constraint Programming Toby Walsh UNSW and NICTA

Local search

• Repair based methods– Generate complete assignment– Change value to some variable in a violated constraint

• Number of choices– Violated constraint, variable within it, …

Unable to exploit powerful constraint propagation techniques

Page 21: Constraint Programming Toby Walsh UNSW and NICTA

Constraint propagation

• Arc-consistency (AC)– A binary constraint r(X1,X2) is AC iff

for every value for X1, there is a consistent value (often called support) for X2 and vice versa

– A problem is AC iff every constraint is AC

Page 22: Constraint Programming Toby Walsh UNSW and NICTA

Enforcing arc-consistency

• Remove all values that are not AC (i.e. have no support)

• May remove support from other values (often queue based algorithm)

• Best AC algorithms (AC7, AC-2000) run in O(ed^2)– Optimal if we know nothing else about the constraints

Page 23: Constraint Programming Toby Walsh UNSW and NICTA

Enforcing arc-consistency

• Consider 1st column– Binary not equals

constraints:– X1=/=X10, X1=/=X19, ..

• Consider 8th row– Look at domain of the 1st

variable

Page 24: Constraint Programming Toby Walsh UNSW and NICTA

Properties of AC

• Unique maximal AC subproblem– Or problem is unsatisfiable

• Enforcing AC can process constraints in any order– But order does affect

(average-case) efficiency

Page 25: Constraint Programming Toby Walsh UNSW and NICTA

Non-binary constraint propagation

• Most popular is generalized arc-consistency (GAC)– A non-binary constraint is GAC iff for every value for a

variable there are consistent values for all other variables in the constraint

– We can again prune values that are not supported

• GAC = AC on binary constraints

Page 26: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 6th row – {1,2,4,5,6,7}– {1,2,4,5,6,7}– {8}– {9}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {3}– {1,2,4,5,6,7}

Page 27: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 1st col– {6,7}– {1,2,4,5,6,7}– {8}– {9}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {3}– {1,2,4,5,6,7}

Page 28: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 2nd col– {6,7}– {1,2,4,5}– {8}– {9}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {3}– {1,2,4,5,6,7}

Page 29: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 4th small square– {6,7}– {1,2,4}– {8}– {9}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {3}– {1,2,4,5,6,7}

Page 30: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 5th col– {6,7}– {1,2,4}– {8}– {9}– {1,2,5,6,7}– {1,2,4,5,6,7}– {1,2,4,5,6,7}– {3}– {1,2,4,5,6,7}

Page 31: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 6th col– {6,7}– {1,2,4}– {8}– {9}– {1,2,5,6,7}– {1,4,5,7}– {1,2,4,5,6,7}– {3}– {1,2,4,5,6,7}

Page 32: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 5th small square– {6,7}– {1,2,4}– {8}– {9}– {5,7}– {4,5}– {1,2,4,5,6,7}– {3}– {1,2,4,5,6,7}

Page 33: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 7th col– {6,7}– {1,2,4}– {8}– {9}– {5,7}– {4,5}– {1,7}– {3}– {1,2,4,5,6,7}

Page 34: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 9th col– {6,7}– {1,2,4}– {8}– {9}– {5,7}– {4,5}– {1,7}– {3}– {4,5,6}

Page 35: Constraint Programming Toby Walsh UNSW and NICTA

GAC on alldifferent

• AllDifferent on 6th small square– {6,7}– {1,2,4}– {8}– {9}– {5,7}– {4,5}– {1,7}– {3}– {4,6}

Page 36: Constraint Programming Toby Walsh UNSW and NICTA

Enforcing GAC

• Enforcing GAC is expensive in general– GAC schema is O(d^k)

On k-ary constraint on vars with domains of size d

• Trick is to exploit semantics of constraints– Regin’s all-different algorithm– Achieves GAC in just O(k^2 d^2)

On k-ary all different constraint with domains of size dBased on finding matching in “value graph”

Page 37: Constraint Programming Toby Walsh UNSW and NICTA

Other types of constraint propagation

• (i,j)-consistency – Non-empty domains– Any consistent instantiation for i variables can be

extended to j others

• Describes many different consistency techniques

Page 38: Constraint Programming Toby Walsh UNSW and NICTA

(i,j)-consistency

• Generalization of arc-consistency– AC = (1,1)-consistency– Path-consistency = (2,1)-consistency

• Strong path-consistency = AC + PC

– Path inverse consistency = (1,2)-consistency

Page 39: Constraint Programming Toby Walsh UNSW and NICTA

Enforcing (i,j)-consistency

• problem is (1,1)-consistent (AC)

• BUT is not (2,1)-consistent (PC)– X1=2, X2=3 cannot be extended to

X3– Need to add constraints:

not(X1=2 & X2=3)not(X1=2 & X3=3)

• Nor is it (1,2)-consistent (PIC)– X1=2 cannot be extended to X2 &

X3 (so needs to be deleted)

{1,2}

{2,3} {2,3}

\=

\=

X1

X3X2

\=

Page 40: Constraint Programming Toby Walsh UNSW and NICTA

Other types of constraint propagation

• Bounds consistency (BC)– With ordered domains– Enforce AC just on max/min elements – Used often for arithmetic constraints (eg linear

inequalities)– GAC is intractable on a sum constraints (= subset sum)

Page 41: Constraint Programming Toby Walsh UNSW and NICTA

Maintaining a local consistency property

• Tree search– Assign value to variable– Enforce some level of local consistency

• Remove values/add new constraints

– If any future variable has no values, backtrack else repeat

• Two popular algorithms– Maintaining arc-consistency (MAC)

• Make the whole problem arc-consistent

– Forward checking (very restricted form of AC maintained)

Page 42: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking

• Binary constraints (FC)– Make constraints involving current variable and one

future variable arc-consistent– No need to look at any other constraints!

• Non-binary constraints– Several choices as to how to do forward checking

Page 43: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking with non-binary constraints

nFC0 makes AC only those k-ary constraints with k-1 variables setnFC1 applies one pass of AC on constraints and projections involving current var and one future varnFC2 applies one pass of GAC on constraints involving current var and at least one future varnFC3 enforces GAC on this setnFC4 applies one pass of GAC on constraints involving at least one past and one future varnFC5 enforces GAC on this set

Page 44: Constraint Programming Toby Walsh UNSW and NICTA

n-queens problem

• Put n-queens on a n by n chess board

• Constraints:– No queen attacks another

• Here n=8• Studied by Carl Gauss and

others (1850)

Page 45: Constraint Programming Toby Walsh UNSW and NICTA

n-queens problem

• ILP formulation– Xij = 1 iff (i,j) square has

queen on it– Row attack constraints– Col attack constraints– Diag attack constraints– Objective function (can

only get n queens on board!)

Page 46: Constraint Programming Toby Walsh UNSW and NICTA

n-queens problem

• CSP model– One variable for each row– Value is position of queen

on that row– Constraints

• Col attack• Diag attack

– Here n=4

Page 47: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col1– Queen on 1st row placed

on 1st column– Forward check prunes

domains of future variables

Page 48: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col1– Queen on 1st row placed

on 1st column– Forward checking prunes

domains of future variables

• Assign Row2=col3– Queen on 2nd row placed

on 3rd column– Forward checking prunes

domains of future variables

– Row3 has domain wipeout (no values left)

Page 49: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col1– Queen on 1st row placed

on 1st column– Forward checking prunes

domains of future variables

• Assign Row2=col3– Queen on 2nd row placed

on 3rd column– Forward checking prunes

domains of future variables– Row3 has domain wipeout

(no values left)– Backtrack to last

assignment and try some other value

Page 50: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col1– Queen on 1st row placed

on 1st column– Forward checking prunes

domains of future variables

• Assign Row2=col4– Queen on 2nd row placed

on 4th column

Page 51: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col1– Queen on 1st row placed

on 1st column– Forward checking prunes

domains of future variables

• Assign Row2=col4– Queen on 2nd row placed

on 4th column– Forward checking prunes

domains of future variables

Page 52: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col1– Queen on 1st row placed

on 1st column– Forward checking prunes

domains of future variables

• Assign Row2=col4– Queen on 2nd row placed

on 4th column– Forward checking prunes

domains of future variables

• Row4 is forced– Only one value left

Page 53: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col1– Queen on 1st row placed on

1st column– Forward checking prunes

domains of future variables

• Assign Row2=col4– Queen on 2nd row placed

on 4th column– Forward checking prunes

domains of future variables

• Row4 is forced– Only one value left

• Row3 has domain wipeout– Backtrack to next “choice”

Page 54: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col1– Queen on 1st row placed

on 1st column

Page 55: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col2– Queen on 1st row placed

on 2nd column

Page 56: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col2– Queen on 1st row placed

on 2nd column– Forward checking prunes

domains of future variables

Page 57: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col2– Queen on 1st row placed

on 2nd column– Forward checking prunes

domains of future variables

• Row2 is forced– Only one value left– Forward checking prunes

domains of future variables

Page 58: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col2– Queen on 1st row placed

on 2nd column– Forward checking prunes

domains of future variables

• Row2 is forced– Only one value left– Forward checking prunes

domains of future variables

Page 59: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col2– Queen on 1st row placed

on 2nd column– Forward checking prunes

domains of future variables

• Row2 is forced– Only one value left– Forward checking prunes

domains of future variables

• Row3 is forced– Only one value left– Forward checking prunes

domain of last variable

Page 60: Constraint Programming Toby Walsh UNSW and NICTA

Forward checking on n-queens problem

• Assign Row1=col2– Queen on 1st row placed on

2nd column– Forward checking prunes

domains of future variables

• Row2 is forced– Only one value left– Forward checking prunes

domains of future variables

• Row3 is forced– Only one value left– Forward checking prunes

domain of last variable

• Row4 is forced

Page 61: Constraint Programming Toby Walsh UNSW and NICTA

Local search for n-queens problem

• Throw n queens on chessboard– Move a queen to reduce number of attacks– Repeat until no queen is attacked

• Surprisingly effective– Especially as n increases!

Page 62: Constraint Programming Toby Walsh UNSW and NICTA

Summary

• Constraint solving– Tree search

• Try variable assignment• Be prepared to backtrack

on this choice if it does not lead to a solution

• Use propagation (inference) to prune domains of remaining variables

– Local search

• Tomorrow– Modelling problems using

constraint programming