-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresults.py
More file actions
428 lines (366 loc) · 11.9 KB
/
Copy pathresults.py
File metadata and controls
428 lines (366 loc) · 11.9 KB
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
import ast
import numpy as np
import pandas as pd
# Use non-interactive backend so it works even without Tk
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="whitegrid")
# Order of profiles in plots
PROFILES = ["balanced", "aggressive", "conceder", "boulware"]
# -------------------------------------------------------------
# 1. Load and expand CSV (Dict → per-run rows)
# -------------------------------------------------------------
def load_results(csv_path: str = "output2.csv") -> pd.DataFrame:
"""
Load the aggregated CSV produced by the tests and parse the Dict column.
"""
df = pd.read_csv(csv_path)
df["Dict"] = df["Dict"].apply(ast.literal_eval)
return df
def expand_runs(df: pd.DataFrame) -> pd.DataFrame:
"""
From one row per (Agent_1, Agent_2) with Dict of lists
-> one row per *simulation run*.
Robust to Dict lists having different lengths.
"""
def safe_get(d, key, i, default=None):
"""Return d[key][i] if it exists and is long enough, else default."""
if key not in d:
return default
v = d[key]
if isinstance(v, list):
return v[i] if i < len(v) else default
# If for some reason it's a scalar
return v
rows = []
for _, row in df.iterrows():
d = row["Dict"]
# Use the longest list as the number of runs
list_lengths = [len(v) for v in d.values() if isinstance(v, list)]
if not list_lengths:
continue
n = max(list_lengths)
for i in range(n):
agreement = safe_get(d, "agreements_flag", i, 0)
num_rounds = safe_get(d, "num_rounds", i, None)
rec = {
"Agent_1": row["Agent_1"], # provider
"Agent_2": row["Agent_2"], # initiator
"run": i,
"agreement": agreement,
"num_rounds": num_rounds,
"agent_1_score": safe_get(d, "agent_1_score", i, None),
"agent_2_score": safe_get(d, "agent_2_score", i, None),
"agent_1_severity": safe_get(d, "agent_1_severity", i, None),
"agent_2_severity": safe_get(d, "agent_2_severity", i, None),
}
rows.append(rec)
return pd.DataFrame(rows)
# -------------------------------------------------------------
# 2. Pair-wise metrics (balanced vs aggressive, etc.)
# -------------------------------------------------------------
def compute_pair_metrics(runs: pd.DataFrame) -> pd.DataFrame:
"""
Aggregated metrics per (Agent_1 profile, Agent_2 profile) pair.
n_runs: number of simulations for that pair
agreement_rate: fraction of runs with agreement
mean_rounds_all: avg rounds over all runs
mean_rounds_agree: avg rounds only where agreement == 1
agent_*_score: mean context similarity per side
agent_*_severity: mean severity per side
"""
def agg_pair(g: pd.DataFrame) -> pd.Series:
n_total = len(g)
n_agree = g["agreement"].sum()
agreement_rate = n_agree / n_total if n_total > 0 else np.nan
mean_rounds_all = g["num_rounds"].mean()
mean_rounds_agree = g.loc[g["agreement"] == 1, "num_rounds"].mean()
m1_score = g["agent_1_score"].mean()
m2_score = g["agent_2_score"].mean()
m1_sev = g["agent_1_severity"].mean()
m2_sev = g["agent_2_severity"].mean()
return pd.Series(
dict(
n_runs=n_total,
agreement_rate=agreement_rate,
mean_rounds_all=mean_rounds_all,
mean_rounds_agree=mean_rounds_agree,
agent_1_score=m1_score,
agent_2_score=m2_score,
agent_1_severity=m1_sev,
agent_2_severity=m2_sev,
)
)
pair_df = (
runs.groupby(["Agent_1", "Agent_2"], as_index=False)
.apply(agg_pair)
.reset_index(drop=True)
)
return pair_df
# -------------------------------------------------------------
# 3. Role-based metrics (profile as Agent_1 vs Agent_2)
# -------------------------------------------------------------
def build_role_long_df(runs: pd.DataFrame) -> pd.DataFrame:
"""
Long format with one row per (run, role, profile).
Allows: "how does 'aggressive' behave as provider vs initiator?"
"""
rows = []
for _, r in runs.iterrows():
# Provider side (Agent_1)
rows.append(
dict(
profile=r["Agent_1"],
role="provider", # Agent_1
agreement=r["agreement"],
num_rounds=r["num_rounds"],
score=r["agent_1_score"],
severity=r["agent_1_severity"],
)
)
# Initiator side (Agent_2)
rows.append(
dict(
profile=r["Agent_2"],
role="initiator", # Agent_2 starts the negotiation
agreement=r["agreement"],
num_rounds=r["num_rounds"],
score=r["agent_2_score"],
severity=r["agent_2_severity"],
)
)
return pd.DataFrame(rows)
def compute_role_metrics(role_df: pd.DataFrame) -> pd.DataFrame:
"""
Aggregated metrics per (profile, role).
"""
role_metrics = (
role_df.groupby(["profile", "role"])
.agg(
n_runs=("agreement", "size"),
agreement_rate=("agreement", "mean"),
mean_rounds=("num_rounds", "mean"),
mean_score=("score", "mean"),
mean_severity=("severity", "mean"),
)
.reset_index()
)
return role_metrics
# -------------------------------------------------------------
# 4. Plot helpers – Heatmaps and Bars for the paper
# -------------------------------------------------------------
def heatmap_pair(metric_df: pd.DataFrame,
value_col: str,
title: str,
cmap: str = "RdYlGn",
vmin=None,
vmax=None,
ax=None,
cbar_label: str | None = None):
"""
Heatmap with Agent_1 on rows and Agent_2 on cols.
If ax is None, creates its own figure, otherwise draws on the given ax.
"""
pivot = (
metric_df.pivot(index="Agent_1", columns="Agent_2", values=value_col)
.reindex(index=PROFILES, columns=PROFILES)
)
if ax is None:
plt.figure(figsize=(6, 5))
ax = plt.gca()
hm = sns.heatmap(
pivot,
annot=True,
fmt=".3f",
cmap=cmap,
vmin=vmin,
vmax=vmax,
cbar_kws={"label": cbar_label or value_col.replace("_", " ").title()},
ax=ax,
)
ax.set_xlabel("Agent 2 Profile (initiator)")
ax.set_ylabel("Agent 1 Profile (provider)")
ax.set_title(title)
return hm
def plot_agreement_heatmap(pair_df: pd.DataFrame):
"""
Heatmap of agreement rate per (Agent_1, Agent_2) profile pair.
"""
plt.figure(figsize=(6, 5))
heatmap_pair(
pair_df,
"agreement_rate",
"Agreement Rate",
cmap="RdYlGn",
vmin=0,
vmax=1,
cbar_label="Agreement Rate",
)
plt.tight_layout()
plt.savefig("heatmap_agreement_rate.png", dpi=300)
plt.close()
def plot_rounds_heatmap(pair_df: pd.DataFrame):
"""
Heatmap of mean rounds to agreement (only successful negotiations).
"""
plt.figure(figsize=(6, 5))
heatmap_pair(
pair_df,
"mean_rounds_agree",
"Mean Rounds (Agreements Only)",
cmap="YlOrBr",
cbar_label="Mean Rounds Agree",
)
plt.tight_layout()
plt.savefig("heatmap_rounds_agree.png", dpi=300)
plt.close()
def plot_score_heatmaps(pair_df: pd.DataFrame):
"""
Two heatmaps:
- Agent_1_score (provider) = context similarity for provider
- Agent_2_score (initiator) = context similarity for initiator
"""
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
heatmap_pair(
pair_df,
"agent_1_score",
"Mean Context Score (Agent 1 / provider)",
cmap="YlGnBu",
vmin=0,
vmax=1,
ax=axes[0],
cbar_label="Mean Score",
)
heatmap_pair(
pair_df,
"agent_2_score",
"Mean Context Score (Agent 2 / initiator)",
cmap="YlGnBu",
vmin=0,
vmax=1,
ax=axes[1],
cbar_label="Mean Score",
)
plt.tight_layout()
plt.savefig("heatmap_scores.png", dpi=300)
plt.close()
def plot_severity_heatmaps(pair_df: pd.DataFrame):
"""
Two heatmaps:
- Agent_1_severity (provider)
- Agent_2_severity (initiator)
"""
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
heatmap_pair(
pair_df,
"agent_1_severity",
"Mean Severity (Agent 1 / provider)",
cmap="YlOrRd",
vmin=0,
vmax=1,
ax=axes[0],
cbar_label="Mean Severity",
)
heatmap_pair(
pair_df,
"agent_2_severity",
"Mean Severity (Agent 2 / initiator)",
cmap="YlOrRd",
vmin=0,
vmax=1,
ax=axes[1],
cbar_label="Mean Severity",
)
plt.tight_layout()
plt.savefig("heatmap_severity.png", dpi=300)
plt.close()
def barplot_role(role_metrics: pd.DataFrame, metric: str, title: str, filename: str, ylim=None):
"""
Barplot comparing profiles per role (provider vs initiator).
"""
plt.figure(figsize=(7, 4))
ax = sns.barplot(
data=role_metrics,
x="profile",
y=metric,
hue="role",
order=PROFILES,
)
ax.set_title(title)
ax.set_xlabel("Profile")
ax.set_ylabel(metric.replace("_", " ").title())
if ylim is not None:
ax.set(ylim=ylim)
plt.tight_layout()
plt.savefig(filename, dpi=300)
plt.close()
def boxplot_rounds_by_profile(role_df: pd.DataFrame, filename: str):
"""
Distribution of rounds per profile & role.
"""
plt.figure(figsize=(8, 4))
ax = sns.boxplot(
data=role_df,
x="profile",
y="num_rounds",
hue="role",
order=PROFILES,
)
ax.set_title("Distribution of Rounds per Profile & Role")
ax.set_xlabel("Profile")
ax.set_ylabel("Number of Rounds")
plt.tight_layout()
plt.savefig(filename, dpi=300)
plt.close()
# -------------------------------------------------------------
# 5. Main script
# -------------------------------------------------------------
if __name__ == "__main__":
# Load & expand
df = load_results("output2.csv")
runs = expand_runs(df)
# --- Pair-wise behaviour (profile vs profile) ---
pair_df = compute_pair_metrics(runs)
print("Pair-wise metrics:")
print(pair_df.head())
# Heatmaps for paper
plot_agreement_heatmap(pair_df)
plot_rounds_heatmap(pair_df)
plot_score_heatmaps(pair_df)
plot_severity_heatmaps(pair_df)
# --- Role-based behaviour (same profile as provider vs initiator) ---
role_df = build_role_long_df(runs)
role_metrics = compute_role_metrics(role_df)
print("\nRole-based metrics:")
print(role_metrics)
# Role-based barplots / boxes (optional but nice for paper)
barplot_role(
role_metrics,
"agreement_rate",
"Agreement Rate per Profile & Role",
filename="bar_agreement_rate_role.png",
ylim=(0, 1),
)
barplot_role(
role_metrics,
"mean_rounds",
"Mean Rounds per Profile & Role",
filename="bar_mean_rounds_role.png",
)
barplot_role(
role_metrics,
"mean_score",
"Mean Context Score per Profile & Role",
filename="bar_mean_score_role.png",
ylim=(0, 1),
)
barplot_role(
role_metrics,
"mean_severity",
"Mean Severity per Profile & Role",
filename="bar_mean_severity_role.png",
ylim=(0, 1),
)
boxplot_rounds_by_profile(role_df, filename="box_rounds_profile_role.png")