use crate::engine::fitness::Metric;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StopReason {
MaxSteps,
CustomStop,
Interrupted,
}
pub const SMOOTHING_WINDOW: usize = 10;
pub const DEFAULT_CHECKPOINT_EVERY: usize = 10;
pub const DEFAULT_LR: f32 = 1e-3;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LogLevel {
None,
#[default]
Summ,
Minimal,
}
impl LogLevel {
pub fn parse(name: &str) -> Option<Self> {
match name.trim().to_ascii_lowercase().as_str() {
"none" | "off" | "silent" => Some(LogLevel::None),
"summ" | "summary" => Some(LogLevel::Summ),
"minimal" => Some(LogLevel::Minimal),
_ => None,
}
}
pub fn env_filter(self) -> &'static str {
match self {
LogLevel::Summ => "info",
LogLevel::Minimal | LogLevel::None => "warn",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CrossoverGate {
#[default]
Hard,
Soft,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CrossCullPolicy {
#[default]
Worst,
Random,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum MutationCullPolicy {
#[default]
InverseFitness,
Worst,
Random,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CrossoverOp {
OnePoint,
Uniform,
}
impl CrossoverOp {
pub fn required_parents(&self) -> usize {
match self {
CrossoverOp::OnePoint | CrossoverOp::Uniform => 2,
}
}
}
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Default)]
pub enum RunMode {
#[default]
Tabular,
OneCImage,
ThreeCImage,
Nlp,
Rl,
}
pub struct RaceConfig {
pub pop_size: usize,
pub checkpoint_every: usize,
pub crossover_rolls: usize,
pub mutate_rolls: usize,
pub crossover_gate: CrossoverGate,
pub crossover_gate_window: usize,
pub crossover_retries: usize,
pub crossover_cull_policy: CrossCullPolicy,
pub mutation_cull_policy: MutationCullPolicy,
pub elite_count: usize,
pub freeze_elites: bool,
pub smoothing_window: usize,
pub mode_specific: ModeConfig,
pub crossover_ops_pool: Vec<String>,
pub max_steps: Option<usize>,
pub hidden_dim_pool: Option<std::ops::RangeInclusive<usize>>,
pub hidden_dim_stride: usize,
pub combine_op_pool: Vec<String>,
pub activation_pool: Vec<String>,
pub standardize_op_pool: Vec<String>,
pub topology_options: crate::graph::topology::TopologyOptions,
pub crossover_prob: f32,
pub mutate_prob: f32,
pub custom_stop: StopFn,
pub log_level: LogLevel,
pub trace_file: bool,
pub metrics: Vec<Metric>,
pub mode: RunMode,
pub csv_export: bool,
pub history_flush_each: bool,
pub elite_save_topology: bool,
pub elite_save_safetensors: bool,
pub worst_save_topology: bool,
pub worst_save_safetensors: bool,
pub pop_pruner: Option<PopPruner>,
pub run_name: Option<String>,
pub run_topologies: Vec<crate::graph::topology::Topology>,
pub elite_checkpoint_weights: bool,
pub mutation_probation_steps: usize,
pub challenge_prob: f32,
}
pub fn run_progress(step: usize, max_steps: Option<usize>) -> Option<f32> {
let max = max_steps?;
if max == 0 {
return None;
}
Some((step as f32 / max as f32).min(1.0))
}
pub fn effective_challenge_prob(prob: f32, step: usize, max_steps: Option<usize>) -> f32 {
let progress = run_progress(step, max_steps).unwrap_or(0.0);
prob * (1.0 - progress)
}
pub(crate) fn challenge_fires(
run_seed: u64,
net_seed: u64,
step: usize,
prob: f32,
max_steps: Option<usize>,
) -> bool {
let eff = effective_challenge_prob(prob, step, max_steps);
if eff <= 0.0 {
return false;
}
let mut rng = fastrand::Rng::with_seed(run_seed ^ net_seed.rotate_left(17) ^ ((step as u64) << 32));
rng.f32() < eff
}
#[derive(Clone, Debug, PartialEq)]
pub enum ModeConfig {
Tabular(TabularConfig),
Rl(RlConfig),
}
impl ModeConfig {
pub fn run_mode(&self) -> RunMode {
match self {
ModeConfig::Tabular(_) => RunMode::Tabular,
ModeConfig::Rl(_) => RunMode::Rl,
}
}
pub fn rl(&self) -> Option<&RlConfig> {
match self {
ModeConfig::Rl(rl) => Some(rl),
ModeConfig::Tabular(_) => None,
}
}
pub fn is_rl(&self) -> bool {
matches!(self, ModeConfig::Rl(_))
}
pub fn mutation_catch_up(&self) -> bool {
match self {
ModeConfig::Tabular(t) => t.mutation_catch_up,
ModeConfig::Rl(r) => r.mutation_catch_up,
}
}
pub fn crossover_catch_up(&self) -> bool {
match self {
ModeConfig::Tabular(t) => t.crossover_catch_up,
ModeConfig::Rl(r) => r.crossover_catch_up,
}
}
pub fn run_pop_catch_up(&self) -> bool {
match self {
ModeConfig::Tabular(t) => t.run_pop_catch_up,
ModeConfig::Rl(r) => r.run_pop_catch_up,
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct TabularConfig {
pub mutation_catch_up: bool,
pub crossover_catch_up: bool,
pub run_pop_catch_up: bool,
}
impl TabularConfig {
pub fn builder() -> TabularConfigBuilder {
TabularConfigBuilder::default()
}
}
#[derive(Debug, Default)]
pub struct TabularConfigBuilder {
cfg: TabularConfig,
}
impl TabularConfigBuilder {
pub fn set_mutation_catch_up(mut self, yes: bool) -> Self {
self.cfg.mutation_catch_up = yes;
self
}
pub fn set_crossover_catch_up(mut self, yes: bool) -> Self {
self.cfg.crossover_catch_up = yes;
self
}
pub fn set_run_pop_catch_up(mut self, yes: bool) -> Self {
self.cfg.run_pop_catch_up = yes;
self
}
pub fn build(self) -> TabularConfig {
self.cfg
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct RlConfig {
pub mutation_catch_up: bool,
pub crossover_catch_up: bool,
pub run_pop_catch_up: bool,
}
impl Default for RlConfig {
fn default() -> Self {
Self {
mutation_catch_up: false,
crossover_catch_up: true,
run_pop_catch_up: false,
}
}
}
impl RlConfig {
pub fn builder() -> RlConfigBuilder {
RlConfigBuilder::default()
}
}
#[derive(Debug, Default)]
pub struct RlConfigBuilder {
cfg: RlConfig,
}
impl RlConfigBuilder {
pub fn set_mutation_catch_up(mut self, yes: bool) -> Self {
self.cfg.mutation_catch_up = yes;
self
}
pub fn set_crossover_catch_up(mut self, yes: bool) -> Self {
self.cfg.crossover_catch_up = yes;
self
}
pub fn set_run_pop_catch_up(mut self, yes: bool) -> Self {
self.cfg.run_pop_catch_up = yes;
self
}
pub fn build(self) -> RlConfig {
self.cfg
}
}
pub type StopFn = Option<Box<dyn Fn(&RaceSnapshot) -> bool + Send + Sync + 'static>>;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PopPrunerMethod {
#[default]
Hard,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PopPruner {
pub method: PopPrunerMethod,
pub steps: usize,
}
#[derive(Clone, Debug, Default)]
pub struct RunMetaCtx {
pub input_dim: usize,
pub output_dim: usize,
pub batch_size: Option<usize>,
pub dropout_prob: f32,
pub fitness_label: String,
pub direction: String,
pub pop_size: usize,
pub run_seed: u64,
}
#[derive(Clone)]
pub struct RaceSnapshot {
pub live_count: usize,
pub step: usize,
pub best_smoothed_fitness: f32,
pub worst_smoothed_fitness: f32,
pub mean_smoothed_fitness: f32,
pub culls: usize,
pub elapsed_seconds: u64,
}
impl RaceConfig {
pub fn device(&self) -> flodl::Device {
#[cfg(feature = "cuda")]
{
flodl::Device::CUDA(0)
}
#[cfg(not(feature = "cuda"))]
{
flodl::Device::CPU
}
}
pub fn defaults() -> Self {
const DEFAULT_HIDDEN_POOL: std::ops::RangeInclusive<usize> = 4..=8;
RaceConfig {
pop_size: 5,
checkpoint_every: DEFAULT_CHECKPOINT_EVERY,
crossover_rolls: 1,
mutate_rolls: 1,
crossover_gate: CrossoverGate::default(),
crossover_gate_window: 0,
crossover_retries: 0,
crossover_cull_policy: CrossCullPolicy::default(),
mutation_cull_policy: MutationCullPolicy::default(),
elite_count: 1,
freeze_elites: true,
smoothing_window: SMOOTHING_WINDOW,
mode_specific: ModeConfig::Tabular(TabularConfig::default()),
crossover_ops_pool: Vec::new(),
max_steps: None,
hidden_dim_pool: Some(DEFAULT_HIDDEN_POOL),
hidden_dim_stride: 16,
combine_op_pool: Vec::new(),
activation_pool: Vec::new(),
standardize_op_pool: Vec::new(),
topology_options: crate::graph::topology::TopologyOptions::default(),
crossover_prob: 0.5,
mutate_prob: 0.2,
custom_stop: None,
metrics: Vec::new(),
log_level: LogLevel::default(),
trace_file: false,
mode: RunMode::Tabular,
csv_export: true,
history_flush_each: false,
elite_save_topology: true,
elite_save_safetensors: true,
worst_save_topology: false,
worst_save_safetensors: false,
pop_pruner: None,
run_name: None,
run_topologies: Vec::new(),
elite_checkpoint_weights: true,
mutation_probation_steps: 0,
challenge_prob: 0.0,
}
}
pub fn builder() -> RaceConfigBuilder {
RaceConfigBuilder {
cfg: RaceConfig::defaults(),
pending_pruner: None,
}
}
}
pub type RlRaceConfig = RaceConfig;
pub type TabularRaceConfig = RaceConfig;
#[doc(hidden)]
pub fn tabular_race_config_builder() -> RaceConfigBuilder {
RaceConfigBuilder {
cfg: RaceConfig::defaults(),
pending_pruner: None,
}
}
#[doc(hidden)]
pub fn rl_race_config_builder() -> RaceConfigBuilder {
RaceConfigBuilder {
cfg: RaceConfig {
mode_specific: ModeConfig::Rl(RlConfig::default()),
..RaceConfig::defaults()
},
pending_pruner: None,
}
}
impl Default for RaceConfig {
fn default() -> Self {
Self::defaults()
}
}
pub struct RaceConfigBuilder {
cfg: RaceConfig,
pending_pruner: Option<PopPruner>,
}
impl RaceConfigBuilder {
pub fn set_run_pop_size(mut self, n: usize) -> Self {
self.cfg.pop_size = n;
self
}
pub fn set_run_checkpoint_every(mut self, n: usize) -> Self {
self.cfg.checkpoint_every = n.max(1);
self
}
pub fn set_crossover_rolls(mut self, n: usize) -> Self {
self.cfg.crossover_rolls = n;
self
}
pub fn set_mutate_rolls(mut self, n: usize) -> Self {
self.cfg.mutate_rolls = n;
self
}
pub fn set_crossover_gate(mut self, mode: CrossoverGate) -> Self {
self.cfg.crossover_gate = mode;
self
}
pub fn set_crossover_gate_window(mut self, k: usize) -> Self {
self.cfg.crossover_gate_window = k;
self
}
pub fn set_crossover_retries(mut self, n: usize) -> Self {
self.cfg.crossover_retries = n;
self
}
pub fn set_crossover_cull_policy(mut self, policy: CrossCullPolicy) -> Self {
self.cfg.crossover_cull_policy = policy;
self
}
pub fn set_mutation_cull_policy(mut self, policy: MutationCullPolicy) -> Self {
self.cfg.mutation_cull_policy = policy;
self
}
pub fn set_elite_count(mut self, n: usize) -> Self {
self.cfg.elite_count = n.max(1);
self
}
pub fn set_elite_freeze(mut self, yes: bool) -> Self {
self.cfg.freeze_elites = yes;
self
}
pub fn set_mutation_catch_up(mut self, yes: bool) -> Self {
match &mut self.cfg.mode_specific {
ModeConfig::Rl(rl) => rl.mutation_catch_up = yes,
ModeConfig::Tabular(t) => t.mutation_catch_up = yes,
}
self
}
pub fn set_crossover_catch_up(mut self, yes: bool) -> Self {
match &mut self.cfg.mode_specific {
ModeConfig::Rl(rl) => rl.crossover_catch_up = yes,
ModeConfig::Tabular(t) => t.crossover_catch_up = yes,
}
self
}
pub fn set_run_pop_catch_up(mut self, yes: bool) -> Self {
match &mut self.cfg.mode_specific {
ModeConfig::Rl(rl) => rl.run_pop_catch_up = yes,
ModeConfig::Tabular(t) => t.run_pop_catch_up = yes,
}
self
}
pub fn set_run_smoothing_window(mut self, k: usize) -> Self {
self.cfg.smoothing_window = k.max(1);
self
}
pub fn set_stop_custom(
mut self,
f: impl Fn(&RaceSnapshot) -> bool + Send + Sync + 'static,
) -> Self {
self.cfg.custom_stop = Some(Box::new(f));
self
}
pub fn set_crossover_ops_pool<I, S>(mut self, ops: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.cfg.crossover_ops_pool = ops.into_iter().map(|s| s.as_ref().to_string()).collect();
self
}
pub fn set_stop_max_steps(mut self, n: impl Into<Option<usize>>) -> Self {
self.cfg.max_steps = n.into();
self
}
pub fn set_pruner(mut self, pruner: PopPruner) -> Self {
self.cfg.pop_pruner = Some(pruner);
self
}
pub fn set_pruner_enabled(mut self, enabled: bool) -> Self {
self.cfg.pop_pruner = enabled.then(|| {
self.pending_pruner.take().unwrap_or(PopPruner {
method: PopPrunerMethod::Hard,
steps: 0,
})
});
self
}
pub fn set_pruner_method(mut self, method: PopPrunerMethod) -> Self {
match self.cfg.pop_pruner.as_mut() {
Some(pruner) => pruner.method = method,
None => {
let mut p = self.pending_pruner.take().unwrap_or(PopPruner {
method: PopPrunerMethod::Hard,
steps: 0,
});
p.method = method;
self.pending_pruner = Some(p);
}
}
self
}
pub fn set_pruner_steps(mut self, steps: usize) -> Self {
match self.cfg.pop_pruner.as_mut() {
Some(pruner) => pruner.steps = steps,
None => {
let mut p = self.pending_pruner.take().unwrap_or(PopPruner {
method: PopPrunerMethod::Hard,
steps: 0,
});
p.steps = steps;
self.pending_pruner = Some(p);
}
}
self
}
pub fn set_topology_hidden_dim_range(mut self, min: usize, max: usize) -> Self {
self.cfg.hidden_dim_pool = Some(min..=max);
self
}
pub fn set_topology_hidden_dim_stride(mut self, n: usize) -> Self {
self.cfg.hidden_dim_stride = n;
self
}
pub fn set_topology_combine_op_pool<I, S>(mut self, pool: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.cfg.combine_op_pool = pool.into_iter().map(|s| s.as_ref().to_string()).collect();
self
}
pub fn set_topology_activation_pool<I, S>(mut self, pool: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.cfg.activation_pool = pool.into_iter().map(|s| s.as_ref().to_string()).collect();
self
}
pub fn set_topology_standardize_op_pool<I, S>(mut self, pool: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.cfg.standardize_op_pool = pool.into_iter().map(|s| s.as_ref().to_string()).collect();
self
}
pub fn set_topology_dropout_prob(mut self, p: f32) -> Self {
self.cfg.topology_options.dropout_prob = p;
self
}
pub fn set_topology_seed(mut self, seed: usize) -> Self {
self.cfg.topology_options.topology_seed = seed;
self
}
pub fn set_topology_min_hidden_num_nodes(mut self, n: usize) -> Self {
self.cfg.topology_options.min_hidden_num_nodes = n;
self
}
pub fn set_topology_max_hidden_num_nodes(mut self, n: usize) -> Self {
self.cfg.topology_options.max_hidden_num_nodes = n;
self
}
pub fn set_topology_min_inputs_per_node(mut self, n: usize) -> Self {
self.cfg.topology_options.min_hidden_inputs_per_node = n;
self
}
pub fn set_topology_max_inputs_per_node(mut self, n: usize) -> Self {
self.cfg.topology_options.max_hidden_inputs_per_node = n;
self
}
pub fn set_topology_min_outputs_per_node(mut self, n: usize) -> Self {
self.cfg.topology_options.min_hidden_outputs_per_node = n;
self
}
pub fn set_topology_max_outputs_per_node(mut self, n: usize) -> Self {
self.cfg.topology_options.max_hidden_outputs_per_node = n;
self
}
pub fn set_topology_input_dim(mut self, n: usize) -> Self {
self.cfg.topology_options.input_dim = Some(n);
self
}
pub fn set_topology_output_dim(mut self, n: usize) -> Self {
self.cfg.topology_options.output_dim = Some(n);
self
}
pub fn set_crossover_prob(mut self, v: f32) -> Self {
self.cfg.crossover_prob = v;
self
}
pub fn set_mutate_prob(mut self, v: f32) -> Self {
self.cfg.mutate_prob = v;
self
}
pub fn set_run_metrics(mut self, metrics: Vec<Metric>) -> Self {
self.cfg.metrics = metrics;
self
}
pub fn set_run_name(mut self, name: impl Into<String>) -> Self {
self.cfg.run_name = Some(name.into());
self
}
pub fn set_run_log_level(mut self, level: LogLevel) -> Self {
self.cfg.log_level = level;
self
}
pub fn set_run_trace_file(mut self, enabled: bool) -> Self {
self.cfg.trace_file = enabled;
self
}
pub fn set_run_csv_export(mut self, enabled: bool) -> Self {
self.cfg.csv_export = enabled;
self
}
pub fn set_run_topologies(mut self, topos: Vec<crate::graph::topology::Topology>) -> Self {
self.cfg.run_topologies = topos;
self
}
pub fn set_elite_checkpoint_weights(mut self, enabled: bool) -> Self {
self.cfg.elite_checkpoint_weights = enabled;
self
}
pub fn set_mutation_probation_steps(mut self, k: usize) -> Self {
self.cfg.mutation_probation_steps = k;
self
}
pub fn set_run_challenge_prob(mut self, p: f32) -> Self {
assert!((0.0..=1.0).contains(&p), "challenge_prob must be in [0, 1]");
self.cfg.challenge_prob = p;
self
}
pub fn set_elite_save_topology(mut self, enabled: bool) -> Self {
self.cfg.elite_save_topology = enabled;
self
}
pub fn set_elite_save_safetensors(mut self, enabled: bool) -> Self {
self.cfg.elite_save_safetensors = enabled;
self
}
pub fn set_worst_save_topology(mut self, enabled: bool) -> Self {
self.cfg.worst_save_topology = enabled;
self
}
pub fn set_worst_save_safetensors(mut self, enabled: bool) -> Self {
self.cfg.worst_save_safetensors = enabled;
self
}
pub(crate) fn validate_single_stop(_cfg: &RaceConfig) -> Result<(), String> {
Ok(())
}
pub fn build(mut self) -> RaceConfig {
let cfg = self.cfg;
if let Some(p) = self.pending_pruner.take() {
tracing::warn!(
"set_pruner_method({:?})/set_pruner_steps({}) were called without set_pruner_enabled(true) — pruner is OFF",
p.method,
p.steps
);
}
Self::validate_single_stop(&cfg).unwrap_or_else(|e| panic!("invalid RaceConfig: {e}"));
cfg
}
}
impl RaceConfig {
fn parse_activation(s: &str) -> Result<crate::graph::node::Activation, String> {
use crate::graph::node::Activation::*;
Ok(match s.to_lowercase().as_str() {
"identity" => Identity,
"relu" => ReLU,
"gelu" => GeLU,
"silu" => SiLU,
"selu" => SELU,
"tanh" => Tanh,
"sigmoid" => Sigmoid,
"mish" => Mish,
"leaky_relu" => LeakyReLU,
"elu" => ELU,
"gelu_tanh" => GeluTanh,
"softplus" => Softplus,
"hardswish" => HardSwish,
"hardsigmoid" => HardSigmoid,
"sin" => Sin,
"cos" => Cos,
"softmax" => Softmax,
"log_softmax" | "logsoftmax" => LogSoftmax,
other => return Err(format!("unknown activation '{other}'")),
})
}
fn parse_combine(s: &str) -> Result<crate::graph::node::CombineOp, String> {
use crate::graph::node::CombineOp::*;
Ok(match s.to_lowercase().as_str() {
"add" => Add,
"mean" => Mean,
"mul" | "multiply" => Multiply,
"sub" | "subtract" => Subtract,
"div" | "divide" => Divide,
"max" => Max,
"min" => Min,
other => return Err(format!("unknown combine op '{other}'")),
})
}
pub fn resolved_crossover_ops(&self) -> Result<Vec<CrossoverOp>, String> {
if self.crossover_ops_pool.is_empty() {
return Ok(vec![CrossoverOp::OnePoint, CrossoverOp::Uniform]);
}
self.crossover_ops_pool
.iter()
.map(|s| match s.to_lowercase().as_str() {
"one_point" | "onepoint" => Ok(CrossoverOp::OnePoint),
"uniform" => Ok(CrossoverOp::Uniform),
other => Err(format!(
"unknown crossover op '{other}' (use \"one_point\" or \"uniform\")"
)),
})
.collect()
}
fn parse_standardize(s: &str) -> Result<crate::graph::node::StandardizeOp, String> {
use crate::graph::node::StandardizeOp::*;
Ok(match s.to_lowercase().as_str() {
"identity" => Identity,
"layernorm" => LayerNorm,
"rmsnorm" | "rms_norm" => RmsNorm,
"instancenorm" | "instance_norm" => InstanceNorm,
other => return Err(format!("unknown standardize op '{other}'")),
})
}
pub fn resolved_activation_pool(&self) -> Result<Vec<crate::graph::node::Activation>, String> {
if self.activation_pool.is_empty() {
return Ok(crate::evolution::pools::all_activations());
}
self.activation_pool
.iter()
.map(|s| Self::parse_activation(s))
.collect()
}
pub fn resolved_combine_pool(&self) -> Result<Vec<crate::graph::node::CombineOp>, String> {
if self.combine_op_pool.is_empty() {
return Ok(crate::evolution::pools::all_combine_ops());
}
self.combine_op_pool
.iter()
.map(|s| Self::parse_combine(s))
.collect()
}
pub fn resolved_standardize_pool(
&self,
) -> Result<Vec<crate::graph::node::StandardizeOp>, String> {
if self.standardize_op_pool.is_empty() {
return Ok(crate::evolution::pools::all_standardize_ops());
}
self.standardize_op_pool
.iter()
.map(|s| Self::parse_standardize(s))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::{LogLevel, RaceConfig, challenge_fires, effective_challenge_prob, run_progress};
use crate::utils::seed::derive_seed;
#[test]
fn run_progress_and_challenge_decay_share_one_convention() {
assert_eq!(run_progress(0, Some(10)), Some(0.0));
assert_eq!(run_progress(5, Some(10)), Some(0.5));
assert_eq!(run_progress(10, Some(10)), Some(1.0));
assert_eq!(run_progress(99, Some(10)), Some(1.0));
assert_eq!(run_progress(3, None), None);
assert_eq!(run_progress(3, Some(0)), None);
assert!((effective_challenge_prob(0.2, 5, Some(10)) - 0.1).abs() < 1e-6);
assert_eq!(effective_challenge_prob(0.2, 5, None), 0.2);
assert_eq!(effective_challenge_prob(0.2, 10, Some(10)), 0.0);
for step in 10..14 {
for net_seed in 0..50u64 {
assert!(
!challenge_fires(1, net_seed, step, 1.0, Some(10)),
"step {step} is past the budget — the knob is 0"
);
}
}
for net_seed in 0..50u64 {
assert!(challenge_fires(1, net_seed, 0, 1.0, Some(10)));
}
let fired = (0..200u64)
.filter(|net_seed| challenge_fires(1, *net_seed, 5, 1.0, Some(10)))
.count();
assert!(
fired > 0 && fired < 200,
"eff 0.5 over 200 independent net-steps should mix, got {fired}"
);
}
#[test]
fn log_level_names_parse_to_a_visible_verbosity() {
assert_eq!(LogLevel::parse("summ"), Some(LogLevel::Summ));
assert_eq!(LogLevel::parse("SUMM"), Some(LogLevel::Summ));
assert_eq!(LogLevel::parse("minimal"), Some(LogLevel::Minimal));
assert_eq!(LogLevel::parse("none"), Some(LogLevel::None));
assert_eq!(
LogLevel::parse("debug"),
None,
"env_logger levels pass through"
);
assert_eq!(LogLevel::Summ.env_filter(), "info");
assert_eq!(LogLevel::Minimal.env_filter(), "warn");
assert_eq!(LogLevel::None.env_filter(), "warn");
}
#[test]
fn stop_setters_are_exclusive_last_writer_wins() {
let cfg = RaceConfig::builder()
.set_stop_max_steps(15)
.build();
assert_eq!(cfg.max_steps, Some(15));
}
#[test]
fn challenge_trigger_is_per_net_not_per_step() {
let fired = (0..64usize)
.filter(|i| challenge_fires(42, derive_seed(42, *i), 3, 0.5,None))
.count();
assert!(
(16..=48).contains(&fired),
"trigger must track prob across nets, fired {fired}/64"
);
}
}