forked from zyrrron/Vespa-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomCaseGenerator.py
318 lines (283 loc) · 11.9 KB
/
RandomCaseGenerator.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import matplotlib.pyplot as plt
import networkx as nx
import random
import pydot
from networkx.drawing.nx_pydot import write_dot
from collections import Counter
import os
def createVertexList(nodeinfo, typeinfo):
Vertexlist = []
i = 0
while i < len(nodeinfo):
j = 1
while j <= nodeinfo[i]:
NodeName = f"{typeinfo[i]}{j}"
Vertexlist.append(NodeName)
j += 1
i += 1
return Vertexlist
def generateRandomNodeInfo(ComplexityUpperBound, ConstraintLowerBound):
# randomly choose the total number N of nodes
N = random.randint(2 * ConstraintLowerBound + 2, int(ComplexityUpperBound / 2) - 1)
# randomly choose number Nc for ctrl-layer components, Nv for valves and Nf for flow ports (at least 2), Nv can be 0
Nc = random.randint(2 * ConstraintLowerBound, N - 2)
Nf = N - Nc
Nv = random.randint(1, Nc - 1)
# decide number for Nfp, Nfo, Ncp, Nco
Nfp = random.randint(2, Nf)
Nfo = Nf - Nfp
Ncp = random.randint(1, Nc - Nv)
Nco = Nc - Ncp - Nv
return [N, Nf, Nc, Nfp, Nfo, Ncp, Nv, Nco]
# generate edges, if the edge not exist in the past edge groups, go to the next enumerate
# exist, generate the edges again
def createFlowEdge(ENf):
i = 0
iterate = 1
flow_edge_list = []
FlowNodeInfo = CurrentNodeInfo[3:5]
FPflag = 0
FOflag = 0
while i < ENf:
iterate += 1
if iterate > 200:
break
# each flow port should be one terminal of a connection, so firstly generate the edges include all flow ports
# then generate the edges include all other flow components
if FPflag < FlowNodeInfo[0]:
NodeType1 = "f"
NodeNum1 = FPflag + 1
elif FOflag < FlowNodeInfo[1]:
NodeType1 = "fo"
NodeNum1 = FOflag + 1
else:
NodeType1Num = random.randint(0, len(FlowTypeList) - 1)
if FlowNodeInfo[NodeType1Num] < 1:
continue
NodeType1 = FlowTypeList[NodeType1Num]
NodeNum1 = random.randint(1, FlowNodeInfo[NodeType1Num])
NodeType2Num = random.randint(0, len(FlowTypeList) - 1)
if FlowNodeInfo[NodeType2Num] < 1:
continue
NodeType2 = FlowTypeList[NodeType2Num]
NodeNum2 = random.randint(1, FlowNodeInfo[NodeType2Num])
Node1 = f"{NodeType1}{NodeNum1}"
Node2 = f"{NodeType2}{NodeNum2}"
if Node1 == Node2:
continue
weight = 2
CurrentEdge = [Node1, Node2, weight]
# check if the edge we generate in this iteration exists in the edge list, if not, add into the list
RepeatFlag = 0
for ExistsEdge in flow_edge_list:
if Counter(CurrentEdge) == Counter(ExistsEdge):
RepeatFlag = 1
break
if RepeatFlag == 1:
continue
if FPflag < FlowNodeInfo[0]:
FPflag += 1
elif FOflag < FlowNodeInfo[1]:
FOflag += 1
flow_edge_list.append(CurrentEdge)
i += 1
return flow_edge_list
def createControlEdge(ENc):
i = 0
iterate = 1
control_edge_list = []
ControlNodeInfo = CurrentNodeInfo[5:]
Vflag = 0
COflag = 0
# show if in this iteration the valve is one terminal necessarily
while i < ENc:
Vround = 0
iterate += 1
if iterate > 200:
break
# each control component should be one node of an edge and the other node is control port in need.
# one co may connect to many control ports
NodeType2Num = 0
if Vflag < ControlNodeInfo[1] or COflag < ControlNodeInfo[2]:
if Vflag < ControlNodeInfo[1]:
NodeType1 = "v"
Vround = 1
NodeNum1 = Vflag + 1
else:
NodeType1 = "co"
NodeNum1 = COflag + 1
NodeType2Num = 0
else:
NodeType1Num = random.randint(0, len(ControlTypeList) - 1)
if ControlNodeInfo[NodeType1Num] < 1:
continue
NodeType1 = ControlTypeList[NodeType1Num]
NodeNum1 = random.randint(1, ControlNodeInfo[NodeType1Num])
# two control ports are not allowed to connect as an edge
if NodeType1Num == 0:
NodeType2Num = random.randint(1, len(ControlTypeList) - 1)
if ControlNodeInfo[NodeType2Num] < 1:
continue
NodeType2 = ControlTypeList[NodeType2Num]
NodeNum2 = random.randint(1, ControlNodeInfo[NodeType2Num])
Node1 = f"{NodeType1}{NodeNum1}"
Node2 = f"{NodeType2}{NodeNum2}"
if Node1 == Node2:
continue
weight = 1
CurrentEdge = [Node1, Node2, weight]
# check if the edge we generate in this iteration exists in the edge list, if not, add into the list
RepeatFlag = 0
for ExistsEdge in control_edge_list:
if Counter(CurrentEdge) == Counter(ExistsEdge):
RepeatFlag = 1
break
if RepeatFlag == 1:
continue
# Check if this is round for building edge with one valve, if not, check if COflag is bigger than its upper
# bound, if not, add 1 into its value
if Vround == 1:
Vflag += 1
elif COflag < ControlNodeInfo[2]:
COflag += 1
control_edge_list.append(CurrentEdge)
i += 1
return control_edge_list
def createNumberOfFlowAndControlEdge(EdgeNum, lb):
ENf = 0
ENc = 0
iterate = 0
while 1:
iterate += 1
if iterate > 200:
break
ENfUpperBound = min(Nf + 1, EdgeNum - Nco - Nv)
if Nf - 1 >= ENfUpperBound:
continue
ENf = random.randint(Nf-1, ENfUpperBound)
ENc = EdgeNum - ENf
if ENc > 0 and ENc in range(Nco + Nv, int(Nc * (Nc - 1) / 2)):
break
elif ENc == 0:
break
return iterate, ENf, ENc
# plot the random graph, two layer with different color
def plotAndSave(SectionNum, flow_edge_list, control_edge_list, CurrentNodeInfo, CurrentEdgeInfo, edgenum, nodenum):
g_flow = nx.DiGraph()
g_control = nx.DiGraph()
# create a list of node names
FlowNodeInfo = CurrentNodeInfo[3:5]
ControlNodeInfo = CurrentNodeInfo[5:]
flow_node_list = createVertexList(FlowNodeInfo, FlowTypeList)
control_node_list = createVertexList(ControlNodeInfo, ControlTypeList)
# add elements into g_flow and g_control
g_flow.add_nodes_from(flow_node_list)
g_flow.add_weighted_edges_from(flow_edge_list)
g_control.add_nodes_from(control_node_list)
g_control.add_weighted_edges_from(control_edge_list)
if EdgeGroupNum == 1:
print("Flow and Control node list:")
print(flow_node_list)
print(control_node_list)
ValveCOLocateList = []
valve_list = control_node_list[CurrentNodeInfo[5]:CurrentNodeInfo[5]+CurrentNodeInfo[6]]
co_list = control_node_list[CurrentNodeInfo[5]+CurrentNodeInfo[6]:]
for i in range(CurrentNodeInfo[6]):
valve_location = random.randint(0, len(flow_edge_list) - 1)
valve_relationship = [valve_list[i]] + flow_edge_list[valve_location][0:2]
ValveCOLocateList.append(valve_relationship)
for i in range(CurrentNodeInfo[7]):
co_location = random.randint(0, len(flow_edge_list) - 1)
co_relationship = [co_list[i]] + flow_edge_list[co_location][0:2]
ValveCOLocateList.append(co_relationship)
str1 = '_'.join(str(v) for v in CurrentNodeInfo)
str2 = '_'.join(str(v) for v in CurrentEdgeInfo)
folder_path = f"RandomCaseFiles/backup/Section_{SectionNum}/Node{nodenum}_{str1}"
outpath_v = f"RandomCaseFiles/backup/Section_{SectionNum}/Node{nodenum}_{str1}/Edge{edgenum}|{str2}_valve&co.txt"
outpath_c = f"RandomCaseFiles/backup/Section_{SectionNum}/Node{nodenum}_{str1}/Edge{edgenum}|{str2}_control.dot"
outpath_f = f"RandomCaseFiles/backup/Section_{SectionNum}/Node{nodenum}_{str1}/Edge{edgenum}|{str2}_flow.dot"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
write_dot(g_flow, outpath_f)
write_dot(g_control, outpath_c)
with open(outpath_v, 'w') as f:
for vi in ValveCOLocateList:
vi = str(vi).strip('[').strip(']').replace(',', '').replace('\'', '') + '\n'
f.writelines(vi)
# plot control layer graph
pos = nx.spring_layout(g_control)
color_list = ['red' for i in range(len(control_node_list))]
nx.draw_networkx(g_control, pos, nodelist=control_node_list, node_color=color_list)
plt.show()
# plot flow layer graph
pos = nx.spring_layout(g_flow)
color_list = ['yellow' for i in range(len(flow_node_list))]
nx.draw_networkx(g_flow, pos, nodelist=flow_node_list, node_color=color_list)
plt.show()
# complexity bound for each section: [0, 100, 500, inf]
# Constraint bound for each section: [1, 5, 10, 15]
# for section 3, the upperbound of loop var flag and iteration is 3 times bigger than previous ones
if __name__ == '__main__':
SectionNum = 3
ComplexityLowerBound, ComplexityUpperBound = 500, 1000
ConstraintLowerBound, ConstraintUpperBound = 10, 15
FlowTypeList = ["f", "fo"]
ControlTypeList = ["c", "v", "co"]
NodeGroupNum = 1
NodeList = []
while NodeGroupNum <= 5:
[N, Nf, Nc, Nfp, Nfo, Ncp, Nv, Nco] = generateRandomNodeInfo(ComplexityUpperBound, ConstraintLowerBound)
CurrentNodeInfo = [N, Nf, Nc, Nfp, Nfo, Ncp, Nv, Nco]
if CurrentNodeInfo in NodeList:
continue
else:
NodeList.append(CurrentNodeInfo)
# creat edges and check if it will go beyond the complexity upper bound for current section
# randomly choose number of edges for the current node group in a limited range
# since we divide the chip into two different graphs, we need to have limited edges giving the num of nodes
EdgeGroupNum = 1
flag = 0
EdgeNumInfo = []
EdgeListAll = []
flow_edge_listAll = []
control_edge_listAll = []
while EdgeGroupNum <= 10:
flag += 1
if flag > 2000:
break
EdgeNumUpperBound = min(ComplexityUpperBound - N * 2, int(Nf * (Nf - 1) / 2 + Nc * (Nc - 1) / 2))
# make sure all control components has connection to control port and all flow layer components are connected
EdgeNumLowerBound = max(ComplexityLowerBound - 2 * N, Nf - 1 + Nv + Nco)
if EdgeNumUpperBound < EdgeNumLowerBound:
continue
# randomly choose number of flow and control edges, and create flow and control edges in detail
EdgeNum = random.randint(EdgeNumLowerBound, EdgeNumUpperBound)
iterate, ENf, ENc = createNumberOfFlowAndControlEdge(EdgeNum, ConstraintLowerBound)
if iterate > 200:
continue
flow_edge_list = createFlowEdge(ENf)
control_edge_list = createControlEdge(ENc)
EdgeList = flow_edge_list + control_edge_list
# number of edges are not be given in a good departure, return back to change the edge number
if len(EdgeList) < EdgeNum:
continue
CurrentEdgeInfo = [EdgeNum, ENf, ENc]
if CurrentEdgeInfo in EdgeNumInfo:
continue
EdgeNumInfo.append(CurrentEdgeInfo)
EdgeGroupNum += 1
EdgeListAll.append(EdgeList)
flow_edge_listAll.append(flow_edge_list)
control_edge_listAll.append(control_edge_list)
if flag > 2000:
NodeList.pop()
print("Beyond loop upper bound, try a new node group again!")
continue
print()
print(NodeList[-1])
for i in range(len(EdgeListAll)):
plotAndSave(SectionNum, flow_edge_listAll[i], control_edge_listAll[i], NodeList[-1], EdgeNumInfo[i], i+1, NodeGroupNum)
# print(EdgeNumInfo[i])
# print(EdgeListAll[i])
print()
NodeGroupNum += 1