use core::hash::Hash;
use alloc::vec::Vec;
use hashbrown::HashMap;
use crate::config::Config;
use crate::mo::movi::mo_value_iteration;
use crate::mo::{MoMdp, Objectives};
use crate::model::{Goal, Heuristic};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct ZeroVector;
impl<S, const N: usize> Heuristic<S, Vec<[f64; N]>> for ZeroVector {
fn estimate(&mut self, _state: &S) -> Option<Vec<[f64; N]>> {
Some(alloc::vec![[0.0; N]])
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IdealPoint<H, const N: usize>(pub [H; N]);
impl<S, H, const N: usize> Heuristic<S, Vec<[f64; N]>> for IdealPoint<H, N>
where
H: Heuristic<S, f64>,
{
fn estimate(&mut self, state: &S) -> Option<Vec<[f64; N]>> {
let mut vector = [0.0; N];
for (component, heuristic) in vector.iter_mut().zip(&mut self.0) {
let estimate = heuristic.estimate(state)?;
if estimate == f64::INFINITY {
return None;
}
*component = estimate;
}
Some(alloc::vec![vector])
}
}
#[derive(Debug, Clone, Copy)]
pub struct Determinized<'a, M>(pub &'a M);
impl<M, const N: usize> MoMdp<N> for Determinized<'_, M>
where
M: MoMdp<N>,
M::State: Clone,
{
type State = M::State;
type Action = M::State;
fn actions(&self, state: &M::State, mut emit: impl FnMut(M::State, [f64; N])) {
self.0.actions(state, |action, cost| {
self.0
.outcomes(state, &action, |target, _| emit(target, cost));
});
}
fn outcomes(&self, _state: &M::State, action: &M::State, mut emit: impl FnMut(M::State, f64)) {
emit(action.clone(), 1.0);
}
}
pub struct Abstraction<'a, M: MoMdp<N>, P, G, const N: usize> {
mdp: &'a M,
project: P,
goal: &'a G,
objectives: Objectives<N>,
config: Config,
table: HashMap<M::State, Vec<[f64; N]>>,
}
impl<'a, M: MoMdp<N>, P, G, const N: usize> Abstraction<'a, M, P, G, N> {
pub fn new(mdp: &'a M, project: P, goal: &'a G, objectives: Objectives<N>) -> Self {
Self {
mdp,
project,
goal,
objectives,
config: Config::default(),
table: HashMap::new(),
}
}
#[must_use]
pub fn with_config(self, config: Config) -> Self {
Self { config, ..self }
}
}
impl<S, M, P, G, const N: usize> Heuristic<S, Vec<[f64; N]>> for Abstraction<'_, M, P, G, N>
where
M: MoMdp<N>,
M::State: Clone + Eq + Hash,
P: FnMut(&S) -> M::State,
G: Goal<M::State>,
{
fn estimate(&mut self, state: &S) -> Option<Vec<[f64; N]>> {
let abstract_state = (self.project)(state);
if !self.table.contains_key(&abstract_state) {
self.solve(abstract_state.clone());
}
match self.table.get(&abstract_state) {
Some(value) if !value.is_empty() => Some(value.clone()),
_ => Some(alloc::vec![[0.0; N]]),
}
}
}
impl<M, P, G, const N: usize> Abstraction<'_, M, P, G, N>
where
M: MoMdp<N>,
M::State: Clone + Eq + Hash,
G: Goal<M::State>,
{
fn solve(&mut self, state: M::State) {
let Ok(solution) =
mo_value_iteration(self.mdp, state, self.goal, self.objectives, self.config)
else {
return;
};
if solution.status() == crate::Status::BudgetExhausted {
return;
}
for index in 0..solution.state_count() {
let id = crate::space::Id::try_from(index).expect("ids fit u32");
self.table
.entry(solution.state_of(id).clone())
.or_insert_with(|| solution.vectors_of(id).copied().collect());
}
}
}