| Crates.io | primordial-opt |
| lib.rs | primordial-opt |
| version | 0.1.4 |
| created_at | 2025-12-13 18:41:46.739518+00 |
| updated_at | 2025-12-13 19:42:18.935856+00 |
| description | Genetic algorithm library with composable selection, crossover, and mutation operators |
| homepage | |
| repository | https://github.com/S4H/primordial-opt |
| max_upload_size | |
| id | 1983282 |
| size | 124,390 |
A genetic algorithm library for Rust, providing composable building blocks for evolutionary optimization.
rank, tournament, roulette, and nsga2 (multi-objective)single_pointbit_flip, fill_randomAll selection methods return iterators, enabling flexible composition with .take(n).
[dependencies]
primordial-opt = "0.1"
use primordial_opt::{Chromosome, selection, crossover, mutation};
use rand::SeedableRng;
let mut rng = rand::rngs::StdRng::seed_from_u64(42);
// Create and evaluate population
let mut population: Vec<Chromosome> = (0..100)
.map(|_| Chromosome::new(64, &mut rng))
.collect();
for c in &mut population {
let fitness = evaluate(c); // your fitness function
c.set_fitness(vec![fitness]);
}
// Select top 50 by rank
let parents: Vec<_> = selection::rank(&population).take(50).collect();
// Crossover
let (mut child1, mut child2) = crossover::single_point(&parents[0], &parents[1], &mut rng);
// Mutate
mutation::bit_flip(&mut child1, 0.01, &mut rng);
Use NSGA-II for Pareto-based selection:
// Set multi-objective fitness
chromosome.set_fitness(vec![objective1, objective2]);
// NSGA-II sorts by Pareto front, then crowding distance
let selected: Vec<_> = selection::nsga2(&population).take(50).collect();
cargo run --example rastrigin - Single-objective optimizationcargo run --example zdt1 - Multi-objective optimization with NSGA-IIMIT OR Apache-2.0