Outline
Outline
• Introduction
• Introduction
• Constraint propagation
• Constraint propagation
• Backtracking search
• Backtracking search
• Complexity issues
• • • • • •
branching strategies constraint propagation non-chronological backtracking nogood recording heuristics for variable and value ordering portfolios and restart strategies
• Complexity issues
Backtracking search
Backtracking search
• CSPs often solved using backtracking search
• A backtracking search is a depth-first traversal of a search tree
• Many techniques for improving efficiency of a backtracking search algorithm • Best combinations of these techniques give robust backtracking algorithms that can routinely solve large, hard instances that are of practical importance
• 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
Branching strategies
treeSearch( i : integer ) : integer 1. if all variables assigned a value then 2. return 0 // solution found 3. x getNextVariable( ) 4. backtrackLevel i 5. for each branching constraint bi do 6. post( bi ) 7. if propagate( bi ) then 8. backtrackLevel treeSearch( i + 1 ) 9. undo( bi ) 10. if backtrackLevel < i then 11. return backtrackLevel 12. backtrackLevel getBacktrackLevel() 13. setNogoods() 14. return backtrackLevel
• 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 {b1j+1}
…
k } p {bj+1
1
Popular branching strategies
Outline
• Let x be the variable branched on, let dom(x) = {1, …, 6}
• Introduction
• Enumeration (or d-way branching)
• Constraint propagation
• 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
• Backtracking search • • • • • •
branching strategies constraint propagation non-chronological backtracking nogood recording heuristics for variable and value ordering portfolios and restart strategies
• Complexity issues
• constraint posted splits the domain of the variable • e.g., x 3 is posted along the first branch, x > 3 along second branch, respectively
Constraint propagation
Constraint propagation
• Effective backtracking algorithms for CSPs maintain a level of local consistency during the search; i.e., perform constraint propagation
• Backtracking search integrated with constraint propagation has two important benefits
• Perform constraint propagation at each node in the search tree • if any domain of a variable becomes empty, inconsistent so backtrack
Some backtracking algorithms
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
CSP for 4-queens
• 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)
variables: x1, x2 , x3 , x4 domains:
x2 x3 x4 x3 x4 x4
x2
x3
x4
1
{1, 2, 3, 4} constraints: x1 x1 x1 x2 x2 x3
x1
| x1 – x2 | 1 | x1 – x3 | 2 | x1 – x4 | 3 | x2 – x3 | 1 | x2 – x4 | 2 | x3 – x4 | 1
2 3 4
• backjumps; no constraint propagation
2
Search tree for 4-queens x1=1
Chronological backtracking (BT) x1= 4
x1 Q
x2
Q
…
x3 Q Q
Q
Q
Q
Q Q
x4
Q
(1,1,1,1)
(2,4,1,3)
(4,4,4,4)
(3,1,4,2)
Forward checking (FC)
Forward checking (FC) on 4-queens
Enforce arc consistency on constraints with exactly one variable uninstantiated x1 1
{ x1 = 1}
x2
x3
x4
Q
Q Q
2
constraints: x1 x2 |x1 – x2| 1 x1 x3 |x1 – x3| 2 x1 x4 |x1 – x4| 3
…
3 4
Q
Q Q Q
Maintaining arc consistency (MAC)
Maintaining arc consistency (MAC) on 4-queens
Enforce arc consistency on constraints with at least one variable uninstantiated { x1 = 1}
x1 1
constraints: x1 x1 x1 x2 x2 x3
x2 x3 x4 x3 x4 x4
| x1 – x2 | 1 | x1 – x3 | 2 | x1 – x4 | 3 | x2 – x3 | 1 | x2 – x4 | 2 | x3 – x4 | 1
x2
x3
x4
Q Q
Q
2
Q Q
3 4
?
Q
√
3
Outline
Non-chronological backtracking
• Introduction
• Upon discovering a deadend, a backtracking algorithm must retract some previously posted branching constraint
• Constraint propagation
• chronological backtracking: only the most recently posted branching constraint is retracted
• Backtracking search • • • • • •
• non-chronological backtracking: algorithm backtracks to and retracts the closest branching constraint which bears some responsibility for the deadend
branching strategies constraint propagation non-chronological backtracking nogood recording heuristics for variable and value ordering portfolios and restart strategies
• Complexity issues
Conflict-directed backjumping (CBJ)
• Introduction
{x1 = 1}
x1
x3
x4
x1 x2
2
Q
3 {x1 = 1, x2 = 3 , x4 = 2}
x2
Q
1 {x1 = 1, x2 = 3}
Outline
Q
x1 x2
4
• Constraint propagation • Backtracking search • • • • • •
branching strategies constraint propagation non-chronological backtracking nogood recording heuristics for variable and value ordering portfolios and restart strategies
• Complexity issues
x3
Nogood recording
Nogood recording
• One of the most effective techniques known for improving the performance of backtracking search on a CSP is to add redundant constraints
• A nogood is a set of assignments and branching constraints that is not consistent with any solution
• a constraint is redundant if set of solutions does not change when constraint is added
• Three methods:
• 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
• add hand-crafted constraints during modeling • apply a constraint propagation algorithm before solving • learn constraints while solving
nogood recording
4
Example nogoods: 4-queens
Nogood recording x1
• 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
1
x2
x3
x4
Q
• Idea: record nogoods that might be useful later in the search
2 3
• If the CSP had included the nogood as a constraint, deadend would not have been visited
Q
4
• But {x1 = 1, x2 = 3} is not a minimal nogood
Discovering nogoods
Outline
• Discover nogoods when:
• Introduction
• 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
• Constraint propagation • Backtracking search • • • • • •
branching strategies constraint propagation non-chronological backtracking nogood recording heuristics for variable and value ordering portfolios and restart strategies
• set of assignments {x1 = 1, x2 = 4} is a nogood • {x1 = 1} is a nogood
• Complexity issues
Heuristics for backtracking algorithms
Variable ordering
• Variable ordering (very important)
• Static variable ordering
• what variable to branch on next
• Value ordering (can be important) • given a choice of variable, what order to try values
• fixed before search starts
• Dynamic variable ordering • chosen during search
5
Variable ordering: Possible goals
Variable ordering: Basic idea
• Minimize the underlying search space
• Assign a heuristic value to a variable that estimates how difficult it is to find a satisfying value for that variable
• 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
• Principle: most likely to fail first • or don’t postpone the hard part
• Minimize expected number of branches • Minimize size of search space explored by backtracking algorithm • intractable to find “best” variable
Some dynamic variable ordering heuristics
Value ordering: Basic idea
• 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.
• Principle:
• dom: choose the variable x that minimizes: rem( x | p )
• 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
• dom / deg: divide domain size of a variable by degree of the variable
• estimate the probability of a solution for each value a for x
• Example: choose the variable a dom(x) that maximizes:
y
rem( y | p {x = a})
where y ranges over some or all unassigned variables
Outline
Portfolios
• Introduction
• Observation: Backtracking algorithms can be quite brittle, performing well on some instances but poorly on other similar instances
• Constraint propagation • Backtracking search • • • • • •
branching strategies constraint propagation non-chronological backtracking nogood recording heuristics for variable and value ordering portfolios and restart strategies
• Complexity issues
• Portfolios of algorithms have been shown to improve performance
We are the last Dodos on the planet, so I’ve put all of our eggs safely into this basket…
6
Portfolios: Definitions
Examples of general portfolios
• Given a set of backtracking algorithms and a time deadline d, a portfolio P for a single processor is a sequence of pairs,
• Increasing levels of constraint propagation
• An algorithm selection portfolio is a portfolio where,
• Phase 1 • Phase 2 • Phase 3
bounds consistency arc consistency singleton arc consistency
• Alternative search heuristics
• A restart strategy portfolio is a portfolio where,
Restart strategy portfolio
Restart strategies
• Randomize backtracking algorithm
• Let f(t) be the probability a randomized backtracking algorithm A on instance x stops after taking exactly t steps
• randomize selection of a value • randomize selection of a variable
• f(t) is called the runtime distribution of algorithm A on instance x
• A restart strategy (t1, t2, t3, …, tm) is a sequence
• 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*
• 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
• A fixed cutoff strategy is an example of a non-universal strategy: designed to work on a particular instance
Universal restart strategies
Summary: backtracking search
• Non-universal strategies are open to catastrophic failure
• CSPs often solved using backtracking search • Many techniques for improving efficiency of a backtracking search algorithm
• In contrast to non-universal strategies, universal strategies are designed to be used on any instance
• branching strategies, constraint propagation, nogood recording, non-chronological backtracking (backjumping), heuristics for variable and value ordering, portfolios and restart strategies
• Luby strategy (1, 1, 2, 1, 1, 2, 4, 1, 1, 2, 1, 1, 2, 4, 8, 1, …)
• Best combinations of these techniques give robust backtracking algorithms that can routinely solve large, hard instances that are of practical importance grows linearly
• Walsh strategy (1, r, r2, r3, …), r > 1
grows exponentially
7
Outline
Unfortunately…
• Introduction
• Often easy to state a CSP
• Constraint propagation
• Much harder is to design an efficient CSP given a solver • what is a good model depends on algorithm
• Backtracking search
• choice of variables defines search space
• Complexity issues
• choice of constraints defines • how search space can be reduced • how search can be guided
Computational complexity
Should we give up?
• How hard is it to solve a CSP instance?
“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.”
• 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
Wikipedia, 2009
Another exponential curve
Acceptable solving time
time (seconds)
An exponential curve
size of instance
Instances that arise in practice
8
Improving model efficiency
Reformulate the model
• 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?
• change the denotation of the variables
• Example: 4-queens
• Given a model: • add redundant constraints
• xi = j
means place the queen in column i, row j
• add symmetry-breaking
• 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
Example: crossword puzzles
means there is no queen in row i, column j
Adding redundant constraints • Improve computational efficiency of model by adding “right” constraints
1
2
3
4
6
7
8
10
11
12
13
14
16
17
18
21
22
23
15 19
20
• dead-ends encountered earlier in search process
5
a aardvark aback abacus abaft abalone abandon ...
9
… monarch monarchy monarda ... zymurgy zyrian zythum
Example of redundant constraints: block scheduling dependency DAG
variables domains {1, …, m} constraints DA+3
B
3
3 D
ED+1 alldifferent(A, B, C, D, E)
3 E
v2
v3
v4
{1, 2, 3} constraints: C
1
v1
domains:
DB+3 EC+3
Example of redundant constraints: 3-coloring variables: v 1, v 2 , v 3 , v 4 , v 5
A, B, C, D, E A
• E.g., x 1 in 4-queens problem
v1 v1 v2 v2 v3 v3 v4
v2 v3 v3 v4 v4 v5 v5
v1 = v4 v2 = v5
v5
D min(A, B) + 4
9
Symmetry in constraint models
Example of variable symmetry: block scheduling dependency DAG
variables
• Many constraint models contain symmetry
A, B, C, D, E
• variables are “interchangeable”
domains
• values are “interchangeable”
{1, …, m}
• variable-value symmetry
constraints
• As a result, when searching for a solution: • search tree contains many equivalent subtrees
DA+3
• if a subtree does not contain a solution, neither will equivalent subtrees elsewhere in the tree
DB+3
A
B
3
3
EC+3
• failing to recognize equivalent subtrees results in needless search
ED+1 cardinality(A, B, C, D, E, 1)
D
C
1
3 E
Variables A and B are symmetric
Example of variable symmetry: block scheduling dependency DAG
Example of value symmetry: 3-coloring variables: v 1, v 2 , v 3 , v 4 , v 5 domains:
v1
v2
v3
v4
{1, 2, 3} constraints: v1 v1 v2 v3 v3 v4
…
Example of value symmetry: 3-coloring
= = = = =
1 2 2 1 3
v5
Example of value symmetry: 3-coloring
A solution v1 v2 v3 v4 v5
v2 v3 v4 v4 v5 v5
Another solution v1
v2
v3
v4
v1 v2 v3 v4 v5
= = = = =
1 2 2 1 3
v1
v2
v3
v4
Mapping
v5
v5
10
Example of variable-value symmetry: 4-queens variables: x1, x2 , x3 , x4
x1
domains:
x4
1
2
3
4
13
9
5
1
16
15
14
13
4
8
12
16
5
6
7
8
14
10
6
2
12
11
10
9
3
7
11
15
9
10
11
12
15
11
7
3
8
7
6
5
2
6
10
14
13
14
15
16
16
12
8
4
4
3
2
1
1
5
9
13
identity
constraints: x2 x3 x4 x3 x4 x4
x3
1
{1, 2, 3, 4} x1 x1 x1 x2 x2 x3
x2
Symmetries for 4-queens
| x1 – x2 | 1 | x1 – x3 | 2 | x1 – x4 | 3 | x2 – x3 | 1 | x2 – x4 | 2 | x3 – x4 | 1
rotate 90
3 4
Example of variable-value symmetry: 4-queens A partial non-solution x1 = 1
13
14
15
16
4
3
2
1
16
12
8
4
1
5
9
13
9
10
11
12
8
7
6
5
15
11
7
3
2
6
10
14
5
6
7
8
12
11
10
9
14
10
6
2
3
7
11
15
1
2
3
4
16
15
14
13
13
9
5
1
4
8
12
16
x1 1
vertical axis
diagonal 1
diagonal 2
Break symmetry by adding constraints to model • leave at least one solution
x1
x1 = 1
rotate 270
2
horizontal axis
Another partial non-solution x4 = 4
rotate 180
1
x2
x3
x4
• eliminate some/all symmetric solutions and non-solutions
Q
2 3
x4 = 4
x4 4
4
Breaking symmetry by adding constraints to model: block scheduling dependency DAG
variables
variables: v 1, v 2 , v 3 , v 4 , v 5
A, B, C, D, E domains {1, …, m} constraints DA+3
A
B
ED+1 cardinality(A, B, C, D, E, 0, 1)
v1
v2
v3
v4
domains: 3
{1, 2, 3}
3
constraints: D
C
1
3
DB+3 EC+3
Breaking symmetry by adding constraints to model: 3-coloring
vi vj if vi and vj are adjacent fixing colors in a single clique
v5
E
BA+1
11
Breaking symmetry by adding constraints to model: 4-queens
x1
variables: x1, x2 , x3 , x4
x1
domains: {1, 2, 3, 4} constraints:
1 2
xi xi | xi – xj | | i – j | break horizontal symmetry by adding x1 ≤ 2 break vertical symmetry by adding x2 ≤ x3
Danger of adding symmetry breaking constraints
3
x2
x3
x4
x2
x1
x2
x3
Q
2
Q Q
3 4
x4
Q
1
Q
3 4
x4
Q
1 2
x3
Q Q
4 adding x2 ≤ x3 removes this solution
adding x1 ≤ 2 removes this solution
but …
12