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