MATSIM
FastMultiNodeDijkstra.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * FastDijkstra.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2011 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.router;
22 
23 import java.util.Collection;
24 import java.util.Iterator;
25 
39 import org.matsim.vehicles.Vehicle;
40 
51 
52  /*package*/ final RoutingNetwork routingNetwork;
53  private final FastRouterDelegate fastRouter;
55  private int maxSize = -1;
56 
57  /*
58  * Create the routing network here and clear the nodeData map
59  * which is not used by this implementation.
60  */
61  protected FastMultiNodeDijkstra(final RoutingNetwork routingNetwork, final TravelDisutility costFunction,
63  final FastRouterDelegateFactory fastRouterFactory, boolean searchAllEndNodes) {
64  super(routingNetwork, costFunction, timeFunction, preProcessData, searchAllEndNodes);
65 
66  this.routingNetwork = routingNetwork;
67  this.fastRouter = fastRouterFactory.createFastRouterDelegate(this, new DijkstraNodeDataFactory(), routingNetwork);
68 
69  this.nodeData.clear();
70  }
71 
72  /*
73  * Replace the references to the from and to nodes with their corresponding
74  * nodes in the routing network.
75  */
76  @Override
77  public Path calcLeastCostPath(final Node fromNode, final Node toNode, final double startTime, final Person person, final Vehicle vehicle) {
78 
79  this.fastRouter.initialize();
80  this.routingNetwork.initialize();
81 
82  Node routingNetworkFromNode;
83  Node routingNetworkToNode;
84 
85  if (fromNode instanceof ImaginaryNode) {
86  Collection<? extends InitialNode> initialNodes = ((ImaginaryNode) fromNode).initialNodes;
87  for (InitialNode initialNode : initialNodes) initialNode.node = routingNetwork.getNodes().get(initialNode.node.getId());
88  routingNetworkFromNode = fromNode;
89  } else routingNetworkFromNode = routingNetwork.getNodes().get(fromNode.getId());
90 
91  if (toNode instanceof ImaginaryNode) {
92  Collection<? extends InitialNode> initialNodes = ((ImaginaryNode) toNode).initialNodes;
93  for (InitialNode initialNode : initialNodes) initialNode.node = routingNetwork.getNodes().get(initialNode.node.getId());
94  routingNetworkToNode = toNode;
95  } else routingNetworkToNode = routingNetwork.getNodes().get(toNode.getId());
96 
97  return super.calcLeastCostPath(routingNetworkFromNode, routingNetworkToNode, startTime, person, vehicle);
98  }
99 
100  @Override
101  /*package*/ RouterPriorityQueue<? extends Node> createRouterPriorityQueue() {
102  /*
103  * Re-use existing BinaryMinHeap instead of creating a new one. For large networks (> 10^6 nodes and links) this reduced
104  * the computation time by 40%! cdobler, oct'15
105  */
106  if (this.routingNetwork instanceof ArrayRoutingNetwork) {
107  int size = this.routingNetwork.getNodes().size();
108  if (this.heap == null || this.maxSize != size) {
109  this.maxSize = size;
110  this.heap = new BinaryMinHeap<>(maxSize);
111  return this.heap;
112  } else {
113  this.heap.reset();
114  return this.heap;
115  }
116 // int maxSize = this.routingNetwork.getNodes().size();
117 // return new BinaryMinHeap<ArrayRoutingNetworkNode>(maxSize);
118  } else {
119  return super.createRouterPriorityQueue();
120  }
121  }
122 
123  /*
124  * Constructs the path and replaces the nodes and links from the routing network
125  * with their corresponding nodes and links from the network.
126  */
127  @Override
128  protected Path constructPath(Node fromNode, Node toNode, double startTime, double arrivalTime) {
129  /*
130  * If the fromNode is an imaginaryNode some special treatment is necessary.
131  * The path returned by the fastRouter also contains the travel time and cost
132  * from the trips start coordinate to the start node of the path. This information
133  * is stored in the ImaginaryNode (respectively in its InitialNodes). Therefore,
134  * we have to store a reference to the imaginary node.
135  */
136  ImaginaryNode imaginaryNode = null;
137  if (fromNode instanceof ImaginaryNode) imaginaryNode = (ImaginaryNode) fromNode;
138 
139  if (!(fromNode instanceof RoutingNetworkNode)) fromNode = this.routingNetwork.getNodes().get(fromNode.getId());
140  if (!(toNode instanceof RoutingNetworkNode)) toNode = this.routingNetwork.getNodes().get(toNode.getId());
141 
142  Path path = this.fastRouter.constructPath(fromNode, toNode, startTime, arrivalTime);
143 
144  /*
145  * Here, we correct the path's travel time and cost if necessary.
146  * To do so, we look for the InitialNode that matches the path's first node.
147  * The path's travel time and cost are then reduced by the values
148  * found in the InitialNode.
149  */
150  if (imaginaryNode != null && path != null && path.nodes.size() > 0) {
151  Node pathFromNode = path.getFromNode();
152  double initialCost = 0.0;
153  double initialTime = 0.0;
154 
155  Iterator<? extends InitialNode> iter = imaginaryNode.initialNodes.iterator();
156  while (iter.hasNext()) {
157  InitialNode initialNode = iter.next();
158  if (initialNode.node.getId().equals(pathFromNode.getId())) {
159  initialCost = initialNode.initialCost;
160  initialTime = initialNode.initialTime;
161  break;
162  }
163  }
164 
165  return new Path(path.nodes, path.links, path.travelTime - initialTime, path.travelCost - initialCost);
166  }
167 
168  return path;
169  }
170 
171  /*
172  * Constructs the path and replaces the nodes and links from the routing network
173  * with their corresponding nodes and links from the network.
174  */
175  @Override
176  public Path constructPath(Node fromNode, Node toNode, double startTime) {
177  if (toNode == null || fromNode == null) return null;
178  if (!(fromNode instanceof RoutingNetworkNode)) fromNode = this.routingNetwork.getNodes().get(fromNode.getId());
179  if (!(toNode instanceof RoutingNetworkNode)) toNode = this.routingNetwork.getNodes().get(toNode.getId());
180  return super.constructPath(fromNode, toNode, startTime);
181  }
182 
183  /*
184  * For performance reasons the outgoing links of a node are stored in
185  * the routing network in an array instead of a map. Therefore we have
186  * to iterate over an array instead of over a map.
187  */
188  @Override
189  protected void relaxNode(final Node outNode, final Node toNode, final RouterPriorityQueue<Node> pendingNodes) {
190  fastRouter.relaxNode(outNode, toNode, pendingNodes);
191  }
192 
193  /*
194  * The DijkstraNodeData is taken from the RoutingNetworkNode and not from a map.
195  */
196  @Override
197  protected DijkstraNodeData getData(final Node n) {
198  return (DijkstraNodeData) fastRouter.getData(n);
199  }
200 
201  /*
202  * The DeadEndData is taken from the RoutingNetworkNode and not from a map.
203  */
204  @Override
206  return fastRouter.getPreProcessData(n);
207  }
208 }
FastMultiNodeDijkstra(final RoutingNetwork routingNetwork, final TravelDisutility costFunction, final TravelTime timeFunction, final PreProcessDijkstra preProcessData, final FastRouterDelegateFactory fastRouterFactory, boolean searchAllEndNodes)
Map< Id< Node >, RoutingNetworkNode > getNodes()
PreProcessDijkstra.DeadEndData getPreProcessData(final Node n)
void relaxNode(final Node outNode, final Node toNode, final RouterPriorityQueue< Node > pendingNodes)
final PreProcessDijkstra preProcessData
Definition: Dijkstra.java:127
final TravelDisutility costFunction
Definition: Dijkstra.java:98
final TravelTime timeFunction
Definition: Dijkstra.java:103
BinaryMinHeap< ArrayRoutingNetworkNode > heap
Path constructPath(Node fromNode, Node toNode, double startTime)
Path constructPath(Node fromNode, Node toNode, double startTime, double arrivalTime)
FastRouterDelegate createFastRouterDelegate(Dijkstra dijkstra, NodeDataFactory nodeDataFactory, RoutingNetwork routingNetwork)
Path calcLeastCostPath(final Node fromNode, final Node toNode, final double startTime, final Person person, final Vehicle vehicle)