MATSIM
AbstractFastRouterDelegate.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * FastRouterDelegate.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 
32 
33 import java.util.ArrayList;
34 
35 /*package*/ abstract class AbstractFastRouterDelegate implements FastRouterDelegate {
36 
37  /*package*/ final Dijkstra dijkstra;
38  /*package*/ final NodeDataFactory nodeDataFactory;
39 
40  /*package*/ AbstractFastRouterDelegate(final Dijkstra dijkstra, final NodeDataFactory nodeDataFactory) {
41  this.dijkstra = dijkstra;
42  this.nodeDataFactory = nodeDataFactory;
43  }
44 
45  @Override
46  public void initialize() {
47  // Some classes might override this method and do some additional stuff...
48  }
49 
50  @Override
51  public Path constructPath(Node fromNode, Node toNode, double startTime, double arrivalTime) {
52  ArrayList<Node> nodes = new ArrayList<>();
53  ArrayList<Link> links = new ArrayList<>();
54 
55  nodes.add(0, ((RoutingNetworkNode) toNode).getNode());
56  Link tmpLink = getData(toNode).getPrevLink();
57 // if (tmpLink != null) {
58  // original code
59 // while (tmpLink.getFromNode() != fromNode) {
60 // links.add(0, ((RoutingNetworkLink) tmpLink).getLink());
61 // nodes.add(0, ((RoutingNetworkLink) tmpLink).getLink().getFromNode());
62 // tmpLink = getData(tmpLink.getFromNode()).getPrevLink();
63 // }
64 // links.add(0, ((RoutingNetworkLink) tmpLink).getLink());
65 // nodes.add(0, ((RoutingNetworkNode) tmpLink.getFromNode()).getNode());
66 
67  /*
68  * Adapted this code to be compatible with the MultiNodeDijkstra located in
69  * the location choice contrib. When a MultiNodeDijkstra uses multiple start nodes,
70  * there is not a single start node that could be used to check whether
71  * "tmpLink.getFromNode() != fromNode" is true. Instead, the start nodes do not have
72  * a previous link.
73  * For the regular Dikstra, this is also fine since the start node also does not have
74  * a previous node.
75  * cdobler, feb'14
76  */
77  while (tmpLink != null) {
78  links.add(0, ((RoutingNetworkLink) tmpLink).getLink());
79  nodes.add(0, ((RoutingNetworkLink) tmpLink).getLink().getFromNode());
80  tmpLink = getData(tmpLink.getFromNode()).getPrevLink();
81  }
82 // }
83 
84  NodeData toNodeData = getData(toNode);
85  return new Path(nodes, links, arrivalTime - startTime, toNodeData.getCost());
86  }
87 
88  @Override
89  public void relaxNode(final Node outNode, final Node toNode, final RouterPriorityQueue<Node> pendingNodes) {
90 
91  RoutingNetworkNode routingNetworkNode = (RoutingNetworkNode) outNode;
92  NodeData outData = getData(routingNetworkNode);
93  double currTime = outData.getTime();
94  double currCost = outData.getCost();
95  if (this.dijkstra.pruneDeadEnds) {
96  PreProcessDijkstra.DeadEndData ddOutData = getPreProcessData(routingNetworkNode);
97 
98  for (Link l : routingNetworkNode.getOutLinksArray()) {
99  this.dijkstra.relaxNodeLogic(l, pendingNodes, currTime, currCost, toNode, ddOutData);
100  }
101  } else { // this.pruneDeadEnds == false
102  for (Link l : routingNetworkNode.getOutLinksArray()) {
103  this.dijkstra.relaxNodeLogic(l, pendingNodes, currTime, currCost, toNode, null);
104  }
105  }
106  }
107 
108  @Override
109  public PreProcessDijkstra.DeadEndData getPreProcessData(final Node n) {
110  return ((RoutingNetworkNode) n).getDeadEndData();
111  }
112 }