Pseudo Code Outline - petecallaghan/PopulationFitness GitHub Wiki
Overview
- Create the initial population
- Simulate generations for the epochs with known historical population sizes
- Simulate generations for the epochs under test
- Write the results to file
Creating the initial population:
// Create a set of individuals with randomly generated genes
for each individual up to the initial population size
create a new individual with all zeros gene encoding
for each bit in the individual's gene encoding
if random(0, 1) < 0.5
flip the bit
Simulating Generations:
// Loop through epochs and then years within epochs
for each epoch in the simulation
for each year in the epoch
// kill those unfit or ready to die
for each individual in the population
if age > max age then kill individual
calculate individual fitness for the epoch fitness factor
if the epoch is historical
// Adjust fitness to historical population`
if (individual fitness * (epoch target population size / population size) ) < ( random(0,1) * epoch kill constant )
kill individual
else
if individual fitness < random(0,1) * epoch kill constant
kill individual
// add new generation
for each adjacent pair of individuals as father and mother
if random(0,1) < epoch breeding probability and father is of breeding age and mother is of breeding age
new individual inherits from father and mother
add new individual to the end of the list of individuals
Calculating fitness:
Convert the genetic encoding from an array of bits into an array of real numbers
Calculate the fitness from these real numbers * epoch fitness factor
Inheritance:
// Combine genes from father and mother with randomly selected cross over point
gene cross over index = random(0, number of bits in the gene)
copy (to new genes, from mother[0 to cross over index])
copy (to end of new genes, from father [cross over index to end])
mutate a random selection of bits in the new gene
Note:
- Epochs are consecutive years that have the same properties, such as the kill constants, breeding probability and target population size.
- Pseudo-random number sequences are used for selecting individuals to kill, pairs to breed, inheritance cross-over point, gene bits to mutate and to generate the genetic coding of the initial population.