Revision 62798f6e

View differences:

Rscripts/centralityPlotter.R
1
# set the environment
2
setwd("~/src/iof-tools/Rscripts")
3

  
4
library(dplyr)
5
library(ggplot2)
6
library(Hmisc)
7

  
8
source("functions.R")
9

  
10
args <- commandArgs(trailingOnly = TRUE)
11

  
12
file <- "centrality.csv"
13
outFile <- "outCentrality"
14

  
15
csv1 <- read.csv(file, header = T, sep=" ")
16

  
17
plot(ecdf(csv1$centrality),
18
     main="CDF for node centrality",
19
     xlab="centrality",
20
     ylab="CDF")
chain_gadget.py
72 72
        }
73 73
        nx.set_edge_attributes(g, attrs)
74 74
    if weight:
75
        for i in range(2, total_nodes, 2):
75
        for i in range(0, total_nodes, 2):
76 76
            attrs = {
77 77
                (i+id_delta, 0+id_delta): {
78
                    ATTR_EDGE_WEIGHT: i//2-1
78
                    ATTR_EDGE_WEIGHT: i//2
79
                },
80
                (i + id_delta, i + 1 + id_delta): {
81
                    ATTR_EDGE_WEIGHT: 0
82
                },
83
                (i + id_delta, i + 2 + id_delta): {
84
                    ATTR_EDGE_WEIGHT: 1
79 85
                }
80 86
            }
81 87
            nx.set_edge_attributes(g, attrs)
gen_bird_preferences.py
23 23
                    default="pref.conf", action="store",
24 24
                    help="Output conf file where the configuration will be written")
25 25
parser.add_argument("-g", "--graph", dest="graph", required=True, type=str, action="store")
26
parser.add_argument("-o", "--outer", dest="outer", default=False,
27
                    action="store_true", help="signal the presence of inner nodes")
28
parser.add_argument("-o", "--outer", dest="outer", default=False,
29
                    action="store_true", help="signal the presence of inner nodes")
26 30

  
27 31
args = parser.parse_args()
28 32

  
29 33
mlgraph = args.graph
30 34
out = args.writeto
35
outer = args.outer
31 36

  
32 37
graph = nx.read_graphml(mlgraph)
33 38

  
......
111 116

  
112 117
for key in pathPref:
113 118
    if pathPref[key] != '':
114
        pathPref[key] = 100 + int(pathPref[key].zfill(32), 2)
119
        if not outer:
120
            pathPref[key] = 100 + int(pathPref[key].zfill(32), 2)
121
        else:
122
            pathPref[key] = 100 + int(pathPref[key].zfill(3)[::-1], 3)
115 123
    else:
116 124
        pathPref[key] = 10
117 125

  
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]
mrai_setter/test_mrai_setter.py
12 12
    @classmethod
13 13
    def setup_class(cls):
14 14
        cls.small_saw = nx.Graph()
15
        cls.small_saw.add_edge('x1', 'x2', type='transit', customer='x2')
16
        cls.small_saw.add_edge('x2', 'x3', type='transit', customer='x3')
15
        cls.small_saw.add_edge('x1', 'x2', type='transit', customer='x1')
16
        cls.small_saw.add_edge('x2', 'x3', type='transit', customer='x2')
17 17

  
18
        cls.small_saw.add_edge('x1', 'y1', type='transit', customer='y1')
19
        cls.small_saw.add_edge('y1', 'x2', type='transit', customer='x2')
20
        cls.small_saw.add_edge('x2', 'y2', type='transit', customer='y2')
21
        cls.small_saw.add_edge('y2', 'x3', type='transit', customer='x3')
18
        cls.small_saw.add_edge('x1', 'y1', type='transit', customer='x1')
19
        cls.small_saw.add_edge('y1', 'x2', type='transit', customer='y1')
20
        cls.small_saw.add_edge('x2', 'y2', type='transit', customer='x2')
21
        cls.small_saw.add_edge('y2', 'x3', type='transit', customer='y2')
22 22

  
23 23
        cls.saw = nx.Graph()
24
        cls.saw.add_edge('x1', 'x2', type='transit', customer='x2')
25
        cls.saw.add_edge('x2', 'x3', type='transit', customer='x3')
