use alloc::vec::Vec;
use rand_core::Rng;
use crate::operators::selection::{Selection, Tournament};
use crate::operators::sequence::UniformCrossover;
use crate::operators::{Crossover, Initializer, Mutation};
use crate::rng::{below, chance};
use crate::{ranking, sanitize};
#[derive(Debug, Clone, PartialEq)]
pub struct Step<S> {
pub state: S,
pub cost: f64,
pub done: bool,
}
pub trait ForwardModel {
type State: Clone;
type Action: Clone;
fn step<R: Rng + ?Sized>(
&mut self,
state: &Self::State,
action: &Self::Action,
rng: &mut R,
) -> Step<Self::State>;
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Config {
pub horizon: usize,
pub population_size: usize,
pub evaluations: u64,
pub discount: f64,
pub rollouts: usize,
pub shift_buffer: bool,
pub reevaluate: bool,
pub mutation_rate: Option<f64>,
}
impl Default for Config {
fn default() -> Self {
Self {
horizon: 10,
population_size: 1,
evaluations: 200,
discount: 1.0,
rollouts: 1,
shift_buffer: true,
reevaluate: false,
mutation_rate: None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Decision<A> {
pub action: A,
pub plan: Vec<A>,
pub cost: f64,
pub evaluations: u64,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PointMutation<S> {
pub actions: S,
pub rate: Option<f64>,
}
impl<A, S: Initializer<A>> Mutation<Vec<A>> for PointMutation<S> {
fn mutate<R: Rng + ?Sized>(&mut self, genome: &mut Vec<A>, rng: &mut R) {
if genome.is_empty() {
return;
}
#[expect(clippy::cast_precision_loss, reason = "horizons are far below 2^52")]
let rate = self.rate.unwrap_or(1.0 / genome.len() as f64);
let mut changed = false;
for action in genome.iter_mut() {
if chance(rng, rate) {
*action = self.actions.initialize(rng);
changed = true;
}
}
if !changed {
let at = below(rng, genome.len());
genome[at] = self.actions.initialize(rng);
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RollingHorizon<A, S, M> {
pub config: Config,
pub actions: S,
pub mutation: M,
buffer: Vec<Vec<A>>,
}
impl<A: Clone, S: Initializer<A> + Clone> RollingHorizon<A, S, PointMutation<S>> {
#[must_use]
pub fn new(config: Config, actions: S) -> Self {
let mutation = PointMutation {
actions: actions.clone(),
rate: config.mutation_rate,
};
Self {
config,
actions,
mutation,
buffer: Vec::new(),
}
}
#[must_use]
pub fn rmhc(config: Config, actions: S) -> Self {
Self::new(
Config {
population_size: 1,
..config
},
actions,
)
}
}
impl<A: Clone, S: Initializer<A>, M: Mutation<Vec<A>>> RollingHorizon<A, S, M> {
#[must_use]
pub fn with_mutation(config: Config, actions: S, mutation: M) -> Self {
Self {
config,
actions,
mutation,
buffer: Vec::new(),
}
}
pub fn reset(&mut self) {
self.buffer.clear();
}
pub fn plan<F, R>(&mut self, model: &mut F, state: &F::State, rng: &mut R) -> Decision<A>
where
F: ForwardModel<Action = A>,
R: Rng + ?Sized,
{
let c = self.config;
assert!(
c.horizon > 0 && c.population_size > 0 && c.rollouts > 0,
"horizon, population and rollouts must be positive"
);
let mut population = self.seed_population(rng);
let mut evaluations = 0;
let mut costs: Vec<f64> = Vec::with_capacity(population.len());
for sequence in &population {
costs.push(self.evaluate(model, state, sequence, rng));
evaluations += 1;
}
while evaluations < c.evaluations.max(1) {
let remaining = c.evaluations.max(1) - evaluations;
if c.population_size == 1 {
evaluations += self.hill_climb_step(
model,
state,
&mut population[0],
&mut costs[0],
remaining,
rng,
);
} else {
evaluations +=
self.generation(model, state, &mut population, &mut costs, remaining, rng);
}
}
self.decide(&population, &costs, evaluations, rng)
}
fn seed_population<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Vec<Vec<A>> {
let c = self.config;
let mut population: Vec<Vec<A>> = core::mem::take(&mut self.buffer);
population.truncate(c.population_size);
for sequence in &mut population {
sequence.resize_with(c.horizon, || self.actions.initialize(rng));
}
while population.len() < c.population_size {
population.push(
(0..c.horizon)
.map(|_| self.actions.initialize(rng))
.collect(),
);
}
population
}
fn hill_climb_step<F, R>(
&mut self,
model: &mut F,
state: &F::State,
current: &mut Vec<A>,
cost: &mut f64,
remaining: u64,
rng: &mut R,
) -> u64
where
F: ForwardModel<Action = A>,
R: Rng + ?Sized,
{
let mut spent = 0;
if self.config.reevaluate && remaining >= 2 {
*cost = self.evaluate(model, state, current, rng);
spent += 1;
}
let mut candidate = current.clone();
self.mutation.mutate(&mut candidate, rng);
let candidate_cost = self.evaluate(model, state, &candidate, rng);
if candidate_cost <= *cost {
*current = candidate;
*cost = candidate_cost;
}
spent + 1
}
fn generation<F, R>(
&mut self,
model: &mut F,
state: &F::State,
population: &mut Vec<Vec<A>>,
costs: &mut Vec<f64>,
remaining: u64,
rng: &mut R,
) -> u64
where
F: ForwardModel<Action = A>,
R: Rng + ?Sized,
{
let order = ranking(costs);
let mut next = alloc::vec![population[order[0]].clone()];
let mut next_costs = alloc::vec![costs[order[0]]];
let mut parents = Vec::with_capacity(2);
let mut spent = 0;
while next.len() < population.len() && spent < remaining {
parents.clear();
Tournament { size: 2 }.select(costs, 2, rng, &mut parents);
let [mut child, _] = UniformCrossover::default().crossover(
&population[parents[0]],
&population[parents[1]],
rng,
);
self.mutation.mutate(&mut child, rng);
next_costs.push(self.evaluate(model, state, &child, rng));
next.push(child);
spent += 1;
}
for &k in order.iter().skip(1) {
if next.len() == population.len() {
break;
}
next.push(population[k].clone());
next_costs.push(costs[k]);
}
*population = next;
*costs = next_costs;
spent
}
fn decide<R: Rng + ?Sized>(
&mut self,
population: &[Vec<A>],
costs: &[f64],
evaluations: u64,
rng: &mut R,
) -> Decision<A> {
let order = ranking(costs);
let best = order[0];
let plan = population[best].clone();
if self.config.shift_buffer {
self.buffer = order
.iter()
.map(|&k| {
let mut shifted: Vec<A> = population[k][1..].to_vec();
shifted.push(self.actions.initialize(rng));
shifted
})
.collect();
}
Decision {
action: plan[0].clone(),
cost: costs[best],
plan,
evaluations,
}
}
fn evaluate<F, R>(&self, model: &mut F, state: &F::State, sequence: &[A], rng: &mut R) -> f64
where
F: ForwardModel<Action = A>,
R: Rng + ?Sized,
{
let mut total = 0.0;
for _ in 0..self.config.rollouts {
total += rollout(model, state, sequence, self.config.discount, rng);
}
#[expect(clippy::cast_precision_loss, reason = "rollout counts are tiny")]
let mean = total / self.config.rollouts as f64;
sanitize(mean)
}
}
pub fn rollout<F, R>(
model: &mut F,
state: &F::State,
sequence: &[F::Action],
discount: f64,
rng: &mut R,
) -> f64
where
F: ForwardModel,
R: Rng + ?Sized,
{
let mut current = state.clone();
let mut total = 0.0;
let mut weight = 1.0;
for action in sequence {
let step = model.step(¤t, action, rng);
total += weight * step.cost;
if step.done {
break;
}
weight *= discount;
current = step.state;
}
total
}
#[cfg(test)]
mod tests {
use super::*;
use crate::operators::{initializer_fn, mutation_fn};
use crate::{SplitMix64, standard_normal, unit_f64};
struct Corridor {
goal: i32,
slip: f64,
}
impl ForwardModel for Corridor {
type State = i32;
type Action = i32;
fn step<R: Rng + ?Sized>(&mut self, state: &i32, action: &i32, rng: &mut R) -> Step<i32> {
let next = if unit_f64(rng) < self.slip {
*state
} else {
state + action
};
Step {
state: next,
cost: f64::from((self.goal - next).abs()),
done: next == self.goal,
}
}
}
type CorridorPlanner<M> =
RollingHorizon<i32, crate::operators::InitializerFn<fn(&mut dyn Rng) -> i32>, M>;
fn episode<M: Mutation<Vec<i32>>>(
planner: &mut CorridorPlanner<M>,
slip: f64,
seed: u64,
) -> usize {
let mut model = Corridor { goal: 6, slip };
let mut rng = SplitMix64::new(seed);
let mut state = 0;
for t in 0..100 {
if state == 6 {
return t;
}
let decision = planner.plan(&mut model, &state, &mut rng);
state = model.step(&state, &decision.action, &mut rng).state;
}
100
}
fn moves(rng: &mut dyn Rng) -> i32 {
i32::try_from(below(rng, 3)).expect("small") - 1
}
#[test]
fn deterministic_corridor() {
for population_size in [1, 8] {
let config = Config {
horizon: 10,
population_size,
evaluations: 300,
..Config::default()
};
let mut planner =
RollingHorizon::new(config, initializer_fn(moves as fn(&mut dyn Rng) -> i32));
assert_eq!(
episode(&mut planner, 0.0, 3),
6,
"population {population_size}"
);
}
}
#[test]
fn exact_evaluations_per_decision() {
for (population_size, reevaluate) in [(1, false), (1, true), (7, false)] {
let config = Config {
horizon: 6,
population_size,
reevaluate,
evaluations: 101,
..Config::default()
};
let mut planner =
RollingHorizon::new(config, initializer_fn(moves as fn(&mut dyn Rng) -> i32));
let mut model = Corridor { goal: 6, slip: 0.0 };
let decision = planner.plan(&mut model, &0, &mut SplitMix64::new(1));
assert_eq!(decision.evaluations, 101, "{population_size} {reevaluate}");
}
}
#[test]
fn stochastic_corridor() {
let config = Config {
horizon: 10,
rollouts: 4,
reevaluate: true,
..Config::default()
};
let mut planner =
RollingHorizon::new(config, initializer_fn(moves as fn(&mut dyn Rng) -> i32));
let steps = episode(&mut planner, 0.2, 5);
assert!(steps <= 14, "{steps} steps");
}
#[test]
fn shift_buffer_keeps_the_plan() {
let config = Config {
horizon: 8,
evaluations: 400,
..Config::default()
};
let mut planner =
RollingHorizon::new(config, initializer_fn(moves as fn(&mut dyn Rng) -> i32));
let mut model = Corridor { goal: 6, slip: 0.0 };
let mut rng = SplitMix64::new(9);
let first = planner.plan(&mut model, &0, &mut rng);
planner.config.evaluations = 1;
let second = planner.plan(&mut model, &first.action, &mut rng);
assert_eq!(second.plan[..first.plan.len() - 1], first.plan[1..]);
}
#[test]
fn continuous_point_mass() {
struct PointMass;
impl ForwardModel for PointMass {
type State = (f64, f64);
type Action = f64;
fn step<R: Rng + ?Sized>(
&mut self,
s: &(f64, f64),
a: &f64,
_rng: &mut R,
) -> Step<(f64, f64)> {
let a = a.clamp(-1.0, 1.0);
let v = s.1 + 0.1 * a;
let x = s.0 + 0.1 * v;
Step {
state: (x, v),
cost: x * x + 0.1 * v * v + 0.01 * a * a,
done: false,
}
}
}
let mutation = mutation_fn(|plan: &mut Vec<f64>, rng: &mut dyn Rng| {
for a in plan.iter_mut() {
*a = (*a + 0.3 * standard_normal(rng)).clamp(-1.0, 1.0);
}
});
let config = Config {
horizon: 20,
evaluations: 300,
..Config::default()
};
let mut planner = RollingHorizon::with_mutation(
config,
initializer_fn(|rng| 2.0 * unit_f64(rng) - 1.0),
mutation,
);
let mut rng = SplitMix64::new(2);
let mut state = (1.0, 0.0);
for _ in 0..150 {
let decision = planner.plan(&mut PointMass, &state, &mut rng);
state = PointMass.step(&state, &decision.action, &mut rng).state;
}
assert!(state.0.abs() < 0.1 && state.1.abs() < 0.2, "{state:?}");
}
}