use core::hash::{BuildHasher, Hash};
use core::mem;
use alloc::vec::Vec;
use hashbrown::{DefaultHashBuilder, HashTable};
use crate::error::Error;
use crate::mo::MoMdp;
use crate::mo::coverage::{NO_ACTION, Point, Scratch, ValueSet, prune};
use crate::model::{Goal, Heuristic};
use crate::space::expand::validate_distribution;
use crate::space::{Id, Kind, Outcome};
pub(crate) struct Problem<'a, M, G, H, const N: usize> {
pub(crate) mdp: &'a M,
pub(crate) goal: &'a G,
pub(crate) heuristic: H,
pub(crate) bound: [f64; N],
pub(crate) margin: f64,
}
#[derive(Debug, Clone)]
pub(crate) struct Node<const N: usize> {
pub(crate) kind: Kind,
pub(crate) value: ValueSet<N>,
pub(crate) solved: bool,
pub(crate) expanded: bool,
pub(crate) actions: (u32, u32),
pub(crate) mark: u32,
}
#[derive(Debug, Clone)]
pub(crate) struct ActionEntry<A, const N: usize> {
pub(crate) action: A,
pub(crate) cost: [f64; N],
pub(crate) outcomes: (u32, u32),
}
pub(crate) struct Space<S, A, const N: usize> {
states: Vec<S>,
nodes: Vec<Node<N>>,
index: HashTable<Id>,
hasher: DefaultHashBuilder,
actions: Vec<ActionEntry<A, N>>,
outcomes: Vec<Outcome>,
expanded: u64,
scratch_actions: Vec<(A, [f64; N])>,
scratch_outcomes: Vec<(S, f64)>,
}
impl<S, A, const N: usize> Space<S, A, N> {
pub(crate) fn new() -> Self {
Self {
states: Vec::new(),
nodes: Vec::new(),
index: HashTable::new(),
hasher: DefaultHashBuilder::default(),
actions: Vec::new(),
outcomes: Vec::new(),
expanded: 0,
scratch_actions: Vec::new(),
scratch_outcomes: Vec::new(),
}
}
pub(crate) fn len(&self) -> usize {
self.nodes.len()
}
pub(crate) fn node(&self, id: Id) -> &Node<N> {
&self.nodes[id as usize]
}
pub(crate) fn node_mut(&mut self, id: Id) -> &mut Node<N> {
&mut self.nodes[id as usize]
}
pub(crate) fn nodes_mut(&mut self) -> &mut [Node<N>] {
&mut self.nodes
}
pub(crate) fn state(&self, id: Id) -> &S {
&self.states[id as usize]
}
pub(crate) fn action(&self, index: u32) -> &ActionEntry<A, N> {
&self.actions[index as usize]
}
pub(crate) fn action_range(&self, id: Id) -> core::ops::Range<u32> {
let (start, end) = self.node(id).actions;
start..end
}
pub(crate) fn outcomes(&self, index: u32) -> &[Outcome] {
let (start, end) = self.action(index).outcomes;
&self.outcomes[start as usize..end as usize]
}
}
impl<S, A, const N: usize> crate::meter::Counts for Space<S, A, N> {
type State = S;
fn states(&self) -> u64 {
self.nodes.len() as u64
}
fn expanded(&self) -> u64 {
self.expanded
}
}
impl<S: Hash + Eq, A, const N: usize> Space<S, A, N> {
pub(crate) fn find(&self, state: &S) -> Option<Id> {
let hash = self.hasher.hash_one(state);
self.index
.find(hash, |&id| self.states[id as usize] == *state)
.copied()
}
}
impl<S: Hash + Eq + Clone, A, const N: usize> Space<S, A, N> {
pub(crate) fn intern<M, G, H>(
&mut self,
problem: &mut Problem<'_, M, G, H, N>,
state: S,
scratch: &mut Scratch<N>,
) -> Result<Id, Error<S>>
where
G: Goal<S>,
H: Heuristic<S, Vec<[f64; N]>>,
{
let hash = self.hasher.hash_one(&state);
if let Some(&id) = self
.index
.find(hash, |&id| self.states[id as usize] == state)
{
return Ok(id);
}
let (kind, value) = initial_value(problem, &state, scratch)?;
let id = Id::try_from(self.nodes.len()).expect("at most u32::MAX states fit the space");
self.nodes.push(Node {
kind,
value,
solved: kind != Kind::Open,
expanded: false,
actions: (0, 0),
mark: 0,
});
self.states.push(state);
let (states, hasher) = (&self.states, &self.hasher);
self.index
.insert_unique(hash, id, |&other| hasher.hash_one(&states[other as usize]));
Ok(id)
}
pub(crate) fn expand<M, G, H>(
&mut self,
problem: &mut Problem<'_, M, G, H, N>,
id: Id,
scratch: &mut Scratch<N>,
) -> Result<(), Error<S>>
where
M: MoMdp<N, State = S, Action = A>,
G: Goal<S>,
H: Heuristic<S, Vec<[f64; N]>>,
{
let node = self.node(id);
if node.expanded || node.kind != Kind::Open {
return Ok(());
}
let mut raw = mem::take(&mut self.scratch_actions);
raw.clear();
problem
.mdp
.actions(self.state(id), |action, cost| raw.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, scratch);
self.scratch_actions = raw;
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;
if start == end {
node.kind = Kind::DeadEnd;
node.value = ValueSet::dead_end();
node.solved = true;
}
self.expanded += 1;
Ok(())
}
fn cache_actions<M, G, H>(
&mut self,
problem: &mut Problem<'_, M, G, H, N>,
id: Id,
raw: &mut Vec<(A, [f64; N])>,
scratch: &mut Scratch<N>,
) -> Result<(), Error<S>>
where
M: MoMdp<N, State = S, Action = A>,
G: Goal<S>,
H: Heuristic<S, Vec<[f64; N]>>,
{
for (action, cost) in raw.drain(..) {
if let Some(&bad) = cost.iter().find(|c| !(c.is_finite() && **c >= 0.0)) {
return Err(Error::InvalidCost {
state: self.state(id).clone(),
cost: bad,
});
}
let outcomes = self.cache_outcomes(problem, id, &action, scratch)?;
self.actions.push(ActionEntry {
action,
cost,
outcomes,
});
}
Ok(())
}
fn cache_outcomes<M, G, H>(
&mut self,
problem: &mut Problem<'_, M, G, H, N>,
id: Id,
action: &A,
scratch: &mut Scratch<N>,
) -> Result<(u32, u32), Error<S>>
where
M: MoMdp<N, State = S, Action = A>,
G: Goal<S>,
H: Heuristic<S, Vec<[f64; N]>>,
{
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(defect) = validate_distribution(&raw) {
self.scratch_outcomes = raw;
return Err(defect.at(self.state(id).clone()));
}
let start = arena_index(self.outcomes.len());
let mut result = Ok(());
for (state, probability) in raw.drain(..) {
match self.intern(problem, state, scratch) {
Ok(target) => self.outcomes.push(Outcome {
target,
probability,
}),
Err(error) => {
result = Err(error);
break;
}
}
}
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())))
}
}
fn initial_value<S: Clone, M, G, H, const N: usize>(
problem: &mut Problem<'_, M, G, H, N>,
state: &S,
scratch: &mut Scratch<N>,
) -> Result<(Kind, ValueSet<N>), Error<S>>
where
G: Goal<S>,
H: Heuristic<S, Vec<[f64; N]>>,
{
if problem.goal.is_goal(state) {
return Ok((Kind::Goal, ValueSet::goal()));
}
let estimate = problem.heuristic.estimate(state).unwrap_or_default();
if estimate.iter().flatten().any(|c| !c.is_finite()) {
return Err(Error::InvalidHeuristic {
state: state.clone(),
});
}
if estimate.is_empty() {
return Ok((Kind::DeadEnd, ValueSet::dead_end()));
}
let mut points: Vec<Point<N>> = estimate
.into_iter()
.map(|vector| Point {
vector,
action: NO_ACTION,
})
.collect();
prune(&mut points, problem.margin, scratch);
Ok((
Kind::Open,
ValueSet {
points,
bound: Vec::new(),
},
))
}
fn arena_index(len: usize) -> u32 {
u32::try_from(len).expect("at most u32::MAX actions and outcomes fit the arenas")
}