use crate::{
domain::{Cost, Reversible, Weighted},
error::NoError,
motion::Timed,
};
#[derive(Debug, Clone, Copy)]
pub struct TravelTimeCost(pub f64);
impl Default for TravelTimeCost {
fn default() -> Self {
TravelTimeCost(1.0)
}
}
impl<State: Timed, Action> Weighted<State, Action> for TravelTimeCost {
type Cost = Cost<f64>;
type WeightedError = NoError;
fn cost(
&self,
from_state: &State,
_: &Action,
to_state: &State,
) -> Result<Option<Self::Cost>, Self::WeightedError> {
let duration = (to_state.time() - from_state.time()).as_secs_f64().abs();
Ok(Some(Cost(duration * self.0)))
}
fn initial_cost(&self, _: &State) -> Result<Option<Self::Cost>, Self::WeightedError> {
Ok(Some(Cost(0.0)))
}
}
impl Reversible for TravelTimeCost {
type ReversalError = NoError;
fn reversed(&self) -> Result<Self, Self::ReversalError>
where
Self: Sized,
{
Ok(*self)
}
}