forked from johnjim0816/joyrl-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
319 lines (288 loc) · 13.8 KB
/
agent.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
# coding=utf-8
'''
@Author: John
@Email: [email protected]
@Date: 2020-06-09 20:25:52
@LastEditor: John
LastEditTime: 2023-04-16 21:50:41
@Discription:
@Environment: python 3.7.7
'''
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from common.memories import ReplayBufferQue
from common.optms import SharedAdam
class Actor(nn.Module):
def __init__(self, n_states, n_actions, hidden_dim, init_w=3e-3):
'''
actor 模型的结构定义
Args:
n_states (int): 输入状态的维度
n_actions (int): 可执行动作的数量
hidden_dim (int): 隐含层数量
init_w (float, optional): 均匀分布初始化权重的范围
'''
super(Actor, self).__init__()
self.linear1 = nn.Linear(n_states, hidden_dim)
self.linear2 = nn.Linear(hidden_dim, hidden_dim)
self.linear3 = nn.Linear(hidden_dim, n_actions)
self.linear3.weight.data.uniform_(-init_w, init_w)
self.linear3.bias.data.uniform_(-init_w, init_w)
def forward(self, x):
x = F.relu(self.linear1(x))
x = F.relu(self.linear2(x))
x = torch.tanh(self.linear3(x))
return x
class Critic(nn.Module):
def __init__(self, n_states, n_actions, hidden_dim, init_w=3e-3):
'''
critic 模型的结构定义
Args:
n_states (int): 输入状态的维度
n_actions (int): 可执行动作的数量
hidden_dim (int): 隐含层数量
init_w (float, optional): 均匀分布初始化权重的范围
'''
super(Critic, self).__init__()
self.linear1 = nn.Linear(n_states + n_actions, hidden_dim)
self.linear2 = nn.Linear(hidden_dim, hidden_dim)
self.linear3 = nn.Linear(hidden_dim, 1)
# 随机初始化为较小的值
self.linear3.weight.data.uniform_(-init_w, init_w)
self.linear3.bias.data.uniform_(-init_w, init_w)
def forward(self, state, action):
# 按维数1拼接
x = torch.cat([state, action], 1)
x = F.relu(self.linear1(x))
x = F.relu(self.linear2(x))
x = self.linear3(x)
return x
class OUNoise(object):
'''
构造 Ornstein–Uhlenbeck 噪声的类
https://djalil.chafai.net/docs/M2/history-brownian-motion/Uhlenbeck%20&%20Ornstein%20-%201930.pdf
'''
def __init__(self, action_space, mu=0.0, theta=0.15, max_sigma=0.3, min_sigma=0.3, decay_period=100000):
'''
初始化输入参数
Args:
action_space (gym.spaces.box.Box): env 中的 action_space
mu (float, optional): 噪声均值
theta (float, optional): 系统对噪声的扰动程度,theta 越大,噪声扰动越小
max_sigma (float, optional): 最大 sigma,用于更新衰变 sigma 值
min_sigma (float, optional): 最小 sigma,用于更新衰变 sigma 值
decay_period (int, optional): 衰变周期
'''
self.mu = mu
self.theta = theta
self.sigma = max_sigma
self.max_sigma = max_sigma
self.min_sigma = min_sigma
self.decay_period = decay_period
self.n_actions = action_space.shape[0] # env环境中可执行动作的数量,因是连续动作,所以一般为1
self.low = action_space.low # env环境中动作取值的最小值
self.high = action_space.high # env环境中动作取值的最大值
self.reset()
def reset(self):
'''
重置噪声
'''
self.obs = np.ones(self.n_actions) * self.mu # reset the noise
def evolve_obs(self):
'''
更新噪声
Returns:
返回更新后的噪声值
'''
x = self.obs
dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(self.n_actions) # Ornstein–Uhlenbeck process
self.obs = x + dx
return self.obs
def get_action(self, action, t=0):
'''
根据输入的动作,输出加入 OU 噪声后的动作
Args:
action (np.ndarray[float]): 输入的动作值
t (int, optional): 当前环境已执行的帧数
Returns:
action (np.ndarray[float]): 返回加入 OU 噪声后的动作
'''
ou_obs = self.evolve_obs()
## 根据env进程(t),通过设定的衰变周期(decay_period),进行更新衰变的sigma值
self.sigma = self.max_sigma - (self.max_sigma - self.min_sigma) * min(1.0, t / self.decay_period)
return np.clip(action + ou_obs, self.low, self.high) # add noise to action
class Agent:
def __init__(self, cfg, is_share_agent = False):
'''
构建智能体
Args:
cfg (class): 超参数类 AlgoConfig
'''
self.n_states = cfg.n_states
self.n_actions = cfg.n_actions
self.action_space = cfg.action_space # env 中的 action_space
self.ou_noise = OUNoise(self.action_space) # 实例化 构造 Ornstein–Uhlenbeck 噪声的类
self.batch_size = cfg.batch_size
self.gamma = cfg.gamma
self.tau = cfg.tau
self.sample_count = 0 # 记录采样动作的次数
self.update_flag = False # 标记是否更新网络
self.device = torch.device(cfg.device)
self.critic = Critic(self.n_states, self.n_actions, hidden_dim=cfg.critic_hidden_dim).to(self.device)
self.target_critic = Critic(self.n_states, self.n_actions, hidden_dim=cfg.critic_hidden_dim).to(self.device)
self.actor = Actor(self.n_states, self.n_actions, hidden_dim=cfg.actor_hidden_dim).to(self.device)
self.target_actor = Actor(self.n_states, self.n_actions, hidden_dim=cfg.actor_hidden_dim).to(self.device).to(
self.device)
## 将 critc 网络的参数赋值给target critic 网络
for target_param, param in zip(self.target_critic.parameters(), self.critic.parameters()):
target_param.data.copy_(param.data)
## 将 actor 网络的参数赋值给target actor 网络
for target_param, param in zip(self.target_actor.parameters(), self.actor.parameters()):
target_param.data.copy_(param.data)
self.critic_optimizer = optim.Adam(self.critic.parameters(), lr=cfg.critic_lr)
self.actor_optimizer = optim.Adam(self.actor.parameters(), lr=cfg.actor_lr)
self.memory = ReplayBufferQue(cfg.buffer_size)
if is_share_agent:
self.actor.share_memory()
self.actor_optimizer = SharedAdam(self.actor.parameters(), lr=cfg.actor_lr)
self.actor_optimizer.share_memory()
self.critic.share_memory()
self.critic_optimizer = SharedAdam(self.critic.parameters(), lr=cfg.critic_lr)
self.critic_optimizer.share_memory()
def sample_action(self, state):
'''
根据输入的状态采样动作
Args:
state (np.ndarray): 输入的状态
Returns:
action (np.ndarray[float]): 根据状态采样后的动作
'''
self.sample_count += 1
state = torch.tensor(state, device=self.device, dtype=torch.float32).unsqueeze(dim=0)
action_tanh = self.actor(state) # action_tanh is in [-1, 1]
# convert action_tanh to action in the original action space
action_scale = torch.FloatTensor((self.action_space.high - self.action_space.low) / 2.).to(self.device)
action_bias = torch.FloatTensor((self.action_space.high + self.action_space.low) / 2.).to(self.device)
action = action_scale * action_tanh + action_bias
action = action.cpu().detach().numpy()[0]
# add noise to action
action = self.ou_noise.get_action(action, self.sample_count)
return action
@torch.no_grad()
def predict_action(self, state):
'''
根据输入的状态预测下一步的动作
Args:
state (np.ndarray): 输入的状态
Returns:
action (np.ndarray[float]): 根据状态采样后的动作
'''
state = torch.tensor(state, device=self.device, dtype=torch.float32).unsqueeze(dim=0)
action_tanh = self.actor(state) # action_tanh is in [-1, 1]
# convert action_tanh to action in the original action space
action_scale = torch.FloatTensor((self.action_space.high - self.action_space.low) / 2.).to(self.device)
action_bias = torch.FloatTensor((self.action_space.high + self.action_space.low) / 2.).to(self.device)
action = action_scale * action_tanh + action_bias
action = action.cpu().detach().numpy()[0]
return action
def update(self, share_agent=None):
## 当经验回放池中的数量小于 batch_size 时,直接返回不更新
if len(self.memory) < self.batch_size: # when memory size is less than batch size, return
return
else:
if not self.update_flag:
print("Begin to update!")
self.update_flag = True
## 从经验回放池中采样 batch_size 个样本
state, action, reward, next_state, done = self.memory.sample(self.batch_size)
## 将状态、动作等 array 转为 tensor
state = torch.FloatTensor(np.array(state)).to(self.device)
next_state = torch.FloatTensor(np.array(next_state)).to(self.device)
action = torch.FloatTensor(np.array(action)).to(self.device)
reward = torch.FloatTensor(reward).unsqueeze(1).to(self.device)
done = torch.FloatTensor(np.float32(done)).unsqueeze(1).to(self.device)
## 输入状态及通过 actor 网络 根据该状态输出的动作,计算 critic 网络输出的价值,类似于DQN中的q-value
policy_loss = self.critic(state, self.actor(state))
## 计算均值作为 critic 网络的损失
policy_loss = -policy_loss.mean()
## 根据下一个 time step 的状态用 target_actor 网络输出目标动作
next_action = self.target_actor(next_state)
## 输入下一个 time step 的状态及目标动作,计算 target_critc 网络输出的目标价值
target_value = self.target_critic(next_state, next_action.detach())
## 根据真实奖励更新目标价值
expected_value = reward + (1.0 - done) * self.gamma * target_value
expected_value = torch.clamp(expected_value, -np.inf, np.inf)
## 输入状态和动作,用 critic 网络计算预估的价值
value = self.critic(state, action)
## 将 critic 网络输出的价值和 target_critic输出并更新后的价值通过MSE进行损失计算
value_loss = nn.MSELoss()(value, expected_value.detach())
if share_agent is not None:
share_agent.actor_optimizer.zero_grad()
share_agent.critic_optimizer.zero_grad()
# self.actor_optimizer.zero_grad()
# self.critic_optimizer.zero_grad()
policy_loss.backward()
value_loss.backward()
for param, share_param in zip(self.actor.parameters(), share_agent.actor.parameters()):
share_param._grad = param.grad
for param, share_param in zip(self.critic.parameters(), share_agent.critic.parameters()):
share_param._grad = param.grad
share_agent.actor_optimizer.step()
share_agent.critic_optimizer.step()
self.actor.load_state_dict(share_agent.actor.state_dict())
self.critic.load_state_dict(share_agent.critic.state_dict())
## 通过软更新的方法,缓慢更新 target critic 网络的参数
for target_param, param in zip(self.target_critic.parameters(), self.critic.parameters()):
target_param.data.copy_(
target_param.data * (1.0 - self.tau) +
param.data * self.tau
)
## 通过软更新的方法,缓慢更新 target actor 网络的参数
for target_param, param in zip(self.target_actor.parameters(), self.actor.parameters()):
target_param.data.copy_(
target_param.data * (1.0 - self.tau) +
param.data * self.tau
)
else :
## 更新 actor 网络参数
self.actor_optimizer.zero_grad()
policy_loss.backward()
self.actor_optimizer.step()
## 更新 critic 网络参数
self.critic_optimizer.zero_grad()
value_loss.backward()
self.critic_optimizer.step()
## 通过软更新的方法,缓慢更新 target critic 网络的参数
for target_param, param in zip(self.target_critic.parameters(), self.critic.parameters()):
target_param.data.copy_(
target_param.data * (1.0 - self.tau) +
param.data * self.tau
)
## 通过软更新的方法,缓慢更新 target actor 网络的参数
for target_param, param in zip(self.target_actor.parameters(), self.actor.parameters()):
target_param.data.copy_(
target_param.data * (1.0 - self.tau) +
param.data * self.tau
)
def save_model(self, fpath):
'''
保存模型
Args:
fpath (str): 模型存放路径
'''
from pathlib import Path
# create path
Path(fpath).mkdir(parents=True, exist_ok=True)
torch.save(self.actor.state_dict(), f"{fpath}/actor_checkpoint.pt")
def load_model(self, fpath):
'''
根据模型路径导入模型
Args:
fpath (str): 模型路径
'''
actor_ckpt = torch.load(f"{fpath}/actor_checkpoint.pt", map_location=self.device)
self.actor.load_state_dict(actor_ckpt)