def iteration(self, i): parents = range(self.__nrIndividuals) #two parent => one child nrChildren = len(parents) // 2 #generate the population of offsprings offspringPopulation = Population(nrChildren) #create the offsprings for i in range(nrChildren): #random indexes for the parents crossover = False while not crossover: i1 = random.randint(0, self.__nrIndividuals - 1) i2 = random.randint(0, self.__nrIndividuals - 1) #select parents if (i1 != i2): crossover = True #combine parents parent1 = self.population.individuals[i1] parent2 = self.population.individuals[i2] offspringPopulation.individuals[i] = Chromosome.crossover( parent1, parent2, self.probability_crossover) #mutate offspring offspringPopulation.individuals[i].mutate( self.probability_mutate) #evaluating the offspring population offspringPopulation.evaluate(self.inputTrain, self.outputTrain) #we add the population of children to the population of parents self.population.reunion(offspringPopulation) #select tot nrIndividuals from the new population, i.e. only the best individuals survive to the next generation self.population.selection(self.__nrIndividuals)
def test_randSinglePoint(self): parents = (Chromosome([1, 1, 1, 1, 1, 1]), Chromosome([0, 0, 0, 0, 0, 0])) results = (Chromosome([1, 1, 1, 1, 0, 0]), Chromosome([0, 0, 0, 0, 1, 1])) children = Chromosome.crossover(parents, seed=5) self.assertEqual(len(parents[0]), len(children[0]), "Children are not same length") self.assertEqual(len(parents[1]), len(children[1]), "Children are not same length") self.assertEqual(results, children, "Incorrect result")
def iteration(self, i): parents = range(self.nrInd) nrChilds = len(parents) // 2 offspring = Population(nrChilds, self.depht // 2) for i in range(nrChilds): ch = Chromosome(self.depht) offspring.arr[i] = ch.crossover(self.pop.arr[i << 1], self.pop.arr[(i << 1) | 1]) offspring.arr[i].mutate(self.probability_mutate) offspring.fitness_eval(self.xTrain, self.yTrain) self.pop.reunion(offspring) self.pop.selection(self.nrInd)
def iteration(self, i): parents = range(self.nrInd) nrChildren = len(parents) // 2 offspring = Population(nrChildren) for i in range(nrChildren): offspring.individuals[i] = Chromosome.crossover( self.population.individuals[i << 1], self.population.individuals[(i << 1) | 1], self.probability_crossover) offspring.individuals[i].mutate(self.probability_mutate) offspring.evaluate(self.inputTrain, self.outputTrain) self.population.reunion(offspring) self.population.selection(self.nrInd)
def iteration(self, i): offspring = Population(self.nrOfIndividuals) for child in range(self.nrOfIndividuals): first_pick = random.randint(0, self.nrOfIndividuals) second_pick = first_pick while second_pick == first_pick: second_pick = random.randint(0, self.nrOfIndividuals) offspring.individuals[child] = Chromosome.crossover( self.population.individuals[first_pick], self.population.individuals[second_pick], self.crossoverProbability) offspring.individuals[child].mutate(self.mutationProbability) offspring.evaluate(self.inputTraining, self.outputTraining) self.population.reunion(offspring) self.population.selection(self.nrOfIndividuals)
print "Best Four Chromosomes in Gen",i+1 for i in range(4): index = Chromosome.getFittestChromosome(initialPopulation) nextPopulation.append(initialPopulation[index]) print initialPopulation[index].rankString initialPopulation.pop(index) print "Undergoing Crossover and Mutation" for i in range(totalPopulation/2 - 2): firstParentIndex = random.randrange(0,len(initialPopulation),1) firstParent = initialPopulation[firstParentIndex] initialPopulation.pop(firstParentIndex) secondParentIndex = random.randrange(0,len(initialPopulation),1) secondParent = initialPopulation[secondParentIndex] initialPopulation.pop(secondParentIndex) result = Chromosome.crossover(firstParent,secondParent) result[0].mutate(maxRank) result[1].mutate(maxRank) nextPopulation = nextPopulation + result initialPopulation = nextPopulation finalPopulation = Chromosome.calculateProbability(initialPopulation,userChromosome) print "Final Population" Chromosome.printPopulation(finalPopulation) print "--------" print "Best Chromosome : ", finalPopulation[Chromosome.getFittestChromosome(finalPopulation)].rankString
print "Best Four Chromosomes in Gen",i+1 for i in range(4): index = Chromosome.getFittestChromosome(initialPopulation) nextPopulation.append(initialPopulation[index]) print initialPopulation[index].rankString initialPopulation.pop(index) print "Undergoing Crossover and Mutation" for i in range(totalPopulation/2 - 2): firstParentIndex = random.randrange(0,len(initialPopulation),1) firstParent = initialPopulation[firstParentIndex] initialPopulation.pop(firstParentIndex) secondParentIndex = random.randrange(0,len(initialPopulation),1) secondParent = initialPopulation[secondParentIndex] initialPopulation.pop(secondParentIndex) result = Chromosome.crossover(firstParent,secondParent) result[0].mutate(maxRank) result[1].mutate(maxRank) nextPopulation = nextPopulation + result initialPopulation = nextPopulation finalPopulation = Chromosome.calculateProbability(initialPopulation,userChromosome) print "Final Population" Chromosome.printPopulation(finalPopulation) print "--------" print "Best Chromosome : ", finalPopulation[Chromosome.getFittestChromosome(finalPopulation)].rankString