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

Method _sample_inspirations

data/database.py:1558–1680  ·  view source on GitHub ↗

Sample inspiration programs for the next evolution step. For proper island-based evolution, inspirations are sampled ONLY from the current island, maintaining genetic isolation between islands. Args: parent: Parent program n: Number of inspi

(self, parent: Program, n: int = 5)

Source from the content-addressed store, hash-verified

1556 return self.programs[parent_id]
1557
1558 def _sample_inspirations(self, parent: Program, n: int = 5) -> List[Program]:
1559 """
1560 Sample inspiration programs for the next evolution step.
1561
1562 For proper island-based evolution, inspirations are sampled ONLY from the
1563 current island, maintaining genetic isolation between islands.
1564
1565 Args:
1566 parent: Parent program
1567 n: Number of inspirations to sample
1568
1569 Returns:
1570 List of inspiration programs from the current island
1571 """
1572 inspirations = []
1573
1574 # Get the parent's island (should be current_island)
1575 parent_island = parent.metadata.get("island", self.current_island)
1576
1577 # Get all programs from the current island
1578 island_program_ids = list(self.islands[parent_island])
1579 island_programs = [self.programs[pid] for pid in island_program_ids if pid in self.programs]
1580
1581 if not island_programs:
1582 logger.warning(f"Island {parent_island} has no programs for inspiration sampling")
1583 return []
1584
1585 # Include the island's best program if available and different from parent
1586 island_best_id = self.island_best_programs[parent_island]
1587 if (
1588 island_best_id is not None
1589 and island_best_id != parent.id
1590 and island_best_id in self.programs
1591 ):
1592 island_best = self.programs[island_best_id]
1593 inspirations.append(island_best)
1594 logger.debug(
1595 f"Including island {parent_island} best program {island_best_id} in inspirations"
1596 )
1597 elif island_best_id is not None and island_best_id not in self.programs:
1598 # Clean up stale island best reference
1599 logger.warning(
1600 f"Island {parent_island} best program {island_best_id} no longer exists, clearing reference"
1601 )
1602 self.island_best_programs[parent_island] = None
1603
1604 # Add top programs from the island as inspirations
1605 top_n = max(1, int(n * self.config.elite_selection_ratio))
1606 top_island_programs = self.get_top_programs(n=top_n, island_idx=parent_island)
1607 for program in top_island_programs:
1608 if program.id not in [p.id for p in inspirations] and program.id != parent.id:
1609 inspirations.append(program)
1610
1611 # Add diverse programs from within the island
1612 if len(island_programs) > n and len(inspirations) < n:
1613 remaining_slots = n - len(inspirations)
1614
1615 # Try to sample from different feature cells within the island

Callers 1

sampleMethod · 0.95

Calls 5

get_top_programsMethod · 0.95
getMethod · 0.80
sampleMethod · 0.80

Tested by

no test coverage detected