-
Notifications
You must be signed in to change notification settings - Fork 6
/
bnb.rs
218 lines (191 loc) · 7.07 KB
/
bnb.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use core::cmp::Reverse;
use crate::float::Ordf32;
use super::CoinSelector;
use alloc::collections::BinaryHeap;
/// An [`Iterator`] that iterates over rounds of branch and bound to minimize the score of the
/// provided [`BnbMetric`].
#[derive(Debug)]
pub(crate) struct BnbIter<'a, M: BnbMetric> {
queue: BinaryHeap<Branch<'a>>,
best: Option<Ordf32>,
/// The `BnBMetric` that will score each selection
metric: M,
}
impl<'a, M: BnbMetric> Iterator for BnbIter<'a, M> {
type Item = Option<(CoinSelector<'a>, Ordf32)>;
fn next(&mut self) -> Option<Self::Item> {
// {
// println!("=========================== {:?}", self.best);
// for thing in self.queue.iter() {
// println!("{} {:?}", &thing.selector, thing.lower_bound);
// }
// let _ = std::io::stdin().read_line(&mut alloc::string::String::new());
// }
let branch = self.queue.pop()?;
if let Some(best) = &self.best {
// If the next thing in queue is not better than our best we're done.
if *best < branch.lower_bound {
// println!(
// "\t\t(SKIP) branch={} inclusion={} lb={:?}, score={:?}",
// branch.selector,
// !branch.is_exclusion,
// branch.lower_bound,
// self.metric.score(&branch.selector),
// );
return None;
}
}
// println!(
// "\t\t( POP) branch={} inclusion={} lb={:?}, score={:?}",
// branch.selector,
// !branch.is_exclusion,
// branch.lower_bound,
// self.metric.score(&branch.selector),
// );
let selector = branch.selector;
self.insert_new_branches(&selector);
if branch.is_exclusion {
return Some(None);
}
let score = match self.metric.score(&selector) {
Some(score) => score,
None => return Some(None),
};
if let Some(best_score) = &self.best {
if score >= *best_score {
return Some(None);
}
}
self.best = Some(score);
Some(Some((selector, score)))
}
}
impl<'a, M: BnbMetric> BnbIter<'a, M> {
pub(crate) fn new(mut selector: CoinSelector<'a>, metric: M) -> Self {
let mut iter = BnbIter {
queue: BinaryHeap::default(),
best: None,
metric,
};
if iter.metric.requires_ordering_by_descending_value_pwu() {
selector.sort_candidates_by_descending_value_pwu();
}
iter.consider_adding_to_queue(&selector, false);
iter
}
fn consider_adding_to_queue(&mut self, cs: &CoinSelector<'a>, is_exclusion: bool) {
let bound = self.metric.bound(cs);
if let Some(bound) = bound {
if self.best.is_none() || self.best.as_ref().unwrap() >= &bound {
let branch = Branch {
lower_bound: bound,
selector: cs.clone(),
is_exclusion,
};
println!(
"\t\t(PUSH) branch={} inclusion={} lb={:?} score={:?}",
branch.selector,
!branch.is_exclusion,
branch.lower_bound,
self.metric.score(&branch.selector),
);
self.queue.push(branch);
} else {
println!(
"\t\t( REJ) branch={} inclusion={} lb={:?} score={:?}",
cs,
!is_exclusion,
bound,
self.metric.score(cs),
);
}
} else {
println!(
"\t\t(NO B) branch={} inclusion={} score={:?}",
cs,
!is_exclusion,
self.metric.score(cs),
);
}
}
fn insert_new_branches(&mut self, cs: &CoinSelector<'a>) {
let (next_index, next) = match cs.unselected().next() {
Some(c) => c,
None => return, // exhausted
};
let mut inclusion_cs = cs.clone();
inclusion_cs.select(next_index);
self.consider_adding_to_queue(&inclusion_cs, false);
// for the exclusion branch, we keep banning if candidates have the same weight and value
let mut is_first_ban = true;
let mut exclusion_cs = cs.clone();
let to_ban = (next.value, next.weight);
for (next_index, next) in cs.unselected() {
if (next.value, next.weight) != to_ban {
break;
}
let (index, candidate) = exclusion_cs
.candidates()
.find(|(i, _)| *i == next_index)
.expect("must have index since we are planning to ban it");
if is_first_ban {
is_first_ban = false;
} else {
println!("banning: [{}] {:?}", index, candidate);
}
exclusion_cs.ban(next_index);
}
self.consider_adding_to_queue(&exclusion_cs, true);
}
}
#[derive(Debug, Clone)]
struct Branch<'a> {
lower_bound: Ordf32,
selector: CoinSelector<'a>,
is_exclusion: bool,
}
impl<'a> Ord for Branch<'a> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
// NOTE: Reverse comparision `lower_bound` because we want a min-heap (by default BinaryHeap
// is a max-heap).
// NOTE: We tiebreak equal scores based on whether it's exlusion or not (preferring
// inclusion). We do this because we want to try and get to evaluating complete selection
// returning actual scores as soon as possible.
core::cmp::Ord::cmp(
&(Reverse(&self.lower_bound), !self.is_exclusion),
&(Reverse(&other.lower_bound), !other.is_exclusion),
)
}
}
impl<'a> PartialOrd for Branch<'a> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<'a> PartialEq for Branch<'a> {
fn eq(&self, other: &Self) -> bool {
self.lower_bound == other.lower_bound
}
}
impl<'a> Eq for Branch<'a> {}
/// A branch and bound metric where we minimize the [`Ordf32`] score.
///
/// This is to be used as input for [`CoinSelector::run_bnb`] or [`CoinSelector::bnb_solutions`].
pub trait BnbMetric {
/// Get the score of a given selection.
///
/// If this returns `None`, the selection is invalid.
fn score(&mut self, cs: &CoinSelector<'_>) -> Option<Ordf32>;
/// Get the lower bound score using a heuristic.
///
/// This represents the best possible score of all descendant branches (according to the
/// heuristic).
///
/// If this returns `None`, the current branch and all descendant branches will not have valid
/// solutions.
fn bound(&mut self, cs: &CoinSelector<'_>) -> Option<Ordf32>;
/// Returns whether the metric requies we order candidates by descending value per weight unit.
fn requires_ordering_by_descending_value_pwu(&self) -> bool {
false
}
}