evolutionary-planning 0.1.1

Evolutionary planning for no_std: genetic algorithms, evolution strategies, CMA-ES and multi-objective evolutionary search over plans, paths and trajectories
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! Rolling-horizon evolutionary planning (RHEA; Perez et al. 2013) over a caller-supplied
//! forward model, with random-mutation hill climbing (RMHC) as the population-of-one case
//! used by Olesen et al. (2020) to plan in the latent space of a learned world model.
//!
//! At every decision the planner evolves action sequences of length `horizon`, each scored
//! by rolling it out through the model from the current state; it returns the first action
//! of the best sequence. With *shift buffering* the best sequence is shifted by one step,
//! padded with a random action and becomes the starting point of the next decision, so the
//! search is not repeated from scratch. Each decision spends exactly
//! [`Config::evaluations`] sequence evaluations (at least one per initial sequence).
//!
//! The model is the caller's: a simulator, an analytic model or a learned one. This crate
//! minimises, so [`ForwardModel::step`] reports a per-step **cost** (use `-reward` for a
//! reward-based model).
//!
//! ```
//! use evolutionary_planning::rhea::{Config, ForwardModel, RollingHorizon, Step};
//! use evolutionary_planning::operators::initializer_fn;
//! use evolutionary_planning::{SplitMix64, below};
//! use rand_core::Rng;
//!
//! /// A corridor: move left (-1) or right (+1); every step before reaching cell 5 costs 1.
//! struct Corridor;
//!
//! 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 = state + action;
//!         Step { state: next, cost: 1.0, done: next == 5 }
//!     }
//! }
//!
//! let actions = initializer_fn(|rng| if below(rng, 2) == 0 { -1 } else { 1 });
//! let mut planner = RollingHorizon::rmhc(Config { horizon: 8, ..Config::default() }, actions);
//! let mut rng = SplitMix64::new(1);
//! let mut state = 0;
//! for _ in 0..5 {
//!     let decision = planner.plan(&mut Corridor, &state, &mut rng);
//!     state += decision.action;
//! }
//! assert_eq!(state, 5);
//! ```

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};

/// The result of one model step.
#[derive(Debug, Clone, PartialEq)]
pub struct Step<S> {
    /// The successor state.
    pub state: S,
    /// The cost of the step (minimised; `-reward` for reward models).
    pub cost: f64,
    /// Whether the episode ended: the rest of the sequence is not simulated.
    pub done: bool,
}

/// A forward model: what happens when an action is taken in a state.
///
/// Stochastic models draw from `rng`; [`Config::rollouts`] averages several samples.
pub trait ForwardModel {
    /// The world state.
    type State: Clone;
    /// An action.
    type Action: Clone;

    /// Simulates one step from `state` under `action`.
    fn step<R: Rng + ?Sized>(
        &mut self,
        state: &Self::State,
        action: &Self::Action,
        rng: &mut R,
    ) -> Step<Self::State>;
}

/// Parameters of a [`RollingHorizon`] planner.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Config {
    /// The length of every action sequence, at least `1`.
    pub horizon: usize,
    /// The number of sequences evolved; `1` is RMHC.
    pub population_size: usize,
    /// Sequence evaluations (each `rollouts` model rollouts) per decision. Exact, except that
    /// every sequence of the initial population is evaluated, so the effective budget is at
    /// least `population_size`.
    pub evaluations: u64,
    /// The discount `γ ∈ (0, 1]` of step costs; `1` is the undiscounted sum of Olesen et al.
    pub discount: f64,
    /// Rollouts averaged per evaluation, for stochastic models.
    pub rollouts: usize,
    /// Keep the best sequence, shifted by one step, as the seed of the next decision.
    pub shift_buffer: bool,
    /// RMHC on stochastic models: re-evaluate the incumbent each iteration so one lucky
    /// rollout cannot hold the lead forever.
    pub reevaluate: bool,
    /// The per-action mutation probability of the default point mutation; `None` means
    /// `1 / horizon`. At least one action always changes.
    pub mutation_rate: Option<f64>,
}

impl Default for Config {
    /// Horizon 10, RMHC, 200 evaluations per decision, undiscounted, one rollout, shift
    /// buffering on.
    fn default() -> Self {
        Self {
            horizon: 10,
            population_size: 1,
            evaluations: 200,
            discount: 1.0,
            rollouts: 1,
            shift_buffer: true,
            reevaluate: false,
            mutation_rate: None,
        }
    }
}

