T
- The type of entity evolved by the evolution engine.public abstract class AbstractEvolutionEngine<T extends java.lang.Comparable<? super T>> extends java.lang.Object implements EvolutionEngine<T>
EvolutionEngine
implementations.CandidateFactory
,
FitnessEvaluator
Modifier and Type | Field and Description |
---|---|
protected CandidateFactory<T> |
candidateFactory |
protected FitnessEvaluator<? super T> |
fitnessEvaluator |
protected java.util.Random |
rng |
protected java.util.List<TerminationCondition> |
satisfiedTerminationConditions |
Modifier | Constructor and Description |
---|---|
protected |
AbstractEvolutionEngine(CandidateFactory<T> candidateFactory,
FitnessEvaluator<? super T> fitnessEvaluator,
java.util.Random rng)
Creates a new evolution engine by specifying the various components
required by an evolutionary algorithm.
|
Modifier and Type | Method and Description |
---|---|
void |
addEvolutionObserver(EvolutionObserver<? super T> observer)
Adds a listener to receive status updates on the evolution progress.
|
protected java.util.List<EvaluatedCandidate<T>> |
evaluatePopulation(java.util.List<T> population)
Takes a population, assigns a fitness score to each member and returns
the members with their scores attached, sorted in descending order of
fitness (descending order of fitness score for natural scores, ascending
order of scores for non-natural scores).
|
T |
evolve(int populationSize,
int eliteCount,
java.util.Collection<T> seedCandidates,
TerminationCondition... conditions)
Execute the evolutionary algorithm until one of the termination conditions is met,
then return the fittest candidate from the final generation.
|
T |
evolve(int populationSize,
int eliteCount,
TerminationCondition... conditions)
Execute the evolutionary algorithm until one of the termination conditions is met,
then return the fittest candidate from the final generation.
|
java.util.List<EvaluatedCandidate<T>> |
evolvePopulation(int populationSize,
int eliteCount,
java.util.Collection<T> seedCandidates,
TerminationCondition... conditions)
Execute the evolutionary algorithm until one of the termination conditions is met,
then return all of the candidates from the final generation.
|
java.util.List<EvaluatedCandidate<T>> |
evolvePopulation(int populationSize,
int eliteCount,
TerminationCondition... conditions)
Execute the evolutionary algorithm until one of the termination conditions is met,
then return all of the candidates from the final generation.
|
java.util.List<TerminationCondition> |
getSatisfiedTerminationConditions()
Returns a list of all
TerminationCondition s that are satisfied by
the current state of the evolution engine. |
protected abstract java.util.List<EvaluatedCandidate<T>> |
nextEvolutionStep(java.util.List<EvaluatedCandidate<T>> evaluatedPopulation,
int eliteCount,
java.util.Random rng)
This method performs a single step/iteration of the evolutionary process.
|
protected void |
notifyPopulationChange(PopulationData<T> data)
Send the population data to all registered observers.
|
void |
removeEvolutionObserver(EvolutionObserver<? super T> observer)
Removes an evolution progress listener.
|
void |
setSingleThreaded(boolean singleThreaded)
By default, fitness evaluations are performed on separate threads (as
many as there are available cores/processors).
|
protected final java.util.Random rng
protected final CandidateFactory<T extends java.lang.Comparable<? super T>> candidateFactory
protected final FitnessEvaluator<? super T extends java.lang.Comparable<? super T>> fitnessEvaluator
protected java.util.List<TerminationCondition> satisfiedTerminationConditions
protected AbstractEvolutionEngine(CandidateFactory<T> candidateFactory, FitnessEvaluator<? super T> fitnessEvaluator, java.util.Random rng)
candidateFactory
- Factory used to create the initial population that is
iteratively evolved.fitnessEvaluator
- A function for assigning fitness scores to candidate
solutions.rng
- The source of randomness used by all stochastic processes
(including evolutionary operators and selection strategies).public T evolve(int populationSize, int eliteCount, TerminationCondition... conditions)
EvolutionEngine.evolvePopulation(int, int, TerminationCondition[])
method instead.evolve
in interface EvolutionEngine<T extends java.lang.Comparable<? super T>>
populationSize
- The number of candidate solutions present in the population
at any point in time.eliteCount
- The number of candidates preserved via elitism. In elitism, a
sub-set of the population with the best fitness scores are preserved unchanged in
the subsequent generation. Candidate solutions that are preserved unchanged through
elitism remain eligible for selection for breeding the remainder of the next generation.
This value must be non-negative and less than the population size. A value of zero
means that no elitism will be applied.conditions
- One or more conditions that may cause the evolution to terminate.EvolutionEngine.evolve(int, int, Collection, TerminationCondition[])
public T evolve(int populationSize, int eliteCount, java.util.Collection<T> seedCandidates, TerminationCondition... conditions)
EvolutionEngine.evolvePopulation(int, int, Collection, TerminationCondition[])
method instead.evolve
in interface EvolutionEngine<T extends java.lang.Comparable<? super T>>
populationSize
- The number of candidate solutions present in the population
at any point in time.eliteCount
- The number of candidates preserved via elitism. In elitism, a
sub-set of the population with the best fitness scores are preserved unchanged in
the subsequent generation. Candidate solutions that are preserved unchanged through
elitism remain eligible for selection for breeding the remainder of the next generation.
This value must be non-negative and less than the population size. A value of zero
means that no elitism will be applied.seedCandidates
- A set of candidates to seed the population with. The size of
this collection must be no greater than the specified population size.conditions
- One or more conditions that may cause the evolution to terminate.EvolutionEngine.evolve(int,int,TerminationCondition[])
public java.util.List<EvaluatedCandidate<T>> evolvePopulation(int populationSize, int eliteCount, TerminationCondition... conditions)
EvolutionEngine.evolve(int, int, TerminationCondition[])
method instead.evolvePopulation
in interface EvolutionEngine<T extends java.lang.Comparable<? super T>>
populationSize
- The number of candidate solutions present in the population
at any point in time.eliteCount
- The number of candidates preserved via elitism. In elitism, a
sub-set of the population with the best fitness scores are preserved unchanged in
the subsequent generation. Candidate solutions that are preserved unchanged through
elitism remain eligible for selection for breeding the remainder of the next generation.
This value must be non-negative and less than the population size. A value of zero
means that no elitism will be applied.conditions
- One or more conditions that may cause the evolution to terminate.EvolutionEngine.evolve(int, int, Collection, TerminationCondition[])
,
EvolutionEngine.evolvePopulation(int, int, Collection, TerminationCondition[])
public java.util.List<EvaluatedCandidate<T>> evolvePopulation(int populationSize, int eliteCount, java.util.Collection<T> seedCandidates, TerminationCondition... conditions)
EvolutionEngine.evolve(int, int, Collection, TerminationCondition[])
method instead.evolvePopulation
in interface EvolutionEngine<T extends java.lang.Comparable<? super T>>
populationSize
- The number of candidate solutions present in the population
at any point in time.eliteCount
- The number of candidates preserved via elitism. In elitism, a
sub-set of the population with the best fitness scores are preserved unchanged in
the subsequent generation. Candidate solutions that are preserved unchanged through
elitism remain eligible for selection for breeding the remainder of the next generation.
This value must be non-negative and less than the population size. A value of zero
means that no elitism will be applied.seedCandidates
- A set of candidates to seed the population with. The size of
this collection must be no greater than the specified population size.conditions
- One or more conditions that may cause the evolution to terminate.EvolutionEngine.evolve(int, int, Collection, TerminationCondition[])
,
EvolutionEngine.evolvePopulation(int, int, Collection, TerminationCondition[])
protected abstract java.util.List<EvaluatedCandidate<T>> nextEvolutionStep(java.util.List<EvaluatedCandidate<T>> evaluatedPopulation, int eliteCount, java.util.Random rng)
evaluatedPopulation
- The population at the beginning of the process.eliteCount
- The number of the fittest individuals that must be preserved.rng
- A source of randomness.protected java.util.List<EvaluatedCandidate<T>> evaluatePopulation(java.util.List<T> population)
population
- The population to evaluate (each candidate is assigned a
fitness score).public java.util.List<TerminationCondition> getSatisfiedTerminationConditions()
Returns a list of all TerminationCondition
s that are satisfied by
the current state of the evolution engine. Usually this list will contain
only one item, but it is possible that mutliple termination conditions
will become satisfied at the same time. In this case the condition
objects in the list will be in the same order that they were specified
when passed to the engine.
If the evolution has not yet terminated (either because it is still in progress or because it hasn't even been started) then an IllegalStateException will be thrown.
If the evolution terminated because the request thread was interrupted before any termination conditions were satisfied then this method will return an empty list.
getSatisfiedTerminationConditions
in interface EvolutionEngine<T extends java.lang.Comparable<? super T>>
java.lang.IllegalStateException
- If this method is invoked on an evolution engine before
evolution is started or while it is still in progress.public void addEvolutionObserver(EvolutionObserver<? super T> observer)
addEvolutionObserver
in interface EvolutionEngine<T extends java.lang.Comparable<? super T>>
observer
- An evolution observer call-back.removeEvolutionObserver(EvolutionObserver)
public void removeEvolutionObserver(EvolutionObserver<? super T> observer)
removeEvolutionObserver
in interface EvolutionEngine<T extends java.lang.Comparable<? super T>>
observer
- An evolution observer call-back.addEvolutionObserver(EvolutionObserver)
protected void notifyPopulationChange(PopulationData<T> data)
data
- Information about the current state of the population.public void setSingleThreaded(boolean singleThreaded)
singleThreaded
- If true, fitness evaluations will be performed synchronously
on the request thread. If false, fitness evaluations will be
performed by worker threads.