Crate moors

Source
Expand description

§moors

Multi‑Objective Optimization in Pure Rust
Fast, extensible evolutionary algorithms with first‑class ndarray support.

§Overview

moors provides a battery of evolutionary algorithms for solving multi‑objective optimization problems. The core goals are:

  • Performance – minimal allocations, Rayon‑powered parallel loops where it matters.
  • Extensibility – every operator (sampling, crossover, mutation, selection, survival) is pluggable via pure Rust traits.

Currently implemented algorithms

FamilyAlgorithms
NSGANSGA‑II, NSGA‑III, RNSGA‑II
SPEASPEA‑2
OthersAGE‑MOEA, REVEA (WIP)

§Quick start

use ndarray::{Array1, Array2, Axis, stack};

use moors::{
    algorithms::{AlgorithmError, Nsga2Builder},
    duplicates::ExactDuplicatesCleaner,
    operators::{SinglePointBinaryCrossover, BitFlipMutation, RandomSamplingBinary},
};

// ----- problem data (0/1 knapsack) -------------------------------------
const WEIGHTS: [f64; 5] = [12.0, 2.0, 1.0, 4.0, 10.0];
const VALUES:  [f64; 5] = [ 4.0, 2.0, 1.0, 5.0,  3.0];
const CAPACITY: f64 = 15.0;

/// Multi‑objective fitness ⇒ [−total_value, total_weight]
fn fitness(pop_genes: &Array2<f64>) -> Array2<f64> {
    let w = Array1::from_vec(WEIGHTS.into());
    let v = Array1::from_vec(VALUES.into());
    let total_v = pop_genes.dot(&v);
    let total_w = pop_genes.dot(&w);
    stack(Axis(1), &[(-&total_v).view(), total_w.view()]).unwrap()
}

/// Single inequality constraint ⇒ total_weight − CAPACITY ≤ 0
fn constraints(pop_genes: &Array2<f64>) -> Array1<f64> {
    let w = Array1::from_vec(WEIGHTS.into());
    pop_genes.dot(&w) - CAPACITY
}

fn main() -> Result<(), AlgorithmError> {
    let mut algo = Nsga2Builder::default()
        .fitness_fn(fitness)
        .constraints_fn(constraints)
        .sampler(RandomSamplingBinary::new())
        .crossover(SinglePointBinaryCrossover::new())
        .mutation(BitFlipMutation::new(0.5))
        .duplicates_cleaner(ExactDuplicatesCleaner::new())
        .num_vars(5)
        .population_size(100)
        .crossover_rate(0.9)
        .mutation_rate(0.1)
        .num_offsprings(32)
        .num_iterations(200)
        .build()?;

    algo.run()?;
    println!("Final population size: {}", algo.population()?.len());
    Ok(())
}

§Module layout

  • algorithms – high‑level algorithm builders
  • operators – sampling, crossover, mutation, selection, survival
  • genetic – core data types (Individual, Population, …)
  • evaluator – fitness + constraints evaluation pipeline
  • random – pluggable RNG abstraction
  • duplicates – duplicate‑handling strategies

Re-exports§

pub use algorithms::AgeMoea;
pub use algorithms::AgeMoeaBuilder;
pub use algorithms::AlgorithmBuilder;
pub use algorithms::AlgorithmBuilderError;
pub use algorithms::AlgorithmError;
pub use algorithms::GeneticAlgorithm;
pub use algorithms::InitializationError;
pub use algorithms::Nsga2;
pub use algorithms::Nsga2Builder;
pub use algorithms::Nsga3;
pub use algorithms::Nsga3Builder;
pub use algorithms::Revea;
pub use algorithms::ReveaBuilder;
pub use algorithms::Rnsga2;
pub use algorithms::Rnsga2Builder;
pub use algorithms::Spea2;
pub use algorithms::Spea2Builder;
pub use duplicates::CloseDuplicatesCleaner;
pub use duplicates::ExactDuplicatesCleaner;
pub use duplicates::NoDuplicatesCleaner;
pub use duplicates::PopulationCleaner;
pub use evaluator::ConstraintsFn;
pub use evaluator::EvaluatorError;
pub use evaluator::FitnessFn;
pub use evaluator::NoConstraints;
pub use genetic::Individual;
pub use genetic::IndividualMOO;
pub use genetic::IndividualSOO;
pub use genetic::Population;
pub use genetic::PopulationMOO;
pub use genetic::PopulationSOO;
pub use operators::selection;
pub use operators::survival;
pub use operators::AgeMoeaSurvival;
pub use operators::ArithmeticCrossover;
pub use operators::BitFlipMutation;
pub use operators::CrossoverOperator;
pub use operators::DanAndDenisReferencePoints;
pub use operators::DisplacementMutation;
pub use operators::ExponentialCrossover;
pub use operators::FrontsAndRankingBasedSurvival;
pub use operators::GaussianMutation;
pub use operators::InversionMutation;
pub use operators::MutationOperator;
pub use operators::Nsga2RankCrowdingSurvival;
pub use operators::Nsga3ReferencePoints;
pub use operators::Nsga3ReferencePointsSurvival;
pub use operators::OrderCrossover;
pub use operators::PermutationSampling;
pub use operators::RandomSamplingBinary;
pub use operators::RandomSamplingFloat;
pub use operators::RandomSamplingInt;
pub use operators::RandomSelectionMOO;
pub use operators::RankAndScoringSelectionMOO;
pub use operators::ReveaReferencePointsSurvival;
pub use operators::Rnsga2ReferencePointsSurvival;
pub use operators::SamplingOperator;
pub use operators::ScrambleMutation;
pub use operators::SelectionOperator;
pub use operators::SimulatedBinaryCrossover;
pub use operators::SinglePointBinaryCrossover;
pub use operators::Spea2KnnSurvival;
pub use operators::StructuredReferencePoints;
pub use operators::SurvivalOperator;
pub use operators::SwapMutation;
pub use operators::TwoPointBinaryCrossover;
pub use operators::UniformBinaryCrossover;
pub use operators::UniformBinaryMutation;
pub use operators::UniformRealMutation;
pub use operators::evolve::EvolveError;
pub use random::MOORandomGenerator;
pub use random::NoopRandomGenerator;
pub use random::RandomGenerator;
pub use random::TestDummyRng;

Modules§

algorithms
duplicates
duplicates – Keeping the Gene Pool Diverse
evaluator
evaluator – From Genes to Population
genetic
genetic – Core Data Structures
non_dominated_sorting
operators
operators – Building Blocks for Evolution
random
random – Unified RNG Abstraction

Macros§

__constraints_helper
Concatenate one or more already-wrapped constraint closures (inequalities or equalities) into a single evaluator closure returning an Array2.
__eq_helper
Wrap a single constraint function as an equality (|g(genes)| - ε ≤ 0, ε = 1e-6).
create_algorithm
fitness_fn
Build a composite fitness-evaluation closure from one or more scalar / vector fitness functions.
impl_constraints_fn
Defines a struct and implements [moors::ConstraintsFn] for it.

Functions§

cross_euclidean_distances
Computes the cross squared Euclidean distance matrix between data and reference using matrix algebra.