Update the archive of elite programs Args: program: Program to consider for archive
(self, program: Program)
| 1135 | return fitness1 > fitness2 |
| 1136 | |
| 1137 | def _update_archive(self, program: Program) -> None: |
| 1138 | """ |
| 1139 | Update the archive of elite programs |
| 1140 | |
| 1141 | Args: |
| 1142 | program: Program to consider for archive |
| 1143 | """ |
| 1144 | # If archive not full, add program |
| 1145 | if len(self.archive) < self.config.archive_size: |
| 1146 | self.archive.add(program.id) |
| 1147 | return |
| 1148 | |
| 1149 | # Clean up stale references and get valid archive programs |
| 1150 | valid_archive_programs = [] |
| 1151 | stale_ids = [] |
| 1152 | |
| 1153 | for pid in self.archive: |
| 1154 | if pid in self.programs: |
| 1155 | valid_archive_programs.append(self.programs[pid]) |
| 1156 | else: |
| 1157 | stale_ids.append(pid) |
| 1158 | |
| 1159 | # Remove stale references from archive |
| 1160 | for stale_id in stale_ids: |
| 1161 | self.archive.discard(stale_id) |
| 1162 | logger.debug(f"Removing stale program {stale_id} from archive") |
| 1163 | |
| 1164 | # If archive is now not full after cleanup, just add the new program |
| 1165 | if len(self.archive) < self.config.archive_size: |
| 1166 | self.archive.add(program.id) |
| 1167 | return |
| 1168 | |
| 1169 | # Find worst program among valid programs |
| 1170 | if valid_archive_programs: |
| 1171 | worst_program = min( |
| 1172 | valid_archive_programs, |
| 1173 | key=lambda p: get_fitness_score(p.metrics, self.config.feature_dimensions), |
| 1174 | ) |
| 1175 | |
| 1176 | # Replace if new program is better |
| 1177 | if self._is_better(program, worst_program): |
| 1178 | self.archive.remove(worst_program.id) |
| 1179 | self.archive.add(program.id) |
| 1180 | else: |
| 1181 | # No valid programs in archive, just add the new one |
| 1182 | self.archive.add(program.id) |
| 1183 | |
| 1184 | def _update_best_program(self, program: Program) -> None: |
| 1185 | """ |
no test coverage detected