Skip to content

Commit ebbcc21

Browse files
committed
PEA* seems almost ready what comes to animation.
1 parent 989e4c3 commit ebbcc21

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

src/main/java/io/github/coderodde/pathfinding/finders/PEAStarFinder.java

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.github.coderodde.pathfinding.finders;
22

3+
import static io.github.coderodde.pathfinding.finders.Finder.searchSleep;
34
import io.github.coderodde.pathfinding.heuristics.HeuristicFunction;
45
import io.github.coderodde.pathfinding.logic.GridCellNeighbourIterable;
56
import io.github.coderodde.pathfinding.logic.PathfindingSettings;
67
import io.github.coderodde.pathfinding.logic.SearchState;
78
import io.github.coderodde.pathfinding.logic.SearchStatistics;
89
import io.github.coderodde.pathfinding.model.GridModel;
910
import io.github.coderodde.pathfinding.utils.Cell;
11+
import io.github.coderodde.pathfinding.utils.CellType;
1012
import java.util.HashMap;
1113
import java.util.HashSet;
1214
import java.util.List;
@@ -51,11 +53,16 @@ public List<Cell> findPath(GridModel model,
5153
open.add(new HeapNode(source, F.get(source)));
5254

5355
while (!open.isEmpty()) {
54-
// System.out.println("main loop");
5556
HeapNode heapNode = open.remove();
5657
Cell cell = heapNode.cell;
5758
openSet.remove(cell);
58-
System.out.println(cell);
59+
searchStatistics.decrementOpened();
60+
searchStatistics.incrementVisited();
61+
62+
if (!cell.getCellType().equals(CellType.SOURCE) &&
63+
!cell.getCellType().equals(CellType.TARGET)) {
64+
model.setCellType(cell, CellType.VISITED);
65+
}
5966

6067
if (cell.equals(target)) {
6168
return tracebackPath(target, p);
@@ -66,8 +73,6 @@ public List<Cell> findPath(GridModel model,
6673
neighbourIterable.setStartingCell(cell);
6774

6875
for (Cell child : neighbourIterable) {
69-
// System.out.println("first inner loop");)
70-
7176
double tentativeDistance = (g.containsKey(child))
7277
? g.get(child)
7378
: g.get(cell) + ps.getWeight(cell, child);
@@ -79,21 +84,8 @@ public List<Cell> findPath(GridModel model,
7984
} else {
8085
aboveSet.add(child);
8186
}
82-
//
83-
// if (g.containsKey(child)) {
84-
// double f = g.get(child) + h.estimate(child, target);
85-
//
86-
//
87-
//
88-
// if (f <= F.get(cell) + C) {
89-
// belowSet.add(child);
90-
// } else {
91-
// aboveSet.add(child);
92-
// }
93-
// } else {
94-
// System.out.println("shit");
95-
// aboveSet.add(child);
96-
// }
87+
88+
searchStatistics.incrementOpened();;
9789
}
9890

9991
for (Cell child : belowSet) {
@@ -102,6 +94,21 @@ public List<Cell> findPath(GridModel model,
10294

10395
if (!openSet.contains(child) && !closed.contains(child)) {
10496

97+
if (searchState.haltRequested()) {
98+
return List.of();
99+
}
100+
101+
while (searchState.pauseRequested()) {
102+
searchSleep(ps);
103+
104+
if (searchState.haltRequested()) {
105+
return List.of();
106+
}
107+
}
108+
109+
searchStatistics.incrementOpened();
110+
searchSleep(ps);
111+
105112
g.put(child, tentativeDistance);
106113
F.put(child, tentativeDistance + h.estimate(child, target));
107114
p.put(child, cell);
@@ -111,26 +118,60 @@ public List<Cell> findPath(GridModel model,
111118
} else if (openSet.contains(child)
112119
&& tentativeDistance < g.get(child)) {
113120

121+
if (searchState.haltRequested()) {
122+
return List.of();
123+
}
124+
125+
while (searchState.pauseRequested()) {
126+
searchSleep(ps);
127+
128+
if (searchState.haltRequested()) {
129+
return List.of();
130+
}
131+
}
132+
133+
searchStatistics.incrementOpened();
134+
searchSleep(ps);
135+
114136
g.put(child, tentativeDistance);
115137
F.put(child, tentativeDistance + h.estimate(child, target));
116138
p.put(child, cell);
117139

118140
} else if (closed.contains(child)
119141
&& tentativeDistance < g.get(child)) {
120142

143+
if (searchState.haltRequested()) {
144+
return List.of();
145+
}
146+
147+
while (searchState.pauseRequested()) {
148+
searchSleep(ps);
149+
150+
if (searchState.haltRequested()) {
151+
return List.of();
152+
}
153+
}
154+
155+
searchStatistics.incrementOpened();
156+
searchSleep(ps);
157+
121158
g.put(child, tentativeDistance);
122159
F.put(child, tentativeDistance + h.estimate(child, target));
123160
p.put(child, cell);
124161
closed.remove(child);
125162
openSet.add(child);
126163
open.add(new HeapNode(child, (double) F.get(child)));
127164
}
165+
166+
if (!child.getCellType().equals(CellType.SOURCE) &&
167+
!child.getCellType().equals(CellType.TARGET)) {
168+
model.setCellType(child, CellType.OPENED);
169+
}
128170
}
129171

130172
if (aboveSet.isEmpty()) {
131173
closed.add(cell);
132174
} else {
133-
// System.out.println("if at end");
134175
double fmin = Double.POSITIVE_INFINITY;
135176

136177
for (Cell c : aboveSet) {
@@ -147,6 +188,13 @@ public List<Cell> findPath(GridModel model,
147188
F.put(cell, fmin);
148189
openSet.add(cell);
149190
open.add(new HeapNode(cell, fmin));
191+
192+
if (!cell.getCellType().equals(CellType.SOURCE) &&
193+
!cell.getCellType().equals(CellType.TARGET)) {
194+
model.setCellType(cell, CellType.OPENED);
195+
}
196+
197+
searchStatistics.incrementOpened();
150198
}
151199
}
152200

Binary file not shown.

0 commit comments

Comments
 (0)