use crate::engine::fitness::Metric;
use crate::graph::topology::TopologyOptions;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StopReason {
MaxSteps,
TargetScore,
CustomStop,
}
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,
Full,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CrossoverGating {
#[default]
Hard,
Soft,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CrossCullPolicy {
#[default]
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_gating: CrossoverGating,
pub crossover_retries: usize,
pub crossover_cull_policy: CrossCullPolicy,
pub elite_count: usize,
pub crossover_ops_pool: Vec<String>,
pub max_steps: Option<usize>,
pub max_target_fitness: Option<f32>,
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 metrics: Vec<Metric>,
pub mode: RunMode,
pub csv_export: bool,
pub pop_pruner: Option<PopPruner>,
}
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: usize,
pub dropout_prob: f32,
pub fitness_label: String,
pub loss_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_gating: CrossoverGating::default(),
crossover_retries: 0,
crossover_cull_policy: CrossCullPolicy::default(),
elite_count: 0,
crossover_ops_pool: Vec::new(),
max_steps: None,
max_target_fitness: 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(),
mode: RunMode::Tabular,
csv_export: true,
pop_pruner: None,
}
}
pub fn builder() -> RaceConfigBuilder {
RaceConfigBuilder {
cfg: RaceConfig::defaults(),
pending_pruner: None,
}
}
}
impl Default for RaceConfig {
fn default() -> Self {
Self::defaults()
}
}
pub struct RaceConfigBuilder {
cfg: RaceConfig,
pending_pruner: Option<(PopPrunerMethod, usize)>,
}
impl RaceConfigBuilder {
pub fn set_pop_size(mut self, n: usize) -> Self {
self.cfg.pop_size = n;
self
}
pub fn set_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_gating(mut self, mode: CrossoverGating) -> Self {
self.cfg.crossover_gating = mode;
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_elite_count(mut self, n: usize) -> Self {
self.cfg.elite_count = n;
self
}
pub fn set_crossover_ops_pool(mut self, ops: Vec<String>) -> Self {
self.cfg.crossover_ops_pool = ops;
self
}
pub fn set_crossover_ops_pool_from_strs(self, ops: &[&str]) -> Self {
self.set_crossover_ops_pool(ops.iter().map(|s| s.to_string()).collect())
}
pub fn set_max_steps(mut self, n: impl Into<Option<usize>>) -> Self {
self.cfg.max_steps = n.into();
self
}
pub fn set_max_target_fitness(mut self, v: impl Into<Option<f32>>) -> Self {
self.cfg.max_target_fitness = v.into();
self
}
pub fn set_pop_pruner(mut self, enabled: bool) -> Self {
self.cfg.pop_pruner = enabled.then(|| {
let (method, steps) = self
.pending_pruner
.take()
.unwrap_or((PopPrunerMethod::Hard, 0));
PopPruner { method, steps }
});
self
}
pub fn set_pop_pruner_method(mut self, method: PopPrunerMethod, steps: usize) -> Self {
match self.cfg.pop_pruner.as_mut() {
Some(pruner) => *pruner = PopPruner { method, steps },
None => self.pending_pruner = Some((method, steps)),
}
self
}
pub fn set_hidden_range(mut self, min: usize, max: usize) -> Self {
self.cfg.hidden_dim_pool = Some(min..=max);
self
}
pub fn set_hidden_dim_stride(mut self, n: usize) -> Self {
self.cfg.hidden_dim_stride = n;
self
}
pub fn set_combine_op_pool(mut self, pool: Vec<String>) -> Self {
self.cfg.combine_op_pool = pool;
self
}
pub fn set_combine_ops(self, ops: &[&str]) -> Self {
self.set_combine_op_pool(ops.iter().map(|s| s.to_string()).collect())
}
pub fn set_activation_pool(mut self, pool: Vec<String>) -> Self {
self.cfg.activation_pool = pool;
self
}
pub fn set_activations(self, ops: &[&str]) -> Self {
self.set_activation_pool(ops.iter().map(|s| s.to_string()).collect())
}
pub fn set_standardize_op_pool(mut self, pool: Vec<String>) -> Self {
self.cfg.standardize_op_pool = pool;
self
}
pub fn set_standardize_ops(self, ops: &[&str]) -> Self {
self.set_standardize_op_pool(ops.iter().map(|s| s.to_string()).collect())
}
pub fn set_topology_options(mut self, opts: TopologyOptions) -> Self {
self.cfg.topology_options = opts;
self
}
pub fn set_min_hidden_num_nodes(mut self, n: usize) -> Self {
self.cfg.topology_options.min_hidden_num_nodes = n;
self
}
pub fn set_max_hidden_num_nodes(mut self, n: usize) -> Self {
self.cfg.topology_options.max_hidden_num_nodes = n;
self
}
pub fn set_min_hidden_inputs_per_node(mut self, n: usize) -> Self {
self.cfg.topology_options.min_hidden_inputs_per_node = n;
self
}
pub fn set_max_hidden_inputs_per_node(mut self, n: usize) -> Self {
self.cfg.topology_options.max_hidden_inputs_per_node = n;
self
}
pub fn set_min_hidden_outputs_per_node(mut self, n: usize) -> Self {
self.cfg.topology_options.min_hidden_outputs_per_node = n;
self
}
pub fn set_max_hidden_outputs_per_node(mut self, n: usize) -> Self {
self.cfg.topology_options.max_hidden_outputs_per_node = n;
self
}
pub fn set_input_dim(mut self, n: usize) -> Self {
self.cfg.topology_options.input_dim = Some(n);
self
}
pub fn set_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_metrics(mut self, metrics: Vec<Metric>) -> Self {
self.cfg.metrics = metrics;
self
}
pub fn set_log_level(mut self, level: LogLevel) -> Self {
self.cfg.log_level = level;
self
}
pub fn set_dropout_prob(mut self, p: f32) -> Self {
self.cfg.topology_options.dropout_prob = p;
self
}
pub fn set_mode(mut self, mode: RunMode) -> Self {
self.cfg.mode = mode;
self
}
pub fn set_csv_export(mut self, enabled: bool) -> Self {
self.cfg.csv_export = enabled;
self
}
pub(crate) fn validate_single_stop(cfg: &RaceConfig) -> Result<(), String> {
let _ = cfg;
Ok(())
}
pub fn build(mut self) -> RaceConfig {
let cfg = self.cfg;
if let Some((method, steps)) = self.pending_pruner.take() {
log::warn!(
"set_pop_pruner_method({method:?}, {steps}) was called without set_pop_pruner(true) — pruner is OFF"
);
}
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()
}
}