Revision 62798f6e mrai_setter/mrai_setter.py

View differences:

mrai_setter/mrai_setter.py
62 62
def apply_fabrikant_strategy(G, adv_node):
63 63
    """ set mrai timers according to the Fabrikant gadget paper """
64 64
    fl = fabrikant_levels(G, adv_node)
65
    print(fl)
65 66
    for i in G.nodes:
66 67
        if i in fl:
67 68
            set_node_mrai(G, i, default_mrai/(2**fl[i]))
......
162 163
    Graph is split in three logic parts. """
163 164
    T = default_mrai  # max mrai in seconds
164 165
    cent = mice.mice_centrality(G, normalized=True)
165

  
166 166
    visited_nodes = set()
167 167
    set_node_mrai(G, adv_node, T*cent[adv_node]/2)
168 168
    fifo = set()
......
241 241
            e['mrai1'] = float(mrai)
242 242
            e['termination2'] = j
243 243

  
244
"""for node in self.graph.nodes(data=True):
245
                self.G.add_node(node[0])
246

  
247
            for edge in self.graph.edges(data=True):
248
                e1 = str(edge[2]["customer"])
249
                if e1 == str(edge[2]["termination1"]):
250
                    e2 = str(edge[2]["termination2"])
251
                else:
252
                    e2 = str(edge[2]["termination1"])
253
                if 'fabrikant_weight' in edge[2]:
254
                    self.G.add_edge(e1, e2, customer=edge[2]['customer'], termination2=edge[2]['termination2'],
255
                               fabrikant_weight=edge[2]['fabrikant_weight'], termination1=edge[2]['termination1'],
256
                               mrai1=edge[2]['mrai1'], mrai2=edge[2]['mrai2'], type=edge[2]['type'])
257
                else:
258
                    self.G.add_edge(e1, e2, customer=edge[2]['customer'], termination2=edge[2]['termination2'],
259
                               termination1=edge[2]['termination1'], mrai1=edge[2]['mrai1'], mrai2=edge[2]['mrai2'],
260
                               type=edge[2]['type'])"""
261

  
244 262

  
245 263
def fabrikant_levels(G, adv_node):
246 264
    class FabrikantLeveler(object):
......
274 292
                i = to_explore.pop()
275 293
                for j in nx.neighbors(G, i):
276 294
                    e = G.edges[(i,j)]
277
                    if e['type'] == 'transit' and str(e['customer']) == j:
295
                    if e['type'] == 'transit' and str(e['customer']) == i:
278 296
                        if j not in self.level:
279 297
                            self.level[j] = self.level[i] + 1
280 298
                        else:
......
285 303
                        self.sub_nodes[j] = self.sub_nodes[j].union(self.sub_nodes[i])
286 304
                        self.sub_nodes[j].add(i)
287 305
                        to_explore.add(j)  # there cannot be customer-provider loops
288

  
289
            explored.add(i)
306
                explored.add(i)
290 307

  
291 308
    fl = FabrikantLeveler(G)
292 309
    return fl.levels(adv_node)
......
305 322

  
306 323
def adapt_to_mean(G, expected_mean):
307 324
    mean = 0.0
308
    n_elements = len(G.edges)*2
325
    n_elements = 0
309 326
    for e in G.edges(data=True):
310
        mean += e[2]['mrai1'] + e[2]['mrai2']
327
        if e[2]['mrai1'] != 0.0:
328
            mean += e[2]['mrai1']
329
            n_elements += 1
330
        if e[2]['mrai2'] != 0.0:
331
            mean += e[2]['mrai2']
332
            n_elements += 1
311 333
    mean /= n_elements
312 334

  
313 335
    multiplier = round(float(expected_mean) / mean, 2)
......
317 339

  
318 340

  
319 341
if __name__ == "__main__":
320
    if len(sys.argv) in list(range(5, 6)):
342
    if len(sys.argv) in list(range(5, 7)):
321 343
        filename = sys.argv[1]
322 344
        strategy = sys.argv[2]
323 345
        outDir = sys.argv[3]

Also available in: Unified diff