Skip to content

Avoid overflow when summing probability-selection fitness - #381

Merged
ahmedfgad merged 2 commits into
ahmedfgad:github-actionsfrom
ktz03:fix/public-input-reliability-0914
Oct 16, 2026
Merged

ahmedfgad merged 2 commits into
ahmedfgad:github-actionsfrom
ktz03:fix/public-input-reliability-0914

Conversation

@ktz03

@ktz03 ktz03 commented Oct 15, 2026

Copy link
Copy Markdown

Roulette-wheel (rws) and stochastic-universal (sus) selection reduce fitness in its original NumPy dtype. Four positive numpy.int64(2**62) fitness values therefore wrap their sum to zero, and GA.run() raises ZeroDivisionError. Large positive uint64, float16 and float32 values also overflow these reductions; multi-objective fitness can overflow before its row sums are normalized.

Use an accumulator dtype of numpy.result_type(fitness.dtype, float) for both reductions in each selector. This promotes native integer and narrow floating arrays while preserving object arrays containing arbitrary-size Python integers. The existing exception for a genuine zero fitness sum remains unchanged.

Validation on Windows, Python 3.12.14 and NumPy 2.5.4:

  • The parent-selection regression file has 117 tests: baseline 16 failures/101 passes, fixed 117 passes. New coverage includes both selectors, single/multiple objectives, four numeric dtypes, genuine zero sums and arbitrary-size Python integers.
  • All eight files used by the Windows CI test step pass locally: 582 tests, with one existing warning that crossover and mutation are disabled. These were run from the source checkout; a wheel install and the complete CI matrix were not run.
  • A separate public GA.run() script changes from 16 failures/20 normal controls to 36 completed calls; every previously normal row, selected-parent index and caller input remains identical.
  • git diff --check passes.

No full repository, other Python/NumPy versions, other platforms, model training or performance validation is claimed.

@github-actions

Copy link
Copy Markdown

📊 Change summary

2 files changed · +66 / −4 lines

pie showData
    title Lines changed per file (70 total)
    "tests/test_parent_selection_regressions.py" : 62
    "pygad/utils/parent_selection.py" : 8
Loading
Per-file breakdown
File + − total
tests/test_parent_selection_regressions.py 62 0 62
pygad/utils/parent_selection.py 4 4 8
Total (2 files) 66 4 70

@ahmedfgad ahmedfgad added the enhancement New feature or request label Oct 15, 2026
@ahmedfgad
ahmedfgad changed the base branch from master to github-actions October 16, 2026 03:42
@ahmedfgad
ahmedfgad merged commit fc03d54 into ahmedfgad:github-actions Oct 16, 2026
1 check passed
@ahmedfgad ahmedfgad mentioned this pull request Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants