-
Notifications
You must be signed in to change notification settings - Fork 1
/
replay_memory.py
250 lines (223 loc) · 12.2 KB
/
replay_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
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
import numpy as np
import torch as T
from numba import jit
np_load_old = np.load
# modify the default parameters of np.load
# np.load = lambda *a, **k: np_load_old(*a, allow_pickle=True, **k)
class ReplayBuffer(object):
def __init__(self, max_size, input_shape, n_actions, offline=False, dir=None, uniform=False):
self.mem_size = max_size
self.dir = dir
self.uniform = uniform
self.input_shape = input_shape
if offline == False:
self.state_memory = T.zeros((self.mem_size, input_shape), dtype=T.float32)
self.new_state_memory = T.zeros((self.mem_size, input_shape), dtype=T.float32)
self.action_memory = T.zeros(self.mem_size, dtype=T.int64)
self.new_action_memory = T.zeros(self.mem_size, dtype=T.int64)
self.reward_memory = T.zeros(self.mem_size, dtype=T.float32)
self.terminal_memory = T.zeros(self.mem_size, dtype=T.bool)
self.mem_cntr = 0
elif uniform == True:
# load offline data
print("load offline data Uniform!!!!!")
d = np.load(self.dir)
# data = dict([('state', []), ('action', []), ('reward', []), ('nstate', []), ('naction', []), ('done', [])])
self.state_memory = T.tensor((d.item().get('state')), dtype=T.float32)
self.new_state_memory = T.tensor((d.item().get('nstate')), dtype=T.float32)
self.action_memory = T.tensor((d.item().get('action')), dtype=T.int64)
self.reward_memory = T.tensor((d.item().get('reward')), dtype=T.float32)
self.terminal_memory = T.tensor((d.item().get('done')), dtype=T.bool)
self.mem_cntr = self.mem_size
else:
print("load offline data!!!!!")
d = np.load(self.dir)
# data = dict([('state', []), ('action', []), ('reward', []), ('nstate', []), ('naction', []), ('done', [])])
self.state_memory = T.zeros((self.mem_size, input_shape), dtype=T.float32)
self.new_state_memory = T.zeros((self.mem_size, input_shape), dtype=T.float32)
self.action_memory = T.zeros(self.mem_size, dtype=T.int64)
self.new_action_memory = T.zeros(self.mem_size, dtype=T.int64)
self.reward_memory = T.zeros(self.mem_size, dtype=T.float32)
self.terminal_memory = T.zeros(self.mem_size, dtype=T.bool)
temp = T.tensor((d.item().get('state')), dtype=T.float32)
self.state_memory[:len(temp), :] = T.tensor((d.item().get('state')), dtype=T.float32)
self.new_state_memory[:len(temp), :] = T.tensor((d.item().get('nstate')), dtype=T.float32)
self.action_memory[:len(temp)] = T.tensor((d.item().get('action')), dtype=T.int64)
self.new_action_memory[:len(temp)] = T.tensor((d.item().get('naction')), dtype=T.int64)
self.reward_memory[:len(temp)] = T.tensor((d.item().get('reward')), dtype=T.float32)
self.terminal_memory[:len(temp)] = T.tensor((d.item().get('done')), dtype=T.bool)
self.mem_cntr = len(temp)
# self.state_memory = self.state_memory[:self.mem_size]
# self.new_state_memory = self.new_state_memory[:self.mem_size]
# self.action_memory = self.action_memory[:self.mem_size]
# self.new_action_memory = self.new_action_memory[:self.mem_size]
# self.reward_memory = self.reward_memory[:self.mem_size]
# self.terminal_memory = self.terminal_memory[:self.mem_size]
# self.mem_cntr = self.mem_size
# @jit(target='cuda')
# @jit
def store_transition(self, state, action, reward, state_, done):
# print("stor!!!!!!!!!!!!!!!!!!!!!!!!")
index = self.mem_cntr % self.mem_size
self.state_memory[index] = T.tensor(state)
self.new_state_memory[index] = T.tensor(state_)
self.action_memory[index] = T.tensor(action)
self.reward_memory[index] = T.tensor(reward)
self.terminal_memory[index] = T.tensor(done)
self.mem_cntr += 1
# @jit(target='cuda')
# @jit
def store_transition_withnewaction(self, state, action, reward, state_, action_, done):
# print("stor!!!!!!!!!!!!!!!!!!!!!!!!")
index = self.mem_cntr % self.mem_size
self.state_memory[index] = T.tensor(state)
self.new_state_memory[index] = T.tensor(state_)
self.action_memory[index] = T.tensor(action)
self.new_action_memory[index] = T.tensor(action_)
self.reward_memory[index] = T.tensor(reward)
self.terminal_memory[index] = T.tensor(done)
self.mem_cntr += 1
# @jit(target='cuda')
# @jit
def sample_buffer(self, batch_size):
max_mem = min(self.mem_cntr, self.mem_size)
batch = np.random.choice(max_mem, batch_size, replace=False)
states = self.state_memory[batch]
actions = self.action_memory[batch]
rewards = self.reward_memory[batch]
states_ = self.new_state_memory[batch]
terminal = self.terminal_memory[batch]
return states, actions, rewards, states_, terminal
# @jit(target='cuda')
# @jit
def sample_buffer_nextaction(self, batch_size):
max_mem= min(self.mem_cntr, self.mem_size)
batch = np.random.choice(max_mem, batch_size, replace=False)
states = self.state_memory[batch]
actions = self.action_memory[batch]
rewards = self.reward_memory[batch]
states_ = self.new_state_memory[batch]
actions_ = self.new_action_memory[batch]
terminal = self.terminal_memory[batch]
return states, actions, rewards, states_, actions_, terminal
def sample_buffer_nextaction_givenindex(self, batch_size, itr, shuffle_index):
start_ind = itr* batch_size
batch = shuffle_index[start_ind: start_ind+batch_size]
states = self.state_memory[batch]
actions = self.action_memory[batch]
rewards = self.reward_memory[batch]
states_ = self.new_state_memory[batch]
actions_ = self.new_action_memory[batch]
terminal = self.terminal_memory[batch]
return states, actions, rewards, states_, actions_, terminal
# @jit(target='cuda')
# @jit
def sample_buffer_nextaction_consequtive(self, sequence_size):
max_mem= min(self.mem_cntr, self.mem_size)
endpoint = max( 1, (max_mem - sequence_size))
startpoint = np.random.choice(endpoint, 1, replace=False)
## for the last chunk only
# SP = endpoint
# EP = min((endpoint+sequence_size), max_mem)
## for general case
SP = np.int(startpoint)
EP = np.int(min((SP + sequence_size), max_mem))
states = self.state_memory[SP:EP]
actions = self.action_memory[SP:EP]
rewards = self.reward_memory[SP:EP]
states_ = self.new_state_memory[SP:EP]
actions_ = self.new_action_memory[SP:EP]
terminal = self.terminal_memory[SP:EP]
# states = self.state_memory[-max_mem:]
# actions = self.action_memory[-max_mem:]
# rewards = self.reward_memory[-max_mem:]
# states_ = self.new_state_memory[-max_mem:]
# actions_ = self.new_action_memory[-max_mem:]
# terminal = self.terminal_memory[-max_mem:]
return states, actions, rewards, states_, actions_, terminal
# @jit(target='cuda')
# @jit
def sample_buffer_nextaction_consequtive_chunk(self, sequence_size, chunk_num=1):
# print("chunk_num", chunk_num)
# print("sequence_size:", sequence_size)
max_mem= min(self.mem_cntr, self.mem_size)
max_mem_per_chunk = np.int(max_mem / chunk_num)
sequence_size_per_chunk = np.int(sequence_size / chunk_num)
mem_st =0
states = []
actions = []
rewards = []
states_ = []
actions_ = []
terminal = []
for i in range(chunk_num):
mem = mem_st + max_mem_per_chunk
endpoint = max( 1, (mem - sequence_size_per_chunk))
startpoint = np.random.randint(mem_st, endpoint) #np.random.choice([mem_st, endpoint], 1, replace=False)
## for the last chunk only
# SP = endpoint
# EP = min((endpoint+sequence_size), max_mem)
## for general case
SP = np.int(startpoint)
EP = np.int(min((SP + sequence_size_per_chunk), mem))
states.append(self.state_memory[SP:EP])
actions.append(self.action_memory[SP:EP])
rewards.append(self.reward_memory[SP:EP])
states_.append(self.new_state_memory[SP:EP])
actions_.append(self.new_action_memory[SP:EP])
terminal.append(self.terminal_memory[SP:EP])
mem_st = mem_st + max_mem_per_chunk
states = T.cat(states, dim=0)
actions = T.cat(actions, dim=0)
rewards = T.cat(rewards, dim=0)
states_ = T.cat(states_, dim=0)
actions_ = T.cat(actions_, dim=0)
terminal = T.cat(terminal, dim=0)
return states, actions, rewards, states_, actions_, terminal
def load_mem(self):
if self.uniform == True:
# load offline data
print("load offline data Uniform!!!!!")
d = np.load(self.dir)
# data = dict([('state', []), ('action', []), ('reward', []), ('nstate', []), ('naction', []), ('done', [])])
self.state_memory = T.tensor((d.item().get('state')), dtype=T.float32)
self.new_state_memory = T.tensor((d.item().get('nstate')), dtype=T.float32)
self.action_memory = T.tensor((d.item().get('action')), dtype=T.int64)
self.reward_memory = T.tensor((d.item().get('reward')), dtype=T.float32)
self.terminal_memory = T.tensor((d.item().get('done')), dtype=T.bool)
self.mem_cntr = self.mem_size
else:
# print("load offline data!!!!!")
# d = np.load(self.dir)
# # data = dict([('state', []), ('action', []), ('reward', []), ('nstate', []), ('naction', []), ('done', [])])
# self.state_memory = T.tensor((d.item().get('state')), dtype=T.float32)
# self.new_state_memory = T.tensor((d.item().get('nstate')), dtype=T.float32)
# self.action_memory = T.tensor((d.item().get('action')), dtype=T.int64)
# self.new_action_memory = T.tensor((d.item().get('naction')), dtype=T.int64)
# self.reward_memory = T.tensor((d.item().get('reward')), dtype=T.float32)
# self.terminal_memory = T.tensor((d.item().get('done')), dtype=T.bool)
# self.mem_cntr = self.mem_size
#
# self.state_memory = self.state_memory[:self.mem_size]
# self.new_state_memory = self.new_state_memory[:self.mem_size]
# self.action_memory = self.action_memory[:self.mem_size]
# self.new_action_memory = self.new_action_memory[:self.mem_size]
# self.reward_memory = self.reward_memory[:self.mem_size]
# self.terminal_memory = self.terminal_memory[:self.mem_size]
# self.mem_cntr = len(self.state_memory)-1
d = np.load(self.dir)
# data = dict([('state', []), ('action', []), ('reward', []), ('nstate', []), ('naction', []), ('done', [])])
self.state_memory = T.zeros((self.mem_size, self.input_shape), dtype=T.float32)
self.new_state_memory = T.zeros((self.mem_size, self.input_shape), dtype=T.float32)
self.action_memory = T.zeros(self.mem_size, dtype=T.int64)
self.new_action_memory = T.zeros(self.mem_size, dtype=T.int64)
self.reward_memory = T.zeros(self.mem_size, dtype=T.float32)
self.terminal_memory = T.zeros(self.mem_size, dtype=T.bool)
temp = T.tensor((d.item().get('state')), dtype=T.float32)
self.state_memory[:len(temp), :] = T.tensor((d.item().get('state')), dtype=T.float32)
self.new_state_memory[:len(temp), :] = T.tensor((d.item().get('nstate')), dtype=T.float32)
self.action_memory[:len(temp)] = T.tensor((d.item().get('action')), dtype=T.int64)
self.new_action_memory[:len(temp)] = T.tensor((d.item().get('naction')), dtype=T.int64)
self.reward_memory[:len(temp)] = T.tensor((d.item().get('reward')), dtype=T.float32)
self.terminal_memory[:len(temp)] = T.tensor((d.item().get('done')), dtype=T.bool)
self.mem_cntr = len(temp)