fix: use island-aware inspiration sampling in sample_from_island#471
Open
pathakanshika144-afk wants to merge 1 commit into
Conversation
sample_from_island() used plain random.sample() for inspirations when the island was non-empty, while only the empty-island fallback used _sample_inspirations() (island best + elites + feature-cell diversity). Add an explicit island_id parameter to _sample_inspirations() and reuse it in sample_from_island(). This keeps parallel workers isolated even when archive fallback returns a parent whose metadata belongs to a different island, and avoids the shared current_island fallback. Closes algorithmicsuperintelligence#437
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.
Summary
Fixes #437.
sample_from_island()selects the parent with the same exploration/exploitation/weighted strategy assample(), but samples inspirations with plainrandom.sample()when the island is non-empty. Only the empty-island fallback (self.sample()) uses_sample_inspirations(), which applies the island-best + elite + feature-cell diversity strategy. As the issue notes,_sample_inspirations()should be used in all cases.Change
island_id: Optional[int] = Noneto_sample_inspirations(). When provided, it overridesparent.metadata["island"]; when omitted, behavior is unchanged.sample_from_island()now callsself._sample_inspirations(parent, n=num_inspirations, island_id=island_id)instead ofrandom.sample(other_programs, ...).Why an explicit
island_id_sample_inspirations()resolves the island viaparent.metadata.get("island", self.current_island). Reusing it naively would reintroduce two problems:sample_from_island()is documented as thread-safe and does not touch shared state. Thecurrent_islandfallback would reintroduce a shared-state read for parallel workers._sample_from_archive_for_island()falls back to any archive program when the requested island has none, so the returned parent'smetadata["island"]may differ from the requestedisland_id. Relying onparent.metadatawould pull inspirations from the wrong island, breaking the genetic isolation thattest_sample_from_island_returns_from_correct_islandasserts.Passing
island_idexplicitly closes both gaps.Compatibility
sample()is unchanged — it calls_sample_inspirations(parent, n=...)withoutisland_id, preserving the existingparent.metadata->current_islandresolution.parent_island %= len(self.islands)is added defensively;sample_from_island()already wrapsisland_idat entry, so this is a no-op for the new call site but guards future callers.Tests
New
tests/test_sample_from_island_inspirations.py(3 tests):test_sample_from_island_reuses_strategy_aware_inspiration_sampler— verifies delegation to_sample_inspirations(parent, n=..., island_id=island_id).test_explicit_island_overrides_parent_metadata_for_inspirations— parent from island 0, request island 1; inspirations come from island 1, not the parent's island.test_zero_inspirations_is_forwarded_without_random_sampling—num_inspirations=0is forwarded without random sampling.Existing
tests/test_sample_from_island_ratios.py(8 tests) still passes, includingtest_sample_from_island_returns_from_correct_island(inspirations stay on the requested island) andtest_single_program_island(single-program island returns no inspirations).