Interface Fitness<T extends Comparable<T>>
- Type Parameters:
T
- the type of the fitness value, must be comparable for selection operations
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
The fitness function is a crucial component of evolutionary algorithms as it determines the quality or performance of individual solutions. It maps a genotype to a fitness value that can be used for selection, ranking, and determining evolutionary progress.
Implementations should be:
- Deterministic: The same genotype should always produce the same fitness value
- Thread-safe: May be called concurrently from multiple threads
- Fast: Called frequently during evolution, performance matters
Common fitness function patterns:
- Minimization: Lower values indicate better solutions (errors, costs)
- Maximization: Higher values indicate better solutions (profits, accuracy)
- Multi-objective: Use FitnessVector from the MOO module for multiple objectives
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionComputes the fitness value for the specified genotype.static <U,
V extends Comparable<V>>
Fitness<V> forChromosome
(int chromosomeIndex, Function<U, V> chromosomeFitness) Creates a fitness function that evaluates a specific chromosome within a genotype.
-
Method Details
-
compute
Computes the fitness value for the specified genotype.This method should evaluate how well the genotype solves the problem and return a comparable fitness value. The interpretation of "better" depends on whether the optimization is for minimization or maximization.
- Parameters:
genotype
- the genotype to evaluate- Returns:
- the fitness value representing the quality of the genotype
- Throws:
RuntimeException
- if evaluation fails due to invalid genotype or computation error
-
forChromosome
static <U,V extends Comparable<V>> Fitness<V> forChromosome(int chromosomeIndex, Function<U, V> chromosomeFitness) Creates a fitness function that evaluates a specific chromosome within a genotype.This utility method simplifies fitness evaluation when the fitness depends on only one chromosome from the genotype. It extracts the chromosome at the specified index and applies the provided fitness function to it.
This is particularly useful for:
- Single-chromosome problems where genotype contains only one chromosome
- Multi-chromosome problems where fitness depends on one specific chromosome
- Compositional fitness functions that evaluate chromosomes independently
- Type Parameters:
U
- the type of the chromosome to evaluateV
- the type of the fitness value, must be comparable- Parameters:
chromosomeIndex
- the zero-based index of the chromosome to evaluate within the genotypechromosomeFitness
- the function that computes fitness for the extracted chromosome- Returns:
- a fitness function that evaluates the specified chromosome
- Throws:
IllegalArgumentException
- if chromosomeIndex is negativeNullPointerException
- if chromosomeFitness is nullClassCastException
- at runtime if the chromosome at the specified index cannot be cast to type UIllegalArgumentException
- at runtime if chromosomeIndex exceeds the genotype size
-