use core::hash::Hash;
use core::mem;
use alloc::vec::Vec;
use crate::error::{Error, PROBABILITY_TOLERANCE};
use crate::model::{Goal, Heuristic, Mdp};
use crate::space::{ActionEntry, Id, Kind, Outcome, Problem, Space};
impl<S: Hash + Eq + Clone, A> Space<S, A> {
pub(crate) fn expand<M, G, H>(
&mut self,
problem: &mut Problem<'_, M, G, H>,
id: Id,
) -> Result<(), Error<S>>
where
M: Mdp<State = S, Action = A>,
G: Goal<S>,
H: Heuristic<S, f64>,
{
let node = self.node(id);
if node.expanded || node.kind != Kind::Open {
return Ok(());
}
let mut raw_actions = mem::take(&mut self.scratch_actions);
raw_actions.clear();
problem.mdp.actions(self.state(id), |action, cost| {
raw_actions.push((action, cost));
});
let start = arena_index(self.actions.len());
let outcomes_start = self.outcomes.len();
let result = self.cache_actions(problem, id, &mut raw_actions);
self.scratch_actions = raw_actions;
if let Err(error) = result {
self.actions.truncate(start as usize);
self.outcomes.truncate(outcomes_start);
return Err(error);
}
let end = arena_index(self.actions.len());
let node = self.node_mut(id);
node.actions = (start, end);
node.expanded = true;
self.expanded += 1;
Ok(())
}
fn cache_actions<M, G, H>(
&mut self,
problem: &mut Problem<'_, M, G, H>,
id: Id,
raw_actions: &mut Vec<(A, f64)>,
) -> Result<(), Error<S>>
where
M: Mdp<State = S, Action = A>,
G: Goal<S>,
H: Heuristic<S, f64>,
{
for (action, cost) in raw_actions.drain(..) {
if !(cost.is_finite() && cost >= 0.0) {
return Err(Error::InvalidCost {
state: self.state(id).clone(),
cost,
});
}
let outcomes = self.cache_outcomes(problem, id, &action)?;
self.actions.push(ActionEntry {
action,
cost,
outcomes,
});
}
Ok(())
}
fn cache_outcomes<M, G, H>(
&mut self,
problem: &mut Problem<'_, M, G, H>,
id: Id,
action: &A,
) -> Result<(u32, u32), Error<S>>
where
M: Mdp<State = S, Action = A>,
G: Goal<S>,
H: Heuristic<S, f64>,
{
let mut raw = mem::take(&mut self.scratch_outcomes);
raw.clear();
problem
.mdp
.outcomes(self.state(id), action, |state, p| raw.push((state, p)));
if let Err(error) = validate_distribution(&raw) {
self.scratch_outcomes = raw;
return Err(error.at(self.state(id).clone()));
}
let start = arena_index(self.outcomes.len());
let result = self.intern_outcomes(problem, &mut raw);
raw.clear();
self.scratch_outcomes = raw;
if let Err(error) = result {
self.outcomes.truncate(start as usize);
return Err(error);
}
Ok((start, arena_index(self.outcomes.len())))
}
}
impl<S: Hash + Eq + Clone, A> Space<S, A> {
fn intern_outcomes<M, G, H>(
&mut self,
problem: &mut Problem<'_, M, G, H>,
raw: &mut Vec<(S, f64)>,
) -> Result<(), Error<S>>
where
G: Goal<S>,
H: Heuristic<S, f64>,
{
for (state, probability) in raw.drain(..) {
let target = self.intern(problem, state)?;
self.outcomes.push(Outcome {
target,
probability,
});
}
Ok(())
}
}
pub(crate) enum Defect {
Probability(f64),
Sum(f64),
}
impl Defect {
pub(crate) fn at<S>(self, state: S) -> Error<S> {
match self {
Self::Probability(probability) => Error::InvalidProbability { state, probability },
Self::Sum(sum) => Error::ProbabilitySum { state, sum },
}
}
}
pub(crate) fn validate_distribution<S>(outcomes: &[(S, f64)]) -> Result<(), Defect> {
let mut sum = 0.0;
for &(_, p) in outcomes {
if !(p > 0.0 && p <= 1.0) {
return Err(Defect::Probability(p));
}
sum += p;
}
if abs(sum - 1.0) > PROBABILITY_TOLERANCE {
return Err(Defect::Sum(sum));
}
Ok(())
}
pub(crate) fn abs(x: f64) -> f64 {
if x < 0.0 { -x } else { x }
}
fn arena_index(len: usize) -> u32 {
u32::try_from(len).expect("at most u32::MAX actions and outcomes fit the arenas")
}