File size: 3,181 Bytes
73ab68a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# stratego/benchmarking/analysis/analyze_csv.py

import csv
import os
from collections import Counter
from datetime import datetime

OUTPUT_DIR = "stratego/benchmarking/output"
SUMMARY_DIR = "stratego/benchmarking/summaries"


def find_latest_benchmark_csv():
    files = [
        f for f in os.listdir(OUTPUT_DIR)
        if f.startswith("benchmark_") and f.endswith(".csv")
    ]

    if not files:
        raise FileNotFoundError("No benchmark CSV files found in output/")

    files.sort()
    return os.path.join(OUTPUT_DIR, files[-1])


def analyze_benchmark_csv(csv_path: str):
    games = []

    with open(csv_path, newline="", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        for row in reader:
            games.append(row)

    total_games = len(games)

    wins = Counter()
    terminations = Counter()

    turns = []
    invalid_p0 = 0
    invalid_p1 = 0
    repetitions = []

    for g in games:
        winner_raw = g["winner"]
        try:
            winner = int(winner_raw)
        except (TypeError, ValueError):
            winner = -1
        reason = g["game_end_reason"]

        if winner == 0:
            wins["p0"] += 1
        elif winner == 1:
            wins["p1"] += 1
        else:
            wins["draw"] += 1

        terminations[reason] += 1

        turns.append(int(g["turns"]))
        invalid_p0 += int(g["invalid_moves_p0"])
        invalid_p1 += int(g["invalid_moves_p1"])
        repetitions.append(int(g["repetitions"]))

    summary = {
        "total_games": total_games,
        "wins_p0": wins["p0"],
        "wins_p1": wins["p1"],
        "draws": wins["draw"],
        "win_rate_p0": wins["p0"] / total_games if total_games else 0,
        "win_rate_p1": wins["p1"] / total_games if total_games else 0,
        "avg_game_length": sum(turns) / total_games if total_games else 0,
        "avg_invalid_moves_p0": invalid_p0 / total_games if total_games else 0,
        "avg_invalid_moves_p1": invalid_p1 / total_games if total_games else 0,
        "avg_repetitions": sum(repetitions) / total_games if total_games else 0,
    }

    return summary, terminations


def write_summary_csv(summary, terminations, source_csv):
    os.makedirs(SUMMARY_DIR, exist_ok=True)

    base = os.path.basename(source_csv).replace("benchmark_", "").replace(".csv", "")
    out_path = os.path.join(SUMMARY_DIR, f"summary_{base}.csv")

    with open(out_path, "w", newline="", encoding="utf-8") as f:
        writer = csv.writer(f)

        writer.writerow(["metric", "value"])
        for k, v in summary.items():
            writer.writerow([k, v])

        writer.writerow([])
        writer.writerow(["termination_reason", "count"])
        for reason, count in terminations.items():
            writer.writerow([reason, count])

    print(f"[OK] Summary written to {out_path}")


if __name__ == "__main__":
    latest_csv = find_latest_benchmark_csv()
    print(f"[INFO] Analyzing {latest_csv}")

    summary, terminations = analyze_benchmark_csv(latest_csv)
    write_summary_csv(summary, terminations, latest_csv)