forked from Ohohcakester/Any-Angle-Pathfinding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStarStaticMemory.java
More file actions
280 lines (213 loc) · 7.95 KB
/
Copy pathAStarStaticMemory.java
File metadata and controls
280 lines (213 loc) · 7.95 KB
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
package algorithms;
import grid.GridGraph;
import algorithms.datatypes.Memory;
import algorithms.priorityqueue.ReusableIndirectHeap;
public class AStarStaticMemory extends PathFindingAlgorithm {
protected boolean postSmoothingOn = false;
protected boolean repeatedPostSmooth = false;
protected float heuristicWeight = 1f;
protected ReusableIndirectHeap pq;
protected int finish;
public AStarStaticMemory(GridGraph graph, int sx, int sy, int ex, int ey) {
super(graph, graph.sizeX, graph.sizeY, sx, sy, ex, ey);
}
public static AStarStaticMemory postSmooth(GridGraph graph, int sx, int sy, int ex, int ey) {
AStarStaticMemory aStar = new AStarStaticMemory(graph, sx, sy, ex, ey);
aStar.postSmoothingOn = true;
aStar.repeatedPostSmooth = false;
return aStar;
}
public static AStarStaticMemory repeatedPostSmooth(GridGraph graph, int sx, int sy, int ex, int ey) {
AStarStaticMemory aStar = new AStarStaticMemory(graph, sx, sy, ex, ey);
aStar.postSmoothingOn = true;
aStar.repeatedPostSmooth = true;
return aStar;
}
public static AStarStaticMemory dijkstra(GridGraph graph, int sx, int sy, int ex, int ey) {
AStarStaticMemory aStar = new AStarStaticMemory(graph, sx, sy, ex, ey);
aStar.heuristicWeight = 0;
return aStar;
}
@Override
public void computePath() {
int totalSize = (graph.sizeX+1) * (graph.sizeY+1);
int start = toOneDimIndex(sx, sy);
finish = toOneDimIndex(ex, ey);
pq = new ReusableIndirectHeap(totalSize);
this.initialiseMemory(totalSize, Float.POSITIVE_INFINITY, -1, false);
initialise(start);
float lastDist = -1;
while (!pq.isEmpty()) {
float dist = pq.getMinValue();
int current = pq.popMinIndex();
//if (Math.abs(dist - lastDist) > 0.01f) { maybeSaveSearchSnapshot(); lastDist = dist;}
maybeSaveSearchSnapshot();
if (current == finish || distance(current) == Float.POSITIVE_INFINITY) {
maybeSaveSearchSnapshot();
break;
}
setVisited(current, true);
int x = toTwoDimX(current);
int y = toTwoDimY(current);
tryRelaxNeighbour(current, x, y, x-1, y-1);
tryRelaxNeighbour(current, x, y, x, y-1);
tryRelaxNeighbour(current, x, y, x+1, y-1);
tryRelaxNeighbour(current, x, y, x-1, y);
tryRelaxNeighbour(current, x, y, x+1, y);
tryRelaxNeighbour(current, x, y, x-1, y+1);
tryRelaxNeighbour(current, x, y, x, y+1);
tryRelaxNeighbour(current, x, y, x+1, y+1);
//maybeSaveSearchSnapshot();
}
maybePostSmooth();
}
protected void tryRelaxNeighbour(int current, int currentX, int currentY, int x, int y) {
if (!graph.isValidCoordinate(x, y))
return;
int destination = toOneDimIndex(x,y);
if (visited(destination))
return;
if (!graph.neighbourLineOfSight(currentX, currentY, x, y))
return;
if (relax(current, destination, weight(currentX, currentY, x, y))) {
// If relaxation is done.
pq.decreaseKey(destination, distance(destination) + heuristic(x,y));
}
}
protected float heuristic(int x, int y) {
//return 0;
return heuristicWeight*graph.distance(x, y, ex, ey);
}
protected float weight(int x1, int y1, int x2, int y2) {
return graph.distance(x1, y1, x2, y2);
}
protected boolean relax(int u, int v, float weightUV) {
// return true iff relaxation is done.
float newWeight = distance(u) + weightUV;
if (newWeight < distance(v)) {
setDistance(v, newWeight);
setParent(v, u);
//maybeSaveSearchSnapshot();
return true;
}
return false;
}
protected final void initialise(int s) {
pq.decreaseKey(s, 0f);
Memory.setDistance(s, 0f);
}
private int pathLength() {
int length = 0;
int current = finish;
while (current != -1) {
current = parent(current);
length++;
}
return length;
}
protected final boolean lineOfSight(int node1, int node2) {
int x1 = toTwoDimX(node1);
int y1 = toTwoDimY(node1);
int x2 = toTwoDimX(node2);
int y2 = toTwoDimY(node2);
return graph.lineOfSight(x1, y1, x2, y2);
}
protected final float physicalDistance(int node1, int node2) {
int x1 = toTwoDimX(node1);
int y1 = toTwoDimY(node1);
int x2 = toTwoDimX(node2);
int y2 = toTwoDimY(node2);
return graph.distance(x1, y1, x2, y2);
}
protected final float physicalDistance(int x1, int y1, int node2) {
int x2 = toTwoDimX(node2);
int y2 = toTwoDimY(node2);
return graph.distance(x1, y1, x2, y2);
}
protected void maybePostSmooth() {
if (postSmoothingOn) {
if (repeatedPostSmooth) {
while(postSmooth());
} else {
postSmooth();
}
}
}
private boolean postSmooth() {
boolean didSomething = false;
int current = finish;
while (current != -1) {
int next = parent(current); // we can skip checking this one as it always has LoS to current.
if (next != -1) {
next = parent(next);
while (next != -1) {
if (lineOfSight(current,next)) {
setParent(current, next);
next = parent(next);
didSomething = true;
maybeSaveSearchSnapshot();
} else {
next = -1;
}
}
}
current = parent(current);
}
return didSomething;
}
public int[][] getPath() {
int length = pathLength();
int[][] path = new int[length][];
int current = finish;
int index = length-1;
while (current != -1) {
int x = toTwoDimX(current);
int y = toTwoDimY(current);
path[index] = new int[2];
path[index][0] = x;
path[index][1] = y;
index--;
current = parent(current);
}
return path;
}
public float getPathLength() {
int current = finish;
if (current == -1) return -1;
float pathLength = 0;
int prevX = toTwoDimX(current);
int prevY = toTwoDimY(current);
current = parent(current);
while (current != -1) {
int x = toTwoDimX(current);
int y = toTwoDimY(current);
pathLength += graph.distance(x, y, prevX, prevY);
current = parent(current);
prevX = x;
prevY = y;
}
return pathLength;
}
@Override
protected final boolean selected(int index) {
return visited(index);
}
protected final int parent(int index) {
return Memory.parent(index);
}
protected final void setParent(int index, int value) {
Memory.setParent(index, value);
}
protected final float distance(int index) {
return Memory.distance(index);
}
protected final void setDistance(int index, float value) {
Memory.setDistance(index, value);
}
protected final boolean visited(int index) {
return Memory.visited(index);
}
protected final void setVisited(int index, boolean value) {
Memory.setVisited(index, value);
}
}