-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreplay_buffer_col.py
348 lines (285 loc) · 12.4 KB
/
replay_buffer_col.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
'''
Replay Buffer file modified from Stable Baselines:
https://github.com/hill-a/stable-baselines/blob/master/stable_baselines/deepq/replay_buffer.py
'''
import random
import numpy as np
from stable_baselines.common.segment_tree import SumSegmentTree, MinSegmentTree
class ReplayBuffer(object):
def __init__(self, size):
"""
Implements a ring buffer (FIFO).
:param size: (int) Max number of transitions to store in the buffer. When the buffer overflows the old
memories are dropped.
"""
self._storage = []
self._maxsize = size
self._next_idx = 0
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._maxsize
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, obs_t, action, reward, obs_tp1, done):
"""
add a new transition to the buffer
:param obs_t: (Any) the last observation
:param action: ([float]) the action
:param reward: (float) the reward of the transition
:param obs_tp1: (Any) the current observation
:param done: (bool) is the episode done
"""
data = (obs_t, action, reward, obs_tp1, done)
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._maxsize
def _encode_sample(self, idxes):
obses_t, actions, rewards, obses_tp1, dones = [], [], [], [], []
for i in idxes:
data = self._storage[i]
obs_t, action, reward, obs_tp1, done = data
obses_t.append(np.array(obs_t, copy=False))
actions.append(np.array(action, copy=False))
rewards.append(reward)
obses_tp1.append(np.array(obs_tp1, copy=False))
dones.append(done)
return np.array(obses_t), np.array(actions), np.array(rewards), np.array(obses_tp1), np.array(dones)
def sample(self, batch_size, **_kwargs):
"""
Sample a batch of experiences.
:param batch_size: (int) How many transitions to sample.
:return:
- obs_batch: (np.ndarray) batch of observations
- act_batch: (numpy float) batch of actions executed given obs_batch
- rew_batch: (numpy float) rewards received as results of executing act_batch
- next_obs_batch: (np.ndarray) next set of observations seen after executing act_batch
- done_mask: (numpy bool) done_mask[i] = 1 if executing act_batch[i] resulted in the end of an episode
and 0 otherwise.
"""
idxes = [random.randint(0, len(self._storage) - 1) for _ in range(batch_size)]
return self._encode_sample(idxes)
class ReplayBufferEpisodes(object):
def __init__(self, size):
"""
Implements a ring buffer (FIFO) that stores full episodes instead of
only transition samples.
:param size: (int) Max number of episodes to store in the buffer. When the buffer overflows the old
memories are dropped.
"""
self._storage = []
self._maxsize = size
self._next_idx = 0
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._maxsize
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, episode_traj):
"""
add a new episode to the buffer
:param episode_traj: list of arrays containing obs_t, action, reward,
obs_tp1, and done for a complete episode.
"""
if self._next_idx >= len(self._storage):
self._storage.append(episode_traj)
else:
self._storage[self._next_idx] = episode_traj
self._next_idx = (self._next_idx + 1) % self._maxsize
def _encode_sample(self, idxes):
for i in range(len(idxes)):
# parse sampled data
data = self._storage[idxes[i]]
new_obs_t = data['obs_t']
new_action = data['action']
new_reward = data['reward'].reshape(-1, 1)
new_obs_tp1 = data['obs_tp1']
new_done = data['done'].reshape(-1, 1)
# aggregate with previous data
if i == 0:
obs_t = new_obs_t
action = new_action
reward = new_reward
obs_tp1 = new_obs_tp1
done = new_done
else:
obs_t = np.vstack((obs_t, new_obs_t))
action = np.vstack((action, new_action))
reward = np.vstack((reward, new_reward))
obs_tp1 = np.vstack((obs_tp1, new_obs_tp1))
done = np.vstack((done, new_done))
return obs_t, action, reward, obs_tp1, done
def sample(self, batch_size, **_kwargs):
"""
Sample a batch of experiences.
:param batch_size: (int) How many transitions to sample.
:return:
- obs_batch: (np.ndarray) batch of observations
- act_batch: (numpy float) batch of actions executed given obs_batch
- rew_batch: (numpy float) rewards received as results of executing act_batch
- next_obs_batch: (np.ndarray) next set of observations seen after executing act_batch
- done_mask: (numpy bool) done_mask[i] = 1 if executing act_batch[i] resulted in the end of an episode
and 0 otherwise.
"""
if batch_size == 0:
return np.zeros(1), np.zeros(1), np.zeros(1), np.zeros(1), np.zeros(1)
else:
idxes = [np.random.randint(0, len(self._storage)) for _ in range(batch_size)]
return self._encode_sample(idxes)
def _test_add_sample(self):
print('Testing adding and sampling complete episodes.')
# generate random episode data
n_epis = 5
for i in range(n_epis):
# generate data
epi_trajs = []
n_steps = np.random.randint(low=5, high=10)
obs_t = np.random.rand(n_steps, 3)
action = np.random.rand(n_steps, 2)
reward = np.random.rand(n_steps, 1)
obs_tp1 = np.random.rand(n_steps, 3)
done = np.random.randint(low=0, high=2, size=n_steps)
# aggregate data
episode_traj = (obs_t, action, reward, obs_tp1, done)
print('Episode {}: done = {}'.format(i, done))
# add to buffer
self.add(episode_traj)
# sample
sampled_obs0, sampled_act, sampled_rew, sampled_obs1, sampled_done = self.sample(batch_size=2)
for j in range(len(sampled_done)):
print('Sampled {}: {}'.format(j, sampled_done[j]))
class PrioritizedReplayBuffer(ReplayBuffer):
def __init__(self, size, alpha):
"""
Create Prioritized Replay buffer.
See Also ReplayBuffer.__init__
:param size: (int) Max number of transitions to store in the buffer. When the buffer overflows the old memories
are dropped.
:param alpha: (float) how much prioritization is used (0 - no prioritization, 1 - full prioritization)
"""
super(PrioritizedReplayBuffer, self).__init__(size)
assert alpha >= 0
self._alpha = alpha
it_capacity = 1
while it_capacity < size:
it_capacity *= 2
self._it_sum = SumSegmentTree(it_capacity)
self._it_min = MinSegmentTree(it_capacity)
self._max_priority = 1.0
# make sure expert samples are not overwritten by setting the first
# index of the buffer to the number of expert samples we received
self.expert_idx = 0
def add(self, obs_t, action, reward, obs_tp1, done):
"""
add a new transition to the buffer
:param obs_t: (Any) the last observation
:param action: ([float]) the action
:param reward: (float) the reward of the transition
:param obs_tp1: (Any) the current observation
:param done: (bool) is the episode done
"""
idx = self._next_idx
super().add(obs_t, action, reward, obs_tp1, done)
self._it_sum[idx] = self._max_priority ** self._alpha
self._it_min[idx] = self._max_priority ** self._alpha
# make sure expert demos are not overwritten
if self._next_idx == 0:
self._next_idx = self.expert_idx
def _sample_proportional(self, batch_size):
res = []
for _ in range(batch_size):
# TODO(szymon): should we ensure no repeats?
mass = random.random() * self._it_sum.sum(0, len(self._storage) - 1)
idx = self._it_sum.find_prefixsum_idx(mass)
res.append(idx)
return res
def sample(self, batch_size, beta=0):
"""
Sample a batch of experiences.
compared to ReplayBuffer.sample
it also returns importance weights and idxes
of sampled experiences.
:param batch_size: (int) How many transitions to sample.
:param beta: (float) To what degree to use importance weights (0 - no corrections, 1 - full correction)
:return:
- obs_batch: (np.ndarray) batch of observations
- act_batch: (numpy float) batch of actions executed given obs_batch
- rew_batch: (numpy float) rewards received as results of executing act_batch
- next_obs_batch: (np.ndarray) next set of observations seen after executing act_batch
- done_mask: (numpy bool) done_mask[i] = 1 if executing act_batch[i] resulted in the end of an episode
and 0 otherwise.
- weights: (numpy float) Array of shape (batch_size,) and dtype np.float32 denoting importance weight of
each sampled transition
- idxes: (numpy int) Array of shape (batch_size,) and dtype np.int32 idexes in buffer of sampled experiences
"""
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)
for idx in idxes:
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)
encoded_sample = self._encode_sample(idxes)
return tuple(list(encoded_sample) + [weights, idxes])
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):
assert 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)
if __name__ == "__main__":
# test adding and sampling complete episodes from buffer
buffer = ReplayBufferEpisodes(size=10)
buffer._test_add_sample()