-
Notifications
You must be signed in to change notification settings - Fork 181
/
replay_memory.py
41 lines (32 loc) · 1.37 KB
/
replay_memory.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
import random
import numpy as np
class ReplayMemory:
def __init__(self, capacity, seed):
random.seed(seed)
self.capacity = capacity
self.buffer = []
self.position = 0
def push(self, state, action, reward, next_state, done):
if len(self.buffer) < self.capacity:
self.buffer.append(None)
self.buffer[self.position] = (state, action, reward, next_state, done)
self.position = (self.position + 1) % self.capacity
def sample(self, batch_size):
batch = random.sample(self.buffer, batch_size)
state, action, reward, next_state, done = map(np.stack, zip(*batch))
return state, action, reward, next_state, done
def __len__(self):
return len(self.buffer)
def save_buffer(self, env_name, suffix="", save_path=None):
if not os.path.exists('checkpoints/'):
os.makedirs('checkpoints/')
if save_path is None:
save_path = "checkpoints/sac_buffer_{}_{}".format(env_name, suffix)
print('Saving buffer to {}'.format(save_path))
with open(save_path, 'wb') as f:
pickle.dump(self.buffer, f)
def load_buffer(self, save_path):
print('Loading buffer from {}'.format(save_path))
with open(save_path, "rb") as f:
self.buffer = pickle.load(f)
self.position = len(self.buffer) % self.capacity