GenomeType
- Type of genome used to represent a single element in
the genetic population. For example, a Vector
.CostParametersType
- Type of parameters that the cost function
takes. For example, Collection<InputOutputPairs>
.@CodeReview(reviewer="Kevin R. Dixon",date="2008-07-23",changesNeeded=false,comments={"I don\'t much like the constructors for this class, but it\'s probably not worth changing at this point.","Made some cosmetic changes to the code.","Added previous code review as CodeReview annotation","Otherwise, looks fine."}) @CodeReview(reviewer="Justin Basilico",date="2006-10-05",changesNeeded=false,comments={"Cleaned up the code a little.","Made the constructor initialize the variables."}) public class GeneticAlgorithm<CostParametersType,GenomeType> extends AbstractAnytimeBatchLearner<CostParametersType,GenomeType> implements BatchCostMinimizationLearner<CostParametersType,GenomeType>, MeasurablePerformanceAlgorithm
SimulatedAnnealer
,
Serialized FormModifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_ITERATIONS
The default maximum number of iterations, 1000.
|
data, keepGoing
maxIterations
DEFAULT_ITERATION, iteration
Constructor and Description |
---|
GeneticAlgorithm(java.util.Collection<GenomeType> initialPopulation,
Reproducer<GenomeType> reproducer,
CostFunction<? super GenomeType,? super CostParametersType> cost)
Creates a new instance of GeneticAlgorithm.
|
GeneticAlgorithm(java.util.Collection<GenomeType> initialPopulation,
Reproducer<GenomeType> reproducer,
CostFunction<? super GenomeType,? super CostParametersType> cost,
int maxIterations)
Creates a new instance of GeneticAlgorithm.
|
GeneticAlgorithm(java.util.Collection<GenomeType> initialPopulation,
Reproducer<GenomeType> reproducer,
CostFunction<? super GenomeType,? super CostParametersType> cost,
int maxIterations,
int maxIterationsWithoutImprovement)
Creates a new instance of GeneticAlgorithm.
|
Modifier and Type | Method and Description |
---|---|
protected void |
cleanupAlgorithm()
Called to clean up the learning algorithm's state after learning has
finished.
|
protected java.util.ArrayList<EvaluatedGenome<GenomeType>> |
evaluatePopulation(java.util.Collection<GenomeType> population)
Converts a population of genomes into evaluated genomes.
|
EvaluatedGenome<GenomeType> |
getBestSoFar()
Gets the best genome found so far.
|
CostFunction<? super GenomeType,? super CostParametersType> |
getCostFunction()
Gets the cost function that the learner is minimizing.
|
java.util.Collection<GenomeType> |
getInitialPopulation()
Getter for initialPopulation.
|
int |
getIterationsWithoutImprovement()
Gets the current number of iterations without improvement.
|
int |
getMaxIterationsWithoutImprovement()
Gets the maximum number of iterations to go without improvement before
stopping.
|
NamedValue<java.lang.Double> |
getPerformance()
Gets the performance, which is the cost of the best genome.
|
java.util.Collection<EvaluatedGenome<GenomeType>> |
getPopulation()
Gets the population of genomes.
|
Reproducer<GenomeType> |
getReproducer()
Gets the reproducer.
|
GenomeType |
getResult()
Gets the current result of the algorithm.
|
protected boolean |
initializeAlgorithm()
Called to initialize the learning algorithm's state based on the
data that is stored in the data field.
|
protected EvaluatedGenome<GenomeType> |
searchForBetter(EvaluatedGenome<GenomeType> bestSoFar,
java.util.Collection<EvaluatedGenome<GenomeType>> population)
Searches the provided population of genomes for one whose cost is lower
than the provided best so far genome.
|
void |
setBestSoFar(EvaluatedGenome<GenomeType> bestSoFar)
Sets the best genome found so far.
|
void |
setCostFunction(CostFunction<? super GenomeType,? super CostParametersType> cost)
Sets the cost function.
|
void |
setInitialPopulation(java.util.Collection<GenomeType> initialPopulation)
Setter for initialPopulation.
|
void |
setIterationsWithoutImprovement(int iterationsWithoutImprovement)
Sets the current number of iterations without improvement.
|
void |
setMaxIterationsWithoutImprovement(int maxIterationsWithoutImprovement)
Sets the maximum number of iterations to go without improvement before
stopping.
|
void |
setPopulation(java.util.Collection<EvaluatedGenome<GenomeType>> population)
Sets the population of genomes.
|
void |
setReproducer(Reproducer<GenomeType> reproducer)
Sets the reproducer.
|
protected boolean |
step()
Called to take a single step of the learning algorithm.
|
clone, getData, getKeepGoing, learn, setData, setKeepGoing, stop
getMaxIterations, isResultValid, setMaxIterations
addIterativeAlgorithmListener, fireAlgorithmEnded, fireAlgorithmStarted, fireStepEnded, fireStepStarted, getIteration, getListeners, removeIterativeAlgorithmListener, setIteration, setListeners
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
learn
clone
getMaxIterations, setMaxIterations
addIterativeAlgorithmListener, getIteration, removeIterativeAlgorithmListener
isResultValid
public static final int DEFAULT_MAX_ITERATIONS
public GeneticAlgorithm(java.util.Collection<GenomeType> initialPopulation, Reproducer<GenomeType> reproducer, CostFunction<? super GenomeType,? super CostParametersType> cost)
cost
- The cost function for genomes.initialPopulation
- The initial population to start the algorithmreproducer
- The reproduction method to use.public GeneticAlgorithm(java.util.Collection<GenomeType> initialPopulation, Reproducer<GenomeType> reproducer, CostFunction<? super GenomeType,? super CostParametersType> cost, int maxIterations)
cost
- The cost function for genomes.initialPopulation
- The initial population to start the algorithmreproducer
- The reproduction method to use.maxIterations
- The maximum number of iterations to run.public GeneticAlgorithm(java.util.Collection<GenomeType> initialPopulation, Reproducer<GenomeType> reproducer, CostFunction<? super GenomeType,? super CostParametersType> cost, int maxIterations, int maxIterationsWithoutImprovement)
cost
- The cost function for genomes.initialPopulation
- The initial population to start the algorithmreproducer
- The reproduction method to use.maxIterations
- The maximum number of iterations to run.maxIterationsWithoutImprovement
- The maximum number of iterations
to go without improvement before stopping.protected boolean initializeAlgorithm()
AbstractAnytimeBatchLearner
initializeAlgorithm
in class AbstractAnytimeBatchLearner<CostParametersType,GenomeType>
protected boolean step()
AbstractAnytimeBatchLearner
step
in class AbstractAnytimeBatchLearner<CostParametersType,GenomeType>
protected void cleanupAlgorithm()
AbstractAnytimeBatchLearner
cleanupAlgorithm
in class AbstractAnytimeBatchLearner<CostParametersType,GenomeType>
public GenomeType getResult()
AnytimeAlgorithm
getResult
in interface AnytimeAlgorithm<GenomeType>
protected EvaluatedGenome<GenomeType> searchForBetter(EvaluatedGenome<GenomeType> bestSoFar, java.util.Collection<EvaluatedGenome<GenomeType>> population)
bestSoFar
- The genome to compare to.population
- The population to search.protected java.util.ArrayList<EvaluatedGenome<GenomeType>> evaluatePopulation(java.util.Collection<GenomeType> population)
population
- The population of genomes to evaluate.public CostFunction<? super GenomeType,? super CostParametersType> getCostFunction()
BatchCostMinimizationLearner
getCostFunction
in interface BatchCostMinimizationLearner<CostParametersType,GenomeType>
public Reproducer<GenomeType> getReproducer()
public EvaluatedGenome<GenomeType> getBestSoFar()
public int getMaxIterationsWithoutImprovement()
public int getIterationsWithoutImprovement()
public java.util.Collection<EvaluatedGenome<GenomeType>> getPopulation()
public void setCostFunction(CostFunction<? super GenomeType,? super CostParametersType> cost)
cost
- The new cost function.public void setReproducer(Reproducer<GenomeType> reproducer)
reproducer
- The new reproducer.public void setBestSoFar(EvaluatedGenome<GenomeType> bestSoFar)
bestSoFar
- The new best genome.public void setMaxIterationsWithoutImprovement(int maxIterationsWithoutImprovement)
maxIterationsWithoutImprovement
- The new maximum.public void setIterationsWithoutImprovement(int iterationsWithoutImprovement)
iterationsWithoutImprovement
- The new iteration.public void setPopulation(java.util.Collection<EvaluatedGenome<GenomeType>> population)
population
- The new population.public java.util.Collection<GenomeType> getInitialPopulation()
public void setInitialPopulation(java.util.Collection<GenomeType> initialPopulation)
initialPopulation
- The initial population of genomes.public NamedValue<java.lang.Double> getPerformance()
getPerformance
in interface MeasurablePerformanceAlgorithm