Commit 06d339dd authored by O.V.B Sewmina's avatar O.V.B Sewmina

Merge branch 'IT19186566' into 'master'

It19186566

See merge request 2022-020/2022-020!18
parents addb6bbe 9b730593
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
val = 0
class PathPlanner():
def __init__(self, M, start=None, goal=None, type=None):
self.map = M
self.start= start
self.goal = goal
self.type = type
self.dis = 0
self.range = 0
self.cost = 0
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.cameFrom = self.create_cameFrom() if goal != None and start != None else None
......@@ -73,9 +74,10 @@ class PathPlanner():
# This path is the best until now. Record it!
global val #
val = val + self.dis
self.record_best_path_to(current, neighbor)
#self.range += self.dis
print("No Path Found")
self.path = None
return False
......@@ -149,10 +151,6 @@ class PathPlanner():
"""Returns the neighbors of a node"""
return set(self.map.roads[node])
def get_traveled_distance(self):
#traveled_distance
return self.dis
#-------------------------------- Calculations --------------------------------#
def get_gScore(self, node):
......@@ -210,12 +208,55 @@ class PathPlanner():
# calculate the result
dist = (c * r)
#print (dist)
self.dis = dist
#self.dis = 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):
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))
temp+=1
currentRouteNo = self.map.routeNo[x]
else :
print("eeeeeeeeeeeeeeeeeeee")
self.func(currentRouteNo, temp)
currentRouteNo = self.map.routeNo[x]
temp = 0
print('cost = ', self.cost)
#cost = cost + route_177.get(temp)
return self.cost
def func(self, no, nodes):
print('fuction')
if(no == 177):
self.cost = self.cost + route_177.get(nodes)
elif(no == 176):
self.cost = self.cost + route_176.get(nodes)
elif(no == 8717):
self.cost = self.cost + route_8717.get(nodes)
else:
pass
# Get route
def main( a, node1, node2):
def main( a, start, destination):
if a == 3:
map = load_map_train()
elif a == 2:
......@@ -223,25 +264,25 @@ def main( a, node1, node2):
else:
map = load_map_test()
planner = PathPlanner(map,node1,node2)
planner = PathPlanner(map,start,destination)
print(map.roads)
path = planner.path
print('distance =' ,val)
if path == [5, 16, 37, 12, 34]:
print("code works for test path!!!")
print(path)
else:
print("Genarated path")
print("Genarated path")
if path == False:
print("No path Found", path)
return
else:
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)
\ No newline at end of file
main( 1, 0, 3)
\ No newline at end of file
......@@ -4,7 +4,6 @@ import pickle
from chart_studio import plotly as py
import random
from plotly.graph_objs import *
from plotly.offline import init_notebook_mode, plot, iplot
#init_notebook_mode(connected=True)
......@@ -26,13 +25,27 @@ route_177 = {
15: 163,
}
route_176 = {
1: 34,
2: 42,
3: 54,
}
route_8717 = {
1: 20,
2: 20,
3: 20,
4: 20,
5: 40
}
map_test = {
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},
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},
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},
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},
......@@ -41,12 +54,12 @@ map_test = {
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},
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},
15: {'pos': (6.923619774894732, 79.84965509086771), 'connections': [14,16], 'name' : 'Kompannavidiya', 'type': 1, 'routeNo': 8717, 'hValue': 1},
16: {'pos': (6.911282166657041, 79.84821747831087), 'connections': [15,1,17], 'name' : 'Kollupitiya', 'type': 1, 'routeNo': 8717, 'hValue': 1},
17: {'pos': (6.893710110662125, 79.85300590028642), 'connections': [16,18], 'name' : 'Bambalapitiya', 'type': 1, 'routeNo': 8717, 'hValue': 1},
18: {'pos': (6.875030063532378, 79.85744793142631), 'connections': [17,19], 'name' : 'Wellawatte', 'type': 1, 'routeNo': 8717, 'hValue': 1},
19: {'pos': (6.850915755796403, 79.8620969312958), 'connections': [18], 'name' : 'Dehiwala ', '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_TR', '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_TR', '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_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, 5, [5]),
(8, 24, [8, 14, 16, 37, 12, 17, 10, 24])
]
def test(shortest_path_function):
map_40 = load_map_40()
map = load_map_test()
correct = 0
for start, goal, answer_path in MAP_40_ANSWERS:
path = shortest_path_function(map_40, start, goal).path
for start, goal, answer_path in MAP_ANSWERS:
path = shortest_path_function(map, start, goal).path
if path == answer_path:
correct += 1
print("For start:", start,
......@@ -19,8 +19,8 @@ def test(shortest_path_function):
"Correct: ", answer_path)
else:
print("Error Testing faild !!!" )
if correct == len(MAP_40_ANSWERS):
if correct == len(MAP_ANSWERS):
print("All tests pass ")
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