Skip to content

Commit f2577de

Browse files
committed
adding an option to stop Astar when a max-number of iterations is reached.
1 parent f316c40 commit f2577de

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.iv4xr-project</groupId>
88
<artifactId>aplib</artifactId>
9-
<version>1.8.5-SNAPSHOT</version>
9+
<version>1.8.6-SNAPSHOT</version>
1010

1111
<name>aplib</name>
1212
<url>https://github.com/iv4xr-project/aplib</url>

src/main/java/eu/iv4xr/framework/extensions/pathfinding/AStar.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)