def select(self, sorted_chromosome_fitness, k): if k >= len(sorted_chromosome_fitness) and self._unique: AttributeError("Not enough alternatives in population") total_fitness = sum(fitness for _, fitness in sorted_chromosome_fitness) selected = [] while len(selected) < k: random_threshold = seeded_random.random() * total_fitness acc = 0 for chromosome, fitness in sorted_chromosome_fitness: acc += fitness if acc > random_threshold and not (chromosome in selected and self._unique): selected.append(chromosome) break return selected
def _is_mutating(self): return seeded_random.random() < self.mutation_rate