Package lejos.robotics.pathfinding
Class DijkstraPathFinder
- java.lang.Object
-
- lejos.robotics.pathfinding.DijkstraPathFinder
-
- All Implemented Interfaces:
PathFinder
public class DijkstraPathFinder extends java.lang.Object implements PathFinder
This class calculates the shortest path from a starting point to a finish point. while avoiding obstacles that are represented as a set of straight lines. The path passes through the end points of some of these lines, which is where the changes of direction occur. Since the robot is not point, the lines representing the obstacles should be lengthened so the actual robot will miss the actual obstacles. Use the lengthenLines() method to do this. Uses the A* algorithm, a variant of the Dijkstra shortest path algorithm It uses the Node inner class for its internal representation of points.- Author:
- Roger Glassey
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classDijkstraPathFinder.Node
-
Field Summary
Fields Modifier and Type Field Description protected boolean_blockedset by segmentBlocked() used by findPath()protected java.util.ArrayList<DijkstraPathFinder.Node>_candidatethe set of nodes that are candidates for being in the shortest path, but whose distance from the start node is not yet knownprotected int_countprotected java.util.ArrayList<Line>_mapThe map of the obstaclesprotected java.util.ArrayList<DijkstraPathFinder.Node>_reachedthe set of nodes that are candidates for being in the shortest path, and whose distance from the start node is known
-
Constructor Summary
Constructors Constructor Description DijkstraPathFinder(LineMap map)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(WaypointListener wpl)PathfindRoute(Pose start, Waypoint finish)Finds the shortest path from start to finish using the map (or collection of lines) in the constructor.PathfindRoute(Pose start, Waypoint finish, LineMap theMap)Finds the shortest path from start to finish using the map (or collection of lines) in the constructor.protected DijkstraPathFinder.NodegetBest(DijkstraPathFinder.Node currentDestination)Helper method for findPath()
returns the node in the Reached set, whose distance from the start node plus its straight line distance to the destination is the minimum.intgetIterationCount()protected java.util.ArrayList<Line>getMap()intgetNodeCount()protected PathgetRoute(DijkstraPathFinder.Node destination)helper method for find path()
calculates the route backtracking through predecessor chainprotected booleaninCandidateSet(DijkstraPathFinder.Node aNode)helper method for findPath; check if aNode is in the set of candidate nodesprotected voidinitialize()protected booleaninReachedSet(DijkstraPathFinder.Node aNode)helper method for findPath; check if aNode is in the set of reached nodesvoidlengthenLines(float delta)lengthens all the lines in the map by delta at each endprotected booleansegmentBlocked(DijkstraPathFinder.Node from, DijkstraPathFinder.Node theDest)helper method for findPath().voidsetMap(java.util.ArrayList<Line> theMap)voidsetMap(LineMap theMap)voidstartPathFinding(Pose start, Waypoint end)
-
-
-
Field Detail
-
_count
protected int _count
-
_blocked
protected boolean _blocked
set by segmentBlocked() used by findPath()
-
_candidate
protected java.util.ArrayList<DijkstraPathFinder.Node> _candidate
the set of nodes that are candidates for being in the shortest path, but whose distance from the start node is not yet known
-
_reached
protected java.util.ArrayList<DijkstraPathFinder.Node> _reached
the set of nodes that are candidates for being in the shortest path, and whose distance from the start node is known
-
_map
protected java.util.ArrayList<Line> _map
The map of the obstacles
-
-
Constructor Detail
-
DijkstraPathFinder
public DijkstraPathFinder(LineMap map)
-
-
Method Detail
-
findRoute
public Path findRoute(Pose start, Waypoint finish) throws DestinationUnreachableException
Finds the shortest path from start to finish using the map (or collection of lines) in the constructor.- Specified by:
findRoutein interfacePathFinder- Parameters:
start- the initial robot posefinish- the final robot location- Returns:
- the shortest route
- Throws:
DestinationUnreachableException- if, for example, you nave not called setMap();
-
findRoute
public Path findRoute(Pose start, Waypoint finish, LineMap theMap) throws DestinationUnreachableException
Finds the shortest path from start to finish using the map (or collection of lines) in the constructor.- Parameters:
start- the initial robot posefinish- the final robot locationtheMap- the LineMap of obstacles- Returns:
- the shortest route
- Throws:
DestinationUnreachableException- if, for example, you nave not called setMap();
-
setMap
public void setMap(java.util.ArrayList<Line> theMap)
-
setMap
public void setMap(LineMap theMap)
-
lengthenLines
public void lengthenLines(float delta)
lengthens all the lines in the map by delta at each end- Parameters:
delta- added to each end of each line
-
initialize
protected void initialize()
-
segmentBlocked
protected boolean segmentBlocked(DijkstraPathFinder.Node from, DijkstraPathFinder.Node theDest)
helper method for findPath(). Determines if the straight line segment crosses a line on the map. Side effect: creates nodes at the end of the blocking line and adds them to the _candidate set- Parameters:
from- the beginning of the line segmenttheDest- the end of the line segment- Returns:
- true if the segment is blocked
-
getBest
protected DijkstraPathFinder.Node getBest(DijkstraPathFinder.Node currentDestination)
Helper method for findPath()
returns the node in the Reached set, whose distance from the start node plus its straight line distance to the destination is the minimum.- Parameters:
currentDestination- : the current destination node, (in the Candidate set)- Returns:
- the node the node which could be the last node in the shortest path
-
inReachedSet
protected boolean inReachedSet(DijkstraPathFinder.Node aNode)
helper method for findPath; check if aNode is in the set of reached nodes- Parameters:
aNode-- Returns:
- true if aNode has been reached already
-
inCandidateSet
protected boolean inCandidateSet(DijkstraPathFinder.Node aNode)
helper method for findPath; check if aNode is in the set of candidate nodes- Parameters:
aNode-- Returns:
- true if aNode has been reached already
-
getRoute
protected Path getRoute(DijkstraPathFinder.Node destination)
helper method for find path()
calculates the route backtracking through predecessor chain- Parameters:
destination-- Returns:
- the route of the shortest path
-
getMap
protected java.util.ArrayList<Line> getMap()
-
getIterationCount
public int getIterationCount()
-
getNodeCount
public int getNodeCount()
-
addListener
public void addListener(WaypointListener wpl)
- Specified by:
addListenerin interfacePathFinder
-
startPathFinding
public void startPathFinding(Pose start, Waypoint end)
- Specified by:
startPathFindingin interfacePathFinder
-
-