-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
55 lines (44 loc) · 1.4 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import random
from sumtree import SumTree
class Memory:
"""
Stores transitions as (s, a, r, s_, done) tuples using a SumTree.
Each sample is assigned a priority which affects retrieval
"""
def __init__(self, capacity, e=0.01, a=0.6):
"""
:param capacity: The maximum number of samples that can be stored
:param e: Ensures that no sample has 0 priority
:param a:
"""
self.capacity = capacity
self.e = e
self.a = a
self.tree = SumTree(capacity)
def _getPriority(self, error):
return (error + self.e) ** self.a
def add(self, error, sample):
"""
Adds a new sample to the buffer
:param error: The error associated with the sample
:param sample: The sample to add
"""
p = self._getPriority(error)
self.tree.add(p, sample)
def sample(self, n):
"""
Returns n samples from the buffer
:param n: The number of samples to return
"""
batch = []
segment = self.tree.total() / n
for i in range(n):
a = segment * i
b = segment * (i + 1)
s = random.uniform(a, b)
(idx, p, data) = self.tree.get(s)
batch.append((idx, data))
return batch
def update(self, idx, error):
p = self._getPriority(error)
self.tree.update(idx, p)