Skip to main content

VariationGenerator

Trait VariationGenerator 

Source
pub trait VariationGenerator: Send + Sync {
    // Required methods
    fn next(
        &mut self,
        baseline: &ConfigSnapshot,
        visited: &HashSet<Variation>,
    ) -> Option<Variation>;
    fn name(&self) -> &'static str;
}
Expand description

A strategy for generating parameter variations one at a time.

Each call to VariationGenerator::next must produce a variation that changes exactly one parameter from the baseline. The caller (ExperimentEngine) is responsible for tracking visited variations and passing them back via visited.

Implementations hold mutable state (position cursor, RNG seed) and must be both Send and Sync so that ExperimentEngine can be used with tokio::spawn. The engine loop accesses the generator exclusively via &mut self, so no concurrent access occurs in practice.

§Implementing a Custom Generator

use std::collections::HashSet;
use zeph_experiments::{ConfigSnapshot, ParameterKind, Variation, VariationValue, VariationGenerator};

/// Always suggests temperature = 0.5, then exhausts.
struct FixedSuggestion;

impl VariationGenerator for FixedSuggestion {
    fn next(&mut self, _baseline: &ConfigSnapshot, visited: &HashSet<Variation>) -> Option<Variation> {
        let v = Variation {
            parameter: ParameterKind::Temperature,
            value: VariationValue::from(0.5_f64),
        };
        if visited.contains(&v) { None } else { Some(v) }
    }

    fn name(&self) -> &'static str { "fixed" }
}

fn main() {
    let mut variation_gen = FixedSuggestion;
    let baseline = ConfigSnapshot::default();
    let mut visited = HashSet::new();
    let first = variation_gen.next(&baseline, &visited).unwrap();
    visited.insert(first);
    assert!(variation_gen.next(&baseline, &visited).is_none());
}

Required Methods§

Source

fn next( &mut self, baseline: &ConfigSnapshot, visited: &HashSet<Variation>, ) -> Option<Variation>

Produce the next untested variation, or None if the space is exhausted.

  • baseline — the current best-known configuration snapshot (updated on acceptance).
  • visited — all variations already tested in this session; must not be returned again.
Source

fn name(&self) -> &'static str

Strategy name used in log messages and experiment reports.

Implementors§