MCPcopy Create free account
hub / github.com/FareedKhan-dev/ai-long-task / sample_from_island

Method sample_from_island

data/database.py:409–476  ·  view source on GitHub ↗

Sample a program and inspirations from a specific island without modifying current_island This method is thread-safe and doesn't modify shared state, avoiding race conditions when multiple workers sample from different islands concurrently. Uses the same exploratio

(
        self, island_id: int, num_inspirations: Optional[int] = None
    )

Source from the content-addressed store, hash-verified

407 return parent, inspirations
408
409 def sample_from_island(
410 self, island_id: int, num_inspirations: Optional[int] = None
411 ) -> Tuple[Program, List[Program]]:
412 """
413 Sample a program and inspirations from a specific island without modifying current_island
414
415 This method is thread-safe and doesn't modify shared state, avoiding race conditions
416 when multiple workers sample from different islands concurrently.
417
418 Uses the same exploration/exploitation/random strategy as sample() to ensure
419 consistent behavior between single-process and parallel execution modes.
420
421 Args:
422 island_id: The island to sample from
423 num_inspirations: Number of inspiration programs to sample (defaults to 5)
424
425 Returns:
426 Tuple of (parent_program, inspiration_programs)
427 """
428 # Ensure valid island ID
429 island_id = island_id % len(self.islands)
430
431 # Get programs from the specific island
432 island_programs = list(self.islands[island_id])
433
434 if not island_programs:
435 # Island is empty, fall back to sampling from all programs
436 logger.debug(f"Island {island_id} is empty, sampling from all programs")
437 return self.sample(num_inspirations)
438
439 # Use exploration_ratio and exploitation_ratio to decide sampling strategy
440 # This matches the logic in _sample_parent() for consistent behavior
441 rand_val = random.random()
442
443 if rand_val < self.config.exploration_ratio:
444 # EXPLORATION: Sample randomly from island (diverse sampling)
445 parent = self._sample_from_island_random(island_id)
446 sampling_mode = "exploration"
447 elif rand_val < self.config.exploration_ratio + self.config.exploitation_ratio:
448 # EXPLOITATION: Sample from archive (elite programs)
449 parent = self._sample_from_archive_for_island(island_id)
450 sampling_mode = "exploitation"
451 else:
452 # WEIGHTED: Use fitness-weighted sampling (remaining probability)
453 parent = self._sample_from_island_weighted(island_id)
454 sampling_mode = "weighted"
455
456 # Select inspirations from the same island
457 if num_inspirations is None:
458 num_inspirations = 5 # Default for backward compatibility
459
460 # Get other programs from the island for inspirations
461 other_programs = [pid for pid in island_programs if pid != parent.id]
462
463 if len(other_programs) < num_inspirations:
464 # Not enough programs in island, use what we have
465 inspiration_ids = other_programs
466 else:

Callers 1

_submit_iterationMethod · 0.80

Calls 4

sampleMethod · 0.95

Tested by

no test coverage detected