-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
177 lines (136 loc) · 5.61 KB
/
generator.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
import random
from collections import defaultdict
def RandomIdxs(n: int, sameRate: float = 1.):
a = round(sameRate * n)
idxs = list(range(a))
for _ in range(n - a):
idxs.append(random.randint(0, a))
random.shuffle(idxs)
return idxs
def MakeEdge(terminals: list[int], name: str, sameRate: float = 1.) -> list[str]:
nZeros = terminals.count(0)
idxs = RandomIdxs(nZeros + 1, sameRate)
edges, idx, head, lastIdx = [], 0, 0, -1
for i, t in enumerate(terminals):
if t == 0:
if idxs[idx] != lastIdx:
lastIdx = idxs[idx]
edges.append(f"{name}{lastIdx} {head} {i}")
else:
edges[-1] = " ".join(edges[-1].split()[:-1] + [str(i)])
head = i
idx += 1
if idxs[idx] != lastIdx:
lastIdx = idxs[idx]
edges.append(f"{name}{lastIdx} {head} {i}")
else:
edges[-1] = " ".join(edges[-1].split()[:-1] + [str(i)])
return edges
def MakeTerminalLists(nNetNumList: list[int], nBreak: int, nNet: int) -> tuple[list[int], list[int]]:
netNumberMap = {
i: (random.choice(nNetNumList) if i != 0 else nBreak) for i in range(nNet + 1)
}
terminals = []
for net, number in netNumberMap.items():
terminals.extend([net for _ in range(number)])
random.shuffle(terminals)
nTerminal = len(terminals)
assert nTerminal % 2 == 0, "Number of terminals must be even."
return terminals[:nTerminal // 2], terminals[nTerminal // 2:]
def IsFeasibleTerminals(upperTerminals: list[int], lowerTerminals: list[int]) -> bool:
def IsHasTwoZero(terminals: list[int]) -> bool:
for i in range(len(terminals) - 1):
if terminals[i] == 0 and terminals[i + 1] == 0:
return True
return False
def IsHasVerticalTerminal(upperTerminals: list[int], lowerTerminals: list[int]) -> bool:
for u, l in zip(upperTerminals, lowerTerminals):
if u == l:
return True
return False
def IsCycleConflict(upperTerminals: list[int], lowerTerminals: list[int]) -> bool:
def DFS(v: str, graph: dict[str, set[str]], t: str) -> bool:
if v not in graph:
return False
visitMap = {k: False for k in graph}
stack = [v]
while stack:
c = stack.pop()
visitMap[c] = True
for n in graph[c]:
if n == t:
return True
if n in visitMap and not visitMap[n]:
stack.append(n)
return False
countMap = defaultdict(int)
for u, l in zip(upperTerminals, lowerTerminals):
countMap[u] += 1
countMap[l] += 1
headMap = defaultdict(int)
graph = defaultdict(set)
for u, l in zip(upperTerminals, lowerTerminals):
if u == 0 or l == 0:
if u != 0: headMap[u] += 1
if l != 0: headMap[l] += 1
continue
isUpperInit = headMap[u] == 0
isLowerInit = headMap[l] == 0
if not (isUpperInit or isLowerInit):
graph[f"{u}_{headMap[u]}"].add(f"{l}_{headMap[l]}")
headMap[u] += 1
headMap[l] += 1
isUpperAdd = headMap[u] < countMap[u]
isLowerAdd = headMap[l] < countMap[l]
if isLowerAdd:
if isUpperAdd:
graph[f"{u}_{headMap[u]}"].add(f"{l}_{headMap[l]}")
if not isUpperInit:
graph[f"{u}_{headMap[u] - 1}"].add(f"{l}_{headMap[l]}")
if isUpperAdd:
if not isLowerInit:
graph[f"{u}_{headMap[u]}"].add(f"{l}_{headMap[l] - 1}")
for v in graph:
if DFS(v, graph, v):
return True
return False
if upperTerminals[0] == 0 or lowerTerminals[0] == 0 or upperTerminals[-1] == 0 or lowerTerminals[-1] == 0:
return False
if IsHasTwoZero(upperTerminals) or IsHasTwoZero(lowerTerminals):
return False
if IsHasVerticalTerminal(upperTerminals, lowerTerminals):
return False
if IsCycleConflict(upperTerminals, lowerTerminals):
return False
return True
def Main():
SAVE_FILE = "data/input9.in"
N_NET = 40
N_BREAK = 10
N_NET_NUM_LIST = [2, 4, 6]
SAME_EDGE_RATE = 0.3
upperTerminals, lowerTerminals = MakeTerminalLists(N_NET_NUM_LIST, N_BREAK, N_NET)
while (not IsFeasibleTerminals(upperTerminals, lowerTerminals)):
upperTerminals, lowerTerminals = MakeTerminalLists(N_NET_NUM_LIST, N_BREAK, N_NET)
print("")
print("Upper terminals:")
print(upperTerminals)
print("Lower terminals:")
print(lowerTerminals)
upperEdges = MakeEdge(upperTerminals, "T", SAME_EDGE_RATE)
lowerEdges = MakeEdge(lowerTerminals, "B", SAME_EDGE_RATE)
print("")
print(f"Upper Edges: (len = {len(upperTerminals)})")
print(upperEdges)
print(f"Lower Edges: (len = {len(lowerTerminals)})")
print(lowerEdges)
print("")
with open(SAVE_FILE, "w") as f:
for e in upperEdges:
f.write(e + '\n')
for e in lowerEdges:
f.write(e + '\n')
f.write(" ".join([str(i) for i in upperTerminals]) + '\n')
f.write(" ".join([str(i) for i in lowerTerminals]))
if __name__ == "__main__":
Main()