-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-reduce.py
executable file
·260 lines (212 loc) · 6.37 KB
/
process-reduce.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
#!/usr/bin/env python
import sys
#from math import fabs
#
# This program simply represents the identity function.
#
class PROCESS_Reduce_Struct:
def __init__(self, node):
"""@todo: Docstring for __init__
:node: @todo
:returns: @todo
"""
self.outnodes = []
self.id = node
self.previous_rank = 0.0
self.current_rank = -1
def add_out(self, out):
"""@todo: Docstring for add_out
:out: @todo
:returns: @todo
"""
self.outnodes.append(out)
def set_curr_rank(self, cr):
"""@todo: Docstring for set_curr_rank
:cr: @todo
:returns: @todo
"""
self.current_rank = cr
def set_prev_rank(self, pr):
"""@todo: Docstring for set_prev_rank
:pr: @todo
:returns: @todo
"""
self.previous_rank = pr
def __str__(self):
"""@todo: Docstring for __str__
:returns: @todo
"""
result = "NodeId:" + str(self.id) + '\t' + str(self.current_rank) + \
',' + str(self.previous_rank)
if len(self.outnodes) == 0:
return result + '\n'
else:
for s in self.outnodes:
result += ',' + str(s)
return result + '\n'
def stoppingCriterion(minimum_digress, maximum, count_iteration):
"""@todo: Docstring for stoppingCriterion
:minimum_digress: @todo
:maximum: @todo
:returns: @todo
"""
if count_iteration >= 200:
return True
if (minimum_digress / maximum < .01):
return True
else:
return False
def fabs(f):
if f < 0:
return -f
else:
return f
def process_processMap(line):
"""@todo: Docstring for processMap
:line: @todo
:returns: @todo
"""
line = line.strip()
line = line.split('\t')
return line
def calculate_digression(list_of_structs):
"""@todo: Docstring for calculate_digression
:list_of_structs: @todo
:returns: @todo
"""
min_d = 0
for struct in list_of_structs:
if fabs(struct.current_rank - struct.previous_rank) > min_d:
min_d = fabs(struct.current_rank - struct.previous_rank)
return min_d
def calculate_maximum(list_of_structs):
"""@todo: Docstring for calculate_maximum
:list_of_structs: @todo
:returns: @todo
"""
maximum = 0
for struct in list_of_structs:
if struct.current_rank > maximum:
maximum = struct.current_rank
return maximum
def findTenBiggest(list_of_structs):
"""@todo: Docstring for findTenBiggest
:list_of_structs: @todo
:returns: @todo
"""
found_indices = [[0,0] for i in range(10)]
for st in list_of_structs:
if (st.current_rank > found_indices[0][1]):
found_indices[9] = found_indices[8]
found_indices[8] = found_indices[7]
found_indices[7] = found_indices[6]
found_indices[6] = found_indices[5]
found_indices[5] = found_indices[4]
found_indices[4] = found_indices[3]
found_indices[3] = found_indices[2]
found_indices[2] = found_indices[1]
found_indices[1] = found_indices[0]
found_indices[0] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[1][1]):
found_indices[9] = found_indices[8]
found_indices[8] = found_indices[7]
found_indices[7] = found_indices[6]
found_indices[6] = found_indices[5]
found_indices[5] = found_indices[4]
found_indices[4] = found_indices[3]
found_indices[3] = found_indices[2]
found_indices[2] = found_indices[1]
found_indices[1] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[2][1]):
found_indices[9] = found_indices[8]
found_indices[8] = found_indices[7]
found_indices[7] = found_indices[6]
found_indices[6] = found_indices[5]
found_indices[5] = found_indices[4]
found_indices[4] = found_indices[3]
found_indices[3] = found_indices[2]
found_indices[2] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[3][1]):
found_indices[9] = found_indices[8]
found_indices[8] = found_indices[7]
found_indices[7] = found_indices[6]
found_indices[6] = found_indices[5]
found_indices[5] = found_indices[4]
found_indices[4] = found_indices[3]
found_indices[3] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[4][1]):
found_indices[9] = found_indices[8]
found_indices[8] = found_indices[7]
found_indices[7] = found_indices[6]
found_indices[6] = found_indices[5]
found_indices[5] = found_indices[4]
found_indices[4] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[5][1]):
found_indices[9] = found_indices[8]
found_indices[8] = found_indices[7]
found_indices[7] = found_indices[6]
found_indices[6] = found_indices[5]
found_indices[5] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[6][1]):
found_indices[9] = found_indices[8]
found_indices[8] = found_indices[7]
found_indices[7] = found_indices[6]
found_indices[6] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[7][1]):
found_indices[9] = found_indices[8]
found_indices[8] = found_indices[7]
found_indices[7] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[8][1]):
found_indices[9] = found_indices[8]
found_indices[8] = [st.id, st.current_rank]
continue
if (st.current_rank > found_indices[9][1]):
found_indices[9] = [st.id, st.current_rank]
continue
return found_indices
last_node = -1
list_of_structs = []
counter = -1
list_to_write = []
count_iteration = 0
for line in sys.stdin:
line = process_processMap(line)
if (int(line[0]) == -10):
count_iteration = int(line[1])
continue
if line[0] != last_node:
if last_node != -1:
list_to_write.append(str(list_of_structs[counter]))
list_of_structs.append(PROCESS_Reduce_Struct(line[0]))
counter += 1
last_node = line[0]
if len(line) == 2:
list_of_structs[counter].add_out(int(line[1]))
else:
if line[2] == '!':
list_of_structs[counter].set_prev_rank(float(line[1]))
if line[2] == '#':
list_of_structs[counter].set_curr_rank(float(line[1]))
list_to_write.append(str(list_of_structs[counter]))
list_to_write.append("ITERATION" + '\t' + str(count_iteration + 1) + '\n')
minimum_digress = calculate_digression(list_of_structs)
maximum = calculate_maximum(list_of_structs)
if stoppingCriterion(minimum_digress, maximum, count_iteration):
top_ten = findTenBiggest(list_of_structs)
for i in range(10):
sys.stdout.write("FinalRank:" + str(top_ten[i][1]) + '\t' + str(top_ten[i][0]) + '\n')
#sys.stdout.write("minimum_digress = " + str(minimum_digress) + '\n')
#sys.stdout.write("iteration = " + str(count_iteration) + '\n')
#sys.exit(33)
else:
for u in list_to_write:
sys.stdout.write(u)
#sys.exit(0)