forked from werner-duvaud/muzero-general
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay_buffer.py
386 lines (339 loc) · 15.8 KB
/
replay_buffer.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
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
import copy
import time
import numpy
import ray
import torch
import models
@ray.remote
class ReplayBuffer:
"""
Class which run in a dedicated thread to store played games and generate batch.
"""
def __init__(self, initial_checkpoint, initial_buffer, config):
self.config = config
self.buffer = copy.deepcopy(initial_buffer) # buffer是一个字典,key是game id,value是game_history
self.num_played_games = initial_checkpoint["num_played_games"]
self.num_played_steps = initial_checkpoint["num_played_steps"]
self.total_samples = sum(
[len(game_history.root_values) for game_history in self.buffer.values()]
)
if self.total_samples != 0:
print(
f"Replay buffer initialized with {self.total_samples} samples ({self.num_played_games} games).\n"
)
# Fix random generator seed
numpy.random.seed(self.config.seed)
def save_game(self, game_history, shared_storage=None):
if self.config.PER:
if game_history.priorities is not None:
# Avoid read only array when loading replay buffer from disk
game_history.priorities = numpy.copy(game_history.priorities)
else:
# Initial priorities for the prioritized replay (See paper appendix Training)
priorities = []
for i, root_value in enumerate(game_history.root_values):
priority = (
numpy.abs(
root_value - self.compute_target_value(game_history, i)
)
** self.config.PER_alpha
)
priorities.append(priority)
game_history.priorities = numpy.array(priorities, dtype="float32")
game_history.game_priority = numpy.max(game_history.priorities)
self.buffer[self.num_played_games] = game_history
self.num_played_games += 1
self.num_played_steps += len(game_history.root_values)
self.total_samples += len(game_history.root_values)
if self.config.replay_buffer_size < len(self.buffer):
del_id = self.num_played_games - len(self.buffer)
self.total_samples -= len(self.buffer[del_id].root_values)
del self.buffer[del_id]
if shared_storage:
shared_storage.set_info.remote("num_played_games", self.num_played_games)
shared_storage.set_info.remote("num_played_steps", self.num_played_steps)
def get_buffer(self):
return self.buffer
def get_batch(self):
(
index_batch,
observation_batch,
action_batch,
reward_batch,
value_batch,
policy_batch,
gradient_scale_batch,
) = ([], [], [], [], [], [], [])
weight_batch = [] if self.config.PER else None
# 从buffer里抽取n鸽样本,有probs的话安装probs的概率抽取,没有的话按照uniform抽取
for game_id, game_history, game_prob in self.sample_n_games(
self.config.batch_size
):
# 每个game_history都是一个游戏运行的序列,使用sample_position从这些序列里随机抽取一个位置
game_pos, pos_prob = self.sample_position(game_history)
# 计算从该位置开始的值,rewards等数据
values, rewards, policies, actions = self.make_target(
game_history, game_pos
)
index_batch.append([game_id, game_pos])
observation_batch.append(
game_history.get_stacked_observations(
game_pos,
self.config.stacked_observations,
len(self.config.action_space),
)
)
action_batch.append(actions)
value_batch.append(values)
reward_batch.append(rewards)
policy_batch.append(policies)
gradient_scale_batch.append(
[
min(
self.config.num_unroll_steps,
len(game_history.action_history) - game_pos,
)
]
* len(actions)
)
if self.config.PER:
weight_batch.append(1 / (self.total_samples * game_prob * pos_prob))
if self.config.PER:
weight_batch = numpy.array(weight_batch, dtype="float32") / max(
weight_batch
)
# observation_batch: batch, channels, height, width
# action_batch: batch, num_unroll_steps+1
# value_batch: batch, num_unroll_steps+1
# reward_batch: batch, num_unroll_steps+1
# policy_batch: batch, num_unroll_steps+1, len(action_space)
# weight_batch: batch
# gradient_scale_batch: batch, num_unroll_steps+1
return (
index_batch,
(
observation_batch,
action_batch,
value_batch,
reward_batch,
policy_batch,
weight_batch,
gradient_scale_batch,
),
)
def sample_game(self, force_uniform=False):
"""
Sample game from buffer either uniformly or according to some priority.
See paper appendix Training.
"""
game_prob = None
if self.config.PER and not force_uniform:
game_probs = numpy.array(
[game_history.game_priority for game_history in self.buffer.values()],
dtype="float32",
)
game_probs /= numpy.sum(game_probs)
game_index = numpy.random.choice(len(self.buffer), p=game_probs)
game_prob = game_probs[game_index]
else:
game_index = numpy.random.choice(len(self.buffer))
game_id = self.num_played_games - len(self.buffer) + game_index
return game_id, self.buffer[game_id], game_prob
def sample_n_games(self, n_games, force_uniform=False):
if self.config.PER and not force_uniform:
game_id_list = []
game_probs = []
for game_id, game_history in self.buffer.items():
game_id_list.append(game_id)
game_probs.append(game_history.game_priority)
game_probs = numpy.array(game_probs, dtype="float32")
game_probs /= numpy.sum(game_probs) # 每一个都除以game_probs的总和,可以看成是归一化
game_prob_dict = dict(
[(game_id, prob) for game_id, prob in zip(game_id_list, game_probs)]
)
selected_games = numpy.random.choice(game_id_list, n_games, p=game_probs) # 抽取n个样本, 抽取的概率是根据game_probs确定的
else:
selected_games = numpy.random.choice(list(self.buffer.keys()), n_games)
game_prob_dict = {}
ret = [
(game_id, self.buffer[game_id], game_prob_dict.get(game_id))
for game_id in selected_games
]
return ret # ret格式为[game_id, game_history, game_prob]
def sample_position(self, game_history, force_uniform=False):
"""
统一或根据某些优先级从游戏中采样位置。
Sample position from game either uniformly or according to some priority.
See paper appendix Training.
"""
position_prob = None
if self.config.PER and not force_uniform:
position_probs = game_history.priorities / sum(game_history.priorities)
position_index = numpy.random.choice(len(position_probs), p=position_probs)
position_prob = position_probs[position_index]
else:
position_index = numpy.random.choice(len(game_history.root_values))
return position_index, position_prob
def update_game_history(self, game_id, game_history):
# The element could have been removed since its selection and update
if next(iter(self.buffer)) <= game_id:
if self.config.PER:
# Avoid read only array when loading replay buffer from disk
game_history.priorities = numpy.copy(game_history.priorities)
self.buffer[game_id] = game_history
def update_priorities(self, priorities, index_info):
"""
Update game and position priorities with priorities calculated during the training.
See Distributed Prioritized Experience Replay https://arxiv.org/abs/1803.00933
"""
for i in range(len(index_info)):
game_id, game_pos = index_info[i]
# The element could have been removed since its selection and training
if next(iter(self.buffer)) <= game_id:
# Update position priorities
priority = priorities[i, :]
start_index = game_pos
end_index = min(
game_pos + len(priority), len(self.buffer[game_id].priorities)
)
self.buffer[game_id].priorities[start_index:end_index] = priority[
: end_index - start_index
]
# Update game priorities
self.buffer[game_id].game_priority = numpy.max(
self.buffer[game_id].priorities
)
def compute_target_value(self, game_history, index):
# The value target is the discounted root value of the search tree td_steps into the
# future, plus the discounted sum of all rewards until then.
# 价值目标是未来搜索树 td_steps 的折扣根值,加上到那时为止的所有奖励的折扣总和。
# 计算公式 ∑r*γ^n
bootstrap_index = index + self.config.td_steps
if bootstrap_index < len(game_history.root_values):
root_values = (
game_history.root_values
if game_history.reanalysed_predicted_root_values is None
else game_history.reanalysed_predicted_root_values
)
# 检查当前的id和目标id是否一致,如果不一致则取负
last_step_value = (
root_values[bootstrap_index]
if game_history.to_play_history[bootstrap_index]
== game_history.to_play_history[index]
else -root_values[bootstrap_index]
)
# 计算公式 r*γ^n
value = last_step_value * self.config.discount**self.config.td_steps
else: # 因为终点的长度超过了数据,因此设为0
value = 0
for i, reward in enumerate(
game_history.reward_history[index + 1 : bootstrap_index + 1] # 获取reward,从index+1到最大(如果长度不够则只会取到最后)
):
# 根据对手决定正负号,只会累计到value上
# The value is oriented from the perspective of the current player
value += (
reward
if game_history.to_play_history[index]
== game_history.to_play_history[index + i]
else -reward
) * self.config.discount**i
return value # 返回value
def make_target(self, game_history, state_index):
"""
Generate targets for every unroll steps.
"""
# target policies 是 策略选择的概率序列,如[[0.4,0.6], [0.5,0.5],...]
target_values, target_rewards, target_policies, actions = [], [], [], []
for current_index in range(
state_index, state_index + self.config.num_unroll_steps + 1
):
value = self.compute_target_value(game_history, current_index)
if current_index < len(game_history.root_values):
target_values.append(value)
target_rewards.append(game_history.reward_history[current_index])
target_policies.append(game_history.child_visits[current_index])
actions.append(game_history.action_history[current_index])
elif current_index == len(game_history.root_values):
target_values.append(0)
target_rewards.append(game_history.reward_history[current_index])
# Uniform policy
# 因为是游戏结束的状态,因此选择各个策略的概率是平均分布的
target_policies.append(
[
1 / len(game_history.child_visits[0])
for _ in range(len(game_history.child_visits[0]))
]
)
actions.append(game_history.action_history[current_index])
else: # 如果current index 大于 game_history的长度
# States past the end of games are treated as absorbing states
# 游戏结束后的状态被视为吸收状态,因此都为0
target_values.append(0)
target_rewards.append(0)
# Uniform policy
target_policies.append(
[
1 / len(game_history.child_visits[0])
for _ in range(len(game_history.child_visits[0]))
]
)
actions.append(numpy.random.choice(self.config.action_space))
return target_values, target_rewards, target_policies, actions
@ray.remote
class Reanalyse:
"""
Class which run in a dedicated thread to update the replay buffer with fresh information.
See paper appendix Reanalyse.
"""
def __init__(self, initial_checkpoint, config):
self.config = config
# Fix random generator seed
numpy.random.seed(self.config.seed)
torch.manual_seed(self.config.seed)
# Initialize the network
self.model = models.MuZeroNetwork(self.config)
self.model.set_weights(initial_checkpoint["weights"])
self.model.to(torch.device("cuda" if self.config.reanalyse_on_gpu else "cpu"))
self.model.eval()
self.num_reanalysed_games = initial_checkpoint["num_reanalysed_games"]
def reanalyse(self, replay_buffer, shared_storage):
while ray.get(shared_storage.get_info.remote("num_played_games")) < 1:
time.sleep(0.1)
while ray.get(
shared_storage.get_info.remote("training_step")
) < self.config.training_steps and not ray.get(
shared_storage.get_info.remote("terminate")
):
self.model.set_weights(ray.get(shared_storage.get_info.remote("weights")))
game_id, game_history, _ = ray.get(
replay_buffer.sample_game.remote(force_uniform=True)
)
# Use the last model to provide a fresher, stable n-step value (See paper appendix Reanalyze)
if self.config.use_last_model_value:
observations = numpy.array(
[
game_history.get_stacked_observations(
i,
self.config.stacked_observations,
len(self.config.action_space),
)
for i in range(len(game_history.root_values))
]
)
observations = (
torch.tensor(observations)
.float()
.to(next(self.model.parameters()).device)
)
values = models.support_to_scalar(
self.model.initial_inference(observations)[0],
self.config.support_size,
)
game_history.reanalysed_predicted_root_values = (
torch.squeeze(values).detach().cpu().numpy()
)
replay_buffer.update_game_history.remote(game_id, game_history)
self.num_reanalysed_games += 1
shared_storage.set_info.remote(
"num_reanalysed_games", self.num_reanalysed_games
)