Problem

Trait Problem 

Source
pub trait Problem<C: Chromosome, T>: Send + Sync {
    // Required methods
    fn encode(&self) -> Genotype<C>;
    fn decode(&self, genotype: &Genotype<C>) -> T;
    fn eval(&self, individual: &Genotype<C>) -> Score;

    // Provided method
    fn eval_batch(&self, individuals: &[Genotype<C>]) -> Vec<Score> { ... }
}
Expand description

The core interface for genetic algorithm problems.

The Problem trait encapsulates the three essential components of a genetic algorithm: encoding, decoding, and fitness evaluation. It provides a unified interface that allows the engine to work with any problem implementation without needing to understand the specific details of how genotypes are represented or how fitness is calculated.

§Generic Parameters

  • C: The chromosome type that represents the genetic material
  • T: The phenotype type that represents the decoded individual

§Thread Safety

All problems must be Send + Sync to support parallel execution across multiple threads. This ensures safe sharing of problems between worker threads during fitness evaluation.

§Performance Considerations

  • Encoding: Called sparingly, so performance is less critical
  • Decoding: Called frequently during evaluation, should be optimized
  • Fitness Evaluation: Debatably the most computationally expensive operation across all problem types

Required Methods§

Source

fn encode(&self) -> Genotype<C>

Creates a new Genotype representing the initial state of the problem. The returned Genotype should represent a valid starting point for evolution.

§Returns

A Genotype that can be used as a starting point for evolution

§Note

The encoding should produce diverse genotypes to ensure the initial population has sufficient genetic diversity for effective evolution.

Source

fn decode(&self, genotype: &Genotype<C>) -> T

Converts a Genotype into its corresponding phenotype.

This method transforms the genetic representation into a form that can be evaluated by the fitness function. The decoding process should be deterministic and efficient, as it’s called frequently during fitness evaluation.

§Arguments
§Returns

The decoded phenotype that can be evaluated

§Performance

This method is called for every individual during fitness evaluation, so it should be optimized for speed.

Source

fn eval(&self, individual: &Genotype<C>) -> Score

Evaluates the fitness of a single individual.

This method computes the fitness score for a given Genotype by first decoding it to a phenotype and then applying the fitness function. The fitness score indicates how well the individual solves the problem.

§Arguments
§Returns

A fitness score representing the quality of the individual

Provided Methods§

Source

fn eval_batch(&self, individuals: &[Genotype<C>]) -> Vec<Score>

Evaluates the fitness of multiple individuals in a batch.

This method provides an efficient way to evaluate multiple individuals at once, which can be more efficient than calling eval multiple times, especially when the fitness function can benefit from vectorization, when there are significant overhead costs per evaluation, or when you need access to parts, or the whole, of the population to make informed decisions.

§Arguments
  • individuals - A slice of genotypes to evaluate
§Returns

A vector of fitness scores, one for each individual

§Default Implementation

The default implementation simply calls eval for each individual. Override this method if you can provide a more efficient batch evaluation strategy.

§Note

The order in which the scores are returned must match the order in which the genotypes are provided.

Implementors§

Source§

impl<C: Chromosome, T> Problem<C, T> for BatchEngineProblem<C, T>

Implementation of Problem for BatchEngineProblem.

This implementation provides both individual and batch evaluation methods. The individual evaluation method (eval) is implemented by wrapping the phenotype in a single-element slice and calling the batch function, then extracting the first result. This ensures consistency between individual and batch evaluation while maintaining the benefits of batch processing.

Source§

impl<C: Chromosome, T> Problem<C, T> for EngineProblem<C, T>

Implementation of Problem for EngineProblem.

This implementation delegates to the contained codec and fitness function, providing a clean separation of concerns between encoding/decoding logic and fitness evaluation.