-
Notifications
You must be signed in to change notification settings - Fork 3
/
jpsplus.py
196 lines (143 loc) · 5.23 KB
/
jpsplus.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
from __future__ import annotations
import itertools
import collections
from types import FunctionType
from solver.base import BaseSolver
from solver.jps import JPS
from solver.pruning.base import BasePruning
from graph.node import Node
from graph.grid import GridMap
from utils.utils import getDirection
class JPSPlus(JPS):
def __init__(
self,
h_func: FunctionType,
prune: BasePruning,
) -> JPSPlus:
super().__init__(h_func, prune)
def addXdim2Goal_(
self,
i: int,
j: int,
dx: int,
dy: int,
p_i: int,
p_j: int,
g_i: int,
g_j: int,
data: list,
) -> bool:
if (i - g_i) * (p_i - g_i) <= 0:
x = g_i
y = (g_i - p_i) * dx * dy + p_j
point = (x, y)
if x == g_i and y == g_j:
data.append(point)
return True
to_goal = getDirection(g_i, g_j, x, y)
if to_goal in self.jump_points[point]:
check_x, check_y = self.jump_points[point][to_goal]
if abs(check_y - y) >= abs(g_j - y):
data.append(point)
return False
return None
def addYdim2Goal_(
self,
i: int,
j: int,
dx: int,
dy: int,
p_i: int,
p_j: int,
g_i: int,
g_j: int,
data: list,
) -> bool:
if (j - g_j) * (p_j - g_j) <= 0:
x = (g_j - p_j) * dx * dy + p_i
y = g_j
point = (x, y)
if x == g_i and y == g_j:
data.append(point)
return True
to_goal = getDirection(g_i, g_j, x, y)
if to_goal in self.jump_points[point]:
check_x, check_y = self.jump_points[point][to_goal]
if abs(check_x - x) >= abs(g_i - x):
data.append(point)
return False
return None
def filteredSuccessors(
self,
state: Node,
goal: Node,
grid: GridMap,
) -> list:
optimal = self.prune.getOptimalDirections(state, goal)
allow = self.getAllowedDirections(state)
recommend = optimal - allow
current = (state.i, state.j)
base_successors = [
(self.jump_points[current][edge], edge)
for edge in self.jump_points[current]
if edge in recommend
]
forced_successors = []
for (i, j), (dx, dy) in base_successors:
#diag
if dx != 0 and dy != 0:
if self.addXdim2Goal_(i, j, dx, dy, state.i, state.j, goal.i, goal.j, forced_successors) or \
self.addYdim2Goal_(i, j, dx, dy, state.i, state.j, goal.i, goal.j, forced_successors):
break
#horisontal
elif dx == 0:
if self.addYdim2Goal_(i, j, dx, dy, state.i, state.j, goal.i, goal.j, forced_successors):
break
else:
if self.addXdim2Goal_(i, j, dx, dy, state.i, state.j, goal.i, goal.j, forced_successors):
break
forced_successors.append((i, j))
return forced_successors
def getJumpPoint(
self,
i: int,
j: int,
dx: int,
dy: int,
grid: GridMap,
) -> tuple:
if not grid.traversable(i, j, dx, dy):
return (i, j), False
x, y = i + dx, j + dy
while True:
if self.checkJumpPoint(x, y, dx, dy, grid):
return (x, y), True
#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, grid)[1]:
return (x, y), True
if not grid.traversable(x, y, dx, dy):
return (x, y), False
x += dx
y += dy
def doPreprocess(
self,
grid: GridMap,
) -> None:
if not self.preprocessed:
self.prune.preprocess(self.getForsedDirections, grid)
self.computeJumpPoints(grid)
self.preprocessed = True
def computeJumpPoints(
self,
grid: GridMap,
) -> None:
self.jump_points = collections.defaultdict(dict)
for node in itertools.product(range(grid.height), range(grid.width)):
if grid.isObstacle(node[0], node[1]):
continue
for edge in grid.getAllowedMovements(node[0], node[1]):
jp, _ = self.getJumpPoint(node[0], node[1], edge[0], edge[1], grid)
if jp is not None:
self.jump_points[node][edge] = jp