gras 2.0.1

A Genetic Programming framework for Neural Architecture Search in Rust
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
//! The anti-plateau challenge trigger — TRAINER-side (2026-10-10 move).
//!
//! Challenges were engine-wired: `RaceConfig::challenge_prob` + a per-step
//! engine roll handed every trainer a `challenged` bool. The engine has
//! forgotten all of it — the trigger is a pure function of values a trainer
//! already has in its [`StepEnv`](crate::trainer::StepEnv)/net seed, so the ROLL belongs where
//! RESPONSE does: inside `train_step`.
//!
//! # Recipe (both modes)
//!
//! Keep `challenge_prob` as your own field/const and roll the trigger per
//! (net, step):
//!
//! ```ignore
//! use gras::trainer::challenge::{ChallengeDecay, challenge_fires};
//!
//! let challenged = challenge_fires(
//!     ctx.env.run_seed,          // recorded in engine.json — replay-safe
//!     ctx.net_seed,
//!     step,
//!     self.challenge_prob,       // your knob (0.0 = never fires)
//!     ctx.env.max_steps,         // the decay denominator
//!     ChallengeDecay::Linear,    // or Custom { exponent } / Exponential
//! );
//! ```
//!
//! On a fired step the trainer owns the response: RL forces an action
//! TRAIN matches through [`ChallengePolicy`] + [`forced_action`] (pure,
//! resume replays the identical force); tabular jitters its batch
//! at the element level against [`effective_challenge_prob`] (the decayed
//! per-value rate). Ignoring the flag is legal — keep the knob at 0.
//!
//! # Challenge policies (CHA)
//!
//! [`ChallengePolicy::Random`] draws ONE action index and holds it for the whole
//! episode. [`ChallengePolicy::LeastAction`] forces, per turn, the action
//! policy wants least (`argmin`) — no draw, state-dependent, and the sharpest
//! push away from what the net already does.
//!
//! # Replay contract
//!
//! [`challenge_fires`] is a pure function of `(run_seed, net_seed, step)` —
//! no stored state, no engine involvement — so a resumed run and the original
//! run both re-derive the identical trigger. The run seed
//! is recorded in `engine.json`; the net seed rides the net's blueprint.
//! stateful schedule (a counter, an RNG stream) would advance
//! replay and silently break determinism — don't keep one.

use crate::utils::training::run_progress;

/// The decay shape of the challenge knob: `eff = prob × (1 − progress^k)` — one
/// closed form so replay/resume re-derive the identical probability.
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum ChallengeDecay {
    /// k = 1 — the knob falls in a straight line to 0 (historical default).
    #[default]
    Linear,
    /// k = 2 — holds the knob higher through mid-run, cliffs at the end.
    Exponential,
    /// k = any positive exponent. `Custom { exponent: 1.0 }` ≡ `Linear`.
    Custom { exponent: f32 },
}

impl ChallengeDecay {
    /// The exponent `k` as `1 − progress^k`, clamped to [0, 100]: non-finite
    /// negative k would explode the knob; the knob stays a probability.
    pub(crate) fn exponent(self) -> f32 {
        /// Lower clamp bound for the challenge exponent.
        const MIN_EXPONENT: f32 = 0.0;
        /// Upper clamp bound for the challenge exponent.
        const MAX_EXPONENT: f32 = 100.0;
        let k = match self {
            ChallengeDecay::Linear => 1.0,
            ChallengeDecay::Exponential => 2.0,
            ChallengeDecay::Custom { exponent } => exponent,
        };
        // max/min, NOT clamp: `clamp` propagates NaN, while `f32::max/min` fall
        // to the finite operand — the degenerate-value fallback we want.
        k.max(MIN_EXPONENT).min(MAX_EXPONENT)
    }
}

// `TryFrom<&str>` lives with the enum (reusable by CLI/config tooling), returns
// Result instead of panicking, and keeps the parse-and-error string in one place.
impl TryFrom<&str> for ChallengeDecay {
    type Error = String;
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        match s.trim() {
            "linear" => Ok(ChallengeDecay::Linear),
            "exponential" => Ok(ChallengeDecay::Exponential),
            other => {
                if let Some(k) = other.strip_prefix("custom:") {
                    let exponent: f32 = k.trim().parse().map_err(|_| {
                        format!("invalid challenge decay '{other}' (custom exponent must be a number, e.g. \"custom:1.5\")")
                    })?;
                    if !(exponent > 0.0) {
                        return Err(format!(
                            "invalid challenge decay '{other}' (exponent must be > 0)"
                        ));
                    }
                    Ok(ChallengeDecay::Custom { exponent })
                } else {
                    Err(format!(
                        "unknown challenge decay '{other}' (use \"linear\", \"exponential\" or \"custom:<exponent>\")"
                    ))
                }
            }
        }
    }
}

