-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplay_buffer.py
70 lines (58 loc) · 2 KB
/
replay_buffer.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
DQN replay buffer
"""
import random
from collections import deque
import numpy as np
class DqnReplayBuffer:
"""
DQN replay buffer to keep track of game play records
"""
def __init__(self, max_size):
self.max_size = max_size
self.experiences = deque(maxlen=max_size)
# pylint: disable=too-many-arguments
def record(self, state, reward, next_state, action, done):
"""
Puts a game play state into records
:param state: current game state
:param reward: reward after taking action
:param next_state: state after taking action
:param action: action taken
:param done: if the episode is finished
:return: None
"""
self.experiences.append((state, next_state, action, reward, done))
def get_volume(self):
"""
Gets the current length of the records
:return: (int) the length of the records
"""
return len(self.experiences)
def can_sample_batch(self, batch_size):
"""
Returns if a batch can be sampled
:param batch_size: the size of the batch to be sampled
:return: (bool) if can sample
"""
return self.get_volume() > batch_size
def sample_batch(self, batch_size):
"""
Samples a batch from the records
:param batch_size: the size of the batch to be sampled
:return: sample batch
"""
sampled_batch = random.sample(self.experiences, batch_size)
state_batch = []
next_state_batch = []
action_batch = []
reward_batch = []
done_batch = []
for record in sampled_batch:
state_batch.append(record[0])
next_state_batch.append(record[1])
action_batch.append(record[2])
reward_batch.append(record[3])
done_batch.append(record[4])
return np.array(state_batch), np.array(next_state_batch), np.array(
action_batch), np.array(reward_batch), np.array(done_batch)