-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper_results.py
277 lines (240 loc) · 10.3 KB
/
paper_results.py
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
import random
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from statistics import NormalDist
from uuid import uuid4
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
import torch
import tyro
from numpy.random import default_rng
from tensordict import TensorDict
from environment.agents.geniusweb import AGENTS
from environment.agents.policy.PPO import GNN
from environment.scenario import Scenario
from ppo import Args, Policies, concat_envs
pio.kaleido.scope.mathjax = None
TESTS = [
{
"name": "GNN_basic_random_on_basic_random",
"module": "GNN",
"models": [str(i) for i in Path("models", "GNN", "basic_opponents_random_scenarios").iterdir()],
"scenario": f"environment/scenarios/random_tmp_GNN_basic_random_on_basic_random_{uuid4()}",
"opponent_sets": ("BASIC",),
},
{
"name": "GNN_all_random_on_all_random",
"module": "GNN",
"models": [str(i) for i in Path("models", "GNN", "all_opponents_random_scenarios").iterdir()],
"scenario": f"environment/scenarios/random_tmp_GNN_all_random_on_all_random_{uuid4()}",
"opponent_sets": ("ANL2022", "ANL2023", "BASIC"),
},
{
"name": "GNN_basic_fixed_on_basic_fixed",
"module": "GNN",
"models": [str(i) for i in Path("models", "GNN", "basic_opponents_fixed_scenario").iterdir()],
"scenario": "environment/scenarios/fixed_utility",
"opponent_sets": ("BASIC",),
},
{
"name": "GNN_all_fixed_on_all_fixed",
"module": "GNN",
"models": [str(i) for i in Path("models", "GNN", "all_opponents_fixed_scenario").iterdir()],
"scenario": "environment/scenarios/fixed_utility",
"opponent_sets": ("ANL2022", "ANL2023", "BASIC"),
},
{
"name": "Higa_basic_fixed_on_basic_fixed",
"module": "HigaEtAl",
"models": [str(i) for i in Path("models", "HigaEtAl", "basic_opponents_fixed_scenario").iterdir()],
"scenario": "environment/scenarios/fixed_utility",
"opponent_sets": ("BASIC",),
},
{
"name": "Higa_all_fixed_on_all_fixed",
"module": "HigaEtAl",
"models": [str(i) for i in Path("models", "HigaEtAl", "all_opponents_fixed_scenario").iterdir()],
"scenario": "environment/scenarios/fixed_utility",
"opponent_sets": ("ANL2022", "ANL2023", "BASIC"),
},
]
@dataclass
class ArgsEval(Args):
test_num: int | None = None
model_paths: tuple[str, ...] | None = None
episodes_per_agent: int = 1000
episodes_per_scenario_per_agent: int = 20
def main():
args = tyro.cli(ArgsEval)
results_dir = Path("analysis", "data")
results_dir.mkdir(parents=True, exist_ok=True)
assert args.test_num is not None
test_data = TESTS[args.test_num]
# TRY NOT TO MODIFY: seeding
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = args.torch_deterministic
scenario_rng = default_rng(args.seed)
device = torch.device("cuda:0" if torch.cuda.is_available() and args.cuda else "cpu")
# env setup
used_agents = [a for a in AGENTS if a.startswith(tuple(test_data["opponent_sets"]))]
args.episodes = args.episodes_per_agent * len(used_agents)
args.episodes_per_scenario = args.episodes_per_scenario_per_agent * len(used_agents)
envs = None
if args.opponent == "all":
args.num_envs = len(used_agents)
iterables = [list(range(len(test_data["models"]))), sorted(used_agents)]
index = pd.MultiIndex.from_product(iterables, names=["model", "opponent"])
data = pd.DataFrame(columns=["my_utility", "opp_utility", "count", "rounds_played", "self_accepted", "found_agreement"], index=index)
for model_index, model_path in enumerate(test_data["models"]):
print(f"model_index: {model_index}")
agent_type = model_path.split("/")[1].split("_")[0]
episodes = 0
iteration = 0
log_metrics = defaultdict(lambda: defaultdict(lambda: .0))
# TRY NOT TO MODIFY: start the game
while episodes < args.episodes:
if test_data["scenario"].startswith("environment/scenarios/random_tmp") or iteration == 0:
if test_data["scenario"].startswith("environment/scenarios/random_tmp"):
scenario = Scenario.create_random([200, 1000], scenario_rng, 5, True)
scenario.to_directory(Path(test_data["scenario"]))
if envs:
envs.close()
env_config = {
"agents": [f"RL_{agent_type}", args.opponent],
"used_agents": used_agents,
"scenario": test_data["scenario"],
"deadline": {"rounds": args.deadline, "ms": 10000},
"random_agent_order": args.random_agent_order,
}
envs = concat_envs(env_config, args.num_envs, num_cpus=args.num_envs)
agent: GNN = Policies[agent_type].value(envs, args).to(device)
agent.load_state_dict(torch.load(model_path, map_location=device))
agent.train(False)
agent.action_nvec = tuple(envs.single_action_space.nvec)
next_obs, _ = envs.reset(seed=args.seed + iteration)
next_obs = TensorDict(next_obs, batch_size=(args.num_envs,), device=device)
episodes_on_this_scenario = 0
print(episodes)
while episodes_on_this_scenario < args.episodes_per_scenario:
# ALGO LOGIC: action logic
with torch.no_grad():
action, _, _, _ = agent.get_action_and_value(next_obs)
# TRY NOT TO MODIFY: execute the game and log data.
next_obs, _, terminations, truncations, infos = envs.step(action.cpu().numpy())
next_done_bool = np.logical_or(terminations, truncations)
next_obs = TensorDict(next_obs, batch_size=(args.num_envs,), device=device)
if next_done_bool.any():
for info in infos:
if info:
for agent_id, utility in info["utility_all_agents"].items():
if agent_id != f"RL_{test_data['module']}":
log_metrics[agent_id]["opp_utility"] += utility
log_metrics[agent_id]["my_utility"] += info["utility_all_agents"][f"RL_{test_data['module']}"]
log_metrics[agent_id]["count"] += 1
log_metrics[agent_id]["rounds_played"] += info["rounds_played"]
log_metrics[agent_id]["self_accepted"] += info["self_accepted"]
log_metrics[agent_id]["found_agreement"] += info["found_agreement"]
episodes += 1
episodes_on_this_scenario += 1
iteration += 1
for opp_id, values in log_metrics.items():
result = {k: v / values["count"] for k, v in values.items() if k != "count"}
result["count"] = values["count"]
data.loc[(model_index, opp_id), result.keys()] = list(result.values())
data.to_csv(results_dir / f"{test_data['name']}.csv")
data_plot = pd.read_csv(results_dir / f"{test_data['name']}.csv", index_col=[0, 1])
plot_results(data_plot, test_data["name"])
def confidence_interval(data, confidence=0.99):
nans = np.count_nonzero(np.isnan(data))
if nans > 0:
print(f"found {nans} NaNs")
data = [d for d in data if not np.isnan(d)]
dist = NormalDist.from_samples(data)
z = NormalDist().inv_cdf((1 + confidence) / 2.)
h = dist.stdev * z / ((len(data) - 1) ** .5)
return h
def data_to_summary(data):
results = {}
for opponent in data.index.unique(1):
opp_data = data.query(f"opponent == '{opponent}'")
results[opponent] = {
"my_utility_mean": opp_data["my_utility"].mean(),
"my_utility_CI_99": confidence_interval(opp_data["my_utility"], 0.99),
"opp_utility_mean": opp_data["opp_utility"].mean(),
"opp_utility_CI_99": confidence_interval(opp_data["opp_utility"], 0.99),
}
return results
def plot_results(data, name):
figures_dir = Path("analysis", "figures")
figures_dir.mkdir(parents=True, exist_ok=True)
colors = px.colors.qualitative.Plotly
summary = data_to_summary(data)
x = []
my_utility_mean = []
my_utility_CI_99 = []
opp_utility_mean = []
opp_utility_CI_99 = []
for opponent, values in summary.items():
x.append(opponent.split("_")[1])
my_utility_mean.append(values["my_utility_mean"])
my_utility_CI_99.append(values["my_utility_CI_99"])
opp_utility_mean.append(values["opp_utility_mean"])
opp_utility_CI_99.append(values["opp_utility_CI_99"])
fig = go.Figure()
color = None
if name.startswith("Higa"):
color = colors[2]
elif name == "GNN_all_random_on_all_random":
color = colors[4]
fig.add_trace(
go.Bar(
name="Ours" if name.startswith("GNN") else "Higa et al.",
marker_color=color,
x=x,
y=my_utility_mean,
error_y=dict(type="data", array=my_utility_CI_99),
opacity=0.75,
)
)
fig.add_trace(
go.Bar(
name="Opponent",
x=x,
y=opp_utility_mean,
error_y=dict(type="data", array=opp_utility_CI_99),
opacity=0.75,
)
)
width = len(x) * 25 + 100
fig.update_layout(
barmode="group",
width=width * (1/0.6),
height=175 * (1/0.6),
font=dict(
family="serif",
),
margin=dict(
t=1,
b=1,
l=1,
r=1,
),
xaxis=dict(
tickangle=20,
),
yaxis=dict(
title="Utility",
range=[0, 1.1],
dtick = 0.1,
),
legend=dict(orientation="h", yanchor="bottom", y=1, xanchor="right", x=1),
)
fig.write_image(str(figures_dir / f"{name}.pdf"), scale=0.45)
if __name__ == "__main__":
main()