use flodl::nn::Optimizer;
use flodl::tensor::Result;
use tracing::debug;
pub(crate) const MAX_PARENT_PAIRINGS: usize = 3;
use crate::graph::network::Network;
use crate::graph::node::NodeKind;
use crate::graph::topology::Topology;
use crate::state::{NetMetrics, NetState, write_net_state};
use crate::utils::seed::derive_seed;
use super::core::CoreEngine;
use super::smoothing::rolling_mean;
pub struct RaceChild {
pub state: NetState,
pub net: Network,
pub optimizer: Box<dyn Optimizer>,
}
impl CoreEngine {
pub(crate) fn generate_child(
&mut self,
clock: usize,
child_idx: usize,
) -> Result<Option<RaceChild>> {
let roll_seed = derive_seed(self.header.run_seed, clock * 1024 + child_idx);
let mut rng = fastrand::Rng::with_seed(roll_seed);
let try_crossover = rng.f32() < self.config.crossover_prob;
if try_crossover {
match self.evolved_child(clock, child_idx, &mut rng) {
Ok(None) => Ok(None),
other => other,
}
} else {
Ok(None) }
}
fn evolved_child(
&mut self,
clock: usize,
child_idx: usize,
rng: &mut fastrand::Rng,
) -> Result<Option<RaceChild>> {
let hashes = self.state.live_hashes();
if hashes.is_empty() {
return Err(flodl::tensor::TensorError::new(
"race: no live nets to select parents from",
));
}
let direction = self.fitness.direction();
let mut ranked: Vec<(String, f32)> = hashes
.iter()
.map(|h| {
(
h.clone(),
rolling_mean(self.rolling_fitness.get(h).unwrap()),
)
})
.collect();
ranked.sort_by(|a, b| direction.cmp(b.1, a.1));
let scores: Vec<f32> = ranked.iter().map(|(_, s)| *s).collect();
let mut child_topo = None;
let mut cx_note = String::new();
let ops = self
.config
.resolved_crossover_ops()
.map_err(|e| flodl::tensor::TensorError::new(&e))?;
let op = ops[rng.usize(..ops.len())];
let n_parents = op.required_parents();
let mut parent_hashes: Vec<String> = Vec::new();
for _ in 0..MAX_PARENT_PAIRINGS {
let mut parent_positions: Vec<usize> = (0..n_parents)
.filter_map(|_| {
crate::evolution::selection::SelectionMethod::Roulette
.apply(&scores, direction, rng, 0)
.into_iter()
.next()
})
.collect();
if n_parents == 2 && parent_positions.len() == 2 && parent_positions[0] == parent_positions[1] {
let others: Vec<usize> =
(0..scores.len()).filter(|&p| p != parent_positions[0]).collect();
if !others.is_empty() {
parent_positions[1] = others[rng.usize(..others.len())];
}
}
let attempt_parents: Vec<String> = parent_positions
.iter()
.map(|&p| ranked[p].0.clone())
.collect();
let mut pa = self.load_parent_topology(&attempt_parents[0])?;
if n_parents == 2 {
let mut pb = self.load_parent_topology(&attempt_parents[1])?;
let cx = match op {
crate::engine::config::CrossoverOp::OnePoint => {
Topology::cx_one_point(&mut pa, &mut pb, rng)
}
crate::engine::config::CrossoverOp::Uniform => {
const UNIFORM_SWAP_PROB: f32 = 0.5;
Topology::cx_uniform(&mut pa, &mut pb, UNIFORM_SWAP_PROB, rng)
}
};
if cx {
child_topo = Some(pa);
cx_note = match op {
crate::engine::config::CrossoverOp::OnePoint => "crossover-one-point",
crate::engine::config::CrossoverOp::Uniform => "crossover-uniform",
}
.to_string();
parent_hashes = attempt_parents;
break;
}
} else {
child_topo = Some(pa);
cx_note = "clone".to_string();
parent_hashes = attempt_parents;
break;
}
}
let mut child_topo = match child_topo {
Some(t) => t,
None => {
return Ok(None);
}
};
child_topo.assign_port_activations(
&self
.config
.resolved_activation_pool()
.unwrap_or_else(|_| crate::evolution::pools::all_activations()),
);
Ok(Some(self.finalize_child(
child_topo,
clock,
child_idx,
&cx_note,
&parent_hashes,
false,
)?))
}
fn load_parent_topology(&self, hash: &str) -> Result<Topology> {
match self.state.net(hash) {
Some(state) => state.topology(),
None => Err(flodl::tensor::TensorError::new(&format!(
"race: parent {hash} not live"
))),
}
}
pub(crate) fn random_child(&mut self, clock: usize, child_idx: usize) -> Result<RaceChild> {
let roll_seed = derive_seed(self.header.run_seed, clock * 1024 + child_idx);
let mut rng = fastrand::Rng::with_seed(roll_seed);
let topo_opts = self.config.topology_options;
let n_hidden = rng.usize(topo_opts.min_hidden_num_nodes..=topo_opts.max_hidden_num_nodes);
let mut opts = topo_opts;
opts.input_dim = Some(self.header.input_dim);
opts.output_dim = Some(self.header.output_dim);
let topo_seed = derive_seed(self.header.run_seed, clock * 1024 + child_idx) as usize;
let mut graph = Topology::new(topo_seed, Some(opts));
graph.create_random_hidden_nodes(n_hidden);
let activation = self
.config
.resolved_activation_pool()
.unwrap_or_else(|_| crate::evolution::pools::all_activations());
let combine = self
.config
.resolved_combine_pool()
.unwrap_or_else(|_| crate::evolution::pools::all_combine_ops());
let standardize = self
.config
.resolved_standardize_pool()
.unwrap_or_else(|_| crate::evolution::pools::all_standardize_ops());
let hidden_dim_pool = self.config.hidden_dim_pool.clone().unwrap_or(4..=8);
let stride = self.config.hidden_dim_stride.max(1);
for node in &mut graph.nodes {
if node.kind == NodeKind::Hidden {
let n = ((hidden_dim_pool.end() - hidden_dim_pool.start()) / stride) + 1;
node.hidden_dim = Some(hidden_dim_pool.start() + rng.usize(0..n) * stride);
node.activation = activation[rng.usize(0..activation.len())];
node.combine_op = Some(combine[rng.usize(0..combine.len())]);
node.standardize = Some(standardize[rng.usize(0..standardize.len())]);
}
}
graph.refresh_labels();
graph.finalize();
let act_pool = self
.config
.resolved_activation_pool()
.unwrap_or_else(|_| crate::evolution::pools::all_activations());
graph.assign_port_activations(&act_pool);
self.finalize_child(graph, clock, child_idx, "random", &[], false)
}
fn finalize_child(
&self,
mut topo: Topology,
clock: usize,
child_idx: usize,
origin: &str,
parents: &[String],
mutated: bool,
) -> Result<RaceChild> {
let new_seed = derive_seed(self.header.run_seed, clock * 1024 + child_idx) as usize;
topo.options.topology_seed = new_seed;
let lineage = if parents.is_empty() {
origin.to_string()
} else {
format!("{origin}:parents={}", parents.join(","))
};
let lineage = if mutated {
format!("{lineage}+mut")
} else {
lineage
};
let mut state = NetState::new(&topo, 0, Some(lineage))?;
state.entered_at_step = clock;
let _net_device = self.config.device();
let net = Network::build(&topo, self.config.device())?;
state.stamp_meta(&net, &self.meta_ctx);
let optimizer = self.trainer.make_optimizer(&net);
Ok(RaceChild {
state,
net,
optimizer,
})
}
pub(crate) fn catch_up(&mut self, child: &mut RaceChild, clock: usize) -> Result<()> {
self.catch_up_range(child, 0, clock)
}
pub(crate) fn catch_up_range_provisional(
&mut self,
child: &mut RaceChild,
from: usize,
to: usize,
) -> Result<()> {
for step in from..to {
self.catch_up_step(child, step)?;
}
Ok(())
}
pub(crate) fn catch_up_range(
&mut self,
child: &mut RaceChild,
from: usize,
to: usize,
) -> Result<()> {
for step in from..to {
self.catch_up_step(child, step)?;
}
write_net_state(&self.run_dir, &child.state)?;
Ok(())
}
fn catch_up_step(&mut self, child: &mut RaceChild, step: usize) -> Result<()> {
let run_data = self
.dataset
.as_ref()
.zip(self.stream.as_ref())
.map(|(dataset, stream)| crate::trainer::RunData { dataset, stream });
let env = crate::trainer::StepEnv {
step,
run_seed: self.header.run_seed,
pop_size: self.config.pop_size,
live_count: self.state.live_count(),
checkpoint_every: self.config.checkpoint_every,
smoothing_window: self.config.smoothing_window,
max_steps: self.config.max_steps,
};
crate::utils::race_steps::seed_step_randomness(child.state.net_seed as u64, step as u64, 0);
let frozen_step = child.state.is_frozen_step(step);
let challenged = crate::engine::config::challenge_fires(
self.header.run_seed,
child.state.net_seed as u64,
step,
self.config.challenge_prob,
self.config.max_steps,
);
let challenge_prob = crate::engine::config::effective_challenge_prob(
self.config.challenge_prob,
step,
self.config.max_steps,
);
let mut noop = crate::engine::core::NoopOptimizer;
let optimizer: &mut dyn flodl::nn::optim::Optimizer = if frozen_step {
&mut noop
} else {
&mut *child.optimizer
};
let report = self.trainer.train_step(
&mut child.net,
optimizer,
step,
run_data.as_ref(),
&self.fitness,
&self.metrics,
env,
&child.state.hash,
child.state.net_seed as u64,
challenged,
challenge_prob,
)?;
let metrics = NetMetrics {
step,
train_loss: report.train_loss,
eval_loss: report.eval_loss,
fitness: report.fitness,
informative: report.informative,
frozen: frozen_step,
};
child.state.record_metrics(metrics.clone());
child.state.advance_step();
if let Some(buf) = self.rolling_fitness.get_mut(&child.state.hash) {
buf.push(metrics.fitness);
}
tracing::debug!(
"catch-up step {}: net {} train_loss↓ {:.4} eval_loss↓ {:?} fitness{} {:.4}",
step,
child.state.hash,
metrics.train_loss,
metrics.eval_loss,
self.fitness.direction().arrow(),
metrics.fitness,
);
Ok(())
}
pub(crate) fn replay_loaded_net(&mut self, state: NetState) -> Result<RaceChild> {
let clock = state.step;
let recorded = state.last_metrics.clone();
let segments = replay_segments(&state);
let mut child = self.build_child_from_state(state)?;
for (from, to) in segments {
self.catch_up_range(&mut child, from, to)?;
}
if let (Some(recorded), Some(replayed)) = (recorded, child.state.last_metrics.clone()) {
if recorded != replayed {
return Err(flodl::tensor::TensorError::new(&format!(
"race resume parity failure: net {} at step {} — recorded \
train_loss={:?} eval_loss={:?} fitness={:.6}, replayed \
train_loss={:?} eval_loss={:?} fitness={:.6} — \
reconstruction is not bit-identical (seed/primitive/config drift)",
child.state.hash,
clock,
recorded.train_loss,
recorded.eval_loss,
recorded.fitness,
replayed.train_loss,
replayed.eval_loss,
replayed.fitness,
)));
}
debug!(
"parity ok: net {} replayed to step {} bit-identical",
child.state.hash, clock,
);
}
Ok(child)
}
fn build_child_from_state(&self, state: NetState) -> Result<RaceChild> {
let topo = state.topology()?;
let net = Network::build(&topo, self.config.device())?;
let optimizer = self.trainer.make_optimizer(&net);
let rebuilt = NetState {
step: 0,
is_alive: true,
last_metrics: None,
culled_at_step: None,
cull_reason: None,
final_smoothed_fitness: None,
..state
};
Ok(RaceChild {
state: rebuilt,
net,
optimizer,
})
}
}
fn replay_segments(state: &NetState) -> Vec<(usize, usize)> {
let trainings = state.step;
if trainings == 0 {
return Vec::new(); }
let Some(end) = state.last_metrics.as_ref().map(|m| m.step) else {
return vec![(0, trainings)];
};
let skipped = (end + 1).saturating_sub(trainings);
if skipped == 0 {
return vec![(0, end + 1)];
}
let birth = state.entered_at_step;
let mut out = Vec::new();
if birth > 0 {
out.push((0, birth.min(end + 1)));
}
let after = birth + 1;
if after <= end {
out.push((after, end + 1));
}
out
}