Class GeneticAlgorithmSimulation<C extends GASolution<C>>

java.lang.Object
edu.cnu.mdi.sim.ga.GeneticAlgorithmSimulation<C>
Type Parameters:
C - The type of solutions in the population, which must extend GASolution.
All Implemented Interfaces:
Simulation

public class GeneticAlgorithmSimulation<C extends GASolution<C>> extends Object implements Simulation
A generic genetic algorithm simulation. This class is responsible for managing the population, applying the GA operators, and tracking the best solution found. It implements the Simulation interface, allowing it to be run in a simulation engine with a GUI.
  • Constructor Details

    • GeneticAlgorithmSimulation

      public GeneticAlgorithmSimulation(GAProblem<C> problem, GAConfig cfg, GAOperators<C> operators)
      Constructor for the GeneticAlgorithmSimulation.
      Parameters:
      problem - The GA problem to solve, which defines the fitness function and initial population.
      cfg - The configuration for the GA, including parameters like population size and mutation rate.
      operators - The set of GA operators to use, including selection, crossover, mutation, and replacement.
  • Method Details

    • setFeedback

      public void setFeedback(GAFeedback feedback)
      Set the optional output channel for messages, progress, and refresh hints.
      Parameters:
      feedback - output channel, or null to discard output
    • init

      public void init(SimulationContext ctx)
      Seeds the RNG, builds the initial population via GAProblem.initialPopulation(int, java.util.Random), evaluates and tracks the best individual, and resets the generation counter to zero.
      Specified by:
      init in interface Simulation
      Parameters:
      ctx - the simulation context (unused; present to satisfy Simulation.init(edu.cnu.mdi.sim.SimulationContext))
      Throws:
      IllegalStateException - if the problem's initial population is null or does not contain exactly GAConfig.populationSize() individuals
    • step

      public boolean step(SimulationContext ctx)
      Advances the GA by one generation: builds an offspring pool via selection/crossover/mutation, evaluates it, replaces the population via ReplacementOperator.replace(java.util.List<T>, java.util.List<T>, double[], double[], java.util.Random), and re-evaluates the resulting generation.

      The replaced generation (nextGen) is always fully re-evaluated rather than reusing currentFits/offFitness by slot, because ReplacementOperator is explicitly allowed to reorder or select individuals from either input in arbitrary ways — fitness values cannot safely be reconstructed by positional convention.

      Returns false (stopping the simulation) once SimulationContext.isCancelRequested() is true or GAConfig.maxGenerations() has been reached.

      Specified by:
      step in interface Simulation
      Parameters:
      ctx - the simulation context, used to check for cancellation
      Returns:
      true if another generation should run; false to stop
      Throws:
      NullPointerException - if a selection or crossover operator returns null, or a mutated child is null
      IllegalStateException - if the replacement operator's requested offspring count is out of range, if crossover produces no children, or if the replacement operator does not return exactly popSize non-null individuals
    • getState

      public GAState getState()
      Returns the current state of the GA, including generation number, best fitness, mean fitness, worst fitness, and diversity. This method computes the mean and worst fitness from the current fitnesses array. The diversity is currently set to 0.0 as a placeholder; it can be implemented based on the population's characteristics.
      Returns:
      A GAState object representing the current state of the GA, which can be used for UI display or logging.
    • getProblem

      public GAProblem<C> getProblem()
      Returns the GA problem being solved by this simulation. This allows external code to access the problem definition, including the fitness function and initial population generator, which can be useful for UI components or logging.
      Returns:
      The GA problem being solved by this simulation.
    • getBestIndividualCopy

      public C getBestIndividualCopy()
      Returns a copy of the best individual found so far in the GA. This allows external code to access the best solution without risking modification of the internal state. If no individuals have been evaluated yet, this method returns null.
      Returns:
      A copy of the best individual found so far, or null if no individuals have been evaluated.
    • getGeneration

      public long getGeneration()
      Returns the current generation number of the GA. This is incremented at the end of each step and can be used for tracking progress or logging.
      Returns:
      The current generation number of the GA.
    • getPopulationSnapshot

      public List<C> getPopulationSnapshot()
      Returns a snapshot of the current population as an unmodifiable list. This allows external code to access the current individuals in the population without risking modification of the internal state. If the population has not been initialized yet, this method returns an empty list.
      Returns:
      An unmodifiable list of the current individuals in the population, or an empty list if the population is not initialized.