Interface EvolutionListener<T>

Type Parameters:
T - the type of fitness values being used in the evolutionary algorithm
All Known Implementing Classes:
CSVEvolutionListener, EvolutionListenerLogTopN, ImmutableCSVEvolutionListener, SimpleEvolutionListener
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface EvolutionListener<T>
Functional interface for monitoring and responding to evolution progress during genetic algorithm execution.

EvolutionListener provides a callback mechanism that allows external components to observe the evolutionary process as it unfolds. Listeners are notified after each generation with complete information about the current population state, enabling real-time monitoring, logging, and analysis.

Evolution listeners are commonly used for:

  • Progress monitoring: Track fitness improvements and convergence trends
  • Data logging: Record population statistics and best solutions
  • Visualization: Update real-time charts and graphs of evolution progress
  • Adaptive control: Modify algorithm parameters based on evolution state
  • Early stopping: Implement custom termination conditions
  • Checkpointing: Save intermediate results for recovery and analysis

Listeners receive comprehensive information about each generation:

  • Generation number: Current iteration count (0-based)
  • Population snapshot: All genotypes in the current generation
  • Fitness values: Corresponding fitness scores for each individual
  • Completion status: Whether termination criteria have been met

Implementation considerations:

  • Performance impact: Listeners are called frequently; keep implementations efficient
  • Thread safety: May be called from different threads in parallel execution contexts
  • Exception handling: Uncaught exceptions may terminate the evolution process
  • Memory usage: Be careful with references to population data to avoid memory leaks

Example implementations:


 // Simple fitness tracker
 EvolutionListener<Double> fitnessTracker = (generation, population, fitness, isDone) -> {
     double bestFitness = fitness.stream().max(Double::compare).orElse(0.0);
     double avgFitness = fitness.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
     System.out.printf("Generation %d: Best=%.3f, Avg=%.3f%n", generation, bestFitness, avgFitness);
 };
 
 // CSV logging listener
 EvolutionListener<Double> csvLogger = (generation, population, fitness, isDone) -> {
     if (generation % 10 == 0) { // Log every 10 generations
         logToCSV(generation, fitness);
     }
 };
 
 // Convergence monitor
 EvolutionListener<Double> convergenceMonitor = new ConvergenceListener(0.001, 50);
 

The framework provides several built-in listener implementations:

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    onEvolution(long generation, List<Genotype> population, List<T> fitness, boolean isDone)
    Called after each generation to notify about evolution progress.
  • Method Details

    • onEvolution

      void onEvolution(long generation, List<Genotype> population, List<T> fitness, boolean isDone)
      Called after each generation to notify about evolution progress.

      This method is invoked by the evolutionary algorithm after each generation has been completed, providing access to the current population state and fitness values. The implementation can use this information for monitoring, logging, or adaptive control.

      The method is called with:

      • Current generation number (starting from 0)
      • Complete population of genotypes for this generation
      • Corresponding fitness values for each individual
      • Flag indicating whether evolution has completed

      Important notes:

      • Population and fitness lists are guaranteed to have the same size
      • Fitness values correspond to genotypes at the same index
      • Data may be shared with the evolution algorithm; avoid modification
      • Method should execute quickly to avoid impacting evolution performance
      Parameters:
      generation - the current generation number (0-based)
      population - the list of genotypes in the current generation
      fitness - the list of fitness values corresponding to each genotype
      isDone - true if the evolution has completed, false otherwise
      Throws:
      RuntimeException - if the listener encounters an error that should halt evolution