Skip to content

Fix SHADE never adapting F and CR, and sample DE-variant initial populations within bounds - #258

Merged
BillHuang2001 merged 2 commits into
EMI-Group:mainfrom
flyingdoubleG:fix-de-variants
Oct 28, 2026
Merged

BillHuang2001 merged 2 commits into
EMI-Group:mainfrom
flyingdoubleG:fix-de-variants

Conversation

@flyingdoubleG

Copy link
Copy Markdown
Contributor

SHADE: the F/CR memory never changes

SHADE.step keeps its success-history memory at the initial 0.5 forever, so it runs as DE with fixed F = CR = 0.5. Two bugs, each enough on its own:

  1. deltas = self.fit - trial_fitness is computed after self.fit has been replaced by the trial fitness, so every successful trial has delta 0 and the weights S_delta / nansum(S_delta) are 0/0 = NaN.
  2. The loop collects the successful F, CR and deltas by blending NaN-filled arrays with arithmetic, is_success * update + (1 - is_success) * temp. Since 0 * NaN is NaN, every entry stays NaN whether or not a trial succeeded.

M_F is then NaN every generation and the is_F_nan guard keeps the old memory.

The fix takes the deltas before the fitness update and selects the successful entries with torch.where (order does not matter for the weighted means, so the loop is no longer needed). The first generation still keeps the memory, because the parents' fitness is inf there, as before.

Sphere/Rastrigin, dim=10, bounds ±10, pop 50, 300 generations, seeds 0–4:

memory after 300 gens Rastrigin best
main all 0.5 4.90, 9.16, 9.41, 6.42, 9.47
this PR adapts (e.g. M_F ≈ 0.62–0.85) 2.94, 0.036, 1.08, 0.45, 2.08

CoDE, SaDE, SHADE: most initial coordinates fall outside the bounds

They initialize with torch.randn(pop_size, dim) * (ub - lb) + lb: a normal centred on lb with standard deviation ub - lb. With bounds ±10, only 34% of the initial coordinates lie in [lb, ub], and their mean is about −8.7. The second commit uses torch.rand, like DE, ODE, JADE and the PSO variants.

Tests

Added to unit_test/algorithms/test_de_variants.py:

  • test_shade_adapts_memory: the memory changes and stays finite after 5 steps;
  • test_initial_population_within_bounds: for SHADE, CoDE and SaDE.

Both fail on main and pass here. Locally (Windows, torch 2.11) I can run the eager and (uncompiled) vmap paths of the existing tests for all five DE variants, which pass; torch.compile needs MSVC, which I don't have, so the compiled paths are left to CI.

This was prepared with AI assistance (Claude); I reviewed and tested the change.

Two bugs kept the success-history memory at its initial 0.5 forever, so
SHADE ran as DE with fixed F and CR:
- the improvements were taken after self.fit had been replaced by the trial
  fitness, so every successful delta was 0 and the weights were 0/0;
- the successful F, CR and deltas were collected by blending NaN-filled
  arrays with arithmetic, and 0 * NaN is NaN, so every entry stayed NaN.

Take the deltas before the fitness update and select the successful entries
with torch.where.
…n the bounds

They drew torch.randn * (ub - lb) + lb, a normal centred on lb with standard
deviation ub - lb, so about two thirds of the initial coordinates fell outside
[lb, ub]. Use torch.rand, as DE, ODE, JADE and the PSO variants do.
@BillHuang2001

Copy link
Copy Markdown
Collaborator

No problem, we welcome AI changes.

@BillHuang2001
BillHuang2001 merged commit 80cc26e into EMI-Group:main Oct 28, 2026
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.

2 participants