Skip to content

Commit f8b0c01

Browse files
committed
Add opponent effect analysis, cdifflib to code_evolve analysis
1 parent edac183 commit f8b0c01

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

codeclash/analysis/code_evolve/main.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import matplotlib.pyplot as plt
1111
import numpy as np
1212
import yaml
13+
from cdifflib import CSequenceMatcher
1314
from tqdm.auto import tqdm
1415
from unidiff import PatchSet
1516

@@ -18,7 +19,7 @@
1819
from codeclash.games import ARENAS
1920

2021
MODELS_PATH = Path("configs/models.yaml")
21-
TARGET_ROUNDS = [1, 5, 10, 15]
22+
TARGET_ROUNDS = [1, 15, 5, 10]
2223

2324

2425
def get_model_arena_logs(model: str | list[str], arena: str | list[str]) -> list[Path]:
@@ -77,6 +78,7 @@ def _compute_code_sim_difflib(diff1: PatchSet, diff2: PatchSet) -> float:
7778
"""Compute similarity score between two diffs using edit distance (0.0 = different, 1.0 = identical)."""
7879
diff1_str = "\n".join(str(f) for f in diff1)
7980
diff2_str = "\n".join(str(f) for f in diff2)
81+
difflib.SequenceMatcher = CSequenceMatcher
8082
seq_matcher = difflib.SequenceMatcher(None, diff1_str, diff2_str, autojunk=False)
8183
return seq_matcher.ratio()
8284

@@ -321,6 +323,36 @@ def plot_opponent_effect_heatmap(data_cache: str, target_round: int, output_path
321323
if opponent in opponent_matrix[model]:
322324
matrix[i, j] = opponent_matrix[model][opponent]
323325

326+
# Calculate row averages (model consistency)
327+
row_means = np.nanmean(matrix, axis=1)
328+
model_stats = [(models[i], row_means[i]) for i in range(n_models)]
329+
model_stats_sorted = sorted(model_stats, key=lambda x: x[1], reverse=True)
330+
331+
# Calculate column averages (opponent effect)
332+
col_means = np.nanmean(matrix, axis=0)
333+
opponent_stats = [(opponents[i], col_means[i]) for i in range(n_opponents)]
334+
opponent_stats_sorted = sorted(opponent_stats, key=lambda x: x[1], reverse=True)
335+
336+
print(f"\n{'=' * 60}")
337+
print(f"Model Consistency Statistics (Round {target_round}):")
338+
print(f"{'=' * 60}")
339+
print(f"{'Model':<25} {'Avg Similarity'}")
340+
print(f"{'-' * 60}")
341+
for model, avg_sim in model_stats_sorted:
342+
display = MODEL_TO_DISPLAY_NAME.get(model, model)
343+
print(f"{display:<25} {avg_sim:.3f}")
344+
print(f"{'=' * 60}")
345+
346+
print(f"\n{'=' * 60}")
347+
print(f"Opponent Effect Statistics (Round {target_round}):")
348+
print(f"{'=' * 60}")
349+
print(f"{'Opponent':<25} {'Avg Similarity'}")
350+
print(f"{'-' * 60}")
351+
for opp, avg_sim in opponent_stats_sorted:
352+
display = MODEL_TO_DISPLAY_NAME.get(opp, opp)
353+
print(f"{display:<25} {avg_sim:.3f}")
354+
print(f"{'=' * 60}\n")
355+
324356
# Create heatmap with blue-white-red colormap like win_rates
325357
FONT_BOLD.set_size(14)
326358
_, ax = plt.subplots(figsize=(6, 6))

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ dependencies = [
3131
"scipy",
3232
"unidiff",
3333
"trueskill",
34+
"difflib",
35+
"cdifflib",
3436
]
3537

3638
[project.optional-dependencies]

0 commit comments

Comments
 (0)