(self, config: DatabaseConfig)
| 120 | """ |
| 121 | |
| 122 | def __init__(self, config: DatabaseConfig): |
| 123 | self.config = config |
| 124 | |
| 125 | # In-memory program storage |
| 126 | self.programs: Dict[str, Program] = {} |
| 127 | |
| 128 | # Per-island feature grids for MAP-Elites |
| 129 | self.island_feature_maps: List[Dict[str, str]] = [{} for _ in range(config.num_islands)] |
| 130 | |
| 131 | # Handle both int and dict types for feature_bins |
| 132 | if isinstance(config.feature_bins, int): |
| 133 | self.feature_bins = max( |
| 134 | config.feature_bins, |
| 135 | int(pow(config.archive_size, 1 / len(config.feature_dimensions)) + 0.99), |
| 136 | ) |
| 137 | else: |
| 138 | # If dict, keep as is (we'll use feature_bins_per_dim instead) |
| 139 | self.feature_bins = 10 # Default fallback for backward compatibility |
| 140 | |
| 141 | # Island populations |
| 142 | self.islands: List[Set[str]] = [set() for _ in range(config.num_islands)] |
| 143 | |
| 144 | # Island management attributes |
| 145 | self.current_island: int = 0 |
| 146 | self.island_generations: List[int] = [0] * config.num_islands |
| 147 | self.last_migration_generation: int = 0 |
| 148 | self.migration_interval: int = getattr(config, "migration_interval", 10) # Default to 10 |
| 149 | self.migration_rate: float = getattr(config, "migration_rate", 0.1) # Default to 0.1 |
| 150 | |
| 151 | # Archive of elite programs |
| 152 | self.archive: Set[str] = set() |
| 153 | |
| 154 | # Track the absolute best program separately |
| 155 | self.best_program_id: Optional[str] = None |
| 156 | |
| 157 | # Track best program per island for proper island-based evolution |
| 158 | self.island_best_programs: List[Optional[str]] = [None] * config.num_islands |
| 159 | |
| 160 | # Track the last iteration number (for resuming) |
| 161 | self.last_iteration: int = 0 |
| 162 | |
| 163 | # Load database from disk if path is provided |
| 164 | if config.db_path and os.path.exists(config.db_path): |
| 165 | self.load(config.db_path) |
| 166 | |
| 167 | # Prompt log |
| 168 | self.prompts_by_program: Dict[str, Dict[str, Dict[str, str]]] = None |
| 169 | |
| 170 | # Set random seed for reproducible sampling if specified |
| 171 | if config.random_seed is not None: |
| 172 | import random |
| 173 | |
| 174 | random.seed(config.random_seed) |
| 175 | logger.debug(f"Database: Set random seed to {config.random_seed}") |
| 176 | |
| 177 | # Diversity caching infrastructure |
| 178 | self.diversity_cache: Dict[int, Dict[str, Union[float, float]]] = ( |
| 179 | {} |
nothing calls this directly
no test coverage detected