use multiobjective_search::{
Heuristic, Output, ZeroHeuristic, boa_star, boa_star_approx, heuristic_fn,
};
use proptest::prelude::*;
use crate::brute_force::pareto_costs;
use crate::check::{check, check_paths};
use crate::fixtures::{EdgeList, Problem, exact_heuristic, problems};
fn solve<H: Heuristic<usize, [u32; 2]>>(
problem: &Problem<2>,
heuristic: H,
) -> Output<usize, [u32; 2]> {
boa_star(&problem.graph, heuristic, problem.start, |v| {
problem.goals[*v]
})
}
fn solve_informed(problem: &Problem<2>) -> Output<usize, [u32; 2]> {
let h = exact_heuristic(problem);
solve(problem, heuristic_fn(|v: &usize| h[*v]))
}
fn problem(
nodes: usize,
edges: &[(usize, usize, [u32; 2])],
start: usize,
goal: usize,
) -> Problem<2> {
Problem {
graph: EdgeList {
nodes,
edges: edges.to_vec(),
},
start,
goals: (0..nodes).map(|v| v == goal).collect(),
}
}
#[test]
fn start_is_goal() {
let p = problem(2, &[(0, 1, [1, 1]), (1, 0, [0, 0])], 0, 0);
let out = solve(&p, ZeroHeuristic);
assert_eq!(out.solutions.len(), 1);
assert_eq!(out.solutions[0].cost, [0, 0]);
assert_eq!(out.solutions[0].path, [0]);
}
#[test]
fn heuristic_by_mut_reference_keeps_state() {
struct Counting(usize);
impl Heuristic<usize, [u32; 2]> for Counting {
fn estimate(&mut self, _state: &usize) -> Option<[u32; 2]> {
self.0 += 1;
Some([0, 0])
}
}
let p = problem(2, &[(0, 1, [1, 2])], 0, 1);
let mut h = Counting(0);
let out = solve(&p, &mut h);
assert_eq!(out.solutions.len(), 1);
assert_eq!(h.0, 2);
}
#[test]
fn unreachable_goal() {
let p = problem(3, &[(0, 1, [1, 1]), (2, 0, [1, 1])], 0, 2);
assert_eq!(solve(&p, ZeroHeuristic).solutions.len(), 0);
let informed = solve_informed(&p);
assert_eq!(informed.solutions.len(), 0);
assert_eq!(informed.stats.generated, 0);
}
#[test]
fn hand_computed() {
let edges = [
(0, 1, [1, 4]),
(1, 3, [1, 2]),
(0, 2, [2, 1]),
(2, 3, [3, 1]),
(0, 3, [6, 6]),
(1, 2, [0, 0]),
(2, 1, [0, 0]),
];
let p = problem(4, &edges, 0, 3);
for out in [solve(&p, ZeroHeuristic), solve_informed(&p)] {
let found: Vec<_> = out
.solutions
.iter()
.map(|s| (s.cost, s.path.clone()))
.collect();
assert_eq!(
found,
[
([2, 6], vec![0, 1, 3]),
([3, 3], vec![0, 2, 1, 3]),
([5, 2], vec![0, 2, 3])
]
);
}
}
proptest! {
#[test]
fn zero_heuristic_matches_brute_force(p in problems::<2>()) {
check(&p, &solve(&p, ZeroHeuristic))?;
}
#[test]
fn exact_heuristic_matches_brute_force(p in problems::<2>()) {
check(&p, &solve_informed(&p))?;
}
#[test]
fn output_is_a_staircase(p in problems::<2>()) {
let out = solve(&p, ZeroHeuristic);
for pair in out.solutions.windows(2) {
prop_assert!(pair[0].cost[0] < pair[1].cost[0] && pair[0].cost[1] > pair[1].cost[1]);
}
}
}
fn check_approx<H: Heuristic<usize, [u32; 2]>>(
problem: &Problem<2>,
heuristic: H,
slack: u32,
) -> Result<(), TestCaseError> {
let goal = |v: &usize| problem.goals[*v];
let out = boa_star_approx(&problem.graph, heuristic, problem.start, goal, slack);
let reference = pareto_costs(problem);
check_paths(problem, &out)?;
for pair in out.solutions.windows(2) {
prop_assert!(pair[0].cost[0] < pair[1].cost[0] && pair[0].cost[1] > pair[1].cost[1]);
}
for solution in &out.solutions {
prop_assert!(reference.contains(&solution.cost));
}
for s in &reference {
let covered = out
.solutions
.iter()
.any(|r| r.cost[0] <= s[0] && r.cost[1] <= s[1] + slack);
prop_assert!(covered, "{s:?} not covered within {slack}");
}
Ok(())
}
#[test]
fn approx_drops_close_solution() {
let edges = [(0, 1, [1, 3]), (0, 1, [2, 2]), (0, 1, [3, 1])];
let p = problem(2, &edges, 0, 1);
let costs = |slack| -> Vec<_> {
boa_star_approx(&p.graph, ZeroHeuristic, 0, |v| *v == 1, slack)
.solutions
.into_iter()
.map(|s| s.cost)
.collect()
};
assert_eq!(costs(0), [[1, 3], [2, 2], [3, 1]]);
assert_eq!(costs(1), [[1, 3], [3, 1]]);
assert_eq!(costs(2), [[1, 3]]);
}
proptest! {
#[test]
fn approx_covers_front(p in problems::<2>(), slack in 0..=3_u32) {
let h = exact_heuristic(&p);
check_approx(&p, ZeroHeuristic, slack)?;
check_approx(&p, heuristic_fn(|v: &usize| h[*v]), slack)?;
}
}