MATSIM
FastDijkstra.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 
36 import org.matsim.vehicles.Vehicle;
37 
48 public class FastDijkstra extends Dijkstra {
49 
51  private final FastRouterDelegate fastRouter;
53  private int maxSize = -1;
54 
55  /*
56  * Create the routing network here and clear the nodeData map
57  * which is not used by this implementation.
58  */
60  final PreProcessDijkstra preProcessData, final FastRouterDelegateFactory fastRouterFactory) {
61  super(routingNetwork, costFunction, timeFunction, preProcessData);
62 
63  this.routingNetwork = routingNetwork;
64  this.fastRouter = fastRouterFactory.createFastRouterDelegate(this, new DijkstraNodeDataFactory(), routingNetwork);
65 
66  this.nodeData.clear();
67  }
68 
69  /*
70  * Replace the references to the from and to nodes with their corresponding
71  * nodes in the routing network.
72  */
73  @Override
74  public Path calcLeastCostPath(final Node fromNode, final Node toNode, final double startTime, final Person person, final Vehicle vehicle) {
75 
76  this.fastRouter.initialize();
77  this.routingNetwork.initialize();
78 
79  RoutingNetworkNode routingNetworkFromNode = this.routingNetwork.getNodes().get(fromNode.getId());
80  RoutingNetworkNode routingNetworkToNode = this.routingNetwork.getNodes().get(toNode.getId());
81 
82  return super.calcLeastCostPath(routingNetworkFromNode, routingNetworkToNode, startTime, person, vehicle);
83  }
84 
85  @Override
86  /*package*/ RouterPriorityQueue<? extends Node> createRouterPriorityQueue() {
87  /*
88  * Re-use existing BinaryMinHeap instead of creating a new one. For large networks (> 10^6 nodes and links) this reduced
89  * the computation time by 40%! cdobler, oct'15
90  */
91  if (this.routingNetwork instanceof ArrayRoutingNetwork) {
92  int size = this.routingNetwork.getNodes().size();
93  if (this.heap == null || this.maxSize != size) {
94  this.maxSize = size;
95  this.heap = new BinaryMinHeap<>(maxSize);
96  return this.heap;
97  } else {
98  this.heap.reset();
99  return this.heap;
100  }
101 // int maxSize = this.routingNetwork.getNodes().size();
102 // return new BinaryMinHeap<ArrayRoutingNetworkNode>(maxSize);
103  } else {
104  return super.createRouterPriorityQueue();
105  }
106  }
107 
108  /*
109  * Constructs the path and replaces the nodes and links from the routing network
110  * with their corresponding nodes and links from the network.
111  */
112  @Override
113  protected Path constructPath(Node fromNode, Node toNode, double startTime, double arrivalTime) {
114  return this.fastRouter.constructPath(fromNode, toNode, startTime, arrivalTime);
115  }
116 
117  /*
118  * For performance reasons the outgoing links of a node are stored in
119  * the routing network in an array instead of a map. Therefore we have
120  * to iterate over an array instead of over a map.
121  */
122  @Override
123  protected void relaxNode(final Node outNode, final Node toNode, final RouterPriorityQueue<Node> pendingNodes) {
124  this.fastRouter.relaxNode(outNode, toNode, pendingNodes);
125  }
126 
127  /*
128  * The DijkstraNodeData is taken from the RoutingNetworkNode and not from a map.
129  */
130  @Override
131  protected DijkstraNodeData getData(final Node n) {
132  return (DijkstraNodeData) this.fastRouter.getData(n);
133  }
134 
135  /*
136  * The DeadEndData is taken from the RoutingNetworkNode and not from a map.
137  */
138  @Override
140  return this.fastRouter.getPreProcessData(n);
141  }
142 }
DijkstraNodeData getData(final Node n)
void relaxNode(final Node outNode, final Node toNode, final RouterPriorityQueue< Node > pendingNodes)
PreProcessDijkstra.DeadEndData getPreProcessData(final Node n)
Map< Id< Node >, RoutingNetworkNode > getNodes()
BinaryMinHeap< ArrayRoutingNetworkNode > heap
Path constructPath(Node fromNode, Node toNode, double startTime, double arrivalTime)
final RoutingNetwork routingNetwork
final PreProcessDijkstra preProcessData
Definition: Dijkstra.java:127
final TravelDisutility costFunction
Definition: Dijkstra.java:98
Path calcLeastCostPath(final Node fromNode, final Node toNode, final double startTime, final Person person, final Vehicle vehicle)
final TravelTime timeFunction
Definition: Dijkstra.java:103
final FastRouterDelegate fastRouter
FastRouterDelegate createFastRouterDelegate(Dijkstra dijkstra, NodeDataFactory nodeDataFactory, RoutingNetwork routingNetwork)