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.

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 ramdom seed, all the runs of algorithm will be the same

bestIndividual()

Returns the population best individual

Return type:the best individual
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.
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
getMinimax()

Gets the minimize/maximize mode

Return type:the Consts.minimaxType type
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
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)

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
setGenerations(num_gens)

Sets the number of generations to evolve

Parameter:num_gens – the number of generations
setMinimax(mtype)

Sets the minimize/maximize mode, use Consts.minimaxType

Parameter:mtype – the minimax mode, from Consts.minimaxType
setMutationRate(rate)

Sets the mutation rate, between 0.0 and 1.0

Parameter:rate – the rate, between 0.0 and 1.0
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 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)

Recent Blog Posts

Previous topic

Statistics – statistical structure module

Next topic

GPopulation – the population module

This Page