Save the database to disk Args: path: Path to save to (uses config.db_path if None) iteration: Current iteration number
(self, path: Optional[str] = None, iteration: int = 0)
| 588 | return sorted_programs[:n] |
| 589 | |
| 590 | def save(self, path: Optional[str] = None, iteration: int = 0) -> None: |
| 591 | """ |
| 592 | Save the database to disk |
| 593 | |
| 594 | Args: |
| 595 | path: Path to save to (uses config.db_path if None) |
| 596 | iteration: Current iteration number |
| 597 | """ |
| 598 | save_path = path or self.config.db_path |
| 599 | if not save_path: |
| 600 | logger.warning("No database path specified, skipping save") |
| 601 | return |
| 602 | |
| 603 | # Perform artifact cleanup before saving |
| 604 | self._cleanup_old_artifacts(save_path) |
| 605 | |
| 606 | # create directory if it doesn't exist |
| 607 | os.makedirs(save_path, exist_ok=True) |
| 608 | |
| 609 | # Save each program |
| 610 | for program in self.programs.values(): |
| 611 | prompts = None |
| 612 | if ( |
| 613 | self.config.log_prompts |
| 614 | and self.prompts_by_program |
| 615 | and program.id in self.prompts_by_program |
| 616 | ): |
| 617 | prompts = self.prompts_by_program[program.id] |
| 618 | self._save_program(program, save_path, prompts=prompts) |
| 619 | |
| 620 | # Save metadata |
| 621 | metadata = { |
| 622 | "island_feature_maps": self.island_feature_maps, |
| 623 | "islands": [list(island) for island in self.islands], |
| 624 | "archive": list(self.archive), |
| 625 | "best_program_id": self.best_program_id, |
| 626 | "island_best_programs": self.island_best_programs, |
| 627 | "last_iteration": iteration or self.last_iteration, |
| 628 | "current_island": self.current_island, |
| 629 | "island_generations": self.island_generations, |
| 630 | "last_migration_generation": self.last_migration_generation, |
| 631 | "feature_stats": self._serialize_feature_stats(), |
| 632 | } |
| 633 | |
| 634 | with open(os.path.join(save_path, "metadata.json"), "w") as f: |
| 635 | json.dump(metadata, f) |
| 636 | |
| 637 | logger.info(f"Saved database with {len(self.programs)} programs to {save_path}") |
| 638 | |
| 639 | def load(self, path: str) -> None: |
| 640 | """ |