/
DBCP.py
217 lines (188 loc) · 6.46 KB
/
DBCP.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
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
209
210
211
212
213
214
215
216
217
import networkx as nx
import matplotlib.pyplot as plt
import time
cc_map = [
'red',
'darkblue',
'darkcyan',
'darkred',
'black',
'darkmagenta',
'brown', #dark yellow
'blue',
'green',
'cyan',
'darkgreen',
'm',
'brown', #dark yellow
'blue',
'green',
'cyan',
'black',
'm'
]
#remove the single nodes in a graph.
def Remove_Single_Node(graph):
for node in graph.nodes():
if nx.is_isolate(graph,node):
graph.remove_node(node)
return graph
#given a graph location to read the graph, graphml style only.
def Read(graph_url):
G = nx.read_graphml(graph_url)
G = Remove_Single_Node(G) #remove the single nodes in a graph.
return G
#given a dc value according to the diameter.
def Find_DC(graph):
dia = nx.diameter(graph)
dc = (dia+2)/3
if dc == 0:
return 1
return dc
#draw the graph after
def Draw_Graph(graph,colors='r',shapes=200):
nx.draw_networkx(graph,node_color=colors,node_size=shapes)
plt.show()
#get the neighbors within the dc value for the given node
def NeighborsWithinDC(node,dc,graph,neighbors=[]):
if len(neighbors) == 0:
neighbors.append(node)
if dc == 0:
return neighbors
dc -= 1
for nd in graph.neighbors(node):
if nd not in neighbors:
neighbors.append(nd)
neighbors = NeighborsWithinDC(nd,dc,graph,neighbors)
return neighbors
#get the density for the given node
def Density(node,dc,graph):
li = NeighborsWithinDC(node,dc,graph,[])
li.remove(node)
return len(li)
#find the most close node with higher density
def Find_Close_Higher_Node(node,densities,graph,uplevenodes=[],distance=0):
hnode = ''
hden = densities[node]
distance +=1
if uplevenodes==[]:
downlevenodes = graph.neighbors(node)
for nd in downlevenodes:
if densities[node]<densities[nd]:
hnode = nd
hden=densities[nd]
if hnode != '':
return hnode,distance
else:
downlevenodes = []
for upnd in uplevenodes:
for nd in graph.neighbors(upnd):
downlevenodes.append(nd)
if densities[node]<densities[nd]:
hnode = nd
hden = densities[nd]
if hnode != '':
return hnode, distance
if distance < 3:
hnode,distance = Find_Close_Higher_Node(node,densities,graph,downlevenodes,distance)
return hnode,distance
else:
return hnode,distance
def subGraph(graph,colors):
nodeset = {}
subG = []
for i in xrange(len(colors)):
indexofcluster = cc_map.index(colors[i])
if indexofcluster not in nodeset.keys():
nodeset[indexofcluster] = []
nodeset[indexofcluster].append(graph.nodes()[i])
for subset in nodeset.keys():
subG.append(graph.subgraph(nodeset[subset]))
return subG
def CombineSubGraph(graph,colors):
subG = subGraph(graph,colors)
change = False
for subg in subG:
if subg.number_of_nodes() == 1:
node = subg.nodes()[0]
print node
for nd in graph.neighbors(node):
#assign the node to neighber cluster with more than 1 node
indexnode = G.nodes().index(nd)
color_nd = colors[indexnode]
for nnd in graph.neighbors(nd):
if colors[G.nodes().index(nnd)] == color_nd:
colors[G.nodes().index(node)] = colors[G.nodes().index(nnd)]
change = True
break
if change:
subG = subGraph(graph,colors)
return subG
#Used to change the tradeoff metric for select the best placement based on the tradeoff
def tradeoff_function(avglat,avg_weight,maxlat,max_weight,interlat,inter_weight):
return avg_weight*avglat+max_weight*maxlat+interlat*inter_weight
#For worst case latecy
def Bestplacement(graph, colors,avg_weight,max_weight,inter_weight):
subG = subGraph(graph,colors)
controllers = []
for subg in subG:
controllerplace = ''
mintl = -1
for node in subg:
lenghts = nx.single_source_shortest_path_length(subg,node)
lenghts_graph = nx.single_source_shortest_path_length(graph,node)
mx = -1
ag = 0.0
wg = 0.0
for l in lenghts:
if lenghts[l]>mx:
mx = lenghts[l]
ag += lenghts[l]
for l in lenghts_graph:
wg += lenghts_graph[l]
tl = tradeoff_function(ag/nx.number_of_nodes(subg),avg_weight,mx,max_weight,wg/nx.number_of_nodes(graph),inter_weight)
#tl = avg_weight*(ag/nx.number_of_nodes(subg))+max_weight*mx
if mintl < 0:
mintl = tl
controllerplace = node
elif tl < mintl:
mintl = tl
controllerplace = node
controllers.append(controllerplace)
return controllers
def Find_Controller_Placement(graph_name,avg_weight=1.0,max_weight=0.0,inter_weight=0.0):
g_name = graph_name
G = Read(g_name)
#Draw_Graph(G)
dc = Find_DC(G)
densities = {}
belongs = {}
start = time.clock()
for node in G.nodes():
densities[node]=Density(node,dc,G)
numofc=0
numofnodes = nx.number_of_nodes(G)
colors = ['w' for i in xrange(numofnodes)]
for node in G.nodes():
cnode,cdis = Find_Close_Higher_Node(node,densities,G,uplevenodes=[],distance=0)
if cdis > 1:
colors[G.nodes().index(node)]=cc_map[numofc]
numofc+=1
else:
belongs[node]=cnode
end = time.clock()
for node in G.nodes():
indexcurnode = G.nodes().index(node)
if colors[indexcurnode] is 'w':
indexbelnode = G.nodes().index(belongs[node])
while colors[indexbelnode] is 'w':
indexbelnode = G.nodes().index(belongs[G.nodes()[indexbelnode]])
colors[indexcurnode] = colors[indexbelnode]
#controllerlist = BestAvgplacement(G,colors)
controllerlist = Bestplacement(G,colors,avg_weight,max_weight,inter_weight)
shapes = [200 for i in xrange(numofnodes)]
for controller in controllerlist:
shapes[G.nodes().index(controller)]=1000
#print "total time consumption: %f s" % (end - start)
Draw_Graph(G,colors,shapes)
return controllerlist