@@ -19,6 +19,16 @@ public enum SearchMode { GREEDY, DIJKSTRA, HEURISTIC } ;
1919
2020 public SearchMode searchMode = SearchMode .HEURISTIC ;
2121
22+ /**
23+ * If specified, this put a maximum in the number of iterations that the
24+ * method findPath can take. When this maximum is reached, the method
25+ * considers that there is no path to the specified destination (and returns
26+ * a null).
27+ *
28+ * <p>Default: null.
29+ */
30+ public Integer maximumNumberOfIterations = null ;
31+
2232 /**
2333 * When this is set, this function will be use to calculate the heuristic distance between
2434 * two nodes, rather than using the default heuristic-method supplied by underlying navigation
@@ -57,11 +67,22 @@ public ArrayList<NodeId> findPath(Navigatable<NodeId> graph, NodeId start, NodeI
5767 if (searchMode == SearchMode .DIJKSTRA ) fDistance_ofStart = 0 ;
5868 open .add (new Priotisable <NodeId >(start ,fDistance_ofStart ));
5969 closed .put (start , 0f );
70+
71+ // keeping track the total number of iterations of the loop below
72+ int numberOfIterations = 0 ;
6073
6174 while (open .size () > 0 ) {
75+
76+ if (maximumNumberOfIterations !=null && numberOfIterations >= maximumNumberOfIterations )
77+
78+ return null ;
79+
80+ numberOfIterations ++ ;
81+
6282 // remove the node with the lowest "priority" from the open-list.
6383 NodeId current = open .remove ().item ;
64-
84+
85+
6586 // Check if goal is reached, the search stops, and the path to it is returned.
6687 // In particular note that we don't search further e.g. to find a better path.
6788 if (current .equals (goal )) {
0 commit comments