-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.py
125 lines (100 loc) · 3.52 KB
/
algo.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
import pylab
import networkx as nx
import math
import os
import simulate_file as sf
g=nx.Graph()
path_length = 0
edges_list = [(1,2,3),(1,3,2),(2,4,1),(3,4,8),(4,5,1)]
node_energies = [-3000,300,300,300,300,300]
centroid_array = [('Koramangala',1),('BTM',2),('Indiranagar',3),('Rajajinagar',4),('Shivajinagar',5)]
energy_cycles = []
def createGraph(): #area specified for each node,weights or distances given for each edge
global g
g.add_node(1,area='Kor')
g.add_node(2,area='BTM')
g.add_node(3,area='Indiranagar')
g.add_node(4,area='Rajajinagar')
g.add_node(5,area='Shivajinagar')
g.add_weighted_edges_from(edges_list)
#nx.draw(g)
#pylab.show()
def return_centroid(destination_area): #return the centroid for the requested region
global g
for (x,y) in centroid_array:
if destination_area == x:
print 'Centroid returned' , y
return y
def computeHNiR(list_of_neighbours,destination_area,source):
dest_centroid = return_centroid(destination_area)
all_paths = []
all_costs = []
weights=nx.get_edge_attributes(g,'weight')
print 'weights list is : ', weights
for neighbour in list_of_neighbours:
for path in nx.all_simple_paths(g, source=neighbour, target=dest_centroid):
l=path
cost = 0
if source not in path:
try:
for i in range(len(path)):
if i != (len(path)-1):
cost = cost + weights[(path[i],path[i+1])]
except Exception,e:
cost = cost + weights[(path[i+1],path[i])]
path_length = cost
print path, "Cost : " + str(cost)
cost = 0.65*cost + 0.35*5 #the equation
all_costs = all_costs + [cost]
all_paths = all_paths + [path]
print 'All costs is : ', all_costs
print 'All paths is : ',all_paths
min_cost = min(all_costs)
index = all_costs.index(min_cost)
return all_paths[index]
def transmit():
global g
global energy_cycles
source_node=raw_input("Enter the source node : ")
destination_area=raw_input("Enter the destination area : ")
no_of_bits = raw_input('Enter the number of bits in the packet')
no_of_bits = int(no_of_bits)
dest_centroid = return_centroid(destination_area)
source_node = int(source_node)
neighbours_list = g.neighbors(source_node)
print 'Neighbours list is : ', neighbours_list
if dest_centroid in neighbours_list: # trasmitting to a neighbour :
mincost_path = [dest_centroid]
else :
mincost_path=computeHNiR(neighbours_list,destination_area,source_node)
print mincost_path
#checking if all the nodes are alive on the path :
for node in mincost_path :
if node_energies[node] > 7:
break
else :
g.remove_node(node)
overall_path=[source_node] + mincost_path
if "Node-energy-cycles.csv" in os.listdir('.') :
os.system("rm Node-energy-cycles.csv")
fl = open("Node-energy-cycles.csv","a")
for node in overall_path:
print 'Node energies are : ' , node_energies
for energy in node_energies[1:] :
fl.write(str(energy)+",")
fl.write("\n")
if node == source_node:
node_energies[node]=node_energies[node]-(10*no_of_bits+0.02*no_of_bits*math.pow(path_length,2))
elif node == dest_centroid:
node_energies[node]=node_energies[node]-(10*no_of_bits)
else :
node_energies[node]=node_energies[node]-5
fl.close()
print 'Packet transmitted through path : ', overall_path
print 'Energies after transmission :' , node_energies[1:]
#print 'Energy cycles are : ' ,energy_cycles
sf.initialSituation(g,source_node,dest_centroid)
sf.transmission(overall_path,g,source_node,dest_centroid)
createGraph()
transmit()
os.system("python LEACH/leach.py")