Skip to content

Commit 989e4c3

Browse files
committed
PEAStarFinder seems to return valid paths.
1 parent 0563b40 commit 989e4c3

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

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

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ public List<Cell> findPath(GridModel model,
4848
F.put(source, h.estimate(source, target));
4949
p.put(source, null);
5050
openSet.add(source);
51-
open.add(new HeapNode(source, 0.0));
51+
open.add(new HeapNode(source, F.get(source)));
5252

5353
while (!open.isEmpty()) {
54+
// System.out.println("main loop");
5455
HeapNode heapNode = open.remove();
5556
Cell cell = heapNode.cell;
57+
openSet.remove(cell);
58+
System.out.println(cell);
5659

5760
if (cell.equals(target)) {
5861
return tracebackPath(target, p);
@@ -63,39 +66,61 @@ public List<Cell> findPath(GridModel model,
6366
neighbourIterable.setStartingCell(cell);
6467

6568
for (Cell child : neighbourIterable) {
66-
if (g.containsKey(child)) {
67-
double f = g.get(child) + h.estimate(child, target);
68-
69-
if (f <= F.get(cell) + C) {
70-
belowSet.add(child);
71-
} else {
72-
aboveSet.add(child);
73-
}
69+
// System.out.println("first inner loop");)
70+
71+
double tentativeDistance = (g.containsKey(child))
72+
? g.get(child)
73+
: g.get(cell) + ps.getWeight(cell, child);
74+
75+
double f = tentativeDistance + h.estimate(child, target);
76+
77+
if (f <= F.get(cell) + C) {
78+
belowSet.add(child);
7479
} else {
7580
aboveSet.add(child);
7681
}
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+
// }
7797
}
7898

7999
for (Cell child : belowSet) {
100+
double tentativeDistance = g.get(cell)
101+
+ ps.getWeight(cell, child);
102+
80103
if (!openSet.contains(child) && !closed.contains(child)) {
81-
double gScore = g.get(cell) + ps.getWeight(cell, child);
82-
F.put(child, gScore + h.estimate(child, target));
104+
105+
g.put(child, tentativeDistance);
106+
F.put(child, tentativeDistance + h.estimate(child, target));
107+
p.put(child, cell);
83108
openSet.add(child);
84109
open.add(new HeapNode(child, (double) F.get(child)));
110+
85111
} else if (openSet.contains(child)
86-
&& g.get(cell) +
87-
ps.getWeight(cell, child) < g.get(child)) {
112+
&& tentativeDistance < g.get(child)) {
88113

89-
g.put(child, g.get(cell) + ps.getWeight(cell, child));
90-
F.put(child, g.get(child) + h.estimate(child, target));
114+
g.put(child, tentativeDistance);
115+
F.put(child, tentativeDistance + h.estimate(child, target));
116+
p.put(child, cell);
91117

92118
} else if (closed.contains(child)
93-
&& g.get(cell) +
94-
ps.getWeight(cell, child) < g.get(child)) {
95-
96-
g.put(child, g.get(cell) + ps.getWeight(cell, child));
97-
F.put(child, g.get(child) + h.estimate(child, target));
119+
&& tentativeDistance < g.get(child)) {
98120

121+
g.put(child, tentativeDistance);
122+
F.put(child, tentativeDistance + h.estimate(child, target));
123+
p.put(child, cell);
99124
closed.remove(child);
100125
openSet.add(child);
101126
open.add(new HeapNode(child, (double) F.get(child)));
@@ -105,11 +130,18 @@ public List<Cell> findPath(GridModel model,
105130
if (aboveSet.isEmpty()) {
106131
closed.add(cell);
107132
} else {
133+
// System.out.println("if at end");
108134
double fmin = Double.POSITIVE_INFINITY;
109135

110136
for (Cell c : aboveSet) {
111-
fmin = Math.min(fmin,
112-
g.get(c) + h.estimate(c, target));
137+
double tentativeGScore =
138+
g.containsKey(c) ?
139+
g.get(c) :
140+
g.get(cell) + ps.getWeight(cell, c);
141+
142+
double f = tentativeGScore + h.estimate(c, target);
143+
144+
fmin = Math.min(fmin, f);
113145
}
114146

115147
F.put(cell, fmin);
Binary file not shown.

0 commit comments

Comments
 (0)