-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
65 lines (51 loc) · 1.86 KB
/
Copy pathmemory.py
File metadata and controls
65 lines (51 loc) · 1.86 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
memory.py
---------
Description:
This file implements a replay memory class.
"""
import random
from typing import Any
class ReplayMemory:
"""Implements a circular replay memory object based on list storage and with random sampling."""
def __init__(self, capacity: int, batch_size: int):
"""
Initialize the replay memory.
Args:
capacity: Size of buffer.
batch_size: Size of minibatch sample.
"""
self._capacity = capacity
self._batch_size = batch_size
self._buffer: list = []
self._index: int = 0
def __len__(self):
return len(self._buffer)
def push(self, obs: Any, action: Any, reward: Any, next_obs: Any, terminal: bool) -> None:
"""
Add a transition to the replay memory. When the buffer is full,
the oldest transitions are replaced with new ones.
Args:
obs: Agent's observation
action: Executed action.
reward: Reward received.
next_obs: Resulting observation.
terminal: Whether it is terminal transition.
"""
if len(self._buffer) < self._capacity:
self._buffer.append(None)
self._buffer[self._index] = (obs, action, reward, next_obs, int(terminal))
self._index = (self._index + 1) % self._capacity
def sample(self) -> tuple:
"""
Sample a minibatch of transitions.
Raises:
ValueError: if not enough transitions exist to sample.
Returns:
5-tuple of obs, actions, rewards, next_obs, dones
"""
if len(self._buffer) < self._batch_size:
raise ValueError("Not enough transitions to sample a minibatch")
sample = random.sample(self._buffer, self._batch_size)
return tuple(zip(*sample))
# ============== END OF FILE ==============