-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperience_buffer.py
More file actions
39 lines (30 loc) · 1.05 KB
/
experience_buffer.py
File metadata and controls
39 lines (30 loc) · 1.05 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
class ExperienceBuffer:
"""
Stores transitions for learning + abstraction.
"""
def __init__(self, max_size=1000):
self.buffer = []
self.max_size = max_size
# =====================================================
# STORE EXPERIENCE
# =====================================================
def add(self, state, action, reward, next_state):
self.buffer.append({
"state": state,
"action": action,
"reward": reward,
"next_state": next_state
})
if len(self.buffer) > self.max_size:
self.buffer.pop(0)
# =====================================================
# SAMPLE BATCH
# =====================================================
def sample(self, n=32):
import random
return random.sample(self.buffer, min(n, len(self.buffer)))
# =====================================================
# FULL HISTORY
# =====================================================
def all(self):
return self.buffer