use multiobjective_search::{
AnytimeFront, ApproxFactor, Epsilon, Heuristic, Solution, Stats, WeightedSum, ZeroHeuristic,
boa_star, boa_star_anytime, boa_star_anytime_basic, boa_star_anytime_ws, heuristic_fn,
pp_a_star_iterative,
};
use proptest::prelude::*;
use crate::brute_force::pareto_costs;
use crate::check::check_paths;
use crate::fixtures::{Problem, exact_heuristic, problems};
type Front = [Solution<usize, [u32; 2]>];
type Report = (Vec<[u32; 2]>, ApproxFactor<u32>);
fn within(factor: ApproxFactor<u32>, x: u32, y: u32) -> bool {
match factor {
ApproxFactor::Zero => x <= y,
ApproxFactor::Infinite => x <= y || y > 0,
ApproxFactor::Ratio { num, den } => {
u64::from(den) * u64::from(x) <= (u64::from(den) + u64::from(num)) * u64::from(y)
}
}
}
fn check_report(
front: &[[u32; 2]],
costs: &[[u32; 2]],
factor: ApproxFactor<u32>,
) -> Result<(), TestCaseError> {
for c in costs {
prop_assert!(front.contains(c), "{:?} is not Pareto-optimal", c);
}
prop_assert!(costs.windows(2).all(|w| w[0] < w[1]));
for s in front {
let covered = costs
.iter()
.any(|r| within(factor, r[0], s[0]) && within(factor, r[1], s[1]));
prop_assert!(
covered,
"{:?} not covered by {:?} within {:?}",
s,
costs,
factor
);
}
Ok(())
}
fn check_run(
problem: &Problem<2>,
reports: &[Report],
result: &AnytimeFront<usize, u32>,
) -> Result<(), TestCaseError> {
let front = pareto_costs(problem);
for (costs, factor) in reports {
check_report(&front, costs, *factor)?;
}
for pair in reports.windows(2) {
prop_assert!(pair[1].1 < pair[0].1, "factors not falling: {:?}", pair);
}
let costs: Vec<_> = result.solutions.iter().map(|s| s.cost).collect();
check_report(&front, &costs, result.factor)?;
if result.complete {
let exact = boa_star(&problem.graph, ZeroHeuristic, problem.start, |v: &usize| {
problem.goals[*v]
});
let boa: Vec<_> = exact.solutions.iter().map(|s| s.cost).collect();
prop_assert!(result.factor.is_zero());
prop_assert_eq!(&costs, &boa);
prop_assert_eq!(&costs, &front);
if !front.is_empty() {
prop_assert!(
reports
.last()
.is_some_and(|(c, f)| f.is_zero() && *c == front)
);
}
}
let output = multiobjective_search::Output {
solutions: result.solutions.clone(),
stats: result.stats,
};
check_paths(problem, &output)
}
fn recorder(reports: &mut Vec<Report>) -> impl FnMut(&Front, ApproxFactor<u32>) + '_ {
|front, factor| reports.push((front.iter().map(|s| s.cost).collect(), factor))
}
fn budget(limit: Option<usize>) -> impl FnMut(&Stats) -> bool {
move |stats: &Stats| limit.is_some_and(|l| stats.expanded >= l)
}
fn weighted_sum(problem: &Problem<2>, w: u32) -> Vec<Option<u32>> {
let mut dist: Vec<Option<u32>> = problem.goals.iter().map(|&g| g.then_some(0)).collect();
for _ in 0..problem.graph.nodes {
for &(from, to, c) in &problem.graph.edges {
if let Some(d) = dist[to] {
let candidate = d + c[0] + w * c[1];
if dist[from].is_none_or(|cur| candidate < cur) {
dist[from] = Some(candidate);
}
}
}
}
dist
}
fn check_variants<H: Heuristic<usize, [u32; 2]> + Copy>(
problem: &Problem<2>,
heuristic: H,
d: u32,
w: u32,
limit: Option<usize>,
) -> Result<(), TestCaseError> {
let (graph, start) = (&problem.graph, problem.start);
let goal = |v: &usize| problem.goals[*v];
let mut reports = Vec::new();
let result = boa_star_anytime(
graph,
heuristic,
start,
goal,
d,
recorder(&mut reports),
budget(limit),
);
check_run(problem, &reports, &result)?;
let mut reports = Vec::new();
let result = boa_star_anytime_basic(
graph,
heuristic,
start,
goal,
d,
recorder(&mut reports),
budget(limit),
);
check_run(problem, &reports, &result)?;
let sums = weighted_sum(problem, w);
let weighted = WeightedSum {
weight: w,
heuristic: heuristic_fn(|v: &usize| sums[*v]),
};
let mut reports = Vec::new();
let result = boa_star_anytime_ws(
graph,
heuristic,
start,
goal,
d,
weighted,
recorder(&mut reports),
budget(limit),
);
check_run(problem, &reports, &result)?;
let zero = WeightedSum {
weight: w,
heuristic: heuristic_fn(|_: &usize| Some(0_u32)),
};
let mut reports = Vec::new();
let result = boa_star_anytime_ws(
graph,
heuristic,
start,
goal,
d,
zero,
recorder(&mut reports),
budget(limit),
);
check_run(problem, &reports, &result)
}
fn scaled(mut problem: Problem<2>) -> Problem<2> {
for edge in &mut problem.graph.edges {
edge.2 = edge.2.map(|c| c * 25 + 1);
}
problem
}
#[test]
fn four_routes() {
let problem = Problem {
graph: crate::fixtures::EdgeList {
nodes: 2,
edges: vec![
(0, 1, [10, 100]),
(0, 1, [40, 45]),
(0, 1, [42, 41]),
(0, 1, [100, 10]),
],
},
start: 0,
goals: vec![false, true],
};
let mut reports = Vec::new();
let result = boa_star_anytime(
&problem.graph,
ZeroHeuristic,
0,
|v: &usize| *v == 1,
4,
recorder(&mut reports),
|_: &Stats| false,
);
assert!(result.complete);
assert_eq!(reports[0].0, [[10, 100], [100, 10]]);
assert_eq!(
reports.last().unwrap().0,
[[10, 100], [40, 45], [42, 41], [100, 10]]
);
let exact = boa_star(&problem.graph, ZeroHeuristic, 0, |v: &usize| *v == 1);
assert_eq!(result.solutions, exact.solutions);
}
fn check_cover(
front: &[[u32; 2]],
costs: &[[u32; 2]],
factor: ApproxFactor<u32>,
) -> Result<(), TestCaseError> {
prop_assert!(
costs
.windows(2)
.all(|w| w[0][0] < w[1][0] && w[0][1] > w[1][1])
);
for s in front {
let covered = costs
.iter()
.any(|r| within(factor, r[0], s[0]) && within(factor, r[1], s[1]));
prop_assert!(
covered,
"{:?} not covered by {:?} within {:?}",
s,
costs,
factor
);
}
Ok(())
}
fn check_iterative<H: Heuristic<usize, [u32; 2]>>(
problem: &Problem<2>,
heuristic: H,
schedule: &[Epsilon],
limit: Option<usize>,
) -> Result<(), TestCaseError> {
let goal = |v: &usize| problem.goals[*v];
let mut reports = Vec::new();
let result = pp_a_star_iterative(
&problem.graph,
heuristic,
problem.start,
goal,
schedule.iter().copied(),
recorder(&mut reports),
budget(limit),
);
let front = pareto_costs(problem);
for (costs, factor) in &reports {
check_cover(&front, costs, *factor)?;
}
for pair in reports.windows(2) {
prop_assert!(pair[1].1 < pair[0].1);
}
let costs: Vec<_> = result.solutions.iter().map(|s| s.cost).collect();
if !schedule.is_empty() {
check_cover(&front, &costs, result.factor)?;
}
if result.complete {
prop_assert!(result.factor.is_zero());
prop_assert_eq!(&costs, &front);
}
let output = multiobjective_search::Output {
solutions: result.solutions.clone(),
stats: result.stats,
};
check_paths(problem, &output)
}
proptest! {
#[test]
fn iterative_pp_a(p in problems::<2>(), steps in prop::collection::vec((1..=3_u32, 1..=8_u32), 0..4), limit in prop::option::of(0..40_usize)) {
let p = scaled(p);
let mut schedule: Vec<Epsilon> = steps.iter().map(|&(n, d)| Epsilon::new(n, d)).collect();
schedule.sort_by(|a, b| (u64::from(b.num()) * u64::from(a.den())).cmp(&(u64::from(a.num()) * u64::from(b.den()))));
schedule.push(Epsilon::ZERO);
let h = exact_heuristic(&p);
check_iterative(&p, ZeroHeuristic, &schedule, limit)?;
check_iterative(&p, heuristic_fn(|v: &usize| h[*v]), &schedule, limit)?;
}
}
proptest! {
#[test]
fn complete_runs(p in problems::<2>(), d in 2..=5_u32, w in 1..=3_u32) {
let h = exact_heuristic(&p);
check_variants(&p, ZeroHeuristic, d, w, None)?;
check_variants(&p, heuristic_fn(|v: &usize| h[*v]), d, w, None)?;
let p = scaled(p);
let h = exact_heuristic(&p);
check_variants(&p, ZeroHeuristic, d, w, None)?;
check_variants(&p, heuristic_fn(|v: &usize| h[*v]), d, w, None)?;
}
#[test]
fn budgeted_runs(p in problems::<2>(), d in 2..=5_u32, limit in 0..30_usize) {
let p = scaled(p);
let h = exact_heuristic(&p);
check_variants(&p, ZeroHeuristic, d, 1, Some(limit))?;
check_variants(&p, heuristic_fn(|v: &usize| h[*v]), d, 1, Some(limit))?;
}
}