Skip to content

Swap 2 random genes in swap_mutation, as documented - #375

Open
tachsin wants to merge 1 commit into
ahmedfgad:masterfrom
tachsin:fix-swap-mutation
Open

tachsin wants to merge 1 commit into
ahmedfgad:masterfrom
tachsin:fix-swap-mutation

Conversation

@tachsin

@tachsin tachsin commented Sep 28, 2026

Copy link
Copy Markdown

Fixes #374.

swap_mutation() picked its first gene at random and took the gene num_genes // 2 after it as the second, so only num_genes // 2 pairs could be swapped. The docs say it "interchanges the values of 2 randomly selected genes"; this makes the code do that, with numpy.random.choice(num_genes, size=2, replace=False), and updates the docstring.

Test: test_swap_mutation_manual_call in tests/test_crossover_mutation.py swaps 1000 copies of range(6) and checks that each has exactly 2 genes exchanged and that all 15 pairs occur. It fails on master (3 pairs) and passes with this change.

pytest tests (without test_kerasga.py and test_torchga.py, which need TensorFlow and PyTorch): 872 passed, 1 skipped. The only failure is test_submodule_versions.py::test_changed_submodule_is_version_bumped[utils], since pygad/utils changed; I left the version bump to you for the release.

The second gene was always num_genes // 2 after the first, so only
num_genes // 2 pairs of genes could be swapped. Pick 2 different genes at
random, as the docs say.

Fixes ahmedfgad#374

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

swap_mutation only swaps genes num_genes // 2 apart; the docs say it swaps 2 random genes

1 participant