branch_and_bound/
lib.rs

1//! This library implements generic branch-and-bound and backtracking solver.
2//!
3//! Branch-and-bound (and backtracking, which is its special case) is the method
4//! of solving an optimization problem by recursively breaking a problem down
5//! to subproblems and then solving them. Unlike brute-force, branch-and-bound
6//! will discard a subproblem if it discovers that the best potentially obtainable
7//! solution to this subproblem is not better than the current best solution
8//! (aka incumbent).
9//!
10//! To use the library, one shell implement a type that represents a problem
11//! (subproblem) and implement the [`Subproblem`] trait for it.
12//!
13//! One can then [`solve`] an instance of problem using one of the predefined
14//! methods (DFS, BFS, BeFS, etc) or use [`solve_with_container`], through
15//! which custom strategies can be implemented.
16
17pub mod bnb_aware_containers;
18
19use bnb_aware_containers::BinaryHeapExt;
20pub use bnb_aware_containers::BnbAwareContainer;
21
22/// Represents the set of subproblems of an intermediate problem
23/// or the value of the objective function of a feasible solution (leaf node).
24pub enum SubproblemResolution<Node: ?Sized, Score> {
25    /// Subproblems of an intermediate problem
26    Branched(Box<dyn Iterator<Item = Node>>),
27    /// The value of the objective function of a feasible solution
28    Solved(Score),
29}
30// TODO: Consider an alternative implementation by making the iterator
31// type a generic variable rather than a `dyn`
32
33/// A problem (subproblem) to be solved with branch-and-bound
34pub trait Subproblem {
35    // Major TODO: Let `Subproblem` have a non-static lifetime. This will simplify
36    // usage of the library a lot.
37    //
38    // Major major TODO: Allow `Subproblem` to return its children one by one,
39    // rather than all at a time. This way, DFS could be implemented efficiently
40    // by the users of the library.
41
42    /// Return type of the boundary and the objective function.
43    /// Higher score is better.
44    type Score: Ord;
45
46    /// Evaluates a problem space.
47    ///
48    /// If the space is to be broken further into subproblems, returns
49    /// a sequence of subproblems (may be empty, which discards
50    /// the current subspace).
51    ///
52    /// If the space consists of just one feasible solution to be solved
53    /// directly, returns the score, which is the value of the objective
54    /// function at the solution. The node is then considered a successful candidate.
55    ///
56    /// The method may mutate `self` as follows:
57    /// - If `SubproblemResolution::Branched` is returned, the library shall
58    ///   discard the object after that, so any changes to `self` are allowed, even
59    ///   if after the changes it no longer represents the original subproblem;
60    /// - If `SubproblemResolution::Solved` is returned, the library will use
61    ///   the subproblem object as a successful candidate, so mutations to the internal
62    ///   state are allowed, as long as `self` continues to represent the same
63    ///   subproblem.
64    fn branch_or_evaluate(&mut self) -> SubproblemResolution<Self, Self::Score>;
65
66    /// Value of the boundary function at the problem space.
67    ///
68    /// The boundary function gives an upper-boundary of the best solution
69    /// that could potentially be found in this subproblem space. The value of
70    /// the boundary function must be greater than or equal to every value of
71    /// the objective score of any subproblem reachable through consecutive
72    /// `.branch_or_evaluate` calls.
73    ///
74    /// If at some point in the search process a subproblem's `.bound()` value
75    /// is less than or equal to the current best solution, the subproblem is
76    /// discarded (because no better solution will be found in its subtree).
77    fn bound(&self) -> Self::Score;
78}
79
80/// Solve a problem with branch-and-bound / backtracking using a custom subproblem
81/// container with a custom strategy.
82///
83/// Until the container is empty, a subproblem is popped from the container and evaluated;
84/// when a subproblem is branched, the generated subnodes are put into the container to be
85/// retrieved in the following iterations.
86///
87/// A container is, thus, responsible for the order in which subproblems will be examined,
88/// and can also implement additional features, such as early termination based on
89/// the current best value, early termination based on the number of iterations,
90/// eager or lazy evaluation, etc.
91///
92/// `solve_with_container` should be preferred for advanced use cases (e.g., custom order
93/// or unusual early terination conditions). If you want one of the basic options,
94/// use [`solve`].
95pub fn solve_with_container<Node, Container>(mut container: Container) -> Option<Node>
96where
97    Node: Subproblem,
98    Container: BnbAwareContainer<Node>,
99{
100    // Best candidate: its objective score and the node itself
101    let mut best: Option<(Node::Score, Node)> = None;
102
103    // `container` should initially contain the root node (or even several nodes)
104
105    while let Some(mut candidate) = container.pop_with_incumbent(best.as_ref().map(|x| &x.0)) {
106        match candidate.branch_or_evaluate() {
107            // Intermediate subproblem
108            SubproblemResolution::Branched(subproblems) => {
109                for node in subproblems {
110                    container.push_with_incumbent(node, best.as_ref().map(|x| &x.0));
111                }
112            }
113
114            // Leaf node
115            SubproblemResolution::Solved(candidate_score) => {
116                best = match best {
117                    None => Some((candidate_score, candidate)),
118                    Some((incumbent_score, incumbent)) => {
119                        if incumbent_score < candidate_score {
120                            // Replace the old (boundary) score with the objective score
121                            Some((candidate_score, candidate))
122                        } else {
123                            Some((incumbent_score, incumbent))
124                        }
125                    }
126                }
127            }
128        }
129    }
130
131    best.map(|(_, incumbent)| incumbent)
132}
133
134type NodeCmp<Node> = dyn Fn(&Node, &Node) -> std::cmp::Ordering;
135
136/// Order of traversing the subproblem tree with `solve`. See variants' docs for details.
137pub enum TraverseMethod<Node> {
138    /// Depth-first search (DFS): descends into every subtree until reaches the leaf node
139    /// (or determines that a subtree is not worth descending into because the boundary
140    /// value is not better than the incumbent's objective score).
141    ///
142    /// TODO: stabilize and specify the order in which siblings of a certain node are processed,
143    /// so that the user may return nodes in the order of desired processing.
144    ///
145    /// For typical boundary functions, uses significantly less memory compared to best-first
146    /// and breadth-first search.
147    DepthFirst,
148
149    /// Breadth-first search (BFS): Traverses the subproblem tree layer by layer.
150    /// The processing order among nodes on the same layer is unspecified.
151    ///
152    /// For typical boundary functions, behaves similar to best-first search but uses
153    /// a simpler internal data structure to store subproblems to be processed.
154    BreadthFirst,
155
156    /// Best-first search (BeFS): traverses the tree in many directions simultaneously,
157    /// on every iteration selects and evaluates the subproblem with the best value of
158    /// the boundary function. All its children become candidates for the next selection
159    /// (as long as their boundary value is better than the incumbent's objective score).
160    ///
161    /// The processing order among subproblems with the same boundary value is unspecified.
162    ///
163    /// For typical boundary functions, behaves similar to breadth-first search but selects
164    /// subproblems more optimally.
165    BestFirst,
166
167    /// Like best-first search but selects subproblems in the custom order, based on the
168    /// given comparator `.cmp`.
169    ///
170    /// Processes subproblems in the order specified by `.cmp`: subproblems that compare
171    /// *greater* are processed *first*! The processing order among subproblems that
172    /// compare equal is unspecified.
173    ///
174    /// Set `.cmp_superceeds_bound` to `true` only if `.cmp` guarantees that
175    ///
176    /// if `cmp(subproblem_a, subproblem_b) == Ordering::Less`
177    ///
178    /// then `subproblem_a.bound() < subproblem_b.bound()`
179    ///
180    /// (in other words, the order defined by `.cmp` is a specialized order / super-order
181    /// with respect to the order defined by `Subproblem::bound`).
182    ///
183    /// If `.cmp_superceeds_bound` is set, the search will terminate as soon as the candidate
184    /// that is best according to `.cmp` has the boundary value less (i.e., worse) than that of the
185    /// current incumbent.
186    Custom {
187        cmp: Box<NodeCmp<Node>>,
188        cmp_superceeds_bound: bool,
189    },
190}
191
192/// Solve a problem with branch-and-bound / backtracking, using one of the default strategies.
193///
194/// Walks the subproblem tree (`initial` is the root) according to the method specified by `method`.
195///
196/// `solve` should be preferred for simple scenareous (i.e., a single initial node,
197/// one of the default search strategy implementations). For more advanced use cases, use
198/// [`solve_with_container`].
199#[inline]
200pub fn solve<Node: Subproblem>(initial: Node, method: TraverseMethod<Node>) -> Option<Node> {
201    use TraverseMethod::*;
202
203    match method {
204        BestFirst => {
205            let pqueue = BinaryHeapExt {
206                heap: binary_heap_plus::BinaryHeap::from_vec_cmp(
207                    vec![initial],
208                    |n1: &Node, n2: &Node| n1.bound().cmp(&n2.bound()),
209                ),
210                stop_early: true,
211            };
212            solve_with_container(pqueue)
213        }
214
215        Custom {
216            cmp,
217            cmp_superceeds_bound: stop_early,
218        } => {
219            let pqueue = BinaryHeapExt {
220                heap: binary_heap_plus::BinaryHeap::from_vec_cmp(vec![initial], cmp),
221                stop_early,
222            };
223            solve_with_container(pqueue)
224        }
225
226        BreadthFirst => {
227            let queue = std::collections::VecDeque::from_iter([initial]);
228            solve_with_container(queue)
229        }
230
231        DepthFirst => {
232            let stack = vec![initial];
233            solve_with_container(stack)
234        }
235    }
236}