Statistics
| Branch: | Revision:

iof-tools / gen_bird_preferences.py @ 62798f6e

History | View | Annotate | Download (4.93 KB)

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3

    
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
#
17
# Copyright (C) 2019  Mattia Milani <mattia.milani@studenti.unitn.it>
18
from argparse import ArgumentParser
19
import networkx as nx
20

    
21
parser = ArgumentParser()
22
parser.add_argument("-w", "--write-to", dest="writeto",
23
                    default="pref.conf", action="store",
24
                    help="Output conf file where the configuration will be written")
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")
30

    
31
args = parser.parse_args()
32

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

    
37
graph = nx.read_graphml(mlgraph)
38

    
39
for node in graph.nodes(data=True):
40
    if "destinations" in node[1].keys():
41
        destinationNode = node
42
        break
43

    
44
maxMrai = 30.0
45

    
46
for edge in graph.edges(data=True):
47
    if edge[2]["mrai1"] > maxMrai:
48
        maxMrai = edge[2]["mrai1"]
49
    elif edge[2]["mrai2"] > maxMrai:
50
        maxMrai = edge[2]["mrai2"]
51

    
52
n1 = str(int(destinationNode[0]) + 1)
53
n2 = str(int(destinationNode[0]) + 2)
54
n3 = str(int(destinationNode[0]) + 3)
55

    
56
graph.add_node(n1, type="M")
57
graph.add_node(n2, type="M")
58
graph.add_node(n3, type="M", destinations=destinationNode[1]["destinations"])
59

    
60
graph.add_edge(n1, destinationNode[0], customer=destinationNode[0], termination1=n1, termination2=destinationNode[0],
61
               mrai1=maxMrai, mrai2=maxMrai, fabrikant_weight=0, type="transit")
62
graph.add_edge(n2, destinationNode[0], customer=destinationNode[0], termination1=n2, termination2=destinationNode[0],
63
               mrai1=maxMrai, mrai2=maxMrai, fabrikant_weight=1, type="transit")
64
graph.add_edge(n3, n1, customer=n3, termination1=n3, termination2=n1,
65
               mrai1=maxMrai, mrai2=maxMrai, type="transit")
66
graph.add_edge(n3, n2, customer=n3, termination1=n3, termination2=n2,
67
               mrai1=maxMrai, mrai2=maxMrai, type="transit")
68

    
69
oldDNode = str(destinationNode[0])
70
del graph.node[destinationNode[0]]["destinations"]
71

    
72
destinationNode = (str(n3), graph.node[n3])
73

    
74
G = nx.DiGraph()
75
for node in graph.nodes(data=True):
76
    G.add_node(str(node[0]))
77

    
78
for edge in graph.edges(data=True):
79
    e1 = str(edge[2]["customer"])
80
    if e1 == str(edge[2]["termination1"]):
81
        e2 = str(edge[2]["termination2"])
82
    else:
83
        e2 = str(edge[2]["termination1"])
84
    if e1 == oldDNode:
85
        e2 = str(edge[2]["termination1"])
86
    if e1 == destinationNode[0]:
87
        e1 = str(edge[2]["termination2"])
88
        e2 = str(edge[2]["termination1"])
89
    G.add_edge(e2, e1)
90

    
91
pathSet = set()
92

    
93
for node in G.nodes():
94
    # print("All paths from " + str(node[0]) + " to " + str(destinationNode[0]))
95
    for path in nx.all_simple_paths(G, destinationNode[0], node[0]):
96
        pathSet.add(tuple(path))
97

    
98
pathlist = sorted(list(pathSet), key=len)
99

    
100
pathPref = {}
101
for path in pathlist:
102
    i = 0
103
    j = 1
104
    pathPref[str(path)] = ""
105
    while j < len(path):
106
        e = (path[i], path[j])
107
        edge = graph.get_edge_data(*e)
108
        if edge == None:
109
            e = (path[j], path[i])
110
            edge = graph.get_edge_data(*e)
111

    
112
        if "fabrikant_weight" in edge:
113
            pathPref[str(path)] += str(edge["fabrikant_weight"])
114
        i += 1
115
        j += 1
116

    
117
for key in pathPref:
118
    if pathPref[key] != '':
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)
123
    else:
124
        pathPref[key] = 10
125

    
126
singlePrefFile = "templates/singlePref.template"
127
prefFile = "templates/prefFile.template"
128

    
129
with open(singlePrefFile, "r") as singlePref:
130
    singlePref_template = singlePref.read()
131
with open(prefFile, "r") as pref:
132
    pref_template = pref.read()
133

    
134
singlePref_list = []
135
for key in pathPref:
136
    path = key.replace('(', '').replace(')', '').replace(',', ' ').replace('\'', '')
137
    pref = pathPref[key]
138
    singlePref_list.append(singlePref_template.format(PATH=path, PREFERENCE=pref))
139

    
140
with open(out, "w") as outFile:
141
    outFile.write(
142
        pref_template.format(PREFERENCES_LIST=''.join(singlePref_list), DEFAULT_PREF="1")
143
    )
144
nx.write_graphml(graph, mlgraph.split('.')[0] + "_2.graphml")