-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
27 lines (20 loc) · 690 Bytes
/
memory.py
File metadata and controls
27 lines (20 loc) · 690 Bytes
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
from collections import namedtuple
import random
Experience = namedtuple('Experience',
('states','actions','next_states','rewards'))
class ReplayMemory:
def __init__(self, capacity):
self.capacity = capacity
self.memory = []
self.position = 0
def push(self, *args):
if len(self.memory) < self.capacity:
self.memory.append(None)
self.memory[self.position] = Experience(*args)
self.position = (self.position + 1) % self.capacity
def sample(self, batch_size):
# Randomly sample a batch of experiences from memory"
return random.sample(self.memory, batch_size)
def __len__(self):
"""Return the current size of internal memory."""
return len(self.memory)