lecture 1 - School of Computing Science

Download Report

Transcript lecture 1 - School of Computing Science

Logic & AR Summer School, 2002
Constraints and Search
Toby Walsh
Cork Constraint Computation Centre (4C)
[email protected]
Outline
• What is Constraint Computation?
– aka Constraint Programming (CP)
• How do we search for a solution?
– Useful not just for constraint programming
– But also for theorem proving, …
A Puzzle
• Place numbers 1 through 8 on nodes
– Each number appears exactly once
– No connected
?
nodes have
consecutive
numbers
?
You have
8 minutes!
?
?
?
?
?
?
Heuristic Search
Which nodes are hardest to number?
?
?
?
?
?
?
?
?
Heuristic Search
?
?
?
?
?
?
?
?
Heuristic Search
Which are the least constraining values to use?
?
?
?
?
?
?
?
?
Heuristic Search
Values 1 and 8
?
?
?
1
8
?
?
?
Heuristic Search
Values 1 and 8
?
?
?
1
8
?
?
Symmetry means we don’t need to consider: 8 1
?
Inference/propagation
?
?
?
1
8
?
?
?
We can now eliminate many values for other nodes
Inference/propagation
{1,2,3,4,5,6,7,8}
?
?
?
1
8
?
?
?
Inference/propagation
{2,3,4,5,6,7}
?
?
?
1
8
?
?
?
Inference/propagation
{3,4,5,6}
?
?
?
1
8
?
?
?
Inference/propagation
{3,4,5,6}
?
?
?
1
8
?
?
{3,4,5,6}
By symmetry
?
Inference/propagation
?
{3,4,5,6}
{1,2,3,4,5,6,7,8}
?
?
1
8
?
?
{3,4,5,6}
?
Inference/propagation
?
{3,4,5,6}
{2,3,4,5,6,7}
?
?
1
8
?
?
{3,4,5,6}
?
Inference/propagation
?
{3,4,5,6}
{3,4,5,6}
?
?
1
8
?
?
{3,4,5,6}
?
Inference/propagation
?
By symmetry
{3,4,5,6}
{3,4,5,6}
?
?
1
8
?
?
{3,4,5,6}
{3,4,5,6}
?
Inference/propagation
?
{3,4,5,6}
{3,4,5,6}
?
?
1
8
?
{2,3,4,5,6}
{3,4,5,6,7}
?
?
{3,4,5,6}
{3,4,5,6}
Inference/propagation
?
{3,4,5,6}
{3,4,5,6}
?
?
1
8
?
{2,3,4,5,6}
{3,4,5,6,7}
?
?
{3,4,5,6}
{3,4,5,6}
Value 2 and 7 are left in just one variable domain each
Inference/propagation
7
{3,4,5,6}
{3,4,5,6}
?
?
1
8
{2,3,4,5,6}
{3,4,5,6,7}
And propagate …
2
?
?
{3,4,5,6}
{3,4,5,6}
Inference/propagation
{3,4,5}
7
{3,4,5,6}
?
?
1
8
{2,3,4,5,6}
{3,4,5,6,7}
?
{3,4,5}
And propagate …
2
?
{3,4,5,6}
Inference/propagation
{3,4,5}
7
{4,5,6}
?
?
1
8
{2,3,4,5,6}
{3,4,5,6,7}
?
{3,4,5}
And propagate …
2
?
{4,5,6}
Inference/propagation
{3,4,5}
7
{4,5,6}
?
?
1
8
?
?
{3,4,5}
{4,5,6}
Guess a value, but be prepared to backtrack …
2
Inference/propagation
{3,4,5}
7
{4,5,6}
3
?
1
8
?
?
{3,4,5}
{4,5,6}
Guess a value, but be prepared to backtrack …
2
Inference/propagation
{3,4,5}
7
3
?
1
8
?
?
{3,4,5}
And propagate …
{4,5,6}
{4,5,6}
2
Inference/propagation
{5,6}
7
3
?
1
8
?
?
{4,5}
And propagate …
{4,5,6}
2
Inference/propagation
{5,6}
7
3
?
1
8
?
?
{4,5}
Guess another value …
{4,5,6}
2
Inference/propagation
7
3
5
1
8
?
?
{4,5}
Guess another value …
{4,5,6}
2
Inference/propagation
7
3
5
1
8
?
?
{4,5}
And propagate …
{4,5,6}
2
Inference/propagation
7
{4}
And propagate …
3
5
1
8
?
?
{4,6}
2
Inference/propagation
7
{4}
3
5
1
8
4
?
{4,6}
One node has only a single value left …
2
Inference/propagation
7
3
5
1
8
4
6
{6}
2
Solution
7
3
5
1
8
4
6
2
The Core of Constraint
Computation
• Modelling
– Deciding on variables/domains/constraints
•
•
•
•
Heuristic Search
Inference/Propagation
Symmetry
Backtracking
A Commercial Reality
• First-tier software vendors use CP technology
Supply chain management
• The puzzle is actually a hard problem
– NP-complete
• Can apply the same technology to core
supply chain management problems
– Vehicle routing
– Scheduling
– Warehouse location
Example: Vehicle Routing
[10 20]
10
[10 20]
15
10
Depot
Example: Vehicle Routing
[10 20]
10
[10 20]
15
10
Depot
Example: Vehicle Routing
[15 15]
10
[10 20]
15
10
Depot
Example: Vehicle Routing
[10 20]
[20
10
[10 10]
20]
15
10
Depot
Constraint programming
• Ongoing “dream” of declarative
programming
– State the constraints
– Solver finds a solution
• Paradigm of choice for many hard
combinatorial problems
– Scheduling, assignment, routing, …
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
Constraints are everywhere!
• No meetings before 10am
• Network traffic < 100
Gbytes/sec
• PCB width < 21cm
• Salary > 45k Euros
…
Constraint satisfaction
• Constraint satisfaction problem (CSP) is a triple
<V,D,C> where:
– V is set of variables
– Each X in V has set of values, D_X
• Usually assume finite domain
• {true,false}, {red,blue,green}, [0,10], …
– C is set of constraints
Goal: find assignment of values to variables to satisfy all the
constraints
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
…
Example CSP
• Course timetabling
– Variable for each course
X1, X2 ..
– Domain are possible times for
course
Wed9am, Fri10am, ..
– 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
• Find assignment of vals to vars that:
– Satisfies constraints
– Maximizes (minimizes) objective
• Often solved as sequence of satisfaction problems
Profit > 0, Profit > Ans1, Profit > Ans2, …
Constraint programming v.
Constraint 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, …
Constraints
• Constraints are tuples <S,R> where
– S is the scope, [X1,X2, … Xm]
• list of variables to which constraint applies
– R is relation specifying allowed values (goods)
• Subset of D_X1 x D_X2 x … x D_Xm
• May be specified intensionally or extensionally
Constraints
• Extensional specification
– List of goods (or for tight constraints, nogoods)
• Intensional specification
– X1 =/= X2
– 5*X1 + 6*X2 < X3
– alldifferent([X1,X2,X3,X4]), …
Constraint tightness
• Informally, tight
constraints admit few
tuples
– E.g. on 0/1 vars
X1 =/= X2 is loose as half
tuples satisfy
X1+X2+X3+X4+X5 <= 1 is
tight as only 5 out of 32
tuples satisfy
• More formal definition
later
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!
Constraint graph
• Nodes = variables
• Edge between 2 nodes iff
constraint between 2
associated variables
– Few constraints, sparse
constraint graph
– Lots of constraints, dense
constraint graph
Some non-binary examples
• Timetabling
– Variables: Lecture1, Lecture2, …
– Values: time1, time2, …
– Constraint that lectures taught by same lecturer
do not conflict:
alldifferent(Lecture1,Lecture5,…).
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).
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!
Modelling with non-binary
constraints
• Benefits include:
– Compact, declarative specifications
(discussed now)
– Efficient constraint propagation
(discussed next time)
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).
Modelling with non-binary
constraints
• It’s possible but it’s not very pleasant!
• Nor is it very compact
– alldifferent([X1,…Xn]) expands into n(n-1)/2 binary not-equals
constraints, Xi #\= Xj
– one non-binary constraint or O(n^2) binary constraints?
And there exist very efficient algorithms for reasoning efficiently
with many specialized non-binary constraints
Constraint solvers
• Two main approaches
– Systematic, tree search algorithms
– Local search or repair based procedures
• Other more exotic possibilities
– Hybrid algorithms
– Quantum algorithms
Systematic solvers
• Tree search
– Assign value to variable
– Deduce values that must be removed from future/unassigned
variables
• Constraint propagation
– 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, …
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
To remember: CSP
• Constraint satisfaction problem (CSP) is a triple
<V,D,C> where:
– V is set of variables
– Each variable X in V has a (finite) domain of values,
D_X
– C is set of constraints
Values for sets of variables which are not allowed
• Goal: find assignment of values to variables to
satisfy all the constraints