diff --git a/genetic_algorithm/chromosome.py b/genetic_algorithm/chromosome.py index 09454eb..aa65a88 100644 --- a/genetic_algorithm/chromosome.py +++ b/genetic_algorithm/chromosome.py @@ -173,7 +173,7 @@ def __probability_mutation(self, probability): def mutate(self, method="probability_mutation", mutation_prob=None): if method == "probability_mutation": - mutation_prob = mutation_prob if mutation_prob else 1 / len(self.data) + mutation_prob = mutation_prob if mutation_prob is not None else 1 / len(self.data) return self.__probability_mutation(mutation_prob) elif method == "twors": return self.__twors_mutate() diff --git a/tests/test_chromosome.py b/tests/test_chromosome.py index 4c2d0dc..354bf7b 100644 --- a/tests/test_chromosome.py +++ b/tests/test_chromosome.py @@ -55,6 +55,13 @@ def test_mutation_methods_preserve_length(method): assert len(out) == 6 +def test_probability_mutation_allows_zero_probability(): + random.seed(1) + c = Chromosome(3, [(0, 0)] * 3, data=[1.0, 2.0, 3.0]) + out = c.mutate(method="probability_mutation", mutation_prob=0) + assert out == [1.0, 2.0, 3.0] + + def test_permutation_mutations_preserve_multiset(): c = Chromosome(5, [(0, 100)] * 5, data=[1.0, 2.0, 3.0, 4.0, 5.0]) random.seed(2)