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:
xxxxxxxxxx82
print(bestFitness)import numpy# Parameter initialization genes = 2chromosomes = 10mattingPoolSize = 6offspringSize = chromosomes - mattingPoolSizelb = -5ub = 5populationSize = (chromosomes, genes)generations = 3#Population initializationpopulation = 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


