-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwa_adaptive_v12.py
93 lines (77 loc) · 3.53 KB
/
rwa_adaptive_v12.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
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
class rwa_adaptive:
def __init__(self, G, alpha, theta):
self.G = nx.Graph(G)
self.wavelength_number = 0
self.demand_list = []
self.beta = 1
self.wave_color_usage = [0]
self.W = 0 # number of wavelength - color
self.N = 0 # number of fiber - maximun usage of one color
self.wa_block = False # block cause by wavelength assignment
self.cal_init_weight(alpha, theta)
def cal_init_weight(self, alpha, theta):
centrality = nx.degree_centrality(self.G)
for edges in self.G.edges():
self.G.edge[edges[0]][edges[1]]['centrality'] = theta*(centrality[edges[0]] + centrality[edges[1]])
self.G.edge[edges[0]][edges[1]]['weight'] = alpha*1 + self.G.edge[edges[0]][edges[1]]['centrality']
def show(self):
nx.draw(self.G)
plt.show()
def run(self, W, N, demand_list, beta, alpha, theta): # after many experience Beta = 1.3 giving the best result
self.wavelength_number = W * N
self.demand_list = demand_list
self.beta = beta
self.wave_color_usage.extend([0]*self.wavelength_number)
max_usage = 0
self.W = W
self.N = N
for edges in self.G.edges():
self.G.edge[edges[0]][edges[1]]['color'] = [0]*self.W
try:
for i in range(0, len(self.demand_list), 1):
source = int(self.demand_list[i][1])
target = int(self.demand_list[i][2])
path = nx.astar_path(self.G, source, target, rwa_adaptive.dijkstra_heuristic, 'weight')
# assign wavelength path
if not self.wavelength_assignment_firstfit(path, i):
self.wa_block = True
return 1
# update usage and weight
for j in range(0, len(path)-1, 1):
self.G.edge[path[j]][path[j+1]]['usage'] += 1
if self.G.edge[path[j]][path[j+1]]['usage'] >= max_usage:
max_usage = self.G.edge[path[j]][path[j+1]]['usage']
self.G.edge[path[j]][path[j+1]]['weight'] = (alpha*pow(self.beta, self.G.edge[path[j]][path[j+1]]['usage'])
+ self.G.edge[path[j]][path[j+1]]['centrality'])
if self.G.edge[path[j]][path[j + 1]]['usage'] >= self.wavelength_number:
self.G.remove_edge(path[j], path[j + 1])
except nx.NetworkXNoPath:
return 1
except RuntimeError, e:
print e.message
else:
return float(max_usage)/self.wavelength_number
def wavelength_assignment_firstfit(self, demand_path, demand_order):
for choosen_wavelength in range(0, self.W+1, 1):
if choosen_wavelength == self.W:
return False
all_free = True
for i in range(0, len(demand_path)-1, 1):
if self.G.edge[demand_path[i]][demand_path[i+1]]['color'][choosen_wavelength] >= self.N:
all_free = False
break
if not all_free:
continue
else:
for i in range(0, len(demand_path) - 1, 1):
u = demand_path[i]
v = demand_path[i+1]
self.G.edge[u][v]['color'][choosen_wavelength] += 1
break
return True
@staticmethod
def dijkstra_heuristic(u, v):
return 0