Package org.jppf.ga

Class GeneticAlgorithm

java.lang.Object
org.jppf.ga.GeneticAlgorithm
All Implemented Interfaces:
AutoCloseable

public class GeneticAlgorithm extends Object implements AutoCloseable
Author:
Laurent Cohen
  • Field Details

    • population

      protected Chromosome[] population
      The genetic algorithm's population.
    • generationCount

      protected int generationCount
      The count of generations (epochs).
    • nbSelect

      protected int nbSelect
      The number of chromosomes to select.
    • crossoverProbability

      protected double crossoverProbability
      Probability of corssover for each chromosome.
    • random

      protected Random random
    • outputFrequency

      public int outputFrequency
      How often to display the latest state.
    • nbToKeep

      protected int nbToKeep
    • nbToKeepPct

      protected double nbToKeepPct
  • Constructor Details

    • GeneticAlgorithm

      public GeneticAlgorithm(Chromosome[] population, int nbToKeep, int nbSelect, int tournamentSize, double crossoverProbability)
      Parameters:
      population - the inital population.
      nbToKeep - the number of chromosomes to keep at each generation.
      nbSelect - the number of chromosomes to select at each generation.
      tournamentSize - the tournament selector size.
      crossoverProbability - probability of crossover for each chromosome.
  • Method Details

    • run

      public Chromosome run(int maxGenerations)
      Parameters:
      maxGenerations - max number of generations (epochs) to run for.
      Returns:
      the chromosome with the best fitness score.
    • getCurrentPopulationSize

      public int getCurrentPopulationSize()
      Returns:
      the current population size.
    • postEpoch

      protected void postEpoch(Chromosome best)
      Callback invoked at the end of each epoch. Intended to be overriden in subclasses.
      Parameters:
      best - the chromosome with the best fitness score.
    • shouldStop

      protected boolean shouldStop(Chromosome best)
      Parameters:
      best - the chromosome with the best fitness score.
      Returns:
      whether to stop the algorithm.
    • computeFitness

      protected void computeFitness(Chromosome[] pop)
      Compute the fitness scores for the specified population.
      Parameters:
      pop - the chromosomes for which to compute the fitness.
    • performCrossover

      public Chromosome[] performCrossover(Chromosome[] pop)
      Perform the crossover operation for the specified population.
      Parameters:
      pop - the chromosomes to crossover.
      Returns:
      the new population obtained by corssover.
    • performMutation

      public Chromosome[] performMutation(Chromosome[] pop, int nbSelect)
      Mutate the specified population.
      Parameters:
      pop - the population to mutate.
      nbSelect - the number of chromosmes to mutate.
      Returns:
      the mutated population.
    • displayStats

      public void displayStats(int gen, Chromosome[] pop, long elapsed)
      Displays some statistics on the current state of the algorithm.
      Parameters:
      gen - current generation number.
      pop - current population.
      elapsed - elpased time since previous generation.
    • testValid

      public boolean testValid()
      Returns:
      whether the population is valid.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • addToPoulation

      public Chromosome[] addToPoulation(Chromosome[] toAdd)
      Add new chromosomes to the population.
      Parameters:
      toAdd - the chromosomes to add.
      Returns:
      the new population.
    • getGenerationCount

      public int getGenerationCount()
      Get the current number of generations.
      Returns:
      the generation count as an int.