summaryrefslogtreecommitdiffstats
path: root/algorithms.py
diff options
context:
space:
mode:
authorStefan Hansson <steha708@edu.liu.se>2020-11-16 13:55:06 +0100
committerNewbyte <steha708@liu.se>2020-12-15 18:23:53 +0100
commitfe0d3bcc28f723e97cfa4105bed83eb8bb85bbe5 (patch)
treef9992628c6e82ee0175c072e4da23e22dcd1e000 /algorithms.py
parent6c8acb4f4eabb20da41b941158bf52038035699f (diff)
downloadtdde25-fe0d3bcc28f723e97cfa4105bed83eb8bb85bbe5.tar.gz
wip
Diffstat (limited to 'algorithms.py')
-rw-r--r--algorithms.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/algorithms.py b/algorithms.py
index 9f3fd78..9d4f538 100644
--- a/algorithms.py
+++ b/algorithms.py
@@ -1,5 +1,6 @@
import heapq
import math
+from store import get_relevant_neighbours
def length_haversine(p1, p2):
@@ -17,6 +18,7 @@ def length_haversine(p1, p2):
return 6372797.560856 * c # return the distance in meters
+<<<<<<< HEAD
def grid_search(grid, source_node):
"""
Finds closest node to source node by comparing distance to nodes within
@@ -72,19 +74,25 @@ def get_closest_node(nodes, source_node):
Searches through all nodes in a specified grid and return node
closes to source node.
"""
+def get_closest_node_id(nodes, source_node, transport_mode):
+ """ Search through all nodes and return the id of the node
+ that is closest to 'source_node'. """
min_node = None
min_value = None
for node in nodes:
length = length_haversine(source_node, node)
- if min_node is None or length < min_value:
+
+ relevant_neighbours = get_relevant_neighbours(node, transport_mode)
+
+ if (min_node is None or length < min_value) and relevant_neighbours:
min_node = node
min_value = length
return min_node
-def find_shortest_path(nodes, source_id, target_id):
+def find_shortest_path(nodes, source_id, target_id, transport_mode: str):
""" Return the shortest path using Dijkstra's algortihm. """
# queue contains multiple (walk_dist, (node_0, node_1, ... node_n))-tuples
# where (node_0, node_1, ... node_n) is a walk to node_n
@@ -102,10 +110,15 @@ def find_shortest_path(nodes, source_id, target_id):
if walk_end in visited:
# there exists a shorter walk to walk_end
continue
+
# otherwise this is the shortest walk to walk_end
visited.add(walk_end)
# consider all our neighbours
- for neighbour in nodes[walk_end].neighbours:
+ end_node = nodes[walk_end]
+
+ relevant_neighbours = get_relevant_neighbours(end_node, transport_mode)
+
+ for neighbour in relevant_neighbours:
if neighbour in visited:
# there exists a shorter walk to neighbour
continue
@@ -114,5 +127,6 @@ def find_shortest_path(nodes, source_id, target_id):
new_dist = walk_dist + length_haversine(nodes[walk_end], neighbour)
new_walk = walk + (neighbour.id,)
heapq.heappush(queue, (new_dist, new_walk))
+
# no path found
return None