-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
38 lines (28 loc) · 1.31 KB
/
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
import numpy as np
from collections import namedtuple
import random
class Memory:
def __init__(self):
self.memory = []
self.experience = namedtuple('Experience',
field_names=['actor_state', 'critic_state', 'action', 'log_prob', 'reward'])
def add(self, actor_state, critic_state, action, log_prob, reward):
"""Add a new experience to memory."""
e = self.experience( actor_state, critic_state, action, log_prob, reward )
self.memory.append(e)
def experiences(self, clear=True):
actor_states = np.vstack([e.actor_state for e in self.memory if e is not None])
critic_states = np.vstack([e.critic_state for e in self.memory if e is not None])
actions = np.vstack([e.action for e in self.memory if e is not None])
log_probs = np.vstack([e.log_prob for e in self.memory if e is not None])
rewards = np.vstack([e.reward for e in self.memory if e is not None])
n_exp = len(self)
if clear:
self.clear()
return actor_states, critic_states, actions, log_probs, rewards, n_exp
def delete(self, i):
del self.memory[i]
def clear(self):
self.memory.clear()
def __len__(self):
return len(self.memory)