-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_path.py
102 lines (84 loc) · 5.15 KB
/
search_path.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import numpy as np
import AuxillaryFunctions
import rasterize
map = np.load('risk_zones.npy')
# IMPORTING THE MAP AS A GRAPH WITH 8 CONNECTED NEIGHBORS
vertices = AuxillaryFunctions.vertices
neighbors = AuxillaryFunctions.neighbors
############ AUGMENTED A STAR ###############
def augmented_astar_search(nest,site):
# INITIALIZING DICTIONARIES FOR THE SEARCH ALGORITHM
path = [] # Initializing an empty path
EstTotalCost = dict()
CostTo = dict()
heuristic = dict() # Dictionary to store heuristic
EstTotalCost[nest] = rasterize.calculate_heuristic(nest,site)
pred = dict() # Dictionary to store the predecessors.
# POPULATING DICTIONARIES FOR ALL VERTICES IN THE GRAPH
for vertex in vertices:
CostTo[vertex] = float('inf')
EstTotalCost[vertex] = float('inf')
heuristic[vertex] = rasterize.calculate_heuristic(vertex,site)
CostTo[nest] = 0 # Cost to te nest is zero
EstTotalCost[nest] = heuristic[nest] # Setting heuristic for start node as the estimated total cost for the node.
queue = dict() # Initializing the queue dictionary
queue[nest] = EstTotalCost[nest]
while len(queue)>0 :
vertex = list(queue.keys())[0] # Getting the vertex with the smallest estimated total cost. The dictionary is sorted by value of est total cost.
del queue[vertex] # Removing that vertex from the dictionary
if vertex==site: # Recovering the path if we reach the site
print('Recovering path ... ')
path = AuxillaryFunctions.RecoverPath(nest,site,pred)
print("Path Recovered : A* successful")
return path
for neighbor in neighbors[vertex]:
# cost to go and heuristic are calculated as the number of pixels.
# if the pixel is in the low risk zone, its cost is 1.
# if the pixel is in the high risk zone, its cost is higher. (Tunable)
if map[neighbor[0],neighbor[1]] == 0 :
cost_to_neighbor = CostTo[vertex] + 1 # neighbor is a low risk pixel
if map[neighbor[0],neighbor[1]] == 1 :
cost_to_neighbor = CostTo[vertex] + 1.5 # neighbor is a high risk pixel. Higher cost added.
if cost_to_neighbor < CostTo[neighbor] :
pred[neighbor] = vertex
CostTo[neighbor] = cost_to_neighbor
EstTotalCost[neighbor] = cost_to_neighbor + heuristic[neighbor]
queue[neighbor] = EstTotalCost[neighbor] # Updating the estimated total cost of the vertex in the dictionary
dict(sorted(queue.items(), key=lambda item: item[1])) # Sorting the dictionary.
return path
############# A STAR SEARCH ####################
def astar_search(nest,site):
# INITIALIZING DICTIONARIES FOR THE SEARCH ALGORITHM
path = [] # Initializing an empty path
EstTotalCost = dict()
CostTo = dict()
heuristic = dict() # Dictionary to store heuristic
EstTotalCost[nest] = AuxillaryFunctions.euclidean_distance(nest,site) # Cost for start node to reach goal
pred = dict() # Dictionary to store the predecessors.
# POPULATING DICTIONARIES FOR ALL VERTICES IN THE GRAPH
for vertex in vertices:
CostTo[vertex] = float('inf')
EstTotalCost[vertex] = float('inf')
heuristic[vertex] = AuxillaryFunctions.euclidean_distance(vertex,site)
CostTo[nest] = 0 # Cost to go the nest itself is zero since that is the starting point
EstTotalCost[nest] = heuristic[nest] # Setting heuristic for start node as the estimated total cost for the node.
queue = dict() # Initializing the queue dictionary
queue[nest] = EstTotalCost[nest]
while len(queue)>0 :
vertex = list(queue.keys())[0] # Getting the vertex with the smallest estimated total cost. The dictionary is sorted by value of est total cost.
del queue[vertex] # Removing that vertex from the dictionary
if vertex==site: # Recovering the path if we reach the site
print('Recovering path ... ')
print('Recovering path ... ')
path = AuxillaryFunctions.RecoverPath(nest,site,pred)
print("Path Recovered : A* successful")
return path
for neighbor in neighbors[vertex]:
cost_to_neighbor = CostTo[vertex] + AuxillaryFunctions.euclidean_distance(vertex,neighbor) # Represents the total cost to get TO THE NODE i
if cost_to_neighbor < CostTo[neighbor] :
pred[neighbor] = vertex
CostTo[neighbor] = cost_to_neighbor
EstTotalCost[neighbor] = cost_to_neighbor + heuristic[neighbor]
queue[neighbor] = EstTotalCost[neighbor] # Updating the estimated total cost of the vertex in the dictionary
dict(sorted(queue.items(), key=lambda item: item[1])) # Sorting the dictionary.
return path