-
Notifications
You must be signed in to change notification settings - Fork 0
/
eisenbahn_brutal.py
239 lines (200 loc) · 6.95 KB
/
eisenbahn_brutal.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
import itertools as it
import time, sys, getopt
LINE_BEGIN = '\r'
LINE_CLEAR = '\x1b[2K'
GATE_TYPE_T = 0
GATE_TYPE_C = 1
GATE_TYPE_L = 2
GATE_TYPE_R = 3
GATE_LABEL_T = 'T'
GATE_LABEL_C = 'C'
GATE_LABEL_DICT = {
GATE_TYPE_T: GATE_LABEL_T,
GATE_TYPE_C: GATE_LABEL_C
}
def GT(s):
return (s, GATE_TYPE_T)
def GC(s):
return (s, GATE_TYPE_C)
def GL(s):
return (s, GATE_TYPE_L)
def GR(s):
return (s, GATE_TYPE_R)
def degenerate(g):
t = typeOf(g)
if t == GATE_TYPE_C:
return g
return (switchOf(g), GATE_TYPE_T)
GTC = [GT, GC]
GTC2 = list(it.product(GTC, repeat=2))
def switchOf(g):
return g[0]
def typeOf(g):
return g[1]
def labelOf(g):
return GATE_LABEL_DICT[typeOf(g)]
def reprOf(g):
return f"{switchOf(g)}{labelOf(g)}"
def getAllGates(S):
allGates = []
for s in S:
allGates.append(GT(s))
allGates.append(GC(s))
return allGates
def getAllLRCGates(S):
allLRCGates = []
for s in S:
allLRCGates.append(GL(s))
allLRCGates.append(GR(s))
allLRCGates.append(GC(s))
return allLRCGates
class RailwaySystem:
def __init__(self, rel):
self.rel = rel
self.mapping = dict(rel + [(g2, g1) for (g1, g2) in rel])
self.switches = []
for (g, _) in rel:
s = switchOf(g)
if s not in self.switches:
self.switches.append(s)
self.dimension = len(self.switches)
self.gates = getAllGates(self.switches)
def __str__(self):
return "<" + " ".join([f"{reprOf(g1)}-{reprOf(g2)}" for (g1, g2) in self.rel]) + ">"
def __eq__(self, other):
for (g1, g2) in self.rel:
if not other.linked(g1, g2):
return False
return True
def __getitem__(self, index):
return list(self.rel)[index]
def unique(self, g):
return self.mapping[g]
def linked(self, g1, g2):
return ((g1, g2) in self.rel) or ((g2, g1) in self.rel)
def isConnected(self):
neighbourhoodMapping = dict()
for s1 in self.switches:
neighbourhood = []
for s2 in self.switches:
if s1 != s2:
exists = False
for (X, Y) in GTC2:
if self.linked(X(s1), Y(s2)):
exists = True
break
if exists:
neighbourhood.append(s2)
neighbourhoodMapping[s1] = neighbourhood
checked = [self.switches[0]]
for s in checked:
neighbourhood = neighbourhoodMapping[s]
for n in neighbourhood:
if n not in checked:
checked.append(n)
if len(checked) != len(self.switches):
return False
return True
def isIsomorphicTo(self, R):
for perm in it.permutations(R.switches):
mapping = dict(zip(self.switches, perm))
holdsAll = True
for (s1, s2) in it.product(self.switches, repeat=2):
for (X, Y) in GTC2:
if self.linked(X(s1), Y(s2)):
if not R.linked(X(mapping[s1]), Y(mapping[s2])):
holdsAll = False
break
if not holdsAll:
break
if holdsAll:
return True
return False
def getAllPairings(S):
# from https://stackoverflow.com/a/13020502/13002788
N = len(S)
choice_indices = it.product(*[range(k) for k in reversed(range(1, N, 2))])
for choice in choice_indices:
tmp = S[:]
result = []
for index in choice:
result.append((tmp.pop(0), tmp.pop(index)))
yield result
def getAllConnectedRailwaySystems(dim):
S = list(range(dim))
allLRCGates = getAllLRCGates(S)
allLRCPairings = getAllPairings(allLRCGates)
allConnectedRailwaySystemsDup = []
for LRCPairing in allLRCPairings:
rel = list([(degenerate(g1), degenerate(g2)) for (g1, g2) in LRCPairing])
R = RailwaySystem(rel)
if not R.isConnected():
continue
allConnectedRailwaySystemsDup.append(R)
allConnectedRailwaySystems = allConnectedRailwaySystemsDup.copy()
index = 0
while index < len(allConnectedRailwaySystems) - 1:
R = allConnectedRailwaySystems[index]
while allConnectedRailwaySystems.count(R) > 1:
allConnectedRailwaySystems.remove(R)
index += 1
return allConnectedRailwaySystems
def classifyRailwaySystems(dim):
unclassified = getAllConnectedRailwaySystems(dim)
la = len(unclassified)
result = []
equivalenceClassIndex = -1
while len(unclassified) != 0:
equivalenceClassIndex += 1
print(f"Started collecting railway systems of class {equivalenceClassIndex}")
equivalenceClass = [unclassified[0]]
unclassified.pop(0)
for i, R in enumerate(unclassified):
if R.isIsomorphicTo(equivalenceClass[0]):
lu = len(unclassified)
print(f"{LINE_BEGIN + LINE_CLEAR}> {len(equivalenceClass)} railway systems of class {equivalenceClassIndex} found ({la - lu} / {la} = {round((la - lu) / la * 100, 3)}% of total has been classified)", end='')
equivalenceClass.append(R)
unclassified.pop(i)
result.append(equivalenceClass)
print(f"{LINE_BEGIN + LINE_CLEAR}Finished collecting {len(equivalenceClass)} railway systems of class {equivalenceClassIndex}!\n")
return result
def runClassification(dim, savefile):
assert dim % 2 == 0, "ERROR: Raiway system dimension must be a even number"
print(f"Running classification of {dim}-dimensional railway systems")
print()
tA = time.time()
result = classifyRailwaySystems(dim)
tB = time.time()
tDiff = round(tB - tA, 2)
num = len(result)
print("------------------------------\n")
print(f"All done within {tDiff}s.")
print()
with open(savefile, "w") as file:
file.write(f"Classification of {dim}-dimensional railway systems:\n\n")
for i in range(num):
cl = result[i]
cll = len(result[i])
file.write(f"\nCLASS {i} ({cll})\n")
for j in range(cll):
file.write(str(cl[j]))
if j != cll - 1:
file.write(" ")
else:
file.write("\n")
file.write(f"\n\nNumber of different equivalence classes: {num}\n")
file.write(f"\nTime consumed: {tDiff}s")
print(f"Result saved to {savefile}!")
print()
def main(argv):
opts, _ = getopt.getopt(argv, "d:f:", ["dim=", "dimension=", "file="])
dim = 2
savefile = "./result"
for opt, arg in opts:
if opt in ("-d", "--dim", "--dimension"):
dim = int(arg)
elif opt in ("-f", "--file"):
savefile = arg
runClassification(dim, savefile)
if __name__ == "__main__":
main(sys.argv[1:])