Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion genetic_algorithm/chromosome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/test_chromosome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading