Summary
Chromosome.mutate(method="probability_mutation", mutation_prob=0) currently behaves as if mutation_prob was omitted, because the defaulting logic uses a truthiness check:
mutation_prob = mutation_prob if mutation_prob else 1 / len(self.data)
This makes 0 fall back to 1 / len(self.data) instead of disabling probability-based mutation.
Why this matters
A caller may reasonably pass mutation_prob=0 to run a generation with no random gene changes, for example when comparing crossover-only behavior or making deterministic tests. The current implementation still mutates genes with the default probability.
Location
genetic_algorithm/chromosome.py:174-177
- Existing mutation tests in
tests/test_chromosome.py do not cover the zero-probability case.
Suggested fix
Default only when the argument is actually omitted:
mutation_prob = mutation_prob if mutation_prob is not None else 1 / len(self.data)
Add a regression test that creates a chromosome with known data, calls mutate(method="probability_mutation", mutation_prob=0), and asserts the data remains unchanged.
Summary
Chromosome.mutate(method="probability_mutation", mutation_prob=0)currently behaves as ifmutation_probwas omitted, because the defaulting logic uses a truthiness check:This makes
0fall back to1 / len(self.data)instead of disabling probability-based mutation.Why this matters
A caller may reasonably pass
mutation_prob=0to run a generation with no random gene changes, for example when comparing crossover-only behavior or making deterministic tests. The current implementation still mutates genes with the default probability.Location
genetic_algorithm/chromosome.py:174-177tests/test_chromosome.pydo not cover the zero-probability case.Suggested fix
Default only when the argument is actually omitted:
Add a regression test that creates a chromosome with known data, calls
mutate(method="probability_mutation", mutation_prob=0), and asserts the data remains unchanged.