impl std::fmt::Display for ChallengeDecay {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChallengeDecay::Linear => write!(f, "linear"),
            ChallengeDecay::Exponential => write!(f, "exponential"),
            ChallengeDecay::Custom { exponent } => write!(f, "custom:{exponent}"),
        }
    }
}

/// What a fired challenge forces on the TRAIN matches. `Random` is the historical
/// behaviour; `LeastAction` aims at the policy's own blind spot instead.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChallengePolicy {
    /// One uniformly drawn action index, forced on every train turn of the step.
    #[default]
    Random,
    /// Per turn, the argmin over that turn's action values — the action the policy
    /// least wants. No draw, so it is replay-exact by construction.
    LeastAction,
}

impl TryFrom<&str> for ChallengePolicy {
    type Error = String;
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        match s.trim() {
            "random" => Ok(ChallengePolicy::Random),
            "least_action" | "least-action" => Ok(ChallengePolicy::LeastAction),
            other => Err(format!(
                "unknown challenge policy '{other}' (use \"random\" or \"least_action\")"
            )),
        }
    }
}

impl std::fmt::Display for ChallengePolicy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChallengePolicy::Random => write!(f, "random"),
            ChallengePolicy::LeastAction => write!(f, "least_action"),
        }
    }
}

/// The action a fired challenge forces, resolved per turn by the trainer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ForcedAction {
    /// One drawn index (the `Random` policy) — every turn of the step plays
    Exact(usize),
    /// The least-preferred action of the CURRENT turn (the `LeastAction` policy).
    Least,
}

impl ForcedAction {
    /// The action this turn plays: the drawn index, or the argmin of `values`.
    pub fn resolve(self, values: &[f32]) -> usize {
        match self {
            ForcedAction::Exact(a) => a,
            ForcedAction::Least => least_action(values),
        }
    }
}

/// The least-preferred action: argmin over `values`, ties to the lowest index.
/// NaN ranks as least-preferred, and it never panics (a challenge must not kill a run).
pub fn least_action(values: &[f32]) -> usize {
    let mut best = 0usize;
    for (i, v) in values.iter().enumerate() {
        let better = match (v.partial_cmp(&values[best]), v.is_nan()) {
            (None, true) => !values[best].is_nan(),
            (Some(std::cmp::Ordering::Less), _) => true,
            _ => false,
        };
        if better {
            best = i;
        }
    }
    best
}

/// What a fired challenge forces — pure, like the trigger itself. `Random`
/// reproduces the historical draw; `actions` is the env's count (the trainer's).
pub fn forced_action(
    policy: ChallengePolicy,
    net_seed: u64,
    step: usize,
    actions: usize,
) -> ForcedAction {
    match policy {
        ChallengePolicy::Random => {
            let mut rng = fastrand::Rng::with_seed(
                net_seed ^ (step as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15),
            );
            ForcedAction::Exact(rng.usize(..actions))
        }
        ChallengePolicy::LeastAction => ForcedAction::Least,
    }
}

/// The effective (decayed) challenge probability at `step`: the knob scaled to 0
/// by the end of the budget (flat when unbounded). [`challenge_fires`] decides
pub fn effective_challenge_prob(
    prob: f32,
    step: usize,
    max_steps: Option<usize>,
    shape: ChallengeDecay,
) -> f32 {
    match run_progress(step, max_steps) {
        None => prob,
        Some(progress) => prob * (1.0 - progress.powf(shape.exponent())),
    }
}

/// The challenge trigger: a pure function of `(run_seed, net_seed, step, prob,
/// max_steps, shape)`, so replay re-fires the exact same challenges.
pub fn challenge_fires(
    run_seed: u64,
    net_seed: u64,
    step: usize,
    prob: f32,
    max_steps: Option<usize>,
    shape: ChallengeDecay,
) -> bool {
    let eff = effective_challenge_prob(prob, step, max_steps, shape);
    if eff <= 0.0 {
        return false;
    }
    // Domain-separated seed: independent of every other per-step roll.
    let mut rng =
        fastrand::Rng::with_seed(run_seed ^ net_seed.rotate_left(17) ^ ((step as u64) << 32));
    rng.f32() < eff
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The decay table, pinned: at each progress the factor is exactly
    /// `1 − progress^k` for the three published shapes.
    #[test]
    fn decay_shapes_match_their_closed_form() {
        let cases: &[(f32, ChallengeDecay, f32)] = &[
            (0.0, ChallengeDecay::Linear, 1.0),
            (0.5, ChallengeDecay::Linear, 0.5),
            (1.0, ChallengeDecay::Linear, 0.0),
            (0.5, ChallengeDecay::Exponential, 0.75),
            (1.0, ChallengeDecay::Exponential, 0.0),
            (0.5, ChallengeDecay::Custom { exponent: 3.0 }, 0.875),
        ];
        for (progress, shape, want) in cases {
            let got = 1.0 - progress.powf(shape.exponent());
            assert!(
                (got - want).abs() < 1e-6,
                "progress {progress} shape {shape}: factor {got} ≠ {want}"
            );
        }
    }

    /// The knob decays to 0 at the budget and stays flat when unbounded —
    /// the two edges every schedule in the crate shares (`run_progress`).
    #[test]
    fn effective_prob_decays_to_zero_and_is_flat_unbounded() {
        assert_eq!(
            effective_challenge_prob(1.0, 5, Some(10), ChallengeDecay::Linear),
            0.5
        );
        assert_eq!(
            effective_challenge_prob(1.0, 10, Some(10), ChallengeDecay::Linear),
            0.0
        );
        // Past the budget clamps at 1.0 progress → factor 0 — never negative.
        assert_eq!(
            effective_challenge_prob(1.0, 99, Some(10), ChallengeDecay::Linear),
            0.0
        );
        // Unbounded: the knob is flat all run.
        assert_eq!(
            effective_challenge_prob(0.3, 999, None, ChallengeDecay::Linear),
            0.3
        );
    }

    /// Degenerate exponents clamp instead of exploding the knob: a hand-built
    /// `Custom { exponent: -1.0 }` or NaN falls back to a finite factor.
    #[test]
    fn degenerate_exponents_clamp() {
        assert_eq!(ChallengeDecay::Custom { exponent: -1.0 }.exponent(), 0.0);
        assert_eq!(
            ChallengeDecay::Custom { exponent: f32::NAN }.exponent(),
            0.0
        );
        assert_eq!(ChallengeDecay::Custom { exponent: 1e9 }.exponent(), 100.0);
    }

    /// `prob <= 0` never fires; `prob = 1` at step 0 always fires. The trigger
    /// is a pure function of its arguments — same inputs, same answer.
    #[test]
    fn trigger_is_pure_and_respects_the_knob() {
        assert!(!challenge_fires(
            1,
            7,
            3,
            0.0,
            Some(10),
            ChallengeDecay::Linear
        ));
        assert!(!challenge_fires(
            1,
            7,
            3,
            -0.5,
            Some(10),
            ChallengeDecay::Linear
        ));
        assert!(challenge_fires(
            1,
            7,
            0,
            1.0,
            Some(10),
            ChallengeDecay::Linear
        ));
        // Determinism: 100 identical calls agree.
        let first = challenge_fires(42, 1234, 5, 0.5, Some(10), ChallengeDecay::Linear);
        for step in 0..100 {
            let _ = step;
            assert_eq!(
                challenge_fires(42, 1234, 5, 0.5, Some(10), ChallengeDecay::Linear),
                first
            );
        }
    }

    /// The string form round-trips through the CLI/engine.json vocabulary,
    /// and bad input errors instead of panicking.
    #[test]
    fn decay_string_round_trip_and_parse_errors() {
        for (raw, want) in [
            ("linear", ChallengeDecay::Linear),
            ("exponential", ChallengeDecay::Exponential),
            ("custom:1.5", ChallengeDecay::Custom { exponent: 1.5 }),
        ] {
            assert_eq!(ChallengeDecay::try_from(raw).unwrap(), want);
            assert_eq!(want.to_string(), raw);
        }
        assert!(ChallengeDecay::try_from("sideways").is_err());
        assert!(ChallengeDecay::try_from("custom:zero").is_err());
        assert!(ChallengeDecay::try_from("custom:-1").is_err());
    }

    /// The trigger is per-NET, not per-step: at one step with `p = 0.5`
    /// population of distinct seeds must split roughly in half.
    #[test]
    fn trigger_is_per_net_not_per_step() {
        use crate::utils::seed::derive_seed;
        let fired = (0..64usize)
            .filter(|i| {
                challenge_fires(
                    42,
                    derive_seed(42, *i),
                    3,
                    0.5,
                    None,
                    ChallengeDecay::Linear,
                )
            })
            .count();
        assert!(
            (16..=48).contains(&fired),
            "trigger must track prob across nets, fired {fired}/64"
        );
    }

    /// `LeastAction` resolves per turn from the values it is handed — the same
    /// `ForcedAction` yields a different action as the policy's preference flips.
    #[test]
    fn least_action_resolves_per_turn_from_the_values() {
        let forced = forced_action(ChallengePolicy::LeastAction, 42, 7, 2);
        assert_eq!(forced, ForcedAction::Least);
        // The policy prefers action 0 here, so the challenge takes 1 — and vice versa.
        assert_eq!(forced.resolve(&[5.0, 1.0]), 1);
        assert_eq!(forced.resolve(&[1.0, 5.0]), 0);
        // Three actions: the argmin of the row, not a fixed column.
        assert_eq!(forced.resolve(&[0.5, -2.0, 0.9]), 1);
    }

    /// The argmin: ties go to the lowest index, a single action is always 0,
    /// empty slice is 0, and NaN ranks as least-preferred without ever panicking.
    #[test]
    fn least_action_picks_the_argmin_and_never_panics() {
        assert_eq!(least_action(&[0.5, -1.0, 2.0]), 1);
        assert_eq!(least_action(&[1.0, 1.0]), 0);
        assert_eq!(least_action(&[-0.0, 0.0]), 0);
        assert_eq!(least_action(&[3.0]), 0);
        assert_eq!(least_action(&[]), 0);
        assert_eq!(least_action(&[0.0, f32::NAN]), 1);
        assert_eq!(least_action(&[f32::NAN, 0.0]), 0);
        assert_eq!(least_action(&[f32::NAN, f32::NAN]), 0);
        assert_eq!(least_action(&[f32::INFINITY, f32::NEG_INFINITY]), 1);
    }

    /// The policy string round-trips through the CLI/`describe()` vocabulary,
    /// bad input errors instead of panicking.
    #[test]
    fn policy_string_round_trip_and_parse_errors() {
        for (raw, want) in [
            ("random", ChallengePolicy::Random),
            ("least_action", ChallengePolicy::LeastAction),
        ] {
            assert_eq!(ChallengePolicy::try_from(raw).unwrap(), want);
            assert_eq!(want.to_string(), raw);
        }
        assert_eq!(
            ChallengePolicy::try_from("least-action").unwrap(),
            ChallengePolicy::LeastAction
        );
        assert!(ChallengePolicy::try_from("argmin").is_err());
        assert_eq!(ChallengePolicy::default(), ChallengePolicy::Random);
    }

    /// `Random` must stay bit-identical to the historical inline draw — the policy
    /// refactor may not silently change what a recorded run would do.
    #[test]
    fn random_policy_reproduces_the_historical_draw() {
        /// The draw the examples used before the policy existed (verbatim).
        fn legacy(net_seed: u64, step: usize) -> usize {
            let mut rng = fastrand::Rng::with_seed(
                net_seed ^ (step as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15),
            );
            rng.usize(..2)
        }
        for net_seed in [1u64, 42, 999] {
            for step in 0..8usize {
                assert_eq!(
                    forced_action(ChallengePolicy::Random, net_seed, step, 2),
                    ForcedAction::Exact(legacy(net_seed, step))
                );
            }
        }
        // Golden values, pinned: a changed seed derivation fails here, not in a run.
        assert_eq!(
            forced_action(ChallengePolicy::Random, 42, 3, 2),
            ForcedAction::Exact(1)
        );
        assert_eq!(
            forced_action(ChallengePolicy::Random, 7, 0, 3),
            ForcedAction::Exact(2)
        );
    }

    /// Approximate-rate check: at `prob = p`, the fraction of steps that fire
    /// over an unbounded run converges to p (the knob means what it says).
    #[test]
    fn firing_rate_tracks_the_knob() {
        const P: f32 = 0.25;
        let fires = (0..4000u64)
            .filter(|&i| challenge_fires(9, i as u64, i as usize, P, None, ChallengeDecay::Linear))
            .count();
        let rate = fires as f32 / 4000.0;
        assert!((rate - P).abs() < 0.03, "rate {rate} far from knob {P}");
    }
}