forked from aroradaman/Intelligent-Transport-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.py
79 lines (72 loc) · 2.45 KB
/
testing.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
import neo4j
import random
import math
import json
with open('conf.json','r') as f :
conf = json.loads(f.read())
reAssign = conf['reAssign']
link_speed = conf['link_speed']
radio_radius = conf['radio_radius']
packet_size = conf['packet_size']
prop_speed_channel = conf['prop_speed_channel']
x_reliability = conf['x_reliability']
x_delay = conf['x_delay']
x_hc = conf['x_hc']
reliability_constant = conf['reliability_constant']
hc_constant = conf['hc_constant']
delay_constant = conf['delay_constant']
db_address = conf['db_address']
def obj_func( delay , hc , reliability ) :
return x_reliability * ( reliability / reliability_constant) + x_delay * ( delay_constant / delay ) + x_hc * ( hc_constant / hc )
radio_radius = 250
connection = neo4j.connect('http://localhost:7474')
cursor = connection.cursor()
def getRoute(params) :
if conf['biDirectional'] :
query = "MATCH p = (:Node{id:" + str(params['start']) + "})-[r:IS_CONNECTED*1..10]-(:Node{id:" + str(params['end']) + "}) RETURN DISTINCT p"
else :
if params['end'] > params['start'] :
query = "MATCH p = (:Node{id:" + str(params['start']) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params['end']) + "}) RETURN DISTINCT p"
else :
query = "MATCH p = (:Node{id:" + str(params['end']) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params['start']) + "}) RETURN DISTINCT p"
qosMatrix = []
maxQoS = 0
max_index = 0
minQoS = 100000000000000
min_index = 0
count = 0
Route = []
connection = neo4j.connect('http://localhost:7474')
cursor = connection.cursor()
nodeDetails = {}
print 'starting'
for r in cursor.execute(query) :
delay = 0
reliability = 1
hc = 0
temp = []
for link in r[0] :
if link['type'] == 'node' :
temp.append(link['id'])
#nodeDetails.update({str(link['id']) : {'server_ip':link['server_ip'],'server_port':link['server_port']}})
elif link['type'] == 'edge' :
delay += float(link['delay'])
reliability *= link['reliability']
hc += link['hc']
print temp
data = str(temp)
Route.append(temp)
obj_val = obj_func( delay , hc , reliability )
qosMatrix.append([ delay , hc , reliability , obj_val ])
if obj_val > maxQoS :
maxQoS = obj_val
max_index = count
if obj_val < minQoS :
minQoS = obj_val
min_index = count
count += 1
print '\nRoute to be followed : ' , Route[max_index][1:]
#reply = nodeDetails[str(Route[max_index][1])]
#reply.update({'next':Route[max_index][1]})
#return json.dumps(reply)
getRoute({'start':1,'end':11})