-
Notifications
You must be signed in to change notification settings - Fork 1
/
stA_star.py
191 lines (178 loc) · 8.25 KB
/
stA_star.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
import heapq
import numpy as np
def heuristic(a, b, array,raw_orientation=None,new_orientation=None):
"""
计算两个点之间的曼哈顿距离
"""
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def find_neighbors(point,array=None):
# 小型地图
neighbors=[]
if point[0] in [1,4,7]:
neighbors.append((0,1))
if point[0] in [2,5,8]:
neighbors.append((0,-1))
if point[1] in [0,3,6]:
neighbors.append((1,0))
if point[1] in [1,4,7]:
neighbors.append((-1,0))
'''# 大型地图
if point[0] in [1,4,7,10,13,16,19,22,25]:
neighbors.append((0,1))
if point[0] in [2,5,8,11,14,17,20,23,26]:
neighbors.append((0,-1))
if point[1] in [0,3,6,9,12,15,18,21,24,27]:
neighbors.append((1,0))
if point[1] in [1,4,7,10,13,16,19,22,25,28]:
neighbors.append((-1,0))'''
return neighbors
def update_StTable(raw_map, StTable, path):
if len(StTable) - 1 < path[-1][2]:
new_time_step = path[-1][2] - len(StTable) + 1
new_time_step_map = np.repeat(np.expand_dims(raw_map,axis=0), new_time_step, axis=0)
for node in path[len(path) - new_time_step :]:
new_time_step_map[node[2]-len(StTable)][node[0]][node[1]] = 6
StTable = np.concatenate([StTable,new_time_step_map],axis=0)
for node in path[:len(path) - new_time_step]:
StTable[node[2]][node[0]][node[1]] = 6
else:
for node in path:
StTable[node[2]][node[0]][node[1]] = 6
return StTable
def update_start(StTable, start):
if len(StTable) - 1 >= start[2]:
# 检查新的start是否已经被使用过
if is_start_used(StTable, start):
return update_start(StTable, (start[0], start[1], start[2] + 1))
return start
def is_start_used(StTable, start):
# 检查start是否占用
return StTable[start[2]][start[0]][start[1]] == 6
def StAstar(array, start, goal, StTable):
"""
A* 寻路算法
"""
if len(StTable) - 1 >= start[2]:
while StTable[start[2]][start[0]][start[1]] == 6:
start = update_start(StTable,start)
close_set = set()
came_from = {}
gscore = {start:0}
fscore = {start:heuristic(start, goal, array)}
oheap = []
# 初始化方向,位于上方入口(行索引为0),初始方向向下;位于下方入口,初始方向向上
if start[0] == 0:
last_orientation = (1,0)
if start[0] == len(array)-1:
last_orientation = (-1,0)
heapq.heappush(oheap, (fscore[start], start))
while oheap:
# 标记一下这次有没有找到路
signal=0
current = heapq.heappop(oheap)[1]
close_set.add(current)
# 判断是否成功到达卸货单元
for neighbor in [(0,1),(0,-1),(1,0),(-1,0)]:
if (current[0]+neighbor[0],current[1]+neighbor[1]) == goal:
data = []
data.append(goal)
while current in came_from:
data.append(current)
current = came_from[current]
data.append(start)
return data[::-1], data[1][2] - data[-1][2]
# 判断该点可达的邻居
neighbors=find_neighbors(current,array)
for i, j in neighbors:
# 判断是否转向
if current in came_from.keys():
if current[0] != came_from[current][0] or current[1] != came_from[current][1]:
last_orientation=(current[0]-came_from[current][0],current[1]-came_from[current][1]) #若停留的话last_orientation会成为(0,0),修改后停留时方向不变
#判断是否转向,若转向则时间步+2,否则时间步+1
if last_orientation != (i,j):
neighbor = current[0] + i, current[1] + j, current[2] + 2
if last_orientation == (i,j):
neighbor = current[0] + i, current[1] + j, current[2] + 1
# 约束:1.静态障碍物; 2.地图边界; 3.动态障碍
if 0 <= neighbor[0] < array.shape[0]:
if 0 <= neighbor[1] < array.shape[1]:
# 判断是否为障碍物 此处可以改成其他限制条件
if array[neighbor[0]][neighbor[1]] == 5:
signal = 0
continue
if array[neighbor[0]][neighbor[1]] == 4 and neighbor != goal:
signal = 0
continue
#判断是否为动态障碍物,即优先级高的AGV在该时间步的位置
if neighbor[2]+1 <= len(StTable):
if StTable[neighbor[2]][neighbor[0]][neighbor[1]] == 6:
signal = 0
continue
#若要在原地转向等待一个时间步,需要判断当前节点时间步+1是否被优先级更高任务占用
if last_orientation != (i,j):
if StTable[current[2]+1][current[0]][current[1]] == 6:
signal = 0
continue
else:
# 超出边界
signal = 0
continue
else:
# 超出边界
signal = 0
continue
# 如果要转向,距离额外加1
if last_orientation!=(i,j):
tentative_g_score = gscore[current] + heuristic(current, neighbor, array) + 1
else:
tentative_g_score = gscore[current] + heuristic(current, neighbor, array)
# 如果距离更远,排除
if neighbor in close_set and tentative_g_score > gscore.get(neighbor, 0):
signal = 0
continue
# 如果距离更近,更新
if tentative_g_score <= gscore.get(neighbor, 0) or (neighbor not in [i[1]for i in oheap]):
#若时间步差1,则表示无转向和等待
if neighbor[2] - current[2] == 1:
came_from[neighbor] = current
gscore[neighbor] = tentative_g_score
fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal,array)
heapq.heappush(oheap, (fscore[neighbor], neighbor))
signal=1
#若时间步差2,则表示转向和等待
if neighbor[2] - current[2] > 1:
tmp = current[0], current[1], current[2] + 1
came_from[tmp] = current
came_from[neighbor] = tmp
gscore[neighbor] = tentative_g_score
fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal,array)
heapq.heappush(oheap, (fscore[neighbor], neighbor))
signal=1
'''
came_from[neighbor] = current
gscore[neighbor] = tentative_g_score
fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal,array)
heapq.heappush(oheap, (fscore[neighbor], neighbor))
signal=1
'''
#若没有搜寻到路径则在原地等待
if signal == 0:
if len(StTable)-1 >= current[2]+1:
if StTable[current[2]+1][current[0]][current[1]] != 6:
tentative_g_score = gscore[current]+1
current=(current[0] , current[1] , current[2]+1)
came_from[current] = (current[0] , current[1] , current[2]-1)
gscore[current] = tentative_g_score
fscore[current] = tentative_g_score + heuristic(current, goal,array)
heapq.heappush(oheap, (fscore[current], current))
#下一时刻被前序任务占用
else:
continue
else:
tentative_g_score = gscore[current]+1
current=(current[0] , current[1] , current[2]+1)
came_from[current] = (current[0] , current[1] , current[2]-1)
gscore[current] = tentative_g_score
fscore[current] = tentative_g_score + heuristic(current, goal,array)
heapq.heappush(oheap, (fscore[current], current))
return None