GSimpleGA – the genetic algorithm by itself

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.

Default Parameters

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)

Selectors.GRankSelector()

The Rank Selection method

Class

GSimpleGA.ConvergenceCriteria(ga_engine)

Terminate the evolution when the population have converged

Example:
>>> ga_engine.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
GSimpleGA.FitnessStatsCriteria(ga_engine)

Terminate the evoltion based on the fitness stats

Example:
>>> ga_engine.terminationCriteria.set(GSimpleGA.FitnessStatsCriteria)
class GSimpleGA.GSimpleGA(genome, seed=None, interactiveMode=True)

GA Engine Class - The Genetic Algorithm Core

Example:
>>> ga = GSimpleGA.GSimpleGA(genome)
>>> ga.selector.set(Selectors.GRouletteWheel)
>>> ga.setGenerations(120)
>>> ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
Parameters:
  • genome – the Sample Genome
  • interactiveMode – this flag enables the Interactive Mode, the default is True
  • seed – the random seed value

Note

if you use the same random seed, all the runs of algorithm will be the same

bestIndividual()

Returns the population best individual

Return type:the best individual
clear()
Petrowski’s Clearing Method
dumpStatsDB()
Dumps the current statistics to database adapter
evolve(freq_stats=0)

Do all the generations until the termination criteria, accepts the freq_stats (default is 0) to dump statistics at n-generation

Example:
>>> 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

getCurrentGeneration()

Gets the current generation

Return type:the current generation
getDBAdapter()

Gets the DB Adapter of the GA Engine

Return type:a instance from one of the DBAdapters classes
getGPMode()

Get the Genetic Programming mode of the GA Engine

Return type:True or False
getGenerations()

Return the number of generations to evolve

Return type:the number of generations

New in version 0.6: Added the getGenerations method

getInteractiveGeneration()

returns the generation in which the GA must enter in the Interactive Mode

Return type:the generation number or -1 if not set
getMinimax()

Gets the minimize/maximize mode

Return type:the Consts.minimaxType type
getParam(key, nvl=None)

Gets an internal parameter

Example:
>>> ga.getParam("gp_terminals")
['x', 'y']
Parameters:
  • key – the key of param
  • nvl – if the key doesn’t exist, the nvl will be returned
..versionaddd:: 0.6
Added the getParam method.
getPopulation()

Return the internal population of GA Engine

Return type:the population (GPopulation.GPopulation)
getStatistics()

Gets the Statistics class instance of current generation

Return type:the statistics instance (Statistics.Statistics)
initialize()
Initializes the GA Engine. Create and initialize population
printStats()

Print generation statistics

Return type:the printed statistics as string

Changed in version 0.6: The return of printStats method.

printTimeElapsed()
Shows the time elapsed since the begin of evolution
select(**args)

Select one individual from population

Parameter:args – this parameters will be sent to the selector
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) 
setCrossoverRate(rate)

Sets the crossover rate, between 0.0 and 1.0

Parameter:rate – the rate, between 0.0 and 1.0
setDBAdapter(dbadapter=None)

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.

setElitism(flag)

Sets the elitism option, True or False

Parameter:flag – True or False
setElitismReplacement(numreplace)

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.

setGPMode(bool_value)

Sets the Genetic Programming mode of the GA Engine

Parameter:bool_value – True or False
setGenerations(num_gens)

Sets the number of generations to evolve

Parameter:num_gens – the number of generations
setInteractiveGeneration(generation)

Sets the generation in which the GA must enter in the Interactive Mode

Parameter:generation – the generation number, use “-1” to disable
setInteractiveMode(flag=True)

Enable/disable the interactive mode

Parameter:flag – True or False
setMigrationAdapter(migration_adapter=None)

Sets the Migration Adapter

New in version 0.6: The setMigrationAdapter method.

setMinimax(mtype)

Sets the minimize/maximize mode, use Consts.minimaxType

Parameter:mtype – the minimax mode, from Consts.minimaxType
setMultiProcessing(flag=True, full_copy=False)

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:
  • flag – True (default) or False
  • full_copy – True or False (default)

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.

setMutationRate(rate)

Sets the mutation rate, between 0.0 and 1.0

Parameter:rate – the rate, between 0.0 and 1.0
setParams(**args)

Set the internal params

Example:
>>> ga.setParams(gp_terminals=['x', 'y'])
Parameter:args – params to save
..versionaddd:: 0.6
Added the setParams method.
setPopulationSize(size)

Sets the population size, calls setPopulationSize() of GPopulation

Parameter:size – the population size

Note

the population size must be >= 2

setSortType(sort_type)

Sets the sort type, Consts.sortType[“raw”]/Consts.sortType[“scaled”]

Example:
>>> ga_engine.setSortType(Consts.sortType["scaled"])
Parameter:sort_type – the Sort Type
step()
Just do one step in evolution, one generation
stepCallback

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.

terminationCriteria

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.

GSimpleGA.RawScoreCriteria(ga_engine)

Terminate the evolution using the bestrawscore and rounddecimal parameter obtained from the individual

Example:
>>> genome.setParams(bestrawscore=0.00, rounddecimal=2)
(...)
>>> ga_engine.terminationCriteria.set(GSimpleGA.RawScoreCriteria)
GSimpleGA.RawStatsCriteria(ga_engine)

Terminate the evolution based on the raw stats

Example:
>>> ga_engine.terminationCriteria.set(GSimpleGA.RawStatsCriteria)


Table Of Contents

Previous topic

Statistics – statistical structure module

Next topic

GPopulation – the population module

This Page