-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiag_tournament.py
315 lines (251 loc) · 12.5 KB
/
iag_tournament.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
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
# coding: utf-8
from collections import Counter
import argparse
import numpy as np
import pandas as pd
import torch
from agents.ala_ac_om import ActorCriticAgent, OppoModelingACAgent, UMOMACAgent
from agents.pg_dice import PGDiceBase, PGDice1M, PGDiceOM
from agents.mo_q_learning import QLearningAgent
from envs import IAG
from envs.monfgs import get_payoff_matrix
from utils.hps import HpLolaDice, HpAC, HpGP, HpQ
from utils.utils import mkdir_p
payoff_episode_log1 = []
payoff_episode_log2 = []
act_hist_log = [[], []]
def get_return(rewards, utility, mooc):
if mooc == 'SER':
rewards = torch.mean(torch.mean(torch.Tensor(rewards).permute(1, 2, 0), dim=2), dim=1)
ret = utility(rewards).item()
else:
rewards = utility(torch.mean(torch.Tensor(rewards).permute(1, 2, 0), dim=2))
ret = torch.mean(rewards).item()
return rewards.detach().cpu().numpy(), ret
def step(agents, rollout):
theta1 = agents[0].theta
theta2 = agents[1].theta
rewards1 = []
rewards2 = []
actions1 = []
actions2 = []
(s1, s2), _ = env.reset()
for t in range(rollout):
a1, _ = agents[0].act(s1, theta1)
a2, _ = agents[1].act(s2, theta2)
(s1, s2), (r1, r2), _, _ = env.step((a1, a2))
rewards1.append(r1)
rewards2.append(r2)
actions1.append(a1)
actions2.append(a2)
return [rewards1, rewards2], [actions1, actions2]
def AComGP_loop(agent, actions, rewards, op_actions, op_theta, lookahead):
agent.set_op_theta(op_theta)
for k in range(lookahead):
if k == 0:
umodel, likelihood = agent.makeUModel()
agent.in_lookahead(umodel, likelihood)
agent.update(actions, rewards, opp_actions=op_actions)
def LOLA_loop(agent, op_theta, lookahead):
agent.set_op_theta(op_theta)
for k in range(lookahead):
agent.in_lookahead()
agent.out_lookahead()
def LOLAom_loop(agent, op_theta, lookahead):
agent.set_op_theta(op_theta)
for k in range(lookahead):
if k == 0:
umodel, likelihood = agent.makeUModel()
agent.in_lookahead(umodel, likelihood)
agent.out_lookahead()
def play(n_lookaheads, trials, info, mooc, game, experiment):
state_distribution_log = np.zeros((env.NUM_ACTIONS, env.NUM_ACTIONS))
print("start iterations with", n_lookaheads[0], 'and', n_lookaheads[1], "lookaheads:")
for trial in range(trials):
if trial % 10 == 0:
print(f"Trial {trial}...")
agents = [None, None]
for i in range(len(experiment)):
if experiment[i] == 'AC':
agents[i] = ActorCriticAgent(i, hpAC, u[i], env.NUM_ACTIONS)
elif experiment[i] == 'ACom' or experiment[i] == 'ACoa':
agents[i] = OppoModelingACAgent(i, hpAC, u[i], env.NUM_ACTIONS)
elif experiment[i] == 'AComGP':
agents[i] = UMOMACAgent(i, hpAC, u[i], env.NUM_ACTIONS, hpGP=hpGP)
elif experiment[i] == 'LOLA':
if info == '0M':
agents[i] = PGDiceBase(i, env, hpL, u[i], mooc, u[i - 1])
else:
agents[i] = PGDice1M(i, env, hpL, u[i], mooc, u[i - 1])
elif experiment[i] == 'LOLAom':
agents[i] = PGDiceOM(i, env, hpL, u[i], mooc, hpGP=hpGP)
elif experiment[i] == 'Q':
agents[i] = QLearningAgent(i, hpQ, u[i], env.NUM_ACTIONS)
for update in range(hpL.n_update):
# rollout actual current policies:
if update % 100 == 0:
print(f"Episode {update}...")
r_s, a_s = step(agents, win_rollout)
act_probs = [get_act_probs(a_s[0]), get_act_probs(a_s[1])]
r, a = step(agents, 1)
if experiment == ['LOLA', 'LOLA']:
theta1_ = agents[0].theta.clone().detach().requires_grad_(True)
theta2_ = agents[1].theta.clone().detach().requires_grad_(True)
LOLA_loop(agents[0], theta2_, n_lookaheads[0])
LOLA_loop(agents[1], theta1_, n_lookaheads[1])
if experiment == ['ACoa', 'ACoa']:
theta1_ = agents[0].policy
theta2_ = agents[1].policy
agents[0].set_op_theta(theta2_)
agents[1].set_op_theta(theta1_)
agents[0].update(a[0], r[0], a[1])
agents[1].update(a[1], r[1], a[0])
if experiment == ['LOLA', 'ACoa']:
theta1_ = torch.sigmoid(agents[0].theta.clone().detach())
theta2_ = torch.tensor(agents[1].policy).requires_grad_(True)
LOLA_loop(agents[0], theta2_, n_lookaheads[0])
agents[1].set_op_theta(theta1_.numpy())
agents[1].update(a[1], r[1], a[0])
if experiment == ['ACoa', 'LOLA']:
theta1_ = torch.tensor(agents[0].policy).requires_grad_(True)
theta2_ = torch.sigmoid(agents[1].theta.clone().detach())
LOLA_loop(agents[1], theta1_, n_lookaheads[1])
agents[0].set_op_theta(theta2_.numpy())
agents[0].update(a[0], r[0], a[1])
if experiment == ['LOLA', 'AC']:
theta2_ = torch.tensor(agents[1].policy).requires_grad_(True)
LOLA_loop(agents[0], theta2_, n_lookaheads[0])
agents[1].update(a[1], r[1])
if experiment == ['AC', 'LOLA']:
theta1_ = torch.tensor(agents[0].policy).requires_grad_(True)
LOLA_loop(agents[1], theta1_, n_lookaheads[1])
agents[0].update(a[0], r[0])
for i, exp in enumerate(experiment):
if exp == 'LOLAom':
agents[i].update_logs(np.log(act_probs[i - 1]))
if update > 1:
LOLAom_loop(agents[i], torch.tensor(np.log(act_probs[i - 1])), n_lookaheads[i])
if exp == 'AC':
agents[i].update(a[i], r[i])
if exp == 'ACom':
if update > 1:
agents[i].set_op_theta(act_probs[i - 1])
agents[i].update(a[i], r[i], a[i - 1])
if exp == 'AComGP':
agents[i].update_logs(act_probs[i - 1])
if update > 1:
AComGP_loop(agents[i], a[i], r[i], a[i - 1], act_probs[i - 1], n_lookaheads[i])
if exp == 'Q':
agents[i].update(a[i], r[i])
a1, a2 = a_s
r1, r2 = r_s
if update >= (0.1 * hpL.n_update):
for rol_a in range(len(a1)):
for batch_a in range(len(a1[rol_a])):
state_distribution_log[a1[rol_a][batch_a], a2[rol_a][batch_a]] += 1
ret1, score1 = get_return(r1, u1, mooc)
ret2, score2 = get_return(r2, u2, mooc)
if env.NUM_ACTIONS == 2:
for i in range(len(act_hist_log)):
act_hist_log[i].append([update, trial, n_lookaheads[i],
act_probs[i][0], act_probs[i][1]])
else:
for i in range(len(act_hist_log)):
act_hist_log[i].append([update, trial, n_lookaheads[i],
act_probs[i][0], act_probs[i][1], act_probs[i][2]])
payoff_episode_log1.append([update, trial, n_lookaheads[0], score1])
payoff_episode_log2.append([update, trial, n_lookaheads[1], score2])
if trial % 5 == 0:
columns = ['Episode', 'Trial', 'Lookahead', 'Payoff']
df1 = pd.DataFrame(payoff_episode_log1, columns=columns)
df2 = pd.DataFrame(payoff_episode_log2, columns=columns)
path_data = f'results/tour_{experiment}_{game}_l{n_lookaheads[0]}_{n_lookaheads[1]}' # /{mooc}/{hp.use_baseline}'
mkdir_p(path_data)
df1.to_csv(f'{path_data}/agent1_payoff_{info}.csv', index=False)
df2.to_csv(f'{path_data}/agent2_payoff_{info}.csv', index=False)
state_distribution = state_distribution_log / (
hpL.batch_size * (0.9 * hpL.n_update) * (trial + 1) * win_rollout)
df = pd.DataFrame(state_distribution)
print(np.sum(state_distribution))
df.to_csv(f'{path_data}/states_{info}_{n_lookaheads[0]}_{n_lookaheads[1]}.csv', index=False, header=None)
if env.NUM_ACTIONS == 3:
columns = ['Episode', 'Trial', 'Lookahead', 'Action 1', 'Action 2', 'Action 3']
else:
columns = ['Episode', 'Trial', 'Lookahead', 'Action 1', 'Action 2']
df1 = pd.DataFrame(act_hist_log[0], columns=columns)
df2 = pd.DataFrame(act_hist_log[1], columns=columns)
df1.to_csv(f'{path_data}/agent1_probs_{info}.csv', index=False)
df2.to_csv(f'{path_data}/agent2_probs_{info}.csv', index=False)
del df1, df2, df
columns = ['Episode', 'Trial', 'Lookahead', 'Payoff']
df1 = pd.DataFrame(payoff_episode_log1, columns=columns)
df2 = pd.DataFrame(payoff_episode_log2, columns=columns)
path_data = f'results_local/tour_{experiment}_{game}_l{n_lookaheads[0]}_{n_lookaheads[1]}'
mkdir_p(path_data)
df1.to_csv(f'{path_data}/agent1_payoff_{info}.csv', index=False)
df2.to_csv(f'{path_data}/agent2_payoff_{info}.csv', index=False)
state_distribution = state_distribution_log / (hpL.batch_size * (0.9 * hpL.n_update) * trials * win_rollout)
df = pd.DataFrame(state_distribution)
print(np.sum(state_distribution))
df.to_csv(f'{path_data}/states_{info}_{n_lookaheads[0]}_{n_lookaheads[1]}.csv', index=False, header=None)
if env.NUM_ACTIONS == 3:
columns = ['Episode', 'Trial', 'Lookahead', 'Action 1', 'Action 2', 'Action 3']
else:
columns = ['Episode', 'Trial', 'Lookahead', 'Action 1', 'Action 2']
df1 = pd.DataFrame(act_hist_log[0], columns=columns)
df2 = pd.DataFrame(act_hist_log[1], columns=columns)
df1.to_csv(f'{path_data}/agent1_probs_{info}.csv', index=False)
df2.to_csv(f'{path_data}/agent2_probs_{info}.csv', index=False)
del df1, df2, df
def get_act_probs(act_ep):
act_probs = 1e-8 * np.ones(env.NUM_ACTIONS)
for actions in act_ep:
count = Counter(actions)
total = sum(count.values())
for action in range(env.NUM_ACTIONS):
act_probs[action] += count[action] / total
act_probs = act_probs / len(act_ep)
return act_probs
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-trials', type=int, default=15, help="number of trials")
parser.add_argument('-updates', type=int, default=3000, help="updates")
parser.add_argument('-batch', type=int, default=1, help="batch size")
parser.add_argument('-rollout', type=int, default=1, help="rollout size")
parser.add_argument('-mooc', type=str, default='SER', help="MOO criterion")
# LOLA Agent
parser.add_argument('-lr_out', type=float, default=0.1, help="lr outer loop")
parser.add_argument('-lr_in', type=float, default=0.2, help="lr inner loop")
parser.add_argument('-gammaL', type=float, default=1, help="gamma")
parser.add_argument('-mem', type=str, default='0M', help="memory")
# AC agent
parser.add_argument('-lr_q', type=float, default=0.05, help="lr q")
parser.add_argument('-lr_theta', type=float, default=0.05, help="lr theta")
parser.add_argument('-gammaAC', type=float, default=1, help="gamma")
# experiment
parser.add_argument('-game', type=str, default='iagM', help="game")
parser.add_argument('-experiment', type=str, default='LOLAom-Q', help="experiment")
parser.add_argument('-lookahead1', type=int, default=1, help="number of lookaheads for agent 1")
parser.add_argument('-lookahead2', type=int, default=1, help="number of lookaheads for agent 2")
args = parser.parse_args()
u1 = lambda x: torch.sum(torch.pow(x, 2), dim=0)
u2 = lambda x: torch.prod(x, dim=0)
u = [u1, u2]
n_lookaheads = [args.lookahead1, args.lookahead2]
mooc = args.mooc
trials = args.trials
game = args.game
win_rollout = 100
hpL = HpLolaDice(args.lr_out, args.lr_in, args.gammaL,
args.updates, args.rollout, args.batch)
hpAC = HpAC(args.lr_q, args.lr_theta, args.gammaAC,
args.updates, args.rollout, args.batch)
hpQ = HpQ()
hpGP = HpGP()
payout_mat = get_payoff_matrix(game)
print(payout_mat)
env = IAG(hpL.len_rollout, hpL.batch_size, payout_mat)
experiment = (args.experiment).split("-")
print(experiment)
info = args.mem
play(n_lookaheads, trials, info, mooc, game, experiment)