-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay_buffer.py
206 lines (180 loc) · 10.8 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
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
import torch
import numpy as np
from collections import deque
from sum_tree import SumTree
class ReplayBuffer(object):
def __init__(self, args):
self.batch_size = args.batch_size
self.buffer_capacity = args.buffer_capacity
self.current_size = 0
self.count = 0
self.buffer = {'state': np.zeros((self.buffer_capacity, args.state_dim)),
'action': np.zeros((self.buffer_capacity, 1)),
'reward': np.zeros(self.buffer_capacity),
'next_state': np.zeros((self.buffer_capacity, args.state_dim)),
'terminal': np.zeros(self.buffer_capacity),
}
def store_transition(self, state, action, reward, next_state, terminal, done):
self.buffer['state'][self.count] = state
self.buffer['action'][self.count] = action
self.buffer['reward'][self.count] = reward
self.buffer['next_state'][self.count] = next_state
self.buffer['terminal'][self.count] = terminal
self.count = (self.count + 1) % self.buffer_capacity # When the 'count' reaches buffer_capacity, it will be reset to 0.
self.current_size = min(self.current_size + 1, self.buffer_capacity)
def sample(self, total_steps):
index = np.random.randint(0, self.current_size, size=self.batch_size)
batch = {}
for key in self.buffer.keys(): # numpy->tensor
if key == 'action':
batch[key] = torch.tensor(self.buffer[key][index], dtype=torch.long)
else:
batch[key] = torch.tensor(self.buffer[key][index], dtype=torch.float32)
return batch, None, None
class N_Steps_ReplayBuffer(object):
def __init__(self, args):
self.gamma = args.gamma
self.batch_size = args.batch_size
self.buffer_capacity = args.buffer_capacity
self.current_size = 0
self.count = 0
self.n_steps = args.n_steps
self.n_steps_deque = deque(maxlen=self.n_steps)
self.buffer = {'state': np.zeros((self.buffer_capacity, args.state_dim)),
'action': np.zeros((self.buffer_capacity, 1)),
'reward': np.zeros(self.buffer_capacity),
'next_state': np.zeros((self.buffer_capacity, args.state_dim)),
'terminal': np.zeros(self.buffer_capacity),
}
def store_transition(self, state, action, reward, next_state, terminal, done):
transition = (state, action, reward, next_state, terminal, done)
self.n_steps_deque.append(transition)
if len(self.n_steps_deque) == self.n_steps:
state, action, n_steps_reward, next_state, terminal = self.get_n_steps_transition()
self.buffer['state'][self.count] = state
self.buffer['action'][self.count] = action
self.buffer['reward'][self.count] = n_steps_reward
self.buffer['next_state'][self.count] = next_state
self.buffer['terminal'][self.count] = terminal
self.count = (self.count + 1) % self.buffer_capacity # When the 'count' reaches buffer_capacity, it will be reset to 0.
self.current_size = min(self.current_size + 1, self.buffer_capacity)
def get_n_steps_transition(self):
state, action = self.n_steps_deque[0][:2]
next_state, terminal = self.n_steps_deque[-1][3:5]
n_steps_reward = 0
for i in reversed(range(self.n_steps)):
r, s_, ter, d = self.n_steps_deque[i][2:]
n_steps_reward = r + self.gamma * (1 - d) * n_steps_reward
if d:
next_state, terminal = s_, ter
return state, action, n_steps_reward, next_state, terminal
def sample(self, total_steps):
index = np.random.randint(0, self.current_size, size=self.batch_size)
batch = {}
for key in self.buffer.keys(): # numpy->tensor
if key == 'action':
batch[key] = torch.tensor(self.buffer[key][index], dtype=torch.long)
else:
batch[key] = torch.tensor(self.buffer[key][index], dtype=torch.float32)
return batch, None, None
class Prioritized_ReplayBuffer(object):
def __init__(self, args):
self.max_train_steps = args.max_train_steps
self.alpha = args.alpha
self.beta_init = args.beta_init
self.beta = args.beta_init
self.batch_size = args.batch_size
self.buffer_capacity = args.buffer_capacity
self.sum_tree = SumTree(self.buffer_capacity)
self.current_size = 0
self.count = 0
self.buffer = {'state': np.zeros((self.buffer_capacity, args.state_dim)),
'action': np.zeros((self.buffer_capacity, 1)),
'reward': np.zeros(self.buffer_capacity),
'next_state': np.zeros((self.buffer_capacity, args.state_dim)),
'terminal': np.zeros(self.buffer_capacity),
}
def store_transition(self, state, action, reward, next_state, terminal, done):
self.buffer['state'][self.count] = state
self.buffer['action'][self.count] = action
self.buffer['reward'][self.count] = reward
self.buffer['next_state'][self.count] = next_state
self.buffer['terminal'][self.count] = terminal
# 如果是第一条经验,初始化优先级为1.0;否则,对于新存入的经验,指定为当前最大的优先级
priority = 1.0 if self.current_size == 0 else self.sum_tree.priority_max
self.sum_tree.update(data_index=self.count, priority=priority) # 更新当前经验在sum_tree中的优先级
self.count = (self.count + 1) % self.buffer_capacity # When the 'count' reaches buffer_capacity, it will be reset to 0.
self.current_size = min(self.current_size + 1, self.buffer_capacity)
def sample(self, total_steps):
batch_index, IS_weight = self.sum_tree.get_batch_index(current_size=self.current_size, batch_size=self.batch_size, beta=self.beta)
self.beta = self.beta_init + (1 - self.beta_init) * (total_steps / self.max_train_steps) # beta:beta_init->1.0
batch = {}
for key in self.buffer.keys(): # numpy->tensor
if key == 'action':
batch[key] = torch.tensor(self.buffer[key][batch_index], dtype=torch.long)
else:
batch[key] = torch.tensor(self.buffer[key][batch_index], dtype=torch.float32)
return batch, batch_index, IS_weight
def update_batch_priorities(self, batch_index, td_errors): # 根据传入的td_error,更新batch_index所对应数据的priorities
priorities = (np.abs(td_errors) + 0.01) ** self.alpha
for index, priority in zip(batch_index, priorities):
self.sum_tree.update(data_index=index, priority=priority)
class N_Steps_Prioritized_ReplayBuffer(object):
def __init__(self, args):
self.max_train_steps = args.max_train_steps
self.alpha = args.alpha
self.beta_init = args.beta_init
self.beta = args.beta_init
self.gamma = args.gamma
self.batch_size = args.batch_size
self.buffer_capacity = args.buffer_capacity
self.sum_tree = SumTree(self.buffer_capacity)
self.n_steps = args.n_steps
self.n_steps_deque = deque(maxlen=self.n_steps)
self.buffer = {'state': np.zeros((self.buffer_capacity, args.state_dim)),
'action': np.zeros((self.buffer_capacity, 1)),
'reward': np.zeros(self.buffer_capacity),
'next_state': np.zeros((self.buffer_capacity, args.state_dim)),
'terminal': np.zeros(self.buffer_capacity),
}
self.current_size = 0
self.count = 0
def store_transition(self, state, action, reward, next_state, terminal, done):
transition = (state, action, reward, next_state, terminal, done)
self.n_steps_deque.append(transition)
if len(self.n_steps_deque) == self.n_steps:
state, action, n_steps_reward, next_state, terminal = self.get_n_steps_transition()
self.buffer['state'][self.count] = state
self.buffer['action'][self.count] = action
self.buffer['reward'][self.count] = n_steps_reward
self.buffer['next_state'][self.count] = next_state
self.buffer['terminal'][self.count] = terminal
# 如果是buffer中的第一条经验,那么指定priority为1.0;否则对于新存入的经验,指定为当前最大的priority
priority = 1.0 if self.current_size == 0 else self.sum_tree.priority_max
self.sum_tree.update(data_index=self.count, priority=priority) # 更新当前经验在sum_tree中的优先级
self.count = (self.count + 1) % self.buffer_capacity # When 'count' reaches buffer_capacity, it will be reset to 0.
self.current_size = min(self.current_size + 1, self.buffer_capacity)
def sample(self, total_steps):
batch_index, IS_weight = self.sum_tree.get_batch_index(current_size=self.current_size, batch_size=self.batch_size, beta=self.beta)
self.beta = self.beta_init + (1 - self.beta_init) * (total_steps / self.max_train_steps) # beta:beta_init->1.0
batch = {}
for key in self.buffer.keys(): # numpy->tensor
if key == 'action':
batch[key] = torch.tensor(self.buffer[key][batch_index], dtype=torch.long)
else:
batch[key] = torch.tensor(self.buffer[key][batch_index], dtype=torch.float32)
return batch, batch_index, IS_weight
def get_n_steps_transition(self):
state, action = self.n_steps_deque[0][:2] # 获取deque中第一个transition的s和a
next_state, terminal = self.n_steps_deque[-1][3:5] # 获取deque中最后一个transition的s'和terminal
n_steps_reward = 0
for i in reversed(range(self.n_steps)): # 逆序计算n_steps_reward
r, s_, ter, d = self.n_steps_deque[i][2:]
n_steps_reward = r + self.gamma * (1 - d) * n_steps_reward
if d: # 如果done=True,说明一个回合结束,保存deque中当前这个transition的s'和terminal作为这个n_steps_transition的next_state和terminal
next_state, terminal = s_, ter
return state, action, n_steps_reward, next_state, terminal
def update_batch_priorities(self, batch_index, td_errors): # 根据传入的td_error,更新batch_index所对应数据的priorities
priorities = (np.abs(td_errors) + 0.01) ** self.alpha
for index, priority in zip(batch_index, priorities):
self.sum_tree.update(data_index=index, priority=priority)