26
        cls.saw.add_edge('x3', 'x4', type='transit', customer='x4')
24
        cls.saw.add_edge('x1', 'x2', type='transit', customer='x1')
25
        cls.saw.add_edge('x2', 'x3', type='transit', customer='x2')
26
        cls.saw.add_edge('x3', 'x4', type='transit', customer='x3')
27 27

  
28
        cls.saw.add_edge('x1', 'y1', type='transit', customer='y1')
29
        cls.saw.add_edge('y1', 'x2', type='transit', customer='x2')
30
        cls.saw.add_edge('x2', 'y2', type='transit', customer='y2')
31
        cls.saw.add_edge('y2', 'x3', type='transit', customer='x3')
32
        cls.saw.add_edge('x3', 'y3', type='transit', customer='y3')
33
        cls.saw.add_edge('y3', 'x4', type='transit', customer='x4')
28
        cls.saw.add_edge('x1', 'y1', type='transit', customer='x1')
29
        cls.saw.add_edge('y1', 'x2', type='transit', customer='y1')
30
        cls.saw.add_edge('x2', 'y2', type='transit', customer='x2')
31
        cls.saw.add_edge('y2', 'x3', type='transit', customer='y2')
32
        cls.saw.add_edge('x3', 'y3', type='transit', customer='x3')
33
        cls.saw.add_edge('y3', 'x4', type='transit', customer='y3')
34 34

  
35 35
        cls.chain = nx.Graph()
36
        cls.chain.add_edge('x1', 'x2', type='transit', customer='x2')
37
        cls.chain.add_edge('x2', 'x3', type='transit', customer='x3')
38
        cls.chain.add_edge('x3', 'x4', type='transit', customer='x4')
39

  
40
        cls.chain.add_edge('x1', 'y12', type='transit', customer='y12')
41
        cls.chain.add_edge('y12', 'y11', type='transit', customer='y11')
42
        cls.chain.add_edge('y11', 'y10', type='transit', customer='y10')
43
        cls.chain.add_edge('y10', 'x2', type='transit', customer='x2')
44

  
45
        cls.chain.add_edge('x1', 'z13', type='transit', customer='z13')
46
        cls.chain.add_edge('z13', 'y12', type='transit', customer='y12')
47
        cls.chain.add_edge('y12', 'z12', type='transit', customer='z12')
48
        cls.chain.add_edge('z12', 'y11', type='transit', customer='y11')
49
        cls.chain.add_edge('y11', 'z11', type='transit', customer='z11')
50
        cls.chain.add_edge('z11', 'y10', type='transit', customer='y10')
51
        cls.chain.add_edge('y10', 'z10', type='transit', customer='z10')
52
        cls.chain.add_edge('z10', 'x2', type='transit', customer='x2')
53

  
54
        cls.chain.add_edge('x2', 'y22', type='transit', customer='y22')
55
        cls.chain.add_edge('y22', 'y21', type='transit', customer='y21')
56
        cls.chain.add_edge('y21', 'y20', type='transit', customer='y20')
57
        cls.chain.add_edge('y20', 'x3', type='transit', customer='x3')
58

  
59
        cls.chain.add_edge('x2', 'z23', type='transit', customer='z23')
60
        cls.chain.add_edge('z23', 'y22', type='transit', customer='y22')
61
        cls.chain.add_edge('y22', 'z22', type='transit', customer='z22')
62
        cls.chain.add_edge('z22', 'y21', type='transit', customer='y21')
63
        cls.chain.add_edge('y21', 'z21', type='transit', customer='z21')
64
        cls.chain.add_edge('z21', 'y20', type='transit', customer='y20')
65
        cls.chain.add_edge('y20', 'z20', type='transit', customer='z20')
66
        cls.chain.add_edge('z20', 'x3', type='transit', customer='x3')
67

  
68
        cls.chain.add_edge('x3', 'y32', type='transit', customer='y32')
69
        cls.chain.add_edge('y32', 'y31', type='transit', customer='y31')
70
        cls.chain.add_edge('y31', 'y30', type='transit', customer='y30')
71
        cls.chain.add_edge('y30', 'x4', type='transit', customer='x4')
72

  
73
        cls.chain.add_edge('x3', 'z33', type='transit', customer='z33')
