Repository navigation
Expand file tree
/
Copy pathold_TD3_ActionGNN.py
More file actions
482 lines (371 loc) · 17.2 KB
/
Copy pathold_TD3_ActionGNN.py
File metadata and controls
482 lines (371 loc) · 17.2 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import copy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GCNConv, GATConv, global_mean_pool, global_max_pool, TAGConv
from torch_geometric.nn import global_add_pool, SAGPooling, ASAPooling
from torch_geometric.nn.aggr import AttentionalAggregation
from torch_geometric.nn.pool import TopKPooling
from torch_geometric.nn import global_mean_pool
from torch_geometric.utils import to_undirected, softmax
import torch_geometric.transforms as T
# Implementation of Twin Delayed Deep Deterministic Policy Gradients (TD3)
# Paper: https://arxiv.org/abs/1802.09477
class Actor(nn.Module):
def __init__(self,
max_action,
fx_node_sizes,
feature_dim=8,
GNN_hidden_dim=32,
discrete_actions=1,
device=torch.device('cpu')):
super(Actor, self).__init__()
self.device = device
self.feature_dim = feature_dim
self.discrete_actions = discrete_actions
# Node-specific embedding layers
self.ev_embedding = nn.Linear(fx_node_sizes['ev'], feature_dim)
self.cs_embedding = nn.Linear(fx_node_sizes['cs'], feature_dim)
self.tr_embedding = nn.Linear(fx_node_sizes['tr'], feature_dim)
self.env_embedding = nn.Linear(fx_node_sizes['env'], feature_dim)
# GCN and GAT layers to extract features with a unified edge index
# GCN layer with 8 input, 16 output
self.gcn_conv = GCNConv(feature_dim, GNN_hidden_dim)
self.gcn_conv2 = GCNConv(GNN_hidden_dim, feature_dim)
self.gcn_conv3 = GCNConv(feature_dim, discrete_actions)
self.max_action = max_action
# def forward(self, state):
def forward(self, state, return_mapper=False):
if isinstance(state.env_features, np.ndarray):
ev_features = torch.from_numpy(
state.ev_features).float().to(self.device)
cs_features = torch.from_numpy(
state.cs_features).float().to(self.device)
tr_features = torch.from_numpy(
state.tr_features).float().to(self.device)
env_features = torch.from_numpy(
state.env_features).float().to(self.device)
edge_index = torch.from_numpy(
state.edge_index).long().to(self.device)
else:
ev_features = state.ev_features
cs_features = state.cs_features
tr_features = state.tr_features
env_features = state.env_features
edge_index = state.edge_index
# edge_index = to_undirected(edge_index)
total_nodes = ev_features.shape[0] + cs_features.shape[0] + \
tr_features.shape[0] + env_features.shape[0]
embedded_x = torch.zeros(
total_nodes, self.feature_dim, device=self.device).float()
# Apply embeddings to the corresponding segments
if len(state.ev_indexes) != 0:
embedded_x[state.ev_indexes] = self.ev_embedding(ev_features)
embedded_x[state.cs_indexes] = self.cs_embedding(cs_features)
embedded_x[state.tr_indexes] = self.tr_embedding(tr_features)
embedded_x[state.env_indexes] = self.env_embedding(env_features)
embedded_x = embedded_x.reshape(-1, self.feature_dim)
embedded_x = F.relu(embedded_x)
# Apply GCN layers with the unified edge index
x = self.gcn_conv(embedded_x, edge_index)
x1 = F.relu(x)
x = self.gcn_conv2(x1, edge_index)
x = F.relu(x)
# Residual connection
# x = embedded_x + x
x = self.gcn_conv3(x, edge_index)
# Bound output to action space
x = self.max_action * torch.tanh(x)
# x = self.max_action * torch.sigmoid(x)
# apply action mask
# valid_action_indexes = torch.where(node_types == 3, 1, 0)
if self.discrete_actions > 1:
# print(f'x shape: {x.shape}')
# print(x)
x = torch.nn.functional.softmax(x, dim=1)
# print(f'x shape: {x.shape}')
# print(x)
x = x.reshape(-1)
# input()
# x = x * valid_action_indexes
if return_mapper:
return x, None, state.ev_indexes
else:
return x
def explain_GCN(self, state):
if isinstance(state.env_features, np.ndarray):
ev_features = torch.from_numpy(
state.ev_features).float().to(self.device)
cs_features = torch.from_numpy(
state.cs_features).float().to(self.device)
tr_features = torch.from_numpy(
state.tr_features).float().to(self.device)
env_features = torch.from_numpy(
state.env_features).float().to(self.device)
edge_index = torch.from_numpy(
state.edge_index).long().to(self.device)
else:
ev_features = state.ev_features
cs_features = state.cs_features
tr_features = state.tr_features
env_features = state.env_features
edge_index = state.edge_index
# edge_index = to_undirected(edge_index)
total_nodes = ev_features.shape[0] + cs_features.shape[0] + \
tr_features.shape[0] + env_features.shape[0]
embedded_x = torch.zeros(
total_nodes, self.feature_dim, device=self.device).float()
# Apply embeddings to the corresponding segments
if len(state.ev_indexes) != 0:
embedded_x[state.ev_indexes] = self.ev_embedding(ev_features)
embedded_x[state.cs_indexes] = self.cs_embedding(cs_features)
embedded_x[state.tr_indexes] = self.tr_embedding(tr_features)
embedded_x[state.env_indexes] = self.env_embedding(env_features)
embedded_x = embedded_x.reshape(-1, self.feature_dim)
output_1 = embedded_x.clone().detach().cpu().numpy()
embedded_x = F.relu(embedded_x)
# Apply GCN layers with the unified edge index
x = self.gcn_conv(embedded_x, edge_index)
output_2 = x.clone().detach().cpu().numpy()
x1 = F.relu(x)
# output_3 = x1.clone().detach().cpu().numpy()
x = self.gcn_conv2(x1, edge_index)
output_4 = x.clone().detach().cpu().numpy()
x = F.relu(x)
x = self.gcn_conv3(x, edge_index)
output_5 = x.clone().detach().cpu().numpy()
# Bound output to action space
x = self.max_action * torch.tanh(x)
output_6 = F.relu(x).clone().detach().cpu().numpy()
# apply action mask
if self.discrete_actions > 1:
x = torch.nn.functional.softmax(x, dim=1)
x = x.reshape(-1)
return [output_1, output_2, output_4, output_6]
class Critic_GNN(nn.Module):
def __init__(self,
fx_node_sizes,
feature_dim=8,
GNN_hidden_dim=32,
mlp_hidden_dim=256,
discrete_actions=1,
device=torch.device('cpu')):
super(Critic_GNN, self).__init__()
self.device = device
self.feature_dim = feature_dim
self.discrete_actions = discrete_actions
# Node-specific embedding layers
self.ev_embedding = nn.Linear(fx_node_sizes['ev'], feature_dim)
self.cs_embedding = nn.Linear(fx_node_sizes['cs'], feature_dim)
self.tr_embedding = nn.Linear(fx_node_sizes['tr'], feature_dim)
self.env_embedding = nn.Linear(fx_node_sizes['env'], feature_dim)
# GCN and GAT layers to extract features with a unified edge index
self.gcn_conv = GCNConv(feature_dim+discrete_actions, GNN_hidden_dim)
self.gcn_conv2 = GCNConv(GNN_hidden_dim, 2*GNN_hidden_dim)
self.gcn_conv3 = GCNConv(2*GNN_hidden_dim, 3*GNN_hidden_dim)
self.l1 = nn.Linear(3*GNN_hidden_dim, mlp_hidden_dim)
self.l2 = nn.Linear(mlp_hidden_dim, mlp_hidden_dim)
self.l3 = nn.Linear(mlp_hidden_dim, 1)
def forward(self, state, action):
ev_features = state.ev_features
cs_features = state.cs_features
tr_features = state.tr_features
env_features = state.env_features
edge_index = state.edge_index
# edge_index = to_undirected(edge_index)
total_nodes = ev_features.shape[0] + cs_features.shape[0] + \
tr_features.shape[0] + env_features.shape[0]
embedded_x = torch.zeros(total_nodes,
self.feature_dim,
device=self.device).float()
# Apply embeddings to the corresponding segments
if len(state.ev_indexes) != 0:
embedded_x[state.ev_indexes] = self.ev_embedding(ev_features)
embedded_x[state.cs_indexes] = self.cs_embedding(cs_features)
embedded_x[state.tr_indexes] = self.tr_embedding(tr_features)
embedded_x[state.env_indexes] = self.env_embedding(env_features)
embedded_x = embedded_x.reshape(-1, self.feature_dim)
embedded_x = F.relu(embedded_x)
# Concatenate action to the embedded_x
state_action = torch.cat(
[embedded_x, action.reshape(-1, self.discrete_actions)], 1)
# Apply GCN layers with the unified edge index
x = self.gcn_conv(state_action, edge_index)
x1 = F.relu(x)
x = self.gcn_conv2(x1, edge_index)
x = F.relu(x)
x = self.gcn_conv3(x, edge_index)
x = F.relu(x)
# make batch sample mask
sample_node_length = state.sample_node_length
batch = torch.zeros(x.shape[0], dtype=torch.long, device=self.device)
counter = 0
for i in range(len(sample_node_length)):
batch[counter: counter + sample_node_length[i]] = i
counter += sample_node_length[i]
# Graph Embedding
pooled_embedding = global_mean_pool(x, batch=batch)
# Apply MLP
x = F.relu(self.l1(pooled_embedding))
x = F.relu(self.l2(x))
x = self.l3(x)
return x
class Critic(nn.Module):
def __init__(self,
fx_node_sizes,
feature_dim=8,
GNN_hidden_dim=32,
mlp_hidden_dim=256,
discrete_actions=1,
device=torch.device('cpu')):
super(Critic, self).__init__()
self.device = device
self.feature_dim = feature_dim
self.q1 = Critic_GNN(feature_dim=feature_dim,
fx_node_sizes=fx_node_sizes,
GNN_hidden_dim=GNN_hidden_dim,
mlp_hidden_dim=mlp_hidden_dim,
discrete_actions=discrete_actions,
device=device)
self.q2 = Critic_GNN(feature_dim=feature_dim,
fx_node_sizes=fx_node_sizes,
GNN_hidden_dim=GNN_hidden_dim,
mlp_hidden_dim=mlp_hidden_dim,
discrete_actions=discrete_actions,
device=device)
def forward(self, state, action):
return self.q1(state, action), self.q2(state, action)
def Q1(self, state, action):
return self.q1(state, action)
class TD3_ActionGNN(object):
def __init__(
self,
action_dim,
max_action,
fx_node_sizes,
discount=0.99,
tau=0.005,
policy_noise=0.2,
noise_clip=0.5,
policy_freq=2,
fx_dim=8,
fx_GNN_hidden_dim=32,
mlp_hidden_dim=256,
lr=3e-4,
discrete_actions=1,
**kwargs
):
self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu")
self.discrete_actions = discrete_actions
self.actor = Actor(max_action,
feature_dim=fx_dim,
GNN_hidden_dim=fx_GNN_hidden_dim,
fx_node_sizes=fx_node_sizes,
discrete_actions=discrete_actions,
device=self.device
).to(self.device)
self.actor_target = copy.deepcopy(self.actor).to(self.device)
self.actor_optimizer = torch.optim.Adam(
self.actor.parameters(), lr=lr)
self.critic = Critic(feature_dim=fx_dim,
GNN_hidden_dim=fx_GNN_hidden_dim,
mlp_hidden_dim=mlp_hidden_dim,
fx_node_sizes=fx_node_sizes,
discrete_actions=discrete_actions,
device=self.device
).to(self.device)
self.critic_target = copy.deepcopy(self.critic).to(self.device)
self.critic_optimizer = torch.optim.Adam(
self.critic.parameters(), lr=lr)
self.action_dim = action_dim
self.max_action = max_action
self.discount = discount
self.tau = tau
self.policy_noise = policy_noise
self.noise_clip = noise_clip
self.policy_freq = policy_freq
self.total_it = 0
def select_action(self, state, expl_noise=0, **kwargs):
state = state.to(self.device)
with torch.no_grad():
action, valid_action_indexes, ev_indexes = self.actor(
state, return_mapper=True)
noise = torch.randn_like(action) * expl_noise
action = (action + noise).clamp(-self.max_action, self.max_action)
# action = action * valid_action_indexes
mapped_action = np.zeros(self.action_dim)
if self.discrete_actions == 1:
for index, i in enumerate(state.action_mapper):
mapped_action[i] = action[ev_indexes[index]]
else:
temp_action = action.reshape(-1, self.discrete_actions)
# find max index in each row
temp_action = torch.argmax(temp_action, dim=1)
for index, i in enumerate(state.action_mapper):
mapped_action[i] = temp_action[ev_indexes[index]]
if expl_noise != 0:
return mapped_action, action
else:
return mapped_action
def train(self, replay_buffer, batch_size=256):
self.total_it += 1
# Sample replay buffer
state, action, next_state, reward, not_done = replay_buffer.sample(
batch_size)
with torch.no_grad():
# Select action according to policy and add clipped noise
next_action, valid_action_indexes, _ = self.actor_target(next_state,
return_mapper=True)
noise = (torch.randn_like(next_action) * self.policy_noise).clamp(-self.noise_clip,
self.noise_clip)
next_action = (next_action + noise).clamp(-self.max_action,
self.max_action)
# next_action = next_action * valid_action_indexes
# Compute the target Q value
target_Q1, target_Q2 = self.critic_target(next_state, next_action)
target_Q = torch.min(target_Q1, target_Q2)
target_Q = reward + not_done * self.discount * target_Q
# Get current Q estimates
current_Q1, current_Q2 = self.critic(state, action)
# Compute critic loss
critic_loss = F.mse_loss(current_Q1, target_Q) + \
F.mse_loss(current_Q2, target_Q)
# Optimize the critic
self.critic_optimizer.zero_grad()
critic_loss.backward()
self.critic_optimizer.step()
# Delayed policy updates
if self.total_it % self.policy_freq == 0:
# Compute actor losse
actor_loss = -self.critic.Q1(state, self.actor(state)).mean()
# Optimize the actor
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
# Update the frozen target models
for param, target_param in zip(self.critic.parameters(), self.critic_target.parameters()):
target_param.data.copy_(
self.tau * param.data + (1 - self.tau) * target_param.data)
for param, target_param in zip(self.actor.parameters(), self.actor_target.parameters()):
target_param.data.copy_(
self.tau * param.data + (1 - self.tau) * target_param.data)
return critic_loss.item(), actor_loss.item()
return None, None
def save(self, filename):
torch.save(self.critic.state_dict(), filename + "_critic")
torch.save(self.critic_optimizer.state_dict(),
filename + "_critic_optimizer")
torch.save(self.actor.state_dict(), filename + "_actor")
torch.save(self.actor_optimizer.state_dict(),
filename + "_actor_optimizer")
def load(self, filename):
self.critic.load_state_dict(torch.load(filename + "_critic"))
self.critic_optimizer.load_state_dict(
torch.load(filename + "_critic_optimizer"))
self.critic_target = copy.deepcopy(self.critic)
self.actor.load_state_dict(torch.load(filename + "_actor"))
self.actor_optimizer.load_state_dict(
torch.load(filename + "_actor_optimizer"))
self.actor_target = copy.deepcopy(self.actor)