-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathreplayMemory.py
174 lines (157 loc) · 7.29 KB
/
replayMemory.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import numpy as np
import threading as th
import random
import time
# Basic implementation of a replay memory
class ReplayMemory:
def __init__(self, config):
self.capacity = config.replay_memory_capacity
self.batch_size = config.batch_size
self.buff_size = config.buff_size
self.screens = np.empty((self.capacity, 84,84), dtype=np.uint8)
self.actions = np.empty((self.capacity), dtype=np.uint8)
self.rewards = np.empty((self.capacity), dtype=np.int8)
self.terminals = np.empty((self.capacity), dtype=np.bool)
self.next_state_batch = np.empty((self.batch_size, 84, 84, 4), dtype=np.uint8)
self.state_batch = np.empty((self.batch_size, 84, 84, 4), dtype=np.uint8)
self.current = 0
self.step = 0
self.filled = False
def add(self, screen, action, reward, terminal):
self.screens[self.current] = screen
self.actions[self.current] = action
self.rewards[self.current] = reward
self.terminals[self.current] = terminal
self.current += 1
self.step += 1
if self.current == self.capacity:
self.current = 0
self.filled = True
def get_state(self, index):
if self.filled == False:
assert index < self.current, "%i index has note been added yet"%index
#Fast slice read
if index >= self.buff_size - 1:
state = self.screens[(index - self.buff_size+1):(index + 1), ...]
#Slow list read
else:
indexes = [(index - i) % self.capacity for i in reversed(range(self.buff_size))]
state = self.screens[indexes, ...]
# different screens should be in the 3rd dim as channels
return np.transpose(state, [1,2,0])
def sample_transition_batch(self):
if self.filled == False:
assert self.current >= self.batch_size, "There is not enough to sample. Call add bathc_size times"
indexes = []
while len(indexes) != self.batch_size:
# index, is the index of state, and index + 1 of next_state
if self.filled:
index = random.randint(0, self.capacity - 2) # -2 because index +1 will be used for next state
# if index is in the space we are currently writing
if index >= self.current and index - self.buff_size <= self.current:
continue
else:
# can't start from 0 because get_state would loop back to the end -- wich is uninitialized.
# index +1 can be terminal
index = random.randint(self.buff_size -1, self.current -2)
if self.terminals[(index - self.buff_size):index].any():
continue
self.state_batch[len(indexes)] = self.get_state(index)
self.next_state_batch[len(indexes)] = self.get_state(index + 1)
indexes.append(index)
action_batch = self.actions[indexes]
reward_batch = self.rewards[indexes]
terminal_batch = self.terminals[indexes]
return self.state_batch, action_batch, reward_batch, self.next_state_batch, terminal_batch, indexes
# Optimized version of ReplayMemory (possibly has a deadlock bug)
class CachingReplayMemory:
def __init__(self, config):
self.capacity = config.replay_memory_capacity
self.batch_size = config.batch_size
self.buff_size = config.buff_size
self.screens = np.empty((self.capacity, 84,84), dtype=np.uint8)
self.actions = np.empty((self.capacity), dtype=np.uint8)
self.rewards = np.empty((self.capacity), dtype=np.int8)
self.terminals = np.empty((self.capacity), dtype=np.bool)
self.next_state_batch = np.empty((self.batch_size, 84, 84, 4), dtype=np.uint8)
self.state_batch = np.empty((self.batch_size, 84, 84, 4), dtype=np.uint8)
self.current = 0
self.step = 0
self.filled = False
self.lock = th.Lock()
self.cache_full = th.Event()
self.cache_empty = th.Event()
self.cache_empty.set()
self.stop_caching = False
self.cache_thread = th.Thread(target=self.cache_loop)
self.cache_thread.setDaemon(True)
self.cache_thread.start()
def add(self, screen, action, reward, terminal):
self.lock.acquire()
self.screens[self.current] = screen
self.actions[self.current] = action
self.rewards[self.current] = reward
self.terminals[self.current] = terminal
self.lock.release()
self.current += 1
self.step += 1
if self.current == self.capacity:
self.current = 0
self.filled = True
def get_state(self, index):
if self.filled == False:
assert index < self.current, "%i index has note been added yet"%index
#Fast slice read
if index >= self.buff_size - 1:
state = self.screens[(index - self.buff_size+1):(index + 1), ...]
#Slow list read
else:
indexes = [(index - i) % self.capacity for i in reversed(range(self.buff_size))]
state = self.screens[indexes, ...]
# different screens should be in the 3rd dim as channels
return np.transpose(state, [1,2,0])
def sample_transition_batch(self):
if self.filled == False:
assert self.current >= self.batch_size, "There are to few samples. At least add() batch_size times"
self.cache_full.wait()
transition_ba = [self.state_batch.copy(), self.action_batch, self.reward_batch, self.next_state_batch.copy(), self.terminal_batch, self.indexes]
self.cache_full.clear()
self.cache_empty.set()
return transition_ba
def cache_transition_batch(self):
self.lock.acquire()
indexes = []
while len(indexes) != self.batch_size:
# index, is the index of state, and index + 1 of next_state
if self.filled:
index = random.randint(0, self.capacity - 2) # -2 because index +1 will be used for next state
# if index is in the space we are currently writing
if index >= self.current and index - self.buff_size <= self.current:
continue
else:
# can't start from 0 because get_state would loop back to the end -- wich is uninitialized.
# index +1 can be terminal
index = random.randint(self.buff_size -1, self.current -2)
if self.terminals[(index - self.buff_size):index].any():
continue
self.state_batch[len(indexes)] = self.get_state(index)
self.next_state_batch[len(indexes)] = self.get_state(index + 1)
indexes.append(index)
self.action_batch = self.actions[indexes]
self.reward_batch = self.rewards[indexes]
self.terminal_batch = self.terminals[indexes]
self.indexes = indexes
self.lock.release()
def cache_loop(self):
# until there is a reasonable number of samples
while self.current < self.batch_size:
time.sleep(0.5)
# start caching
while self.stop_caching == False:
self.cache_empty.wait()
self.cache_transition_batch()
self.cache_full.set()
self.cache_empty.clear()
def __del__(self):
self.stop_cache = True
self.cache_thread.join()