-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhetnet.py
More file actions
151 lines (120 loc) · 5.21 KB
/
hetnet.py
File metadata and controls
151 lines (120 loc) · 5.21 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
# -*- coding: utf-8 -*-
"""
Heterogeneous Graph Neural Network for rollout policcy
Modified for Aircraft Maintenance Scheduling
"""
import torch
import torch.nn as nn
from graph.hetgatv2 import MultiHeteroGATLayer
# Default GNN
class HetNet(nn.Module):
def __init__(self, in_dim, hid_dim, out_dim, cetypes, num_heads=4):
super(HetNet, self).__init__()
hid_dim_input = {}
for key in hid_dim:
hid_dim_input[key] = hid_dim[key] * num_heads
self.layer1 = MultiHeteroGATLayer(in_dim, hid_dim, cetypes, num_heads)
self.layer2 = MultiHeteroGATLayer(hid_dim_input, hid_dim, cetypes,
num_heads)
self.layer3 = MultiHeteroGATLayer(hid_dim_input, out_dim, cetypes,
num_heads, merge='avg')
'''
input
g: DGL heterograph
number of Q score nodes = number of available actions
feat_dict: dictionary of input features
'''
def forward(self, g, feat_dict):
h1 = self.layer1(g, feat_dict)
h2 = self.layer2(g, h1)
h3 = self.layer3(g, h2)
return h3
# GNN - 4 layer
class HetNet4Layer(nn.Module):
def __init__(self, in_dim, hid_dim, out_dim, cetypes, num_heads=4):
super(HetNet4Layer, self).__init__()
hid_dim_input = {}
for key in hid_dim:
hid_dim_input[key] = hid_dim[key] * num_heads
self.layer1 = MultiHeteroGATLayer(in_dim, hid_dim, cetypes, num_heads)
self.layer2 = MultiHeteroGATLayer(hid_dim_input, hid_dim, cetypes,
num_heads)
self.layer3 = MultiHeteroGATLayer(hid_dim_input, hid_dim, cetypes,
num_heads)
self.layer4 = MultiHeteroGATLayer(hid_dim_input, out_dim, cetypes,
num_heads, merge='avg')
'''
input
g: DGL heterograph
number of Q score nodes = number of available actions
feat_dict: dictionary of input features
'''
def forward(self, g, feat_dict):
h1 = self.layer1(g, feat_dict)
h2 = self.layer2(g, h1)
h3 = self.layer3(g, h2)
h4 = self.layer4(g, h3)
return h4
# GNN - Actor Critic Verison
class HetNetAC(nn.Module):
def __init__(self, in_dim, hid_dim, out_dim, cetypes, num_heads=4):
super(HetNetAC, self).__init__()
hid_dim_input = {}
for key in hid_dim:
hid_dim_input[key] = hid_dim[key] * num_heads
self.layer1 = MultiHeteroGATLayer(in_dim, hid_dim, cetypes, num_heads)
self.layer2 = MultiHeteroGATLayer(hid_dim_input, hid_dim, cetypes,
num_heads)
self.layer3 = MultiHeteroGATLayer(hid_dim_input, out_dim, cetypes,
num_heads, merge='avg')
self.critic_head = nn.Linear(out_dim['state'], 1)
self.relu = nn.ReLU()
'''
input
g: DGL heterograph
number of Q score nodes = number of available actions
feat_dict: dictionary of input features
'''
def forward(self, g, feat_dict):
h1 = self.layer1(g, feat_dict)
h2 = self.layer2(g, h1)
h3 = self.layer3(g, h2)
# 1x1
critic_value = self.critic_head(self.relu(h3['state']))
return h3, critic_value
# GNN - Actor Critic Verison
# Add time as a sperate input: denoted as key 'extra'
class HetNetACExtra(nn.Module):
def __init__(self, in_dim, hid_dim, out_dim, cetypes, num_heads=4):
super(HetNetACExtra, self).__init__()
hid_dim_input = {}
for key in hid_dim:
hid_dim_input[key] = hid_dim[key] * num_heads
self.layer1 = MultiHeteroGATLayer(in_dim, hid_dim, cetypes, num_heads)
self.layer2 = MultiHeteroGATLayer(hid_dim_input, hid_dim, cetypes,
num_heads)
self.layer3 = MultiHeteroGATLayer(hid_dim_input, out_dim, cetypes,
num_heads, merge='avg')
self.critic_head = nn.Linear(out_dim['extra'] + out_dim['state'], 1)
self.extra_fc1 = nn.Linear(in_dim['extra'], out_dim['extra'])
#self.extra_fc2 = nn.Linear(hid_dim['extra'], out_dim['extra'])
self.relu = nn.ReLU()
'''
input
g: DGL heterograph
number of Q score nodes = number of available actions
feat_dict: dictionary of input features
'''
def forward(self, g, feat_dict):
# gnn
h1 = self.layer1(g, feat_dict)
h2 = self.layer2(g, h1)
h3 = self.layer3(g, h2)
# extra
extra1 = self.relu(self.extra_fc1(feat_dict['extra']))
#extra2 = self.relu(self.extra_fc2(extra1))
# concat
comb = torch.cat([self.relu(h3['state']), extra1], dim=1)
# 1x1
critic_value = self.critic_head(comb)
return h3, critic_value