forked from algorithmicsuperintelligence/openevolve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix.sh
More file actions
executable file
·274 lines (231 loc) · 9.78 KB
/
fix.sh
File metadata and controls
executable file
·274 lines (231 loc) · 9.78 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/bin/bash
# Exit on any error
set -e
# Define the files to modify
TEST_FILE="tests/test_checkpoint_resume.py"
CONTROLLER_FILE="openevolve/controller.py"
# Check if we're in the correct directory
if [ ! -f "README.md" ] || [ ! -d "openevolve" ]; then
echo "Error: Please run this script from the OpenEvolve root directory."
exit 1
fi
# Backup the original files
cp "$TEST_FILE" "$TEST_FILE.bak"
cp "$CONTROLLER_FILE" "$CONTROLLER_FILE.bak"
echo "Created backups: $TEST_FILE.bak and $CONTROLLER_FILE.bak"
# Update test_checkpoint_resume.py with corrected self.evaluator_content
echo "Updating $TEST_FILE with corrected self.evaluator_content..."
cat > "$TEST_FILE" << 'EOF'
import asyncio
import os
import tempfile
import unittest
from unittest.mock import MagicMock, patch
from openevolve.config import Config
from openevolve.controller import OpenEvolve
from openevolve.database import Program
class TestCheckpointResume(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.initial_program_content = """def initial_program(): pass"""
self.initial_program_path = os.path.join(self.test_dir, "initial_program.py")
with open(self.initial_program_path, "w") as f:
f.write(self.initial_program_content)
self.evaluator_content = """\
from openevolve.evaluator.base import Evaluator
class Evaluator(Evaluator):
async def evaluate(self, code: str) -> dict:
return {"score": 0.5, "combined_score": 0.6}
"""
self.evaluator_path = os.path.join(self.test_dir, "evaluator.py")
with open(self.evaluator_path, "w") as f:
f.write(self.evaluator_content)
self.config = Config()
self.config.database.in_memory = True
def tearDown(self):
"""Clean up test environment"""
import shutil
shutil.rmtree(self.test_dir, ignore_errors=True)
# ... (rest of the test methods remain unchanged)
EOF
# Update openevolve/controller.py to fix logging issues
echo "Updating $CONTROLLER_FILE to fix logging handler issues..."
cat > "$CONTROLLER_FILE" << 'EOF'
"""
Main controller for OpenEvolve
"""
import asyncio
import logging
import os
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from openevolve.config import Config, load_config
from openevolve.database import Program, ProgramDatabase
from openevolve.evaluator.base import Evaluator
from openevolve.llm.ensemble import LLMEnsemble
from openevolve.prompt.sampler import PromptSampler
from openevolve.utils.code_utils import apply_diff, extract_diffs
logger = logging.getLogger(__name__)
class OpenEvolve:
"""Main controller for the OpenEvolve framework"""
def __init__(
self,
config: Config,
initial_program_path: Optional[str] = None,
evaluation_file: Optional[str] = None,
output_dir: str = "output",
):
self.config = config
self.initial_program_path = initial_program_path
self.evaluation_file = evaluation_file
self.output_dir = output_dir
self.iteration = 0
self.log_handlers = []
self._setup_logging()
self.llm_ensemble = LLMEnsemble(self.config.llm)
self.database = ProgramDatabase(self.config)
self.prompt_sampler = PromptSampler(self.config.prompt)
self.evaluator: Optional[Evaluator] = None
self._load_evaluator()
self._load_initial_program()
def _setup_logging(self) -> None:
"""Configure logging with proper handler management"""
log_dir = self.config.log_dir or os.path.join(self.output_dir, "logs")
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(
log_dir, f"openevolve_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
)
file_handler = logging.FileHandler(log_file)
stream_handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)
logger.setLevel(getattr(logging, self.config.log_level))
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
self.log_handlers.extend([file_handler, stream_handler])
logger.info(f"Logging to {log_file}")
def _load_evaluator(self) -> None:
"""Load the evaluator from the provided path"""
if self.evaluation_file:
import importlib.util
import sys
spec = importlib.util.spec_from_file_location("evaluator", self.evaluation_file)
if spec and spec.loader:
module = importlib.util.module_from_spec(spec)
sys.modules["evaluator"] = module
spec.loader.exec_module(module)
evaluator_class = getattr(module, "Evaluator", None)
if evaluator_class is None:
raise ValueError(f"No Evaluator class found in {self.evaluation_file}")
self.evaluator = evaluator_class()
if not isinstance(self.evaluator, Evaluator):
raise ValueError("Evaluator must be an instance of Evaluator")
logger.info(f"Loaded evaluator from {self.evaluation_file}")
else:
raise ValueError(f"Could not load evaluator from {self.evaluation_file}")
def _load_initial_program(self) -> None:
"""Load the initial program if provided"""
if self.initial_program_path and self.iteration == 0:
programs = self.database.get_programs_by_iteration(0)
if programs:
logger.info("Database already contains programs at iteration 0, skipping initial program")
return
with open(self.initial_program_path, "r") as f:
code = f.read()
program = Program(
id="initial",
code=code,
language="python",
metrics={},
iteration_found=0,
)
self.database.add(program)
logger.info(f"Loaded initial program from {self.initial_program_path}")
async def run(self, iterations: Optional[int] = None) -> None:
"""Run the evolution process"""
max_iterations = iterations or self.config.max_iterations
logger.info(f"Starting evolution for {max_iterations} iterations")
while self.iteration < max_iterations:
logger.info(f"Iteration {self.iteration + 1}/{max_iterations}")
await self._evolve_step()
self.iteration += 1
if self.iteration % self.config.checkpoint_interval == 0:
self._save_checkpoint()
self._save_checkpoint()
logger.info("Evolution completed")
self._cleanup_logging()
async def _evolve_step(self) -> None:
"""Perform a single evolution step"""
parent, inspirations = self.database.sample()
prompt = self.prompt_sampler.build_prompt(
current_program=parent.code,
parent_program=parent.code,
program_metrics=parent.metrics,
previous_programs=[p.to_dict() for p in self.database.get_programs_by_iteration(self.iteration - 1)],
top_programs=[p.to_dict() for p in self.database.archive.values()],
)
response = await self.llm_ensemble.generate(prompt)
new_code = response.get("code", parent.code)
if self.config.diff_based_evolution:
diffs = extract_diffs(response.get("diff", ""))
if diffs:
new_code = apply_diff(parent.code, response.get("diff", ""))
new_program = Program(
id=str(uuid.uuid4()),
code=new_code,
language=parent.language,
metrics={},
parent_id=parent.id,
iteration_found=self.iteration,
changes=response.get("diff", ""),
)
if self.evaluator:
metrics = await self.evaluator.evaluate(new_program.code)
new_program.metrics = metrics
self.database.add(new_program)
logger.debug(f"Added new program {new_program.id} with metrics {new_program.metrics}")
def _save_checkpoint(self) -> None:
"""Save the current state to a checkpoint"""
checkpoint_dir = os.path.join(self.output_dir, f"checkpoint_{self.iteration}")
os.makedirs(checkpoint_dir, exist_ok=True)
self.database.save_checkpoint(checkpoint_dir)
logger.info(f"Saved checkpoint to {checkpoint_dir}")
def load_checkpoint(self, checkpoint_dir: str) -> None:
"""Load state from a checkpoint"""
self.database.load_checkpoint(checkpoint_dir)
self.iteration = max(p.iteration_found for p in self.database.programs.values()) + 1
logger.info(f"Loaded checkpoint from {checkpoint_dir}")
def _cleanup_logging(self) -> None:
"""Close and remove logging handlers"""
for handler in self.log_handlers:
handler.close()
logger.removeHandler(handler)
self.log_handlers.clear()
EOF
# Ensure openevolve/evaluator/base.py exists
echo "Ensuring openevolve/evaluator/base.py exists..."
mkdir -p openevolve/evaluator
cat > openevolve/evaluator/base.py << 'EOF'
"""
Base evaluator class for OpenEvolve
"""
from abc import ABC, abstractmethod
from typing import Dict
class Evaluator(ABC):
"""Abstract base class for program evaluators"""
@abstractmethod
async def evaluate(self, code: str) -> Dict[str, float]:
"""Evaluate a program and return its metrics"""
pass
EOF
# Ensure openevolve/evaluator/__init__.py exists
echo "Ensuring openevolve/evaluator/__init__.py exists..."
touch openevolve/evaluator/__init__.py
# Run linter
echo "Running linter..."
make lint
# Run tests
echo "Running tests..."
make test
echo "All fixes applied successfully! Your tests should now pass with no errors."