This module contains the GA Engine, the GA Engine class is responsible for all the evolutionary process. It contains the GA Engine related funtions, like the Termination Criteria functions for convergence analysis, etc.
Terminate the evolution when the population have converged
>>> ga_engine.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
Terminate the evoltion based on the fitness stats
>>> ga_engine.terminationCriteria.set(GSimpleGA.FitnessStatsCriteria)
GA Engine Class - The Genetic Algorithm Core
>>> ga = GSimpleGA.GSimpleGA(genome)
>>> ga.selector.set(Selectors.GRouletteWheel)
>>> ga.setGenerations(120)
>>> ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
Parameters: |
|
---|
Note
if you use the same ramdom seed, all the runs of algorithm will be the same
Returns the population best individual
Return type: | the best individual |
---|
Do all the generations until the termination criteria, accepts the freq_stats (default is 0) to dump statistics at n-generation
>>> ga_engine.evolve(freq_stats=10)
(...)
Parameter: | freq_stats – if greater than 0, the statistics will be printed every freq_stats generation. |
---|
Gets the current generation
Return type: | the current generation |
---|
Gets the DB Adapter of the GA Engine
Return type: | a instance from one of the DBAdapters classes |
---|
Gets the minimize/maximize mode
Return type: | the Consts.minimaxType type |
---|
Return the internal population of GA Engine
Return type: | the population (GPopulation.GPopulation) |
---|
Gets the Statistics class instance of current generation
Return type: | the statistics instance (Statistics.Statistics) |
---|
Select one individual from population
Parameter: | args – this parameters will be sent to the selector |
---|
This is the function slot for the selection method if you want to change the default selector, you must do this:
ga_engine.selector.set(Selectors.GRouletteWheel)
Sets the crossover rate, between 0.0 and 1.0
Parameter: | rate – the rate, between 0.0 and 1.0 |
---|
Sets the DB Adapter of the GA Engine
Parameter: | dbadapter – one of the DBAdapters classes instance |
---|
Warning
the use the of a DB Adapter can reduce the speed performance of the Genetic Algorithm.
Sets the elitism option, True or False
Parameter: | flag – True or False |
---|
Sets the number of generations to evolve
Parameter: | num_gens – the number of generations |
---|
Sets the minimize/maximize mode, use Consts.minimaxType
Parameter: | mtype – the minimax mode, from Consts.minimaxType |
---|
Sets the mutation rate, between 0.0 and 1.0
Parameter: | rate – the rate, between 0.0 and 1.0 |
---|
Sets the population size, calls setPopulationSize() of GPopulation
Parameter: | size – the population size |
---|
Note
the population size must be >= 2
Sets the sort type, Consts.sortType[“raw”]/Consts.sortType[“scaled”]
>>> ga_engine.setSortType(Consts.sortType["scaled"])
Parameter: | sort_type – the Sort Type |
---|
This is the step callback function slot, if you want to set the function, you must do this:
def your_func(ga_engine):
# Here you have access to the GA Engine
return False
ga_engine.stepCallback.set(your_func)
now “your_func” will be called every generation. When this function returns True, the GA Engine will stop the evolution and show a warning, if is False, the evolution continues.
This is the termination criteria slot, if you want to set one termination criteria, you must do this:
ga_engine.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
Now, when you run your GA, it will stop when the population converges.
There are those termination criteria functions: GSimpleGA.RawScoreCriteria(), GSimpleGA.ConvergenceCriteria(), GSimpleGA.RawStatsCriteria(), GSimpleGA.FitnessStatsCriteria()
But you can create your own termination function, this function receives one parameter which is the GA Engine, follows an example:
def ConvergenceCriteria(ga_engine):
pop = ga_engine.getPopulation()
return pop[0] == pop[len(pop)-1]
When this function returns True, the GA Engine will stop the evolution and show a warning, if is False, the evolution continues, this function is called every generation.
Terminate the evolution using the bestRawScore parameter obtained from the individual
>>> genome.setParams(bestRawScore=0.00, roundDecimal=2)
(...)
>>> ga_engine.terminationCriteria.set(GSimpleGA.RawScoreCriteria)
Terminate the evolution based on the raw stats
>>> ga_engine.terminationCriteria.set(GSimpleGA.RawStatsCriteria)