Class GeneticAlgorithm<H extends AbstractHypothesis<H>>

java.lang.Object
de.sfuhrm.genetic.GeneticAlgorithm<H>
Type Parameters:
H - The hypothesis class to use.

public class GeneticAlgorithm<H extends AbstractHypothesis<H>>
extends java.lang.Object
Generic genetic algorithm implementation.
Author:
Stephan Fuhrmann
  • Constructor Summary

    Constructors 
    Constructor Description
    GeneticAlgorithm​(double inCrossOverRate, double inMutationRate, int inGenerationSize)
    Constructs a new genetic algorithm.
  • Method Summary

    Modifier and Type Method Description
    protected void crossover​(java.util.List<H> population, java.util.Collection<H> selectedSet)
    Cross-overs a fraction of crossOverRate hypothesis relative to their fitness.
    java.util.Optional<H> findMaximum​(java.util.function.Function<H,​java.lang.Boolean> loopCondition, java.util.function.Supplier<H> hypothesisSupplier)
    Perform the genetic operation.
    protected java.util.Optional<H> max​(java.util.Collection<H> in)
    Find the maximum fitness element of the given collection.
    protected void mutate​(java.util.List<H> selectedSet)
    Mutates a fraction of mutationRate hypothesis.
    protected H probabilisticSelect​(java.util.List<H> population, java.util.Collection<H> targetList, boolean addToTargetList)
    Probabilistically selects one hypothesis relative to the selection probability of it.
    protected void select​(java.util.List<H> population, java.util.Collection<H> selectedList)
    Selects a fraction of 1-crossOverRate hypothesis relative to their fitness.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GeneticAlgorithm

      public GeneticAlgorithm​(double inCrossOverRate, double inMutationRate, int inGenerationSize)
      Constructs a new genetic algorithm.
      Parameters:
      inCrossOverRate - the fraction at which the cross over operator is applied to the population, between 0 and 1.
      inMutationRate - the fraction at which the mutation operator is applied to the population, between 0 and 1.
      inGenerationSize - the number of individual hypothesis in the population for each generation, greater than 1.
      Throws:
      java.lang.IllegalArgumentException - if the parameters are illegal.
  • Method Details

    • select

      protected void select​(java.util.List<H> population, java.util.Collection<H> selectedList)
      Selects a fraction of 1-crossOverRate hypothesis relative to their fitness.
      Parameters:
      population - the population to select on.
      selectedList - the target list to put selected elements to.
    • crossover

      protected void crossover​(java.util.List<H> population, java.util.Collection<H> selectedSet)
      Cross-overs a fraction of crossOverRate hypothesis relative to their fitness.
      Parameters:
      population - the population to select on.
      selectedSet - the target set to put crossed over elements to.
    • mutate

      protected void mutate​(java.util.List<H> selectedSet)
      Mutates a fraction of mutationRate hypothesis.
      Parameters:
      selectedSet - the population to mutate on.
    • probabilisticSelect

      protected H probabilisticSelect​(java.util.List<H> population, java.util.Collection<H> targetList, boolean addToTargetList)
      Probabilistically selects one hypothesis relative to the selection probability of it.
      Parameters:
      population - the population to select from.
      targetList - the target list to eventually add the element to.
      addToTargetList - whether to add the element to the targetList.
      Returns:
      the selected element.
    • max

      protected java.util.Optional<H> max​(java.util.Collection<H> in)
      Find the maximum fitness element of the given collection.
      Parameters:
      in - the population to find the maximum in.
      Returns:
      the maximum element, if any.
    • findMaximum

      public java.util.Optional<H> findMaximum​(java.util.function.Function<H,​java.lang.Boolean> loopCondition, java.util.function.Supplier<H> hypothesisSupplier)
      Perform the genetic operation.
      Parameters:
      loopCondition - the abort condition that stays true while the maximum is not yet reached. Gets presented the best hypothesis as input.
      hypothesisSupplier - creation function for new hypothesis.
      Returns:
      the maximum element, if any.