-
Notifications
You must be signed in to change notification settings - Fork 1
/
distance_calculator.py
164 lines (130 loc) · 7.29 KB
/
distance_calculator.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
import math
import numpy
from heapq import heappush, heappop
def heuristic_cost_estimate(neighbor, goal):
x = neighbor[0] - goal[0]
y = neighbor[1] - goal[1]
return abs(x) + abs(y)
def dist_between(a, b):
return (b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2
def reconstruct_path(came_from, current):
path = [current]
while current in came_from:
current = came_from[current]
path.append(current)
return path
# astar function returns a list of points (shortest path)
def astar(array, start, goal):
debug_array = numpy.array(array)
# 8个方向 (1, 1),(1, -1), (-1, 1), (-1, -1)
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
close_set = set()
came_from = {}
gscore = {start: 0}
fscore = {start: heuristic_cost_estimate(start, goal)}
openSet = []
openSet.append((fscore[start], start)) # 往堆中插入一条新的值
# while openSet is not empty
while openSet:
# debug_array = numpy.array(array)
# for i in range(len(openSet)):
# debug_array[openSet[i][1]] = fscore[openSet[i][1]]
# print(openSet[i][1], fscore[openSet[i][1]])
# print(debug_array)
# current := the node in openSet having the lowest fScore value
current = openSet.pop()[1]
# print(current)
# print("\n")
if current == goal:
return reconstruct_path(came_from, current)
close_set.add(current)
for i, j in directions: # 对当前节点的 8 个相邻节点一一进行检查
neighbor = current[0] + i, current[1] + j
# 判断节点是否在地图范围内,并判断是否为障碍物
if 0 <= neighbor[0] < array.shape[0]:
if 0 <= neighbor[1] < array.shape[1]:
if array[neighbor[0]][neighbor[1]] == 1: # 1为障碍物
continue
else:
# array bound y walls
continue
else:
# array bound x walls
continue
# Ignore the neighbor which is already evaluated.
if neighbor in close_set:
continue
# The distance from start to a neighbor via current
tentative_gScore = gscore[current] + \
dist_between(current, neighbor)
# Discover a new node
if neighbor not in [i[1] for i in openSet]:
came_from[neighbor] = current
gscore[neighbor] = tentative_gScore
fscore[neighbor] = tentative_gScore + heuristic_cost_estimate(neighbor, goal)
openSet.append((fscore.get(neighbor, numpy.inf), neighbor))
# print("openSet sort before:", openSet)
openSet.sort(key=lambda x:x[0], reverse=True)
# print("openSet sort after:", openSet)
elif tentative_gScore < gscore.get(neighbor, numpy.inf):
# This path is the best until now. Record it!
openSet.remove((fscore.get(neighbor, numpy.inf), neighbor))
came_from[neighbor] = current
gscore[neighbor] = tentative_gScore
fscore[neighbor] = tentative_gScore + heuristic_cost_estimate(neighbor, goal)
openSet.append((fscore.get(neighbor, numpy.inf), neighbor))
openSet.sort(key=lambda x:x[0], reverse=True)
# debug_array[neighbor[0]][neighbor[1]] = 6
# print(debug_array)
return False
# def eichilide_distance():
# distance = math.hypot((x - robotlocation.x), (y - robotlocation.y))
# return distance
def astar_distance(map_array, start, goal):
path = astar(map_array, start, goal)
if(path != False):
# for i in range(len(path)):
# map_array[path[i]] = 100
# print(map_array)
# reture the lenght of this path and the next position expected to move
return len(path), path[-2]
return 1, start
if __name__ == "__main__":
# nmap = numpy.array([
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 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