(self, iterations)
| 58 | return child1, child2 |
| 59 | |
| 60 | def run(self, iterations): |
| 61 | self._initialize() |
| 62 | |
| 63 | for epoch in range(iterations): |
| 64 | population_fitness = self._calculate_fitness() |
| 65 | |
| 66 | fittest_individual = self.population[np.argmax(population_fitness)] |
| 67 | highest_fitness = max(population_fitness) |
| 68 | if fittest_individual == self.target: |
| 69 | break |
| 70 | |
| 71 | parent_probabilities = [fitness / sum(population_fitness) for fitness in population_fitness] |
| 72 | |
| 73 | new_population = [] |
| 74 | for i in np.arange(0, self.population_size, 2): |
| 75 | parent1, parent2 = np.random.choice(self.population, size=2, p=parent_probabilities, replace=False) |
| 76 | child1, child2 = self._crossover(parent1, parent2) |
| 77 | new_population += [self._mutate(child1), self._mutate(child2)] |
| 78 | |
| 79 | print ("[%d Closest Candidate: '%s', Fitness: %.2f]" % (epoch, fittest_individual, highest_fitness)) |
| 80 | self.population = new_population |
| 81 | |
| 82 | print ("[%d Answer: '%s']" % (epoch, fittest_individual)) |
| 83 | |
| 84 |
nothing calls this directly
no test coverage detected