Package edu.cnu.mdi.sim.ga
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 Summary
ConstructorsConstructorDescriptionGeneticAlgorithmSimulation(GAProblem<C> problem, GAConfig cfg, GAOperators<C> operators) Constructor for the GeneticAlgorithmSimulation. -
Method Summary
Modifier and TypeMethodDescriptionReturns a copy of the best individual found so far in the GA.longReturns the current generation number of the GA.Returns a snapshot of the current population as an unmodifiable list.Returns the GA problem being solved by this simulation.getState()Returns the current state of the GA, including generation number, best fitness, mean fitness, worst fitness, and diversity.voidinit(SimulationContext ctx) Seeds the RNG, builds the initial population viaGAProblem.initialPopulation(int, java.util.Random), evaluates and tracks the best individual, and resets the generation counter to zero.voidsetFeedback(GAFeedback feedback) Set the optional output channel for messages, progress, and refresh hints.booleanstep(SimulationContext ctx) Advances the GA by one generation: builds an offspring pool via selection/crossover/mutation, evaluates it, replaces the population viaReplacementOperator.replace(java.util.List<T>, java.util.List<T>, double[], double[], java.util.Random), and re-evaluates the resulting generation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface edu.cnu.mdi.sim.Simulation
cancel, shutdown
-
Constructor Details
-
GeneticAlgorithmSimulation
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
Set the optional output channel for messages, progress, and refresh hints.- Parameters:
feedback- output channel, ornullto discard output
-
init
Seeds the RNG, builds the initial population viaGAProblem.initialPopulation(int, java.util.Random), evaluates and tracks the best individual, and resets the generation counter to zero.- Specified by:
initin interfaceSimulation- Parameters:
ctx- the simulation context (unused; present to satisfySimulation.init(edu.cnu.mdi.sim.SimulationContext))- Throws:
IllegalStateException- if the problem's initial population isnullor does not contain exactlyGAConfig.populationSize()individuals
-
step
Advances the GA by one generation: builds an offspring pool via selection/crossover/mutation, evaluates it, replaces the population viaReplacementOperator.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 reusingcurrentFits/offFitnessby slot, becauseReplacementOperatoris 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) onceSimulationContext.isCancelRequested()istrueorGAConfig.maxGenerations()has been reached.- Specified by:
stepin interfaceSimulation- Parameters:
ctx- the simulation context, used to check for cancellation- Returns:
trueif another generation should run;falseto stop- Throws:
NullPointerException- if a selection or crossover operator returnsnull, or a mutated child isnullIllegalStateException- 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 exactlypopSizenon-null individuals
-
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
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
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
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.
-