74
        cls.chain.add_edge('z33', 'y32', type='transit', customer='y32')
75
        cls.chain.add_edge('y32', 'z32', type='transit', customer='z32')
76
        cls.chain.add_edge('z32', 'y31', type='transit', customer='y31')
77
        cls.chain.add_edge('y31', 'z31', type='transit', customer='z31')
78
        cls.chain.add_edge('z31', 'y30', type='transit', customer='y30')
79
        cls.chain.add_edge('y30', 'z30', type='transit', customer='z30')
80
        cls.chain.add_edge('z30', 'x4', type='transit', customer='x4')
36
        cls.chain.add_edge('x1', 'x2', type='transit', customer='x1')
37
        cls.chain.add_edge('x2', 'x3', type='transit', customer='x2')
38
        cls.chain.add_edge('x3', 'x4', type='transit', customer='x3')
39

  
40
        cls.chain.add_edge('x1', 'y12', type='transit', customer='x1')
41
        cls.chain.add_edge('y12', 'y11', type='transit', customer='y12')
42
        cls.chain.add_edge('y11', 'y10', type='transit', customer='y11')
43
        cls.chain.add_edge('y10', 'x2', type='transit', customer='y10')
44

  
45
        cls.chain.add_edge('x1', 'z13', type='transit', customer='x1')
46
        cls.chain.add_edge('z13', 'y12', type='transit', customer='z13')
47
        cls.chain.add_edge('y12', 'z12', type='transit', customer='y12')
48
        cls.chain.add_edge('z12', 'y11', type='transit', customer='z12')
49
        cls.chain.add_edge('y11', 'z11', type='transit', customer='y11')
50
        cls.chain.add_edge('z11', 'y10', type='transit', customer='z11')
51
        cls.chain.add_edge('y10', 'z10', type='transit', customer='y10')
52
        cls.chain.add_edge('z10', 'x2', type='transit', customer='z10')
53

  
54
        cls.chain.add_edge('x2', 'y22', type='transit', customer='x2')
55
        cls.chain.add_edge('y22', 'y21', type='transit', customer='y22')
56
        cls.chain.add_edge('y21', 'y20', type='transit', customer='y21')
57
        cls.chain.add_edge('y20', 'x3', type='transit', customer='y20')
58

  
59
        cls.chain.add_edge('x2', 'z23', type='transit', customer='x2')
60
        cls.chain.add_edge('z23', 'y22', type='transit', customer='z23')
61
        cls.chain.add_edge('y22', 'z22', type='transit', customer='y22')
62
        cls.chain.add_edge('z22', 'y21', type='transit', customer='z22')
63
        cls.chain.add_edge('y21', 'z21', type='transit', customer='y21')
64
        cls.chain.add_edge('z21', 'y20', type='transit', customer='z21')
65
        cls.chain.add_edge('y20', 'z20', type='transit', customer='y20')
66
        cls.chain.add_edge('z20', 'x3', type='transit', customer='z20')
67

  
68
        cls.chain.add_edge('x3', 'y32', type='transit', customer='x3')
69
        cls.chain.add_edge('y32', 'y31', type='transit', customer='y32')
70
        cls.chain.add_edge('y31', 'y30', type='transit', customer='y31')
71
        cls.chain.add_edge('y30', 'x4', type='transit', customer='y30')
72

  
73
        cls.chain.add_edge('x3', 'z33', type='transit', customer='x3')
74
        cls.chain.add_edge('z33', 'y32', type='transit', customer='z33')
75
        cls.chain.add_edge('y32', 'z32', type='transit', customer='y32')
76
        cls.chain.add_edge('z32', 'y31', type='transit', customer='z32')
77
        cls.chain.add_edge('y31', 'z31', type='transit', customer='y31')
78
        cls.chain.add_edge('z31', 'y30', type='transit', customer='z31')
79
        cls.chain.add_edge('y30', 'z30', type='transit', customer='y30')
80
        cls.chain.add_edge('z30', 'x4', type='transit', customer='z30')
81 81

  
82 82
    def test_fabrikant_levels(self):
83 83
        levels = pg.fabrikant_levels(self.small_saw, 'x1')

Also available in: Unified diff