GPopulation – the population module

This module contains the GPopulation.GPopulation class, which is reponsible to keep the population and the statistics.

Default Parameters

Sort Type

>>> Consts.sortType["scaled"]

The scaled sort type

Minimax

>>> Consts.minimaxType["maximize"]

Maximize the evaluation function

Scale Method

Scaling.LinearScaling()

The Linear Scaling scheme

Class

class GPopulation.GPopulation(genome)

GPopulation Class - The container for the population

Examples
Get the population from the GSimpleGA.GSimpleGA (GA Engine) instance
>>> pop = ga_engine.getPopulation()
Get the best fitness individual
>>> bestIndividual = pop.bestFitness()
Get the best raw individual
>>> bestIndividual = pop.bestRaw()
Get the statistics from the Statistics.Statistics instance
>>> stats = pop.getStatistics()
>>> print stats["rawMax"]
10.4
Iterate, get/set individuals
>>> for ind in pop:
>>>   print ind
(...)
>>> for i in xrange(len(pop)):
>>>    print pop[i]
(...)
>>> pop[10] = newGenome
>>> pop[10].fitness
12.5
Parameters:genome – the Sample genome, or a GPopulation object, when cloning.
bestFitness(index=0)

Return the best scaled fitness individual of population

Parameters:index – the index best individual
Return type:the individual
bestRaw(index=0)

Return the best raw score individual of population

Parameters:index – the index best raw individual
Return type:the individual

New in version 0.6: The parameter index.

clear()

Remove all individuals from population

clearFlags()

Clear the sorted and statted internal flags

clone()

Return a brand-new cloned population

copy(pop)

Copy current population to ‘pop’

Parameters:pop – the destination population

Warning

this method do not copy the individuals, only the population logic

create(**args)

Clone the example genome to fill the population

evaluate(**args)

Evaluate all individuals in population, calls the evaluate() method of individuals

Parameters:args – this params are passed to the evaluation function
getParam(key, nvl=None)

Gets an internal parameter

Example:
>>> population.getParam("tournamentPool")
5
Parameters:
  • key – the key of param
  • nvl – if the key doesn’t exist, the nvl will be returned
getStatistics()

Return a Statistics class for statistics

Return type:the Statistics.Statistics instance
initialize(**args)

Initialize all individuals of population, this calls the initialize() of individuals

printStats()

Print statistics of the current population

scale(**args)

Scale the population using the scaling method

Parameters:args – this parameter is passed to the scale method
setMinimax(minimax)

Sets the population minimax

Example:
>>> pop.setMinimax(Consts.minimaxType["maximize"])
Parameters:minimax – the minimax type
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. 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, se you will get a good tradeoff between the process communication speed and the parallel evaluation.

New in version 0.6: The setMultiProcessing method.

setParams(**args)

Gets an internal parameter

Example:
>>> population.setParams(tournamentPool=5)
Parameters:args – parameters to set

New in version 0.6: The setParams method.

setPopulationSize(size)

Set the population size

Parameters:size – the population size
setSortType(sort_type)

Sets the sort type

Example:
>>> pop.setSortType(Consts.sortType["scaled"])
Parameters:sort_type – the Sort Type
sort()

Sort the population

statistics()

Do statistical analysis of population and set ‘statted’ to True

GPopulation.key_fitness_score(individual)

A key function to return fitness score, used by max()/min()

Parameters:individual – the individual instance
Return type:the individual fitness score

Note

this function is used by the max()/min() python functions

GPopulation.key_raw_score(individual)

A key function to return raw score

Parameters:individual – the individual instance
Return type:the individual raw score

Note

this function is used by the max()/min() python functions

GPopulation.multiprocessing_eval(ind)

Internal used by the multiprocessing

GPopulation.multiprocessing_eval_full(ind)

Internal used by the multiprocessing (full copy)