Experience buffer optimisations
@@ -35,16 +35,22 @@ Experience = collections.namedtuple('Experience', field_names=['state', 'action'
class ExperienceBuffer:
def __init__(self, capacity):
- self.buffer = collections.deque(maxlen=capacity)
+ self.buffer = []
+ self.capacity = capacity
+ self.pos = 0
def __len__(self):
return len(self.buffer)
def append(self, experience):
+ if len(self.buffer) < self.capacity:
self.buffer.append(experience)
+ else:
+ self.buffer[self.pos] = experience
+ self.pos = (self.pos + 1) % self.capacity
def sample(self, batch_size):
- indices = np.random.choice(len(self.buffer), batch_size, replace=False)
+ indices = np.random.choice(len(self.buffer), batch_size, replace=True)
states, actions, rewards, dones, next_states = zip(*[self.buffer[idx] for idx in indices])
return np.array(states), np.array(actions), np.array(rewards, dtype=np.float32), \
np.array(dones, dtype=np.uint8), np.array(next_states)
