Sample a parent for exploration (from current island)
(self)
| 1294 | return self._sample_random_parent() |
| 1295 | |
| 1296 | def _sample_exploration_parent(self) -> Program: |
| 1297 | """ |
| 1298 | Sample a parent for exploration (from current island) |
| 1299 | """ |
| 1300 | current_island_programs = self.islands[self.current_island] |
| 1301 | |
| 1302 | if not current_island_programs: |
| 1303 | # If current island is empty, initialize with best program or random program |
| 1304 | if self.best_program_id and self.best_program_id in self.programs: |
| 1305 | # Create a copy of best program for the empty island (don't reuse same ID) |
| 1306 | best_program = self.programs[self.best_program_id] |
| 1307 | copy_program = Program( |
| 1308 | id=str(uuid.uuid4()), |
| 1309 | code=best_program.code, |
| 1310 | language=best_program.language, |
| 1311 | parent_id=best_program.id, |
| 1312 | generation=best_program.generation, |
| 1313 | timestamp=time.time(), |
| 1314 | iteration_found=self.last_iteration, |
| 1315 | metrics=best_program.metrics.copy(), |
| 1316 | complexity=best_program.complexity, |
| 1317 | diversity=best_program.diversity, |
| 1318 | metadata={"island": self.current_island}, |
| 1319 | artifacts_json=best_program.artifacts_json, |
| 1320 | artifact_dir=best_program.artifact_dir, |
| 1321 | ) |
| 1322 | self.programs[copy_program.id] = copy_program |
| 1323 | self.islands[self.current_island].add(copy_program.id) |
| 1324 | logger.debug( |
| 1325 | f"Initialized empty island {self.current_island} with copy of best program" |
| 1326 | ) |
| 1327 | return copy_program |
| 1328 | else: |
| 1329 | # Use any available program |
| 1330 | return next(iter(self.programs.values())) |
| 1331 | |
| 1332 | # Clean up stale references and sample from current island |
| 1333 | valid_programs = [pid for pid in current_island_programs if pid in self.programs] |
| 1334 | |
| 1335 | # Remove stale program IDs from island |
| 1336 | if len(valid_programs) < len(current_island_programs): |
| 1337 | stale_ids = current_island_programs - set(valid_programs) |
| 1338 | logger.debug( |
| 1339 | f"Removing {len(stale_ids)} stale program IDs from island {self.current_island}" |
| 1340 | ) |
| 1341 | for stale_id in stale_ids: |
| 1342 | self.islands[self.current_island].discard(stale_id) |
| 1343 | |
| 1344 | # If no valid programs after cleanup, reinitialize island |
| 1345 | if not valid_programs: |
| 1346 | logger.warning( |
| 1347 | f"Island {self.current_island} has no valid programs after cleanup, reinitializing" |
| 1348 | ) |
| 1349 | if self.best_program_id and self.best_program_id in self.programs: |
| 1350 | # Create a copy of best program for the empty island (don't reuse same ID) |
| 1351 | best_program = self.programs[self.best_program_id] |
| 1352 | copy_program = Program( |
| 1353 | id=str(uuid.uuid4()), |
no test coverage detected