def setUp(self): self.pop = createPop() rand_individual = self.pop[random.randint(0, len(self.pop))] self.ch1 = [rand_individual.wi, rand_individual.wo] rand_individual = self.pop[random.randint(0, len(self.pop))] self.ch2 = [rand_individual.wi, rand_individual.wo] self.o1, self.o2 = crossover(self.ch1, self.ch2, NN)
def evolveNewPop(rankedPop): """ rankedPop is zip(weights, errors, fitnesses) ordered in ascending order of fitness """ rankedWeights = [item[0] for item in rankedPop] fitnessScores = [item[-1] for item in rankedPop] newpopW = deepcopy(rankedWeights[:NN.eliteN]) while len(newpopW) < NN.pop_size: ch1, ch2 = selectTwoIndividuals(fitnessScores, rankedWeights) if random.random() <= NN.crossover_rate: ch1, ch2 = crossover(ch1, ch2, NN) mutate(ch1, NN.mutation_rate) mutate(ch2, NN.mutation_rate) newpopW.append(ch1) newpopW.append(ch2) return newpopW[:NN.pop_size]