-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyper_evolution.py
More file actions
228 lines (191 loc) · 9.72 KB
/
hyper_evolution.py
File metadata and controls
228 lines (191 loc) · 9.72 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
"""
Hyper-Evolution Engine.
The recursive self-improvement loop for the Ultimate AI Agent.
Phase 21: The Singularity.
"""
import time
import random
import json
import asyncio
from datetime import datetime
from typing import Dict, List, Optional
class HyperEvolutionEngine:
def __init__(self, agent_instance):
self.agent = agent_instance
self.evolution_history = []
self.active = False
self.current_fitness = 0.5 # Baseline
async def start_loop(self):
"""Activates the recursive improvement loop."""
self.active = True
print("HYPER-EVOLUTION MODE ACTIVATED")
print("The agent is now self-evolving. Press Ctrl+C to stop.")
while self.active:
try:
await self.run_one_cycle()
# Wait before next cycle - Non-blocking
if getattr(self.agent, "god_mode", False):
await asyncio.sleep(0.1) # God Mode: Machine speed
else:
await asyncio.sleep(5) # Safe Mode: Human speed
except KeyboardInterrupt:
self.stop_loop()
except Exception as e:
print(f"[Evolution] Error: {e}")
self.active = False
async def run_one_cycle(self):
"""Runs a single evolution cycle."""
# 1. Analyze Performance (Fitness Function)
fitness = self._calculate_fitness()
print(f"[Evolution] Current Fitness: {fitness:.4f}")
# 2. Hypothesis Generation & A/B Testing
if fitness < 0.8: # If not perfect, try to improve
print("[Evolution] Fitness sub-optimal. Initiating A/B Testing for improvements...")
hypotheses = self._generate_competing_hypotheses()
# 3. A/B Testing Race Simulator
# In a real scenario, this would spawn sandboxed sub-agents to physically race the code
winner = await self._simulate_ab_test_race(hypotheses)
if winner:
print(f"[Evolution] A/B Test Complete. Winner: '{winner['description']}' (Score: {winner['score']:.2f})")
# 3.5 Register Update in Ledger (Authorize the Evolution)
if hasattr(self.agent, 'ledger'):
print(f"[Evolution] Authorizing winning change in Code Ledger...")
self.agent.ledger.register_update(f"Evolution [A/B Winner]: {winner['description']}")
# 4. Verification (Test)
if self._verify_integrity():
print("[Evolution] Modification verified. Replicating improved version...")
# 5. Replication
self.agent.replicator.replicate_local(self.agent.session_id, self.agent.generation)
else:
print("[Evolution] Integrity check failed. Reverting...")
else:
print("[Evolution] A/B Test failed to produce a viable winner.")
def stop_loop(self):
self.active = False
print("Hyper-Evolution deactivated.")
def _calculate_fitness(self) -> float:
"""
Calculates a score (0.0 - 1.0) based on agent performance.
Metrics: Revenue (50%), Efficiency (30%), Stability (20%)
"""
# Mock metrics for simulation
revenue = self.agent.db.get_total_revenue()
efficiency = random.uniform(0.7, 0.95) # Mock cpu/mem efficiency
stability = 1.0 if not self.agent.ledger.verify_integrity() else 0.0
# Normalize revenue (e.g., target 10k)
rev_score = min(revenue / 10000.0, 1.0)
score = (rev_score * 0.5) + (efficiency * 0.3) + (stability * 0.2)
return score
def _generate_competing_hypotheses(self) -> List[Dict]:
"""Generates multiple varied hypotheses for the A/B test."""
import os
if os.path.exists("sandbox_module.py"):
return [
{"id": "A", "target": "sandbox_module.py", "type": "add_feature", "description": "Add evolved_method to sandbox (Variant A)", "priority": 1},
{"id": "B", "target": "sandbox_module.py", "type": "optimize_feature", "description": "Add evolved_method to sandbox with caching (Variant B)", "priority": 2}
]
return [
{"id": "A", "target": "database.py", "type": "optimize_query", "description": "Index optimization for speed (Variant A)"},
{"id": "B", "target": "database.py", "type": "memory_caching", "description": "In-memory LRU caching (Variant B)"},
{"id": "C", "target": "sales_engine.py", "type": "improve_prompt", "description": "Enhance persuasion tactics with emotional hooks (Variant C)"}
]
async def _simulate_ab_test_race(self, hypotheses: List[Dict]) -> Optional[Dict]:
"""Runs competing modifications in parallel and scores them using real execution benchmarking."""
import tempfile
import subprocess
results = []
# We simulate a race by writing competing codes to temp files and benchmarking them.
benchmark_code = """
import time
def run_benchmark():
start = time.time()
# Dummy workload representing the specific optimized behavior
total = sum([i * 2 for i in range(100000)])
end = time.time()
print(f"{end - start}")
run_benchmark()
"""
for hyp in hypotheses:
print(f" 🏎️ Racing Variant {hyp['id']}: {hyp['description']}")
success = await self._simulate_modification(hyp)
if success:
try:
# Write the benchmark logic into a temp file
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
f.write(benchmark_code)
temp_name = f.name
# Execute and measure time
proc = await asyncio.create_subprocess_exec(
"python", temp_name,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = await proc.communicate()
import os
os.unlink(temp_name)
if proc.returncode == 0:
exec_time = float(stdout.decode().strip())
# Lower execution time = higher score (inverse relationship)
score = max(0.1, 1.0 - (exec_time * 10))
# Add a small random jitter to prevent static ties
score += random.uniform(-0.05, 0.05)
results.append({"hypothesis": hyp, "score": score})
print(f" ✅ Variant {hyp['id']} benchmarked successfully. Score: {score:.2f} (Time: {exec_time:.4f}s)")
else:
print(f" ❌ Variant {hyp['id']} failed during benchmark execution.")
except Exception as e:
print(f" ❌ Variant {hyp['id']} benchmarking threw an error: {e}")
else:
print(f" ❌ Variant {hyp['id']} failed to compile or threw an error.")
if not results:
return None
# Pick the highest scoring winner
results.sort(key=lambda x: x["score"], reverse=True)
winner = results[0]["hypothesis"]
winner["score"] = results[0]["score"]
return winner
async def _simulate_modification(self, hypothesis: Dict) -> bool:
"""Executes the coding process (simulated or sandbox)."""
await asyncio.sleep(1)
target = hypothesis.get("target")
# --- SANDBOX MODE (For Verification) ---
if target == "sandbox_module.py":
print(f"[Evolution] SANDBOX: Modifying {target}...")
try:
# Import here to avoid circular deps
from self_mod_engine import SelfModEngine
import os
# Create a temporary engine pointing to the sandbox
sandbox_engine = SelfModEngine("sandbox_module.py", backup_dir="tests/backups", safety_mode=False)
# Define the evolved class structure
new_class_code = (
"class EvolutionTarget:\n"
" # Target for safe evolution testing.\n"
" def original_method(self):\n"
" return 'I am original'\n"
"\n"
" def evolved_method(self):\n"
" return 'EVOLUTION SUCCESS'"
)
# Execute the modification
# Note: modify_core_class is synchronous, so we wrap it
result = await asyncio.to_thread(
sandbox_engine.modify_core_class,
"EvolutionTarget",
new_class_code
)
if result["success"]:
print(f"[Evolution] SANDBOX SUCCESS: {result['message']}")
return True
else:
print(f"[Evolution] SANDBOX FAILED: {result['error']}")
return False
except Exception as e:
print(f"[Evolution] SANDBOX ERROR: {e}")
return False
# --- Normal Simulation ---
# Random success rate (simulating compiler errors/logic bugs)
return random.random() > 0.2
def _verify_integrity(self) -> bool:
"""Ensures the core is still valid."""
return not self.agent.ledger.verify_integrity()