Repository navigation
Fix DE_binary_crossover sampling from a normal instead of uniform distribution - #252
Merged
BillHuang2001 merged 1 commit intoOct 9, 2026
Conversation
The crossover mask compared torch.randn samples against CR, the documented crossover probability. A standard normal distribution does not give a crossover rate close to CR, especially at low CR, which SHADE and SaDE adapt toward to fine-tune search late in optimization. Sibling code in de.py and ode.py already builds the same kind of mask with torch.rand. Measured on this repo (seed 42, pop_size=2000, dim=20, CUDA): at CR=0.1 the empirical crossover rate was 0.5655 instead of ~0.1; at CR=0.9 it was 0.8276 instead of ~0.9.
Collaborator
|
Thanks for the findings |
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.
Description
DE_binary_crossoverinsrc/evox/operators/crossover/differential_evolution.pybuilds the crossover mask withtorch.randn(pop_size, dim, device=device) < CR, sampling from a standard normal distribution instead of a uniform [0, 1) distribution.CRis the crossover probability and is meant to be compared against a uniform draw, which is exactly what sibling implementations inde.pyandode.pydo withtorch.rand.This affects SHADE, SaDE, and CoDE, which all call
DE_binary_crossover. At CR=0.1 the actual fraction of genes crossed over measured 0.5655 instead of the intended ~0.1; at CR=0.9 it measured 0.8276 instead of ~0.9. Since SHADE and SaDE adapt CR toward low values to fine-tune search late in optimization, this bug means the crossover rate they converge to has little relation to the rate their adaptive mechanism computes, which changes the search behavior of all three algorithms.Fix is a one-line change from
torch.randntotorch.randon line 53.Tested with
python -m unittest discover -s unit_test -v. Added two regression tests inunit_test/operators/test_differential_evolution.py:test_crossover_rate_matches_CR(CR=0.1, expects empirical crossover rate < 0.2) andtest_high_CR_crosses_over_most_genes(CR=0.9, expects rate > 0.85). Both fail against the original code (measured 0.5655 and 0.8276) and pass with the fix. Full suite before and after: 132 baseline tests with 28 pre-existing errors and 11 skips, all unrelated (triton_kernels GPU kernel tests, supervised_learning neuroevolution tests, a distributed NCCL workflow test); after the fix, 134 tests with the same 28 errors and 11 skips, plus the two new tests passing.Checklist
ruff.