Termination criteria
All steps from fitness calculation to environmental selection will be repeated in a loop whereas the total number of iterations will be equal to the variable generations. The complete algorithm is given below:
xxxxxxxxxx
82
print(bestFitness)
import numpy
# Parameter initialization
genes = 2
chromosomes = 10
mattingPoolSize = 6
offspringSize = chromosomes - mattingPoolSize
lb = -5
ub = 5
populationSize = (chromosomes, genes)
generations = 3
#Population initialization
population = numpy.random.uniform(lb, ub, populationSize)
for generation in range(generations):
print(("Generation:", generation+1))
fitness = numpy.sum(population*population, axis=1)
print("\npopulation")
print(population)
print("\nfitness calcuation")
print(fitness)
# Following statement will create an empty two dimensional array to store parents
parents = numpy.empty((mattingPoolSize, population.shape[1]))
# A loop to extract one parent in each iteration
for p in range(mattingPoolSize):
# Finding index of fittest chromosome in the population
fittestIndex = numpy.where(fitness == numpy.max(fitness))
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment