Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/bbs_knapsack_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ fn main() {
let lower_bound_fn = |n: &Node| {
let current_profit = total_profit(n);
let max_remained_profit: u32 = profits[n.len()..].into_iter().sum();
u32::MAX - (current_profit + max_remained_profit)
Some(u32::MAX - (current_profit + max_remained_profit))
};

let cost_fn = |n: &Node| u32::MAX - total_profit(n);
let cost_fn = |n: &Node| Some(u32::MAX - total_profit(n));

let leaf_check_fn = |n: &Node| n.len() == total_items;

Expand Down
32 changes: 18 additions & 14 deletions src/bbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ where
N: Clone,
FN: FnMut(&N) -> IN,
IN: IntoIterator<Item = N>,
FC: Fn(&N) -> C,
FC: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
{
type Item = N;

fn next(&mut self) -> Option<Self::Item> {
if let Some(n) = self.to_see.pop() {
if (self.lower_bound_fn)(&n) <= self.current_best_cost {
for s in (self.successor_fn)(&n) {
self.to_see.push(s.clone());
// get lower bound
if let Some(lb) = (self.lower_bound_fn)(&n) {
if lb <= self.current_best_cost {
for s in (self.successor_fn)(&n) {
self.to_see.push(s.clone());
}
}
}
Some(n)
Expand All @@ -40,7 +43,7 @@ where
N: Clone,
FN: FnMut(&N) -> IN,
IN: IntoIterator<Item = N>,
FC: Fn(&N) -> C,
FC: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
{
}
Expand All @@ -55,7 +58,7 @@ where
N: Clone,
FN: FnMut(&N) -> IN,
IN: IntoIterator<Item = N>,
FC: Fn(&N) -> C,
FC: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
{
BbsReachable {
Expand Down Expand Up @@ -86,8 +89,8 @@ where
N: Clone,
IN: IntoIterator<Item = N>,
FN: FnMut(&N) -> IN,
FC1: Fn(&N) -> C,
FC2: Fn(&N) -> C,
FC1: Fn(&N) -> Option<C>,
FC2: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
FR: Fn(&N) -> bool,
{
Expand All @@ -100,10 +103,11 @@ where
}
let n = op_n.unwrap();
if leaf_check_fn(&n) {
let cost = cost_fn(&n);
if res.current_best_cost > cost {
res.current_best_cost = cost;
best_leaf_node = Some(n)
if let Some(cost) = cost_fn(&n) {
if res.current_best_cost > cost {
res.current_best_cost = cost;
best_leaf_node = Some(n)
}
}
}
}
Expand Down Expand Up @@ -175,10 +179,10 @@ mod test {
let lower_bound_fn = |n: &Node| {
let current_profit = total_profit(n);
let max_remained_profit: u32 = profits[n.len()..].into_iter().sum();
u32::MAX - (current_profit + max_remained_profit)
Some(u32::MAX - (current_profit + max_remained_profit))
};

let cost_fn = |n: &Node| u32::MAX - total_profit(n);
let cost_fn = |n: &Node| Some(u32::MAX - total_profit(n));

let leaf_check_fn = |n: &Node| n.len() == total_items;

Expand Down
6 changes: 3 additions & 3 deletions src/bfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ where
N: Clone,
IN: IntoIterator<Item = N>,
FN: FnMut(&N) -> IN,
FC: Fn(&N) -> C,
FC: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
FR: Fn(&N) -> bool,
{
bms(
start,
successor_fn,
|_| C::min_value(),
|_| Some(C::min_value()),
usize::MAX,
usize::MAX,
cost_fn,
Expand Down Expand Up @@ -95,7 +95,7 @@ mod test {
}
})
.sum();
u32::MAX - cost
Some(u32::MAX - cost)
};

let leaf_check_fn = |n: &Node| n.len() == total_items;
Expand Down
26 changes: 15 additions & 11 deletions src/bms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ where
N: Clone,
FN: FnMut(&N) -> IN,
IN: IntoIterator<Item = N>,
FC: Fn(&N) -> C,
FC: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
{
type Item = N;
Expand All @@ -64,7 +64,10 @@ where
if let Some(node) = self.to_see.pop_front() {
let mut successors: Vec<_> = (self.successor_fn)(&node)
.into_iter()
.map(|n| ((self.eval_fn)(&n), n))
.filter_map(|n| {
let cost = (self.eval_fn)(&n)?;
Some((cost, n))
})
.collect();
successors.sort_unstable_by_key(|x| x.0);
successors
Expand All @@ -90,7 +93,7 @@ where
N: Clone,
FN: FnMut(&N) -> IN,
IN: IntoIterator<Item = N>,
FC: Fn(&N) -> C,
FC: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
{
BmsReachable {
Expand Down Expand Up @@ -127,8 +130,8 @@ where
N: Clone,
IN: IntoIterator<Item = N>,
FN: FnMut(&N) -> IN,
FC1: Fn(&N) -> C,
FC2: Fn(&N) -> C,
FC1: Fn(&N) -> Option<C>,
FC2: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
FR: Fn(&N) -> bool,
{
Expand All @@ -142,10 +145,11 @@ where
}
let n = op_n.unwrap();
if leaf_check_fn(&n) {
let cost = cost_fn(&n);
if current_best_cost > cost {
current_best_cost = cost;
best_leaf_node = Some(n)
if let Some(cost) = cost_fn(&n) {
if current_best_cost > cost {
current_best_cost = cost;
best_leaf_node = Some(n)
}
}
}
}
Expand Down Expand Up @@ -310,12 +314,12 @@ mod test {
let eval_fn = |n: &Node| {
let (remained_duration, route) =
greedy_tsp_solver(n.city, n.children.clone(), &time_func);
n.t + remained_duration + time_func(*route.last().unwrap(), start)
Some(n.t + remained_duration + time_func(*route.last().unwrap(), start))
};

let branch_factor = 10;
let beam_width = 5;
let cost_fn = |n: &Node| n.t + time_func(n.city, start);
let cost_fn = |n: &Node| Some(n.t + time_func(n.city, start));
let leaf_check_fn = |n: &Node| n.is_leaf();

let (cost, best_node) = bms(
Expand Down
6 changes: 3 additions & 3 deletions src/dfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ where
N: Clone,
IN: IntoIterator<Item = N>,
FN: FnMut(&N) -> IN,
FC: Fn(&N) -> C,
FC: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
FR: Fn(&N) -> bool,
{
bbs(
start,
successor_fn,
|_| C::min_value(),
|_| Some(C::min_value()),
cost_fn,
leaf_check_fn,
)
Expand Down Expand Up @@ -93,7 +93,7 @@ mod test {
}
})
.sum();
u32::MAX - cost
Some(u32::MAX - cost)
};

let leaf_check_fn = |n: &Node| n.len() == total_items;
Expand Down
8 changes: 4 additions & 4 deletions src/gds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ where
N: Clone,
IN: IntoIterator<Item = N>,
FN: FnMut(&N) -> IN,
FC1: Fn(&N) -> C,
FC2: Fn(&N) -> C,
FC1: Fn(&N) -> Option<C>,
FC2: Fn(&N) -> Option<C>,
C: Ord + Copy + Bounded,
FR: Fn(&N) -> bool,
{
Expand Down Expand Up @@ -168,9 +168,9 @@ mod test {
let time_func = |p: CityId, c: CityId| distance_matrix[p][c];

let successor_fn = |n: &Node| n.generate_child_nodes(&time_func);
let eval_fn = |n: &Node| n.t;
let eval_fn = |n: &Node| Some(n.t);

let cost_fn = |n: &Node| n.t + time_func(n.city, start);
let cost_fn = |n: &Node| Some(n.t + time_func(n.city, start));
let leaf_check_fn = |n: &Node| n.is_leaf();

let (cost, best_node) =
Expand Down