MCPcopy Index your code
hub / github.com/algorithmicsuperintelligence/openevolve / ProgramDatabase

Class ProgramDatabase

openevolve/database.py:113–2563  ·  view source on GitHub ↗

Database for storing and sampling programs during evolution The database implements a combination of MAP-Elites algorithm and island-based population model to maintain diversity during evolution. It also tracks the absolute best program separately to ensure it's never lost.

Source from the content-addressed store, hash-verified

111
112
113class ProgramDatabase:
114 """
115 Database for storing and sampling programs during evolution
116
117 The database implements a combination of MAP-Elites algorithm and
118 island-based population model to maintain diversity during evolution.
119 It also tracks the absolute best program separately to ensure it's never lost.
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

Calls

no outgoing calls