File size: 2,443 Bytes
0dfa258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import os

LOG_FILE  = os.path.join(os.path.dirname(__file__), "rewards_log.csv")
OUT_DIR   = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(OUT_DIR, exist_ok=True)

df = pd.read_csv(LOG_FILE)
df["global_step"] = range(len(df))
df["rolling_avg"] = df["reward"].rolling(window=10, min_periods=1).mean()

# -- Plot 1: Reward curve -------------------------------
fig, ax = plt.subplots(figsize=(12, 5))

ax.plot(df["global_step"], df["reward"],
        alpha=0.25, color="#4A90D9", linewidth=1, label="raw reward")
ax.plot(df["global_step"], df["rolling_avg"],
        color="#1a5fa8", linewidth=2.5, label="10-step rolling avg")
ax.axhline(0.5, linestyle="--", color="#999999", linewidth=1, label="baseline (0.5)")

ax.set_xlabel("Training Step", fontsize=13)
ax.set_ylabel("Reward (0 - 1)", fontsize=13)
ax.set_title("CodeArena - Agent Reward Over Training", fontsize=15, fontweight="bold")
ax.set_ylim(0, 1.05)
ax.legend(fontsize=11)
ax.grid(axis="y", alpha=0.3)
plt.tight_layout()
plt.savefig(os.path.join(OUT_DIR, "reward_curve.png"), dpi=150)
plt.close()
print("Saved: results/reward_curve.png")

# -- Plot 2: Reward by task -----------------------------
task_avg = df.groupby("task_id")["reward"].mean().sort_values(ascending=False)

fig, ax = plt.subplots(figsize=(8, 5))
colors = ["#2ecc71" if v > 0.7 else "#f39c12" if v > 0.4 else "#e74c3c"
          for v in task_avg.values]
bars = ax.bar(task_avg.index, task_avg.values, color=colors, edgecolor="white", width=0.5)

for bar, val in zip(bars, task_avg.values):
    ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.02,
            f"{val:.2f}", ha="center", fontsize=11, fontweight="bold")

ax.set_xlabel("Task Category", fontsize=13)
ax.set_ylabel("Average Reward", fontsize=13)
ax.set_title("CodeArena - Average Reward by Task Category", fontsize=15, fontweight="bold")
ax.set_ylim(0, 1.15)
ax.grid(axis="y", alpha=0.3)

legend_patches = [
    mpatches.Patch(color="#2ecc71", label="> 0.70 (strong)"),
    mpatches.Patch(color="#f39c12", label="0.40-0.70 (learning)"),
    mpatches.Patch(color="#e74c3c", label="< 0.40 (struggling)")
]
ax.legend(handles=legend_patches, fontsize=10)
plt.tight_layout()
plt.savefig(os.path.join(OUT_DIR, "reward_by_task.png"), dpi=150)
plt.close()
print("Saved: results/reward_by_task.png")
print("\nAll plots saved to results/")