primordial-opt

Crates.ioprimordial-opt
lib.rsprimordial-opt
version0.1.4
created_at2025-12-13 18:41:46.739518+00
updated_at2025-12-13 19:42:18.935856+00
descriptionGenetic algorithm library with composable selection, crossover, and mutation operators
homepage
repositoryhttps://github.com/S4H/primordial-opt
max_upload_size
id1983282
size124,390
Frankie (S4H)

documentation

README

primordial-opt

Crates.io Documentation CI License

A genetic algorithm library for Rust, providing composable building blocks for evolutionary optimization.

Features

  • Chromosome: Binary representation with fitness caching
  • Selection: rank, tournament, roulette, and nsga2 (multi-objective)
  • Crossover: single_point
  • Mutation: bit_flip, fill_random

All selection methods return iterators, enabling flexible composition with .take(n).

Installation

[dependencies]
primordial-opt = "0.1"

Quick Example

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);

Multi-Objective Optimization

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();

Examples

  • cargo run --example rastrigin - Single-objective optimization
  • cargo run --example zdt1 - Multi-objective optimization with NSGA-II

License

MIT OR Apache-2.0

Commit count: 0

cargo fmt