Class EASystem<T extends Comparable<T>>

java.lang.Object
net.bmahe.genetics4j.core.EASystem<T>
Type Parameters:
T - the type of fitness values, must be comparable for selection and ranking

public class EASystem<T extends Comparable<T>> extends Object
Main orchestrator class for evolutionary algorithms, managing the complete evolution process.

EASystem serves as the central coordinator that brings together all components of an evolutionary algorithm including genetic operators, selection strategies, evaluation functions, and termination criteria. It manages the evolutionary cycle and provides a unified interface for running optimizations.

The system coordinates the following evolutionary process:

  1. Initialization: Generate initial population of random genotypes
  2. Evaluation: Compute fitness values for all individuals
  3. Selection: Choose parents for reproduction based on fitness
  4. Reproduction: Create offspring through crossover and mutation
  5. Replacement: Integrate offspring into next generation
  6. Termination check: Determine if stopping criteria are met
  7. Iteration: Repeat until termination conditions are satisfied

Key responsibilities include:

  • Population management: Maintaining population size and diversity
  • Genetic operator coordination: Applying crossover, mutation, and selection
  • Fitness evaluation orchestration: Managing parallel and synchronous evaluation
  • Evolution monitoring: Providing hooks for logging and progress tracking
  • Resource management: Efficient memory usage and computation distribution

Configuration components:

  • EAConfiguration: Defines genetic representation and operator policies
  • EAExecutionContext: Provides runtime services and factory implementations
  • FitnessEvaluator: Computes quality measures for candidate solutions
  • Termination criteria: Determines when to stop evolution

The system supports various evolutionary paradigms:

  • Genetic Algorithms: Traditional binary and real-valued optimization
  • Genetic Programming: Evolution of tree-structured programs
  • Evolution Strategies: Real-valued optimization with adaptive parameters
  • Multi-objective optimization: Pareto-based optimization with multiple objectives

Example usage:


 // Configure the evolutionary algorithm
 EAConfiguration<Double> config = EAConfigurationBuilder.<Double>builder()
     .chromosomeSpecs(DoubleChromosomeSpec.of(10, -5.0, 5.0))
     .parentSelectionPolicy(Tournament.of(3))
     .mutationPolicy(RandomMutation.of(0.1))
     .build();
 
 // Set up execution context
 EAExecutionContext<Double> context = EAExecutionContexts.forScalarFitness();
 
 // Create fitness evaluator
 Fitness<Double> fitness = genotype -> {
     // Implement problem-specific fitness function
     return computeFitness(genotype);
 };
 
 // Build and run the evolutionary system
 EASystem<Double> system = EASystemFactory.from(config, context, fitness);
 EvolutionResult<Double> result = system.evolve(
     populationSize: 100,
     termination: Terminations.generations(1000)
 );
 

Thread safety: EASystem instances are generally not thread-safe and should not be shared between multiple threads without external synchronization. However, the system supports parallel fitness evaluation internally when configured appropriately.

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • evaluate

      private List<T> evaluate(long generation, List<Genotype> population)
    • initializePopulation

      private List<Genotype> initializePopulation()
    • mutateGenotypes

      private List<Genotype> mutateGenotypes(List<Genotype> genotypes)
    • combineParents

      private List<Genotype> combineParents(Population<T> parents, GenotypeCombinator genotypeCombinator)
    • createBasicOffsprings

      private List<Genotype> createBasicOffsprings(Population<T> population, int offspringsNeeded)
      Create offsprings without mutation
      Parameters:
      population -
      offspringsNeeded -
      Returns:
    • getEAConfiguration

      public AbstractEAConfiguration<T> getEAConfiguration()
      Gets the evolutionary algorithm configuration used by this system.
      Returns:
      the EA configuration containing genetic operators, chromosome specs, and evolutionary parameters
    • getPopulationSize

      public long getPopulationSize()
      Gets the target population size for each generation.
      Returns:
      the population size as configured for this evolutionary system
    • createOffsprings

      public List<Genotype> createOffsprings(Population<T> population, int offspringsNeeded)
      Creates offspring from the given population through selection, crossover, and mutation.

      This method orchestrates the reproductive process by:

      1. Selecting parents from the population using the configured selection strategy
      2. Applying crossover operations to generate basic offspring
      3. Applying mutation operators to introduce genetic variation
      Parameters:
      population - the current population to select parents from
      offspringsNeeded - the number of offspring to generate
      Returns:
      a list of mutated offspring genotypes
      Throws:
      NullPointerException - if population is null
      IllegalArgumentException - if offspringsNeeded is not positive
    • evolve

      public EvolutionResult<T> evolve()
      Executes the complete evolutionary algorithm process until termination criteria are met.

      This is the main entry point for running evolution. The method manages the entire evolutionary cycle including:

      • Initial population generation and evaluation
      • Iterative evolution through selection, reproduction, and replacement
      • Progress monitoring via evolution listeners
      • Termination condition checking

      The evolution process continues until the configured termination criteria (e.g., maximum generations, target fitness, convergence) are satisfied.

      Returns:
      an EvolutionResult containing the final population, fitness values, generation count, and configuration details
      See Also:
    • evaluateOnce

      public List<T> evaluateOnce(long generation, List<Genotype> genotypes)
      Evaluates a collection of genotypes once without running the full evolutionary process.

      This method provides a way to evaluate genotypes using the configured fitness evaluator without executing the complete evolutionary algorithm. It handles the pre/post evaluation lifecycle and notifies evolution listeners of the results.

      Useful for:

      • Testing fitness functions with specific genotypes
      • Evaluating externally generated populations
      • Batch evaluation scenarios
      Parameters:
      generation - the generation number for logging and listener notification
      genotypes - the list of genotypes to evaluate
      Returns:
      a list of fitness values corresponding to the input genotypes
      Throws:
      IllegalArgumentException - if generation is negative or genotypes is empty
      NullPointerException - if genotypes is null