@@ -19,6 +19,16 @@ public enum SearchMode { GREEDY, DIJKSTRA, HEURISTIC } ;
19
19
20
20
public SearchMode searchMode = SearchMode .HEURISTIC ;
21
21
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
+
22
32
/**
23
33
* When this is set, this function will be use to calculate the heuristic distance between
24
34
* 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
57
67
if (searchMode == SearchMode .DIJKSTRA ) fDistance_ofStart = 0 ;
58
68
open .add (new Priotisable <NodeId >(start ,fDistance_ofStart ));
59
69
closed .put (start , 0f );
70
+
71
+ // keeping track the total number of iterations of the loop below
72
+ int numberOfIterations = 0 ;
60
73
61
74
while (open .size () > 0 ) {
75
+
76
+ if (maximumNumberOfIterations !=null && numberOfIterations >= maximumNumberOfIterations )
77
+
78
+ return null ;
79
+
80
+ numberOfIterations ++ ;
81
+
62
82
// remove the node with the lowest "priority" from the open-list.
63
83
NodeId current = open .remove ().item ;
64
-
84
+
85
+
65
86
// Check if goal is reached, the search stops, and the path to it is returned.
66
87
// In particular note that we don't search further e.g. to find a better path.
67
88
if (current .equals (goal )) {
0 commit comments