Skip to content

Draw both points of two_points_crossover at random - #371

Open
tachsin wants to merge 1 commit into
ahmedfgad:masterfrom
tachsin:fix-two-points-crossover
Open

tachsin wants to merge 1 commit into
ahmedfgad:masterfrom
tachsin:fix-two-points-crossover

Conversation

@tachsin

@tachsin tachsin commented Sep 28, 2026

Copy link
Copy Markdown

Fixes #370.

two_points_crossover drew only the first point; the second was always the first plus num_genes // 2. This draws both points: 2 different values in [0, num_genes], sorted, with every pair equally likely. The genes between them still come from the second parent, as before.

It also fixes the single-gene case, where the float numpy.zeros(...) points raised a TypeError when slicing: the points are now 0 and 1, so the gene is copied from the second parent, as the old comment intended.

Test: test_two_points_crossover_manual_call in tests/test_crossover_mutation.py checks that the genes from the second parent form one block and that every block length from 1 to num_genes occurs. It fails on master 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 point was always the first plus num_genes // 2, so every child
took exactly half of its genes from its second parent. Draw 2 different
points in [0, num_genes] instead, every pair equally likely. This also
fixes the TypeError with a single gene.

Fixes ahmedfgad#370

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.

two_points_crossover draws only the first point; the second is always num_genes // 2 after it

1 participant