def test_documented_best(self): pop = Population(chromosomes=[100, 100, 100], eval_function=lambda x: x * 2, maximize=True) assert pop.documented_best is None pop.evaluate() assert pop.documented_best.fitness == pop.current_best.fitness pop.mutate(mutate_function=lambda x: x - 10, probability=1).evaluate() assert pop.documented_best.fitness - 20 == pop.current_best.fitness
def test_mutate_func(self): def mutate_func(x): return -x population = Population([1] * 100, eval_function=lambda x: x) population.mutate(mutate_func) for chromosome in population.chromosomes: assert chromosome == -1 assert len(population) == 100
def test_mutate_resets(self): pop = Population(chromosomes=[1, 1, 1], eval_function=float, maximize=True) assert pop.current_best is None and pop.current_worst is None pop.evaluate() assert pop.current_best.fitness == 1 and pop.current_worst.fitness == 1 pop.mutate(lambda x: x) assert pop.current_best is None and pop.current_worst is None
def test_mutate_inplace(self): pop = Population([1] * 100, eval_function=lambda x: x) pop.mutate(lambda x: x + 1) for chromosome in pop.chromosomes: assert chromosome == 2