def create_offspring(self, P, parent1, parent2): offspring = Individual(P, parent1.eco_type) offspring.crossover(parent1.genome, parent2.genome) num_mutations = self.poisson_dist(10**((-1) * P.mutation_rate), offspring.genome_size()) offspring.mutation( num_mutations ) # check to mutate the offspring's genome (small chance of mutation) return offspring
def update_generation(self): self.individuals.sort(key=operator.attrgetter('fitness')) parents = self.individuals[self.size - self.keep:] distribution = self.__selection_distribution(parents) for i in range(0, self.keep): parent1 = self.__select_parent(distribution, parents) parent2 = self.__select_parent(distribution, parents) child = Individual.crossover(parent1, parent2) child.mutate() self.individuals[i] = child
from individual import Individual print("an individual") myInd = Individual() print(myInd) print("mutated") myInd.mutate() print(myInd) print("another individual") myInd2 = Individual() print(myInd2) print("Offspring") offspring = myInd.crossover(myInd2) print(offspring) offspring = myInd.crossover(myInd2) print(offspring)