Package org.matsim.core.router
Class Dijkstra
- java.lang.Object
-
- org.matsim.core.router.Dijkstra
-
- All Implemented Interfaces:
LeastCostPathCalculator
- Direct Known Subclasses:
AStarEuclidean
,FastDijkstra
,MultiNodeDijkstra
public class Dijkstra extends Object implements LeastCostPathCalculator
Implementation of Dijkstra's shortest-path algorithm on a time-dependent network with arbitrary non-negative cost functions (e.g. negative link cost are not allowed). So 'shortest' in our context actually means 'least-cost'.For every router, there exists a class which computes some preprocessing data and is passed to the router class constructor in order to accelerate the routing procedure. The one used for Dijkstra is
PreProcessDijkstra
.
Code example:
PreProcessDijkstra preProcessData = new PreProcessDijkstra();
preProcessData.run(network);
TravelCost costFunction = ...
LeastCostPathCalculator routingAlgo = new Dijkstra(network, costFunction, preProcessData);
routingAlgo.calcLeastCostPath(fromNode, toNode, startTime);If you don't want to preprocess the network, you can invoke Dijkstra as follows:
LeastCostPathCalculator routingAlgo = new Dijkstra(network, costFunction);
Important note
This class is NOT thread-safe!- Author:
- lnicolas, mrieser
- See Also:
PreProcessDijkstra
,AStarEuclidean
,AStarLandmarks
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.matsim.core.router.util.LeastCostPathCalculator
LeastCostPathCalculator.Path
-
-
Field Summary
Fields Modifier and Type Field Description protected TravelDisutility
costFunction
The cost calculator.protected Network
network
The network on which we find routes.protected TravelTime
timeFunction
The travel time calculator.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Dijkstra(Network network, TravelDisutility costFunction, TravelTime timeFunction)
Default constructor.protected
Dijkstra(Network network, TravelDisutility costFunction, TravelTime timeFunction, PreProcessDijkstra preProcessData)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected boolean
addToPendingNodes(Link l, Node n, RouterPriorityQueue<Node> pendingNodes, double currTime, double currCost, Node toNode)
Adds some parameters to the given Node then adds it to the set of pending nodes.protected void
augmentIterationId()
Augments the iterationID and checks whether the visited information in the nodes in the nodes have to be reset.LeastCostPathCalculator.Path
calcLeastCostPath(Node fromNode, Node toNode, double startTime, Person person2, Vehicle vehicle2)
Calculates the cheapest route from Node 'fromNode' to Node 'toNode' at starting time 'startTime'.protected boolean
canPassLink(Link link)
protected LeastCostPathCalculator.Path
constructPath(Node fromNode, Node toNode, double startTime, double arrivalTime)
Constructs the path after the algorithm has been run.protected DijkstraNodeData
createNodeData()
protected DijkstraNodeData
getData(Node n)
Returns the data for the given node.protected int
getIterationId()
protected Person
getPerson()
protected PreProcessDijkstra.DeadEndData
getPreProcessData(Node n)
protected double
getPriority(DijkstraNodeData data)
The value used to sort the pending nodes during routing.protected Vehicle
getVehicle()
protected void
relaxNode(Node outNode, Node toNode, RouterPriorityQueue<Node> pendingNodes)
Expands the given Node in the routing algorithm; may be overridden in sub-classes.protected void
revisitNode(Node n, DijkstraNodeData data, RouterPriorityQueue<Node> pendingNodes, double time, double cost, Link outLink)
Changes the position of the given Node n in the pendingNodes queue and updates its time and cost information.void
setModeRestriction(Set<String> modeRestriction)
Deprecated.Use a filtered network instead which only contains the links you want.protected void
visitNode(Node n, DijkstraNodeData data, RouterPriorityQueue<Node> pendingNodes, double time, double cost, Link outLink)
Inserts the given Node n into the pendingNodes queue and updates its time and cost information.
-
-
-
Field Detail
-
costFunction
protected final TravelDisutility costFunction
The cost calculator. Provides the cost for each link and time step.
-
timeFunction
protected final TravelTime timeFunction
The travel time calculator. Provides the travel time for each link and time step.
-
-
Constructor Detail
-
Dijkstra
protected Dijkstra(Network network, TravelDisutility costFunction, TravelTime timeFunction)
Default constructor.- Parameters:
network
- The network on which to route.costFunction
- Determines the link cost defining the cheapest route.timeFunction
- Determines the travel time on links.
-
Dijkstra
protected Dijkstra(Network network, TravelDisutility costFunction, TravelTime timeFunction, PreProcessDijkstra preProcessData)
Constructor.- Parameters:
network
- The network on which to route.costFunction
- Determines the link cost defining the cheapest route.timeFunction
- Determines the travel time on each link.preProcessData
- The pre processing data used during the routing phase.
-
-
Method Detail
-
setModeRestriction
@Deprecated public void setModeRestriction(Set<String> modeRestriction)
Deprecated.Use a filtered network instead which only contains the links you want.
-
calcLeastCostPath
public LeastCostPathCalculator.Path calcLeastCostPath(Node fromNode, Node toNode, double startTime, Person person2, Vehicle vehicle2)
Calculates the cheapest route from Node 'fromNode' to Node 'toNode' at starting time 'startTime'.- Specified by:
calcLeastCostPath
in interfaceLeastCostPathCalculator
- Parameters:
fromNode
- The Node at which the route should start.toNode
- The Node at which the route should end.startTime
- The time at which the route should start.- See Also:
LeastCostPathCalculator.calcLeastCostPath(org.matsim.api.core.v01.network.Node, org.matsim.api.core.v01.network.Node, double, org.matsim.api.core.v01.population.Person, org.matsim.vehicles.Vehicle)
-
constructPath
protected LeastCostPathCalculator.Path constructPath(Node fromNode, Node toNode, double startTime, double arrivalTime)
Constructs the path after the algorithm has been run.- Parameters:
fromNode
- The node where the path starts.toNode
- The node where the path ends.startTime
- The time when the trip starts.
-
relaxNode
protected void relaxNode(Node outNode, Node toNode, RouterPriorityQueue<Node> pendingNodes)
Expands the given Node in the routing algorithm; may be overridden in sub-classes.- Parameters:
outNode
- The Node to be expanded.toNode
- The target Node of the route.pendingNodes
- The set of pending nodes so far.
-
addToPendingNodes
protected boolean addToPendingNodes(Link l, Node n, RouterPriorityQueue<Node> pendingNodes, double currTime, double currCost, Node toNode)
Adds some parameters to the given Node then adds it to the set of pending nodes.- Parameters:
l
- The link from which we came to this Node.n
- The Node to add to the pending nodes.pendingNodes
- The set of pending nodes.currTime
- The time at which we started to traverse l.currCost
- The cost at the time we started to traverse l.toNode
- The target Node of the route.- Returns:
- true if the node was added to the pending nodes, false otherwise (e.g. when the same node already has an earlier visiting time).
-
canPassLink
protected boolean canPassLink(Link link)
- Returns:
true
if the link can be passed with respect to a possible mode restriction set- See Also:
setModeRestriction(Set)
-
revisitNode
protected void revisitNode(Node n, DijkstraNodeData data, RouterPriorityQueue<Node> pendingNodes, double time, double cost, Link outLink)
Changes the position of the given Node n in the pendingNodes queue and updates its time and cost information.- Parameters:
n
- The Node that is revisited.data
- The data for n.pendingNodes
- The nodes visited and not processed yet.time
- The time of the visit of n.cost
- The accumulated cost at the time of the visit of n.outLink
- The link from which we came visiting n.
-
visitNode
protected void visitNode(Node n, DijkstraNodeData data, RouterPriorityQueue<Node> pendingNodes, double time, double cost, Link outLink)
Inserts the given Node n into the pendingNodes queue and updates its time and cost information.- Parameters:
n
- The Node that is revisited.data
- The data for n.pendingNodes
- The nodes visited and not processed yet.time
- The time of the visit of n.cost
- The accumulated cost at the time of the visit of n.outLink
- The node from which we came visiting n.
-
augmentIterationId
protected void augmentIterationId()
Augments the iterationID and checks whether the visited information in the nodes in the nodes have to be reset.
-
getIterationId
protected int getIterationId()
- Returns:
- iterationID
-
getPriority
protected double getPriority(DijkstraNodeData data)
The value used to sort the pending nodes during routing. This implementation compares the total effective travel cost to sort the nodes in the pending nodes queue during routing.
-
getData
protected DijkstraNodeData getData(Node n)
Returns the data for the given node. Creates a new NodeData if none exists yet.- Parameters:
n
- The Node for which to return the data.- Returns:
- The data for the given Node
-
createNodeData
protected DijkstraNodeData createNodeData()
-
getPreProcessData
protected PreProcessDijkstra.DeadEndData getPreProcessData(Node n)
-
getVehicle
protected final Vehicle getVehicle()
-
-