Add a program to the database Args: program: Program to add iteration: Current iteration (defaults to last_iteration) target_island: Specific island to add to (auto-detects parent's island if None) Returns: Program ID
(
self, program: Program, iteration: int = None, target_island: Optional[int] = None
)
| 215 | self.similarity_threshold = config.similarity_threshold |
| 216 | |
| 217 | def add( |
| 218 | self, program: Program, iteration: int = None, target_island: Optional[int] = None |
| 219 | ) -> str: |
| 220 | """ |
| 221 | Add a program to the database |
| 222 | |
| 223 | Args: |
| 224 | program: Program to add |
| 225 | iteration: Current iteration (defaults to last_iteration) |
| 226 | target_island: Specific island to add to (auto-detects parent's island if None) |
| 227 | |
| 228 | Returns: |
| 229 | Program ID |
| 230 | """ |
| 231 | # Store the program |
| 232 | # If iteration is provided, update the program's iteration_found |
| 233 | if iteration is not None: |
| 234 | program.iteration_found = iteration |
| 235 | # Update last_iteration if needed |
| 236 | self.last_iteration = max(self.last_iteration, iteration) |
| 237 | |
| 238 | self.programs[program.id] = program |
| 239 | |
| 240 | # Calculate feature coordinates for MAP-Elites |
| 241 | feature_coords = self._calculate_feature_coords(program) |
| 242 | |
| 243 | # Determine target island |
| 244 | # If target_island is not specified and program has a parent, inherit parent's island |
| 245 | if target_island is None and program.parent_id: |
| 246 | parent = self.programs.get(program.parent_id) |
| 247 | if parent and "island" in parent.metadata: |
| 248 | # Child inherits parent's island to maintain island isolation |
| 249 | island_idx = parent.metadata["island"] |
| 250 | logger.debug( |
| 251 | f"Program {program.id} inheriting island {island_idx} from parent {program.parent_id}" |
| 252 | ) |
| 253 | else: |
| 254 | # Parent not found or has no island, use current_island |
| 255 | island_idx = self.current_island |
| 256 | if parent: |
| 257 | logger.warning( |
| 258 | f"Parent {program.parent_id} has no island metadata, using current_island {island_idx}" |
| 259 | ) |
| 260 | else: |
| 261 | logger.warning( |
| 262 | f"Parent {program.parent_id} not found, using current_island {island_idx}" |
| 263 | ) |
| 264 | elif target_island is not None: |
| 265 | # Explicit target island specified (e.g., for migrants) |
| 266 | island_idx = target_island |
| 267 | else: |
| 268 | # No parent and no target specified, use current island |
| 269 | island_idx = self.current_island |
| 270 | |
| 271 | island_idx = island_idx % len(self.islands) # Ensure valid island index |
| 272 | |
| 273 | # Novelty check before adding |
| 274 | if not self._is_novel(program.id, island_idx): |
no test coverage detected