forked from shyamsaktawat/OpenAlpha_Evolve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
395 lines (297 loc) · 14.2 KB
/
Copy pathapp.py
File metadata and controls
395 lines (297 loc) · 14.2 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
Gradio web interface for OpenAlpha_Evolve.
"""
import gradio as gr
import asyncio
import json
import os
import sys
import time
import logging
from datetime import datetime
from dotenv import load_dotenv
project_root = os.path.abspath(os.path.dirname(__file__))
if project_root not in sys.path:
sys.path.insert(0, project_root)
load_dotenv()
from core.interfaces import TaskDefinition, Program
from task_manager.agent import TaskManagerAgent
from config import settings
class StringIOHandler(logging.Handler):
def __init__(self):
super().__init__()
self.log_capture = []
def emit(self, record):
try:
msg = self.format(record)
self.log_capture.append(msg)
except Exception:
self.handleError(record)
def get_logs(self):
return "\n".join(self.log_capture)
def clear(self):
self.log_capture = []
string_handler = StringIOHandler()
string_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
root_logger = logging.getLogger()
root_logger.addHandler(string_handler)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
root_logger.addHandler(console_handler)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
for module in ['task_manager.agent', 'code_generator.agent', 'evaluator_agent.agent', 'database_agent.agent',
'selection_controller.agent', 'prompt_designer.agent']:
logging.getLogger(module).setLevel(logging.DEBUG)
current_results = []
async def run_evolution(
task_id,
description,
function_name,
examples_json,
allowed_imports_text,
population_size,
generations,
num_islands,
migration_frequency,
migration_rate
):
"""Run the evolutionary process with the given parameters."""
progress = gr.Progress()
string_handler.clear()
try:
try:
examples = json.loads(examples_json)
if not isinstance(examples, list):
return "Error: Examples must be a JSON list of objects with 'input' and 'output' keys."
for i, example in enumerate(examples):
if not isinstance(example, dict) or "input" not in example or "output" not in example:
return f"Error in example {i+1}: Each example must be an object with 'input' and 'output' keys."
except json.JSONDecodeError:
return "Error: Examples must be valid JSON. Please check the format."
allowed_imports = [imp.strip() for imp in allowed_imports_text.split(",") if imp.strip()]
settings.POPULATION_SIZE = int(population_size)
settings.GENERATIONS = int(generations)
settings.NUM_ISLANDS = int(num_islands)
settings.MIGRATION_FREQUENCY = int(migration_frequency)
settings.MIGRATION_RATE = float(migration_rate)
task = TaskDefinition(
id=task_id,
description=description,
function_name_to_evolve=function_name,
input_output_examples=examples,
allowed_imports=allowed_imports
)
async def progress_callback(generation, max_generations, stage, message=""):
stage_weight = 0.25
gen_progress = generation + (stage * stage_weight)
total_progress = gen_progress / max_generations
progress(min(total_progress, 0.99), f"Generation {generation}/{max_generations}: {message}")
logger.info(f"Progress: Generation {generation}/{max_generations} - {message}")
await asyncio.sleep(0.1)
task_manager = TaskManagerAgent(task_definition=task)
task_manager.progress_callback = progress_callback
progress(0, "Starting evolutionary process...")
class GenerationProgressListener(logging.Handler):
def __init__(self):
super().__init__()
self.current_gen = 0
self.max_gen = settings.GENERATIONS
def emit(self, record):
try:
msg = record.getMessage()
if "--- Generation " in msg:
gen_parts = msg.split("Generation ")[1].split("/")[0]
try:
self.current_gen = int(gen_parts)
asyncio.create_task(
progress_callback(
self.current_gen,
self.max_gen,
0,
"Starting generation"
)
)
except ValueError:
pass
elif "Evaluating population" in msg:
asyncio.create_task(
progress_callback(
self.current_gen,
self.max_gen,
1,
"Evaluating population"
)
)
elif "Selected " in msg and " parents" in msg:
asyncio.create_task(
progress_callback(
self.current_gen,
self.max_gen,
2,
"Selected parents"
)
)
elif "Generated " in msg and " offspring" in msg:
asyncio.create_task(
progress_callback(
self.current_gen,
self.max_gen,
3,
"Generated offspring"
)
)
except Exception:
pass
progress_listener = GenerationProgressListener()
progress_listener.setLevel(logging.INFO)
root_logger.addHandler(progress_listener)
try:
best_programs = await task_manager.execute()
progress(1.0, "Evolution completed!")
global current_results
current_results = best_programs if best_programs else []
if best_programs:
result_text = f"✅ Evolution completed successfully! Found {len(best_programs)} solution(s).\n\n"
for i, program in enumerate(best_programs):
result_text += f"### Solution {i+1}\n"
result_text += f"- ID: {program.id}\n"
result_text += f"- Fitness: {program.fitness_scores}\n"
result_text += f"- Generation: {program.generation}\n"
result_text += f"- Island ID: {program.island_id}\n\n"
result_text += "```python\n" + program.code + "\n```\n\n"
return result_text
else:
return "❌ Evolution completed, but no suitable solutions were found."
finally:
root_logger.removeHandler(progress_listener)
except Exception as e:
import traceback
return f"Error during evolution: {str(e)}\n\n{traceback.format_exc()}"
def get_code(solution_index):
"""Get the code for a specific solution."""
try:
if current_results and 0 <= solution_index < len(current_results):
program = current_results[solution_index]
return program.code
return "No solution available at this index."
except Exception as e:
return f"Error retrieving solution: {str(e)}"
FIB_EXAMPLES = '''[
{"input": [0], "output": 0},
{"input": [1], "output": 1},
{"input": [5], "output": 5},
{"input": [10], "output": 55}
]'''
def set_fib_example():
"""Set the UI to a Fibonacci example task."""
return (
"fibonacci_task",
"Write a Python function that computes the nth Fibonacci number (0-indexed), where fib(0)=0 and fib(1)=1.",
"fibonacci",
FIB_EXAMPLES,
""
)
with gr.Blocks(title="OpenAlpha_Evolve") as demo:
gr.Markdown("# 🧬 OpenAlpha_Evolve: Autonomous Algorithm Evolution")
gr.Markdown("""
* **Custom Tasks:** Write your own problem definition, examples, and allowed imports in the fields below.
* **Multi-Model Support:** Additional language model backends coming soon.
* **Evolutionary Budget:** For novel, complex solutions consider using large budgets (e.g., 100+ generations and population sizes of hundreds or thousands).
* **Island Model:** The population is divided into islands that evolve independently, with periodic migration between them.
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## Task Definition")
task_id = gr.Textbox(
label="Task ID",
placeholder="e.g., fibonacci_task",
value="fibonacci_task"
)
description = gr.Textbox(
label="Task Description",
placeholder="Describe the problem clearly...",
value="Write a Python function that computes the nth Fibonacci number (0-indexed), where fib(0)=0 and fib(1)=1.",
lines=5
)
function_name = gr.Textbox(
label="Function Name to Evolve",
placeholder="e.g., fibonacci",
value="fibonacci"
)
examples_json = gr.Code(
label="Input/Output Examples (JSON)",
language="json",
value=FIB_EXAMPLES,
lines=10
)
allowed_imports = gr.Textbox(
label="Allowed Imports (comma-separated)",
placeholder="e.g., math",
value=""
)
with gr.Row():
population_size = gr.Slider(
label="Population Size",
minimum=2,
maximum=10,
value=3,
step=1
)
generations = gr.Slider(
label="Generations",
minimum=1,
maximum=5,
value=2,
step=1
)
with gr.Row():
num_islands = gr.Slider(
label="Number of Islands",
minimum=1,
maximum=5,
value=3,
step=1
)
migration_frequency = gr.Slider(
label="Migration Frequency (generations)",
minimum=1,
maximum=5,
value=2,
step=1
)
migration_rate = gr.Slider(
label="Migration Rate",
minimum=0.1,
maximum=0.5,
value=0.2,
step=0.1
)
with gr.Row():
example_btn = gr.Button("📘 Fibonacci Example")
run_btn = gr.Button("🚀 Run Evolution", variant="primary")
with gr.Column(scale=1):
with gr.Tab("Results"):
results_text = gr.Markdown("Evolution results will appear here...")
example_btn.click(
set_fib_example,
outputs=[task_id, description, function_name, examples_json, allowed_imports]
)
run_evolution_event = run_btn.click(
run_evolution,
inputs=[
task_id,
description,
function_name,
examples_json,
allowed_imports,
population_size,
generations,
num_islands,
migration_frequency,
migration_rate
],
outputs=results_text
)
if __name__ == "__main__":
demo.launch(share=True)