-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
585 lines (536 loc) · 17.1 KB
/
graph.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# -*- coding: utf-8 -*-
# Node and graph implementation
# Developer
# Ismail AKBUDAK
# ismailakbudak.com
from matplotlib import pyplot as plt
import networkx as nx
import random
from collections import OrderedDict
import pprint
import math
import numpy as np
pp = pprint.PrettyPrinter(indent=4)
"""
Find coordinate region
Args:
X: float
x coodinate value
Y: float
y coodinate value
Return:
int region of x and y coordinates
"""
def findIndex(X, Y):
if X > 0 and Y > 0:
index = 0
elif X < 0 and Y > 0:
index = 1
elif X < 0 and Y < 0:
index = 2
else:
index = 3
return index
"""
Get distributions for graph node capacity
Args:
None
Return:
Array of Distribution class
"""
def getDistributions():
return [
Distribution(2,1),
Distribution(3,1),
Distribution(3,0.1),
Distribution(3,1),
Distribution(3,0.6) ]
"""
Normal Distrubution class
Args:
LOC: float
Median of distribution
SCALE: float
Standart divion of distribution
SIZE: int or tuple of ints, optional
Output shape
"""
class Distribution(object):
"""docstring for Distribution"""
def __init__(self, LOC=0.0, SCALE=1.0, SIZE=None):
super(Distribution, self).__init__()
self.LOC = LOC
self.SCALE = SCALE
self.SIZE = SIZE
"""
Node position class
Args:
X: int
x coordinate
Y: int
y coordinate
"""
class Position(object):
"""docstring for Position"""
def __init__(self, X, Y):
super(Position, self).__init__()
self.X = X
self.Y = Y
"""
Node class for graph structure
Args:
ID: Integer unique id of node
CAPACITY: Integer capacity of node
X: Integer node x coordinate
Y: Integer node y coordinate
"""
class Node(object):
def __init__(self, ID, CAPACITY, X=0, Y=0):
self.ID = ID
self.CAPACITY = CAPACITY
self.POSITION = Position(X,Y)
self.VISITED = False
self.NAME = 'N:%s-C:%s'%(str(ID),str(CAPACITY))
self.SHORT_NAME = 'N:%s'%(str(ID))
self.COORDINATOR = self
self.neighbours={}
self.REGION = findIndex(X, Y)
self.log("node initialized..")
def __repr__(self):
return self.SHORT_NAME
def __str__(self):
return self.NAME
"""
Add new node to neighbours
Args:
Node object
Return:
Boolean result
If added True otherwise False
"""
def addNeighbour(self,node):
if not(self.neighbours.has_key(node.ID)):
self.neighbours[node.ID]=node
node.neighbours[self.ID]=self
self.log("neighbour added..")
return True
else:
self.log("neighbour could not added..")
self.log("ERROR: key '%s' is exist in '%s'"%(node.ID, self) )
return False
"""
Remove node from neighbours
Args:
Node object
Return:
Boolean result
If removed True otherwise False
"""
def removeNeighbour(self,node):
if self.neighbours.has_key(node.ID):
self.neighbours.pop(node.ID)
node.neighbours.pop(self.ID)
self.log("neighbour removed..")
return True
else:
self.log("neighbour could not removed..")
self.log("ERROR: key '%s' is not exist in '%s'"%(node.ID, self) )
return False
"""
Remove all neighbours
Args:
None
Return:
Boolean result
If removed True otherwise False
"""
def remove(self):
iterator = self.neighbours.copy()
for node in iterator.values():
if not(node.removeNeighbour(self)):
return False
self.log("removed all neighbours..")
return True
def log(self, message):
#print("NODE:: %s"%(message) )
pass
"""
Graph class
Args:
None
"""
class Graph(object):
def __init__(self):
self.nodes={}
self.positions={}
self.lastID=0
self.cordinatorCount=1
self.weightValue=1
self.traceElection=True
self.traceLog=True
self.traceElectionVisual=True
self.useRandomCapacity=True
self.distributions = getDistributions()
self.log("Graph initialized..")
"""
Add new node to graph
Args:
node: new node that will add
Return:
Boolean if added True otherwise False
"""
def add(self, node):
if not(self.nodes.has_key(node.ID)):
self.nodes[node.ID]=node
self.lastID += 1
self.log("node added..")
return True
else:
self.log("node could not added..")
self.log("ERROR: key '%s' is exist"%(node.ID) )
return False
"""
Remove node from graph
Args:
node: new node that will remove
Return:
None
"""
def remove(self, node):
node.remove()
self.nodes.pop(node.ID)
self.log("node removed..")
"""
Remove all graph nodes
Args:
None
Return:
None
"""
def removeAll(self):
self.nodes = {}
self.positions = {}
self.lastID=0
self.log("all nodes removed..")
"""
Connect to nodes eachother
Args:
node1: node object
node2: nodes object
Return:
None
"""
def link(self,node1,node2):
if node1.addNeighbour(node2):
self.log("nodes linked..")
return True
else:
return False
"""
Unset graph nodes visited property
Args:
None
Return:
None
"""
def removeVisitedProperty(self):
for node in self.nodes.values():
node.VISITED = False
"""
Get unvisited node in graph
Args:
None
Return:
Node or None
"""
def getNotVisitedNode(self):
for node in self.nodes.values():
if node.VISITED == False:
return node
return None
"""
Find coordinators in graph for different region
Args:
None
Return:
None
"""
def findCoordinates(self):
# Find all shortest path length
self.removeVisitedProperty()
node = self.getNotVisitedNode()
coordinator_list = []
while node is not None:
value = self.findLengths(node)
coordinator_list.append( value )
node = self.getNotVisitedNode()
# Sort list
sorted_list = sorted(coordinator_list, key=lambda value: value['weight_length'])
self.log_pp('Sorted result :', sorted_list , True )
# Select nodes that has minimum length
selected_nodes = []
if len(sorted_list) > self.cordinatorCount:
i = 0; j = 0
while j < self.cordinatorCount:
value = sorted_list[i]
if value['length'] > 0:
selected_nodes.append(value)
j += 1
i += 1
else:
selected_nodes = sorted_list
self.log('sorted list is below than coordinator count')
self.log_pp('Selected nodes result :', selected_nodes , True )
if len(selected_nodes) > 0:
self.draw_coordinator(selected_nodes)
else:
self.log('there is no selected nodes')
"""
Find distance between two position
Args:
node: Node object
node2: Node object
Return:
None
"""
def findDistance(self, node, node2):
return round( math.sqrt( math.pow((node.POSITION.X - node2.POSITION.X), 2) + math.pow((node.POSITION.Y - node2.POSITION.Y), 2)), 2)
"""
Find total shortest path length for given node
Args:
node: startNode object
Return:
object: { 'node': node, 'length': length, 'weight_length': value }
"""
def findLengths(self, startNode):
# Set startNode attribute
startNode.VISITED = True
# Create graph
graph = nx.DiGraph()
for node in self.nodes.values():
graph.add_node(node)
for node_neighbour in node.neighbours.values():
graph.add_edge( node, node_neighbour, weight=self.findDistance(node, node_neighbour) )
# Find shortest path
shortest_length = nx.shortest_path_length(graph,source=startNode, weight='weight' )
total = 0
for l in shortest_length.values():
total += l
value = { 'node': startNode, 'length': round(total,2), 'weight_length': round(total / (self.weightValue * startNode.CAPACITY), 2) }
# self.log_pp('Shortest path length for %s :'%(startNode.SHORT_NAME), shortest_length , True )
# self.log_election('Total length : %s \n'%( str( value['length'] )))
# self.log_election('Total length with weight value: %s \n'%(str(value['weight_length'])))
return value
"""
Draw all nodes with CAPACITY property
Args:
None
Return:
None
"""
def draw(self):
self.log("graph is drawing..")
colors = ["#EFDFBB","orange","lightgreen","lightblue","#FFD300","violet","yellow","#7CB9E8","#E1A95F", "#007FFF","#CCFF00","pink","cyan"]
def find_color(node):
return colors[node.REGION]
def find_length(node, node_neighbour):
return round( math.sqrt( math.pow((node.POSITION.X - node_neighbour.POSITION.X), 2) + math.pow((node.POSITION.Y - node_neighbour.POSITION.Y), 2)), 2)
graph = nx.DiGraph()
node_size = []
for node in self.nodes.values():
node_size.append(100*node.CAPACITY)
graph.add_node(node)
for node_neighbour in node.neighbours.values():
graph.add_edge(node,
node_neighbour,
weight=find_length(node, node_neighbour),
color=find_color(node_neighbour) )
node_colors = map(find_color, graph.nodes())
if len(self.nodes) > 1:
edges,edge_colors = zip(*nx.get_edge_attributes(graph,'color').items())
else:
edges=[]
edge_colors="yellow"
G=nx.grid_2d_graph(1,1)
plt.subplot(111)
labels = nx.get_edge_attributes(graph,'weight')
nx.draw_networkx_edge_labels(
graph,
self.positions,
edge_labels=labels,
font_family='ubuntu',
edgelist=edges,
edge_color=edge_colors
)
nx.draw(graph,
self.positions,
with_labels=True,
font_size=8,
node_size=1300,#node_size,
font_family='ubuntu',
font_color='red',
node_color=node_colors,
edgelist=edges,
edge_color=edge_colors,
width=0.4)
plt.axis('on')
plt.grid('on')
plt.show()
"""
Draw coordinator and coordinator candidates
Args:
coordinator_list: election results
Return:
None
"""
def draw_coordinator(self, coordinator_list):
self.log("coordinator is drawing..")
coordinator_colors = ["yellow"]
region_colors = ["lightgreen","lightblue","violet","#E1A95F", "#007FFF","#CCFF00"]
def find_coordinator_color(node):
for value in coordinator_list:
coordinator = value['node']
if node.ID == coordinator.ID:
return coordinator_colors[0]
return region_colors[node.REGION]
graph = nx.DiGraph()
node_size = []
for node in self.nodes.values():
node_size.append(100*node.CAPACITY)
graph.add_node(node)
for node_neighbour in node.neighbours.values():
graph.add_edge( node,
node_neighbour,
weight=self.findDistance(node, node_neighbour),
coordinator_color=find_coordinator_color(node_neighbour) )
node_coordinator_colors = map(find_coordinator_color, graph.nodes())
coordinator_edges,coordinator_edge_colors = zip(*nx.get_edge_attributes(graph,'coordinator_color').items())
G=nx.grid_2d_graph(1,1)
plt.subplot(111)
labels = nx.get_edge_attributes(graph,'weight')
nx.draw_networkx_edge_labels(
graph,
self.positions,
edge_labels=labels,
font_family='ubuntu',
edgelist=coordinator_edges,
edge_color=coordinator_edge_colors
)
nx.draw(graph,
self.positions,
with_labels=True,
font_size=8,
node_size=1300,#node_size,
font_family='ubuntu',
font_color='red',
node_color=node_coordinator_colors,
edgelist=coordinator_edges,
edge_color=coordinator_edge_colors,
width=0.4)
# Information Text
x=10.0;y=11;i=1;flag=False;dist=1.2
for color in coordinator_colors:
if color == coordinator_colors[0]:
text = "Coordinator"
plt.text(x, y, text, bbox=dict(facecolor=color, alpha=0.8))
y-=dist
y-=dist
previous = None
for node_length in coordinator_list:
node = node_length['node']
length = node_length['length']
weight_length = node_length['weight_length']
if previous == node.REGION:
y-=dist
else:
y-=1*dist
previous = node.REGION
text = '%s Length: %s br Weighted length: %s br'%(node.SHORT_NAME,length, weight_length)
plt.text( x, y, text, bbox=dict(facecolor=region_colors[node.REGION], alpha=1))
plt.axis('on')
plt.grid('on')
if self.traceElectionVisual:
plt.show()
"""
Read nodes and edges from file
Args:
None
Return:
None
"""
def readFiles(self):
#nodes.txt => node_id capacity
#edges.txt => node_id node_id
try:
f = open('nodes.txt','r')
lines = f.readlines()
for line in lines:
content = line.strip().split()
if len(content) == 4:
ID = int(content[0])
X = float(content[2])
Y = float(content[3])
if self.useRandomCapacity:
index = findIndex(X, Y)
CAPACITY = round( np.random.normal(
loc=self.distributions[index].LOC,
scale=self.distributions[index].SCALE,
size=self.distributions[index].SIZE), 2 ) #random.randint(0, self.MAX_CAPACITY)
else:
CAPACITY = int(content[1])
node = Node(ID, CAPACITY, X, Y)
self.add(node)
self.positions[node] = (X,Y)
f = open('edges.txt','r')
lines = f.readlines()
for line in lines:
content = line.strip().split()
if len(content) == 2:
id1 = int(content[0])
id2 = int(content[1])
if self.nodes.has_key(id1) and self.nodes.has_key(id2):
node1 = self.nodes[ id1 ]
node2 = self.nodes[ id2 ]
self.link( node1, node2 )
else:
if not(self.nodes.has_key(id1)):
self.log("ERROR: key '%s' is not exist"%(id1) )
if not(self.nodes.has_key(id2)):
self.log("ERROR: key '%s' is not exist"%(id2) )
self.log('Files readed successfully..')
except Exception, error:
self.log('Files could not read..')
self.log('ERROR: %s' % error)
"""
Log messages
Args:
message: string first message
Return:
None
"""
def log(self, message):
if self.traceLog:
print("GRAPH:: %s"%(message) )
"""
Log messages for prety print array
Args:
message: string first message
array: array for prety print
is_write: Boolean default value is True
Return:
None
"""
def log_pp(self, message, array, is_write=True ):
if is_write:
print("GRAPH:: %s"%(message) )
pp.pprint(array)
"""
Log messages
Args:
message: string first message
Return:
None
"""
def log_election(self, message):
if self.traceElection:
print("GRAPH ELECTION:: %s"%(message) )