Population - danielwilczak101/EasyGA GitHub Wiki

A population is a collection of chromosomes and stores all of the data in a genetic algorithm. Aside from storing the chromosomes representing the population, two separate collections of chromosomes are stored for intermediate calculations for the genetic algorithm. Additionally, many built-in methods are provided to allow easier usage, such as printing.

Relation to GA object

The GA object holds one population, given by ga.population. This is the object which stores the data for the genetic algorithm.

Initialization

A population may be initialized using population = ga.make_population(chromosomes). This creates a population holding the specified chromosomes. How the chromosomes are stored does not matter, but it must an iterable object, such a list of chromosomes or another population. The chromosomes are then stored in the population as population.chromosome_list.

Note: It is not necessary for the chromosomes to be provided as chromosome objects. Either way, they will be cast to chromosomes upon initialization.

>>> population = ga.make_population([
...     [1, 2, 3],
...     [4, 5, 6],
...     [7, 8, 9]
... ])
>>> print(population)
Chromosome - 0 [1][2][3] / Fitness = None
Chromosome - 1 [4][5][6] / Fitness = None
Chromosome - 2 [7][8][9] / Fitness = None

Attributes

A population object has 3 attributes. The main chromosome list is given by population.chromosome_list. The chromosomes selected as parents are contained in population.mating_pool. The chromosomes selected to live on to the next generation are contained in population.next_population. The mating pool and next population are only non-empty during the ga.evolve method.

>>> population = ga.make_population([
...     [1, 2, 3],
...     [4, 5, 6],
...     [7, 8, 9]
... ])
>>> population.chromosome_list
[EasyGA.make_chromosome([EasyGA.make_gene(1), EasyGA.make_gene(2), EasyGA.make_gene(3)]), EasyGA.make_chromosome([EasyGA.make_gene(4), EasyGA.make_gene(5), EasyGA.make_gene(6)]), EasyGA.make_chromosome([EasyGA.make_gene(7), EasyGA.make_gene(8), EasyGA.make_gene(9)])]
>>> population.mating_pool
[]
>>> population.next_population
[]

Additional Methods

The population contains many methods that allow for easier usage. We recommend using these methods instead of interacting with the lists whenever convenient.