-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplayBuffer.py
94 lines (75 loc) · 3.85 KB
/
replayBuffer.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
import gym
import tensorflow as tf
import numpy as np
import random
class replayBuffer (obj):
def __init__ (self, size, historySize):
#Assume that returing frame of zeros at the beginning of the episode
#Params: size: max number of transitions to store in the buffer.When the buffer size is full, the old will be replaced
#Params: historySize: number of memories to be retreied for each observation
self.size = size
self.historySize = historySize
self.next_idx = 0
self.num_in_buffer = 0
self.obs = None
self.action = None
self.reward = None
self.done = None
def canBeSampled(self, batchSize):
return batchSize + 1 <= self.num_in_buffer
def _encode_sample(self, idxes):
obs_batch = np.concatenate([self._encode_observation(idx)[None] for idx in idxes], 0)
act_batch = self.action[idxes]
rew_batch = self.reward[idxes]
next_obs_batch = np.concatenate([self._encode_observation(idx + 1)[None] for idx in idxes], 0)
done_mask = np.array([1.0 if self.done[idx] else 0.0 for idx in idxes], dtype=np.float32)
return obs_batch, act_batch, rew_batch, next_obs_batch, done_mask
def _encode_observation(self, idx):
end_idx = idx + 1 # make noninclusive
start_idx = end_idx - self.historySize
# if there weren't enough frames ever in the buffer for context
if start_idx < 0 and self.num_in_buffer != self.size:
start_idx = 0
for idx in range(start_idx, end_idx - 1):
if self.done[idx % self.size]:
start_idx = idx + 1
missing_context = self.historySize - (end_idx - start_idx)
# if zero padding is needed for missing context
# or we are on the boundry of the buffer
if start_idx < 0 or missing_context > 0:
frames = [np.zeros_like(self.obs[0]) for _ in range(missing_context)]
for idx in range(start_idx, end_idx):
frames.append(self.obs[idx % self.size])
return np.concatenate(frames, 2)
else:
# this optimization has potential to saves about 30% compute time \o/
img_h, img_w = self.obs.shape[1], self.obs.shape[2]
return self.obs[start_idx:end_idx].transpose(1, 2, 0, 3).reshape(img_h, img_w, -1)
def getSample(self, batchSize):
#batchSize: How many transitions to sample.
idxes = sample_n_unique(lambda: random.randint(0, self.num_in_buffer - 2), batchSize)
return self._encode_sample(idxes)
def encodeRecentObs(self):
"""Return the most recent `historySize` frames.
Returns
-------
observation: np.array
Array of shape (img_h, img_w, img_c * historySize)
and dtype np.uint8, where observation[:, :, i*img_c:(i+1)*img_c]
encodes frame at time `t - historySize + i`
"""
assert self.num_in_buffer > 0
return self._encode_observation((self.next_idx - 1) % self.size)
def storeFrame(self, frame):
#Store a single frame in the buffer at the next available index, overwriting old frames if necessary.
#frame: np.array, Array of shape (img_h, img_w, img_c) and dtype np.uint8
if self.obs is None:
self.obs = np.empty([self.size] + list(frame.shape), dtype=np.uint8)
self.action = np.empty([self.size], dtype=np.int32)
self.reward = np.empty([self.size], dtype=np.float32)
self.done = np.empty([self.size], dtype=np.bool)
self.obs[self.next_idx] = frame
ret = self.next_idx
self.next_idx = (self.next_idx + 1) % self.size
self.num_in_buffer = min(self.size, self.num_in_buffer + 1)
return ret