-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
374 lines (307 loc) · 10.3 KB
/
Main.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import networkx as nx
ninserted = 0
nremoved = 0
class Label:
def __init__(self, node, prevnode=None):
self.node = node
self.summ = {}
self.mini = {}
self.awsum = 0
self.awmax = float('inf')
self.awdif = -float('inf')
self.awint = -float('inf')
self.hidden = False
self.prevnode = prevnode
def aggregate(avgs, iavgs, label):
label.awsum = 0
for key, val in label.summ.items():
label.awsum += val / avgs[key]
label.awmax = 0
for key, val in label.mini.items():
label.awmax += val / avgs[key]
label.awdif = label.awsum - label.awmax
label.awint = 0
for key, val in label.summ.items():
label.awint += int(val * iavgs[key])
for key, val in label.mini.items():
label.awint -= int(val * iavgs[key])
# print('aggregate', label.awdif)
def better_than(la, lb):
# print(la.prevnode, '->', la.node, ' (', la.awint, ') // ', lb.prevnode, '->', lb.node, ' (', lb.awint, ')', sep='')
return not (la.awint < lb.awint)
def best_label(tempLabels):
best = 0
# print(tempLabels[0].node, '->', tempLabels[0].prevnode)
for i in range(1, len(tempLabels)):
if better_than(tempLabels[best], tempLabels[i]):
best = i
# print('Best label is', tempLabels[best].prevnode, '->', tempLabels[best].node, 'with awdif', tempLabels[best].awdif)
return best
def dominates(la, lb):
# For better code readability, in this function it's
# better to have many check variables, one for each
# case
# 0 -> la dominates lb
# 1 -> lb dominates la
# 2 -> la dominates lb, but lb is to be kept
# 3 -> lb dominates la, but la is to be kept
# 4 -> they're equal and both are to be kept
# 5 -> they refer to the same arc
# First we check if the labels are equal
# checkEq = check equality
# checkSEq = check sum equality (it's used later)
if la.prevnode == lb.prevnode and la.node == lb.node:
return 5
checkEq = 1
checkSEq = 1
for key in la.summ:
if la.summ[key] != lb.summ[key]:
checkEq = 0
checkSEq = 0
break
if checkEq == 1:
for key in la.mini:
if la.mini[key] != lb.mini[key]:
checkEq = 0
break
# checkEq=1 at this point means they're equal
if checkEq == 1:
# print('EQUAL')
return 4
# checkSLEq = check sum lower equality
checkSLEq = 1
for key in la.summ:
if la.summ[key] > lb.summ[key]:
checkSLEq = 0
break
# checkMGEq = check max greater equality
checkMGEq = 1
for key in la.mini:
if la.mini[key] < lb.mini[key]:
checkMGEq = 0
break
if checkSLEq == 1 and checkMGEq == 1:
# This check is for finding the maximal complete set,
# you can comment it, leaving only the 'return 0',
# if you want only the minimal complete set
if checkSEq == 1:
return 3
return 1
# checkSGEq = check sum greater equality
checkSGEq = 1
for key in la.summ:
if la.summ[key] < lb.summ[key]:
checkSGEq = 0
break
# checkMLEq = check max lower equality
checkMLEq = 1
for key in la.mini:
if la.mini[key] > lb.mini[key]:
checkMLEq = 0
break
if checkSGEq == 1 and checkMLEq == 1:
# Again, this is for finding the maximal complete set
if checkSEq == 1:
return 2
return 0
# print ('NO RELATION')
return 4
def try_insert(permLabels, tempLabels, label):
global ninserted, nremoved
# print('Trying to insert', label.prevnode, '->', label.node)
for perm in permLabels:
if perm.hidden:
# Do not check this one as there's a
# better one in the list
continue
else:
dominance = dominates(label, perm)
if dominance == 1:
# The new label is dominated, drop it
# print(' It\'s dominated')
return 0
elif dominance == 3:
# Accept new label as hidden
label.hidden = True
elif dominance == 5:
# print("It's equal, ignore")
return 0
inserted = False
rem = []
for i in range(len(tempLabels)):
dominance = dominates(label, tempLabels[i])
if dominance == 0:
# tempLabel[i] is dominated by the new label,
# so it can be simply replaced
if not inserted:
# print('DOMINATES')
tempLabels[i] = label
# ninserted += 1
# print('Inserted A', ninserted)
# nremoved += 1
# print('Removed A', nremoved)
inserted = True
else:
rem.append(i)
elif dominance == 1:
# print(' It\'s dominated')
# The new label is dominated
return 0
elif dominance == 2:
tempLabels[i].hidden = True
if not inserted:
tempLabels.insert(i, label)
# ninserted += 1
# print('Inserted B', ninserted)
inserted = True
elif dominance == 3:
label.hidden = True
# Insert label later
elif dominance == 4:
if not inserted:
if better_than(label, tempLabels[i]) == 0:
# ninserted += 1
# print('Inserted C', ninserted)
# print('Sum:')
# for k in label.summ.keys():
# print(' ', label.summ[k], '<=>', tempLabels[i].summ[k])
# print('Minmax:')
# for k in label.mini.keys():
# print(' ', label.mini[k], '<=>', tempLabels[i].mini[k])
tempLabels.insert(i, label)
inserted = True
elif dominance == 5:
# There's already a permanent label for this arc
return 0
if not inserted:
tempLabels.append(label)
# ninserted += 1
# print('Appended', ninserted)
removed = 0
for i in rem:
# nremoved += 1
# print('Removed B', nremoved)
del tempLabels[i - removed]
removed += 1
def labelling(G, avgs, iavgs, permLabels, tempLabels, current):
for neighbor, val in G[current.node].items():
lab = Label(node=neighbor, prevnode=current.node)
# Attributes to be summed
for key, attr in current.summ.items():
lab.summ[key] = attr + val[key]
# print(attr)
# Attributes to be minimized
for key, attr in current.mini.items():
lab.mini[key] = min(attr, val[key])
# print(attr)
aggregate(avgs, iavgs, lab)
try_insert(permLabels, tempLabels, lab)
def mosp(G, s, sum_attr, min_attr, narcs):
global nremoved
avgs = {}
iavgs = {}
permLabels = []
tempLabels = []
start = Label(node=s)
start.prevnode = s
for i in sum_attr:
start.summ[i] = 0
for i in min_attr:
start.mini[i] = float('inf')
permLabels.append(start)
for u, v, data in G.edges().data():
for key, val in data.items():
if key not in avgs:
avgs[key] = val
else:
avgs[key] += val
wmax = -float('inf')
for k in avgs.keys():
avgs[k] = avgs[k] / narcs
if avgs[k] > wmax:
wmax = avgs[k]
# print(wmax)
# print(avgs)
EPS = 0.0001
for k in avgs.keys():
iavgs[k] = int(wmax/avgs[k] + 0.5 + EPS)
if iavgs[k] < 1:
iavgs[k] = 1
# print(iavgs)
# exit()
current = start
empty = False
lsize = 0
while not empty:
# print(current.node)
# n = len(tempLabels)
# if lsize > n:
# print('REDUCED', n)
# elif lsize < n:
# print('INCREASED', n)
# else:
# print('UNCHANGED')
# lsize = n
labelling(G, avgs, iavgs, permLabels, tempLabels, current)
if len(tempLabels) == 0:
empty = True
else:
best = best_label(tempLabels)
# nremoved += 1
# print('Removed', nremoved)
current = tempLabels.pop(best)
permLabels.append(current)
# print(current.prevnode, '->', current.node)
# print(' ', current.summ)
# print(' ', current.mini)
# print(' ', current.awint)
# print(len(permLabels))
return permLabels
if __name__ == '__main__':
G = nx.DiGraph()
f = open('grafo_200_20_1_2_2_0.txt', 'r')
snode = 0
enode = 199
nsum = int(f.readline())
nmin = int(f.readline())
nnodes = int(f.readline())
narcs = int(f.readline()) # Ignore this line (it's the number of arcs)
G.add_nodes_from([i for i in range(nnodes)])
# print(G.nodes())
# Read the first matrix in the file,
# containing the edges without any attributes
for i in range(nnodes):
line = f.readline().strip().split(' ')
for j in range(len(line)):
if line[j] == '1':
G.add_edge(i, j)
# Read the matrices containing the values
# of the sum attributes
for k in range(nsum):
# Skip two blank lines
f.readline()
f.readline()
for i in range(nnodes):
line = f.readline().strip().split(' ')
for j in range(len(line)):
if line[j] != '0':
G[i][j][str(k)] = int(line[j])
# Read the matrices containing the values
# of the minmax attributes
for k in range(nmin):
f.readline()
f.readline()
for i in range(nnodes):
line = f.readline().strip().split(' ')
for j in range(len(line)):
if line[j] != '0':
G[i][j][str(nsum + k)] = int(line[j])
# print(G[i][j][str(nsum + k)])
f.close()
perm = mosp(G, snode, [str(i) for i in range(nsum)], [str(nsum + i) for i in range(nmin)], narcs)
for label in perm:
print (label.prevnode, '->', label.node)
for k, v in label.summ.items():
print(' ', k, ' = ', v)
for k, v in label.mini.items():
print(' ', k, ' = ', v)