/// One decision of the planner.
#[derive(Debug, Clone, PartialEq)]
pub struct Decision<A> {
    /// The action to execute now: the first of the best sequence.
    pub action: A,
    /// The whole best sequence.
    pub plan: Vec<A>,
    /// Its estimated (discounted) cost.
    pub cost: f64,
    /// Sequence evaluations spent on this decision.
    pub evaluations: u64,
}

/// Replaces each action with probability `rate` (default `1/n`) by a fresh random action;
/// at least one changes. The default mutation of the planner.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PointMutation<S> {
    /// The action sampler.
    pub actions: S,
    /// The per-action probability; `None` means `1/n`.
    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);
        }
    }
}

/// The rolling-horizon planner.
#[derive(Debug, Clone, PartialEq)]
pub struct RollingHorizon<A, S, M> {
    /// The parameters.
    pub config: Config,
    /// Samples random actions (initial sequences and shift-buffer padding).
    pub actions: S,
    /// Mutates a sequence.
    pub mutation: M,
    /// The sequences carried over from the last decision (shifted), if any.
    buffer: Vec<Vec<A>>,
}

impl<A: Clone, S: Initializer<A> + Clone> RollingHorizon<A, S, PointMutation<S>> {
    /// A planner with the default point mutation.
    #[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(),
        }
    }

    /// RMHC: a planner with a population of one (whatever `config` says).
    #[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> {
    /// A planner with a custom sequence mutation (for example Gaussian perturbation of
    /// continuous actions).
    #[must_use]
    pub fn with_mutation(config: Config, actions: S, mutation: M) -> Self {
        Self {
            config,
            actions,
            mutation,
            buffer: Vec::new(),
        }
    }

    /// Forgets the shift buffer, for example at the start of a new episode.
    pub fn reset(&mut self) {
        self.buffer.clear();
    }

    /// Plans from `state` and returns the first action of the best sequence found.
    ///
    /// # Panics
    ///
    /// Panics if the horizon, population size or rollout count is `0`.
    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)
    }

    /// The shifted buffer, topped up with random sequences.
    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
    }

    /// One RMHC iteration within `remaining` evaluations (the incumbent is re-evaluated only
    /// if two remain); returns the evaluations spent.
    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
    }

    /// One generation of the population variant (elitism one, binary tournament, uniform
    /// crossover, mutation) with at most `remaining` evaluations; slots left when they run
    /// out keep the best old sequences. Returns the evaluations spent.
    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
    }

    /// Picks the best sequence, fills the shift buffer and builds the decision.
    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,
        }
    }

    /// The mean discounted cost of `rollouts` rollouts of `sequence` from `state`.
    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)
    }
}

/// The discounted cost of executing `sequence` from `state` until it ends or the episode does.
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(&current, 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};

    /// A corridor of cells; each step costs the remaining distance to the goal (an auxiliary
    /// criterion in Jakob's sense: it rewards progress, not only arrival); with `slip` the
    /// move fails.
    struct Corridor {
        /// The goal cell.
        goal: i32,
        /// The probability that a move does nothing.
        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,
            }
        }
    }

    /// A corridor planner with function-pointer action sampling.
    type CorridorPlanner<M> =
        RollingHorizon<i32, crate::operators::InitializerFn<fn(&mut dyn Rng) -> i32>, M>;

    /// Runs a closed-loop episode; returns the steps to the goal.
    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
    }

    /// The action sampler of the corridor: -1, 0 or +1.
    fn moves(rng: &mut dyn Rng) -> i32 {
        i32::try_from(below(rng, 3)).expect("small") - 1
    }

    /// RMHC and the population variant walk straight to the goal on the deterministic
    /// corridor.
    #[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}"
            );
        }
    }

    /// Each decision spends exactly the configured evaluations, with re-evaluation and with a
    /// population.
    #[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}");
        }
    }

    /// With slipping moves and averaged, re-evaluated rollouts the goal is still reached
    /// quickly.
    #[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");
    }

    /// The shift buffer carries the plan: a deterministic plan found once is kept, so later
    /// decisions follow it even with a single evaluation each.
    #[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..]);
    }

    /// Continuous actions with a Gaussian sequence mutation steer a point mass to the origin.
    #[test]
    fn continuous_point_mass() {
        /// Position and velocity; the action is an acceleration in [-1, 1].
        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:?}");
    }
}