Class CSVEvolutionListener<T extends Comparable<T>,U>

java.lang.Object
net.bmahe.genetics4j.extras.evolutionlisteners.CSVEvolutionListener<T,U>
Type Parameters:
T - Fitness type
U - Data type written to the CSV
All Implemented Interfaces:
EvolutionListener<T>
Direct Known Subclasses:
ImmutableCSVEvolutionListener

@Immutable public abstract class CSVEvolutionListener<T extends Comparable<T>,U> extends Object implements EvolutionListener<T>
Evolution Listener which writes the output of each generation to a CSV file
Author:
bruno
  • Field Details

    • logger

      public static final org.apache.logging.log4j.Logger logger
    • DEFAULT_AUTO_FLUSH

      public static final boolean DEFAULT_AUTO_FLUSH
      See Also:
    • csvPrinter

      private org.apache.commons.csv.CSVPrinter csvPrinter
  • Constructor Details

    • CSVEvolutionListener

      public CSVEvolutionListener()
  • Method Details

    • openPrinter

      protected org.apache.commons.csv.CSVPrinter openPrinter()
    • autoFlush

      @Default public boolean autoFlush()
      Whether or not the CSV writer has auto flush enabled. Defaults to true<T extends Comparable<T>,U>
      Returns:
    • evolutionContextSupplier

      @Default public GenerationFunction<T,U> evolutionContextSupplier()
      User defined function to provide some additional information when computing the value to write. Defaults to null
      Returns:
    • skipN

      @Default public int skipN()
      How many generations to skip between each writes. Defaults to writing every generations
      Returns:
    • filter

      @Default public Function<Stream<EvolutionStep<T,U>>,Stream<EvolutionStep<T,U>>> filter()
      Users can supply an optional set of filters to control which individuals get written and in which order. Default to have no impact.
      Returns:
    • filename

      @Parameter public abstract String filename()
      Destination file name for the CSV file
      Returns:
    • columnExtractors

      @Parameter public abstract List<ColumnExtractor<T,U>> columnExtractors()
      List of Column Extractors. They specify how and what to write from each individual at a given generation
      Returns:
    • onEvolution

      public void onEvolution(long generation, List<Genotype> population, List<T> fitness, boolean isDone)
      Description copied from interface: EvolutionListener
      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
      Specified by:
      onEvolution in interface EvolutionListener<T extends Comparable<T>>
      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
    • of

      public static <T extends Comparable<T>, U> CSVEvolutionListener<T,U> of(String filename, List<ColumnExtractor<T,U>> columnExtractors)
    • of

      public static <T extends Comparable<T>, U> CSVEvolutionListener<T,U> of(String filename, Iterable<? extends ColumnExtractor<T,U>> columnExtractors)
    • of

      public static <T extends Comparable<T>, U> CSVEvolutionListener<T,U> of(String filename, GenerationFunction<T,U> evolutionContextSupplier, Iterable<? extends ColumnExtractor<T,U>> columnExtractors)
    • of

      public static <T extends Comparable<T>, U> CSVEvolutionListener<T,U> of(String filename, GenerationFunction<T,U> evolutionContextSupplier, Iterable<? extends ColumnExtractor<T,U>> columnExtractors, int skipN)
    • ofTopN

      public static <T extends Comparable<T>, U> CSVEvolutionListener<T,U> ofTopN(String filename, GenerationFunction<T,U> evolutionContextSupplier, Iterable<? extends ColumnExtractor<T,U>> columnExtractors, Comparator<T> comparator, int topN)
    • ofTopN

      public static <T extends Comparable<T>, U> CSVEvolutionListener<T,U> ofTopN(String filename, GenerationFunction<T,U> evolutionContextSupplier, Iterable<? extends ColumnExtractor<T,U>> columnExtractors, int topN)