Genetic representation for evolutionary agent selection — define agent traits as genes, crossover two agents to produce offspring, mutate populations, and measure diversity.
- Gene representation: named genes with value clamping and normalization to [0, 1]
- Single-point crossover: combine two agent DNA sequences into two children
- Configurable mutation: mutate genes by a delta with probability control
- Population diversity: average pairwise Euclidean distance across a population
- Random DNA generation: seed initial populations with random gene values
use agent_dna::{AgentDNA, Gene};
// Define two agents with genetic traits
let dna_a = AgentDNA::new(vec![
Gene::new(0, 0.5, 0.0, 1.0), // gene 0: value 0.5, range [0, 1]
Gene::new(1, 0.8, 0.0, 1.0),
Gene::new(2, 0.3, 0.0, 1.0),
]);
let dna_b = AgentDNA::new(vec![
Gene::new(0, 0.2, 0.0, 1.0),
Gene::new(1, 0.6, 0.0, 1.0),
Gene::new(2, 0.9, 0.0, 1.0),
]);
// Crossover: produce two children
let (child_a, child_b) = dna_a.crossover(&dna_b);
// Mutation: 10% chance per gene, ±0.2 magnitude
let mutated = dna_a.mutate(0.1, 0.2, &mut rng);
// Measure population diversity (higher = more diverse)
let diversity = AgentDNA::diversity(&population);pub struct Gene {
pub name: u32,
pub value: f64,
pub min: f64,
pub max: f64,
}
impl Gene {
pub fn new(name: u32, value: f64, min: f64, max: f64) -> Self;
pub fn normalize(&self) -> f64; // value mapped to [0, 1]
pub fn mutate(&self, delta: f64) -> Self; // shift by delta, clamped
}pub struct AgentDNA {
pub genes: Vec<Gene>,
pub fitness: f64,
pub generation: u32,
}
impl AgentDNA {
pub fn new(genes: Vec<Gene>) -> Self;
pub fn random(n_genes: u32, min: f64, max: f64, rng: &mut impl FnMut() -> f64) -> Self;
pub fn crossover(&self, other: &AgentDNA) -> (AgentDNA, AgentDNA);
pub fn mutate(&self, prob: f64, magnitude: f64, rng: &mut impl FnMut() -> f64) -> Self;
pub fn distance(&self, other: &AgentDNA) -> f64; // Euclidean
pub fn diversity(population: &[AgentDNA]) -> f64; // avg pairwise distance
pub fn distance_to(&self, other: &AgentDNA) -> f64;
}Part of the SuperInstance OpenConstruct ecosystem. Works with:
- agent-shadow-rs — evolve agents by comparing shadow traces as fitness signals
- agent-manifest-rs — DNA traits can map to capability levels
- bid-engine-rs — genetic strategies for auction bidding
7 tests covering gene creation/clamping, crossover, mutation, random generation, diversity measurement, and distance calculations.
# Cargo.toml
[dependencies]
agent-dna = { git = "https://github.com/SuperInstance/agent-dna-rs" }Requires Rust 2021 edition. No external dependencies.