MATSIM
TransitRouterImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TranitRouter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 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.pt.router;
22 
23 import org.matsim.api.core.v01.Coord;
34 
35 import java.util.Collection;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.Map;
39 
45 public class TransitRouterImpl extends AbstractTransitRouter implements TransitRouter {
46 
48  private final TravelTime travelTime;
51 
52  private boolean cacheTree;
55  private double previousDepartureTime;
56 
58  super(trConfig);
59  this.transitNetwork = TransitRouterNetwork.createFromSchedule(schedule,
61  this.preparedTransitSchedule = new PreparedTransitSchedule(schedule);
62  TransitRouterNetworkTravelTimeAndDisutility transitRouterNetworkTravelTimeAndDisutility = new TransitRouterNetworkTravelTimeAndDisutility(
63  trConfig,
64  new PreparedTransitSchedule(schedule));
65  this.travelDisutility = transitRouterNetworkTravelTimeAndDisutility;
66  this.travelTime = transitRouterNetworkTravelTimeAndDisutility;
67  setTransitTravelDisutility(this.travelDisutility);
68 
69  this.cacheTree = trConfig.isCacheTree();
70  }
71 
74  final PreparedTransitSchedule preparedTransitSchedule,
75  final TransitRouterNetwork routerNetwork,
76  final TravelTime travelTime,
77  final TransitTravelDisutility travelDisutility) {
78 
79  super(trConfig, travelDisutility);
80 
81  this.transitNetwork = routerNetwork;
82  this.preparedTransitSchedule = preparedTransitSchedule;
83  this.travelDisutility = travelDisutility;
84  this.travelTime = travelTime;
85 
86  this.cacheTree = trConfig.isCacheTree();
87  }
88 
89  private Map<Node, InitialNode> locateWrappedNearestTransitNodes(Person person, Coord coord, double departureTime) {
91  coord,
92  this.getConfig().getSearchRadius());
93  if (nearestNodes.size() < 2) {
94  // also enlarge search area if only one stop found, maybe a second one is near the border of the search area
96  .getNearestNode(coord);
97  if (nearestNode != null) { // transit schedule might be completely empty!
98  double distance = CoordUtils.calcEuclideanDistance(coord,
99  nearestNode.stop.getStopFacility().getCoord());
100  nearestNodes = this.getTransitRouterNetwork()
101  .getNearestNodes(coord, distance + this.getConfig().getExtensionRadius());
102  }
103  }
104  Map<Node, InitialNode> wrappedNearestNodes = new LinkedHashMap<>();
105  for (TransitRouterNetwork.TransitRouterNetworkNode node : nearestNodes) {
106  Coord toCoord = node.stop.getStopFacility().getCoord();
107  double initialTime = getWalkTime(person, coord, toCoord);
108  double initialCost = getWalkDisutility(person, coord, toCoord);
109  wrappedNearestNodes.put(node, new InitialNode(initialCost, initialTime + departureTime));
110  }
111  return wrappedNearestNodes;
112  }
113 
114  @Override
115  public List<? extends PlanElement> calcRoute( final RoutingRequest request ) {
116  final Facility fromFacility = request.getFromFacility();
117  final Facility toFacility = request.getToFacility();
118  final double departureTime = request.getDepartureTime();
119  final Person person = request.getPerson();
120 
121  // find possible start stops
122  Map<Node, InitialNode> wrappedFromNodes = this.locateWrappedNearestTransitNodes(person,
123  fromFacility.getCoord(),
124  departureTime);
125 
126 
127  // find possible end stops
128  Map<Node, InitialNode> wrappedToNodes = this.locateWrappedNearestTransitNodes(person,
129  toFacility.getCoord(),
130  departureTime);
131 
132  InternalTransitPassengerRoute transitPassengerRoute = null;
133 
134  if (cacheTree) {
135  if ((fromFacility != previousFromFacility) && (departureTime != previousDepartureTime)) { // Compute tree only if fromFacility and departure time are different from previous request.
138  getTravelTime(),
139  wrappedFromNodes,
140  person);
141  }
142  } else { // Compute new tree for every routing request
145  getTravelTime(),
146  wrappedFromNodes,
147  wrappedToNodes,
148  person);
149  // yyyyyy This sounds like it is doing the full tree. But I think it is not. Kai, nov'16
150  // Yes, only if you leave out the wrappedToNodes from the argument list, it does compute the full tree. See the new case above. dz, june'18
151  }
152 
153  // find routes between start and end stop
154  transitPassengerRoute = tree.getTransitPassengerRoute(wrappedToNodes);
155 
156  if (transitPassengerRoute == null) {
157  return null; // TripRouter / FallbackRoutingModule will create a direct walk leg
158  }
159  double pathCost = transitPassengerRoute.getTravelCost();
160 
161  double directWalkCost = getWalkDisutility(person, fromFacility.getCoord(), toFacility.getCoord());
162 
163  if (directWalkCost * getConfig().getDirectWalkFactor() < pathCost) {
164  return null; // TripRouter / FallbackRoutingModule will create a direct walk leg
165  }
166 
167  previousFromFacility = fromFacility;
168  previousDepartureTime = departureTime;
169 
170  return convertPassengerRouteToLegList(departureTime,
171  transitPassengerRoute,
172  fromFacility.getCoord(),
173  toFacility.getCoord(),
174  person);
175  }
176 
178  return transitNetwork;
179  }
180 
181  TravelTime getTravelTime() {
182  return travelTime;
183  }
184 
185  PreparedTransitSchedule getPreparedTransitSchedule() {
187  }
188 }
Map< Node, InitialNode > locateWrappedNearestTransitNodes(Person person, Coord coord, double departureTime)
List<? extends PlanElement > calcRoute(final RoutingRequest request)
final double getWalkDisutility(Person person, Coord coord, Coord toCoord)
final TransitRouterNetwork transitNetwork
static double calcEuclideanDistance(Coord coord, Coord other)
List< Leg > convertPassengerRouteToLegList(double departureTime, InternalTransitPassengerRoute p, Coord fromCoord, Coord toCoord, Person person)
TransitRouterImpl(final TransitRouterConfig trConfig, final PreparedTransitSchedule preparedTransitSchedule, final TransitRouterNetwork routerNetwork, final TravelTime travelTime, final TransitTravelDisutility travelDisutility)
void setTransitTravelDisutility(TransitTravelDisutility transitTravelDisutility)
final double getWalkTime(Person person, Coord coord, Coord toCoord)
final TransitTravelDisutility travelDisutility
TransitRouterImpl(final TransitRouterConfig trConfig, final TransitSchedule schedule)
static TransitRouterNetwork createFromSchedule(final TransitSchedule schedule, final double maxBeelineWalkConnectionDistance)
InternalTransitPassengerRoute getTransitPassengerRoute(final Map< Node, InitialNode > toNodes)
Collection< TransitRouterNetworkNode > getNearestNodes(final Coord coord, final double distance)
final PreparedTransitSchedule preparedTransitSchedule
TransitRouterNetworkNode getNearestNode(final Coord coord)
final TransitTravelDisutility getTravelDisutility()