use crate::evolution::selection::SelectionMethod;
use crate::graph::topology::FeatureSplitPolicy;
use crate::utils::fitness::Metric;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StopReason {
MaxSteps,
CustomStop,
Interrupted,
}
pub const SMOOTHING_WINDOW: usize = 5;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, strum::IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum MeanType {
Arithmetic,
SignedLog,
#[default]
Median,
}
impl MeanType {
pub fn as_str(&self) -> &'static str {
(*self).into()
}
pub fn parse(raw: &str) -> Option<Self> {
match raw.trim().to_ascii_lowercase().as_str() {
"arithmetic" | "mean" | "simple" | "average" => Some(MeanType::Arithmetic),
"signed_log" | "signedlog" | "signed-log" | "log1p" => Some(MeanType::SignedLog),
"median" => Some(MeanType::Median),
_ => None,
}
}
}
impl std::fmt::Display for MeanType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl std::str::FromStr for MeanType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
MeanType::parse(s)
.ok_or_else(|| format!("{s}: expected one of arithmetic, signed_log, median"))
}
}
pub const DEFAULT_MEAN_TYPE: MeanType = MeanType::Median;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, strum::IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum TrainTicket {
MostFit,
#[default]
MostFitFewestUpdates,
MostFitMostUpdates,
LeastFitFewestUpdates,
LeastFitMostUpdates,
LeastFit,
}
impl TrainTicket {
pub const ALL: [TrainTicket; 6] = [
TrainTicket::MostFit,
TrainTicket::MostFitFewestUpdates,
TrainTicket::MostFitMostUpdates,
TrainTicket::LeastFitFewestUpdates,
TrainTicket::LeastFitMostUpdates,
TrainTicket::LeastFit,
];
pub const ALL_NAMES: [&str; 6] = [
"most_fit",
"most_fit_fewest_updates",
"most_fit_most_updates",
"least_fit_fewest_updates",
"least_fit_most_updates",
"least_fit",
];
pub fn as_str(self) -> &'static str {
self.into()
}
pub fn parse(raw: &str) -> Option<Self> {
let key = raw.trim().to_ascii_lowercase().replace([' ', '-'], "_");
match key.as_str() {
"most_fit" | "best_fit" => Some(TrainTicket::MostFit),
"most_fit_most_updates" | "best_most" => Some(TrainTicket::MostFitMostUpdates),
"most_fit_fewest_updates" | "most_fit_least_updates" | "best_fewest" => {
Some(TrainTicket::MostFitFewestUpdates)
}
"least_fit" | "worst_fit" => Some(TrainTicket::LeastFit),
"least_fit_most_updates" | "worst_most" => Some(TrainTicket::LeastFitMostUpdates),
"least_fit_fewest_updates" | "least_fit_least_updates" | "worst_fewest" => {
Some(TrainTicket::LeastFitFewestUpdates)
}
_ => None,
}
}
pub fn fitness_inverted(self) -> bool {
matches!(
self,
TrainTicket::LeastFit
| TrainTicket::LeastFitFewestUpdates
| TrainTicket::LeastFitMostUpdates
)
}
pub fn updates_armed(self) -> bool {
!matches!(self, TrainTicket::MostFit | TrainTicket::LeastFit)
}
pub fn updates_inverted(self) -> bool {
matches!(
self,
TrainTicket::MostFitFewestUpdates | TrainTicket::LeastFitFewestUpdates
)
}
pub fn with_fitness_inverted(self, inverted: bool) -> Self {
match (inverted, self.updates_armed(), self.updates_inverted()) {
(false, false, _) => TrainTicket::MostFit,
(true, false, _) => TrainTicket::LeastFit,
(false, true, true) => TrainTicket::MostFitFewestUpdates,
(true, true, true) => TrainTicket::LeastFitFewestUpdates,
(false, true, false) => TrainTicket::MostFitMostUpdates,
(true, true, false) => TrainTicket::LeastFitMostUpdates,
}
}
pub fn with_updates_armed(self, armed: bool) -> Self {
if !armed {
return if self.fitness_inverted() {
TrainTicket::LeastFit
} else {
TrainTicket::MostFit
};
}
match (
self.fitness_inverted(),
self.updates_armed(),
self.updates_inverted(),
) {
(false, true, false) => TrainTicket::MostFitMostUpdates,
(true, true, false) => TrainTicket::LeastFitMostUpdates,
(false, _, _) => TrainTicket::MostFitFewestUpdates,
(true, _, _) => TrainTicket::LeastFitFewestUpdates,
}
}
pub fn from_bools(inverted: bool, updates_armed: bool) -> Self {
TrainTicket::MostFitFewestUpdates
.with_fitness_inverted(inverted)
.with_updates_armed(updates_armed)
}
}
impl std::fmt::Display for TrainTicket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl std::str::FromStr for TrainTicket {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
TrainTicket::parse(s)
.ok_or_else(|| format!("{s}: expected one of {}", TrainTicket::ALL_NAMES.join(", ")))
}
}
pub const DEFAULT_TRAIN_TICKET: TrainTicket = TrainTicket::MostFitFewestUpdates;
pub const DEFAULT_TRAIN_MERIT_LEARN_ONLY: bool = false;
pub const DEFAULT_POLICY_SCOPED_FITNESS: bool = true;
pub const DEFAULT_WORST_SAVE_TOPOLOGY: bool = false;
pub const DEFAULT_CHECKPOINT_EVERY: usize = 25;
pub const DEFAULT_POP_SIZE: usize = 10;
pub const DEFAULT_MAX_STEPS: usize = 10;
pub const DEFAULT_CROSSOVER_SHARE: f32 = 0.05;
pub const DEFAULT_MUTATION_SHARE: f32 = 0.02;
pub const DEFAULT_CHAMPION_COUNT: usize = DEFAULT_POP_SIZE / 10;
pub const DEFAULT_MUTATION_PROBATION_STEPS: usize = 0;
pub const DEFAULT_CROSSOVER_PROBATION_STEPS: usize = 0;
pub const DEFAULT_RECOVERY_RETRIES: usize = 0;
pub const DEFAULT_RECOVERY_SAME_SEED: bool = false;
pub const DEFAULT_RECOVERY_TOLERANCE: f32 = 0.0;
pub const DEFAULT_INIT_PROBATION_STEPS: usize = 0;
pub const DEFAULT_HIDDEN_DIM_STRIDE: usize = 16;
pub const DEFAULT_TOURNAMENT_SIZE: usize = 2;
pub const DEFAULT_CROSSOVER_FITTER_BASE: bool = true;
pub const DEFAULT_TRACE_FILE: bool = false;
pub const DEFAULT_SAVE_OPTIM: bool = true;
pub const DEFAULT_CSV_EXPORT: bool = true;
pub const DEFAULT_CHAMPION_SAVE_TOPOLOGY: bool = true;
pub const DEFAULT_CHAMPION_PREVIOUS_LOAD: bool = true;
pub const DEFAULT_RUN_MODE: RunMode = RunMode::Tabular;
pub const DEFAULT_HIDDEN_POOL: std::ops::RangeInclusive<usize> = 16..=64;
pub const DEFAULT_TRAIN_MERIT_P_MIN: f32 = 0.25;
pub const DEFAULT_TRAIN_MERIT_P_MAX: f32 = 0.75;
pub const DEFAULT_EVAL_SHARE: f32 = 0.2;
pub const EVAL_SHARE_MIN: f32 = 0.01;
pub const EVAL_SHARE_MAX: f32 = 0.9;
pub const DEFAULT_TOPOLOGY_DRIFT: f32 = 0.1;
pub const DEFAULT_MUTATION_NOVELTY_MIN: f32 = 0.4;
pub const DEFAULT_MUTATION_NOVELTY_DRAWS: usize = 10;
#[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 parse_verbose(raw: &str) -> Option<(Self, Option<&'static str>)> {
match raw.trim().to_ascii_lowercase().as_str() {
"debug" => Some((LogLevel::Summ, Some("debug"))),
"trace" => Some((LogLevel::Summ, Some("trace"))),
other => LogLevel::parse(other).map(|level| (level, 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, strum::IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum CrossCullPolicy {
LowestFitness,
Random,
HighestTicketChance,
#[default]
LowestFitnessMostUpdates,
}
impl std::fmt::Display for CrossCullPolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str((*self).into())
}
}
impl std::str::FromStr for CrossCullPolicy {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_ascii_lowercase().as_str() {
"lowest_fitness" | "worst" => Ok(Self::LowestFitness),
"random" => Ok(Self::Random),
"highest_ticket_chance" | "unfed" => Ok(Self::HighestTicketChance),
"lowest_fitness_most_updates" | "spent" => Ok(Self::LowestFitnessMostUpdates),
_ => Err(format!(
"{s}: expected lowest_fitness, random, highest_ticket_chance or lowest_fitness_most_updates"
)),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, strum::IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum MutationCullPolicy {
LowestFitness,
Random,
HighestTicketChance,
#[default]
LowestFitnessMostUpdates,
}
impl std::fmt::Display for MutationCullPolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str((*self).into())
}
}
impl std::str::FromStr for MutationCullPolicy {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_ascii_lowercase().as_str() {
"lowest_fitness" | "worst" => Ok(Self::LowestFitness),
"random" => Ok(Self::Random),
"highest_ticket_chance" | "unfed" => Ok(Self::HighestTicketChance),
"lowest_fitness_most_updates" | "spent" => Ok(Self::LowestFitnessMostUpdates),
_ => Err(format!(
"{s}: expected lowest_fitness, random, highest_ticket_chance or lowest_fitness_most_updates"
)),
}
}
}
#[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,
}
}
}
impl TryFrom<&str> for CrossoverOp {
type Error = String;
fn try_from(s: &str) -> Result<Self, Self::Error> {
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\")"
)),
}
}
}
#[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_share: f32,
pub mutation_share: f32,
pub crossover_cull_policy: CrossCullPolicy,
pub mutation_cull_policy: MutationCullPolicy,
pub champion_count: usize,
pub train_merit_p_min: f32,
pub train_merit_p_max: f32,
pub train_ticket: TrainTicket,
pub train_merit_learn_only: bool,
pub eval_share: f32,
pub smoothing_window: usize,
pub mean_type: MeanType,
pub policy_scoped_fitness: bool,
pub worst_save_topology: bool,
pub mode_specific: ModeConfig,
pub crossover_ops_pool: Vec<String>,
pub crossover_selection: SelectionMethod,
pub crossover_fitter_base: bool,
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 feature_split_pool: Vec<String>,
pub drift_prob: f32,
pub activation_pool: Vec<String>,
pub standardize_op_pool: Vec<String>,
pub topology_options: crate::graph::topology::TopologyOptions,
pub mutation_novelty_min: f32,
pub mutation_novelty_draws: usize,
pub custom_stop: StopFn,
pub log_level: LogLevel,
pub trace_file: bool,
pub save_optim: bool,
pub metrics: Vec<Metric>,
pub selection_metrics: Vec<crate::utils::fitness::SelectionMetric>,
pub mode: RunMode,
pub csv_export: bool,
pub champion_save_topology: bool,
pub run_name: Option<String>,
pub champion_previous_load: bool,
pub champion_previous_dir: Option<std::path::PathBuf>,
pub mutation_probation_steps: usize,
pub crossover_probation_steps: usize,
pub init_probation_steps: usize,
pub recovery_retries: usize,
pub recovery_tolerance: f32,
pub recovery_same_seed: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ModeConfig {
Tabular,
Rl,
}
pub type StopFn = Option<Box<dyn Fn(&RaceSnapshot) -> bool + Send + Sync + 'static>>;
#[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 {
RaceConfig {
pop_size: DEFAULT_POP_SIZE,
checkpoint_every: DEFAULT_CHECKPOINT_EVERY,
crossover_share: DEFAULT_CROSSOVER_SHARE,
mutation_share: DEFAULT_MUTATION_SHARE,
crossover_cull_policy: CrossCullPolicy::default(),
mutation_cull_policy: MutationCullPolicy::default(),
champion_count: DEFAULT_CHAMPION_COUNT,
train_merit_p_min: DEFAULT_TRAIN_MERIT_P_MIN,
train_merit_p_max: DEFAULT_TRAIN_MERIT_P_MAX,
train_ticket: DEFAULT_TRAIN_TICKET,
train_merit_learn_only: DEFAULT_TRAIN_MERIT_LEARN_ONLY,
eval_share: DEFAULT_EVAL_SHARE,
smoothing_window: SMOOTHING_WINDOW,
mean_type: DEFAULT_MEAN_TYPE,
policy_scoped_fitness: DEFAULT_POLICY_SCOPED_FITNESS,
worst_save_topology: DEFAULT_WORST_SAVE_TOPOLOGY,
mode_specific: ModeConfig::Tabular,
crossover_ops_pool: Vec::new(),
crossover_selection: SelectionMethod::Tournament {
tournament_size: DEFAULT_TOURNAMENT_SIZE,
},
crossover_fitter_base: DEFAULT_CROSSOVER_FITTER_BASE,
max_steps: Some(DEFAULT_MAX_STEPS),
hidden_dim_pool: Some(DEFAULT_HIDDEN_POOL),
hidden_dim_stride: DEFAULT_HIDDEN_DIM_STRIDE,
combine_op_pool: Vec::new(),
feature_split_pool: Vec::new(),
drift_prob: DEFAULT_TOPOLOGY_DRIFT,
activation_pool: Vec::new(),
standardize_op_pool: Vec::new(),
topology_options: crate::graph::topology::TopologyOptions::default(),
mutation_novelty_min: DEFAULT_MUTATION_NOVELTY_MIN,
mutation_novelty_draws: DEFAULT_MUTATION_NOVELTY_DRAWS,
custom_stop: None,
metrics: Vec::new(),
selection_metrics: Vec::new(),
log_level: LogLevel::default(),
trace_file: DEFAULT_TRACE_FILE,
save_optim: DEFAULT_SAVE_OPTIM,
mode: DEFAULT_RUN_MODE,
csv_export: DEFAULT_CSV_EXPORT,
champion_save_topology: DEFAULT_CHAMPION_SAVE_TOPOLOGY,
run_name: None,
champion_previous_load: DEFAULT_CHAMPION_PREVIOUS_LOAD,
champion_previous_dir: None,
mutation_probation_steps: DEFAULT_MUTATION_PROBATION_STEPS,
crossover_probation_steps: DEFAULT_CROSSOVER_PROBATION_STEPS,
init_probation_steps: DEFAULT_INIT_PROBATION_STEPS,
recovery_retries: DEFAULT_RECOVERY_RETRIES,
recovery_tolerance: DEFAULT_RECOVERY_TOLERANCE,
recovery_same_seed: DEFAULT_RECOVERY_SAME_SEED,
}
}
pub fn selection_metric_terms(&self, metrics: &[Metric]) -> Vec<(usize, f32)> {
self.selection_metrics
.iter()
.filter_map(
|term| match metrics.iter().position(|m| m.label == term.label) {
Some(index) => Some((index, term.weight)),
None => {
tracing::warn!(
"selection_metric {:?}: no metric with that label was registered with \
set_run_metrics — the term is ignored",
term.label
);
None
}
},
)
.collect()
}
pub fn resolved_feature_split_pool(&self) -> Result<Vec<Option<FeatureSplitPolicy>>, String> {
super::pool::resolve::<super::pool::FeatureSplitPool>(self)
}
pub fn feature_split_pool_names(&self) -> Vec<String> {
super::pool::names::<super::pool::FeatureSplitPool>(self)
}
pub fn from_snapshot(snap: &crate::state::ConfigSnapshot) -> Self {
let mut cfg = RaceConfig::defaults();
cfg.pop_size = snap.pop_size;
cfg.checkpoint_every = snap.checkpoint_every;
cfg.crossover_share = snap.crossover_share;
cfg.mutation_share = snap.mutation_share;
cfg.mutation_novelty_min = snap.mutation_novelty_min;
cfg.mutation_novelty_draws = snap.mutation_novelty_draws.max(1);
cfg.champion_count = snap.champion_count;
cfg.crossover_ops_pool = snap.crossover_ops_pool.clone();
cfg.crossover_fitter_base = snap.crossover_fitter_base;
cfg.max_steps = snap.max_steps;
cfg.log_level = LogLevel::parse(&snap.log_level).unwrap_or(cfg.log_level);
cfg.trace_file = snap.trace_file;
cfg.smoothing_window = snap.smoothing_window;
cfg.mean_type = MeanType::parse(&snap.mean_type).unwrap_or(DEFAULT_MEAN_TYPE);
cfg.train_ticket = TrainTicket::parse(&snap.train_ticket).unwrap_or(cfg.train_ticket);
cfg.csv_export = snap.csv_export;
cfg.save_optim = snap.save_optim;
cfg.run_name = snap.run_name.clone();
cfg.selection_metrics = snap.selection_metrics.clone();
cfg.train_merit_p_min = snap.train_merit_p_min;
cfg.train_merit_p_max = snap.train_merit_p_max;
cfg.train_merit_learn_only = snap.train_merit_learn_only;
cfg.policy_scoped_fitness = snap.policy_scoped_fitness;
cfg.worst_save_topology = snap.worst_save_topology;
cfg.drift_prob = snap.drift_prob;
cfg.champion_previous_load = snap.champion_previous_load;
cfg.champion_previous_dir = snap
.champion_previous_dir
.as_ref()
.map(std::path::PathBuf::from);
cfg.champion_save_topology = snap.champion_save_topology;
cfg.mutation_probation_steps = snap.mutation_probation_steps;
cfg.crossover_probation_steps = snap.crossover_probation_steps;
cfg.init_probation_steps = snap.init_probation_steps;
cfg.recovery_retries = snap.recovery_retries;
cfg.recovery_tolerance = snap.recovery_tolerance;
cfg.recovery_same_seed = snap.recovery_same_seed;
let cull = snap.crossover_cull_policy.to_ascii_lowercase();
cfg.crossover_cull_policy = if cull.contains("lowest_fitness_most_updates") {
CrossCullPolicy::LowestFitnessMostUpdates
} else if cull.contains("highest_ticket_chance") {
CrossCullPolicy::HighestTicketChance
} else if cull.contains("random") {
CrossCullPolicy::Random
} else {
CrossCullPolicy::LowestFitness
};
let mcull = snap.mutation_cull_policy.to_ascii_lowercase();
cfg.mutation_cull_policy = if mcull.contains("lowest_fitness_most_updates") {
MutationCullPolicy::LowestFitnessMostUpdates
} else if mcull.contains("highest_ticket_chance") {
MutationCullPolicy::HighestTicketChance
} else if mcull.contains("random") {
MutationCullPolicy::Random
} else if mcull.contains("lowest_fitness") {
MutationCullPolicy::LowestFitness
} else {
MutationCullPolicy::LowestFitness
};
let selection = snap.crossover_selection.to_ascii_lowercase();
cfg.crossover_selection = if selection.contains("tournament") {
let k = selection
.split(|c: char| !c.is_ascii_digit())
.filter_map(|n| n.parse::<usize>().ok())
.next()
.unwrap_or(2);
SelectionMethod::Tournament { tournament_size: k }
} else {
SelectionMethod::Roulette
};
cfg.feature_split_pool = if snap.feature_split_pool.is_empty() {
vec![
snap.feature_split
.clone()
.unwrap_or_else(|| "none".to_string()),
]
} else {
snap.feature_split_pool.clone()
};
cfg.mode = if snap.mode.eq_ignore_ascii_case("rl") {
RunMode::Rl
} else {
RunMode::Tabular
};
cfg
}
pub fn builder() -> RaceConfigBuilder {
RaceConfigBuilder {
cfg: RaceConfig::defaults(),
}
}
}
pub type RlRaceConfig = RaceConfig;
pub type TabularRaceConfig = RaceConfig;
#[doc(hidden)]
pub fn tabular_race_config_builder() -> RaceConfigBuilder {
RaceConfigBuilder {
cfg: RaceConfig::defaults(),
}
}
#[doc(hidden)]
pub fn rl_race_config_builder() -> RaceConfigBuilder {
RaceConfigBuilder {
cfg: RaceConfig {
mode_specific: ModeConfig::Rl,
..RaceConfig::defaults()
},
}
}
pub(crate) fn parse_feature_split_wire(raw: &str) -> Option<FeatureSplitPolicy> {
let raw = raw.to_ascii_lowercase();
if raw.contains("inherited") {
Some(FeatureSplitPolicy::Inherited)
} else if raw.contains("exclusive") {
Some(FeatureSplitPolicy::Exclusive)
} else if raw.contains("random") {
Some(FeatureSplitPolicy::Random)
} else {
None
}
}
pub(crate) fn parse_feature_split_pool_entry(raw: &str) -> Option<Option<FeatureSplitPolicy>> {
match raw.trim().to_ascii_lowercase().as_str() {
"none" | "off" | "split_none" => Some(None),
"exclusive" | "split_exclusive" => Some(Some(FeatureSplitPolicy::Exclusive)),
"random" | "split_random" => Some(Some(FeatureSplitPolicy::Random)),
_ => None,
}
}
impl Default for RaceConfig {
fn default() -> Self {
Self::defaults()
}
}
pub struct RaceConfigBuilder {
cfg: RaceConfig,
}
impl RaceConfigBuilder {
pub fn set_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;
self
}
pub fn set_run_save_optim(mut self, enabled: bool) -> Self {
self.cfg.save_optim = enabled;
self
}
pub fn set_crossover_share(mut self, share: f32) -> Self {
self.cfg.crossover_share = if share.is_finite() {
share.clamp(0.0, 1.0)
} else {
0.0
};
self
}
pub fn set_mutation_share(mut self, share: f32) -> Self {
self.cfg.mutation_share = if share.is_finite() {
share.clamp(0.0, 1.0)
} else {
0.0
};
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_champion_count(mut self, n: usize) -> Self {
self.cfg.champion_count = n.max(1);
self
}
pub fn set_ticket_p_min(mut self, p_min: f32) -> Self {
self.cfg.train_merit_p_min = if p_min.is_finite() {
p_min.clamp(0.0, 1.0)
} else {
DEFAULT_TRAIN_MERIT_P_MIN
};
self
}
pub fn set_ticket_p_max(mut self, p_max: f32) -> Self {
self.cfg.train_merit_p_max = if p_max.is_finite() {
p_max.clamp(0.0, 1.0)
} else {
DEFAULT_TRAIN_MERIT_P_MAX
};
self
}
pub fn set_train_ticket(mut self, legs: TrainTicket) -> Self {
self.cfg.train_ticket = legs;
self
}
pub fn set_ticket_learn_only(mut self, yes: bool) -> Self {
self.cfg.train_merit_learn_only = yes;
self
}
pub fn set_fitness_policy_scoped(mut self, yes: bool) -> Self {
self.cfg.policy_scoped_fitness = yes;
self
}
pub fn set_worst_save_topology(mut self, enabled: bool) -> Self {
self.cfg.worst_save_topology = enabled;
self
}
pub fn set_topology_drift(mut self, p: f32) -> Self {
self.cfg.drift_prob = if p.is_finite() {
p.clamp(0.0, 1.0)
} else {
DEFAULT_TOPOLOGY_DRIFT
};
self
}
pub fn set_fitness_smoothing_window(mut self, k: usize) -> Self {
self.cfg.smoothing_window = k.max(1);
self
}
pub fn set_fitness_mean_type(mut self, mean_type: MeanType) -> Self {
self.cfg.mean_type = mean_type;
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_crossover_selection(mut self, method: SelectionMethod) -> Self {
self.cfg.crossover_selection = method;
self
}
pub fn set_crossover_fitter_base(mut self, on: bool) -> Self {
self.cfg.crossover_fitter_base = on;
self
}
pub fn set_topology_feature_split(mut self, policy: FeatureSplitPolicy) -> Self {
self.cfg.feature_split_pool = vec![policy.to_string()];
self
}
pub fn set_topology_feature_split_pool<I, S>(mut self, pool: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.cfg.feature_split_pool = pool.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_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_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_run_metrics(mut self, metrics: Vec<Metric>) -> Self {
self.cfg.metrics = metrics;
self
}
pub fn set_selection_metric(mut self, label: impl Into<String>, weight: f32) -> Self {
let label = label.into();
match self
.cfg
.selection_metrics
.iter_mut()
.find(|m| m.label == label)
{
Some(term) => term.weight = weight,
None => self
.cfg
.selection_metrics
.push(crate::utils::fitness::SelectionMetric { label, weight }),
}
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_champion_previous_load(mut self, enabled: bool) -> Self {
self.cfg.champion_previous_load = enabled;
self
}
pub fn set_champion_previous_dir(mut self, dir: impl Into<std::path::PathBuf>) -> Self {
self.cfg.champion_previous_dir = Some(dir.into());
self
}
pub fn set_mutation_novelty_min(mut self, d_min: f32) -> Self {
self.cfg.mutation_novelty_min = d_min.max(0.0);
self
}
pub fn set_mutation_novelty_draws(mut self, draws: usize) -> Self {
self.cfg.mutation_novelty_draws = draws.max(1);
self
}
pub fn set_mutation_probation_steps(mut self, k: usize) -> Self {
self.cfg.mutation_probation_steps = k;
self
}
pub fn set_crossover_probation_steps(mut self, k: usize) -> Self {
self.cfg.crossover_probation_steps = k;
self
}
pub fn set_pop_init_probation_steps(mut self, k: usize) -> Self {
self.cfg.init_probation_steps = k;
self
}
pub fn set_pop_recovery_retries(mut self, k: usize) -> Self {
self.cfg.recovery_retries = k;
self
}
pub fn set_pop_recovery_tol(mut self, fraction: f32) -> Self {
self.cfg.recovery_tolerance = if fraction.is_finite() {
fraction.max(0.0)
} else {
0.0
};
self
}
pub fn set_pop_recovery_same_seed(mut self, same: bool) -> Self {
self.cfg.recovery_same_seed = same;
self
}
pub fn set_champion_save_topology(mut self, enabled: bool) -> Self {
self.cfg.champion_save_topology = enabled;
self
}
pub fn build(self) -> RaceConfig {
self.cfg
}
}
impl RaceConfig {
pub(crate) 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}'")),
})
}
pub(crate) 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> {
super::pool::resolve::<super::pool::CrossoverOpsPool>(self)
}
pub fn resolved_activation_pool(&self) -> Result<Vec<crate::graph::node::Activation>, String> {
super::pool::resolve::<super::pool::ActivationPool>(self)
}
pub fn resolved_combine_pool(&self) -> Result<Vec<crate::graph::node::CombineOp>, String> {
super::pool::resolve::<super::pool::CombineOpPool>(self)
}
pub fn resolved_standardize_pool(
&self,
) -> Result<Vec<crate::graph::node::StandardizeOp>, String> {
super::pool::resolve::<super::pool::StandardizePool>(self)
}
pub fn validate_search_pools(&self) -> Result<(), String> {
let errors: Vec<String> = [
super::pool::error::<super::pool::CrossoverOpsPool>(self),
super::pool::error::<super::pool::ActivationPool>(self),
super::pool::error::<super::pool::CombineOpPool>(self),
super::pool::error::<super::pool::StandardizePool>(self),
super::pool::error::<super::pool::FeatureSplitPool>(self),
]
.into_iter()
.flatten()
.collect();
if errors.is_empty() {
Ok(())
} else {
Err(errors.join("; "))
}
}
}
#[cfg(test)]
mod tests {
use super::{LogLevel, RaceConfig};
#[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,
"tracing 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 an_unparseable_pool_name_is_an_error_not_a_fallback() {
assert_eq!(RaceConfig::defaults().validate_search_pools(), Ok(()));
let cfg = RaceConfig {
feature_split_pool: vec!["rondom".to_string()],
..RaceConfig::defaults()
};
assert_eq!(cfg.feature_split_pool_names(), vec!["rondom".to_string()]);
let err = cfg
.validate_search_pools()
.expect_err("a typo must not resolve to a pool");
assert!(
err.contains("feature_split_pool") && err.contains("rondom"),
"the error must name the pool and the entry: {err}"
);
let many = RaceConfig {
feature_split_pool: vec!["rondom".to_string()],
activation_pool: vec!["reluu".to_string()],
crossover_ops_pool: vec!["two_point".to_string()],
..RaceConfig::defaults()
};
let err = many.validate_search_pools().expect_err("three typos");
for needle in [
"feature_split_pool",
"activation_pool",
"crossover_ops_pool",
] {
assert!(err.contains(needle), "{needle} missing from: {err}");
}
}
#[test]
fn feature_split_pool_entries_match_whole_names() {
use super::parse_feature_split_pool_entry as parse;
assert_eq!(parse("none"), Some(None));
assert_eq!(parse("off"), Some(None));
assert_eq!(
parse("random"),
Some(Some(super::FeatureSplitPolicy::Random))
);
assert_eq!(
parse("split_random"),
Some(Some(super::FeatureSplitPolicy::Random))
);
assert_eq!(
parse("Exclusive"),
Some(Some(super::FeatureSplitPolicy::Exclusive))
);
assert_eq!(
parse("split_exclusive"),
Some(Some(super::FeatureSplitPolicy::Exclusive))
);
for bad in [
"nonexclusive",
"random_only",
"exclusive_",
"split_inherited",
"",
] {
assert_eq!(parse(bad), None, "{bad:?} is not a pool name");
}
let cfg = RaceConfig {
feature_split_pool: vec!["nonexclusive".to_string()],
..RaceConfig::defaults()
};
assert!(cfg.validate_search_pools().is_err());
}
#[test]
fn the_ranking_score_is_the_centre_with_no_spread_charge() {
let cfg = RaceConfig::defaults();
let ran = crate::engine::smoothing::rolling_stats_with(
&crate::engine::smoothing::RollingBuffer::from_values(
5,
&[40.0, 500.0, 40.0, 40.0, 40.0],
),
cfg.mean_type,
)
.unwrap();
let steady = crate::engine::smoothing::rolling_stats_with(
&crate::engine::smoothing::RollingBuffer::from_values(
5,
&[40.0, 40.0, 40.0, 40.0, 40.0],
),
cfg.mean_type,
)
.unwrap();
assert!(
steady.score() <= ran.score(),
"the calm window is not penalised"
);
assert_eq!(ran.score(), ran.fam_center, "the score IS the centre");
}
#[test]
fn crossover_selection_defaults_to_tournament_two_and_round_trips() {
use crate::evolution::selection::SelectionMethod;
assert_eq!(
RaceConfig::defaults().crossover_selection,
SelectionMethod::Tournament { tournament_size: 2 },
"the knob's default is the reference loop's draw"
);
let cfg = RaceConfig::builder()
.set_crossover_selection(SelectionMethod::Tournament { tournament_size: 3 })
.build();
assert_eq!(
cfg.crossover_selection,
SelectionMethod::Tournament { tournament_size: 3 }
);
assert_eq!(cfg.crossover_selection.to_string(), "tournament(size=3)");
assert_eq!(SelectionMethod::Roulette.to_string(), "roulette");
}
#[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));
}
}
#[cfg(test)]
mod spelling_pins {
use super::*;
use std::str::FromStr;
#[test]
fn mean_type_spellings_hold() {
for (v, s) in [
(MeanType::Arithmetic, "arithmetic"),
(MeanType::SignedLog, "signed_log"),
(MeanType::Median, "median"),
] {
assert_eq!(v.as_str(), s);
assert_eq!(v.to_string(), s);
assert_eq!(MeanType::parse(s), Some(v));
assert_eq!(MeanType::from_str(s).unwrap(), v);
}
assert_eq!(MeanType::parse("average"), Some(MeanType::Arithmetic));
assert_eq!(MeanType::parse("signed-log"), Some(MeanType::SignedLog));
}
#[test]
fn train_ticket_spellings_hold() {
for (v, s) in TrainTicket::ALL.iter().copied().zip(TrainTicket::ALL_NAMES) {
assert_eq!(v.as_str(), s);
assert_eq!(v.to_string(), s);
assert_eq!(TrainTicket::parse(s), Some(v));
assert_eq!(TrainTicket::from_str(s).unwrap(), v);
}
assert_eq!(
TrainTicket::parse("best_fewest"),
Some(TrainTicket::MostFitFewestUpdates)
);
assert_eq!(
TrainTicket::parse("least_fit_least_updates"),
Some(TrainTicket::LeastFitFewestUpdates)
);
}
#[test]
fn cull_policy_spellings_hold() {
for (v, s) in [
(CrossCullPolicy::LowestFitness, "lowest_fitness"),
(CrossCullPolicy::Random, "random"),
(
CrossCullPolicy::HighestTicketChance,
"highest_ticket_chance",
),
(
CrossCullPolicy::LowestFitnessMostUpdates,
"lowest_fitness_most_updates",
),
] {
assert_eq!(v.to_string(), s);
assert_eq!(CrossCullPolicy::from_str(s).unwrap(), v);
assert_eq!(MutationCullPolicy::from_str(s).unwrap().to_string(), s);
}
assert!(
CrossCullPolicy::from_str("nope")
.unwrap_err()
.contains("expected lowest_fitness")
);
}
}