-
Notifications
You must be signed in to change notification settings - Fork 2
/
Agent.py
239 lines (213 loc) · 8.76 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
import torch
import numpy as np
from typing import Union
from copy import deepcopy
from ppo import *
class AgentPPO:
def __init__(self):
super().__init__()
self.ratio_clip = 0.2 # ratio.clamp(1 - clip, 1 + clip)
self.lambda_entropy = 0.02 # could be 0.02
# could be 0.95~0.99, GAE (Generalized Advantage Estimation. ICLR.2016.)
self.lambda_gae_adv = 0.98
self.get_reward_sum = None
self.state = None
self.device = None
self.criterion = None
self.act = self.act_optimizer = None
self.cri = self.cri_optimizer = self.cri_target = None
def init(self, net_dim, state_dim, action_dim, learning_rate=1e-4, if_use_gae=False):
self.device = torch.device("cpu")
if torch.cuda.is_available():
self.device = torch.device("cuda")
self.get_reward_sum = self.get_reward_sum_gae if if_use_gae else self.get_reward_sum_raw
self.act = ActorPPO(net_dim, state_dim, action_dim).to(self.device)
self.cri = CriticAdv(net_dim, state_dim).to(self.device)
self.cri_target = deepcopy(self.cri) if self.cri_target else self.cri
self.criterion = torch.nn.SmoothL1Loss()
self.act_optimizer = torch.optim.Adam(
self.act.parameters(),
lr=learning_rate
)
self.cri_optimizer = torch.optim.Adam(
self.cri.parameters(),
lr=learning_rate
)
def select_action(self, state):
states = torch.as_tensor(
(state,),
dtype=torch.float32,
device=self.device
)
# plan to be get_action_a_noise
actions, noises = self.act.get_action(states)
return actions[0].cpu().detach().numpy(), noises[0].cpu().detach().numpy()
def explore_env(self, env, target_step, reward_scale, gamma):
trajectory_list = list()
state = self.state
for _ in range(target_step):
action, noise = self.select_action(state)
next_state, reward, done, _ = env.step(np.tanh(action))
other = (
reward * reward_scale,
0.0 if done else gamma,
*action,
*noise
)
trajectory_list.append((state, other))
state = env.reset() if done else next_state
self.state = state
return trajectory_list
def update_net(self, buffer, batch_size, repeat_times, soft_update_tau):
buffer.update_now_len()
buf_len = buffer.now_len
buf_state, buf_action, buf_r_sum, buf_logprob, buf_advantage = self.prepare_buffer(
buffer
)
buffer.empty_buffer()
'''PPO: Surrogate objective of Trust Region'''
obj_critic = obj_actor = logprob = None
for _ in range(int(buf_len / batch_size * repeat_times)):
indices = torch.randint(
buf_len,
size=(batch_size,),
requires_grad=False,
device=self.device
)
state = buf_state[indices]
action = buf_action[indices]
r_sum = buf_r_sum[indices]
logprob = buf_logprob[indices]
advantage = buf_advantage[indices]
new_logprob, obj_entropy = self.act.get_logprob_entropy(
state,
action
) # it is obj_actor
ratio = (new_logprob - logprob.detach()).exp()
surrogate1 = advantage * ratio
surrogate2 = advantage * \
ratio.clamp(1 - self.ratio_clip, 1 + self.ratio_clip)
obj_surrogate = -torch.min(surrogate1, surrogate2).mean()
obj_actor = obj_surrogate + obj_entropy * self.lambda_entropy
self.optim_update(self.act_optimizer, obj_actor)
# critic network predicts the reward_sum (Q value) of state
value = self.cri(state).squeeze(1)
obj_critic = self.criterion(value, r_sum) / (r_sum.std() + 1e-6)
self.optim_update(self.cri_optimizer, obj_critic)
self.soft_update(
self.cri_target,
self.cri,
soft_update_tau
) if self.cri_target is not self.cri else None
# logging_tuple
return obj_critic.item(), obj_actor.item(), logprob.mean().item()
def prepare_buffer(self, buffer):
buf_len = buffer.now_len
with torch.no_grad(): # compute reverse reward
reward, mask, action, a_noise, state = buffer.sample_all()
bs = 2 ** 10 # set a smaller 'BatchSize' when out of GPU memory.
value = torch.cat(
[self.cri_target(state[i:i + bs])
for i in range(0, state.size(0), bs)],
dim=0
)
logprob = self.act.get_old_logprob(action, a_noise)
pre_state = torch.as_tensor(
(self.state,),
dtype=torch.float32,
device=self.device
)
pre_r_sum = self.cri(pre_state).detach()
r_sum, advantage = self.get_reward_sum(
self,
buf_len,
reward,
mask,
value,
pre_r_sum
)
return state, action, r_sum, logprob, advantage
@staticmethod
def get_reward_sum_raw(self, buf_len, buf_reward, buf_mask, buf_value, pre_r_sum) -> Union[torch.Tensor, torch.Tensor]:
buf_r_sum = torch.empty(
buf_len,
dtype=torch.float32,
device=self.device
) # reward sum
for i in range(buf_len - 1, -1, -1):
buf_r_sum[i] = buf_reward[i] + buf_mask[i] * pre_r_sum
pre_r_sum = buf_r_sum[i]
buf_advantage = buf_r_sum - (buf_mask * buf_value.squeeze(1))
buf_advantage = (buf_advantage - buf_advantage.mean()
) / (buf_advantage.std() + 1e-5)
return buf_r_sum, buf_advantage
@staticmethod
def get_reward_sum_gae(self, buf_len, buf_reward, buf_mask, buf_value, pre_r_sum) -> Union[torch.Tensor, torch.Tensor]:
buf_r_sum = torch.empty(
buf_len,
dtype=torch.float32,
device=self.device
) # old policy value
buf_advantage = torch.empty(
buf_len,
dtype=torch.float32,
device=self.device
) # advantage value
pre_advantage = 0 # advantage value of previous step
for i in range(buf_len - 1, -1, -1):
buf_r_sum[i] = buf_reward[i] + buf_mask[i] * pre_r_sum
pre_r_sum = buf_r_sum[i]
buf_advantage[i] = buf_reward[i] + buf_mask[i] * \
(pre_advantage - buf_value[i]) # fix a bug here
pre_advantage = buf_value[i] + \
buf_advantage[i] * self.lambda_gae_adv
buf_advantage = (buf_advantage - buf_advantage.mean()
) / (buf_advantage.std() + 1e-5)
return buf_r_sum, buf_advantage
@staticmethod
def optim_update(optimizer, objective):
optimizer.zero_grad()
objective.backward()
optimizer.step()
@staticmethod
def soft_update(target_net, current_net, tau):
for tar, cur in zip(target_net.parameters(), current_net.parameters()):
tar.data.copy_(cur.data.__mul__(tau) + tar.data.__mul__(1 - tau))
class AgentDiscretePPO(AgentPPO):
def init(self, net_dim, state_dim, action_dim, learning_rate=1e-4, if_use_gae=False):
self.device = torch.device("cpu")
if torch.cuda.is_available():
self.device = torch.device("cuda")
self.get_reward_sum = self.get_reward_sum_gae if if_use_gae else self.get_reward_sum_raw
self.act = ActorDiscretePPO(
net_dim,
state_dim,
action_dim
).to(self.device)
self.cri = CriticAdv(net_dim, state_dim).to(self.device)
self.cri_target = deepcopy(self.cri) if self.cri_target else self.cri
self.criterion = torch.nn.SmoothL1Loss()
self.act_optimizer = torch.optim.Adam(
self.act.parameters(),
lr=learning_rate
)
self.cri_optimizer = torch.optim.Adam(
self.cri.parameters(),
lr=learning_rate
)
def explore_env(self, env, target_step, reward_scale, gamma):
trajectory_list = list()
state = self.state
for _ in range(target_step):
a_int, a_prob = self.select_action(state)
next_state, reward, done, _ = env.step(int(a_int))
other = (
reward * reward_scale,
0.0 if done else gamma,
a_int,
*a_prob
)
trajectory_list.append((state, other))
state = env.reset() if done else next_state
self.state = state
return trajectory_list