1 package org.matsim.core.router.speedy;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.List;
27 private final double[]
data;
33 private final DAryMinHeap
pq;
39 this.data =
new double[graph.nodeCount * 3];
40 this.iterationIds =
new int[graph.nodeCount];
41 this.comingFrom =
new int[graph.nodeCount];
42 this.usedLink =
new int[graph.nodeCount];
43 this.pq =
new DAryMinHeap(graph.nodeCount, 6);
48 return this.data[nodeIndex * 3];
52 return this.data[nodeIndex * 3 + 1];
56 return this.data[nodeIndex * 3 + 2];
59 private void setData(
int nodeIndex,
double cost,
double time,
double distance) {
60 int index = nodeIndex * 3;
61 this.data[index] = cost;
62 this.data[index + 1] = time;
63 this.data[index + 2] = distance;
69 this.currentIteration++;
70 if (this.currentIteration == Integer.MAX_VALUE) {
72 Arrays.fill(this.iterationIds, this.currentIteration);
73 this.currentIteration = Integer.MIN_VALUE;
77 int startNodeIndex = startNode.
getId().index();
78 int endNodeIndex = endNode.
getId().index();
80 this.comingFrom[startNodeIndex] = -1;
81 setData(startNodeIndex, 0, startTime, 0);
83 this.pq.insert(startNodeIndex, 0);
84 boolean foundEndNode =
false;
86 while (!this.pq.isEmpty()) {
87 final int nodeIdx = this.pq.poll();
88 if (nodeIdx == endNodeIndex) {
93 if (hasTurnRestrictions && this.graph.getNode(nodeIdx).
getId().index() == endNodeIndex) {
95 endNodeIndex = nodeIdx;
100 if (Double.isInfinite(currTime)) {
103 double currCost =
getCost(nodeIdx);
106 this.
outLI.reset(nodeIdx);
107 while (this.
outLI.next()) {
108 int linkIdx = this.
outLI.getLinkIndex();
109 Link link = this.graph.getLink(linkIdx);
110 int toNode = this.
outLI.getToNodeIndex();
113 double newTime = currTime + travelTime;
116 if (this.iterationIds[toNode] == this.currentIteration) {
118 double oldCost =
getCost(toNode);
119 if (newCost < oldCost) {
120 this.pq.decreaseKey(toNode, newCost);
122 this.comingFrom[toNode] = nodeIdx;
123 this.usedLink[toNode] = linkIdx;
127 this.pq.insert(toNode, newCost);
128 this.comingFrom[toNode] = nodeIdx;
129 this.usedLink[toNode] = linkIdx;
141 double travelCost =
getCost(endNodeIndex);
142 double arrivalTime =
getTimeRaw(endNodeIndex);
143 if (Double.isInfinite(arrivalTime)) {
146 double travelTime = arrivalTime - startTime;
148 List<Node> nodes =
new ArrayList<>();
149 List<Link> links =
new ArrayList<>();
151 int nodeIndex = endNodeIndex;
153 nodes.add(this.graph.getNode(nodeIndex));
155 int linkIndex = this.usedLink[nodeIndex];
156 nodeIndex = this.comingFrom[nodeIndex];
158 while (nodeIndex >= 0) {
159 nodes.add(this.graph.getNode(nodeIndex));
160 links.add(this.graph.getLink(linkIndex));
162 linkIndex = this.usedLink[nodeIndex];
163 nodeIndex = this.comingFrom[nodeIndex];
166 Collections.reverse(nodes);
167 Collections.reverse(links);
169 return new Path(nodes, links, travelTime, travelCost);
SpeedyDijkstra(SpeedyGraph graph, TravelTime tt, TravelDisutility td)
final boolean hasTurnRestrictions
double getDistance(int nodeIndex)
double getCost(int nodeIndex)
final SpeedyGraph.LinkIterator outLI
final TravelDisutility td
void setData(int nodeIndex, double cost, double time, double distance)
double getLinkTravelTime(Link link, double time, Person person, Vehicle vehicle)
double getLinkTravelDisutility(final Link link, final double time, final Person person, final Vehicle vehicle)
Path calcLeastCostPath(Node startNode, Node endNode, double startTime, Person person, Vehicle vehicle)
final int [] iterationIds
Path constructPath(int endNodeIndex, double startTime)
double getTimeRaw(int nodeIndex)
LinkIterator getOutLinkIterator()