-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
241 lines (200 loc) · 8.26 KB
/
helpers.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
import math
import random
import matplotlib
import matplotlib.pyplot as plt
from collections import namedtuple, deque
# from itertools import count
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
# plt.ion()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Transition Defintion
Transition = namedtuple('Transition', ('state', 'action', 'next_state', 'reward'))
# Replay Memory
class ReplayMemory(object):
def __init__(self, capacity):
self.memory = deque([], maxlen=capacity)
def push(self, *args):
"""Save a transition"""
self.memory.append(Transition(*args))
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
class DQN(nn.Module):
def __init__(self, n_observations, n_actions, n_hidden=128):
super(DQN, self).__init__()
n_hidden = max(n_hidden, n_observations)
self.fc1 = nn.Linear(n_observations, n_hidden)
self.fc2 = nn.Linear(n_hidden, n_hidden)
self.fc3 = nn.Linear(n_hidden, n_actions)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def hierarchy_pos(G, root=None, width=20., vert_gap = 0.4, vert_loc = 0, xcenter = 0.5):
# From Joel's answer at https://stackoverflow.com/a/29597209/2966723.
def _hierarchy_pos(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5, pos = None, parent = None):
if pos is None:
pos = {root:(xcenter,vert_loc)}
else:
pos[root] = (xcenter, vert_loc)
children = list(G.neighbors(root))
if not isinstance(G, nx.DiGraph) and parent is not None:
children.remove(parent)
if len(children)!=0:
dx = width/len(children)
nextx = xcenter - width/2 - dx/2
for child in children:
nextx += dx
pos = _hierarchy_pos(G,child, width = dx, vert_gap = vert_gap,
vert_loc = vert_loc-vert_gap, xcenter=nextx,
pos=pos, parent = root)
return pos
return _hierarchy_pos(G, root, width, vert_gap, vert_loc, xcenter)
# Generating Subassemblies in a recursive manner
def recurGen(p, H, G):
currCon = list(H.edges())
if(len(currCon) > 0):
i = G.number_of_nodes() + 1
for c in currCon:
Hnew = H.copy()
Hnew.remove_edges_from([c])
if(c in seqConstraint.keys() and seqConstraint[c] in Hnew.edges()):
pass # This means a feasability constraint has been failed!
else:
idString = str(Hnew.edges())
nextState = next((x for x, y in nx.get_node_attributes(G,'idS').items() if y == idString), None)
if(nextState is not None):
Rew, Hnew = R(p, c, H)
G.add_edge(p, nextState, a=c, r=Rew)
else:
G.add_node(i, idS=idString)
Rew, Hnew = R(p,c,H)
G.add_edge(p, i, a=c, r=Rew)
G = recurGen(i, Hnew, G)
i = G.number_of_nodes() + 1
return G
else:
return G
# Getting next set of possible states and actions
def nextGen(H):
currCon = list(H.edges())
G = nx.DiGraph()
G.add_node(1, value=H, idS=str(H.edges()))
if(len(currCon) > 0):
for c in currCon:
i = G.number_of_nodes() + 1
if(c in seqConstraint.keys() and seqConstraint[c] in Hnew.edges()):
pass # This means a feasability constraint has been failed!
else:
Hnew = H.copy()
Hnew.remove_edges_from([c])
G.add_node(i, idS=str(Hnew.edges()))
Rew, Hnew = R(1,c,H)
G.add_edge(1, i, a=c, r=Rew)
return G
else:
return None
# Checking Feasability
def T(sN, s, a):
# Sequential Constraints are already handled via the tree generation!
pass
# Cost structure which allows for an intermediary construction zone
# between the supply vehicle and construction area
def Rcaz(s,a,H): # Reward Function Assuming there is a CAZ
Rewards[(s,a)] = 0
APieces = list(nx.connected_components(H))
Hnew = H.copy()
Hnew.remove_edges_from([a])
BPieces = list(nx.connected_components(Hnew))
if(Hnew.number_of_edges() == 0): # Check if fully deconstructed
Rewards[(s,a)] = 1
elif(len(BPieces) - len(APieces) > 0):
diffPieces = [list(i) for i in BPieces if i not in APieces and len(i) <= 3]
# Check if removing last connection for a given part
for i in diffPieces:
# Check if creating multiple assemblies and sizes of these new assemblies
if(len(i) == 1):
if(Hnew.nodes[i[0]]["loc"] == "CL"): # Going from CL to SV
Hnew.nodes[i[0]]["loc"] = "SV"
Rewards[(s,a)] += -(0.0468 + 0.0499) #SV-CL + CL-SV
else: # Going from CAZ to SV
Hnew.nodes[i[0]]["loc"] = "SV"
Rewards[(s,a)] += -(0.0403 + 0.0420) #SV-CAZ + CAZ-SV
elif(len(i) == 2): # Have to fix last bit problem
for p in i:
if(Hnew.nodes[p]["loc"] == "CL"): # Going from CL to CAZ
Hnew.nodes[p]["loc"] = "CAZ"
Rewards[(s,a)] += -(0.0307 + 0.0415) #SV-CL + CL-CAZ2
elif(len(i) == 3): # Have to fix last problem
for p in i:
if(Hnew.nodes[p]["loc"] == "CL"): # Going from CL to CAZ
Hnew.nodes[p]["loc"] = "CAZ"
Rewards[(s,a)] += -(0.0307 + 0.0475) #SV-CL + CL-CAZ3
return Rewards[(s,a)], Hnew
# Reward Function Assuming there is NO CAZ (i.e, structures are constructed at the Supply Vehicle)
def RNOcaz(s,a,H):
s = str(s)
Rewards[(s,a)] = 0
APieces = list(nx.connected_components(H))
Hnew = H.copy()
Hnew.remove_edges_from([a])
BPieces = list(nx.connected_components(Hnew))
if(Hnew.number_of_edges() == 0): # Check if fully deconstructed
Rewards[(s,a)] = 1
elif(len(BPieces) - len(APieces) > 0):
diffPieces = [list(i) for i in BPieces if i not in APieces and len(i) <= 3]
# Check if removing last connection for a given part
for i in diffPieces:
# Check if creating multiple assemblies and sizes of these new assemblies
if(len(i) == 1):
Rewards[(s,a)] += -(0.0468 + 0.0499) #SV-CL + CL-SV
elif(len(i) == 2):
Rewards[(s,a)] += -(0.0749 + 0.0499) #SV-CL + CL-SV2
elif(len(i) == 3):
Rewards[(s,a)] += -(0.0869 + 0.0499) #SV-CL + CL-SV3
return Rewards[(s,a)], Hnew
def Rcustom(s,a,H):
s = str(s)
Rewards[(s,a)] = -0.1
APieces = list(nx.connected_components(H))
Hnew = H.copy()
Hnew.remove_edges_from([a])
BPieces = list(nx.connected_components(Hnew))
if(Hnew.number_of_edges() == 0): # Check if fully deconstructed
Rewards[(s,a)] = 1
elif(len(BPieces) - len(APieces) > 0):
diffPieces = [list(i) for i in BPieces if i not in APieces and len(i) <= 3]
# Check if removing last connection for a given part
for i in diffPieces:
# Check if creating multiple assemblies and sizes of these new assemblies
if(len(i) == 1):
Rewards[(s,a)] += -1 #SV-CL + CL-SV
elif(len(i) == 2):
Rewards[(s,a)] += -1.5 #SV-CL + CL-SV2
elif(len(i) == 3):
Rewards[(s,a)] += -1.75 #SV-CL + CL-SV3
return Rewards[(s,a)], Hnew
def Rsimple(s,a,H):
lab = str(s)
Hnew = H.copy()
Hnew.remove_edges_from([a])
edges = fullE.copy()
if(edges.index(a) < H.number_of_edges()-1):
Rewards[(lab,a)] = H.number_of_edges() - edges.index(a)
else:
Rewards[(lab,a)] = -1
return Rewards[(lab,a)], Hnew
# Allows you to pick which reward function to use!
def R(s, a, H):
lab = str(s)
if((lab,a) not in Rewards.keys()):
Rewards[(lab,a)], Hnew = RNOcaz(s,a,H)
else:
Hnew = H.copy()
Hnew.remove_edges_from([a])
return Rewards[(lab,a)], Hnew