-
Notifications
You must be signed in to change notification settings - Fork 1
/
OBL.py
158 lines (150 loc) · 6.58 KB
/
OBL.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
from agents.players import *
import agents.learners as learners
from UI.plot_funcs import plot_everything
import UI.get_args as get_args
from functions import *
import numpy as np
import sys
import time
import logging
from multiprocessing import Pool
log = logging.getLogger(__name__)
def run(options, games_per_lvl=100000, exploit_freq= 1):
num_lvls = options["num_lvls"]
game_name = options["game_name"]
game = options["game"]
fict_game = options["fict_game"]
exploit_learner = options["exploit_learner"]
averaged_bel = options["avg_bel"]
averaged_pol = options["avg_pol"]
learn_with_avg = options["learn_w_avg"]
learner_type = options["learner_type"]
num_players = 2
if learner_type == "rl":
RL_learners = [learners.actor_critic(learners.softmax, learners.value_advantage,\
game.num_actions[p], game.num_states[p], extra_samples = 0)\
for p in range(num_players)]
players = [RL(RL_learners[p],p) for p in range(num_players)]
if averaged_pol or learn_with_average:
raise NotImplementedError
elif learner_type == "obl":
RL_learners = [learners.actor_critic(learners.softmax, learners.value_advantage,\
game.num_actions[p], game.num_states[p], extra_samples = 0)\
for p in range(num_players)]
players = [OBL(RL_learners[p], p, fict_game) for p in range(num_players)]
elif learner_type == "ot_rl":
RL_learners = [[learners.actor_critic(learners.softmax, learners.value_advantage,\
game.num_actions[p], game.num_states[p], extra_samples = 0)\
for lvl in range(num_lvls)] for p in range(num_players)]
players = [OT_RL(RL_learners[p], p, fict_game) for p in range(num_players)]
fixed_players = [fixed_pol(players[p].opt_pol) for p in range(num_players)]
for p in range(num_players):
curr_player = players.pop(p)
fixed_curr = fixed_players.pop(p)
if curr_player.belief is not None:
if learn_with_avg and learner_type == "obl":
curr_player.set_other_players(fixed_players)
else:
curr_player.set_other_players(players)
fixed_players.insert(p, fixed_curr)
players.insert(p, curr_player)
reward_hist = [[0 for i in range(games_per_lvl)] for lvl in range(num_lvls)]
pol_hist = []
belief_hist = []
avg_pols = []
avg_bels = []
exploitability = []
times = []
tic = time.perf_counter()
for lvl in range(num_lvls):
log.info("Level: " + str(lvl))
pols = []
bels = []
for p in players:
pols.append(p.opt_pol)
if p.belief is not None:
if learn_with_avg:
for p_id, other_p in enumerate(p.other_players):
if other_p != "me":
other_p.opt_pol = players[p_id].opt_pol
if not averaged_bel:
p.belief_buff = []
p.update_mem_and_bel()
bels.append(np.copy(p.belief))
else:
bels.append(np.zeros((1,1)))
pol_hist.append(pols)
log.debug("Policies: " + str(pols))
belief_hist.append(bels)
log.debug("Beliefs: " + str(bels))
if averaged_pol or learn_with_avg:
new_avg_pols = []
for p in players:
if learner_type != "rl":
new_avg_pols.append(p.avg_pol)
avg_pols.append(new_avg_pols)
log.debug("Average polices: " + str(new_avg_pols))
if lvl % exploit_freq == 0 and learner_type != "ot_rl":
if averaged_pol:
exploit, _, _, _ = calc_exploitability(new_avg_pols, game, exploit_learner)
else:
exploit, _, _, _ = calc_exploitability(pols, game, exploit_learner)
exploitability.append(exploit)
log.info("Exploitability: " + str(exploit))
if learn_with_avg:
for p_id, p in enumerate(players):
for other_p_id, other_pol in enumerate(new_avg_pols):
if other_p_id != p_id:
p.other_players[other_p_id].opt_pol = other_pol
for p in players:
p.reset()
play_to_convergence(players, game, tol=1e-7)
times.append(time.perf_counter()-tic)
pols = []
bels = []
for p in players:
pols.append(p.opt_pol)
if p.belief is not None:
if not averaged_bel:
p.belief_buff = []
p.update_mem_and_bel()
bels.append(p.belief)
else:
bels.append(np.zeros((1,1)))
pol_hist.append(pols)
belief_hist.append(bels)
#pol_hist = pol_hist[-5:]
#belief_hist = belief_hist[-5:]
if learner_type == 'ot_rl':
pol_hist = []
avg_pols = []
for lvl in range(num_lvls):
new_avg_pols = []
pols = []
for p in players:
new_avg_pols.append(p.avg_pols[lvl])
pols.append(p.pols[lvl])
if averaged_pol:
exploit, _, _, _ = calc_exploitability(new_avg_pols, game, exploit_learner)
avg_pols.append(new_avg_pols)
else:
pol_hist.append(pols)
exploit, _, _, _ = calc_exploitability(pols, game, exploit_learner)
exploitability.append(exploit)
else:
if averaged_pol:
new_avg_pols = []
for p in players:
new_avg_pols.append(p.avg_pol)
avg_pols.append(new_avg_pols)
exploit, _, _, _ = calc_exploitability(new_avg_pols, game, exploit_learner)
else:
exploit, _, _, _ = calc_exploitability(pols, game, exploit_learner)
exploitability.append(exploit)
if averaged_pol:
pol_plot = avg_pols
else:
pol_plot = pol_hist
bel_plot = belief_hist
log.info("Finished")
return {"pols":pol_plot,"bels":bel_plot, "exploit":exploitability,"times":times}