Date post: | 14-Dec-2015 |
Category: |
Documents |
Upload: | jaylin-oaks |
View: | 232 times |
Download: | 2 times |
Outline
• Introduction
• Constraint propagation
• Backtracking search
• Global constraints
• Symmetry
• Modeling
Some additional resources
“Constraint-Based Local Search”, by Pascal Van Hentenryck and Laurent Michel
Integrated Methods for Optimization, by John N. Hooker
“Constraint Processing,” by Rina Dechter
“Principles of Constraint Programming,” by Krzysztof Apt
“Programming with Constraints,” by Kim Marriott, Peter J. Stuckey
“Handbook of Constraint Programming,” edited by F. Rossi, P. van Beek, T. Walsh
Outline
• Introduction
• Constraint propagation
• Backtracking search
• Global constraints
• Symmetry
• Modeling
What is constraint programming?
• Idea: Solve a problem by stating constraints on acceptable solutions
• Advantages:
• constraints often a natural part of problems
• especially true of difficult combinatorial problems
• once problem is modeled using constraints, wide selection of solution techniques available
• Constraint programming is an active area of research
• draws on techniques from artificial intelligence, algorithms, databases, programming languages, and operations research
What is constraint programming?
• Constraint programming is similar to mathematical programming
• declarative
• user states the constraints
• general purpose constraint solver, often based on backtracking search, is used to solve the constraints
• Constraint programming is similar to computer programming
• extensible
• user-defined constraints
• allows user to program a strategy to search for a solution
What is constraint programming?
• Constraint programming is a problem-solving methodology
• Model problem
• Solve model
• specify in terms of constraints on acceptable solutions
• define/choose constraint model: variables, domains, constraints
• define/choose search algorithm
• define/choose heuristics
What is constraint programming?
• Constraint programming is a collection of core techniques
• Modeling
• deciding on variables/domains/constraints
• improving the efficiency of a model
• Solving
• local consistency
• constraint propagation
• global constraints
• search
• backtracking search
• hybrid methods
Place numbers 1 through 8 on nodes, where each number appears exactly once and no connected nodes have consecutive numbers
?
?
?
?
?
?
??
Acknowledgement: Patrick Prosser
Backtracking search
Which nodes are hardest to number?
?
?
?
?
?
?
??
Guess a value, but be prepared to backtrack
Inference/propagation
We can now eliminate many values for other nodes
?
1
?
?
8
?
??
{1,2,3,4,5,6,7,8}
Inference/propagation
?
1
?
?
8
?
??
{3,4,5,6}
{3,4,5,6}
{3,4,5,6}
{3,4,5,6}
{3,4,5,6,7} {2,3,4,5,6}
Inference/propagation
?
1
?
?
8
?
27
{3,4,5}
{3,4,5}
{4,5,6}
{4,5,6}
Guess a value, but be prepared to backtrack
Inference/propagation
3
1
?
?
8
?
27
{3,4,5}
{4,5,6}
{4,5,6}
Guess a value, but be prepared to backtrack
Constraint programming methodology
• Model problem
• Solve model
• specify in terms of constraints on acceptable solutions
• define/choose constraint model: variables, domains, constraints
• define/choose search algorithm
• define/choose heuristics
ConstraintSatisfaction
Problem
Constraint satisfaction problem (CSP)
• A CSP is defined by:
• a set of variables {x1, …, xn}
• a set of values for each variable dom(x1), …, dom(xn)
• a set of constraints {C1, …, Cm}
• A solution to a CSP is a complete assignment to all the variables that satisfies the constraints
Given a CSP
• Determine whether it has a solution or not
• Find one solution
• Find all solutions
• Find an optimal solution, given some cost function
Example domains and constraints
• Reals, linear constraints
• 3x + 4y ≤ 7, 5x – 3y + z = 2
• Guassian elimination, linear programming
• Integers, linear constraints
• integer linear programming, branch-and-bound
• Boolean values, clauses
• Here:
• finite domains
• rich constraint languages
• user-defined constraints
• global constraints
Constraint languages
• Usual arithmetic operators:
• =, , , < , > , , + , , *, /, absolute value, exponentiation
• e.g., 3x + 4y 7, 5x3 – x*y = 9
• Usual logical operators:
• , , , (or “if … then”)
• e.g., if x = 1 then y = 2, x y z, (3x + 4y 7) (x*y = z)
• Global constraints:
• alldifferent(x1, …, xn) pairwise different
• cardinality(x1, …, xn, l, u) each value must be assigned to at least l variables and at most u variables
• Table constraints
Constraint model for puzzle
variables
v1, …, v8
domains
{1, …, 8}
constraints
| v1 – v2 | 1
| v1 – v3 | 1
…
| v7 – v8 | 1
alldifferent(v1, …, v8)
?
?
?
?
?
?
??
(a + b) + c
Example: Instruction scheduling
Given a basic-block of code and a multiple-issue pipelined processor, find the minimum length schedule
Example: evaluate (a + b) + c
instructions
A r1 a
B r2 b
C r3 c
D r1 r1 + r2
E r1 r1 + r3
3 3
31
A B
D C
E
dependency DAG
Example: evaluate (a + b) + c
non-optimal schedule
A r1 a
B r2 b
nop
nop
D r1 r1 + r2
C r3 c
nop
nop
E r1 r1 + r3
3 3
31
A B
D C
E
dependency DAG
Example: evaluate (a + b) + c
optimal schedule
A r1 a
B r2 b
C r3 c
nop
D r1 r1 + r2
E r1 r1 + r3
3 3
31
A B
D C
E
dependency DAG
Constraint model
variables
A, B, C, D, E
domains
{1, …, m}
constraints
D A + 3
D B + 3
E C + 3
E D + 1
cardinality(A, B, C, D, E, 0, width)
3 3
31
A B
D C
E
dependency DAG
Example: Boolean satisfiability
Given a Boolean formula, does there exist a satisfying assignment
(x1 x2 x4) (x2 x4 x5) (x3
x4 x5)
Constraint model
variables: x1, x2 , x3 , x4 , x5
domains: {true, false}
constraints:
(x1 x2 x4)
(x2 x4 x5)
(x3 x4 x5)
(x1 x2 x4) (x2 x4 x5) (x3
x4 x5)
Example: 3-SAT
A solution
x1 = false
x2 = false
x3 = false
x4 = true
x5 = false
(x1 x2 x4) (x2 x4 x5) (x3
x4 x5)
Example: Graph coloring
Given k colors, does there exist a coloring of the nodes such that adjacent nodes are assigned different colors
Example: 3-coloring
variables: v1, v2 , v3 , v4 , v5
domains: {1, 2, 3}
constraints: vi vj if vi and vj
are adjacent
v2
v3
v1
v5
v4
Constraint model
4
3
2
1
x1 x2 x3 x4
variables: x1, x2 , x3 , x4
domains: {1, 2, 3, 4}
constraints: x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1
A closer look at constraints
• An assignment (also called an instantiation)
• x = a, where a dom(x),
• A tuple t over an ordered set of variables {x1, …, xk} is an ordered list of values (a1, …, ak) such that ai dom(xi), i = 1, …, k
• can be viewed as a set of assignments {x1 = a1, …, xk = ak}
• Given a tuple t, notation t[xi] selects out the value for variable xi; i.e.,t[xi] = ai
A closer look at constraints
• Each constraint C is a relation
• a set of tuples over some ordered subset of the variables, denoted by vars(C)
• specifies the allowed combinations of values for the variables in vars(C)
• The size of vars(C) is known as the arity of the constraint
• a unary constraint has an arity of 1
• a binary constraint has an arity of 2
• a non-binary constraint has arity greater than 2
Example
• Let
• dom(x1) = {1, 2, 3, 4},
• dom(x2) = {1, 2, 3, 4}
• C be the constraint x1 x2 | x1 – x2 | 1
• Then
• vars(C) = {x1, x2}
• tuples in C = {(1,3), (1,4), (2,4), (3,1), (4,1), (4,2)}
• C is a binary constraint
intensional
x1 x2
1 3 1 4
2 4
3 1
4 1
4 2
extensional
(table constraint)
Constraint programming methodology
• Model problem
• Solve model
• specify in terms of constraints on acceptable solutions
• define/choose constraint model: variables, domains, constraints
• define/choose search algorithm
• define/choose heuristics
ConstraintSatisfaction
Problem
Example constraint systems/languages
System Description
ILOG / OPL C++ class library
Comet Programming language & system
Eclipse Logic programming
Choco Java class library
HAL Logic programming
Oz Functional programming
Application areas
• scheduling
• logistics
• planning
• supply chain management
• rostering
• timetabling
• vehicle routing
• bioinformatics
• networks
• configuration
• assembly line sequencing
• cellular frequency assignment
• airport counter and gate allocation
• airline crew scheduling
• optimize placement of transmitters for wireless
• …
Some commercial applications
Testimonial
Hi Prof. van Beek,
I am a graduate student from Management Sciences and was in your AI and CP courses last year.
I applied some of the CP concepts like redundant modeling and exploiting problem symmetry that you taught us in class to optimization problems at Canadian Tire. This together with the MIP solver was able to give us much better results in a fraction of the time. The integration of CP and OR was at a very high level and not at the solver level, which the Optimization team at Canadian Tire found very encouraging (they do not like anything complex).
Following this, I am working as a part time Optimization consultant this term to explore further research avenues for logistics problems that they have. Just thought I should share the CP success story and thank you for introducing me to CP.
Best regards,
A Grateful Student
Outline
• Introduction
• Constraint propagation
• Backtracking search
• Global constraints
• Soft constraints
• Symmetry
• Modeling
Fundamental insight: Local consistency
• A local inconsistency is an instantiation of some of the variables that satisfies the relevant constraints but:
• cannot be extended to one or more additional variables
• so cannot be part of any solution
• Has led to:
• definitions of conditions that characterize the level of local consistency of a CSP
• algorithms which enforce these levels of local consistency by removing inconsistencies from the CSP
• effective backtracking algorithms for finding solutions to a CSP that maintain a level of local consistency during the search
Enforcing local consistency: constraint propagation
• Here, focus on:
Given a constraint, remove a value from the domain of a variable if it cannot be part of a solution according to that constraint
Local consistency: arc consistency
• Given a constraint C, a value a dom(x) for a variable x vars(C) has:
• a domain support in C if there exists a t C such that t[x] = a and t[y] dom(y), for every y vars(C)
• i.e., there exists values for each of the other variables (from their respective domains) such that the constraint is satisfied
• A constraint C is:
• arc consistent iff for each x vars(C), each value a dom(x) has a domain support in C
• A CSP is:
• arc consistent if every constraint is arc consistent
• A CSP can be made arc consistent by repeatedly removing unsupported values from the domains of its variables
Arc consistency’s other names
• domain consistency
• hyper-arc consistency
• generalized arc consistency (GAC)
ac() : boolean1. Q all variable/constraint pairs (x, C) 2. while Q {} do3. select and remove a pair (x, C)
from Q4. if revise(x, C) then5. if dom(x) = {}6. return false7. else8. add pairs to Q9. return true
Generic arc consistency algorithm
revise(x, C) : boolean1. change false2. for each v dom(x) do3. if t C s.t. t[x] = v then4. remove v from dom(x)5. change true6. return change
ac() : boolean1. Q all variable/constraint pairs (x, C) 2. while Q {} do3. select and remove a pair (x, C)
from Q4. if revise(x, C) then5. if dom(x) = {}6. return false7. else8. add pairs to Q9. return true
Generic arc consistency algorithm
revise(x, C) : boolean1. change false2. for each v dom(x) do3. if t C s.t. t[x] = v then4. remove v from dom(x)5. change true6. return change
variable
x
y
z
domain
{1, 2, 3}
{1, 2, 3}
{1, 2, 3}
C1: x < y
constraints
C2: y < z
4-queens: Is it arc consistent?
4
3
2
1
x1 x2 x3 x4
variables: x1, x2 , x3 , x4
domains: {1, 2, 3, 4}
constraints: x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1
Q
Q
Q
Q
Q
Q
Q
Q
Improvements
•Much work on efficient algorithms for table constraints
• Special purpose algorithms for global constraints (coming later)
Local consistency: bounds consistency
• Given a constraint C, a value a dom(x) for a variable x vars(C) has:
• an interval support in C if there exists a t C such that t[x] = a and t[y] [min(dom(y)), max(dom(y))], for every y vars(C)
• i.e., there exists values for each of the other variables (from their respective domains treated as an interval) such that the constraint is satisfied
• A constraint C is said to be:
• bounds consistent iff for each x vars(C), each of the values min(dom(x)) and max(dom(x)) has an interval support in C
• A CSP is:
• bounds consistent if every constraint is bounds consistent
• A CSP can be made bounds consistent by repeatedly removing unsupported values from the domains of its variables
Example of bounds consistency: instruction scheduling
variables
A, B, C, D, E
domains
{1, …, m}
constraints
D A + 3
D B + 3
E C + 3
E D + 1
cardinality(A, B, C, D, E, 0, width)
3 3
31
A B
D C
E
dependency DAG
[1, 3]
[4, 6]
variable
A
B
C
D
E
domain
[1, 6]
[1, 6]
[1, 6]
[1, 6]
[1, 6]
D A + 3
constraints
[4, 5]
[1, 3]
[4, 6]
[1, 3]
[1, 2]
D B + 3
E C + 3
E D + 1
cardinality(A, B, C, D, E, 1)
[5, 6]
[1, 2]
[3, 3]
[6, 6]
Constraint propagation: Bounds consistency
Singleton consistency
• Let t be a set of assignments to some of the variables of a CSP P
• e.g., {x = 1}
• The CSP induced by t, denoted P|t, is the same as P except that the domain of each variable x in vars(t) contains only one value t[x], the value that has been assigned to x by t
• e.g., for P|{x = 1}, the domain of x is just {1}; everything else the same
• A CSP P is:
• singleton arc consistent iff for all variables x, for all a dom(x), P|{x=a} is not arc inconsistent
• singleton bounds consistent iff for all variables x, for all a dom(x), P|{x=a} is not bounds inconsistent
Constraint propagation: Singleton arc consistency
4
3
2
1
x1 x2 x3 x4
Q
?
Consider { x1 = 1}variabl
ex1
x2
x3
x4
constraints x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1
domain{1, 2, 3,
4}{1, 2, 3,
4}{1, 2, 3,
4}
{1, 2, 3, 4}
Constraint propagation: Singleton arc consistency
4
3
2
1
x1 x2 x3 x4
Q
?
Consider { x1 = 2}variabl
ex1
x2
x3
x4
constraints x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1
domain{1, 2, 3,
4}{1, 2, 3,
4}{1, 2, 3,
4}
{1, 2, 3, 4}
Outline
• Introduction
• Constraint propagation
• Backtracking search
• Global constraints
• Symmetry
• Modeling
Constraint programming methodology
• Model problem
• Solve model
• specify in terms of constraints on acceptable solutions
• define/choose constraint model: variables, domains, constraints
• define/choose search algorithm
• define/choose heuristics
Backtracking search
• CSPs often solved using backtracking search
• Many techniques for improving efficiency of a backtracking search algorithm
• branching strategies, constraint propagation, nogood recording, non-chronological backtracking (backjumping), heuristics for variable and value ordering, portfolios and restart strategies
• techniques are not always orthogonal; combining can give
• a multiplicative effect
• a degradation effect
• Best combinations of these techniques give robust backtracking algorithms that can routinely solve large, hard instances that are of practical importance
Outline
• Introduction
• Constraint propagation
• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies
• Global constraints
• Symmetry
• Modeling
Backtracking search
• A backtracking search is a depth-first traversal of a search tree
• search tree is generated as the search progresses
• search tree represents alternative choices that may have to be examined in order to find a solution
• method of extending a node in the search tree is often called a branching strategy
Generic backtracking algorithm
treeSearch( i : integer ) : integer1. if all variables assigned a value then 2. return 0 // solution found3. x getNextVariable( )4. backtrackLevel i5. for each branching constraint bi do6. post( bi )7. if propagate( bi ) then8. backtrackLevel treeSearch( i + 1 )9. undo( bi )10. if backtrackLevel < i then11. return backtrackLevel12. backtrackLevel getBacktrackLevel()13. setNogoods()14. return backtrackLevel
Branching strategies
• A node p = {b1, …, bj} in the search tree is a set of branching constraints, where bi, 1 ≤ i ≤ j, is the branching constraint posted at level i in search tree
• A node p is extended by posting a branching constraint
• to ensure completeness, the constraints posted on all the branches from a node must be mutually exclusive and exhaustive
p = {b1, …, bj}
p {bj+1} p {bj+1}1 k…
Popular branching strategies
• Running example: let x be the variable branched on, let dom(x) = {1, …, 6}
• Enumeration (or d-way branching)
• variable x is instantiated in turn to each value in its domain
• e.g., x = 1 is posted along the first branch, x = 2 along second branch, …
• Binary choice points (or 2-way branching)
• variable x is instantiated to some value in its domain
• e.g., x = 1 is posted along the first branch, x 1 along second branch, respectively
• Domain splitting
• constraint posted splits the domain of the variable
• e.g., x 3 is posted along the first branch, x > 3 along second branch, respectively
Other branching strategies
• Posting non-unary branching constraints, branching strategies that are specific to class of problems
• Example: job shop scheduling
• must schedule a set of tasks t1, …, tk on a set of resources
• let xi be a variable representing the starting time of task ti
• let di be the fixed duration of task ti
• idea: serialize the tasks that share a resource
• consider two task t1 and t2 which share a resource
• post the constraint x1 + d1 <= x2 along one branch
• post the constraint x2 + d2 <= x1 along the other branch
Outline
• Introduction
• Constraint propagation
• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies
• Global constraints
• Symmetry
• Modeling
Constraint propagation
• Effective backtracking algorithms for constraint programming maintain a level of local consistency during the search; i.e., perform constraint propagation
• A generic scheme to maintain a level of local consistency in a backtracking search is to perform constraint propagation at each node in the search tree
• if any domain of a variable becomes empty, inconsistent so backtrack
Constraint propagation
• Backtracking search integrated with constraint propagation has two important benefits
1.removing inconsistencies during search can dramatically prune the search tree by removing deadends and by simplifying the remaining sub-problem
2. some of the most important variable ordering heuristics make use of the information gathered by constraint propagation
Maintaining a level of local consistency
• Definitions of local consistency can be categorized by whether:
• only unary constraints need to be posted during constraint propagation; sometimes called domain filtering
• higher arity constraints may need to be posted
• In implementations of backtracking
• domains represented extensionally
• posting and retracting unary constraints can be done very efficiently
• important that algorithms for enforcing a level of local consistency be incremental
Some backtracking algorithms
• Chronological backtracking (BT)
• naïve backtracking: performs no constraint propagation, only checks a constraint if all of its variables have been instantiated; chronologically backtracks
• Forward checking (FC)
• maintains arc consistency on all constraints with exactly one uninstantiated variable; chronologically backtracks
• Maintaining arc consistency (MAC)
• maintains arc consistency on all constraints with at least one uninstantiated variable; chronologically backtracks
• Conflict-directed backjumping (CBJ)
• backjumps; no constraint propagation
Constraint model for 4-queens
4
3
2
1
x1 x2 x3 x4
variables: x1, x2 , x3 , x4
domains: {1, 2, 3, 4}
constraints: x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1
Forward checking (FC)
4
3
2
1
x1 x2 x3 x4
Q
Enforce arc consistency on constraints with exactly one variable uninstantiated
{ x1 = 1}
x1 x2 |x1 – x2| 1 x1 x3 |x1 – x3| 2x1 x4 |x1 – x4| 3
constraints:
Maintaining arc consistency (MAC)
4
3
2
1
x1 x2 x3 x4
Q
?
{ x1 = 1}
x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1
constraints:
Enforce arc consistency on constraints with at least one variable uninstantiated
Outline
• Introduction
• Constraint propagation
• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies
• Global constraints
• Symmetry
• Modeling
Non-chronological backtracking
• Upon discovering a deadend, a backtracking algorithm must retract some previously posted branching constraint
• chronological backtracking: only the most recently posted branching constraint is retracted
• non-chronological backtracking: algorithm backtracks to and retracts the closest branching constraint which bears some responsibility for the deadend
4
3
2
1
{x1 = 1}
Conflict-directed backjumping (CBJ)
Q
Q{x1 = 1, x2 = 3}
x1
x2
x1
x2{x1 = 1, x2 = 3 , x4 = 2}
Q
x3
x1 x3 x4x2
Outline
• Introduction
• Constraint propagation
• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies
• Global constraints
• Symmetry
• Modeling
Nogood recording
• One of the most effective techniques known for improving the performance of backtracking search on a CSP is to add redundant (implied) constraints
• a constraint is redundant if set of solutions does not change when constraint is added
• Three methods:
• add hand-crafted constraints during modeling
• apply a consistency algorithm before solving
• learn constraints while solving
nogood recording
Nogood recording
• A nogood is a set of assignments and branching constraints that is not consistent with any solution
• i.e., there does not exist a solutionan assignment of a value to each variable that satisfies all the constraintsthat also satisfies all the assignments and branching constraints in the nogood
Example nogoods: 4-queens
• Set of assignments {x1 = 1, x2 = 3} is a nogood
• to rule out the nogood, the redundant constraint
(x1 = 1 x2 = 3)
could be recorded, which is just
x1 1 x2 3
• recorded constraints can be checked and propagated just like the original constraints
• But {x1 = 1, x2 = 3} is not a minimal nogood
4
3
2
1
Q
Q
x1 x3 x4x2
Nogood recording
• If the CSP had included the nogood as a constraint, deadend would not have been visited
• Idea: record nogoods that might be useful later in the search
Discovering nogoods
• Discover nogoods when:
• during backtracking search when current set of assignments and branching constraints fails
• during backtracking search when nogoods have been discovered for every branch
• set of assignments {x1 = 1, x2 = 1} is a nogood
• set of assignments {x1 = 1, x2 = 2} is a nogood
• set of assignments {x1 = 1, x2 = 3} is a nogood
• set of assignments {x1 = 1, x2 = 4} is a nogood
• {x1 = 1} is a nogood
• Tricky when:
• backtracking algorithm maintains a level of local consistency
• in the presence of global constraints
• Standard in SAT solvers
• Currently not yet widely used for solving general CSPs
Outline
• Introduction
• Constraint propagation
• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies
• Global constraints
• Symmetry
• Modeling
Heuristics for backtracking algorithms
• Variable ordering (very important)
• what variable to branch on next
• Value ordering (can be important)
• given a choice of variable, what order to try values
Variable ordering
• Domain dependent heuristics
• Domain independent heuristics
• Static variable ordering
• fixed before search starts
• Dynamic variable ordering
• chosen during search
Variable ordering: Possible goals
• Minimize the underlying search space
• static ordering example:
• suppose x1, x2, x3, x4 with domain sizes 2, 4, 8, 16
• compare static ordering x1, x2, x3, x4 vs x4, x3, x2, x1
• Minimize expected depth of any branch
• Minimize expected number of branches
• Minimize size of search space explored by backtracking algorithm
• intractable to find “best” variable
Variable ordering: Basic idea
• Assign a heuristic value to a variable that estimates how difficult it is to find a satisfying value for that variable
• Principle: most likely to fail first
• or don’t postpone the hard part
Some dynamic variable ordering heuristics
• Let rem( x | p ) be the number of values that remain in the domain of variable x after constraint propagation, give a set of branching constraints p.
• dom: choose the variable x that minimizes:
rem( x | p )
• dom / deg: divide domain size of a variable by degree of the variable
• dom / wdeg: divide domain size of a variable by weighted degree of variable
Some dynamic variable ordering heuristics
• Let rem( x | p ) be the number of values that remain in the domain of variable x after constraint propagation, give a set of branching constraints p.
• More elaborate schemes, e.g., choose the variable x that minimizes:
rem( y | p {x = a})
• Impact-based variable ordering:
• count up effect (domain reductions)
• pick variable with largest effect
• works well in conjunction with singleton consistency
ya dom(x)
where y ranges over some or all unassigned variables
Value ordering: Basic idea
• Principle:
• given that we have already chosen the next variable to instantiate, choose first the values that are most likely to succeed
• Choose the next value for variable x:
• estimate the number of solutions for each value a for x
• estimate the probability of a solution for each value a for x
• Example: choose the variable a dom(x) that maximizes:
rem( y | p {x = a})y
where y ranges over some or all unassigned variables
Outline
• Introduction
• Constraint propagation
• Backtracking search• branching strategies• constraint propagation• non-chronological backtracking • nogood recording• heuristics for variable and value ordering• portfolios and restart strategies
• Global constraints
• Symmetry
• Modeling
Portfolios
• Observation: Backtracking algorithms can be quite brittle, performing well on some instances but poorly on other similar instances
• Portfolios of algorithms have been proposed and shown to improve performance
We are the last Dodos on the planet, so I’ve put all of our eggs safely into
this basket…
Portfolios: Definitions
• Given a set of backtracking algorithms and a time deadline d, a portfolio P for a single processor is a sequence of pairs,
• An algorithm selection portfolio is a portfolio where,
• A restart strategy portfolio is a portfolio where,
Portfolios
• Instance-based
• intended to be used on an instance of a problem
• which portfolio determined online
• Class-based
• intended to be used on all instances in a class of problems
• which portfolio determined offline
Examples of general portfolios
• Increasing levels of constraint propagation
• Phase 1 bounds consistency• Phase 2 singleton bounds consistency• Phase 3 singleton singleton bounds consistency
• Alternative search heuristics
Restart strategy portfolio
• Randomize backtracking algorithm
• randomize selection of a value
• randomize selection of a variable
• A restart strategy (t1, t2, t3, …, tm) is a sequence
• idea: randomized backtracking algorithm is run for t1 steps. If no solution is found within that cutoff, the algorithm is restarted and run for t2 steps, and so on
Restart strategies
• Let f(t) be the probability a randomized backtracking algorithm A on instance x stops after taking exactly t steps
• f(t) is called the runtime distribution of algorithm A on instance x
• Given the runtime distribution of an instance, the optimal restart strategy for that instance is given by (t*, t*, t*, …), for some fixed cutoff t*
• A fixed cutoff strategy is an example of a non-universal strategy: designed to work on a particular instance
Universal restart strategies
• Non-universal strategies are open to catastrophic failure
• In contrast to non-universal strategies, universal strategies are designed to be used on any instance
• Luby strategy
• Walsh strategy
(1, r, r2, r3, …), r > 1
(1, 1, 2, 1, 1, 2, 4, 1, 1, 2, 1, 1, 2, 4, 8, 1, …)
grows exponentially
grows linearly
Summary: backtracking search
• CSPs often solved using backtracking search
• Many techniques for improving efficiency of a backtracking search algorithm
• branching strategies, constraint propagation, nogood recording, non-chronological backtracking (backjumping), heuristics for variable and value ordering, portfolios and restart strategies
• Best combinations of these techniques give robust backtracking algorithms that can routinely solve large, hard instances that are of practical importance
Outline
• Introduction
• Constraint propagation
• Backtracking search
• Global constraints
• Symmetry
• Modeling
Global constraints
• A global constraint is a constraint that can be specified over an arbitrary number of variables
• Advantages:
• captures common constraint patterns
• efficient, special purpose constraint propagation algorithms can be designed
Alldifferent constraint
• Consists of:
• set of variables {x1, …, xn}
• Satisfied iff:
• each of the variables is assigned a different value
Alldifferent: example of arc consistency
• Suppose alldifferent(x1, x2, x3, x4) where:
• dom(x1) = {b, c, d, e}
• dom(x2) = {b, d}
• dom(x3) = {a, b, c, d}
• dom(x4) = {b, d}
• Enforcing domain consistency yields
• dom(x1) = {c, e}
• dom(x2) = {b, d}
• dom(x3) = {a, c}
• dom(x4) = {b, d}
Alldifferent: algorithm for arc consistency
• General idea: based on matching theory applied to variable-value graph
• Suppose alldifferent(x1, x2, x3, x4) where:
• dom(x1) = {b, c, d, e}
• dom(x2) = {b, d}
• dom(x3) = {a, b, c, d}
• dom(x4) = {b, d}
• Construct variable-value graph
x1
x2
x3
x4
a
b
c
d
e
Alldifferent: algorithm for arc consistency
• A matching is a subset of the edges such that no two edges share a vertex. A matching covers a set of vertices if each vertex participates in an edge.
• A matching that covers the variables is a solution to the constraint
x1
x2
x3
x4
a
b
c
d
e
Alldifferent: algorithm for arc consistency
• An alldifferent contraint is arc consistent if every edge in the variable-value graph belongs to some matching that covers the variables
• Remove edges/values that do not belong to some covering matching
• Example: x1 bx1
x2
x3
x4
a
b
c
d
e
Alldifferent: algorithm for arc consistency
• An alldifferent constraint is arc consistent if every edge in the variable-value graph belongs to some matching that covers the variables
• Remove edges/values that do not belong to some covering matching
• Example: x1 b
• So, remove b from dom(x1)
x1
x2
x3
x4
a
b
c
d
e
Alldifferent: example of bounds consistency
• Suppose alldifferent(A, B, C, D, E, F) where:
dom(A) = {3, 4, 5, 6}
dom(B) = {3, 4}
dom(C) = {2, 3, 4, 5}
dom(D) = {2, 3, 4}
dom(E) = {3, 4}
dom(F) = {1, 2, 3, 4, 5, 6}
• Enforcing bounds consistency yields:
dom(A) = {6}
dom(B) = {3, 4}
dom(C) = {5}
dom(D) = {2}
dom(E) = {3, 4}
dom(F) = {1}
Alldifferent: algorithm for bounds consistency
• General idea: based on Hall intervals
• Let I be an interval and let vars(I) be the set of variables whose domains are contained in I; i.e., vars(I) = { xi | dom(xi) I }
• A Hall interval is an interval I such that | vars(I) | = | I |
• Example:
dom(A) = {3, 4, 5, 6}
dom(B) = {3, 4}
dom(C) = {2, 3, 4, 5}
dom(D) = {2, 3, 4}
dom(E) = {3, 4}
dom(F) = {1, 2, 3, 4, 5, 6}
Hall intervals
[3, 4] vars([3,4]) = {B, E}
[2, 4] vars([2,4]) = {B, D, E}
[2, 5] vars([2,5]) = {B, C, D, E}
[2, 6] vars([2,6]) = {A, B, C, D, E}
[1, 6] vars([1,6]) = {A, B, C, D, E, F}
Alldifferent: algorithm for bounds consistency
• If there exists a Hall interval I then any assignment of values to the variables in vars(I) will use all of the values in I
• So, remove these values from the domains of the other variables
• Example:
dom(A) = {3, 4, 5, 6}
dom(B) = {3, 4}
dom(C) = {2, 3, 4, 5}
dom(D) = {2, 3, 4}
dom(E) = {3, 4}
dom(F) = {1, 2, 3, 4, 5, 6}
Hall intervals
[3, 4] vars([3,4]) = {B, E}
[2, 4] vars([2,4]) = {B, D, E}
[2, 5] vars([2,5]) = {B, C, D, E}
[2, 6] vars([2,6]) = {A, B, C, D, E}
[1, 6] vars([1,6]) = {A, B, C, D, E, F}
Alldifferent example: Sudoku
Each Sudoku has a unique solution that can be reached logically without guessing.
Enter digits from 1 to 9 into the blank spaces. Every row must contain one of each digit. So must every column, as must every 3x3 square.
5 3 7
6 1 9 5
9 8 6
8 6 3
4 8 3 1
7 2 6
6 2 8
4 1 9 5
8 7 9
Sudoku
x1 x2 x3 x4 x5 x6 x7 x8 x9
x1
0
x1
1
x1
2
x1
3
x1
4
…
x1
9
x2
0
x2
1
…
x2
8
…
x3
7
…
x4
6
…
x5
5
…
x6
4
…
x7
3
…
5 3 7
6 1 9 5
9 8 6
8 6 3
4 8 3 1
7 2 6
6 2 8
4 1 9 5
8 7 9
Sudoku
dom(xi) = {1, …, 9}, for all i = 1, …, 81
alldifferent(x1, x2, x3, x4, x5, x6, x7, x8, x9)…alldifferent(x1, x10, x19, x28, x37, x46, x55, x64,
x73)…alldifferent(x1, x2, x3, x10, x11, x12, x19, x20,
x21)…x1 = 5, x2 = 3, x5 = 7, …, x81 = 9
5 3 7
6 1 9 5
9 8 6
8 6 3
4 8 3 1
7 2 6
6 2 8
4 1 9 5
8 7 9
Global cardinality constraint (cardinality)
• Consists of:
• set of variables {x1, …, xn}
• a domain D = dom(x1) ∙ ∙ ∙ dom(xn)
• for each v D, a pair [lv, uv]
• Satisfied iff:
• number of times a value v is assigned to a variable is at least lv and at most
uv
• Special cases include:
• lv = 0, uv = 1, for all v D (the alldifferent constraint)
• lv = 1, uv = 1, for all v D (the permutation constraint)
• lv = 1, uv 1, for all v D
Cardinality: example of bounds consistency
• Suppose cardinality(A, B, C, D, E, F, G) where:
dom(A) = {1, 2}
dom(B) = {1, 2}
dom(C) = {1, 2}
dom(D) = {1, 2}
dom(E) = {1, 2, 3}
dom(F) = {2, 3, 4, 5}
dom(G) = {3, 5}
• Enforcing bounds consistency yields:
dom(A) = {1, 2}
dom(B) = {1, 2}
dom(C) = {1, 2}
dom(D) = {1, 2}
dom(E) = {3}
dom(F) = {4, 5}
dom(F) = {5}
1: [1, 2]2: [1, 2]3: [1, 1]4: [0, 2]5: [0, 2]
Sudoku
D = {1, 2, 3, 4, 5, 6, 7, 8, 9}
lv = 1, uv = 1, for all v D
dom(xi) = {1, …, 9}, for all i = 1, …, 81
card(x1, x2, x3, x4, x5, x6, x7, x8, x9)…card(x1, x10, x19, x28, x37, x46, x55, x64, x73)…card(x1, x2, x3, x10, x11, x12, x19, x20, x21)…x1 = 5, x2 = 3, x5 = 7, …, x81 = 9
5 3 7
6 1 9 5
9 8 6
8 6 3
4 8 3 1
7 2 6
6 2 8
4 1 9 5
8 7 9
One of the Sudoku “17”
Using cardinality constraints, all of these most difficult instances can be solved with just constraint propagation; i.e., no backtracking
1
4
2
5 4 7
8 3
1 9
3 4 2
5 1
8 6
Knapsack constraint
• Consists of:
• set of variables {x1, …, xn}
• a scalar value ci for each xi
• two scalar values L and U
• Satisfied iff:
• L ci xi Ui =1
n
Knapsack: example of domain consistency
• Suppose knapsack constraint
80 27x1 + 37x2 + 45x3 + 53x4 82
where:
dom(x1) = {0, 1, 2, 3}
dom(x2) = {0, 1, 2, 3}
dom(x3) = {0, 1, 2, 3}
dom(x4) = {0, 1, 2, 3}
• Enforcing domain consistency yields
dom(x1) = {0, 1, 3}
dom(x2) = {0, 1}
dom(x3) = {0, 1}
dom(x4) = {0, 1}
• Example of a propagator that solves an NP-Complete problem using a pseudo-polynomial algorithm based on dynamic programming
Element constraint
• Consists of:
• an array of variables x = [x1, …, xn]
• an integer variable i
• a variable y with arbitrary finite domain
• Satisfied iff:
• xi = y
Element: example of domain consistency
• Suppose xi = y where:
• x = [d, e, h, g]; i.e., dom(x1) = {d}, …, dom(x4) = {g}
• dom(i) = {2, 3, 4}
• dom(y) = {a, b, c, d, e, f, g}
• Enforcing domain consistency yields:
• dom(i) = {2, 4}
• dom(y) = {e, g}
Lexicographic constraint (lex)
• Consists of:
• an array of variables x = [x1, …, xn]
• an array of variables y = [y1, …, yn]
• x lex y satisfied iff:
• x is lexicographically less than or equal to y
(x1 < y1) or
(x1 = y1 and x2 < y2) or
(x1 = y1 and x2 = y2 and x3 < y3) or …
(x1 = y1 and x2 = y2 and … xn = yn)
• Use:
• especially useful for symmetry breaking
Lexicographic: example of domain consistency
• Suppose x = [x1, x2, x3 , x4 , x5] and y = [y1, y2, y3 , y4 , y5] where:
• Enforcing domain consistency on x lex y yields:
X {2} {1,3,4}
{1,2,3,4,5}
{1,2} {3,4,5}
y {0,1,2}
{1} {0,1,2,3,4}
{0,1} {0,1,2}
X {2} {1,3,4}
{1,2,3,4,5}
{1,2} {3,4,5}
y {0,1,2}
{1} {0,1,2,3,4}
{0,1} {0,1,2}
Other global constraints
• Regular constraint
• sequence of variables
• values taken by these variables belongs to a given regular language
• applications: rostering and sequencing problems
• Cumulative constraint
• collection of tasks: release time, processing time, deadline, resource consumption
• resource capacities
• applications: scheduling
• Stretch constraint, nvalue constraint, …
• Optimization global constraints
Outline
• Introduction
• Constraint propagation
• Backtracking search
• Global constraints
• Symmetry
• Modeling
Symmetry in constraint models
• Many constraint models contain symmetry
• variables are “interchangeable”
• values are “interchangeable”
• variable-value symmetry
• As a result, when searching for a solution:
• search tree contains many equivalent subtrees
• if a subtree does not contain a solution, neither will equivalent subtrees elsewhere in the tree
• failing to recognize equivalent subtrees results in needless search
Example of variable symmetry: block scheduling
variables
A, B, C, D, E
domains
{1, …, m}
constraints
D A + 3
D B + 3
E C + 3
E D + 1
cardinality(A, B, C, D, E, 1)
3 3
31
A B
D C
E
dependency DAG
Variables A and B are symmetric
Example of value symmetry: 3-coloring
variables: v1, v2 , v3 , v4 , v5
domains: {1, 2, 3}
constraints: v1 v2
v1 v3
v2 v4
v3 v4
v3 v5
v4 v5
v2
v3
v1
v5
v4
Example of value symmetry: 3-coloring
A solution
v1 = 1
v2 = 2
v3 = 2
v4 = 1
v5 = 3
v2
v3
v1
v5
v4
Mapping
Example of value symmetry: 3-coloring
Another solution
v1 = 1
v2 = 2
v3 = 2
v4 = 1
v5 = 3
v2
v3
v1
v5
v4
Example of value symmetry: 3-coloring
A partial non-solution
v1 = 1
v2 = 2
v3 = 3
v2
v3
v1
v5
v4Another partial non-solution
v1 = 1
v2 = 2
v3 = 3
And so on …
Example of variable-value symmetry: 4-queens
4
3
2
1
x1 x2 x3 x4
variables: x1, x2 , x3 , x4
domains: {1, 2, 3, 4}
constraints: x1 x2 | x1 – x2 | 1 x1 x3 | x1 – x3 | 2 x1 x4 | x1 – x4 | 3 x2 x3 | x2 – x3 | 1 x2 x4 | x2 – x4 | 2 x3 x4 | x3 – x4 | 1
Symmetries for 4-queens
21 3 4
5 6 7
10
9
8
11
12
13
14
15
16
913
5 1
14
10
6
11
15
2
7 3
16
12
8 4
15
16
14
13
12
11
10
78
9
6 5
4 3 2 1
84 12
16
3 7 11
62
15
10
14
1 5 9 13
14
13
15
16
9 10
11
65
12
7 8
1 2 3 4
34 2 1
8 7 6
11
12
5
10
9
16
15
14
13
12
16
8 4
15
11
7
10
14
3
6 2
13
9 5 1
51 9 13
2 6 10
73
14
11
15
4 8 12
16
horizontal axis
vertical axis diagonal 1 diagonal 2
identity rotate 90 rotate 180 rotate 270
Example of variable-value symmetry: 4-queens
Q
x1 x2 x3 x4
4
3
2
1
A partial non-solution x1 = 1
Another partial non-solution x4 = 4
x1 = 1
x1 1
x4 = 4 x4 4
A formal definition of symmetry
• Let P be a CSP where
• V = {x1, …, xn} is the set of variables
• D = dom(x1) dom(xn) is the set of values
• A (solution) symmetry of P is a permutation of the set V D that preserves the set of solutions of P
• special cases:
• value ordering symmetry
• variable ordering symmetry
Symmetries and permutations
21 3 4
5 6 7
10
9
8
11
12
13
14
15
16
identity
( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 )
( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 )
( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 )
( 13 14 15 16 9 10 11 12 5 6 7 8 1 2 3 4 )
14
13
15
16
9 10
11
65
12
7 8
1 2 3 4
horizontal axis
Symmetries and permutations
21 3 4
5 6 7
10
9
8
11
12
13
14
15
16
identity
14
13
15
16
9 10
11
65
12
7 8
1 2 3 4
horizontal axis
x1 x2 x3 x4
4
3
2
1
x1 = 1 x1 = 1
x1 = 2 x1 = 2…
15
16
14
13
12
11
10
78
9
6 5
4 3 2 1
rotate 180
x1 = 1 x1 = 4
x1 = 2 x1 = 3…
x1 = 1 x4 = 4
x1 = 2 x4 = 3…
x1 x2 x3 x4
4
3
2
1
Mitigating symmetry in constraint models
• Reformulate the constraint model to reduce or eliminate symmetry
• e.g., use set variables
• Break symmetry by adding constraints to model
• leave at least one solution
• eliminate some/all symmetric solutions and non-solutions
• Break symmetry during backtracking search algorithm
• recognize and ignore some/all symmetric parts of the search tree dynamically while searching
Breaking symmetry by adding constraints to model: block scheduling
variables
A, B, C, D, E
domains
{1, …, m}
constraints
D A + 3
D B + 3
E C + 3
E D + 1
cardinality(A, B, C, D, E, 0, 1)
3 3
31
A B
D C
E
dependency DAG
B A + 1
Breaking symmetry by adding constraints to model: 3-coloring
variables: v1, v2 , v3 , v4 , v5
domains: {1, 2, 3}
constraints: vi vj if vi and vj
are adjacent
v2
v3
v1
v5
v4
fixing colors in a single clique
Breaking symmetry by adding constraints to model: 4-queens
4
3
2
1
x1 x2 x3 x4
variables: x1, x2 , x3 , x4
domains: {1, 2, 3, 4}
constraints:
xi xi | xi – xj | | i – j |
break horizontal symmetry by adding x1 ≤ 2break vertical symmetry by adding x2 ≤ x3
but …
Danger of adding symmetry breaking constraints
Q
Q
Q
Q
x1 x2 x3 x4
4
3
2
1
Q
Q
Q
Q
x1 x2 x3 x4
4
3
2
1
adding x2 ≤ x3 removes this solution
adding x1 ≤ 2 removes this solution
Breaking symmetry during backtracking search
• Let g() be a permutation
• Let p be a node in the search tree
• a set of assignments and branching constraints
• Suppose node p is to be extended by x = v
• Post the constraint:
(p g(p) x v) g(x v)
Breaking symmetry during backtracking search: 4-queens
x1 = 1
x1 1
x4 = 4 x4 4
General form: (p g(p) x v) g(x v)
p
Here (p is empty): (x v) g(x v)
So, post: (x1 1) (x1 4 x4 1 x4 4)
Outline
• Introduction
• Constraint propagation
• Backtracking search
• Global constraints
• Symmetry
• Modeling
Constraint programming
• Model problem
• specify in terms of constraints on acceptable solutions
• define variables (denotations) and domains
• define constraints in some language
• Solve model
• define algorithm
• design heuristics CSP
Example constraint systems/languages
System Description
ILOG / OPL C++ class library
Comet Programming language & system
Eclipse Logic programming
Choco Java class library
HAL Logic programming
Oz Functional programming
Unfortunately…
• Often easy to state a model
• Much harder is to design an efficient model given a solver
• even harder still to design an efficient model + solver + heuristics
Importance of the constraint model
“In integer programming, formulating a ‘good’ model is of crucial importance to solving the model.”
G. L. Nemhauser and L. A. Wolsey Handbook in OR & MS, 1989
“Same for constraint programming.”
Helmut Simonis, expert CP modeler
Measures for comparing models
• How easy is it to
• write down,
• understand,
• modify, debug,
• communicate?
• How computationally difficult is it to solve?
Computational difficulty?
• What is a good model depends on algorithm
• Choice of variables defines search space
• Choice of constraints defines
• how search space can be reduced
• how search can be guided
Computational complexity
• How hard is it to solve a CSP instance?
• Class of all CSP instances is NP-hard
• if CSPs can be solved efficiently (polynomially), then so can Boolean satisfiability, set covering, partition, traveling salesperson problem, graph coloring, …
• so unlikely efficient general-purpose algorithms exists
Should we give up?
“While a method for computing the solutions to NP-complete problems using a reasonable amount of time remains undiscovered, computer scientists and programmers still frequently encounter NP-complete problems. An expert programmer should be able to recognize an NP-complete problem so that he or she does not unknowingly waste time trying to solve a problem which so far has eluded generations of computer scientists.”
Wikipedia, 2009
Improving model efficiency
• Reformulate the model
• change the denotation of the variables
• Given a model:
• add redundant variables
• add redundant constraints
• add a redundant model
• redundant (or implied): does not change the set of solutions and hence are logically redundant
• add symmetry-breaking
• add dominance constraints
• symmetry-breaking and dominance do change the set of solutions
• must leave at least one solution (satisfaction) or an optimal solution (optimization)
Reformulate the model
• Change the denotation of the variables
• i.e., what does assigning a value to a variable mean in terms of the original problem?
• Example: 4-queens
• xi = j means place the queen in column i, row j
• xi = j means place the queen in row i, column j
• x = [i, j] means place the queen in row i, column j
• xij = 0 means there is no queen in row i, column j
Example: crossword puzzles
1 2 3
6
4
7
5
8
10
9
20
11
22
12
21
13
17
14
181615
23
19
aaardvarkabackabacusabaftabaloneabandon...
…monarchmonarchymonarda...zymurgyzyrianzythum
Adding redundant (auxiliary) variables
• Variables that are abstractions of other variables
• e.g, decision variables
suppose x has domain {1,…,10}
add Boolean variable to represent decisions
(x < 5), (x 5)
• Variables that represent constraints (reified constraints)
• e.g., associate a decision variable x with a constraint so that x takes the value 1 if the constraint is satisfied and 0 otherwise
suppose there is the constraint: (y1 + d1 ≤ y2 ) (y2 + d2 ≤ y1)
add Boolean variable to represent (y1 + d1 ≤ y2 )
Adding redundant constraints
• Improve computational efficiency of model by adding “right” constraints
• dead-ends encountered earlier in search process
• Three methods:
• apply a local consistency enforcing algorithm before solving
• learn constraints (nogoods) while solving
• add hand-crafted constraints during modeling
• Can often be explained as projections of conjunctions of a subset of the existing constraints
Adding redundant models
• Consider two alternative constraint models for the same problem
• example: 4-queens
• Combine into one constraint model
• channeling constraints
• xi = j yj = i for all i, j
4
3
2
1
x1 x3 x4x2
y4
y3
y2
y1
1 3 42
Outline: putting it all together
• Constraint propagation
• Backtracking search
• Global constraints
• Symmetry
• Modeling
• Multiple-issue
• multiple functional units; e.g., ALUs, FPUs, load/store units, branch units
• multiple instructions can be issued (begin execution) each clock cycle
• issue width
max number of instructions that can be issued each clock cycle
• on most architectures issue width less than number of functional units
Computer architecture:Performing instructions in parallel
• Pipelining
• overlap execution of instructions on a single functional unit
• latency of an instruction
number of cycles before result is available
• execution time of an instruction
number of cycles before next instruction can be issued on same functional unit
• serializing instruction
instruction that requires exclusive use of entire processor in cycle in which it is issued
Computer architecture:Performing instructions in parallel
Analogy: vehicle assembly line
Superblock instruction scheduling
• Instruction scheduling
• assignment of a clock cycle to each instruction
• needed to take advantage of complex features of architecture
• sometimes necessary for correctness (VLIW)
• Basic block
• straight-line sequence of code with single entry, single exit
• Superblock
• collection of basic blocks with a unique entrance but multiple exits
• Given a target architecture, find schedule with minimum expected completion time
dependency DAG
• nodes
• one for each instruction
• labeled with execution
time
• nodes F and G are branch
instructions, labeled with
probability the exit is
taken
• arcs
• represent precedence
• labeled with latencies
Example superblock1
A:1
D:1
C:1
E:1
F:1
G:1
1
2
B:3
40%
60%
5 5
2
0
0 0
Example superblock
optimal cost schedule for 2-issue processor
cycle
ALU
FPU
1 A
2 B
3
4
5 C
6
7 D
8 E
9 F
10 G
1
A:1
D:1
C:1
E:1
F:1
G:1
1
2
B:3
40%
60%
5 5
2
0
0 0
Approaches
• Superblock instruction scheduling is NP-complete
• Heuristic approaches in all commercial and open-source research compilers
• greedy list scheduling algorithm coupled with a priority heuristic
• e.g., dependency height and speculative yield (DHASY) heuristic
• Here: Optimal approach
• useful when longer compile times are tolerable
• e.g., compiling for software libraries, digital signal processing, embedded applications, final production build
Assumptions
• Much previous work assumes an idealized architectural model
• processor is fully pipelined: every instruction has an execution time of 1
• issue width of processor is equal to number of functional units
• processor contains no serializing instructions
• However, compiler needs an accurate architectural model to schedule code in best possible manner
• An architectural model is said to be to realistic if it does not make any of these simplifying assumptions
Optimal approaches: State-of-the-art for basic blocks
• Idealized architectures
Wilken et al. (2000)
van Beek & Wilken (2001)
Malik et al. (2006)
Scale up to largest basic blocks that arise in practice
• Realistic architectures
Ertl & Krall (1991)
Kästner & Winkel (2001)
Liu & Chow (2002)
Do not scale beyond 10-40 instructions (largest that arise in practice have 2600 instructions)
• Here:
Builds on Malik et al. (2006)
Scales up to largest basic blocks that arise in practice
Applies to realistic architectures
Optimal approaches: State-of-the-art for superblocks
• Idealized architectures
Shobaki & Wilken (2004)
Scales up to large superblocks
• Realistic architectures
No work
• Here:
Scales up to larger and more difficult superblocks
Applies to realistic architectures
1
A
D
C
E
Basic constraint model
F
G
1
2
B
40%
60%
5 5
2
0
0 0
variables
A, B, C, D, E, F, G
domains
{1, …, m}
constraints
B A + 1, C A + 1,
D B + 5, …, G F
card(A, B, C, F, G, nALU)
card(D, E, nFPU)
card(A, …, G, issuewidth)
cost function
40F + 60G
Basic constraint model (con’t)
B:3non-fully pipelined
instructions• introduce auxiliary variables
PB,1
PB,2
• introduce additional constraints
B + 1 = PB,1
B + 2 = PB,2
card(A, B, PB,1, PB,2 C, F, G,
nALU)
serializing instructions
• similar technique
Improving the model
• Add constraints to increase constraint propagation
• implied constraints: do not change set of solutions
• dominance constraints: preserve an optimal solution
• Here:
• many constraints added to constraint model in extensive preprocessing stage that occurs once
• extensive preprocessing effort pays off as model is solved many times
Improving the model: Implied constraints
i
j
Let nodes i and j define a region; i.e., there is more than one path from i to j
• Implied constraint added to model: xj xi + di,j
• if region small enough, di,j is exactly determined by solving region in isolation
• if region larger, di,j is a lower bound estimate
• Implied constraint added to model: xj xi + di,j
• only if i and j are articulation nodes and region small enough to be solved quickly and exactly in isolation
• tight upper bound on distance between i and j in any optimal schedule
Improving the model: Implied constraints
Implied constraints: xj xi + di,j
j j+1j+2j+3j+4j+5
5
A F
A
B
ED
H
F G
C
1
1
1
33
1
3
1
3
Add: F ≥ A + 5
Implied constraints: xj xi + di,j
A
j j+1j+2j+3j+4j+5
E H
5
A
B
ED
H
F G
C
1
1
1
33
1
3
1
3
Add: H ≥ E + 5
Implied constraints: xj xi + di,j
A
9
A
j j+1j+2j+3j+4j+5
j+6j+7j+8j+9
H
A
B
ED
H
F G
C
1
1
1
33
1
3
1
3
Add: H ≥ A + 9
• Dominance constraints added to model: xj xi
• requires identifying pairs of disjoint, isomorphic subgraphs
• mapping must preserve instruction types, edges, latencies
• fast heuristic approach to finding pairs
• Example
• {D} and {E} are subgraphs that satisfy conditions
• Can add edge from D to E
Improving the model: Dominance constraints
D E
F
2
B5 5
0
Improving the solver:From optimization to satisfaction
• Find bounds on cost function
• upper bound found using list scheduling algorithm
• Enumerate solutions to cost function (knapsack constraint)
lower bound ≤ 40F + 60G ≤ upper bound
• Step through in increasing order of cost
• until one is found that can be extended to a solution to entire constraint model
• testing whether a solution to cost function can be extended is done using a backtracking search algorithm
• Use portfolio to improve performance of backtracking search algorithm
• Increasing levels of constraint propagation
• Phase 1 bounds consistency• Phase 2 singleton bounds consistency• Phase 3 singleton singleton bounds consistency
• Restart and move to next phase if solution not found within time limit
Improving the solver: Portfolios
• During Phases 2 & 3, use impact-based variable ordering heuristic to improve performance of backtracking search algorithm
• measure the importance of a variable for reducing search space
• very effective and essentially free as a side-effect of enforcing singleton consistency
Improving the solver:Impact-based variable ordering
Case study: Value of constraint programming
• Ease of adding constraints to model realistic architectures
• not clear how to similarly extend previously proposed enumeration and integer programming approaches
• global constraints
• Allows and facilitates programming in the computer science sense of the word
• allowed us to incorporate and fine-tune ideas such as portfolios and impact-based variable ordering heuristics into our solver