Skip to content

Commit f30ecbe

Browse files
committed
Committing on my erroneous work on DCBSS.
1 parent 6c45b0f commit f30ecbe

File tree

4 files changed

+39
-47
lines changed

4 files changed

+39
-47
lines changed

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

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ public List<Cell> findPath(GridModel model,
3737
SearchStatistics searchStatistics) {
3838

3939
Deque<BeamStackEntry> beamStack = new ArrayDeque<>();
40-
beamStack.push(new BeamStackEntry(0, Double.POSITIVE_INFINITY));
40+
beamStack.push(new BeamStackEntry(0.0, Double.POSITIVE_INFINITY));
41+
4142
List<Cell> optimalPath = null;
4243
DoubleHolder U = new DoubleHolder();
4344
U.value = Double.POSITIVE_INFINITY;
4445

4546
while (!beamStack.isEmpty()) {
4647
System.out.println("outermost");
47-
List<Cell> path = null;
48+
List<Cell> path;
4849

4950
try {
5051
path = search(model,
@@ -213,8 +214,11 @@ private static List<Cell> search(GridModel model,
213214

214215
if (bse.fmin <= f && f < bse.fmax) {
215216
Queue<HeapNode> nextOpen = open.get(layerIndex + 1);
216-
nextOpen.removeIf(c -> c.equals(child));
217+
218+
// Add for the first time or improve the g-cost:
219+
nextOpen.removeIf(c -> c.cell.equals(child));
217220
nextOpen.add(new HeapNode(child, f));
221+
218222
g.put(child, tentativeGscore);
219223
p.put(child, cell);
220224
searchStatistics.incrementOpened();
@@ -226,32 +230,31 @@ private static List<Cell> search(GridModel model,
226230
}
227231
}
228232

229-
if (open.get(layerIndex + 1).size() >
230-
pathfindingSettings.getBeamWidth()) {
231-
233+
PriorityQueue<HeapNode> nextOpen = open.get(layerIndex + 1);
234+
235+
if (nextOpen.size() > pathfindingSettings.getBeamWidth()) {
232236
double fMinPruned = pruneLayer(model,
233-
open.get(layerIndex + 1),
237+
nextOpen,
234238
pathfindingSettings,
235-
searchStatistics);
239+
searchStatistics);
236240

237241
prunedAtThisLayer = true;
238242
nextBound = Math.min(nextBound, fMinPruned);
239-
System.out.println("pruned: " + open.get(layerIndex + 1).size());
240243
}
241244
}
242245

243246
BeamStackEntry bse = beamStack.peek();
244-
bse.fmax = prunedAtThisLayer ? nextBound : Double.POSITIVE_INFINITY;
247+
bse.fmax = prunedAtThisLayer ?
248+
nextBound :
249+
Double.POSITIVE_INFINITY;
245250

246251
layerIndex++;
247252
open.put(layerIndex + 1, new PriorityQueue<>());
248253
closed.put(layerIndex, new HashSet<>());
249254

250255
if (beamStack.size() == layerIndex) {
251-
beamStack.push(new BeamStackEntry(0, U.value));
256+
beamStack.push(new BeamStackEntry(0.0, U.value));
252257
}
253-
254-
System.out.println("hello");
255258
}
256259

257260
for (PriorityQueue<HeapNode> queue : open.values()) {
@@ -281,11 +284,9 @@ private static List<Cell> search(GridModel model,
281284
System.out.println("before path returning");
282285
if (bestGoal != null) {
283286
List<Cell> path = new ArrayList<>();
284-
Cell cell = bestGoal;
285287

286-
while (cell != null) {
288+
for (Cell cell = bestGoal; cell != null; cell = p.get(cell)) {
287289
path.add(cell);
288-
cell = p.get(cell);
289290
}
290291

291292
Collections.reverse(path);
@@ -297,56 +298,47 @@ private static List<Cell> search(GridModel model,
297298
return null;
298299
}
299300

300-
private static boolean pruneLayer(GridModel model,
301-
PriorityQueue<HeapNode> open,
302-
PathfindingSettings pathfindingSettings,
303-
SearchStatistics searchStatistics) {
301+
private static double pruneLayer(GridModel model,
302+
PriorityQueue<HeapNode> open,
303+
PathfindingSettings pathfindingSettings,
304+
SearchStatistics searchStatistics) {
305+
306+
List<HeapNode> all = new ArrayList<>(open);
307+
searchStatistics.addToOpened(-all.size());
304308

305-
List<HeapNode> keep = new ArrayList<>(open);
306-
searchStatistics.addToOpened(-keep.size());
307-
for (HeapNode node : keep) {
309+
for (HeapNode node : all) {
308310
Cell cell = node.cell;
309311

310312
if (!cell.getCellType().equals(CellType.TARGET)) {
311313
model.setCellType(cell, CellType.FREE);
312314
}
313315
}
314316

315-
keep.sort((a, b) -> {
316-
return Double.compare(a.f, b.f);
317-
});
317+
all.sort((a, b) -> Double.compare(a.f, b.f));
318318

319-
keep = keep.subList(0, pathfindingSettings.getBeamWidth());
320-
searchStatistics.addToOpened(keep.size());
321-
Set<Cell> keepSet = new HashSet<>();
319+
List<HeapNode> keep =
320+
all.subList(0, pathfindingSettings.getBeamWidth());
321+
322+
double fMinPruned = Double.POSITIVE_INFINITY;
322323

323324
for (HeapNode heapNode : keep) {
325+
fMinPruned = Math.min(fMinPruned, heapNode.f);
326+
}
327+
328+
open.clear();
329+
330+
for (HeapNode heapNode : keep) {
331+
open.add(heapNode);
324332
Cell cell = heapNode.cell;
325-
keepSet.add(cell);
326333

327334
if (!cell.getCellType().equals(CellType.TARGET)) {
328335
model.setCellType(cell, CellType.OPENED);
329336
}
330337
}
331338

332-
Set<HeapNode> pruned = new HashSet<>();
333-
double fmin = Double.POSITIVE_INFINITY;
334-
335-
for (HeapNode heapNode : open) {
336-
if (!keepSet.contains(heapNode.cell)) {
337-
pruned.add(heapNode);
338-
}
339-
}
340-
341-
for (HeapNode heapNode : pruned) {
342-
fmin = Math.min(fmin, heapNode.f);
343-
}
339+
searchStatistics.addToOpened(open.size());
344340

345-
beamStack.peek().fmax = fmin;
346-
347-
for (HeapNode heapNode : pruned) {
348-
open.remove(heapNode);
349-
}
341+
return fMinPruned;
350342
}
351343

352344
private static final class BeamStackEntry {
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)