Skip to content

mutation_prob=0 is ignored by probability_mutation #14

Description

@alerodriargui

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions