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.
Number of Generations
Default is 100 generations
Mutation Rate
Default is 0.02, which represents 0.2%
Crossover Rate
Default is 0.9, which represents 90%
Elitism Replacement
Default is 1 individual
Population Size
Default is 80 individuals
Minimax
>>> Consts.minimaxType["maximize"]Maximize the evaluation function
DB Adapter
Default is None
Migration Adapter
Default is None
Interactive Mode
Default is True
Selector (Selection Method)
The Rank Selection method
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 random 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. |
---|---|
Return type: | returns the best individual of the evolution |
New in version 0.6: the return of the best individual
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 |
---|
Get the Genetic Programming mode of the GA Engine
Return type: | True or False |
---|
Return the number of generations to evolve
Return type: | the number of generations |
---|
New in version 0.6: Added the getGenerations method
returns the generation in which the GA must enter in the Interactive Mode
Return type: | the generation number or -1 if not set |
---|
Gets the minimize/maximize mode
Return type: | the Consts.minimaxType type |
---|
Gets an internal parameter
>>> ga.getParam("gp_terminals")
['x', 'y']
Parameters: |
|
---|
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) |
---|
Print generation statistics
Return type: | the printed statistics as string |
---|
Changed in version 0.6: The return of printStats method.
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 |
---|
Set the number of best individuals to copy to the next generation on the elitism
Parameter: | numreplace – the number of individuals |
---|
New in version 0.6: The setElitismReplacement method.
Sets the Genetic Programming mode of the GA Engine
Parameter: | bool_value – True or False |
---|
Sets the number of generations to evolve
Parameter: | num_gens – the number of generations |
---|
Sets the generation in which the GA must enter in the Interactive Mode
Parameter: | generation – the generation number, use “-1” to disable |
---|
Enable/disable the interactive mode
Parameter: | flag – True or False |
---|
Sets the Migration Adapter
New in version 0.6: The setMigrationAdapter method.
Sets the minimize/maximize mode, use Consts.minimaxType
Parameter: | mtype – the minimax mode, from Consts.minimaxType |
---|
Sets the flag to enable/disable the use of python multiprocessing module. Use this option when you have more than one core on your CPU and when your evaluation function is very slow.
Pyevolve will automaticly check if your Python version has multiprocessing support and if you have more than one single CPU core. If you don’t have support or have just only one core, Pyevolve will not use the multiprocessing feature.
Pyevolve uses the multiprocessing to execute the evaluation function over the individuals, so the use of this feature will make sense if you have a truly slow evaluation function (which is commom in GAs).
The parameter “full_copy” defines where the individual data should be copied back after the evaluation or not. This parameter is useful when you change the individual in the evaluation function.
Parameters: |
|
---|
Warning
Use this option only when your evaluation function is slow, so you’ll get a good tradeoff between the process communication speed and the parallel evaluation. The use of the multiprocessing doesn’t means always a better performance.
Note
To enable the multiprocessing option, you MUST add the __main__ check on your application, otherwise, it will result in errors. See more on the Python Docs site.
New in version 0.6: The setMultiProcessing method.
Sets the mutation rate, between 0.0 and 1.0
Parameter: | rate – the rate, between 0.0 and 1.0 |
---|
Set the internal params
>>> ga.setParams(gp_terminals=['x', 'y'])
Parameter: | args – params to save |
---|
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 and rounddecimal 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)