forked from algorithmicsuperintelligence/openevolve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenevolve_database.py
More file actions
186 lines (155 loc) · 6.51 KB
/
openevolve_database.py
File metadata and controls
186 lines (155 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
Program management for OpenEvolve
"""
import logging
import random
import uuid
from typing import Any, Dict, List, Optional, Tuple
import numpy as np
from openevolve.config import Config
from openevolve.utils.code_utils import apply_diff
logger = logging.getLogger(__name__')
class Program:
"""Stores a program and its metadata"""
def __init__(
self,
id: str,
code: str,
language: str,
metrics: Dict[str, float],
parent_id: Optional[str] = None,
iteration_found: int = 0,
changes: str = '',
parent_metrics: str = '',
key_features: Optional[List[str]] = None,
):
self.id = id
self.code = code
self.language = language
self.metrics = metrics
self.parent_id = parent_id
self.iteration_found = iteration_found
self.changes = changes
self.parent_metrics = parent_metrics or {}
self.key_features = key_features or []
def to_dict(self) -> Dict[str, Any]:
"""Convert program to dictionary"""
return {
"id": self.id,
"code": self.code,
"language": self.language,
"metrics": self.metrics,
"parent_id": self.parent_id,
"iteration_found": self.iteration_found,
"changes": self.changes,
"parent_metrics": self.parent_metrics,
"key_features": self.key_features,
}
class ProgramDatabase:
"""Manages the program population and archive"""
def __init__(self, config: Config):
self.config = config
self.programs: Dict[str, Program] = {}
self.archive: Dict[str, Program] = {}
self.island_populations: Dict[int, List[str]] = {
i: [] for i in range(config.database.num_islands)
}
self.last_iteration = 0
self._program_hashes: Dict[str, str] = {} # Maps program hash to ID
if not config.database.in_memory:
logger.warning("Non-in-memory database not yet implemented")
def add(self, program: Program) -> bool:
"""Add a program to the database if it doesn't already exist"""
# Compute a simple hash of the code (for deduplication)
program_hash = str(hash(program.code))
if program_hash in self._program_hashes:
logger.debug(f"Duplicate program detected: {program.id}")
return False
# Add to main programs dictionary
self.programs[program.id] = program
self._program_hashes[program_hash] = program.id
# Add to island population
island = random.randint(0, self.config.database.num_islands - 1)
self.island_populations[island].append(program.id)
# Maintain population size
while len(self.island_populations[island]) > self.config.database.population_size:
program_to_remove = self.island_populations[island].pop(0)
if program_to_remove in self.programs:
del self._program_hashes[str(hash(self.programs[program_to_remove].code))]
del self.programs[program_to_remove]
# Add to archive if it's among the best
if len(self.archive) < self.config.database.archive_size:
self.archive[program.id] = program
else:
worst_in_archive = min(
self.archive.values(),
key=lambda p: sum(p.metrics.values()) / max(1, len(p.metrics)),
)
if sum(program.metrics.values()) / max(1, len(program.metrics)) > sum(
worst_in_archive.metrics.values()
) / max(1, len(worst_in_archive.metrics)):
del self.archive[worst_in_archive.id]
self.archive[program.id] = program
return True
def get(self, program_id: str) -> Optional[Program]:
"""Retrieve a program by ID"""
return self.programs.get(program_id)
def get_best_program(self) -> Optional[Program]:
"""Get the best performing program"""
if not self.programs:
return None
return max(
self.programs.values(),
key=lambda p: sum(p.metrics.values()) / max(1, len(p.metrics)),
)
def sample(self) -> Tuple[Program, List[Program]]:
"""Sample a parent program and inspirations from the database."""
import random
mutation_rate = getattr(self.config.database, "mutation_rate", 0.1) # Default to 0.1
parent = None
inspirations = []
# Select parent from elite programs
if self.programs and random.random() > mutation_rate:
elite = sorted(
self.programs.values(),
key=lambda p: sum(p.metrics.values()) / max(1, len(p.metrics)),
reverse=True,
)[: int(len(self.programs) * self.config.database.elite_selection_ratio)]
parent = random.choice(elite) if elite else random.choice(list(self.programs.values()))
else:
# Mutate a random program
parent = random.choice(list(self.programs.values()))
parent = self._mutate_program(parent) # Apply mutation
# Select inspirations
inspirations = random.sample(
list(self.programs.values()),
min(self.config.prompt.num_top_programs, len(self.programs)),
)
return parent, inspirations
def _mutate_program(self, program: Program) -> Program:
"""Apply a simple mutation to a program."""
import random
# Example: Add a comment to a random line
code_lines = program.code.split("\n")
if code_lines and random.random() < 0.5:
line_idx = random.randint(0, len(code_lines) - 1)
code_lines[line_idx] += " # Mutated"
new_code = "\n".join(code_lines)
return Program(
id=f"{program.id}_mutated_{random.randint(1000, 9999)}",
code=new_code,
language=program.language,
metrics=program.metrics,
iteration_found=self.last_iteration + 1,
)
def get_programs_by_iteration(self, iteration: int) -> List[Program]:
"""Get all programs from a specific iteration"""
return [p for p in self.programs.values() if p.iteration_found == iteration]
def save_checkpoint(self, output_dir: str) -> None:
"""Save the current database state to disk"""
# Placeholder for checkpoint saving
pass
def load_checkpoint(self, checkpoint_dir: str) -> None:
"""Load database state from a checkpoint"""
# Placeholder for checkpoint loading
pass