forked from sfujim/BCQ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
37 lines (29 loc) · 1.11 KB
/
utils.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
import numpy as np
# Code based on:
# https://github.com/openai/baselines/blob/master/baselines/deepq/replay_buffer.py
# Simple replay buffer
class ReplayBuffer(object):
def __init__(self):
self.storage = []
# Expects tuples of (state, next_state, action, reward, done)
def add(self, data):
self.storage.append(data)
def sample(self, batch_size):
ind = np.random.randint(0, len(self.storage), size=batch_size)
state, next_state, action, reward, done = [], [], [], [], []
for i in ind:
s, s2, a, r, d = self.storage[i]
state.append(np.array(s, copy=False))
next_state.append(np.array(s2, copy=False))
action.append(np.array(a, copy=False))
reward.append(np.array(r, copy=False))
done.append(np.array(d, copy=False))
return (np.array(state),
np.array(next_state),
np.array(action),
np.array(reward).reshape(-1, 1),
np.array(done).reshape(-1, 1))
def save(self, filename):
np.save("./buffers/"+filename+".npy", self.storage, allow_pickle=True)
def load(self, filename):
self.storage = np.load("./buffers/"+filename+".npy", allow_pickle=True)