Skip to content

Commit 1fdb08d

Browse files
committed
BIDDFS has optimality bug.
1 parent 1334dc5 commit 1fdb08d

File tree

2 files changed

+120
-75
lines changed

2 files changed

+120
-75
lines changed

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

Lines changed: 120 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -48,82 +48,88 @@ public List<Cell> findPath(GridModel model,
4848
int previousVisitedSizeForward = 0;
4949
int previousVisitedSizeBackward = 0;
5050

51-
for (int depth = 0; ; ++depth) {
51+
try {
5252

53-
clearFrontier(frontier,
54-
model);
55-
56-
clearVisited(visitedForward,
57-
model);
58-
59-
visitedBackward.clear();
60-
backwardSearchStack.clear();
61-
parentForward.clear();
62-
63-
depthLimitedSearchForward(source,
64-
depth,
65-
visitedForward,
66-
frontier,
67-
parentForward,
68-
model,
69-
neighbourIterable,
70-
pathfindingSettings,
71-
searchState,
72-
searchStatistics);
73-
74-
if (visitedForward.size() == previousVisitedSizeForward) {
75-
return List.of();
76-
}
77-
78-
previousVisitedSizeForward = visitedForward.size();
79-
clearVisited(visitedBackward, model);
80-
81-
Cell meetingCell =
82-
depthLimitedSearchBackward(
83-
target,
84-
depth,
85-
visitedBackward,
86-
frontier,
87-
backwardSearchStack,
88-
model,
89-
neighbourIterable,
90-
pathfindingSettings,
91-
searchState,
92-
searchStatistics);
93-
94-
if (meetingCell != null) {
95-
return buildPath(meetingCell,
96-
parentForward,
97-
backwardSearchStack);
98-
}
99-
100-
clearVisited(visitedBackward,
101-
model);
53+
for (int depth = 0; ; ++depth) {
10254

103-
meetingCell =
104-
depthLimitedSearchBackward(
105-
target,
106-
depth + 1, // We need this for correctness!
107-
visitedBackward,
108-
frontier,
109-
backwardSearchStack,
110-
model,
111-
neighbourIterable,
112-
pathfindingSettings,
113-
searchState,
114-
searchStatistics);
115-
116-
if (meetingCell != null) {
117-
return buildPath(meetingCell,
118-
parentForward,
119-
backwardSearchStack);
120-
}
121-
122-
if (visitedBackward.size() == previousVisitedSizeBackward) {
123-
return List.of();
55+
clearFrontier(frontier,
56+
model);
57+
58+
clearVisited(visitedForward,
59+
model);
60+
61+
visitedBackward.clear();
62+
backwardSearchStack.clear();
63+
parentForward.clear();
64+
65+
depthLimitedSearchForward(source,
66+
depth,
67+
visitedForward,
68+
frontier,
69+
parentForward,
70+
model,
71+
neighbourIterable,
72+
pathfindingSettings,
73+
searchState,
74+
searchStatistics);
75+
76+
if (visitedForward.size() == previousVisitedSizeForward) {
77+
return List.of();
78+
}
79+
80+
previousVisitedSizeForward = visitedForward.size();
81+
clearVisited(visitedBackward, model);
82+
83+
Cell meetingCell =
84+
depthLimitedSearchBackward(
85+
target,
86+
depth,
87+
visitedBackward,
88+
frontier,
89+
backwardSearchStack,
90+
model,
91+
neighbourIterable,
92+
pathfindingSettings,
93+
searchState,
94+
searchStatistics);
95+
96+
if (meetingCell != null) {
97+
return buildPath(meetingCell,
98+
parentForward,
99+
backwardSearchStack);
100+
}
101+
102+
clearVisited(visitedBackward,
103+
model);
104+
105+
meetingCell =
106+
depthLimitedSearchBackward(
107+
target,
108+
depth + 1, // We need this for correctness!
109+
visitedBackward,
110+
frontier,
111+
backwardSearchStack,
112+
model,
113+
neighbourIterable,
114+
pathfindingSettings,
115+
searchState,
116+
searchStatistics);
117+
118+
if (meetingCell != null) {
119+
return buildPath(meetingCell,
120+
parentForward,
121+
backwardSearchStack);
122+
}
123+
124+
if (visitedBackward.size() == previousVisitedSizeBackward) {
125+
return List.of();
126+
}
127+
128+
previousVisitedSizeBackward = visitedBackward.size();
124129
}
125-
126-
previousVisitedSizeBackward = visitedBackward.size();
130+
} catch (HaltRequestedException ex) {
131+
// User requested the halt.
132+
return List.of();
127133
}
128134
}
129135

@@ -162,7 +168,11 @@ private static void depthLimitedSearchForward(
162168

163169
if (depth == 0) {
164170
frontier.add(node);
165-
// model.setCellType(node, CellType.TRACED);
171+
172+
if (!node.getCellType().equals(CellType.SOURCE)) {
173+
model.setCellType(node, CellType.TRACED);
174+
}
175+
166176
return;
167177
}
168178

@@ -177,6 +187,20 @@ private static void depthLimitedSearchForward(
177187
parents.put(child, node);
178188
}
179189

190+
if (searchState.haltRequested()) {
191+
throw new HaltRequestedException();
192+
}
193+
194+
while (searchState.pauseRequested()) {
195+
searchSleep(ps);
196+
197+
if (searchState.haltRequested()) {
198+
throw new HaltRequestedException();
199+
}
200+
}
201+
202+
searchSleep(ps);
203+
180204
depthLimitedSearchForward(child,
181205
depth - 1,
182206
visitedForward,
@@ -189,7 +213,9 @@ private static void depthLimitedSearchForward(
189213
searchStatistics);
190214
}
191215

192-
model.setCellType(node, CellType.FREE);
216+
if (!node.getCellType().equals(CellType.SOURCE)) {
217+
model.setCellType(node, CellType.VISITED);
218+
}
193219
}
194220

195221
private static Cell depthLimitedSearchBackward(
@@ -242,6 +268,21 @@ private static Cell depthLimitedSearchBackward(
242268
iterable.setStartingCell(cell);
243269

244270
for (Cell parent : iterable) {
271+
272+
if (searchState.haltRequested()) {
273+
throw new HaltRequestedException();
274+
}
275+
276+
while (searchState.pauseRequested()) {
277+
searchSleep(ps);
278+
279+
if (searchState.haltRequested()) {
280+
throw new HaltRequestedException();
281+
}
282+
}
283+
284+
searchSleep(ps);
285+
245286
Cell meetingCell =
246287
depthLimitedSearchBackward(
247288
parent,
@@ -260,6 +301,10 @@ private static Cell depthLimitedSearchBackward(
260301
}
261302
}
262303

304+
if (!cell.getCellType().equals(CellType.TARGET)) {
305+
model.setCellType(cell, CellType.VISITED);
306+
}
307+
263308
backwardsSearchStack.removeFirst();
264309
return null;
265310
}
Binary file not shown.

0 commit comments

Comments
 (0)