-
Notifications
You must be signed in to change notification settings - Fork 3
/
jps.py
254 lines (189 loc) · 6.28 KB
/
jps.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
from __future__ import annotations
from types import FunctionType
from solver.base import BaseSolver
from solver.pruning.base import BasePruning
from graph.grid import GridMap
from graph.node import Node
from utils.utils import getDirection
class JPS(BaseSolver):
def __init__(
self,
h_func: FunctionType,
prune: BasePruning,
) -> JPS:
super().__init__(h_func, prune)
def getSuccessors(
self,
state: Node,
goal: Node,
grid: GridMap,
k: int,
) -> list:
nodes = [
Node(
i = i,
j = j,
h = self.h_func(i, j, goal.i, goal.j),
parent = state,
k = k,
)
for i, j in filter(None, self.filteredSuccessors(state, goal, grid))
]
return nodes
def filteredSuccessors(
self,
state: Node,
goal: Node,
grid: GridMap,
) -> list:
optimal = self.prune.getOptimalDirections(state, goal)
allow = self.getAllowedDirections(state)
recommend = optimal - allow
successors = [
self.getJumpPoint(state.i, state.j, delta[0], delta[1], goal, grid)
for delta in grid.getAllowedMovements(state.i, state.j)
if delta in recommend
]
return successors
def getAllowedDirections(
self,
state: Node,
) -> set:
directions = set()
parent = state.parent
if parent is None:
return directions
dx, dy = getDirection(parent.i, parent.j, state.i, state.j)
#diag
if dx != 0 and dy != 0:
directions = {
(dx, dy),
(dx, 0),
( 0, dy),
}
#horisontal
elif dx == 0:
directions = {
(dx, dy),
(+1, 0),
(-1, 0),
(+1, dy),
(-1, dy),
}
#vertical
else:
directions = {
(dx, dy),
( 0, +1),
( 0, -1),
(dx, +1),
(dx, -1),
}
return directions
def isDiagonalJumpPoint(
self,
i: int,
j: int,
dx: int,
dy: int,
grid: GridMap,
) -> bool:
down = grid.traversable(i, j, -dx, +dy) and not grid.traversable(i, j, -dx, 0)
up = grid.traversable(i, j, +dx, -dy) and not grid.traversable(i, j, 0, -dy)
return up or down
def isVerticalJumpPoint(
self,
i: int,
j: int,
dx: int,
dy: int,
grid: GridMap,
) -> bool:
up = grid.traversable(i, j, dx, +1) and not grid.traversable(i, j, 0, +1)
down = grid.traversable(i, j, dx, -1) and not grid.traversable(i, j, 0, -1)
return up or down
def isHorisontalJumpPoint(
self,
i: int,
j: int,
dx: int,
dy: int,
grid: GridMap,
) -> bool:
up = grid.traversable(i, j, +1, dy) and not grid.traversable(i, j, +1, 0)
down = grid.traversable(i, j, -1, dy) and not grid.traversable(i, j, -1, 0)
return up or down
def getJumpPoint(
self,
i: int,
j: int,
dx: int,
dy: int,
goal: Node,
grid: GridMap,
) -> tuple:
if not grid.traversable(i, j, dx, dy):
return None
x, y = i + dx, j + dy
if x == goal.i and y == goal.j:
return x, y
while True:
if self.checkJumpPoint(x, y, dx, dy, grid):
return x, y
#diag
if dx != 0 and dy != 0:
for e_dx, e_dy in [(dx, 0), (0, dy)]:
if self.getJumpPoint(x, y, e_dx, e_dy, goal, grid) is not None:
return x, y
if not grid.traversable(x, y, dx, dy):
return None
x += dx
y += dy
if x == goal.i and y == goal.j:
return x, y
def checkJumpPoint(
self,
i: int,
j: int,
dx: int,
dy: int,
grid: GridMap,
) -> list:
if dx != 0 and dy != 0:
return self.isDiagonalJumpPoint(i, j, dx, dy, grid)
elif dx == 0:
return self.isHorisontalJumpPoint(i, j, dx, dy, grid)
elif dy == 0:
return self.isVerticalJumpPoint(i, j, dx, dy, grid)
return False
def getForsedDirections(
self,
i: int,
j: int,
dx: int,
dy: int,
grid: GridMap,
) -> list:
forced = []
#diag
if dx != 0 and dy != 0:
for edge in [(dx, 0), (0, dy)]:
if grid.traversable(i, j, edge[0], edge[1]):
forced.append(edge)
f_edge = (dx - 2*edge[0], dy - 2*edge[1])
if not grid.traversable(i, j, -edge[0], -edge[1] ) \
and grid.traversable(i, j, +f_edge[0], +f_edge[1]):
forced.append(f_edge)
#horisontal
elif dx == 0:
for e_dx in [1, -1]:
if not grid.traversable(i, j, e_dx, +0) \
and grid.traversable(i, j, e_dx, dy):
forced.append((e_dx, dy))
#vertical
else:
for e_dy in [1, -1]:
if not grid.traversable(i, j, +0, e_dy) \
and grid.traversable(i, j, dx, e_dy):
forced.append((dx, e_dy))
return forced