Constraint Programming: Toby Walsh Unsw and Nicta
Constraint Programming: Toby Walsh Unsw and Nicta
Constraint Programming: Toby Walsh Unsw and Nicta
Toby Walsh
UNSW and NICTA
Overview
Constraint satisfaction
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),
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
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
Example CSP
Sudoku
Column
AllDifferent(X1,X10,..)
Small square
AllDifferent(X1,..,X3,X10,..X12,..)
Example CSP
Course timetabling
Variable for each course
X1, X2 ..
Constraints:
X1 \= Wed9am
Capacity constraints:
atmost(3,[X1,X2..],Wed9am)
Lecturer constraints:
alldifferent([X1,X5,])
Constraint optimization
CSP + objective function
E.g. objective is Profit = Income - Costs
Constraints
Constraints
Scope
list of variables to which constraint applies
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!
onediff(El,[]).
onediff(El,[Head|Tail]):El #\= Head,
onediff(El,Tail).
Constraint solvers
Two main approaches
Systematic, tree search algorithms
Local search or repair based procedures
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
Number of choices
Variable to assign next, value to assign
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
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
Enforcing arc-consistency
Remove all values that are not AC
(i.e. have no support)
Enforcing arc-consistency
Properties of AC
Unique maximal AC
subproblem
Or problem is unsatisfiable
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}
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}
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}
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}
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}
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}
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}
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}
GAC on alldifferent
AllDifferent on 9th col
{6,7}
{1,2,4}
{8}
{9}
{5,7}
{4,5}
{1,7}
{3}
{4,5,6}
GAC on alldifferent
AllDifferent on 6th small
square
{6,7}
{1,2,4}
{8}
{9}
{5,7}
{4,5}
{1,7}
{3}
{4,6}
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
(i,j)-consistency
Generalization of arc-consistency
AC = (1,1)-consistency
Path-consistency = (2,1)-consistency
Strong path-consistency = AC + PC
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)
{1,2} X1
\=
{2,3}
X2
\=
\=
{2,3}
X3
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
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)
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!)
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
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)
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
Assign Row2=col4
Queen on 2nd row placed
on 4th column
Assign Row2=col4
Queen on 2nd row placed
on 4th 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
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
Row2 is forced
Only one value left
Forward checking prunes
domains of future
variables
Row2 is forced
Only one value left
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
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
Surprisingly effective
Especially as n increases!
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