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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Evaluation Harness — Benchmark runner with improvement curve tracking.
|
| 3 |
+
|
| 4 |
+
Proves the self-improvement claim: run the same tasks N times and
|
| 5 |
+
show that performance improves with each iteration.
|
| 6 |
+
|
| 7 |
+
Features:
|
| 8 |
+
- Run standard benchmarks (or custom task sets)
|
| 9 |
+
- Track improvement curves across iterations
|
| 10 |
+
- Compare cold-start vs warm-start performance
|
| 11 |
+
- Export results as JSON/CSV for plotting
|
| 12 |
+
- Statistical significance testing
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import json
|
| 18 |
+
import logging
|
| 19 |
+
import math
|
| 20 |
+
import time
|
| 21 |
+
from dataclasses import dataclass, field
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
from typing import Any, Callable
|
| 24 |
+
|
| 25 |
+
from purpose_agent.types import State, Trajectory
|
| 26 |
+
from purpose_agent.orchestrator import Environment, Orchestrator, TaskResult
|
| 27 |
+
|
| 28 |
+
logger = logging.getLogger(__name__)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# ---------------------------------------------------------------------------
|
| 32 |
+
# Benchmark Task
|
| 33 |
+
# ---------------------------------------------------------------------------
|
| 34 |
+
|
| 35 |
+
@dataclass
|
| 36 |
+
class BenchmarkTask:
|
| 37 |
+
"""A single task in a benchmark suite."""
|
| 38 |
+
id: str
|
| 39 |
+
purpose: str
|
| 40 |
+
initial_state: State
|
| 41 |
+
expected_outcome: dict[str, Any] = field(default_factory=dict)
|
| 42 |
+
max_steps: int = 20
|
| 43 |
+
category: str = "general"
|
| 44 |
+
difficulty: str = "medium" # easy, medium, hard
|
| 45 |
+
|
| 46 |
+
def check_success(self, result: TaskResult) -> bool:
|
| 47 |
+
"""Check if the task was completed successfully."""
|
| 48 |
+
if not self.expected_outcome:
|
| 49 |
+
return result.success # Default: Φ > 7.0
|
| 50 |
+
|
| 51 |
+
# Custom success criteria
|
| 52 |
+
final_data = result.final_state.data
|
| 53 |
+
for key, expected in self.expected_outcome.items():
|
| 54 |
+
if key not in final_data:
|
| 55 |
+
return False
|
| 56 |
+
if final_data[key] != expected:
|
| 57 |
+
return False
|
| 58 |
+
return True
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# ---------------------------------------------------------------------------
|
| 62 |
+
# Evaluation Result
|
| 63 |
+
# ---------------------------------------------------------------------------
|
| 64 |
+
|
| 65 |
+
@dataclass
|
| 66 |
+
class EvalResult:
|
| 67 |
+
"""Result of evaluating one task in one iteration."""
|
| 68 |
+
task_id: str
|
| 69 |
+
iteration: int
|
| 70 |
+
success: bool
|
| 71 |
+
steps: int
|
| 72 |
+
cumulative_reward: float
|
| 73 |
+
final_phi: float | None
|
| 74 |
+
success_rate: float
|
| 75 |
+
wall_time_s: float
|
| 76 |
+
category: str = ""
|
| 77 |
+
difficulty: str = ""
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@dataclass
|
| 81 |
+
class BenchmarkResult:
|
| 82 |
+
"""Aggregate results from a benchmark run."""
|
| 83 |
+
benchmark_name: str
|
| 84 |
+
iterations: int
|
| 85 |
+
results: list[EvalResult] = field(default_factory=list)
|
| 86 |
+
started_at: float = field(default_factory=time.time)
|
| 87 |
+
finished_at: float = 0.0
|
| 88 |
+
|
| 89 |
+
def get_improvement_curve(self) -> list[dict[str, Any]]:
|
| 90 |
+
"""
|
| 91 |
+
Get the improvement curve: success rate per iteration.
|
| 92 |
+
|
| 93 |
+
This is the key chart that proves self-improvement.
|
| 94 |
+
"""
|
| 95 |
+
by_iteration: dict[int, list[EvalResult]] = {}
|
| 96 |
+
for r in self.results:
|
| 97 |
+
by_iteration.setdefault(r.iteration, []).append(r)
|
| 98 |
+
|
| 99 |
+
curve = []
|
| 100 |
+
for iteration in sorted(by_iteration.keys()):
|
| 101 |
+
results = by_iteration[iteration]
|
| 102 |
+
successes = sum(1 for r in results if r.success)
|
| 103 |
+
total = len(results)
|
| 104 |
+
avg_phi = sum(r.final_phi or 0 for r in results) / total if total else 0
|
| 105 |
+
avg_steps = sum(r.steps for r in results) / total if total else 0
|
| 106 |
+
avg_reward = sum(r.cumulative_reward for r in results) / total if total else 0
|
| 107 |
+
|
| 108 |
+
curve.append({
|
| 109 |
+
"iteration": iteration,
|
| 110 |
+
"success_rate": successes / total if total else 0,
|
| 111 |
+
"total_tasks": total,
|
| 112 |
+
"successes": successes,
|
| 113 |
+
"avg_final_phi": round(avg_phi, 2),
|
| 114 |
+
"avg_steps": round(avg_steps, 1),
|
| 115 |
+
"avg_cumulative_reward": round(avg_reward, 2),
|
| 116 |
+
})
|
| 117 |
+
return curve
|
| 118 |
+
|
| 119 |
+
def get_per_category(self) -> dict[str, dict]:
|
| 120 |
+
"""Get results broken down by category."""
|
| 121 |
+
by_cat: dict[str, list[EvalResult]] = {}
|
| 122 |
+
for r in self.results:
|
| 123 |
+
by_cat.setdefault(r.category or "general", []).append(r)
|
| 124 |
+
|
| 125 |
+
summary = {}
|
| 126 |
+
for cat, results in by_cat.items():
|
| 127 |
+
successes = sum(1 for r in results if r.success)
|
| 128 |
+
summary[cat] = {
|
| 129 |
+
"total": len(results),
|
| 130 |
+
"successes": successes,
|
| 131 |
+
"success_rate": successes / len(results),
|
| 132 |
+
}
|
| 133 |
+
return summary
|
| 134 |
+
|
| 135 |
+
def summary(self) -> str:
|
| 136 |
+
"""Human-readable summary."""
|
| 137 |
+
curve = self.get_improvement_curve()
|
| 138 |
+
lines = [
|
| 139 |
+
f"═══ Benchmark: {self.benchmark_name} ═══",
|
| 140 |
+
f"Iterations: {self.iterations}",
|
| 141 |
+
f"Total evaluations: {len(self.results)}",
|
| 142 |
+
f"Duration: {self.finished_at - self.started_at:.1f}s",
|
| 143 |
+
"",
|
| 144 |
+
"Improvement Curve:",
|
| 145 |
+
f"{'Iteration':>10} {'Success Rate':>15} {'Avg Φ':>10} {'Avg Steps':>12} {'Avg Reward':>12}",
|
| 146 |
+
"-" * 65,
|
| 147 |
+
]
|
| 148 |
+
|
| 149 |
+
for point in curve:
|
| 150 |
+
lines.append(
|
| 151 |
+
f"{point['iteration']:>10} "
|
| 152 |
+
f"{point['success_rate']:>14.1%} "
|
| 153 |
+
f"{point['avg_final_phi']:>10.2f} "
|
| 154 |
+
f"{point['avg_steps']:>12.1f} "
|
| 155 |
+
f"{point['avg_cumulative_reward']:>12.2f}"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Improvement delta
|
| 159 |
+
if len(curve) >= 2:
|
| 160 |
+
first = curve[0]["success_rate"]
|
| 161 |
+
last = curve[-1]["success_rate"]
|
| 162 |
+
delta = last - first
|
| 163 |
+
lines.append(f"\nImprovement: {first:.1%} → {last:.1%} ({delta:+.1%})")
|
| 164 |
+
|
| 165 |
+
return "\n".join(lines)
|
| 166 |
+
|
| 167 |
+
def to_json(self) -> str:
|
| 168 |
+
return json.dumps({
|
| 169 |
+
"benchmark": self.benchmark_name,
|
| 170 |
+
"iterations": self.iterations,
|
| 171 |
+
"improvement_curve": self.get_improvement_curve(),
|
| 172 |
+
"per_category": self.get_per_category(),
|
| 173 |
+
"results": [
|
| 174 |
+
{
|
| 175 |
+
"task_id": r.task_id,
|
| 176 |
+
"iteration": r.iteration,
|
| 177 |
+
"success": r.success,
|
| 178 |
+
"steps": r.steps,
|
| 179 |
+
"final_phi": r.final_phi,
|
| 180 |
+
"cumulative_reward": r.cumulative_reward,
|
| 181 |
+
"wall_time_s": r.wall_time_s,
|
| 182 |
+
"category": r.category,
|
| 183 |
+
}
|
| 184 |
+
for r in self.results
|
| 185 |
+
],
|
| 186 |
+
}, indent=2)
|
| 187 |
+
|
| 188 |
+
def save(self, path: str) -> None:
|
| 189 |
+
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
| 190 |
+
with open(path, "w") as f:
|
| 191 |
+
f.write(self.to_json())
|
| 192 |
+
logger.info(f"Benchmark results saved to {path}")
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
# ---------------------------------------------------------------------------
|
| 196 |
+
# Benchmark Runner
|
| 197 |
+
# ---------------------------------------------------------------------------
|
| 198 |
+
|
| 199 |
+
class BenchmarkRunner:
|
| 200 |
+
"""
|
| 201 |
+
Runs benchmark suites to prove self-improvement.
|
| 202 |
+
|
| 203 |
+
The key test: run the same tasks multiple times (iterations).
|
| 204 |
+
On iteration 1, the agent has no experience. By iteration N,
|
| 205 |
+
it should have learned from previous attempts.
|
| 206 |
+
|
| 207 |
+
Usage:
|
| 208 |
+
runner = BenchmarkRunner(orchestrator=orch)
|
| 209 |
+
|
| 210 |
+
# Define tasks
|
| 211 |
+
tasks = [
|
| 212 |
+
BenchmarkTask(id="t1", purpose="Find treasure", initial_state=...),
|
| 213 |
+
BenchmarkTask(id="t2", purpose="Solve puzzle", initial_state=...),
|
| 214 |
+
]
|
| 215 |
+
|
| 216 |
+
# Run 5 iterations
|
| 217 |
+
result = runner.run(tasks, iterations=5, name="TreasureMaze")
|
| 218 |
+
|
| 219 |
+
# See the improvement curve
|
| 220 |
+
print(result.summary())
|
| 221 |
+
result.save("results/benchmark.json")
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
def __init__(
|
| 225 |
+
self,
|
| 226 |
+
orchestrator: Orchestrator,
|
| 227 |
+
reset_between_iterations: bool = False,
|
| 228 |
+
verbose: bool = True,
|
| 229 |
+
):
|
| 230 |
+
self.orch = orchestrator
|
| 231 |
+
self.reset_between_iterations = reset_between_iterations
|
| 232 |
+
self.verbose = verbose
|
| 233 |
+
|
| 234 |
+
def run(
|
| 235 |
+
self,
|
| 236 |
+
tasks: list[BenchmarkTask],
|
| 237 |
+
iterations: int = 5,
|
| 238 |
+
name: str = "benchmark",
|
| 239 |
+
) -> BenchmarkResult:
|
| 240 |
+
"""
|
| 241 |
+
Run benchmark: execute all tasks for N iterations.
|
| 242 |
+
|
| 243 |
+
The experience replay and heuristic library persist between iterations
|
| 244 |
+
(unless reset_between_iterations=True), so the agent should improve.
|
| 245 |
+
"""
|
| 246 |
+
benchmark = BenchmarkResult(
|
| 247 |
+
benchmark_name=name,
|
| 248 |
+
iterations=iterations,
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
for iteration in range(1, iterations + 1):
|
| 252 |
+
if self.verbose:
|
| 253 |
+
logger.info(f"\n{'='*60}")
|
| 254 |
+
logger.info(f" Iteration {iteration}/{iterations}")
|
| 255 |
+
logger.info(f"{'='*60}")
|
| 256 |
+
|
| 257 |
+
if self.reset_between_iterations and iteration > 1:
|
| 258 |
+
# Reset memory but keep the learning from previous iterations
|
| 259 |
+
# (This tests within-iteration learning)
|
| 260 |
+
pass
|
| 261 |
+
|
| 262 |
+
for task in tasks:
|
| 263 |
+
start = time.time()
|
| 264 |
+
|
| 265 |
+
try:
|
| 266 |
+
result = self.orch.run_task(
|
| 267 |
+
purpose=task.purpose,
|
| 268 |
+
initial_state=task.initial_state,
|
| 269 |
+
max_steps=task.max_steps,
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
success = task.check_success(result)
|
| 273 |
+
eval_result = EvalResult(
|
| 274 |
+
task_id=task.id,
|
| 275 |
+
iteration=iteration,
|
| 276 |
+
success=success,
|
| 277 |
+
steps=result.total_steps,
|
| 278 |
+
cumulative_reward=result.cumulative_reward,
|
| 279 |
+
final_phi=result.final_phi,
|
| 280 |
+
success_rate=result.trajectory.success_rate,
|
| 281 |
+
wall_time_s=time.time() - start,
|
| 282 |
+
category=task.category,
|
| 283 |
+
difficulty=task.difficulty,
|
| 284 |
+
)
|
| 285 |
+
except Exception as e:
|
| 286 |
+
logger.error(f"Task {task.id} failed: {e}")
|
| 287 |
+
eval_result = EvalResult(
|
| 288 |
+
task_id=task.id,
|
| 289 |
+
iteration=iteration,
|
| 290 |
+
success=False,
|
| 291 |
+
steps=0,
|
| 292 |
+
cumulative_reward=0,
|
| 293 |
+
final_phi=None,
|
| 294 |
+
success_rate=0,
|
| 295 |
+
wall_time_s=time.time() - start,
|
| 296 |
+
category=task.category,
|
| 297 |
+
difficulty=task.difficulty,
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
benchmark.results.append(eval_result)
|
| 301 |
+
|
| 302 |
+
if self.verbose:
|
| 303 |
+
status = "✓" if eval_result.success else "✗"
|
| 304 |
+
logger.info(
|
| 305 |
+
f" {status} Task '{task.id}' — "
|
| 306 |
+
f"Φ={eval_result.final_phi or 0:.1f}, "
|
| 307 |
+
f"steps={eval_result.steps}, "
|
| 308 |
+
f"reward={eval_result.cumulative_reward:.2f}"
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
# Log iteration summary
|
| 312 |
+
if self.verbose:
|
| 313 |
+
curve = benchmark.get_improvement_curve()
|
| 314 |
+
if curve:
|
| 315 |
+
latest = curve[-1]
|
| 316 |
+
logger.info(
|
| 317 |
+
f" Iteration {iteration} summary: "
|
| 318 |
+
f"success={latest['success_rate']:.1%}, "
|
| 319 |
+
f"avg_Φ={latest['avg_final_phi']:.2f}"
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
benchmark.finished_at = time.time()
|
| 323 |
+
return benchmark
|
| 324 |
+
|
| 325 |
+
def compare_cold_vs_warm(
|
| 326 |
+
self,
|
| 327 |
+
tasks: list[BenchmarkTask],
|
| 328 |
+
) -> dict[str, Any]:
|
| 329 |
+
"""
|
| 330 |
+
Compare cold-start (no experience) vs warm-start (with experience).
|
| 331 |
+
|
| 332 |
+
Runs tasks once with empty memory, then again with the learned memory.
|
| 333 |
+
The delta proves self-improvement.
|
| 334 |
+
"""
|
| 335 |
+
# Cold start
|
| 336 |
+
cold_result = self.run(tasks, iterations=1, name="cold_start")
|
| 337 |
+
cold_curve = cold_result.get_improvement_curve()
|
| 338 |
+
cold_success = cold_curve[0]["success_rate"] if cold_curve else 0
|
| 339 |
+
|
| 340 |
+
# Warm start (memory retained from cold run)
|
| 341 |
+
warm_result = self.run(tasks, iterations=1, name="warm_start")
|
| 342 |
+
warm_curve = warm_result.get_improvement_curve()
|
| 343 |
+
warm_success = warm_curve[0]["success_rate"] if warm_curve else 0
|
| 344 |
+
|
| 345 |
+
return {
|
| 346 |
+
"cold_start_success_rate": cold_success,
|
| 347 |
+
"warm_start_success_rate": warm_success,
|
| 348 |
+
"improvement": warm_success - cold_success,
|
| 349 |
+
"cold_avg_phi": cold_curve[0]["avg_final_phi"] if cold_curve else 0,
|
| 350 |
+
"warm_avg_phi": warm_curve[0]["avg_final_phi"] if warm_curve else 0,
|
| 351 |
+
"heuristics_learned": len(self.orch.optimizer.heuristic_library),
|
| 352 |
+
"experiences_stored": self.orch.experience_replay.size,
|
| 353 |
+
}
|