-
Notifications
You must be signed in to change notification settings - Fork 11
/
dqn_memory_priortized_replay_buffer.py
316 lines (264 loc) · 12.6 KB
/
dqn_memory_priortized_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
# part of the code are from https://github.com/hill-a/stable-baselines/
import random
from collections import namedtuple
import numpy as np
import torch
from generic import to_np
from segment_tree import SumSegmentTree, MinSegmentTree
# a snapshot of state to be stored in replay memory
Transition = namedtuple('Transition', ('observation_list', 'prev_action_list', 'action_candidate_list', 'chosen_indices', 'graph_triplets', 'reward', 'graph_reward', 'count_reward', 'is_final'))
class PrioritizedReplayMemory(object):
def __init__(self, capacity=100000, priority_fraction=0.0, discount_gamma_game_reward=1.0, discount_gamma_graph_reward=1.0, discount_gamma_count_reward=1.0, accumulate_reward_from_final=False, seed=None):
self.rng = np.random.RandomState(seed)
# prioritized replay memory
self._storage = []
self.capacity = capacity
self._next_idx = 0
assert priority_fraction >= 0
self._alpha = priority_fraction
it_capacity = 1
while it_capacity < capacity:
it_capacity *= 2
self._it_sum = SumSegmentTree(it_capacity)
self._it_min = MinSegmentTree(it_capacity)
self._max_priority = 1.0
self.discount_gamma_game_reward = discount_gamma_game_reward
self.discount_gamma_graph_reward = discount_gamma_graph_reward
self.discount_gamma_count_reward = discount_gamma_count_reward
self.accumulate_reward_from_final = accumulate_reward_from_final
def __len__(self):
return len(self._storage)
@property
def storage(self):
"""[(np.ndarray, float, float, np.ndarray, bool)]: content of the replay buffer"""
return self._storage
@property
def buffer_size(self):
"""float: Max capacity of the buffer"""
return self.capacity
def can_sample(self, n_samples):
"""
Check if n_samples samples can be sampled
from the buffer.
:param n_samples: (int)
:return: (bool)
"""
return len(self) >= n_samples
def is_full(self):
"""
Check whether the replay buffer is full or not.
:return: (bool)
"""
return len(self) == self.buffer_size
def add(self, *args):
"""
add a new transition to the buffer
"""
idx = self._next_idx
data = Transition(*args)
if self._next_idx >= len(self._storage):
self._storage.append(data)
else:
self._storage[self._next_idx] = data
self._next_idx = (self._next_idx + 1) % self.capacity
self._it_sum[idx] = self._max_priority ** self._alpha
self._it_min[idx] = self._max_priority ** self._alpha
def get_next_final_pos(self, which_memory, head):
i = head
while True:
if i >= len(self._storage):
return None
if self._storage[i].is_final:
return i
i += 1
return None
def _get_single_transition(self, idx, n):
assert n > 0
head = idx
# if n is 1, then head can't be is_final
if n == 1:
if self._storage[head].is_final:
return None
# if n > 1, then all except tail can't be is_final
else:
if np.any([item.is_final for item in self._storage[head: head + n]]):
return None
next_final = self.get_next_final_pos(self._storage, head)
if next_final is None:
return None
# all good
obs = self._storage[head].observation_list
prev_action = self._storage[head].prev_action_list
candidate = self._storage[head].action_candidate_list
chosen_indices = self._storage[head].chosen_indices
graph_triplets = self._storage[head].graph_triplets
next_obs = self._storage[head + n].observation_list
next_prev_action = self._storage[head + n].prev_action_list
next_candidate = self._storage[head + n].action_candidate_list
next_graph_triplets = self._storage[head + n].graph_triplets
tmp = next_final - head + 1 if self.accumulate_reward_from_final else n + 1
rewards_up_to_next_final = [self.discount_gamma_game_reward ** i * self._storage[head + i].reward for i in range(tmp)]
reward = torch.sum(torch.stack(rewards_up_to_next_final))
graph_rewards_up_to_next_final = [self.discount_gamma_graph_reward ** i * self._storage[head + i].graph_reward for i in range(tmp)]
graph_reward = torch.sum(torch.stack(graph_rewards_up_to_next_final))
count_rewards_up_to_next_final = [self.discount_gamma_count_reward ** i * self._storage[head + i].count_reward for i in range(tmp)]
count_reward = torch.sum(torch.stack(count_rewards_up_to_next_final))
return (obs, prev_action, candidate, chosen_indices, graph_triplets, reward + graph_reward + count_reward, next_obs, next_prev_action, next_candidate, next_graph_triplets)
def _encode_sample(self, idxes, ns):
actual_indices, actual_ns = [], []
obs, prev_action, candidate, chosen_indices, graph_triplets, reward, next_obs, next_prev_action, next_candidate, next_graph_triplets = [], [], [], [], [], [], [], [], [], []
for i, n in zip(idxes, ns):
t = self._get_single_transition(i, n)
if t is None:
continue
actual_indices.append(i)
actual_ns.append(n)
obs.append(t[0])
prev_action.append(t[1])
candidate.append(t[2])
chosen_indices.append(t[3])
graph_triplets.append(t[4])
reward.append(t[5])
next_obs.append(t[6])
next_prev_action.append(t[7])
next_candidate.append(t[8])
next_graph_triplets.append(t[9])
if len(actual_indices) == 0:
return None
chosen_indices = np.array(chosen_indices) # batch
reward = torch.stack(reward, 0) # batch
actual_ns = np.array(actual_ns)
return [obs, prev_action, candidate, chosen_indices, graph_triplets, reward, next_obs, next_prev_action, next_candidate, next_graph_triplets, actual_indices, actual_ns]
def sample(self, batch_size, beta=0, multi_step=1):
assert beta > 0
idxes = self._sample_proportional(batch_size)
weights = []
p_min = self._it_min.min() / self._it_sum.sum()
max_weight = (p_min * len(self._storage)) ** (-beta)
# sample n
ns = self.rng.randint(1, multi_step + 1, size=batch_size)
encoded_sample = self._encode_sample(idxes, ns)
if encoded_sample is None:
return None
actual_indices = encoded_sample[-2]
for idx in actual_indices:
p_sample = self._it_sum[idx] / self._it_sum.sum()
weight = (p_sample * len(self._storage)) ** (-beta)
weights.append(weight / max_weight)
weights = np.array(weights)
return encoded_sample + [weights]
def _get_single_sequence_transition(self, idx, sample_history_length):
assert sample_history_length > 0
head = idx
# if n is 1, then head can't be is_final
if sample_history_length == 1:
if self._storage[head].is_final:
return None
# if n > 1, then all except tail can't be is_final
else:
if np.any([item.is_final for item in self._storage[head: head + sample_history_length]]):
return None
next_final = self.get_next_final_pos(self._storage, head)
if next_final is None:
return None
# all good
res = []
for m in range(sample_history_length):
obs = self._storage[head + m].observation_list
candidate = self._storage[head + m].action_candidate_list
chosen_indices = self._storage[head + m].chosen_indices
graph_triplets = self._storage[head + m].graph_triplets
next_obs = self._storage[head + m + 1].observation_list
next_candidate = self._storage[head + m + 1].action_candidate_list
next_graph_triplets = self._storage[head + m + 1].graph_triplets
tmp = next_final - (head + m) + 1 if self.accumulate_reward_from_final else 1
rewards_up_to_next_final = [self.discount_gamma_game_reward ** i * self._storage[head + m + i].reward for i in range(tmp)]
reward = torch.sum(torch.stack(rewards_up_to_next_final))
graph_rewards_up_to_next_final = [self.discount_gamma_graph_reward ** i * self._storage[head + m + i].graph_reward for i in range(tmp)]
graph_reward = torch.sum(torch.stack(graph_rewards_up_to_next_final))
count_rewards_up_to_next_final = [self.discount_gamma_count_reward ** i * self._storage[head + m + i].count_reward for i in range(tmp)]
count_reward = torch.sum(torch.stack(count_rewards_up_to_next_final))
res.append([obs, candidate, chosen_indices, graph_triplets, reward + graph_reward + count_reward, next_obs, next_candidate, next_graph_triplets])
return res
def _encode_sample_sequence(self, idxes, sample_history_length):
assert sample_history_length > 0
res = []
for _ in range(sample_history_length):
tmp = []
for i in range(8):
tmp.append([])
res.append(tmp)
actual_indices = []
# obs, candidate, chosen_indices, graph_triplets, reward, next_obs, next_candidate, next_graph_triplets
for i in idxes:
t = self._get_single_sequence_transition(i, sample_history_length)
if t is None:
continue
actual_indices.append(i)
for step in range(sample_history_length):
t_s = t[step]
res[step][0].append(t_s[0])
res[step][1].append(t_s[1])
res[step][2].append(t_s[2])
res[step][3].append(t_s[3])
res[step][4].append(t_s[4])
res[step][5].append(t_s[5])
res[step][6].append(t_s[6])
res[step][7].append(t_s[7])
if len(actual_indices) == 0:
return None
for i in range(sample_history_length):
res[i][2] = np.array(res[i][2]) # batch
res[i][4] = torch.stack(res[i][4], 0) # batch
return res + [actual_indices]
def sample_sequence(self, batch_size, beta=0, sample_history_length=1):
assert beta > 0
idxes = self._sample_proportional(batch_size)
res_weights = []
p_min = self._it_min.min() / self._it_sum.sum()
max_weight = (p_min * len(self._storage)) ** (-beta)
encoded_sample = self._encode_sample_sequence(idxes, sample_history_length)
if encoded_sample is None:
return None
actual_indices = encoded_sample[-1]
for _h in range(sample_history_length):
tmp_weights = []
for idx in actual_indices:
p_sample = self._it_sum[idx + _h] / self._it_sum.sum()
weight = (p_sample * len(self._storage)) ** (-beta)
tmp_weights.append(weight / max_weight)
tmp_weights = np.array(tmp_weights)
res_weights.append(tmp_weights)
return encoded_sample + [res_weights]
def _sample_proportional(self, batch_size):
res = []
for _ in range(batch_size):
mass = self.rng.random() * self._it_sum.sum(0, len(self._storage) - 1)
idx = self._it_sum.find_prefixsum_idx(mass)
res.append(idx)
return res
def update_priorities(self, idxes, priorities):
"""
Update priorities of sampled transitions.
sets priority of transition at index idxes[i] in buffer
to priorities[i].
:param idxes: ([int]) List of idxes of sampled transitions
:param priorities: ([float]) List of updated priorities corresponding to transitions at the sampled idxes
denoted by variable `idxes`.
"""
assert len(idxes) == len(priorities)
for idx, priority in zip(idxes, priorities):
if priority > 0:
assert 0 <= idx < len(self._storage)
self._it_sum[idx] = priority ** self._alpha
self._it_min[idx] = priority ** self._alpha
self._max_priority = max(self._max_priority, priority)
else:
print("something wrong with priority: ", str(priority))
return False
return True
def avg_rewards(self):
if len(self._storage) == 0:
return 0.0
rewards = [self._storage[i].reward for i in range(len(self._storage))]
return to_np(torch.mean(torch.stack(rewards)))