T
- The class of chromosomes to work with.public class GeneticAlgorithm<T extends Chromosome>
extends java.lang.Object
A genetic algorithm can be implemented by providing a Fitness
function.
An alternative way (but deprecated) is to subclass
GeneticAlgorithm
and to overwrite the method
evaluateIndividual(Individual)
.
The genetic algorithm body is made
of a sequence of stages. Which stages and in which order to consider is left
to the algorithm needs. Generally, the sequence is made of a selection
operator, followed by a a crossover operator, and then by a mutation
operator. This schema is implemented by SimpleGA
.
An example of code is provided below. First, the initial population has to be created.
BooleanChromosome chrom = new BooleanChromosome(CHROMOSOME_LENGTH); Individual<BooleanChromosome> ind = new Individual<BooleanChromosome>(chrom); Population<BooleanChromosome> pop = new Population<BooleanChromosome>(ind, POPULATION_SIZE);
A fitness function is implmented.
Fitnessfit = new Fitness (true) {
Modifier and Type | Class and Description |
---|---|
static class |
GeneticAlgorithm.ElitismStrategy
The elitism strategy enumeration
|
static class |
GeneticAlgorithm.ResizeStrategy
The resize stategy enumeration.
|
static class |
GeneticAlgorithm.Statistics
This class provides some basic statistics regarding the algorithm
execution.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.List<AlgorithmEventListener<T>> |
algorithmListeners
The genetic algorithm listeners
|
protected Sequence<T> |
body
The main algorithm sequence
|
static int |
DEFAULT_GENERATION_LIMIT
The default maximum number of generations
|
static int |
DEFAULT_HISTORY_SIZE
The default history size
|
protected int |
elitism
The elitism number
|
protected GeneticAlgorithm.ElitismStrategy |
elitismStrategy
The elitism strategy used by this genetic algorithm
|
protected boolean |
fullEvaluationForced
The flag controlling the evalution.
|
protected int |
generation
Current generation count
|
protected int |
generationLimit
Maximum number of generations
|
protected java.util.List<GenerationEventListener<T>> |
generationListeners
The generation listeners
|
protected Population<T> |
initialPopulation
The initial population
|
static int |
MAX_HISTORY_SIZE
The maximum history size
|
static int |
MIN_HISTORY_SIZE
The minimum history size
|
protected Random |
random
The random instance
|
protected double |
randomization
Rate of randomization of initial population
|
protected GeneticAlgorithm.ResizeStrategy |
resizeStrategy
The resize strategy used by this genetic algorithm
|
protected GeneticAlgorithm.Statistics |
statistics
The statistics object responsible for storing statistics about this
genetic algorithm.
|
Constructor and Description |
---|
GeneticAlgorithm()
Constructs a new genetic algorithm with no initial population and the
default generation limit.
|
GeneticAlgorithm(Fitness fitness)
Constructs a new genetic algorithm with no initial population and the
default generation limit.
|
GeneticAlgorithm(Fitness fitness,
Population<T> pop)
Constructs a new genetic algorithm with the specified population and the
default generation limit.
|
GeneticAlgorithm(Fitness fitness,
Population<T> pop,
int genlimit)
Constructs a new genetic algorithm with the specified population and the
specified generation limit.
|
GeneticAlgorithm(Population<T> pop)
Constructs a new genetic algorithm with the specified population and the
default generation limit.
|
GeneticAlgorithm(Population<T> pop,
int genlimit)
Constructs a new genetic algorithm with the specified population and the
specified generation limit.
|
Modifier and Type | Method and Description |
---|---|
void |
addAlgorithmEventListener(AlgorithmEventListener<T> ael)
Adds a new algorthm event listener
|
void |
addGenerationEventListener(GenerationEventListener<T> gel)
Adds a new generation event listener
|
void |
addStage(AbstractStage<T> stage)
Adds a new stage at the genetic algorithm's body
|
protected void |
applyElitism()
Applies the elitism to the current population, according to the chosen
strategy.
|
protected boolean |
end()
Provides the algorithm termination condition.
|
void |
evaluateIndividual(Individual<T> individual)
Evaluates a single individual.
|
void |
evaluatePopulation(Population<T> population)
Evaluates the population.
|
void |
evaluatePopulation(Population<T> population,
boolean forced)
Evaluates the population.
|
void |
evolve()
Evolves the algorithms by restarting the algorithm from the initial population.
|
void |
evolve(boolean restart)
Evolves the algorithm until the termination condition or the generation
limit is reached.
|
void |
evolve(Population<T> pop)
Evolves the algorithms by resetting the initial population and restarting the algorithm.
|
Sequence<T> |
getBody()
Returns the algorithm's body sequence.
|
Population<T> |
getCurrentPopulation()
Deprecated.
Use
getLastPopulation() instead. |
int |
getElitism()
Returns the number of individuals considered for elitism by the genetic
algorithm.
|
GeneticAlgorithm.ElitismStrategy |
getElitismStrategy()
Returns the elitism strategy used by this genetic algorithm
|
Fitness |
getFitness()
Returns the
Fitness from this genetic algorithm's body
|
int |
getGeneration()
Returns the current generation counter
|
int |
getGenerationLimit()
Returns the generation limit
|
Population<T> |
getHistoryAt(int pos)
Returns the history population at the specified generation.
|
int |
getHistorySize()
Returns the history size that is the number of populations kept by
history
|
Population<T> |
getInitialPopulation()
Returns the initial population.
|
Population<T> |
getLastPopulation()
Returns the current population.
|
Population<T> |
getNextPopulation()
Returns the genetic algorithm next population.
|
Pool<T> |
getPool()
Returns the individuals pool used by populations
|
double |
getRandomization()
Provides the randomization rate, that is the percentage of individuals
being randomized by the algorithm.
|
GeneticAlgorithm.ResizeStrategy |
getResizeStrategy()
Returns the resize strategy used by this genetic algorithm.
|
Runner |
getRunner()
Get current runner implementation
|
GeneticAlgorithm.Statistics |
getStatistics()
Returns algoritm statistics at the moment of invokation.
|
boolean |
isBiggerBetter()
Deprecated.
|
boolean |
isFitnessChanged()
Says if the fitness is changed or not.
|
boolean |
isFullEvaluationForced()
Returns the current evaluation mode.
|
protected void |
onGeneration(long time)
Invoked when a generation ga end event occurs.
|
protected void |
onInit(long time)
Invoked when an init end ga event occurs.
|
protected void |
onStart(long time)
Invoked when a start ga event occurs.
|
protected void |
onStop(long time)
Invoked when a stop event occurs.
|
protected void |
randomizeIndividual(Individual<T> individual)
Performs an individual randomization.
|
protected void |
randomizePopulation(Population<T> pop)
Perform a population randomization, by itering on individuals.
|
void |
removeAlgorithmEventListener(AlgorithmEventListener ael)
Removes an algorithm event listener
|
void |
removeGenerationEventListener(GenerationEventListener gel)
Removes the generation event listener
|
void |
setBiggerIsBetter(boolean flag)
Deprecated.
|
void |
setElitism(int elitism)
Sets the number of individuals considered for elitism by the genetic
algorithm.
|
void |
setElitismStrategy(GeneticAlgorithm.ElitismStrategy es)
Sets the elitism strategy to used by this genetic algorithm.
|
void |
setFitness(Fitness fitness)
Sets the
Fitness |
void |
setFullEvaluationForced(boolean flag)
Sets the evalaution mode.
|
void |
setGenerationLimit(int limit)
Sets the generation limit
|
void |
setHistorySize(int hs)
Sets the history size, that is the number of populations kept by history.
|
void |
setRandomization(boolean value)
Sets the randomization rate to 0 or 1, according to the flag.
|
void |
setRandomization(double rate)
Sets the randomization rate.
|
void |
setRandomSeed(long seed)
Impose
Random seed in order to reproduce an execution producing
the same enviroinment |
void |
setResizeStrategy(GeneticAlgorithm.ResizeStrategy rs)
Sets the resize stragety.
|
void |
setRunner(Runner runner)
Set the
Runner |
protected void |
start(boolean reset)
Starts this genetic algorithm.
|
protected void |
stop()
Terminates the genetic algorithm, notifying the stop event to listeners.
|
java.lang.String |
toString() |
void |
updateStatistics(GeneticAlgorithm.Statistics stats)
Updates the algoritm statistics at the moment of invokation.
|
public static final int DEFAULT_GENERATION_LIMIT
public static final int DEFAULT_HISTORY_SIZE
public static final int MIN_HISTORY_SIZE
public static final int MAX_HISTORY_SIZE
protected Sequence<T extends Chromosome> body
protected GeneticAlgorithm.Statistics statistics
protected int generation
protected double randomization
protected int generationLimit
protected int elitism
protected GeneticAlgorithm.ElitismStrategy elitismStrategy
protected Population<T extends Chromosome> initialPopulation
protected java.util.List<AlgorithmEventListener<T extends Chromosome>> algorithmListeners
protected java.util.List<GenerationEventListener<T extends Chromosome>> generationListeners
protected GeneticAlgorithm.ResizeStrategy resizeStrategy
protected boolean fullEvaluationForced
protected Random random
public GeneticAlgorithm()
public GeneticAlgorithm(Population<T> pop)
pop
- the sample populationpublic GeneticAlgorithm(Population<T> pop, int genlimit)
pop
- the sample populationgenlimit
- the generations upper boundpublic GeneticAlgorithm(Fitness fitness)
public GeneticAlgorithm(Fitness fitness, Population<T> pop)
pop
- the sample populationpublic GeneticAlgorithm(Fitness fitness, Population<T> pop, int genlimit)
pop
- the sample populationgenlimit
- the generations upper boundpublic final Fitness getFitness()
Fitness
from this genetic algorithm's body
public final void setFitness(Fitness fitness)
Fitness
fitness
- new fitnesspublic final boolean isFitnessChanged()
true
, if fitness is changedpublic Runner getRunner()
public void setRandomSeed(long seed)
Random
seed in order to reproduce an execution producing
the same enviroinmentseed
- the seed to setpublic final int getHistorySize()
public final Pool<T> getPool()
public void setHistorySize(int hs)
hs
- the new number of history populationspublic final Population<T> getHistoryAt(int pos)
pos
value can be relative or absolute. If relative, it
must be a negative number that specifies how many generations to go back
in order to get the population. So that, 0 means the current generation,
-1 means one generation back, -2 means two generations back, and so on.
If positive, the generation index is absolute. If the population at the
specified generation is not available, the method returns null.
pos
- the population positionpublic final Population<T> getInitialPopulation()
public final Population<T> getCurrentPopulation()
getLastPopulation()
instead.getLastPopulation()
instead.
public final Population<T> getLastPopulation()
public final Population<T> getNextPopulation()
public final void evolve() throws AlgorithmException
AlgorithmException
public final void evolve(Population<T> pop)
AlgorithmException
public final void evolve(boolean restart) throws AlgorithmException
restart
- if true, the initial population is reset;
if false, the algorithm continues from the last population.AlgorithmException
protected boolean end()
protected final void start(boolean reset)
evolve()
, thus it should
not be explicity invoked.reset
- if true, the algorithm reset the initial population.protected final void stop()
evolve()
, thus it
should not be explicity invoked.protected void onStart(long time)
GeneticAlgorithm
subclass
able to be notfied of the start event.time
- the start event time expressed in millisecondsprotected void onInit(long time)
GeneticAlgorithm
subclass able to be notfied of the init event.time
- the init event time expressed in millisecondsprotected void onStop(long time)
GeneticAlgorithm
subclass
able to be notfied of the stop event.time
- the stop event time expressed in millisecondsprotected void onGeneration(long time)
GeneticAlgorithm
subclass able to be notfied of the generation event.time
- the generation event time expressed in millisecondspublic final void evaluatePopulation(Population<T> population)
fullEvaluationForced
population
- the population to be evaluatedpublic final void evaluatePopulation(Population<T> population, boolean forced)
population
- the population to be evaluatedpublic void evaluateIndividual(Individual<T> individual)
Fitness
, this method calls Fitness.evaluate(jenes.population.Individual)
method, otherwise method requiring an implementation by the sublass.
individual
- the individual to be evaluatedprotected final void randomizePopulation(Population<T> pop)
The percentage of individuals to be randomized can be finely controlled
by the randomization rate. This is useful in many problems where a
dominant solution should be kept in the population, but still providing a
random genetic variety. The randomization rate is controlled by the
setRandomization(double)
method.
This method is automatically invoked by the start(boolean)
method, so
no explicit invokation is required.
pop
- the population to randomizeprotected void randomizeIndividual(Individual<T> individual)
randomizePopulation(Population)
. By default randomization is
delegated to the individual. In some problems, it would be useful to
control the randomization process of individual. This is especially the
case of when there are some constraints on genes in order to make the
individual valid.
individual
- the individual to be randomizepublic final void setRandomization(boolean value)
value
- if true the rate is set to 1, otherwise to 0.public void setRandomization(double rate)
rate
- the randomization rate to use during the randomization phasepublic final double getRandomization()
public final int getGeneration()
public final int getGenerationLimit()
public void setGenerationLimit(int limit)
limit
- the new generation limitpublic void addStage(AbstractStage<T> stage)
stage
- the stage to be add@Deprecated public final boolean isBiggerBetter()
@Deprecated public void setBiggerIsBetter(boolean flag)
flag
- true to set the body to maximize the fitness, false to
minimizepublic boolean isFullEvaluationForced()
public void setFullEvaluationForced(boolean flag)
flag
- - true if evaluation is referred to the whole popuation.
False if evaluation is only referred to unevaluated individuals. By default it is false.protected final void applyElitism()
evolve()
,
and should not be explicitaly invoked.public final int getElitism()
public void setElitism(int elitism)
elitism
- the new elitism parameterpublic final GeneticAlgorithm.ElitismStrategy getElitismStrategy()
public void setElitismStrategy(GeneticAlgorithm.ElitismStrategy es)
es
- the elitism strategypublic void setResizeStrategy(GeneticAlgorithm.ResizeStrategy rs)
Individual
. The resize strategy specifies the way to
perform such a task. Possible strategies are:
GeneticAlgorithm.ResizeStrategy.AUTO
: entails the creation of new
individuals, generally by cloning.
GeneticAlgorithm.ResizeStrategy.EMPTY
: expands the population, without
creating new individuals.
GeneticAlgorithm.ResizeStrategy.NONE
: disables the automatic resize of
populations.
rs
- the new resize strategypublic final GeneticAlgorithm.ResizeStrategy getResizeStrategy()
public final Sequence<T> getBody()
public final void addAlgorithmEventListener(AlgorithmEventListener<T> ael)
ael
- the listener to addpublic final void removeAlgorithmEventListener(AlgorithmEventListener ael)
ael
- the listener to removepublic final void addGenerationEventListener(GenerationEventListener<T> gel)
gel
- the generation listener to addpublic final void removeGenerationEventListener(GenerationEventListener gel)
gel
- the listener to removepublic final GeneticAlgorithm.Statistics getStatistics()
public final void updateStatistics(GeneticAlgorithm.Statistics stats)
stats
- the statistics object to be updatedpublic final java.lang.String toString()
toString
in class java.lang.Object