-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.py
282 lines (226 loc) · 10.5 KB
/
find.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# will be using A* algoritm
import math
import numpy as np
import time
t1 = time.perf_counter()
# checks if node exists
def exists(img, x, y, width, height):
if x < width and x >= 0 and y < height and y >= 0:
return True
else:
return False
# checks if the node is traversable
def is_traversable(img, x, y):
r = img[y][x][0]
g = img[y][x][1]
b = img[y][x][2]
# white and green path
if r >= 250 and g >= 250 and b >= 250:
return True
if r < 10 and g > 220 and b < 10:
return True
else:
return False
# checks if the destination has been reached
def is_destination(img, x, y, ending_position):
if x == ending_position[0] and y == ending_position[1]:
return True
else:
return False
# calculates heuristics (diagonal)
def heuristics(img, x, y, ending_position):
# getting V/H distance
r = img[y][x][0]
g = img[y][x][1]
b = img[y][x][2]
if r >= 250 and g >= 250 and b >= 250:
d1 = 100 # 1 * 10
x_end = ending_position[0]
y_end = ending_position[1]
x_distance = abs(x - x_end)
y_distance = abs(y - y_end)
# getting diagonal distance
d2 = 140 # sqrt(2) * 10
diag_distance = abs(x_distance - y_distance)
#diag_distance = math.sqrt(x_distance * x_distance + y_distance * y_distance)
h = min(x_distance, y_distance) * d1 + diag_distance * d2
#diag_distance = math.sqrt(x_distance * x_distance + y_distance * y_distance)
#h = (x_distance + y_distance) * d1 + diag_distance * d2
#h = (x_distance + y_distance) * d1 + min(x_distance, y_distance) * (d2 - 2 * d1)
elif r < 10 and g > 220 and b < 10:
d1 =1 # 1 * 10
x_end = ending_position[0]
y_end = ending_position[1]
x_distance = abs(x - x_end)
y_distance = abs(y - y_end)
# getting diagonal distance
d2 = 1.4 # sqrt(2) * 10
#diag_distance = math.sqrt(x_distance * x_distance + y_distance *y_distance)
diag_distance = abs(x_distance - y_distance)
h = min(x_distance, y_distance) * d1 + diag_distance * d2
#h = (x_distance+ y_distance) * d1 + diag_distance * d2
#h = (x_distance + y_distance) * d1 +min(x_distance, y_distance) *(d2 - 2 * d1)
return h
# finds the optimized path using the parent coords in node details
# sets the path to the color green
# sets the start and end to the color red
def print_path(img, starting_position, ending_position, path_color, node_details):
endpoint_color = (255, 0, 0)
start_x = starting_position[0]
start_y = starting_position[1]
end_x = ending_position[0]
end_y = ending_position[1]
parent_x = node_details[end_x][end_y][3]
parent_y = node_details[end_x][end_y][4]
img[parent_y][parent_x] = path_color
img[parent_y+1][parent_x+1] = path_color
img[parent_y+1][parent_x-1] = path_color
img[parent_y-1][parent_x-1] = path_color
img[parent_y-1][parent_x+1] = path_color
img[parent_y + 1][parent_x ] = path_color
img[parent_y ][parent_x - 1] = path_color
img[parent_y - 1][parent_x ] = path_color
img[parent_y ][parent_x + 1] = path_color
# will loop until the parent x and y are the starting position
while True:
if parent_x == start_x and parent_y == start_y:
img[start_y][start_x] = endpoint_color
img[end_y][end_x] = endpoint_color
return
# getting the parent coords and then setting the coords to the path color
temp_x = parent_x
temp_y = parent_y
parent_x = node_details[temp_x][temp_y][3]
parent_y = node_details[temp_x][temp_y][4]
img[parent_y][parent_x] = path_color
img[parent_y + 1][parent_x + 1] = path_color
img[parent_y + 1][parent_x - 1] = path_color
img[parent_y - 1][parent_x - 1] = path_color
img[parent_y - 1][parent_x + 1] = path_color
img[parent_y + 1][parent_x] = path_color
img[parent_y][parent_x - 1] = path_color
img[parent_y - 1][parent_x] = path_color
img[parent_y][parent_x + 1] = path_color
# A* alg
def find(img, width, height, starting_position, ending_position, path_color):
# if the user chose the starting and ending position to be the same
if is_destination(img, starting_position[0], starting_position[1], ending_position):
print("The starting and ending position you have chosen is the same.")
print("Exiting program.")
exit()
# creating a list to hold the f, g, and h-values and the parent coords for each node
# 5 for f, g, h values and parent x, y values
node_details = np.full((width, height, 5), -1)
# create a 2D-list that marks all nodes that have been evaluated by 1, 0 if not
closed_list = np.zeros((width, height))
# creating an open list that will contains nodes with calculated f costs
# each index will contain: [f, [x, y]]
open_list = []
# putting the starting position in open list with its f as 0
open_list.append([0, [starting_position[0], starting_position[1]]])
while len(open_list) != 0:
# set current equal to node with the lowest f-value
if len(open_list) == 1:
current = open_list[0]
else:
current = open_list[0]
for node in open_list:
node_x = node[1][0]
node_y = node[1][1]
cur_x = current[1][0]
cur_y = current[1][1]
if node[0] < current[0]:
current = node
# if f-values are equal
elif node[0] == current[0]:
# setting current equal to node if the h-value is lower
node_h = node_details[node_x][node_y][2]
current_h = node_details[cur_x][cur_y][2]
if node_h < current_h:
current = node
# current node coords vars
cur_x = current[1][0]
cur_y = current[1][1]
r = img[cur_y][cur_x][0]
g = img[cur_y][cur_x][1]
b = img[cur_y][cur_x][2]
# removing current node from open list
open_list.remove(current)
# marking node as 1 in closed list
closed_list[cur_x][cur_y] = 1
# checking if current node is the destination
if is_destination(img, cur_x, cur_y, ending_position):
print("Path was successfully found.")
print_path(img, starting_position, ending_position, path_color, node_details)
return
# getting the neighbor nodes// 8 neighbors
else:
neighbor_list = [(cur_x - 1, cur_y), (cur_x + 1, cur_y), (cur_x, cur_y + 1), (cur_x, cur_y - 1),
(cur_x - 1, cur_y + 1), (cur_x - 1, cur_y - 1), (cur_x + 1, cur_y + 1),
(cur_x + 1, cur_y - 1)]
direction_counter = 0
for neighbor in neighbor_list:
# neighbor node x and y vars
n_x = neighbor[0]
n_y = neighbor[1]
# check that neighbor exists, is traversable and is marked as 0 in closed list
if (exists(img, n_x, n_y, width, height) and is_traversable(img, n_x, n_y) and closed_list[n_x][n_y] == 0):
# checking if current node is starting position in order to get right value g
if current[1] == starting_position:
if direction_counter <= 3 and r >= 250 and g >= 250 and b >= 250:
g = 100
elif direction_counter > 3 and r >= 250 and g >= 250 and b >= 250:
g = 140
elif direction_counter <= 3 and r < 10 and g > 220 and b < 10:
g = 1
else:
g = 1.4
else:
if direction_counter <= 3 and r >= 250 and g >= 250 and b >= 250:
g = node_details[cur_x][cur_y][1] + 100
elif direction_counter > 3 and r >= 250 and g >= 250 and b >= 250:
g = node_details[cur_x][cur_y][1] + 140
elif direction_counter <= 3 and r < 10 and g > 220 and b < 10:
g = node_details[cur_x][cur_y][1] + 1
else:
g = node_details[cur_x][cur_y][1] + 1.4
# getting other values
h = heuristics(img, n_x, n_y, ending_position)
print(h)
f = g + h
print(g)
print(f)
# checking if neighbor needs to be updated
# checking if f-cost to neighbor is shorter or if neighbor not in open list
update = True
in_open = False
index = -1
count = 0
for node in open_list:
node_f = node[0]
node_x = node[1][0]
node_y = node[1][1]
if n_x == node_x and n_y == node_y:
in_open = True
index = count
if f >= node_f:
update = False
count += 1
# update node_details and updating open list
if update == True:
# storing details in node details
node_details[n_x][n_y][0] = f
node_details[n_x][n_y][1] = g
node_details[n_x][n_y][2] = h
node_details[n_x][n_y][3] = cur_x
node_details[n_x][n_y][4] = cur_y
# if neighbor is not in open, then append neighbor, else just update f-value
if in_open == False:
open_list.append(([f, [n_x, n_y]]))
else:
open_list[index][0] = f
direction_counter += 1
# print( direction_counter )
# print(f)
print("Path was not found. Exiting program.")
exit()