import numpy genes = 2 chromosomes = 10 mattingtoolsize = 6 offspringsize = chromosomes - mattingpoolsize lb = -5 ub = 5 populationsize = (chromosomes, genes) generations = 3 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 calculation") print(fitness) parents = numpy.empty((mattingpoolsize, population.shape[1])) for p in range(mattingpoolsize): fittestindex = numpy.where(fitness == numpy.max(fitness)) fittestindex = fittestindex[0][0] parents[p,:]=population[fittestindex, :] fitness[fittestindex] = -1 print("\nParents :") print(parents) offspring = numpy.empty((offspringsize, population.shape[1])) for k in range(offspringsize): crossoverpoint = numpy.random.randint(0,genes) parent1index = k%parents.shape[0] parent2index = (k+1)%parents.shape[0] offspring[k,0:crossoverpoint] = parents[parent1index, 0: crossoverpoint] offspring[k, crossoverpoint:] = parents[parent2index, crossoverpoint:]