Conversation
With allow_duplicate_genes=False and a gene space of num_genes values, as for permutations, every value of the space is used, so a mutated gene kept its value and the mutation never changed a solution. In the 4 mutations by space, swap the gene with another gene whose value is in its space and whose space holds its value instead. Fixes ahmedfgad#372
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #372.
With
allow_duplicate_genes=Falseand a gene space ofnum_genesvalues (the permutation setup ofpygad.benchmarks.tsp), every value of the space is used, soselect_unique_value()kept the gene's value and the random mutation never changed a solution.This adds
swap_gene_by_space()to theMutationclass. In the 4 mutations by space (mutation_by_space,mutation_probs_by_space,adaptive_mutation_by_space,adaptive_mutation_probs_by_space), when the picked value equals the gene's value and duplicates aren't allowed, the gene swaps values with another gene, picked at random among those whose value is in its space and whose space holds its value. The swap keeps the genes unique, so the duplicate resolution isn't needed. Genes with agene_constraintaren't swapped, and nothing changes when duplicates are allowed or a free value exists.On
examples/benchmarks/example_tsp.py's settings with 12 cities on a circle (200 generations, seeds 0 to 2), the best tours go from lengths 11.7, 12.5 and 9.6 to the optimum, 6.2.Tests: 3 tests in
tests/test_crossover_mutation.pymutate 100 permutations of 8 (withmutation_num_genes, withmutation_probability=1.0, and with a nested gene space) and check that each one changes and stays a permutation. They fail on master and pass with this change.pytest tests(withouttest_kerasga.pyandtest_torchga.py, which need TensorFlow and PyTorch): 874 passed, 1 skipped. The only failure istest_submodule_versions.py::test_changed_submodule_is_version_bumped[utils], sincepygad/utilschanged; I left the version bump to you for the release.