def mate(self, mom, dad): # make empty child child = myChromosome(self.chromosomeLength, True) # select random spot in chromosome spot = randint(0, self.chromosomeLength) # add the first half mom to the kid and the second half of dad child.chromo[0:spot] = mom.chromo[0:spot] child.chromo[spot:] = dad.chromo[spot:] self.findFitness(child) return child
def __init__(self, _cl, _popSize, _numberOfGenerations, _maxMutations=5, _mutateChance=5, _maxFitness=20): # init constants self.thisGeneration = 0 # set values to parameters self.chromosomeLength = _cl self.maxGenerations = _numberOfGenerations self.populationSize = _popSize self.maxFitness = _maxFitness self.mutateChance = _mutateChance self.maxMutations = _maxMutations # init internal varibles self.population = [myChromosome(self.chromosomeLength)] * self.populationSize self.newPopulation = [] # finds fitness map(self.findFitness, self.population)