11package io .github .coderodde .pathfinding .finders ;
22
3+ import static io .github .coderodde .pathfinding .finders .Finder .searchSleep ;
34import io .github .coderodde .pathfinding .heuristics .HeuristicFunction ;
45import io .github .coderodde .pathfinding .logic .GridCellNeighbourIterable ;
56import io .github .coderodde .pathfinding .logic .PathfindingSettings ;
67import io .github .coderodde .pathfinding .logic .SearchState ;
78import io .github .coderodde .pathfinding .logic .SearchStatistics ;
89import io .github .coderodde .pathfinding .model .GridModel ;
910import io .github .coderodde .pathfinding .utils .Cell ;
11+ import io .github .coderodde .pathfinding .utils .CellType ;
1012import java .util .ArrayDeque ;
1113import java .util .ArrayList ;
1214import java .util .Collections ;
@@ -40,14 +42,35 @@ public List<Cell> findPath(GridModel model,
4042 U .value = Double .POSITIVE_INFINITY ;
4143
4244 while (!beamStack .isEmpty ()) {
43- List <Cell > path = search (model ,
44- U ,
45- beamStack ,
46- neighbourIterable ,
47- pathfindingSettings );
45+ List <Cell > path = null ;
46+
47+ try {
48+ path = search (model ,
49+ U ,
50+ beamStack ,
51+ neighbourIterable ,
52+ pathfindingSettings ,
53+ searchState ,
54+ searchStatistics );
55+
56+ } catch (HaltRequestedException ex ) {
57+ return List .of ();
58+ }
4859
4960 System .out .println ("bye bye" );
5061
62+ if (searchState .haltRequested ()) {
63+ return List .of ();
64+ }
65+
66+ while (searchState .pauseRequested ()) {
67+ searchSleep (pathfindingSettings );
68+
69+ if (searchState .haltRequested ()) {
70+ return List .of ();
71+ }
72+ }
73+
5174 if (path != null ) {
5275 optimalPath = path ;
5376 U .value = getPathCost (path , pathfindingSettings );
@@ -71,6 +94,7 @@ public List<Cell> findPath(GridModel model,
7194 private static double
7295 getPathCost (List <Cell > path ,
7396 PathfindingSettings pathsPathfindingSettings ) {
97+
7498 double cost = 0.0 ;
7599
76100 for (int i = 0 ; i < path .size () - 1 ; ++i ) {
@@ -86,7 +110,9 @@ private static List<Cell> search(GridModel model,
86110 DoubleHolder U ,
87111 Deque <BeamStackEntry > beamStack ,
88112 GridCellNeighbourIterable iterable ,
89- PathfindingSettings pathfindingSettings ) {
113+ PathfindingSettings pathfindingSettings ,
114+ SearchState searchState ,
115+ SearchStatistics searchStatistics ) {
90116
91117 Map <Integer , PriorityQueue <HeapNode >> open = new HashMap <>();
92118 Map <Integer , Set <Cell >> closed = new HashMap <>();
@@ -97,22 +123,43 @@ private static List<Cell> search(GridModel model,
97123 Cell source = model .getSourceGridCell ();
98124 Cell target = model .getTargetGridCell ();
99125
126+ Cell bestGoal = null ;
127+ int layerIndex = 0 ;
128+
100129 open .put (0 , new PriorityQueue <>());
101130 open .put (1 , new PriorityQueue <>());
102131 open .get (0 ).add (new HeapNode (source , 0.0 ));
103132
104133 closed .put (0 , new HashSet <>());
105134 g .put (source , 0.0 );
106135 p .put (source , null );
107- Cell bestGoal = null ;
108- int layerIndex = 0 ;
136+
137+ searchStatistics . incrementOpened () ;
109138
110139 while (!open .get (layerIndex ).isEmpty () ||
111140 !open .get (layerIndex + 1 ).isEmpty ()) {
112141
113142 while (!open .get (layerIndex ).isEmpty ()) {
143+
144+ if (searchState .haltRequested ()) {
145+ throw new HaltRequestedException ();
146+ }
147+
148+ if (searchState .pauseRequested ()) {
149+ continue ;
150+ }
151+
152+ searchSleep (pathfindingSettings );
153+
114154 Cell cell = open .get (layerIndex ).remove ().cell ;
115155 closed .get (layerIndex ).add (cell );
156+ searchStatistics .decrementOpened ();
157+ searchStatistics .incrementVisited ();
158+
159+ if (!cell .getCellType ().equals (CellType .SOURCE )) {
160+ model .setCellType (cell , CellType .VISITED );
161+ }
162+
116163 System .out .println ("shit!" );
117164
118165 if (cell .equals (target )) {
@@ -125,17 +172,33 @@ private static List<Cell> search(GridModel model,
125172 BeamStackEntry beamStackEntry = beamStack .peek ();
126173
127174 for (Cell child : iterable ) {
175+ if (searchState .haltRequested ()) {
176+ throw new HaltRequestedException ();
177+ }
178+
179+ while (searchState .pauseRequested ()) {
180+ searchSleep (pathfindingSettings );
181+
182+ if (searchState .haltRequested ()) {
183+ throw new HaltRequestedException ();
184+ }
185+ }
186+
128187 if (g .containsKey (child )) {
129188 continue ;
130189 }
131190
191+ searchSleep (pathfindingSettings );
192+
132193 double gscore = g .get (cell )
133194 + pathfindingSettings .getWeight (cell ,
134195 child );
135196
136197 double f = gscore + h .estimate (child , target );
137198
138199 if (beamStackEntry .fmin <= f && f <= beamStackEntry .fmax ) {
200+ searchStatistics .incrementOpened ();
201+
139202 open .get (layerIndex + 1 ).add (new HeapNode (child , f ));
140203 g .put (child , gscore );
141204 p .put (child , cell );
@@ -145,24 +208,46 @@ private static List<Cell> search(GridModel model,
145208 if (open .get (layerIndex + 1 ).size () >
146209 pathfindingSettings .getBeamWidth ()) {
147210
148- pruneLayer (open .get (layerIndex + 1 ),
149- beamStack ,
150- pathfindingSettings );
211+ pruneLayer (model ,
212+ open .get (layerIndex + 1 ),
213+ beamStack ,
214+ pathfindingSettings ,
215+ searchStatistics );
151216 }
152217 }
153218
154- // if ((1 < layerIndex && layerIndex <= relay) ||
155- // (layerIndex > relay + 1)) {
156- // closed.get(layerIndex - 1).clear();
157- // }
158-
159219 layerIndex ++;
160220 open .put (layerIndex + 1 , new PriorityQueue <>());
161221 closed .put (layerIndex , new HashSet <>());
162222 beamStack .push (new BeamStackEntry (0 , U .value ));
163223 System .out .println ("hello" );
164224 }
165225
226+ for (PriorityQueue <HeapNode > queue : open .values ()) {
227+ for (HeapNode heapNode : queue ) {
228+ Cell cell = heapNode .cell ;
229+
230+ if (cell .getCellType ().equals (CellType .SOURCE )) {
231+ System .err .println ("cell.getCellType().equals(CellType.SOURCE)" );
232+ }
233+
234+ if (cell .getCellType ().equals (CellType .TARGET )) {
235+ System .err .println ("cell.getCellType().equals(CellType.TARGET)" );
236+ }
237+
238+ model .setCellType (cell , CellType .FREE );
239+ }
240+ }
241+
242+ for (Set <Cell > closedSet : closed .values ()) {
243+ for (Cell cell : closedSet ) {
244+
245+ if (!cell .getCellType ().equals (CellType .SOURCE )) {
246+ model .setCellType (cell , CellType .FREE );
247+ }
248+ }
249+ }
250+
166251 if (bestGoal != null ) {
167252 List <Cell > path = new ArrayList <>();
168253 Cell cell = bestGoal ;
@@ -180,19 +265,36 @@ private static List<Cell> search(GridModel model,
180265 return null ;
181266 }
182267
183- private static void pruneLayer (PriorityQueue <HeapNode > open ,
268+ private static void pruneLayer (GridModel model ,
269+ PriorityQueue <HeapNode > open ,
184270 Deque <BeamStackEntry > beamStack ,
185- PathfindingSettings pathfindingSettings ) {
271+ PathfindingSettings pathfindingSettings ,
272+ SearchStatistics searchStatistics ) {
186273 List <HeapNode > keep = new ArrayList <>(open );
274+ searchStatistics .addToOpened (-keep .size ());
275+ for (HeapNode node : keep ) {
276+ Cell cell = node .cell ;
277+
278+ if (!cell .getCellType ().equals (CellType .TARGET )) {
279+ model .setCellType (cell , CellType .FREE );
280+ }
281+ }
282+
187283 keep .sort ((a , b ) -> {
188284 return Double .compare (a .f , b .f );
189285 });
190286
191287 keep = keep .subList (0 , pathfindingSettings .getBeamWidth ());
288+ searchStatistics .addToOpened (keep .size ());
192289 Set <Cell > keepSet = new HashSet <>();
193290
194291 for (HeapNode heapNode : keep ) {
195- keepSet .add (heapNode .cell );
292+ Cell cell = heapNode .cell ;
293+ keepSet .add (cell );
294+
295+ if (!cell .getCellType ().equals (CellType .TARGET )) {
296+ model .setCellType (cell , CellType .OPENED );
297+ }
196298 }
197299
198300 Set <HeapNode > pruned = new HashSet <>();
0 commit comments