-
Notifications
You must be signed in to change notification settings - Fork 0
/
symphony.py
302 lines (213 loc) · 10.1 KB
/
symphony.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
#This is the second version of Symphony algorithm.
#The learning speed is slowed down by approx 20% to learn (Bellemare et al) and increase (something new) Q variance.
#Advantage in Q Variance's empirically has gradient with relation to actions.
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
import copy
import math
# random seeds
r1, r2, r3 = 830143436, 167430301, 2193498338 #r = random.randint(0,2**32-1)
print(r1, ", ", r2, ", ", r3)
torch.manual_seed(r1)
np.random.seed(r2)
#Rectified Hubber Error Loss Function
def ReHE(error):
ae = torch.abs(error).mean()
return ae*torch.tanh(ae)
#Rectified Hubber Assymetric Error Loss Function
def ReHaE(error):
e = error.mean()
return torch.abs(e)*torch.tanh(e)
class ReSine(nn.Module):
def forward(self, x):
return F.leaky_relu(torch.sin(x), 0.1)
class FourierSeries(nn.Module):
def __init__(self, hidden_dim, f_out):
super().__init__()
self.fft = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
ReSine(),
nn.Linear(hidden_dim, f_out)
)
def forward(self, x):
return self.fft(x)
# Define the actor network
class Actor(nn.Module):
def __init__(self, state_dim, action_dim, device, hidden_dim=32, max_action=1.0, burst=False, tr_noise=True):
super(Actor, self).__init__()
self.input = nn.Sequential(
nn.Linear(state_dim, hidden_dim),
nn.LayerNorm(hidden_dim)
)
self.net = nn.Sequential(
FourierSeries(hidden_dim, action_dim),
nn.Tanh()
)
self.max_action = torch.mean(max_action).item()
self.noise = GANoise(max_action, tr_noise, burst)
#self.noise = OUNoise(action_dim, device)
def forward(self, state, mean=False):
x = self.input(state)
x = self.max_action*self.net(x)
if mean: return x
x += self.noise.generate(x)
return x.clamp(-self.max_action, self.max_action)
# Define the critic network
class Critic(nn.Module):
def __init__(self, state_dim, action_dim, hidden_dim=32):
super(Critic, self).__init__()
self.input = nn.Sequential(
nn.Linear(state_dim+action_dim, hidden_dim),
nn.LayerNorm(hidden_dim)
)
qA = FourierSeries(hidden_dim, 1)
qB = FourierSeries(hidden_dim, 1)
qC = FourierSeries(hidden_dim, 1)
s2 = FourierSeries(hidden_dim, 1)
self.nets = nn.ModuleList([qA, qB, qC, s2])
def forward(self, state, action, united=False):
x = torch.cat([state, action], -1)
x = self.input(x)
xs = [net(x) for net in self.nets]
if not united: return xs
stack = torch.stack(xs[:3], dim=-1)
return torch.min(stack, dim=-1).values, xs[3]
# Define the actor-critic agent
class Symphony(object):
def __init__(self, state_dim, action_dim, hidden_dim, device, max_action=1.0, burst=False, tr_noise=True):
self.actor = Actor(state_dim, action_dim, device, hidden_dim, max_action, burst, tr_noise).to(device)
self.critic = Critic(state_dim, action_dim, hidden_dim).to(device)
self.critic_target = copy.deepcopy(self.critic)
self.actor_optimizer = optim.Adam(self.actor.parameters(), lr=3e-4)
self.critic_optimizer = optim.Adam(self.critic.parameters(), lr=7e-4)
self.max_action = max_action
self.device = device
self.state_dim = state_dim
self.action_dim = action_dim
self.q_old_policy = 0.0
self.s2_old_policy = 0.0
def select_action(self, state, mean=False):
with torch.no_grad():
state = torch.FloatTensor(state).reshape(-1,self.state_dim).to(self.device)
action = self.actor(state, mean=mean)
return action.cpu().data.numpy().flatten()
def train(self, batch):
state, action, reward, next_state, done = batch
self.critic_update(state, action, reward, next_state, done)
return self.actor_update(state)
def critic_update(self, state, action, reward, next_state, done):
with torch.no_grad():
for target_param, param in zip(self.critic_target.parameters(), self.critic.parameters()):
target_param.data.copy_(0.997*target_param.data + 0.003*param)
next_action = self.actor(next_state, mean=True)
q_next_target, s2_next_target = self.critic_target(next_state, next_action, united=True)
q_value = reward + (1-done) * 0.99 * q_next_target
s2_value = 3e-3 * (3e-3 * torch.var(reward) + (1-done) * 0.99 * s2_next_target) #reduced objective to learn Bellman's sum of dumped variance
out = self.critic(state, action, united=False)
critic_loss = ReHE(q_value - out[0]) + ReHE(q_value - out[1]) + ReHE(q_value - out[2]) + ReHE(s2_value - out[3])
self.critic_optimizer.zero_grad()
critic_loss.backward()
self.critic_optimizer.step()
def actor_update(self, state):
action = self.actor(state, mean=True)
q_new_policy, s2_new_policy = self.critic(state, action, united=True)
actor_loss = -ReHaE(q_new_policy - self.q_old_policy) -ReHaE(s2_new_policy - self.s2_old_policy)
with torch.no_grad():
self.q_old_policy = q_new_policy.mean().detach()
self.s2_old_policy = s2_new_policy.mean().detach()
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
return actor_loss
class ReplayBuffer:
def __init__(self, state_dim, action_dim, capacity, device, fade_factor=7.0, stall_penalty=0.03):
capacity_dict = {"short": 100000, "medium": 300000, "full": 500000}
self.capacity, self.length, self.device = capacity_dict[capacity], 0, device
self.batch_size = min(max(128, self.length//500), 1024) #in order for sample to describe population
self.random = np.random.default_rng()
self.indices, self.indexes, self.probs, self.step = [], np.array([]), np.array([]), 0
self.fade_factor = fade_factor
self.stall_penalty = stall_penalty
self.states = torch.zeros((self.capacity, state_dim), dtype=torch.float32).to(device)
self.actions = torch.zeros((self.capacity, action_dim), dtype=torch.float32).to(device)
self.rewards = torch.zeros((self.capacity, 1), dtype=torch.float32).to(device)
self.next_states = torch.zeros((self.capacity, state_dim), dtype=torch.float32).to(device)
self.dones = torch.zeros((self.capacity, 1), dtype=torch.float32).to(device)
def add(self, state, action, reward, next_state, done):
if self.length<self.capacity:
self.length += 1
self.indices.append(self.length-1)
self.indexes = np.array(self.indices)
idx = self.length-1
#moving is life, stalling is dangerous
delta = np.mean(np.abs(next_state - state)).clip(1e-1, 10.0)
reward -= self.stall_penalty*math.log10(1.0/delta)
if self.length==self.capacity:
self.states = torch.roll(self.states, shifts=-1, dims=0)
self.actions = torch.roll(self.actions, shifts=-1, dims=0)
self.rewards = torch.roll(self.rewards, shifts=-1, dims=0)
self.next_states = torch.roll(self.next_states, shifts=-1, dims=0)
self.dones = torch.roll(self.dones, shifts=-1, dims=0)
self.states[idx,:] = torch.FloatTensor(state).to(self.device)
self.actions[idx,:] = torch.FloatTensor(action).to(self.device)
self.rewards[idx,:] = torch.FloatTensor([reward]).to(self.device)
self.next_states[idx,:] = torch.FloatTensor(next_state).to(self.device)
self.dones[idx,:] = torch.FloatTensor([done]).to(self.device)
self.batch_size = min(max(128,self.length//500), 1024)
self.step += 1
def generate_probs(self):
if self.step>self.capacity: return self.probs
def fade(norm_index): return np.tanh(self.fade_factor*norm_index**2) # linear / -> non-linear _/‾
weights = 1e-7*(fade(self.indexes/self.length))# weights are based solely on the history, highly squashed
self.probs = weights/np.sum(weights)
return self.probs
def sample(self):
indices = self.random.choice(self.indexes, p=self.generate_probs(), size=self.batch_size)
return (
self.states[indices],
self.actions[indices],
self.rewards[indices],
self.next_states[indices],
self.dones[indices]
)
def __len__(self):
return self.length
# NOISES with cosine decrease==============================================================
class GANoise:
def __init__(self, max_action, tr_noise=True, burst=False):
self.x_coor = 0.0
self.tr_noise = tr_noise
self.scale = 1.0 if burst else 0.15
self.max_action = max_action
def generate(self, x):
if self.tr_noise and self.x_coor>=2.498: return (0.03*torch.randn_like(x)).clamp(-0.075, 0.075)
if self.x_coor>=math.pi: return 0.0
with torch.no_grad():
eps = self.scale * self.max_action * (math.cos(self.x_coor) + 1.0)
lim = 2.5*eps
self.x_coor += 3e-5
return (eps*torch.randn_like(x)).clamp(-lim, lim)
class OUNoise:
def __init__(self, action_dim, device, mu=0, theta=0.15, sigma=0.2):
self.action_dim = action_dim
self.device = device
self.x_coor = 0.0
self.mu = mu
self.theta = theta
self.sigma = sigma
self.state = torch.ones(self.action_dim).to(self.device) * self.mu
self.reset()
def reset(self):
self.state = torch.ones(self.action_dim).to(self.device) * self.mu
def generate(self, x):
if self.x_coor>=math.pi: return 0.0
with torch.no_grad():
eps = (math.cos(self.x_coor) + 1.0)
self.x_coor += 7e-4
x = self.state
dx = self.theta * (self.mu - x) + self.sigma * torch.randn_like(x)
self.state = x + dx
return eps*self.state