use crate::{
domain::{Key, KeyedCloser, Reversible},
graph::{Graph, SharedGraph},
motion::{r2::*, SpeedLimiter, TravelTimeCost},
templates::{GraphMotion, InformedSearch},
};
pub type SearchR2<G> = InformedSearch<
GraphMotion<DiscreteSpaceTimeR2<<G as Graph>::Key>, SharedGraph<G>, LineFollow>,
TravelTimeCost,
DirectTravelHeuristic<SharedGraph<G>, TravelTimeCost>,
KeyedCloser<DiscreteSpaceTimeR2<<G as Graph>::Key>>,
InitializeR2<SharedGraph<G>>,
(),
(),
>;
impl<G> SearchR2<G>
where
G: Graph + Reversible,
G::Key: Key + Clone,
G::Vertex: Positioned,
G::EdgeAttributes: SpeedLimiter,
{
pub fn new_r2(graph: SharedGraph<G>, line_follow: LineFollow) -> Self {
InformedSearch::new(
GraphMotion {
space: DiscreteSpaceTimeR2::<G::Key>::new(),
graph: graph.clone(),
extrapolator: line_follow,
},
TravelTimeCost(1.0),
DirectTravelHeuristic {
space: DiscreteSpaceTimeR2::<G::Key>::new(),
graph: graph.clone(),
weight: TravelTimeCost(1.0),
extrapolator: line_follow,
},
KeyedCloser(DiscreteSpaceTimeR2::<G::Key>::new()),
)
.with_initializer(InitializeR2(graph))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
algorithm::AStar, domain::AsTimeVariant, graph::SimpleGraph, motion::SpeedLimit, Planner,
};
use std::sync::Arc;
fn make_test_graph() -> SimpleGraph<Position, SpeedLimit> {
let s = SpeedLimit(None);
SimpleGraph::from_iters(
[
Position::new(0.0, 0.0), Position::new(1.0, 0.0), Position::new(2.0, 0.0), Position::new(3.0, 0.0), Position::new(1.0, -1.0), Position::new(2.0, -1.0), Position::new(3.0, -1.0), Position::new(2.0, -2.0), Position::new(3.0, -2.0), ],
[
(0, 1, s),
(1, 0, s),
(1, 2, s),
(2, 1, s),
(2, 3, s),
(3, 2, s),
(2, 4, s),
(4, 2, s),
(3, 6, s),
(6, 3, s),
(4, 5, s),
(5, 4, s),
(5, 7, s),
(7, 5, s),
(7, 8, s),
(8, 7, s),
],
)
}
#[test]
fn test_simple_r2() {
let planner = Planner::new(Arc::new(AStar(InformedSearch::new_r2(
SharedGraph::new(make_test_graph()),
LineFollow::new(2.0).unwrap(),
))));
let solution = planner.plan(0usize, 8usize).unwrap().solve().unwrap();
assert!(solution.solved());
}
use crate::graph::occupancy::{Cell, NeighborhoodGraph, SparseGrid, Visibility};
#[test]
fn test_occupancy_r2() {
let mut visibility = Visibility::new(SparseGrid::new(0.5), 1.25);
visibility.change_cells(
&(0..=5)
.into_iter()
.flat_map(|i| (0..=5).into_iter().map(move |j| (Cell::new(i, j), true)))
.collect(),
);
let graph = NeighborhoodGraph::new(Arc::new(visibility), []);
let planner = Planner::new(AStar(InformedSearch::new_r2(
SharedGraph::new(graph),
LineFollow::new(2.0).unwrap(),
)));
let solution = planner
.plan(Cell::new(-3, -3), Cell::new(10, 10))
.unwrap()
.solve()
.unwrap();
assert!(solution.solved());
}
#[test]
fn test_simple_time_variant_r2() {
let planner = Planner::new(AStar(
InformedSearch::new_r2(
SharedGraph::new(make_test_graph()),
LineFollow::new(2.0).unwrap(),
)
.as_time_variant(),
));
let solution = planner.plan(0usize, 8usize).unwrap().solve().unwrap();
assert!(solution.solved());
}
}