Commit 2cc6a52b authored by Binara's avatar Binara

distance fuction refactored

parent 60ab9ba1
from helpers import load_map_test, load_map_bus, load_map_train,route_177 from tokenize import ContStr
from turtle import distance
from helpers import load_map_test, load_map_bus, load_map_train,route_177,route_176,route_8717
import math import math
val = 0
class PathPlanner(): class PathPlanner():
def __init__(self, M, start=None, goal=None, type=None): def __init__(self, M, start=None, goal=None, type=None):
self.map = M self.map = M
self.start= start self.start= start
self.goal = goal self.goal = goal
self.type = type self.type = type
self.dis = 0 self.range = 0
self.closedSet = self.create_closedSet() if goal != None and start != None else None self.closedSet = self.create_closedSet() if goal != None and start != None else None
self.openSet = self.create_openSet() if goal != None and start != None else None self.openSet = self.create_openSet() if goal != None and start != None else None
self.cameFrom = self.create_cameFrom() if goal != None and start != None else None self.cameFrom = self.create_cameFrom() if goal != None and start != None else None
...@@ -73,9 +73,10 @@ class PathPlanner(): ...@@ -73,9 +73,10 @@ class PathPlanner():
# This path is the best until now. Record it! # This path is the best until now. Record it!
global val #
val = val + self.dis
self.record_best_path_to(current, neighbor) self.record_best_path_to(current, neighbor)
#self.range += self.dis
print("No Path Found") print("No Path Found")
self.path = None self.path = None
return False return False
...@@ -149,10 +150,6 @@ class PathPlanner(): ...@@ -149,10 +150,6 @@ class PathPlanner():
"""Returns the neighbors of a node""" """Returns the neighbors of a node"""
return set(self.map.roads[node]) return set(self.map.roads[node])
def get_traveled_distance(self):
#traveled_distance
return self.dis
#-------------------------------- Calculations --------------------------------# #-------------------------------- Calculations --------------------------------#
def get_gScore(self, node): def get_gScore(self, node):
...@@ -210,12 +207,48 @@ class PathPlanner(): ...@@ -210,12 +207,48 @@ class PathPlanner():
# calculate the result # calculate the result
dist = (c * r) dist = (c * r)
#print (dist) #print (dist)
self.dis = dist #self.dis = dist
return dist return dist
def get_traveled_distance(self):
#traveled_distance
lastNode = self.start
for n in self.path[1:]:
self.range = self.range+ distance(lastNode, n )
lastNode = n
return self.range
def get_fare(self,path):
cost = 0
currentRouteNo = self.map.routeNo[self.start]
temp = 0
for x in path:
if self.map.routeNo[x] == currentRouteNo:
print('path is(x) =' + str(x)+ ' ,temp =' + str(temp) + ' ,cost =' + str(cost))
temp+=1
else:
if(currentRouteNo == 177):
cost = cost + route_177.get(temp)
temp = 0
currentRouteNo = self.map.routeNo[x]
elif(currentRouteNo == 176):
cost = cost + route_176.get(temp)
temp = 0
currentRouteNo = self.map.routeNo[x]
elif(currentRouteNo == 8717):
cost = cost + route_8717.get(temp)
temp = 0
currentRouteNo = self.map.routeNo[x]
else:
temp = 0
cost = cost + route_177.get(temp)
return cost
# Get route # Get route
def main( a, node1, node2): def main( a, start, destination):
if a == 3: if a == 3:
map = load_map_train() map = load_map_train()
elif a == 2: elif a == 2:
...@@ -223,25 +256,25 @@ def main( a, node1, node2): ...@@ -223,25 +256,25 @@ def main( a, node1, node2):
else: else:
map = load_map_test() map = load_map_test()
planner = PathPlanner(map,node1,node2) planner = PathPlanner(map,start,destination)
print(map.roads) print(map.roads)
path = planner.path path = planner.path
print('distance =' ,val)
if path == [5, 16, 37, 12, 34]: print("Genarated path")
print("code works for test path!!!") if path == False:
print(path) print("No path Found", path)
else: return
print("Genarated path") else:
print(path) print(path)
print('distance =' , planner.get_traveled_distance())
#print(planner.get_fare(path))
# for x in path:
# if routeNo
# print (x)
#for each in path:
main( 1, 2, 14) main( 1, 0, 3)
\ No newline at end of file \ No newline at end of file
...@@ -4,7 +4,6 @@ import pickle ...@@ -4,7 +4,6 @@ import pickle
from chart_studio import plotly as py from chart_studio import plotly as py
import random import random
from plotly.graph_objs import * from plotly.graph_objs import *
from plotly.offline import init_notebook_mode, plot, iplot
#init_notebook_mode(connected=True) #init_notebook_mode(connected=True)
...@@ -26,13 +25,27 @@ route_177 = { ...@@ -26,13 +25,27 @@ route_177 = {
15: 163, 15: 163,
} }
route_176 = {
1: 34,
2: 42,
3: 54,
}
route_8717 = {
1: 20,
2: 20,
3: 20,
4: 20,
5: 40
}
map_test = { map_test = {
0: {'pos': (6.911034627182109, 79.84918916006576), 'connections': [1], 'name' : 'Kollupitiya','type': 0,'routeNo': 177, 'hValue': 1}, 0: {'pos': (6.911034627182109, 79.84918916006576), 'connections': [1], 'name' : 'Kollupitiya','type': 0,'routeNo': 177, 'hValue': 1},
1: {'pos': (6.911751932322411, 79.86194701315071), 'connections': [0,2], 'name' : 'Viharamahadevi Park', 'type': 0, 'routeNo': 177, 'hValue': 1}, 1: {'pos': (6.911751932322411, 79.86194701315071), 'connections': [0,2], 'name' : 'Viharamahadevi Park', 'type': 0, 'routeNo': 177, 'hValue': 1},
2: {'pos': (6.911385550864001, 79.87682791026592), 'connections': [1,3], 'name' : 'House Of Fashion', 'type': 0, 'routeNo': 177, 'hValue': 1}, 2: {'pos': (6.911385550864001, 79.87682791026592), 'connections': [1,3], 'name' : 'House Of Fashion', 'type': 0, 'routeNo': 177, 'hValue': 1},
3: {'pos': (6.911031363415147, 79.88498429384545), 'connections': [2,4], 'name' : 'Castle Street', 'type': 0, 'routeNo': 177, 'hValue': 1}, 3: {'pos': (6.911031363415147, 79.88498429384545), 'connections': [2,4], 'name' : 'Castle Street', 'type': 0, 'routeNo': 177, 'hValue': 1},
4: {'pos': (6.908462881966912, 79.89338919261249), 'connections': [3,5], 'name' : 'Rajagiriya', 'type': 0, 'routeNo': 177, 'hValue': 1}, 4: {'pos': (6.908462881966912, 79.89338919261249), 'connections': [3,5], 'name' : 'Rajagiriya', 'type': 0, 'routeNo': 177, 'hValue': 1},
5: {'pos': (6.906663916178293, 79.90205413217801), 'connections': [4,6], 'name' : 'Hsbc Rajagiriya', 'type': 0, 'routeNo': 177, 'hValue': 1}, 5: {'pos': (6.906663916178293, 79.90205413217801), 'connections': [4,6], 'name' : 'HSBC Rajagiriya', 'type': 0, 'routeNo': 177, 'hValue': 1},
6: {'pos': (6.90333333857459, 79.90747047529919), 'connections': [5,7], 'name' : 'Ethulkotte New', 'type': 0, 'routeNo': 177, 'hValue': 1}, 6: {'pos': (6.90333333857459, 79.90747047529919), 'connections': [5,7], 'name' : 'Ethulkotte New', 'type': 0, 'routeNo': 177, 'hValue': 1},
7: {'pos': (6.903185701293392, 79.91168232658337), 'connections': [6,8], 'name' : 'Parliament Junction', 'type': 0, 'routeNo': 177, 'hValue': 1}, 7: {'pos': (6.903185701293392, 79.91168232658337), 'connections': [6,8], 'name' : 'Parliament Junction', 'type': 0, 'routeNo': 177, 'hValue': 1},
8: {'pos': (6.902102452411621, 79.91741047710077), 'connections': [7,9], 'name' : 'Battaramulla Junction', 'type': 0, 'routeNo': 177, 'hValue': 1}, 8: {'pos': (6.902102452411621, 79.91741047710077), 'connections': [7,9], 'name' : 'Battaramulla Junction', 'type': 0, 'routeNo': 177, 'hValue': 1},
...@@ -41,12 +54,12 @@ map_test = { ...@@ -41,12 +54,12 @@ map_test = {
11: {'pos': (6.9084657304543455, 79.9383708894408), 'connections': [10,12], 'name' : 'Kotte-Bope', 'type': 0, 'routeNo': 177, 'hValue': 1}, 11: {'pos': (6.9084657304543455, 79.9383708894408), 'connections': [10,12], 'name' : 'Kotte-Bope', 'type': 0, 'routeNo': 177, 'hValue': 1},
12: {'pos': (6.9085023402918155, 79.94425286198016), 'connections': [11,13], 'name' : 'Thalahena Junction', 'type': 0, 'routeNo': 177, 'hValue': 1}, 12: {'pos': (6.9085023402918155, 79.94425286198016), 'connections': [11,13], 'name' : 'Thalahena Junction', 'type': 0, 'routeNo': 177, 'hValue': 1},
13: {'pos': (6.903962700873259, 79.95430199947661), 'connections': [12,14], 'name' : 'Malabe', 'type': 0, 'routeNo': 177, 'hValue': 1}, 13: {'pos': (6.903962700873259, 79.95430199947661), 'connections': [12,14], 'name' : 'Malabe', 'type': 0, 'routeNo': 177, 'hValue': 1},
14: {'pos': (6.933539686667202, 79.85005389798111), 'connections': [15], 'name' : 'Fort', 'type': 1, 'routeNo': 8717, 'hValue': 1}, 14: {'pos': (6.933539686667202, 79.85005389798111), 'connections': [15], 'name' : 'Fort_TR', 'type': 1, 'routeNo': 8717, 'hValue': 1},
15: {'pos': (6.923619774894732, 79.84965509086771), 'connections': [14,16], 'name' : 'Kompannavidiya', 'type': 1, 'routeNo': 8717, 'hValue': 1}, 15: {'pos': (6.923619774894732, 79.84965509086771), 'connections': [14,16], 'name' : 'Kompannavidiya_TR', 'type': 1, 'routeNo': 8717, 'hValue': 1},
16: {'pos': (6.911282166657041, 79.84821747831087), 'connections': [15,1,17], 'name' : 'Kollupitiya', 'type': 1, 'routeNo': 8717, 'hValue': 1}, 16: {'pos': (6.911282166657041, 79.84821747831087), 'connections': [15,1,17], 'name' : 'Kollupitiya_TR', 'type': 1, 'routeNo': 8717, 'hValue': 1},
17: {'pos': (6.893710110662125, 79.85300590028642), 'connections': [16,18], 'name' : 'Bambalapitiya', 'type': 1, 'routeNo': 8717, 'hValue': 1}, 17: {'pos': (6.893710110662125, 79.85300590028642), 'connections': [16,18], 'name' : 'Bambalapitiya_TR', 'type': 1, 'routeNo': 8717, 'hValue': 1},
18: {'pos': (6.875030063532378, 79.85744793142631), 'connections': [17,19], 'name' : 'Wellawatte', 'type': 1, 'routeNo': 8717, 'hValue': 1}, 18: {'pos': (6.875030063532378, 79.85744793142631), 'connections': [17,19], 'name' : 'Wellawatte_TR', 'type': 1, 'routeNo': 8717, 'hValue': 1},
19: {'pos': (6.850915755796403, 79.8620969312958), 'connections': [18], 'name' : 'Dehiwala ', 'type': 1, 'routeNo': 8717, 'hValue': 1}, 19: {'pos': (6.850915755796403, 79.8620969312958), 'connections': [18], 'name' : 'Dehiwala_TR', 'type': 1, 'routeNo': 8717, 'hValue': 1},
} }
......
from helpers import load_map_40 from helpers import load_map_test
MAP_40_ANSWERS = [ MAP_ANSWERS = [
(5, 34, [5, 16, 37, 12, 34]), (5, 34, [5, 16, 37, 12, 34]),
(5, 5, [5]), (5, 5, [5]),
(8, 24, [8, 14, 16, 37, 12, 17, 10, 24]) (8, 24, [8, 14, 16, 37, 12, 17, 10, 24])
] ]
def test(shortest_path_function): def test(shortest_path_function):
map_40 = load_map_40() map = load_map_test()
correct = 0 correct = 0
for start, goal, answer_path in MAP_40_ANSWERS: for start, goal, answer_path in MAP_ANSWERS:
path = shortest_path_function(map_40, start, goal).path path = shortest_path_function(map, start, goal).path
if path == answer_path: if path == answer_path:
correct += 1 correct += 1
print("For start:", start, print("For start:", start,
...@@ -19,8 +19,8 @@ def test(shortest_path_function): ...@@ -19,8 +19,8 @@ def test(shortest_path_function):
"Correct: ", answer_path) "Correct: ", answer_path)
else: else:
print("Error Testing faild !!!" ) print("Error Testing faild !!!" )
if correct == len(MAP_40_ANSWERS): if correct == len(MAP_ANSWERS):
print("All tests pass ") print("All tests pass ")
else: else:
print("Only passed", correct, "/", len(MAP_40_ANSWERS), "test cases") print("Only passed", correct, "/", len(MAP_ANSWERS), "test cases")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment