Repository navigation
Expand file tree
/
Copy pathrun_evolution_demo.py
More file actions
270 lines (214 loc) · 10.3 KB
/
Copy pathrun_evolution_demo.py
File metadata and controls
270 lines (214 loc) · 10.3 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
#!/usr/bin/env python3
"""
Comprehensive demonstration of MCTS + Evolution system with persistence
Shows DSL evolution over multiple cycles with GPT-4o integration
"""
import asyncio
import os
import time
from dotenv import load_dotenv
from typing import Dict, Any
# Load environment variables
load_dotenv()
from dsl import DSL
from mcts_gpt4o import GPT4BootstrapSystem
from llm_integration import LLMConfig
from persistence import SessionManager, DSLPersistence
class EvolutionDemo:
"""Comprehensive demo showing DSL evolution"""
def __init__(self, session_name: str = "evolution_demo"):
self.session_name = session_name
self.session = SessionManager(session_name)
# Configure LLM for demo (smaller parameters for cost control)
self.llm_config = LLMConfig(
model="gpt-4o-mini", # Cheaper variant for demo
temperature=0.7,
max_tokens=300,
rate_limit_delay=0.3,
max_retries=2
)
print(f"🚀 Starting Evolution Demo: Session '{session_name}'")
print(f"💰 Using {self.llm_config.model} for cost efficiency")
async def run_complete_demo(self, num_cycles: int = 3, resume: bool = True):
"""Run complete evolution demonstration"""
print("\n" + "="*60)
print("🧬 MCTS + EVOLUTION DSL DEMONSTRATION")
print("="*60)
# Initialize or resume session
dsl = self.session.start_session(resume=resume)
self._show_dsl_state(dsl, "INITIAL")
# Create bootstrap system
system = GPT4BootstrapSystem(initial_dsl=dsl, llm_config=self.llm_config)
# Track evolution metrics
evolution_metrics = []
print(f"\n🔄 Running {num_cycles} evolution cycles...")
for cycle in range(num_cycles):
print(f"\n{'='*40}")
print(f"🔬 CYCLE {cycle + 1}/{num_cycles}")
print(f"{'='*40}")
cycle_start = time.time()
try:
# Define progressive tasks for each cycle
target_tasks = self._get_cycle_tasks(cycle)
# Run bootstrap cycle
summary = await system.run_bootstrap_cycle_async(
target_tasks=target_tasks,
mcts_iterations=15, # Small for demo
evolution_generations=3, # Small for demo
cycle_id=cycle
)
cycle_time = time.time() - cycle_start
summary['cycle_time'] = cycle_time
# Save cycle results
self.session.save_cycle(dsl, cycle, summary)
# Track metrics
evolution_metrics.append(summary)
# Show progress
self._show_cycle_results(cycle, summary, dsl)
# Export current state
if cycle == num_cycles - 1: # Last cycle
self._export_final_results(dsl)
except Exception as e:
print(f"❌ Error in cycle {cycle}: {e}")
print("Continuing with next cycle...")
continue
# Show final summary
await self._show_final_summary(evolution_metrics, dsl)
print("\n✅ Evolution demonstration complete!")
return evolution_metrics
def _get_cycle_tasks(self, cycle: int) -> list:
"""Get progressive tasks for each cycle"""
task_progression = [
["factorial", "power"], # Cycle 0: Basic math
["fibonacci", "sum_range"], # Cycle 1: Sequences
["max_value", "sort_pair"] # Cycle 2: Comparisons
]
if cycle < len(task_progression):
return task_progression[cycle]
else:
return ["advanced_function", "optimization"]
def _show_dsl_state(self, dsl: DSL, stage: str):
"""Display current DSL state"""
print(f"\n📋 DSL STATE - {stage}")
print("-" * 30)
primitives = []
evolved = []
for name, func in dsl.functions.items():
if func.body is None: # Primitive function
primitives.append(name)
else: # Evolved function
evolved.append((name, func.fitness_score))
print(f"🔧 Primitives ({len(primitives)}): {', '.join(primitives)}")
if evolved:
print(f"🧬 Evolved ({len(evolved)}):")
for name, fitness in sorted(evolved, key=lambda x: x[1], reverse=True):
print(f" • {name} (fitness: {fitness:.3f})")
else:
print("🧬 Evolved (0): None yet")
print(f"📊 Total functions: {len(dsl.functions)}")
def _show_cycle_results(self, cycle: int, summary: Dict[str, Any], dsl: DSL):
"""Display results from a cycle"""
print(f"\n📈 CYCLE {cycle + 1} RESULTS")
print("-" * 25)
print(f"⏱️ Time: {summary.get('cycle_time', 0):.1f}s")
print(f"🔧 Functions added: {summary.get('new_functions', 0)}")
print(f"📊 DSL size: {summary.get('start_dsl_size', 0)} → {summary.get('end_dsl_size', 0)}")
print(f"🎯 MCTS programs: {summary.get('mcts_programs', 0)}")
print(f"🧬 Evolved candidates: {summary.get('evolved_functions', 0)}")
llm_stats = summary.get('llm_stats', {})
if llm_stats:
print(f"🤖 LLM calls: {llm_stats.get('total_calls', 0)}")
print(f"💰 Est. cost: ${llm_stats.get('total_calls', 0) * 0.002:.3f}")
# Show new functions added this cycle
if summary.get('new_functions', 0) > 0:
print("\n🆕 New functions discovered:")
current_evolved = [(name, func.fitness_score) for name, func in dsl.functions.items()
if func.body is not None]
recent_functions = sorted(current_evolved, key=lambda x: x[1], reverse=True)[:summary.get('new_functions', 0)]
for name, fitness in recent_functions:
print(f" ✨ {name} (fitness: {fitness:.3f})")
if name in dsl.functions and dsl.functions[name].body:
# Show a snippet of the code
code_lines = dsl.functions[name].body.split('\n')
if len(code_lines) > 1:
print(f" {code_lines[1].strip()}") # Show function signature line
def _export_final_results(self, dsl: DSL):
"""Export final results for inspection"""
print(f"\n💾 EXPORTING FINAL RESULTS")
print("-" * 30)
persistence = DSLPersistence(f"sessions/{self.session_name}")
# Export as Python code
python_file = persistence.export_to_python(dsl)
print(f"🐍 Python code: {python_file}")
# Show file contents preview
try:
with open(python_file, 'r') as f:
lines = f.readlines()
if len(lines) > 20:
print("📄 Preview (first 20 lines):")
for i, line in enumerate(lines[:20]):
print(f" {i+1:2d}: {line.rstrip()}")
print(f" ... ({len(lines)-20} more lines)")
except Exception as e:
print(f"❌ Could not preview file: {e}")
async def _show_final_summary(self, metrics: list, dsl: DSL):
"""Show final evolution summary"""
print(f"\n🎉 EVOLUTION SUMMARY")
print("=" * 40)
if not metrics:
print("❌ No successful cycles completed")
return
# Overall statistics
total_functions_added = sum(m.get('new_functions', 0) for m in metrics)
total_time = sum(m.get('cycle_time', 0) for m in metrics)
total_llm_calls = sum(m.get('llm_stats', {}).get('total_calls', 0) for m in metrics)
estimated_cost = total_llm_calls * 0.002 # Rough estimate
print(f"🔄 Cycles completed: {len(metrics)}")
print(f"⏱️ Total time: {total_time:.1f}s")
print(f"🧬 Functions discovered: {total_functions_added}")
print(f"📊 Final DSL size: {len(dsl.functions)}")
print(f"🤖 Total LLM calls: {total_llm_calls}")
print(f"💰 Estimated cost: ${estimated_cost:.3f}")
# Evolution trajectory
print(f"\n📈 EVOLUTION TRAJECTORY")
print("-" * 25)
for i, metric in enumerate(metrics):
functions_added = metric.get('new_functions', 0)
dsl_size = metric.get('end_dsl_size', 0)
cycle_time = metric.get('cycle_time', 0)
print(f"Cycle {i+1}: +{functions_added} functions → {dsl_size} total ({cycle_time:.1f}s)")
# Show session summary
session_summary = self.session.get_session_summary()
print(f"\n📋 SESSION SUMMARY")
print("-" * 20)
for key, value in session_summary.items():
print(f"{key}: {value}")
# Final DSL state
self._show_dsl_state(dsl, "FINAL")
async def main():
"""Main demonstration entry point"""
# Check API key
if not os.getenv("OPENAI_API_KEY"):
print("❌ OPENAI_API_KEY not found in environment")
print("Please set your OpenAI API key and try again")
return
print("🔑 OpenAI API key found")
# Create and run demo
demo = EvolutionDemo("comprehensive_demo")
try:
# Run the evolution demo
await demo.run_complete_demo(
num_cycles=3, # Start with 3 cycles
resume=False # Start fresh for demo
)
print("\n🎯 Demo completed successfully!")
print(f"📁 Results saved in sessions/{demo.session_name}/")
print("🔍 Check the generated files to see your evolved DSL!")
except KeyboardInterrupt:
print("\n⏹️ Demo interrupted by user")
except Exception as e:
print(f"\n❌ Demo failed: {e}")
print("This might be due to API limits, network issues, or other factors")
if __name__ == "__main__":
print("🚀 MCTS + Evolution DSL Demo Starting...")
asyncio.run(main())