-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsampling_manager.py
More file actions
executable file
·280 lines (233 loc) · 10.6 KB
/
sampling_manager.py
File metadata and controls
executable file
·280 lines (233 loc) · 10.6 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import random
from replay_memory import ReplayMemory
import numpy as np
class SamplingManager:
def __init__(self, args, replay_memory):
self.replay_memory = replay_memory
self.batch_size = args.train_batch_size
self.history_len = args.screen_history
self.sampling_mode = args.prioritized_mode
self.alpha = args.sampling_alpha
self.beta = args.sampling_beta
self.sort_term = args.heap_sort_term
self.heap_index_list = [-1] * args.max_replay_memory # This list maps replay_index to heap_index
self.heap = [] # Binary heap
self.heap.append((None, None))
self.proportion_epsilon = 0.0000001
self.max_weight= 0
self.max_td = 1.0
self.segment_calculation_unit = 1000
self.segment_index = {} # heap indexes for each segment
self.add_call_no = 0
@property
def count(self):
return self.replay_memory.count
def add(self, action, reward, screen, terminal, td=None):
if td == None:
td = self.max_td
added_replay_index = self.replay_memory.add(action, reward, screen, terminal)
# If there was the same data then remove it first
heap_index = self.heap_index_list[added_replay_index]
if heap_index != -1:
self.remove(heap_index)
item = (added_replay_index, td)
self.heap.append(item)
child_index = len(self.heap) - 1
self.heap_index_list[added_replay_index] = child_index
self.reorder_upward(child_index)
self.add_call_no += 1
if self.add_call_no % self.sort_term == 0:
self.sort()
if self.add_call_no % (10**5) == 0: # Clear segment_index to calculate segment again
self.segment_index = {}
def remove(self, index):
last_index = len(self.heap) - 1
self.heap_index_list[self.heap[index][0]] = -1
if index == last_index:
self.heap.pop(last_index)
else:
self.heap[index] = self.heap[last_index]
self.heap_index_list[self.heap[index][0]] = index
self.heap.pop(last_index)
self.reorder(index)
def get_top(self):
return self.heap[1]
def get(self, index):
return self.heap[index]
def get_heap_length(self):
return len(self.heap) - 1
def swap(self, index1, item1, index2, item2):
self.heap[index1] = item1
self.heap[index2] = item2
self.heap_index_list[item1[0]] = index1
self.heap_index_list[item2[0]] = index2
def reorder(self, index, newValue=None):
if newValue != None:
self.heap[index] = (self.heap[index][0], newValue)
reordered = self.reorder_upward(index)
if reordered == False:
self.reorder_downward(index)
def reorder_upward(self, index):
reordered = False
child_index = index
while True:
item = self.heap[child_index]
parent_index = child_index / 2
parent_item = self.heap[parent_index]
if parent_index == 0 or item[1] <= parent_item[1]:
break
self.swap(parent_index, item, child_index, parent_item)
child_index = parent_index
reordered = True
return reordered
def reorder_downward(self, index):
parent_index = index
while True:
parent_item = self.heap[parent_index]
child_index1 = parent_index * 2
child_index2 = parent_index * 2 + 1
if child_index2 > len(self.heap) - 1:
if child_index1 <= len(self.heap) - 1 and self.heap[child_index1][1] > parent_item[1]:
self.swap(parent_index, self.heap[child_index1], child_index1, parent_item)
self.heap_index_list[self.heap[child_index1][0]] = parent_index
self.heap_index_list[parent_item[0]] = child_index1
parent_index = child_index1
else:
break
else:
if self.heap[child_index1][1] > parent_item[1] and self.heap[child_index1][1] >= self.heap[child_index2][1]:
self.swap(parent_index, self.heap[child_index1], child_index1, parent_item)
parent_index = child_index1
elif self.heap[child_index2][1] > parent_item[1] and self.heap[child_index2][1] >= self.heap[child_index1][1]:
self.swap(parent_index, self.heap[child_index2], child_index2, parent_item)
parent_index = child_index2
else:
break
def reorder_top(self, new_top_value):
top = self.get_top()
self.heap[1] = (top[0], new_top_value)
self.reorder_downward(1)
def sort(self):
new_heap = []
new_heap.append((None, None))
heap_size = len(self.heap)
#print 'heap_size : %s' % heap_size
for i in range(1, heap_size):
#print 'i : %s' % i
top = self.get_top()
new_heap.append(top)
last_index = heap_size - i
#print 'last_index : %s' % last_index
self.heap[1] = self.heap[last_index]
self.heap.pop(last_index)
if last_index > 1:
self.reorder_downward(1)
self.heap = new_heap
for i in range(1, heap_size):
self.heap_index_list[self.heap[i][0]] = i
self.segment_index = {}
def update_td(self, heap_index, td):
if td > self.max_td:
self.max_td = td
self.reorder(heap_index, td)
def get_segments(self):
data_len = len(self.heap) - 1
segment = data_len / self.segment_calculation_unit * self.segment_calculation_unit
if segment == 0: # If data len is less than necessary size then use uniform segments
return None
else:
if segment not in self.segment_index:
self.segment_index[segment] = self.calculate_segments(segment)
return self.segment_index[segment]
def get_p(self, heap_index):
if self.sampling_mode == 'RANK':
return (1.0 / heap_index) ** self.alpha
elif self.sampling_mode == 'PROPORTION':
return (abs(self.heap[heap_index][1]) + self.proportion_epsilon) ** self.alpha
def calculate_segments(self, data_len=None):
if data_len == None:
data_len = len(self.heap)
last_data_index = len(self.heap) - 1
self.total_psum = 0
for i in range(1, data_len):
self.total_psum += self.get_p(i)
if self.heap[i][1] == 0: # ignore data with 0 td
last_data_index = i - 1
break
segment = self.total_psum / self.batch_size
segment_sum = 0
segment_no = 1
segment_index = []
for i in range(1, data_len):
segment_sum += self.get_p(i)
if segment_sum >= segment * segment_no:
segment_index.append(i)
segment_no += 1
if len(segment_index) == self.batch_size - 1:
segment_index.append(last_data_index)
break
"""
for i in range(len(self.heap)):
print 'self.heap[%s] : %s, %s' % (i, self.heap[i][0], self.heap[i][1])
print 'segment_index : %s' % segment_index
"""
return segment_index
def get_minibatch(self):
segment_index = self.get_segments()
# sample random indexes
indexes = []
heap_indexes = []
weights = []
for segment in range(self.batch_size):
if segment_index == None:
index1 = 1
index2 = self.count
else:
if segment == 0:
index1 = 1
else:
index1 = segment_index[segment-1] + 1
index2 = segment_index[segment]
# find index
while True:
heap_index = random.randint(index1, index2)
replay_index = self.heap[heap_index][0]
repeat_again = False
if replay_index < self.history_len:
repeat_again = True
# if wraps over current pointer, then get new one
if replay_index >= self.replay_memory.current and replay_index - self.history_len < self.replay_memory.current:
repeat_again = True
# if wraps over episode end, then get new one
# NB! poststate (last screen) can be terminal state!
if self.replay_memory.terminals[(replay_index - self.history_len):replay_index].any():
repeat_again = True
if repeat_again:
self.reorder(heap_index, 0) # Discard and never use this data again
continue
if segment_index == None:
weight = 1.0
else:
weight = (self.total_psum / self.get_p(heap_index) / len(self.heap)) ** self.beta
if weight > self.max_weight:
self.max_weight = weight
weight = weight / self.max_weight
weights.append(weight)
break
# NB! having index first is fastest in C-order matrices
self.replay_memory.prestates[len(indexes), ...] = self.replay_memory.get_state(replay_index - 1)
self.replay_memory.poststates[len(indexes), ...] = self.replay_memory.get_state(replay_index)
indexes.append(replay_index)
heap_indexes.append(heap_index)
# copy actions, rewards and terminals with direct slicing
actions = self.replay_memory.actions[indexes]
rewards = self.replay_memory.rewards[indexes]
terminals = self.replay_memory.terminals[indexes]
return self.replay_memory.prestates, actions, rewards, self.replay_memory.poststates, terminals, indexes, heap_indexes, weights
@property
def history_buffer(self):
return self.replay_memory.history_buffer
def add_to_history_buffer(self, state):
self.replay_memory.add_to_history_buffer(state)
def clear_history_buffer(self):
self.replay_memory.clear_history_buffer()