Version: 0.6.22
1. Combinatorial Search#
- Lecture:
Lecture 7.1
(slides)- Objectives:
Understand what is combinatorial search, and how to approach it
- Concepts:
Combinations, pruning, branch-and-bound, Knapsack problem
- Implementation:
In the previous module, we have already solved two optimization problems: finding the shortest path between two vertices of a graph, and finding its minimum spanning tree. In both cases, there are many possible answers (many paths, many spanning trees), and we want the best one. We were lucky: These problems have enough structure for a clever algorithm, such as Dijkstra’s or Prim’s, to find the best answer without looking at all the possibilities.
Many optimization problems are not so kind. Which items shall we pack in a box to take the most valuable? How shall we place queens on a chessboard so that none of them can capture another? Which route visits all our customers with the least driving? For these, no such shortcut is known, and we have to search.
In this lecture, we look at the simplest way to search: trying all the possibilities, and then at how to be smart about it, so that we do not try them all.
1.1. Optimization Problems#
An optimization problem asks us to find, among a set of possible solutions, one that satisfies some constraints and that is the best according to some objective. Sometimes we do not even care about the best: Any solution that satisfies the constraints will do.
In this lecture, the set of possible solutions is finite, or at least countable: We can, in principle, list all the solutions, one after the other. This is called discrete optimization. When the possible solutions form a continuum, such as all the real numbers between 0 and 1, we speak of continuous optimization, which needs other tools (see the lecture on heuristic search).
Here are a few classic examples:
- The N Queens Problem
Place \(n\) queens on a chessboard with \(n\) rows and \(n\) columns, so that no queen can capture another one. Here, any solution will do.
- Sudoku
Fill a grid with digits so that every row, column and block contains each digit once.
- Motion Planning
Find a path for a robot among obstacles, as short as possible.
- The Traveling Salesperson Problem (TSP)
Find the shortest tour that visits a set of cities exactly once and comes back to the starting point.
- The Knapsack Problem
We have a box that can carry a limited weight, and items that each have a weight and a value. Which items shall we put in the box, to maximize their total value?
For four queens, there are exactly two solutions, shown on Fig. 1.36.
Fig. 1.36 The two ways to place four queens on a four-by-four chessboard so that none can capture another.#
We will use the knapsack problem as our running example. Say the box can carry 10 kg, and we can choose among these four items:
Item |
Weight |
Value |
|---|---|---|
Music instruments |
2 kg |
9 000 NOK |
Laptop |
7 kg |
3 000 NOK |
Books |
3 kg |
4 000 NOK |
Cooking tools |
8 kg |
7 000 NOK |
The constraint is that the items we take must not weigh more than 10 kg, and the objective is to maximize their total value.
1.2. Brute Force#
Suppose you are locked out of a vault that opens with a four-digit code. There is no clever way to find the code: You try 0000, then 0001, then 0002, and so on, until the vault opens. There are only 10 000 codes to try, so this brute force, also called exhaustive search, will succeed in a few minutes at worst.
Brute force is simple, and always works. The question is whether we can afford it. Computers perform millions of operations per second, so a million possibilities is easy, and a billion is manageable. How quickly do we reach these numbers?
What we enumerate |
How many |
Example |
|---|---|---|
Subsets of \(n\) items |
\(2^n\) |
20 items: 1 048 576 |
Orders of \(n\) items |
\(n!\) |
10 items: 3 628 800 |
Words of \(k\) letters |
\(26^k\) |
5 letters: 11 881 376 |
Brute force works for small problems: With 20 items, we can try all the subsets in a second. But the numbers grow explosively: Each additional item doubles the number of subsets. With 100 items, there are about \(1.3 \times 10^{30}\) subsets, which no computer will ever enumerate. We need to be smarter.
1.3. Enumerating the Solutions#
How do we list all the possible solutions of a problem, without missing any, and without any twice? We build them step by step, by making one choice at a time. For the knapsack, we decide, item after item, whether we take it or leave it. We call configuration a solution under construction, where some choices have been made, and others have not.
The configurations form a tree, shown on Fig. 1.38. The root is the empty configuration, where nothing is decided. Each level of the tree is one more choice that a solution requires, and each possible choice leads to a new configuration, a child. A solution needs all its choices to be made. This is why only the leaves of the tree, the complete configurations, can be solutions. The other nodes are partial: Some choices are still open. With four items, the tree has \(2^4 = 16\) leaves, and enumerating all the solutions means visiting all of them.
What the choices are depends on the problem. A level of the tree is always something that must be decided for a solution to exist:
Problem |
One level is… |
The choices are… |
A leaf is… |
|---|---|---|---|
Knapsack |
an item |
take it, or leave it |
a decision for every item |
N queens |
a queen (a column of the board) |
the row where it goes |
all the queens placed |
Sudoku |
an empty cell |
the digit to write, from 1 to 9 |
all the cells filled |
TSP |
a step of the tour |
the next city to visit |
a tour through all the cities |
The N queens problem, with four queens, is the easiest to picture (Fig. 1.37). The first level places the queen of the first column, in one of the four rows. The second level places the queen of the second column, and so on. A leaf is a board with four queens, but it is a solution only if no queen can capture another.
Fig. 1.37 The first levels of the tree of the four queens problem. Each level places one more queen. In red, boards where two queens can capture one another.#
Fig. 1.38 All the ways to fill the box, with the weight carried in each configuration. The leaves in red weigh more than 10 kg, so they are not solutions.#
A search needs only a few things from the problem. We describe them as an abstract data type, which we call the Search Problem. This is all that the algorithms below know about the problem.
- problem.empty() Configuration#
Return the configuration where nothing is decided yet.
- Preconditions:
None
- Postconditions:
None
- problem.children(c: Configuration) Sequence[Configuration]#
Return the configurations we get by making one more choice. For the knapsack, there are two: one where we take the next item, and one where we leave it.
- Preconditions:
The configuration \(c\) is not complete.
\[\neg \, is\_complete(c)\]
- Postconditions:
None
- problem.is_complete(c: Configuration) Boolean#
Tell whether everything is decided in the configuration \(c\), that is, whether it is a leaf of the tree.
- Preconditions:
None
- Postconditions:
None
- problem.is_valid(c: Configuration) Boolean#
Tell whether the configuration \(c\) respects the constraints of the problem so far. For the knapsack, the items must not weigh more than the box can carry. A partial configuration can be valid so far, and still not be a solution: A solution is a configuration that is both complete and valid.
- Preconditions:
None
- Postconditions:
Once a configuration breaks a constraint, so do all of its children. If a configuration is not valid, none of its children is.
\[\neg \, is\_valid(c) \implies \forall c' \in children(c), \, \neg \, is\_valid(c')\]
The simplest search goes down the tree, always making the next choice, until the configuration is complete. Then it backs up to the last choice, and tries the other possibility. This is called backtracking, and it is a depth-first traversal of the tree of configurations.
function exhaustive(problem, c):
if problem.is_complete(c):
if problem.is_valid(c):
process c
else:
for each next in problem.children(c):
exhaustive(problem, next)
exhaustive(problem, problem.empty())
Implementation in Java
A problem is anything that offers the operations above. In Java, we describe them with an interface, which the problems we want to search implement:
1public interface Problem<C> {
2
3 C empty();
4
5 List<C> children(C configuration);
6
7 boolean isComplete(C configuration);
8
9 boolean isValid(C configuration);
10}
Here is how the knapsack provides them. A configuration is the list of
the choices made so far, where true means that we take the item,
and false that we leave it.
1public List<Boolean> empty() {
2 return List.of();
3}
4
5// For the next item, we can take it or leave it
6public List<List<Boolean>> children(List<Boolean> configuration) {
7 return List.of(
8 with(configuration, true),
9 with(configuration, false)
10 );
11}
12
13public boolean isComplete(List<Boolean> configuration) {
14 return configuration.size() == items.size();
15}
16
17// The items we take must not weigh more than the box can carry
18public boolean isValid(List<Boolean> configuration) {
19 return weightOf(configuration) <= capacity;
20}
Backtracking is then a short recursive method:
1// All the solutions, found by trying every possible sequence of choices
2public static <C> List<C> exhaustive(Problem<C> problem) {
3 var solutions = new ArrayList<C>();
4 exhaustive(problem, problem.empty(), solutions);
5 return solutions;
6}
7
8private static <C> void exhaustive(Problem<C> problem, C configuration, List<C> solutions) {
9 if (problem.isComplete(configuration)) {
10 if (problem.isValid(configuration)) {
11 solutions.add(configuration);
12 }
13 } else {
14 for (var next : problem.children(configuration)) {
15 exhaustive(problem, next, solutions);
16 }
17 }
18}
1.4. Pruning#
Look at the tree again. Once the box is too heavy, it does not matter what we decide for the remaining items: It will stay too heavy. All the configurations below are invalid, so there is no point in building them. This is pruning: Cutting off a whole branch of the tree as soon as we know it leads nowhere.
Fig. 1.39 With pruning, we stop at the first configuration that is too heavy, and never visit what is below it.#
Pruning only needs a small change: We check the constraints at every
configuration, and no longer only at the leaves. This is valid, because
of the property of is_valid: if a configuration is not valid, none
of its children is.
function with_pruning(problem, c):
if not problem.is_valid(c):
return
if problem.is_complete(c):
process c
else:
for each next in problem.children(c):
with_pruning(problem, next)
In our small example, pruning saves little. But on larger problems, it makes the difference between hopeless and instantaneous. Take the N queens problem, which we solve by placing the queens one column after the other. For six queens, trying everything means looking at 9 331 configurations, whereas pruning looks at only 149. For eight queens, pruning finds all the 92 solutions by looking at fewer than 2 000 configurations, although there are over 16 million ways to place eight queens, one per column.
If we only need one solution, we can do even better: We stop at the first complete and valid configuration we find, and do not visit the rest of the tree.
Implementation in Java
The search with pruning is the same as before, with the validity check at every configuration:
1// All the solutions, but we give up on a configuration as soon as it is
2// invalid: none of its children can be valid
3public static <C> List<C> withPruning(Problem<C> problem) {
4 var solutions = new ArrayList<C>();
5 withPruning(problem, problem.empty(), solutions);
6 return solutions;
7}
8
9private static <C> void withPruning(Problem<C> problem, C configuration, List<C> solutions) {
10 if (!problem.isValid(configuration)) {
11 return;
12 }
13 if (problem.isComplete(configuration)) {
14 solutions.add(configuration);
15 } else {
16 for (var next : problem.children(configuration)) {
17 withPruning(problem, next, solutions);
18 }
19 }
20}
To stop at the first solution, each call returns what it found
(an Optional is either a solution, or nothing), and we stop looking
as soon as there is one:
1// Any solution: we stop as soon as we find one
2public static <C> Optional<C> anySolution(Problem<C> problem) {
3 return anySolution(problem, problem.empty());
4}
5
6private static <C> Optional<C> anySolution(Problem<C> problem, C configuration) {
7 if (!problem.isValid(configuration)) {
8 return Optional.empty();
9 }
10 if (problem.isComplete(configuration)) {
11 return Optional.of(configuration);
12 }
13 for (var next : problem.children(configuration)) {
14 var solution = anySolution(problem, next);
15 if (solution.isPresent()) {
16 return solution;
17 }
18 }
19 return Optional.empty();
20}
1.5. Branch-and-Bound#
What if we want the best solution, and not just any? We cannot stop at the first solution we find: A better one may be hiding in another branch. The obvious approach is to enumerate all the solutions, and to keep the best one, but that brings us back to trying everything.
Yet, we can use what we have found so far. Suppose we have already found a solution worth 16 000 NOK. If we can tell that nothing below some configuration can be worth more than 16 000, there is no point in exploring it. We can prune it, just as we prune the invalid configurations.
To tell this, the problem must anticipate what exploring a configuration further can lead to. Only a solution has a value. For any other configuration, we need an optimistic estimate of the best outcome, which we call its upper bound. It is allowed to be too optimistic, but it must never underestimate: Nothing below the configuration may be worth more than its upper bound. For the knapsack, a simple upper bound is the value of the items we have already taken, plus the value of all the items we have not decided about, as if the box could carry everything. Reality can only be worse. For a solution, where nothing remains to decide, the upper bound is simply its value.
- optimization_problem.value(s: Configuration) Number#
Return how good the solution \(s\) is. For the knapsack, this is the total value of the items we take. The higher, the better.
- Preconditions:
The configuration \(s\) is a solution, that is, it is complete and valid.
\[is\_complete(s) \, \land \, is\_valid(s)\]
- Postconditions:
None
- optimization_problem.upper_bound(c: Configuration) Number#
Return an optimistic estimate of the best outcome of exploring the configuration \(c\) further: What we can hope for at best.
- Preconditions:
None
- Postconditions:
The upper bound never underestimates. No solution below \(c\) is worth more than its upper bound.
\[is\_complete(s) \, \land \, is\_valid(s) \, \land \, s \text{ is below } c \implies value(s) \leq upper\_bound(c)\]For a solution, the upper bound is its value.
\[is\_complete(s) \, \land \, is\_valid(s) \implies upper\_bound(s) = value(s)\]
This is the branch-and-bound algorithm. It goes through the tree as before, remembering the best solution found so far. We branch, that is, we explore the children of a configuration, only if its upper bound shows that it still has a chance to beat the best solution. As long as we have not found any solution, we cannot cut anything: We simply go down to the first leaf.
function branch_and_bound(problem, c):
if not problem.is_valid(c):
return
if best exists and optimization_problem.upper_bound(c) ≤ value of best:
return # Even at best, c is no better than best
if problem.is_complete(c):
best ← c
else:
for each next in problem.children(c):
branch_and_bound(problem, next)
branch_and_bound(problem, problem.empty())
Fig. 1.40 shows the algorithm on our example. The number in each circle is the upper bound of the configuration, in thousands of NOK. The search goes down the tree, taking each item first. Until it reaches its first solution, worth 12 000, it cannot cut anything. Then it finds a second solution, worth 13 000, and a third one, worth 16 000. The right half of the tree starts with an upper bound of 14 000, below what we already have: We never look at it. The algorithm expands only 7 of the 15 configurations that are not leaves, whereas the search with pruning alone expands 14.
Fig. 1.40 Branch-and-bound on the knapsack. Once we have a solution worth 16 000, the configurations whose upper bound is not above are cut.#
The order matters. Branch-and-bound works best when we find a good solution early, because a high “best so far” lets us cut more. Going through the choices in a different order can therefore change the work a lot (although never the result). Instead of simply going deep first, we can always continue with the most promising configuration, that is, the one with the highest upper bound. We keep the configurations still to explore in a priority queue, where the priority is the upper bound. As we explore the configurations by decreasing upper bound, we can stop as soon as the best upper bound that remains is not above the best solution found: Nothing left can beat it. This is called best-first search.
The quality of the upper bound is what makes branch-and-bound effective. The tighter it is, the more we cut. A bound that is too loose cuts nothing, and we are back to trying everything. In the worst case, branch-and-bound is no better than exhaustive search, and takes time exponential in the number of choices. But on many problems, a good bound makes it practical far beyond what brute force can do.
Implementation in Java
A problem we want to optimize offers two more operations, the value and the upper bound:
1public interface OptimizationProblem<C> extends Problem<C> {
2
3 int value(C solution);
4
5 int upperBound(C configuration);
6}
For the knapsack, they are:
1// The total value of the items we take
2public int value(List<Boolean> solution) {
3 return valueOfTakenItems(solution);
4}
5
6// At best, we will be able to take all the items we have not decided about
7public int upperBound(List<Boolean> configuration) {
8 int total = valueOfTakenItems(configuration);
9 for (int i = configuration.size(); i < items.size(); i++) {
10 total += items.get(i).value();
11 }
12 return total;
13}
14
15private int valueOfTakenItems(List<Boolean> configuration) {
16 int total = 0;
17 for (int i = 0; i < configuration.size(); i++) {
18 if (configuration.get(i)) {
19 total += items.get(i).value();
20 }
21 }
22 return total;
23}
Branch-and-bound keeps the best solution found so far in a small
helper, Best:
1// The best solution, with branch-and-bound. Once we have found a solution, we
2// give up on a configuration if even its upper bound is no better than it
3public static <C> Optional<C> branchAndBound(OptimizationProblem<C> problem) {
4 var best = new Best<C>();
5 branchAndBound(problem, problem.empty(), best);
6 return Optional.ofNullable(best.solution);
7}
8
9private static <C> void branchAndBound(OptimizationProblem<C> problem, C configuration, Best<C> best) {
10 if (!problem.isValid(configuration)) {
11 return;
12 }
13 if (best.isAtLeastAsGoodAs(problem, configuration)) {
14 return;
15 }
16 if (problem.isComplete(configuration)) {
17 best.solution = configuration;
18 } else {
19 for (var next : problem.children(configuration)) {
20 branchAndBound(problem, next, best);
21 }
22 }
23}
24
25// The best solution found so far, if any
26private static class Best<C> {
27 C solution = null;
28
29 // Is the best solution at least as good as anything this configuration
30 // can lead to? Before we have found any solution, nothing is.
31 boolean isAtLeastAsGoodAs(OptimizationProblem<C> problem, C configuration) {
32 return solution != null && problem.upperBound(configuration) <= problem.value(solution);
33 }
34}
The best-first variant uses a priority queue, which always gives us the configuration with the highest upper bound:
1// The same, but we always continue with the most promising configuration
2// (the one with the highest upper bound), instead of going deep first
3public static <C> Optional<C> bestFirst(OptimizationProblem<C> problem) {
4 var promising = new PriorityQueue<C>(
5 (first, second) -> Integer.compare(problem.upperBound(second), problem.upperBound(first))
6 );
7 promising.add(problem.empty());
8
9 var best = new Best<C>();
10 while (!promising.isEmpty()) {
11 var configuration = promising.poll();
12 if (!problem.isValid(configuration)) {
13 continue;
14 }
15 if (best.isAtLeastAsGoodAs(problem, configuration)) {
16 break; // The most promising one is not promising enough: we are done
17 }
18 if (problem.isComplete(configuration)) {
19 best.solution = configuration;
20 } else {
21 promising.addAll(problem.children(configuration));
22 }
23 }
24 return Optional.ofNullable(best.solution);
25}
