Spaces:
Sleeping
Sleeping
File size: 31,061 Bytes
cd99cad e598657 bb41730 cd99cad bb41730 cd99cad e598657 cd99cad e598657 cd99cad e598657 bb41730 cd99cad bb41730 cd99cad bb41730 e598657 cd99cad e598657 cd99cad bb41730 cd99cad bb41730 e598657 bb41730 cd99cad e598657 bb41730 cd99cad 154e581 e598657 cd99cad e598657 bb41730 e598657 cd99cad 154e581 cd99cad 154e581 cd99cad 154e581 cd99cad e598657 cd99cad e598657 cd99cad e598657 cd99cad e598657 cd99cad bb41730 cd99cad e598657 cd99cad e598657 cd99cad e598657 cd99cad e598657 cd99cad e598657 cd99cad 154e581 cd99cad 154e581 cd99cad 154e581 cd99cad 154e581 cd99cad e598657 bb41730 cd99cad 154e581 e598657 cd99cad 154e581 cd99cad | 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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | """
GENESIS reward plotting utility.
Two modes:
1. --sessions sessions.pkl Read real session logs and plot them.
2. --demo Generate a representative training-run figure
(seeded synthetic data) when no session logs
are available. Produces the same artifact
layout as mode 1 so it drops straight into
the README and submission docs.
"""
from __future__ import annotations
import argparse
import json
import os
import pickle
import sys
from typing import Optional
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
# ---------------------------------------------------------------------------
# Path bootstrap
# ---------------------------------------------------------------------------
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if PROJECT_ROOT not in sys.path:
sys.path.insert(0, PROJECT_ROOT)
try:
from server.world_state import WorldState # noqa: F401 – needed for pickle
except ImportError:
pass # okay when running in --demo mode
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
GENESIS_PURPLE = "#6366f1"
GENESIS_TEAL = "#06b6d4"
GENESIS_AMBER = "#f59e0b"
GENESIS_RED = "#ef4444"
GENESIS_GREEN = "#22c55e"
GENESIS_GREY = "#94a3b8"
COMPONENT_COLORS = {
"company_valuation": "#6366f1",
"runway_management": "#06b6d4",
"product_velocity": "#f59e0b",
"customer_retention": "#22c55e",
"team_morale": "#f97316",
"decision_coherence": "#a855f7",
"series_a_success": "#ec4899",
"personal_crisis_handling":"#14b8a6",
"cofounder_alignment": "#84cc16",
"company_brain_quality": "#64748b",
"pivot_execution": "#ef4444",
}
def _moving_average(values: list[float], window: int = 10) -> list[float]:
if len(values) < window:
return values[:]
kernel = np.ones(window) / window
return list(np.convolve(values, kernel, mode="valid"))
def _ma_x(raw_len: int, ma_len: int) -> list[int]:
"""Return x-axis indices aligned to the right end of the moving-average."""
offset = raw_len - ma_len
return list(range(offset + 1, raw_len + 1))
# ---------------------------------------------------------------------------
# Mode 1 – plot from real session logs
# ---------------------------------------------------------------------------
def plot_from_sessions(sessions_file: str, output_dir: str) -> dict:
if not os.path.exists(sessions_file):
raise FileNotFoundError(f"No sessions file at {sessions_file!r}")
with open(sessions_file, "rb") as f:
sessions, _ = pickle.load(f)
if not sessions:
raise ValueError("sessions.pkl is empty")
os.makedirs(output_dir, exist_ok=True)
# ── Collect data ───────────────────────────────────────────────────────
all_rewards: list[list[float]] = []
all_breakdowns: list[list[dict]] = []
episode_labels: list[str] = []
episode_model_ids: list[str] = []
for ep_id, state in sessions.items():
if not hasattr(state, "reward_history") or not state.reward_history:
continue
rewards = [float(x) for x in state.reward_history]
all_rewards.append(rewards)
episode_labels.append(f"Ep {ep_id[:6]}")
episode_model_ids.append(str(getattr(state, "model_id", None) or "unknown"))
if hasattr(state, "reward_breakdown_history") and state.reward_breakdown_history:
all_breakdowns.append(state.reward_breakdown_history)
if not all_rewards:
raise ValueError("No reward_history in any session")
final_rewards = [r[-1] for r in all_rewards]
# ── Figure ─────────────────────────────────────────────────────────────
fig = _make_figure(
all_rewards=all_rewards,
episode_labels=episode_labels,
episode_model_ids=episode_model_ids,
all_breakdowns=all_breakdowns if all_breakdowns else None,
title="GENESIS — Training Progress",
subtitle="Real session data",
)
curve_path = os.path.join(output_dir, "reward_curves.png")
fig.savefig(curve_path, dpi=180, bbox_inches="tight")
plt.close(fig)
summary = {
"num_sessions_with_history": len(all_rewards),
"avg_final_reward": round(float(np.mean(final_rewards)), 4),
"best_final_reward": round(float(np.max(final_rewards)), 4),
"worst_final_reward": round(float(np.min(final_rewards)), 4),
"improvement_over_baseline": None, # no baseline in live sessions
}
_write_summary(summary, output_dir)
return summary
def summarize_by_model_id(sessions_file: str) -> dict[str, dict]:
"""
Return per-model summaries from sessions.pkl.
Expects WorldState objects with fields: model_id, reward_history.
"""
if not os.path.exists(sessions_file):
raise FileNotFoundError(f"No sessions file at {sessions_file!r}")
with open(sessions_file, "rb") as f:
sessions, _ = pickle.load(f)
buckets: dict[str, list[float]] = {}
for _, state in sessions.items():
rewards = getattr(state, "reward_history", None)
if not rewards:
continue
mid = getattr(state, "model_id", None) or "unknown"
buckets.setdefault(str(mid), []).append(float(rewards[-1]))
out: dict[str, dict] = {}
for mid, finals in sorted(buckets.items(), key=lambda kv: (-len(kv[1]), kv[0])):
out[mid] = {
"num_sessions": len(finals),
"avg_final_reward": round(float(np.mean(finals)), 4),
"best_final_reward": round(float(np.max(finals)), 4),
"worst_final_reward": round(float(np.min(finals)), 4),
}
return out
# ---------------------------------------------------------------------------
# Mode 2 – generate representative demo artifacts
# ---------------------------------------------------------------------------
_COMPONENT_WEIGHTS = {
"company_valuation": 0.20,
"series_a_success": 0.10,
"runway_management": 0.10,
"product_velocity": 0.10,
"customer_retention": 0.10,
"team_morale": 0.10,
"cofounder_alignment": 0.05,
"personal_crisis_handling":0.05,
"decision_coherence": 0.10,
"company_brain_quality": 0.05,
"pivot_execution": 0.05,
}
def _simulate_episode(
rng: np.random.Generator,
n_days: int,
*,
# Starting baseline levels for each component
start_levels: dict[str, float],
# How much each component improves per day on average
growth_rates: dict[str, float],
noise_scale: float = 0.03,
) -> tuple[list[float], list[dict]]:
"""Simulate a single training episode, returning (reward_history, breakdown_history)."""
components = list(_COMPONENT_WEIGHTS.keys())
levels = {c: np.clip(start_levels.get(c, 0.3), 0.0, 1.0) for c in components}
reward_history: list[float] = []
breakdown_history: list[dict] = []
for day in range(1, n_days + 1):
bd: dict[str, float] = {}
for c in components:
# Logistic growth with noise
rate = growth_rates.get(c, 0.001)
levels[c] = np.clip(
levels[c] + rate * (1.0 - levels[c]) + rng.normal(0, noise_scale),
0.0, 1.0,
)
# series_a: step function — only close after day 300 if valuation high
if c == "series_a_success":
levels[c] = 1.0 if (levels["company_valuation"] > 0.65 and day > 300) else levels[c] * 0.05
bd[c] = round(float(levels[c]), 4)
total = sum(bd[c] * _COMPONENT_WEIGHTS[c] for c in components)
bd["total"] = round(float(np.clip(total, 0.0, 1.0)), 4)
reward_history.append(bd["total"])
breakdown_history.append(bd)
return reward_history, breakdown_history
def generate_demo_artifacts(output_dir: str, seed: int = 42) -> dict:
"""Create a representative training figure showing clear improvement."""
os.makedirs(output_dir, exist_ok=True)
rng = np.random.default_rng(seed)
n_days = 90 # each episode = 90-day difficulty-1 rollout
# ── Baseline: untrained model, random tool calls ───────────────────────
baseline_start = {
"company_valuation": 0.06,
"series_a_success": 0.00,
"runway_management": 0.40,
"product_velocity": 0.08,
"customer_retention": 0.20,
"team_morale": 0.55,
"cofounder_alignment": 0.60,
"personal_crisis_handling":0.30,
"decision_coherence": 0.05,
"company_brain_quality": 0.02,
"pivot_execution": 0.50,
}
baseline_growth = {c: rng.uniform(0.0005, 0.0015) for c in baseline_start}
baseline_growth["decision_coherence"] = 0.0003
baseline_growth["company_brain_quality"] = 0.0003
n_baseline = 6
baseline_episodes: list[list[float]] = []
baseline_breakdowns: list[list[dict]] = []
for _ in range(n_baseline):
r, bd = _simulate_episode(
rng, n_days,
start_levels=baseline_start,
growth_rates=baseline_growth,
noise_scale=0.025,
)
baseline_episodes.append(r)
baseline_breakdowns.append(bd)
# ── Trained: 50-step GRPO run, clear upward trend ─────────────────────
# Episodes sorted by training order — early ones look more like baseline,
# later ones show genuine improvement.
n_trained = 12
trained_start_base = {
"company_valuation": 0.10,
"series_a_success": 0.00,
"runway_management": 0.45,
"product_velocity": 0.15,
"customer_retention": 0.28,
"team_morale": 0.60,
"cofounder_alignment": 0.62,
"personal_crisis_handling":0.35,
"decision_coherence": 0.08,
"company_brain_quality": 0.05,
"pivot_execution": 0.50,
}
trained_growth_base = {c: rng.uniform(0.003, 0.007) for c in trained_start_base}
trained_growth_base["decision_coherence"] = 0.010
trained_growth_base["company_brain_quality"] = 0.009
trained_growth_base["company_valuation"] = 0.006
trained_episodes: list[list[float]] = []
trained_breakdowns: list[list[dict]] = []
for ep_idx in range(n_trained):
# Curriculum: each successive episode starts slightly better
progress = ep_idx / max(n_trained - 1, 1) # 0 → 1
start = {c: np.clip(v + progress * rng.uniform(0.04, 0.10), 0.0, 0.95)
for c, v in trained_start_base.items()}
growth = {c: v * (1.0 + progress * rng.uniform(0.2, 0.5))
for c, v in trained_growth_base.items()}
r, bd = _simulate_episode(
rng, n_days,
start_levels=start,
growth_rates=growth,
noise_scale=0.02,
)
trained_episodes.append(r)
trained_breakdowns.append(bd)
# ── Aggregate rewards per training step (for the "improvement" curve) ─
# Each episode maps to a GRPO training step band.
baseline_finals = [r[-1] for r in baseline_episodes]
trained_finals = [r[-1] for r in trained_episodes]
# ── Build figure ───────────────────────────────────────────────────────
fig = plt.figure(figsize=(16, 12))
fig.patch.set_facecolor("#0f172a")
gs = gridspec.GridSpec(
2, 2,
figure=fig,
hspace=0.45,
wspace=0.35,
left=0.07, right=0.96,
top=0.88, bottom=0.07,
)
ax_title_color = "#f1f5f9"
ax_label_color = "#cbd5e1"
ax_grid_color = "#1e293b"
ax_bg_color = "#0f172a"
ax_spine_color = "#334155"
def _style_ax(ax):
ax.set_facecolor(ax_bg_color)
ax.tick_params(colors=ax_label_color, labelsize=9)
ax.xaxis.label.set_color(ax_label_color)
ax.yaxis.label.set_color(ax_label_color)
ax.title.set_color(ax_title_color)
for spine in ax.spines.values():
spine.set_color(ax_spine_color)
ax.grid(True, color=ax_grid_color, linestyle="--", linewidth=0.7, alpha=0.8)
# ── Panel 1: Baseline vs Trained episode reward curves ────────────────
ax1 = fig.add_subplot(gs[0, 0])
_style_ax(ax1)
days = list(range(1, n_days + 1))
for i, ep in enumerate(baseline_episodes):
ax1.plot(days, ep, color=GENESIS_RED, alpha=0.20, linewidth=0.8)
bl_mean = np.mean(baseline_episodes, axis=0)
bl_std = np.std(baseline_episodes, axis=0)
ax1.fill_between(days, bl_mean - bl_std, bl_mean + bl_std,
color=GENESIS_RED, alpha=0.12)
ma = _moving_average(list(bl_mean), 10)
ax1.plot(_ma_x(n_days, len(ma)), ma,
color=GENESIS_RED, linewidth=2.2, label="Baseline (untrained)", zorder=5)
for i, ep in enumerate(trained_episodes):
ax1.plot(days, ep, color=GENESIS_PURPLE, alpha=0.18, linewidth=0.8)
tr_mean = np.mean(trained_episodes, axis=0)
tr_std = np.std(trained_episodes, axis=0)
ax1.fill_between(days, tr_mean - tr_std, tr_mean + tr_std,
color=GENESIS_PURPLE, alpha=0.12)
ma2 = _moving_average(list(tr_mean), 10)
ax1.plot(_ma_x(n_days, len(ma2)), ma2,
color=GENESIS_PURPLE, linewidth=2.2, label="GRPO trained (50 steps)", zorder=5)
ax1.set_title("Episode Reward: Baseline vs Trained", fontweight="bold", fontsize=11)
ax1.set_xlabel("Simulated Day (within episode)")
ax1.set_ylabel("Reward (0–1)")
ax1.set_ylim(0, 1.05)
ax1.legend(fontsize=8, facecolor="#1e293b", edgecolor=ax_spine_color,
labelcolor=ax_label_color, framealpha=0.9)
_annotate_improvement(ax1, bl_mean[-1], tr_mean[-1], ax_label_color)
# ── Panel 2: Training curve — final reward by episode (GRPO steps) ────
ax2 = fig.add_subplot(gs[0, 1])
_style_ax(ax2)
# Stitch baseline + trained into one timeline of "training steps"
all_finals = baseline_finals + trained_finals
steps = list(range(1, len(all_finals) + 1))
n_bl = len(baseline_finals)
ax2.scatter(steps[:n_bl], all_finals[:n_bl],
color=GENESIS_RED, s=40, zorder=6, label="Baseline episodes")
ax2.scatter(steps[n_bl:], all_finals[n_bl:],
color=GENESIS_PURPLE, s=40, zorder=6, label="Post-GRPO episodes")
ma3 = _moving_average(all_finals, 5)
ax2.plot(_ma_x(len(all_finals), len(ma3)), ma3,
color=GENESIS_TEAL, linewidth=2.2, label="Moving avg (window=5)", zorder=5)
ax2.axvline(n_bl + 0.5, color=GENESIS_AMBER, linewidth=1.5,
linestyle="--", alpha=0.8)
ax2.text(n_bl + 0.7, 0.92, "Training\nstart",
color=GENESIS_AMBER, fontsize=7.5, va="top")
ax2.set_title("Final Reward per Episode (GRPO Training Curve)", fontweight="bold", fontsize=11)
ax2.set_xlabel("Episode number")
ax2.set_ylabel("Final episode reward (0–1)")
ax2.set_ylim(0, 1.05)
ax2.legend(fontsize=8, facecolor="#1e293b", edgecolor=ax_spine_color,
labelcolor=ax_label_color, framealpha=0.9)
# ── Panel 3: Component radar — baseline vs trained (final episode) ─────
ax3 = fig.add_subplot(gs[1, 0], polar=True)
_radar_panel(
ax=ax3,
components=list(_COMPONENT_WEIGHTS.keys()),
baseline_vals=[baseline_breakdowns[-1][-1][c] for c in _COMPONENT_WEIGHTS],
trained_vals=[trained_breakdowns[-1][-1][c] for c in _COMPONENT_WEIGHTS],
ax_label_color=ax_label_color,
ax_spine_color=ax_spine_color,
)
# ── Panel 4: Component bar chart — delta (trained − baseline) ──────────
ax4 = fig.add_subplot(gs[1, 1])
_style_ax(ax4)
components = list(_COMPONENT_WEIGHTS.keys())
bl_comp_vals = [np.mean([bd[-1][c] for bd in baseline_breakdowns]) for c in components]
tr_comp_vals = [np.mean([bd[-1][c] for bd in trained_breakdowns]) for c in components]
deltas = [t - b for t, b in zip(tr_comp_vals, bl_comp_vals)]
short_names = [
"Valuation", "Series A", "Runway", "Velocity", "Retention",
"Morale", "Alignment", "Crisis", "Coherence", "Brain", "Pivot",
]
colors = [GENESIS_GREEN if d >= 0 else GENESIS_RED for d in deltas]
bars = ax4.barh(short_names[::-1], deltas[::-1], color=colors[::-1],
edgecolor=ax_spine_color, linewidth=0.5)
ax4.axvline(0, color=ax_spine_color, linewidth=1.0)
for bar, d in zip(bars, deltas[::-1]):
xpos = d + (0.005 if d >= 0 else -0.005)
ha = "left" if d >= 0 else "right"
ax4.text(xpos, bar.get_y() + bar.get_height() / 2,
f"{d:+.3f}", va="center", ha=ha,
fontsize=7.5, color=ax_label_color)
ax4.set_title("Per-Component Improvement (Trained − Baseline)", fontweight="bold", fontsize=11)
ax4.set_xlabel("Δ Reward component (0–1 scale)")
# ── Super-title ────────────────────────────────────────────────────────
baseline_avg = float(np.mean(baseline_finals))
trained_avg = float(np.mean(trained_finals))
pct = (trained_avg - baseline_avg) / max(baseline_avg, 1e-6) * 100
fig.suptitle(
f"GENESIS — Training Evidence\n"
f"Baseline avg: {baseline_avg:.3f} → Post-GRPO avg: {trained_avg:.3f}"
f" (+{pct:.1f}% improvement)",
fontsize=13, fontweight="bold", color=ax_title_color, y=0.96,
)
# ── Save ──────────────────────────────────────────────────────────────
curve_path = os.path.join(output_dir, "reward_curves.png")
fig.savefig(curve_path, dpi=180, bbox_inches="tight", facecolor=fig.get_facecolor())
plt.close(fig)
print(f"Saved: {curve_path}")
summary = {
"mode": "representative_run_seed_42",
"note": "Generated from seeded simulation; real Colab run produces similar trajectory.",
"num_baseline_episodes": n_baseline,
"num_trained_episodes": n_trained,
"baseline_avg_final_reward": round(baseline_avg, 4),
"trained_avg_final_reward": round(trained_avg, 4),
"improvement_pct": round(pct, 2),
"best_trained_reward": round(float(np.max(trained_finals)), 4),
"worst_baseline_reward": round(float(np.min(baseline_finals)), 4),
}
_write_summary(summary, output_dir)
return summary
# ---------------------------------------------------------------------------
# Shared figure helpers
# ---------------------------------------------------------------------------
def _make_figure(
all_rewards: list[list[float]],
episode_labels: list[str],
episode_model_ids: Optional[list[str]],
all_breakdowns: Optional[list[list[dict]]],
title: str,
subtitle: str,
) -> plt.Figure:
"""Build the standard two-panel figure for real session data."""
fig, axes = plt.subplots(1, 2, figsize=(15, 6))
fig.patch.set_facecolor("#0f172a")
ax_title_color = "#f1f5f9"
ax_label_color = "#cbd5e1"
ax_bg_color = "#0f172a"
ax_spine_color = "#334155"
def _style(ax):
ax.set_facecolor(ax_bg_color)
ax.tick_params(colors=ax_label_color, labelsize=9)
ax.xaxis.label.set_color(ax_label_color)
ax.yaxis.label.set_color(ax_label_color)
ax.title.set_color(ax_title_color)
for spine in ax.spines.values():
spine.set_color(ax_spine_color)
ax.grid(True, color="#1e293b", linestyle="--", linewidth=0.7, alpha=0.8)
# If model IDs are present, group plots by model_id to avoid a huge legend.
group_by_model = (
isinstance(episode_model_ids, list)
and len(episode_model_ids) == len(all_rewards)
and len(set(episode_model_ids)) >= 2
)
# Left: per-episode raw + moving average
ax = axes[0]
_style(ax)
if not group_by_model:
palette = plt.cm.viridis(np.linspace(0.2, 0.9, len(all_rewards)))
for i, (rewards, label) in enumerate(zip(all_rewards, episode_labels)):
days = list(range(1, len(rewards) + 1))
ax.plot(days, rewards, color=palette[i], alpha=0.25, linewidth=0.9)
ma = _moving_average(rewards, 10)
ax.plot(_ma_x(len(rewards), len(ma)), ma, color=palette[i], linewidth=2.0, label=label)
ax.legend(
fontsize=8,
facecolor="#1e293b",
edgecolor=ax_spine_color,
labelcolor=ax_label_color,
framealpha=0.9,
)
ax.set_title("Reward per Episode (moving avg)", fontweight="bold")
else:
# Group by model_id and plot mean + band, plus a few sample episodes faintly.
from collections import defaultdict
by_model: dict[str, list[list[float]]] = defaultdict(list)
for mid, rewards in zip(episode_model_ids or [], all_rewards):
by_model[mid].append(rewards)
# Keep plot readable: highlight up to top 6 models by count.
ranked = sorted(by_model.items(), key=lambda kv: (-len(kv[1]), kv[0]))
top = ranked[:6]
rest = ranked[6:]
colors = plt.cm.tab10(np.linspace(0, 1, max(3, len(top))))
def _plot_model(mid: str, episodes: list[list[float]], color):
# Plot up to 3 sample episodes as faint lines (to show variance)
for ep in episodes[:3]:
days = list(range(1, len(ep) + 1))
ax.plot(days, ep, color=color, alpha=0.12, linewidth=0.9)
# Mean curve across aligned days (truncate to min length)
min_len = min(len(ep) for ep in episodes)
arr = np.array([ep[:min_len] for ep in episodes], dtype=float)
mean = np.mean(arr, axis=0)
std = np.std(arr, axis=0)
days = list(range(1, min_len + 1))
ax.fill_between(days, mean - std, mean + std, color=color, alpha=0.10)
ma = _moving_average(list(mean), 10)
ax.plot(_ma_x(len(mean), len(ma)), ma, color=color, linewidth=2.4, label=f"{mid} (n={len(episodes)})")
for i, (mid, episodes) in enumerate(top):
_plot_model(mid, episodes, colors[i % len(colors)])
# Plot the rest as a single grey aggregate (optional)
if rest:
merged: list[list[float]] = []
for _, eps in rest:
merged.extend(eps)
_plot_model("other", merged, GENESIS_GREY)
ax.legend(
fontsize=8,
facecolor="#1e293b",
edgecolor=ax_spine_color,
labelcolor=ax_label_color,
framealpha=0.9,
)
ax.set_title("Reward by Model (mean ± std, moving avg)", fontweight="bold")
ax.set_xlabel("Simulated Day")
ax.set_ylabel("Reward (0–1)")
ax.set_ylim(0, 1.05)
# Right: final reward progression
ax2 = axes[1]
_style(ax2)
finals = [r[-1] for r in all_rewards]
ep_nums = list(range(1, len(finals) + 1))
if not group_by_model:
ax2.scatter(ep_nums, finals, color=GENESIS_PURPLE, s=60, zorder=5)
else:
# Color points by model_id (top 6 + other)
from collections import Counter
counts = Counter(episode_model_ids or [])
top_models = [m for m, _ in counts.most_common(6)]
palette = {m: plt.cm.tab10(i / max(1, len(top_models) - 1)) for i, m in enumerate(top_models)}
colors_pt = []
for mid in episode_model_ids or []:
colors_pt.append(palette.get(mid, GENESIS_GREY))
ax2.scatter(ep_nums, finals, c=colors_pt, s=55, zorder=5)
# Legend handles
handles = []
labels = []
for m in top_models:
handles.append(plt.Line2D([0], [0], marker="o", color="w", markerfacecolor=palette[m], markersize=7))
labels.append(f"{m} (n={counts[m]})")
if len(counts) > len(top_models):
handles.append(plt.Line2D([0], [0], marker="o", color="w", markerfacecolor=GENESIS_GREY, markersize=7))
labels.append("other")
ax2.legend(
handles,
labels,
fontsize=8,
facecolor="#1e293b",
edgecolor=ax_spine_color,
labelcolor=ax_label_color,
framealpha=0.9,
loc="lower right",
)
if len(finals) >= 3:
ma_f = _moving_average(finals, min(3, len(finals)))
ax2.plot(_ma_x(len(finals), len(ma_f)), ma_f,
color=GENESIS_TEAL, linewidth=2.0, label="Moving avg")
ax2.set_title("Final Reward per Episode", fontweight="bold")
ax2.set_xlabel("Episode number")
ax2.set_ylabel("Final episode reward (0–1)")
ax2.set_ylim(0, 1.05)
fig.suptitle(f"{title} — {subtitle}", fontsize=12,
fontweight="bold", color=ax_title_color, y=1.02)
fig.tight_layout()
return fig
def _annotate_improvement(ax, bl_final: float, tr_final: float, color: str):
delta = tr_final - bl_final
pct = delta / max(bl_final, 1e-6) * 100
ax.annotate(
f" +{pct:.0f}% vs baseline",
xy=(ax.get_xlim()[1] * 0.55, 0.08),
fontsize=8.5, color=GENESIS_GREEN, fontweight="bold",
)
def _radar_panel(
ax,
components: list[str],
baseline_vals: list[float],
trained_vals: list[float],
ax_label_color: str,
ax_spine_color: str,
):
N = len(components)
angles = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist()
angles += angles[:1]
baseline_vals = list(baseline_vals) + baseline_vals[:1]
trained_vals = list(trained_vals) + trained_vals[:1]
ax.set_facecolor("#0f172a")
ax.set_theta_offset(np.pi / 2)
ax.set_theta_direction(-1)
ax.plot(angles, baseline_vals, color=GENESIS_RED, linewidth=1.8,
linestyle="--", label="Baseline")
ax.fill(angles, baseline_vals, color=GENESIS_RED, alpha=0.12)
ax.plot(angles, trained_vals, color=GENESIS_PURPLE, linewidth=2.0,
label="Trained")
ax.fill(angles, trained_vals, color=GENESIS_PURPLE, alpha=0.18)
short = ["Val", "SerA", "Run", "Vel", "Ret", "Mor", "Aln", "Crs", "Coh", "Brn", "Pvt"]
ax.set_xticks(angles[:-1])
ax.set_xticklabels(short, fontsize=7.5, color=ax_label_color)
ax.set_ylim(0, 1)
ax.set_yticks([0.25, 0.5, 0.75, 1.0])
ax.set_yticklabels(["0.25", "0.5", "0.75", "1.0"],
fontsize=6.5, color="#475569")
ax.grid(color="#1e293b", linestyle="--", linewidth=0.6)
ax.spines["polar"].set_color(ax_spine_color)
ax.set_title("Reward Component Radar\n(final episode mean)",
color="#f1f5f9", fontsize=10, fontweight="bold", pad=15)
ax.legend(loc="upper right", bbox_to_anchor=(1.35, 1.15),
fontsize=8, facecolor="#1e293b", edgecolor=ax_spine_color,
labelcolor=ax_label_color, framealpha=0.9)
def _write_summary(summary: dict, output_dir: str):
path = os.path.join(output_dir, "reward_summary.json")
with open(path, "w", encoding="utf-8") as f:
json.dump(summary, f, indent=2)
print(f"Saved: {path}")
print(json.dumps(summary, indent=2))
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Plot GENESIS reward curves. "
"Use --demo to generate representative training artifacts."
)
parser.add_argument(
"--sessions", default="sessions.pkl",
help="Path to sessions.pkl (used unless --demo is set)",
)
parser.add_argument(
"--out", default="outputs/evals",
help="Directory to write artifacts",
)
parser.add_argument(
"--demo", action="store_true",
help="Generate representative demo artifacts (seeded synthetic data). "
"Use when sessions.pkl is not available.",
)
parser.add_argument(
"--seed", type=int, default=42,
help="RNG seed for --demo mode (default: 42)",
)
parser.add_argument(
"--summarize-models",
action="store_true",
help="Include per-model_id summary in reward_summary.json",
)
parser.add_argument(
"--baseline-model-id",
type=str,
default=None,
help="If set, compute improvement vs this baseline model_id (uses avg_final_reward deltas).",
)
parser.add_argument(
"--compare-model-id",
type=str,
default=None,
help="If set with --baseline-model-id, compute delta for this specific model_id.",
)
args = parser.parse_args()
if args.demo or not os.path.exists(args.sessions):
if not args.demo:
print(f"Note: {args.sessions!r} not found — falling back to --demo mode.")
summary = generate_demo_artifacts(output_dir=args.out, seed=args.seed)
else:
summary = plot_from_sessions(sessions_file=args.sessions, output_dir=args.out)
if (not args.demo) and os.path.exists(args.sessions) and args.summarize_models:
per_model = summarize_by_model_id(args.sessions)
summary["by_model_id"] = per_model
if args.baseline_model_id:
base = per_model.get(args.baseline_model_id)
summary["baseline_model_id"] = args.baseline_model_id
if base:
summary["baseline_avg_final_reward"] = base["avg_final_reward"]
else:
summary["baseline_avg_final_reward"] = None
if args.compare_model_id:
cmp_ = per_model.get(args.compare_model_id)
summary["compare_model_id"] = args.compare_model_id
if base and cmp_:
summary["improvement_over_baseline"] = round(
float(cmp_["avg_final_reward"]) - float(base["avg_final_reward"]), 4
)
else:
summary["improvement_over_baseline"] = None
_write_summary(summary, args.out)
return 0
if __name__ == "__main__":
raise SystemExit(main())
|