This repository was archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_insertion_xml_sorted.py
More file actions
208 lines (184 loc) · 6.66 KB
/
node_insertion_xml_sorted.py
File metadata and controls
208 lines (184 loc) · 6.66 KB
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
from math import sqrt
from platform import node
from turtle import pos
import numpy
import xmltodict
import json
import pandas
import time
import matplotlib.pyplot as plt
from tqdm import tqdm
from functools import cache, lru_cache
x_coord = "@xCoord"
y_coord = "@yCoord"
def n2p_to_json(filename):
"""
Convert n2p file (xml) to json data
Also creating json dump file
"""
with open(filename) as f:
n2p_dict = xmltodict.parse(f.read())
f.close()
json_data = json.dumps(n2p_dict)
with open(f"{filename}.json", "w") as f:
f.write(json_data)
f.close()
return n2p_dict
def node_extractor(n2p_dict):
"""
Extract node information from n2p json data
Return a list of dicts, each dict has these keys:
@id, @xCoord, @yCoord, @name
"""
try:
nodes = n2p_dict["network"]["node"]
except KeyError as e:
try:
nodes = n2p_dict["network"]["physicalTopology"]["node"]
except Exception as e:
print(e)
return nodes
def calc_centroid(nodes_list):
"""
Return tuple (x, y) as average coordinate from the list of nodes
"""
x_sum = 0
y_sum = 0
for node in nodes_list:
y_sum += float(node[x_coord])
x_sum += float(node[y_coord])
return (x_sum/len(nodes_list),y_sum/len(nodes_list))
def distance_sorter(nodes, center=None, sort=None, reverse=None):
"""
Sort nodes according to its distance to the center.
Return list of string of node names.
Do no sorting when sort == False.
When sort == True, return sorted list of nodes
from 'furthest' to the 'nearest' nodes to center,
vice versa.
"""
if sort == None:
sort = True
if reverse == None:
reverse = True
if sort:
node_distance = []
for node in nodes:
print(node) if debug else None
print(node["@id"]) if debug else None
x = float(node[x_coord])
y = float(node[y_coord])
dis = sqrt((x-center[0])**2 +(y-center[1])**2)
node_distance.append((node["@id"], dis))
node_distance.sort(key=lambda x:x[1],reverse = reverse)
sorted_node = [node for (node,distance) in node_distance]
else:
sorted_node = [node["@id"] for node in nodes]
return sorted_node
def gen_cost_matrix(nodes):
# prepare for the costs matrix with empty array.
costs = numpy.empty([len(nodes), len(nodes)])
i = 0
for node1 in nodes:
j = 0
for node2 in nodes:
x1 = float(node1[x_coord])
y1 = float(node1[y_coord])
x2 = float(node2[x_coord])
y2 = float(node2[y_coord])
# cost = euclidian distance between two nodes
dis = sqrt((x1-x2)**2 +(y1-y2)**2)
costs[i][j] = dis
j+=1
i+=1
return costs
# def node_insertion(size,nodes,df):
def node_insertion(size,center,sort=None,reverse=None):
# sort node according to the distance from center
nodes_sliced = nodes[:size]
sorted_node = distance_sorter(nodes=nodes_sliced, center=center, sort=sort, reverse=reverse)
new_node = sorted_node[-1] # last item
if size==1:
return 0, [new_node]
# stop function
else:
# recursive call, should be in cache
last_cost, current_string = node_insertion(size-1,center)
print("-----------")
print("size", size)
# generating possible rings
try:
del min_cost
except Exception:
pass
for i in range (1,size):
possible_ring = current_string[:]
print("pre", possible_ring)
possible_ring.insert(i,str(new_node))
print("inserted", possible_ring)
j = (i-1) % len(possible_ring)
k = (i+1) % len(possible_ring)
new_node = int(possible_ring[i])
pre_node = int(possible_ring[j])
post_node = int(possible_ring[k])
print("neigh", pre_node,new_node,post_node)
cost = last_cost - df[pre_node][post_node] + df[new_node][pre_node] + df[new_node][post_node]
print("last_cost", last_cost)
print("cost", cost)
input()
try:
if cost<min_cost:
min_cost = cost
min_str = possible_ring
print()
except Exception:
# set the minimum cost = current cost
# if the min_cost variable doesn't exist
# (for the first comparison, instead of
# assigning min_cost = inf befor the first loop)
min_cost = cost
min_str = possible_ring
# preparing for next iteration
current_string = min_str
# reporting
print("return", size, min_cost, min_str)
return min_cost, min_str
def main():
# set debug = True to print a bunch of detailed information
global debug
debug = False
# choose the file to be read here
# filename = 'ATTWorldNet_N90_E274.n2p'
filename = '20 node.n2p'
# open the n2p file that contains geographical locations
n2p_dict = n2p_to_json(filename=filename)
# extract only the relevant info
global nodes
nodes = node_extractor(n2p_dict=n2p_dict)
# calculate distance between every two nodes, so #nodes**2 calculation.
costs_matrix = gen_cost_matrix(nodes=nodes)
# print the costs matrix
global df
df = pandas.DataFrame(costs_matrix, columns=[x for x in range(0,len(nodes))])
df_rounded = df.round(decimals=3)
print("Costs matrix (Euclidian distance):\n", df_rounded,"\n")
print("--------------------------")
input("Press enter to start the algorithm:\n")
result_lst = []
node_list = distance_sorter(nodes, center=None, sort=False)
# calculate center of the nodes
center = calc_centroid(nodes_list=nodes)
# for size in tqdm(range(1,len(nodes)+1)):
# for size in tqdm(range(1,len(node_list)+1)):
for size in range(1,len(node_list)+1):
start_time = time.perf_counter()
min_cost, min_str = node_insertion(size=size,center=center,sort=False,reverse=False)
duration = time.perf_counter() - start_time
result = {"size": size, "min_cost": min_cost, "min_str":min_str, "duration": duration}
result_lst.append(result)
for result in result_lst:
print(result["size"], result["min_cost"], result["duration"])
if __name__ == "__main__":
main()
# function (size, list nodes, cost matrix)
# return cost, ring