-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
80 lines (58 loc) · 2.46 KB
/
algorithm.py
File metadata and controls
80 lines (58 loc) · 2.46 KB
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
from init import np
from init import grid_x, grid_y, UNIT_BLOCK
init_cost = -1
# Node Class(Structure)
class Node:
def __init__(self, node):
self.x = node[0]
self.y = node[1]
# Find Cost Around(4 Nearest Nodes) a given Node
def find_neighbors(cost_matrix, i_node, cost_value):
nearest_neighbors = []
# 4 nearest neighbours : Top, Down, Left, Right
nodes = [Node([i_node.x - 1, i_node.y]), Node([i_node.x + 1, i_node.y]), Node([i_node.x, i_node.y - 1]), Node([i_node.x, i_node.y + 1])]
for node in nodes:
# Check if the node is within the grid size
if (node.x >= 0) and (node.x < grid_x) and (node.y >= 0) and (node.y < grid_y):
# Check if the node is unvisited (Cost Value = -1 : Unvisited Node)
if cost_matrix[node.y, node.x] == cost_value:
nearest_neighbors.append(node)
return nearest_neighbors
# Find Cost Matrix : Iterate to Find the Cost for All Nodes in The Grid
def find_cost_mat(cost_matrix, i_node):
iteration_list = [i_node]
while len(iteration_list) != 0:
for i_node in iteration_list:
n_neighbors = find_neighbors(cost_matrix, i_node, init_cost)
for i in n_neighbors:
cost_matrix[i.y, i.x] = cost_matrix[i_node.y, i_node.x] + 1
# Add new neighbour nodes and remove the existing node from iteration list
iteration_list.extend(n_neighbors)
iteration_list = iteration_list[1:]
def path_planner(cost_matrix, i_node, i_cost):
g_path = np.array([i_node.x, i_node.y])
n_neighbors = [i_node]
while i_cost != 0:
i_cost = i_cost - 1
n_neighbors = find_neighbors(cost_matrix, n_neighbors[0], i_cost)
g_path = np.vstack((g_path, np.array([n_neighbors[0].x, n_neighbors[0].y])))
return g_path
def grassfire_path(n_start, n_end):
# Check if start and end node is valid
if (Node(n_start).x < 0) and (Node(n_start).x >= grid_x) and (Node(n_start).y < 0) and (Node(n_start).y >= grid_y):
print("Cannot Start from this Node")
if (Node(n_end).x < 0) and (Node(n_end).x >= grid_x) and (Node(n_end).y < 0) and (Node(n_end).y >= grid_y):
print("Cannot End at this Node")
# print("Start and End \n", n_start, n_end)
cost_matrix = init_cost * np.ones([grid_y, grid_x])
# Start Node = 0, 0
cost_matrix[Node(n_end).y, Node(n_end).x] = 0
find_cost_mat(cost_matrix, Node(n_end))
total_cost = cost_matrix[Node(n_start).y, Node(n_start).x]
if total_cost == init_cost:
print(" No Route Exists ")
else:
path = path_planner(cost_matrix, Node(n_start), total_cost)
return path
#if __name__ == "__main__":
# grassfire_path(n_start, n_end)