|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object jenes.GeneticAlgorithm<T>
T
- The class of chromosomes to work with.public abstract class GeneticAlgorithm<T extends Chromosome>
This is the main class of JENES, providing the skeleton for implementing genetic algorithms.
A genetic algorithm can be implemented by subclassing
GeneticAlgorithm
and specifying 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);
Then, the genetic algorithm is subclassed and instanced.
GeneticAlgorithm<BooleanChromosome> ga = new GeneticAlgorithm<BooleanChromosome>(pop, GENERATION_LIMIT) { protected void evaluateIndividual(Individual<BooleanChromosome> individual) { BooleanChromosome chrom = individual.getChromosome(); int count = 0; int length=chrom.length(); for(int i=0; i<length i++) if(chrom.getValue(i)) count++; individual.setScore(count); } };
In this example, we used an anonymous subclass, but other subclassing methods can be used. After, stages (operators in particular) are added to the algorithm's body.
AbstractStage<BooleanChromosome> selection = new TournamentSelector<BooleanChromosome>( 3); AbstractStage<BooleanChromosome> crossover = new OnePointCrossover<BooleanChromosome>( 0.8); AbstractStage<BooleanChromosome> mutation = new SimpleMutator<BooleanChromosome>( 0.2); ga.addStage(selection); ga.addStage(crossover); ga.addStage(mutation);
Finally, the algorithm is executed.
ga.evolve();
A genetic algorithm processes a Population
of Individual
s.
At each generation there are an input and output population. The reference to
these populations can be respectively obtained by the methods
getCurrentPopulation()
and getNextPopulation()
. Past
populations are buffered in the algorithm's history. An history population
can be retrieve by the method getHistoryAt(int)
. The reference to
an history population is valid for the history length. After, populations are
collected for reuse, so references to them are not anymore valid.
The genetic algorithm execution is invoked by the method evolve()
.
The algorithm execution passes through the following events:
AlgorithmEventListener
s and
GenerationEventListener
s. They can also be caputured by the
GeneticAlgorithm
subclass, by overriding the methods
onStart(long)
, onInit(long)
, onGeneration(long)
, and
onStop(long)
. Capturing events is useful to collect statistics and to
perform analyses. Evolution terminates when the maximum number of generations
is reached. It is possible to terminate the execution on the basis of some
condition (e.g. precision level, variance of population, ecc.) by overriding
the method end()
.
GeneticAlgorithm
include a support for elitism, that is best
individuals at each generation are assured to be at the next generation. The
number of elite individuals is set by the method setElitism(int)
.
These individuals are substituted to some individuals to the processed
population according to the following strategies:
GeneticAlgorithm.ElitismStrategy.RANDOM
: next population individuals are
randomly selected and substituted by elite.
GeneticAlgorithm.ElitismStrategy.WORST
: next population worst individuals are
substituted by elite.
The genetic algorithm can be used for both maximizing or minimizing the
fitness function. This can be accomplished by setting the body objective
through the mehod setBiggerIsBetter(boolean)
.
GeneticAlgorithm
can process populations with a variable
number of individuals at each generation.
GeneticAlgorithm
performs by default an initial randomization
of the population.
Nested Class Summary | |
---|---|
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. |
Field Summary | |
---|---|
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 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 Summary | |
---|---|
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. |
Method Summary | |
---|---|
void |
addAlgorithmEventListener(AlgorithmEventListener ael)
Adds a new algorthm event listener |
void |
addGenerationEventListener(GenerationEventListener 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. |
protected abstract void |
evaluateIndividual(Individual<T> individual)
Evaluates a single individual. |
protected void |
evaluatePopulation(Population<T> population)
Evaluates the population. |
void |
evolve()
Evolves the algorithms by resetting the initial population and restarting the algorithm. |
void |
evolve(boolean restart)
Evolves the algorithm until the termination condition or the generation limit is reached. |
Sequence<T> |
getBody()
Returns the algorithm's body sequence. |
Population<T> |
getCurrentPopulation()
Returns the current population. |
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 |
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> |
getNextPopulation()
Returns the genetic algorithm next population. |
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. |
GeneticAlgorithm.Statistics |
getStatistics()
Returns algoritm statistics at the moment of invokation. |
boolean |
isBiggerBetter()
Says if the algorithm's body objective is set to maximize or minimize individual fitness. |
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)
Sets the algorithm's body objective to maximize (true) or minimize (false) the individual fitness. |
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 |
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 |
setResizeStrategy(GeneticAlgorithm.ResizeStrategy rs)
Sets the resize stragety. |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
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 Random random
Constructor Detail |
---|
public GeneticAlgorithm(Population<T> pop)
pop
- the sample populationpublic GeneticAlgorithm(Population<T> pop, int genlimit)
pop
- the sample populationgenlimit
- the generations upper boundMethod Detail |
---|
public final int getHistorySize()
public final 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 position
public final Population<T> getInitialPopulation()
public final Population<T> getCurrentPopulation()
public final Population<T> getNextPopulation()
public final void evolve() throws AlgorithmException
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 millisecondsprotected final void evaluatePopulation(Population<T> population)
evolve()
,
thus it should not be explicity invoked.
population
- the population to be evaluatedprotected abstract void evaluateIndividual(Individual<T> individual)
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 final 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 final void setGenerationLimit(int limit)
limit
- the new generation limitpublic final void addStage(AbstractStage<T> stage)
stage
- the stage to be addpublic final boolean isBiggerBetter()
public final void setBiggerIsBetter(boolean flag)
flag
- true to set the body to maximize the fitness, false to
minimizeprotected final void applyElitism()
evolve()
,
and should not be explicitaly invoked.
public final int getElitism()
public final void setElitism(int elitism)
elitism
- the new elitism parameterpublic final GeneticAlgorithm.ElitismStrategy getElitismStrategy()
public final void setElitismStrategy(GeneticAlgorithm.ElitismStrategy es)
es
- the elitism strategypublic final 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 ael)
ael
- the listener to addpublic final void removeAlgorithmEventListener(AlgorithmEventListener ael)
ael
- the listener to removepublic final void addGenerationEventListener(GenerationEventListener 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
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |