MATSIM
RouteUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.core.population.routes;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.matsim.api.core.v01.Id;
33 import org.matsim.core.gbl.Gbl;
41 import org.matsim.vehicles.Vehicle;
42 
48 public class RouteUtils {
49  private static final Logger log = LogManager.getLogger( RouteUtils.class ) ;
50 
51  private RouteUtils(){} // do not instantiate
52 
60  public static List<Node> getNodes(final NetworkRoute route, final Network network) {
61  List<Id<Link>> linkIds = route.getLinkIds();
62  List<Node> nodes = new ArrayList<>(linkIds.size() + 1);
63  if ((linkIds.size() > 0)) {
64  nodes.add(network.getLinks().get(linkIds.get(0)).getFromNode());
65  for (Id<Link> linkId : linkIds) {
66  Link link = network.getLinks().get(linkId);
67  nodes.add(link.getToNode());
68  }
69  } else if (!route.getStartLinkId().equals(route.getEndLinkId())) {
70  nodes.add(network.getLinks().get(route.getStartLinkId()).getToNode());
71  }
72  return nodes;
73  }
74 
75  public static List<Link> getLinksFromNodes(final List<Node> nodes) {
76  ArrayList<Link> links = new ArrayList<>(nodes.size());
77  Node prevNode = null;
78  for (Node node : nodes) {
79  if (prevNode != null) {
80  Link foundLink = findLink(prevNode, node);
81  if (foundLink != null) {
82  links.add(foundLink);
83  }
84  }
85  prevNode = node;
86  }
87  links.trimToSize();
88  return links;
89  }
90 
91  public static List<Link> getLinksFromNodeIds(final Network network, final List<Id<Node>> nodeIds) {
92  ArrayList<Link> links = new ArrayList<>(nodeIds.size());
93  Node prevNode = null;
94  for (Id<Node> nodeId : nodeIds) {
95  Node node = network.getNodes().get(nodeId);
96  if (prevNode != null) {
97  Link foundLink = findLink(prevNode, node);
98  if (foundLink != null) {
99  links.add(foundLink);
100  }
101  }
102  prevNode = node;
103  }
104  links.trimToSize();
105  return links;
106  }
107 
108  private static Link findLink(Node prevNode, Node node) {
109  for (Link link : prevNode.getOutLinks().values()) {
110  if (link.getToNode().equals(node)) {
111  return link;
112  }
113  }
114  return null;
115  }
116 
117  public static NetworkRoute getSubRoute(final NetworkRoute route, final Node fromNode, final Node toNode, final Network network) {
118  Id<Link> fromLinkId = null;
119  Id<Link> toLinkId = null;
120 
121  List<Id<Link>> linkIds = new ArrayList<>(route.getLinkIds().size() + 2);
122  linkIds.add(route.getStartLinkId());
123  linkIds.addAll(route.getLinkIds());
124  linkIds.add(route.getEndLinkId());
125  for (Id<Link> linkId : linkIds) {
126  Link link = network.getLinks().get(linkId);
127  if (link.getToNode() == fromNode) {
128  fromLinkId = link.getId();
129  }
130  if (link.getFromNode() == toNode) {
131  toLinkId = link.getId();
132  break; // we found the toLinkId, so we can stop searching, fromLinkId should be before after all
133  }
134  }
135 
136  return route.getSubRoute(fromLinkId, toLinkId);
137  }
138 
147  public static double calcDistanceExcludingStartEndLink(final NetworkRoute route, final Network network) {
148  double dist = 0;
149  for (Id<Link> linkId : route.getLinkIds()) {
150  dist += network.getLinks().get(linkId).getLength();
151  }
152  return dist;
153  }
154 
155  public static double calcTravelTimeExcludingStartEndLink( final NetworkRoute networkRoute, double now, Person person, Vehicle vehicle, final
156  Network network, TravelTime travelTime ) {
157  double newTravelTime = 0.0;
158  for (Id<Link> routeLinkId : networkRoute.getLinkIds()) newTravelTime += travelTime.getLinkTravelTime(network.getLinks().get(routeLinkId),
159  now + newTravelTime, person, vehicle);
160  return newTravelTime;
161  }
162 
163 
174  public static double calcDistance(final NetworkRoute networkRoute, final double relPosOnDepartureLink, final double relPosOnArrivalLink, final Network network) {
175  // sum distance of all link besides departure and arrival link
176  double routeDistance = calcDistanceExcludingStartEndLink(networkRoute, network);
177  // add relative distance of departure link
178  routeDistance += network.getLinks().get(networkRoute.getStartLinkId()).getLength() * (1.0 - relPosOnDepartureLink);
179  if (!networkRoute.getStartLinkId().equals(networkRoute.getEndLinkId())){
180  // add relative distance of arrival link
181  routeDistance += network.getLinks().get(networkRoute.getEndLinkId()).getLength() * relPosOnArrivalLink;
182  } else { // i.e. departure = arrival link
183  // subtract relative distance of arrival link that is not traveled
184  routeDistance -= network.getLinks().get(networkRoute.getEndLinkId()).getLength() * (1.0 - relPosOnArrivalLink);
185  }
186  return routeDistance;
187  }
188 
189  public static double calcTravelTime( final NetworkRoute networkRoute, final double relPosOnDepartureLink, final double relPosOnArrivalLink,
190  double now, Person person, Vehicle vehicle, final Network network, TravelTime travelTime) {
191 
192  if (!networkRoute.getStartLinkId().equals(networkRoute.getEndLinkId())){
193  return 0.;
194  }
195  double startTime = now;
196 
197  // add relative distance of departure link
198  now += (1.0 - relPosOnDepartureLink) * travelTime.getLinkTravelTime( network.getLinks().get( networkRoute.getStartLinkId() ), now, person, vehicle );
199 
200  // sum distance of all link besides departure and arrival link
201  now += calcTravelTimeExcludingStartEndLink( networkRoute, now, person, vehicle, network, travelTime);
202 
203  // add time on arrival link
204  now += relPosOnArrivalLink * travelTime.getLinkTravelTime( network.getLinks().get( networkRoute.getEndLinkId() ), now, person, vehicle );
205 
206  return now - startTime;
207  }
208 
209  @Deprecated // rename to calcDistanceExcludingStartEndLink. kai, feb'25
210  public static double calcDistance( final LeastCostPathCalculator.Path path ) {
211  double length = 0. ;
212  for ( Link link : path.links ) {
213  length += link.getLength() ;
214  }
215  return length ;
216  }
217  @Deprecated // network argument is not needed; please inline. kai, sep'20
218  public static NetworkRoute createNetworkRoute( List<Id<Link>> routeLinkIds, Network network ) {
219  return createNetworkRoute( routeLinkIds );
220  }
221  public static NetworkRoute createNetworkRoute( List<Id<Link>> routeLinkIds ) {
222  Id<Link> startLinkId = routeLinkIds.get(0);
223  List<Id<Link>> linksBetween = (routeLinkIds.size() > 2) ? routeLinkIds.subList(1, routeLinkIds.size() - 1) : new ArrayList<>(0);
224  Id<Link> endLinkId = routeLinkIds.get(routeLinkIds.size() - 1);
225  NetworkRoute route = createLinkNetworkRouteImpl(startLinkId, endLinkId);
226  route.setLinkIds(startLinkId, linksBetween, endLinkId);
227  return route;
228  }
229 
230  public static double calcDistance(TransitPassengerRoute route, TransitSchedule ts, Network network) {
231  Id<TransitLine> lineId = route.getLineId();
232  Id<TransitRoute> routeId = route.getRouteId();
233  Id<TransitStopFacility> enterStopId = route.getAccessStopId();
234  Id<TransitStopFacility> exitStopId = route.getEgressStopId();
235 
236  TransitLine line = ts.getTransitLines().get(lineId);
237  TransitRoute tr = line.getRoutes().get(routeId);
238 
239  TransitStopFacility accessFacility = ts.getFacilities().get(enterStopId);
240  TransitStopFacility egressFacility = ts.getFacilities().get(exitStopId);
241 
242  return calcDistance(tr, accessFacility, egressFacility, network);
243  }
244 
245  public static double calcDistance(TransitRoute tr, TransitStopFacility accessFacility, TransitStopFacility egressFacility, Network network) {
246  Id<Link> enterLinkId = accessFacility.getLinkId();
247  Id<Link> exitLinkId = egressFacility.getLinkId();
248 
249  NetworkRoute nr = tr.getRoute();
250  double dist = 0;
251  boolean count = false;
252  if (enterLinkId.equals(nr.getStartLinkId())) {
253  count = true;
254  }
255  for (Id<Link> linkId : nr.getLinkIds()) {
256  if (count) {
257  Link l = network.getLinks().get(linkId);
258  if ( l==null ) {
259  log.error( "link is null; linkId=" + linkId + "; network=" + network ) ;
260  }
261  dist += l.getLength();
262  }
263  if (enterLinkId.equals(linkId)) {
264  count = true;
265  }
266  if (exitLinkId.equals(linkId)) {
267  count = false;
268  break;
269  }
270  }
271  if (count) {
272  Link l = network.getLinks().get(nr.getEndLinkId());
273  dist += l.getLength();
274  }
275  return dist;
276  }
277 
286  public static double calculateCoverage(NetworkRoute route1, NetworkRoute route2, Network network ) {
287  Gbl.assertNotNull( route1 );
288  Gbl.assertNotNull( route2 );
289  Gbl.assertNotNull( network );
290 
291  double routeLength = 0. ;
292  double coveredLength = 0. ;
293  for ( Id<Link> id : route1.getLinkIds() ) {
294  final Link link = network.getLinks().get( id );
295  Gbl.assertNotNull( link );
296  routeLength += link.getLength() ;
297  if ( route2.getLinkIds().contains(id) ) {
298  coveredLength += link.getLength() ;
299  }
300  }
301  if ( routeLength > 0. ) {
302  return coveredLength/routeLength ;
303  } else {
304  return 1. ; // route has zero length = fully covered by any other route. (but they are not similar!?!?!?)
305  }
306  }
307 
308  public static Route createGenericRouteImpl(Id<Link> startLinkId, Id<Link> endLinkId) {
309  return new GenericRouteImpl(startLinkId, endLinkId);
310  }
311 
312  public static NetworkRoute createLinkNetworkRouteImpl(Id<Link> startLinkId, Id<Link> endLinkId) {
313  return new LinkNetworkRouteImpl(startLinkId, endLinkId);
314  }
315 
316  public static NetworkRoute createLinkNetworkRouteImpl(Id<Link> startLinkId, List<Id<Link>> linkIds,
317  Id<Link> endLinkId) {
318  return new LinkNetworkRouteImpl(startLinkId, linkIds, endLinkId);
319  }
320 
321  public static NetworkRoute createLinkNetworkRouteImpl(Id<Link> startLinkId, Id<Link>[] linkIds,
322  Id<Link> endLinkId) {
323  return new LinkNetworkRouteImpl(startLinkId, linkIds, endLinkId);
324  }
325 
326 }
Map< Id< TransitRoute >, TransitRoute > getRoutes()
static< T > Id< T > get(int index, final Class< T > type)
Definition: Id.java:112
Map< Id< Node >, ? extends Node > getNodes()
Map< Id< TransitStopFacility >, TransitStopFacility > getFacilities()
static Route createGenericRouteImpl(Id< Link > startLinkId, Id< Link > endLinkId)
static List< Link > getLinksFromNodes(final List< Node > nodes)
Definition: RouteUtils.java:75
static Link findLink(Node prevNode, Node node)
double getLinkTravelTime(Link link, double time, Person person, Vehicle vehicle)
NetworkRoute getSubRoute(final Id< Link > fromLinkId, final Id< Link > toLinkId)
static List< Link > getLinksFromNodeIds(final Network network, final List< Id< Node >> nodeIds)
Definition: RouteUtils.java:91
static NetworkRoute createLinkNetworkRouteImpl(Id< Link > startLinkId, List< Id< Link >> linkIds, Id< Link > endLinkId)
static double calcDistance(TransitRoute tr, TransitStopFacility accessFacility, TransitStopFacility egressFacility, Network network)
static NetworkRoute createLinkNetworkRouteImpl(Id< Link > startLinkId, Id< Link > endLinkId)
Id< TransitStopFacility > getEgressStopId()
static NetworkRoute createNetworkRoute(List< Id< Link >> routeLinkIds)
static NetworkRoute createNetworkRoute(List< Id< Link >> routeLinkIds, Network network)
static void assertNotNull(Object obj)
Definition: Gbl.java:212
static double calcDistance(final NetworkRoute networkRoute, final double relPosOnDepartureLink, final double relPosOnArrivalLink, final Network network)
Map< Id< Link >, ? extends Link > getLinks()
static double calcDistance(TransitPassengerRoute route, TransitSchedule ts, Network network)
static List< Node > getNodes(final NetworkRoute route, final Network network)
Definition: RouteUtils.java:60
static double calcTravelTimeExcludingStartEndLink(final NetworkRoute networkRoute, double now, Person person, Vehicle vehicle, final Network network, TravelTime travelTime)
static double calcDistanceExcludingStartEndLink(final NetworkRoute route, final Network network)
static NetworkRoute createLinkNetworkRouteImpl(Id< Link > startLinkId, Id< Link >[] linkIds, Id< Link > endLinkId)
static double calcDistance(final LeastCostPathCalculator.Path path)
static double calculateCoverage(NetworkRoute route1, NetworkRoute route2, Network network)
Map< Id< Link >, ? extends Link > getOutLinks()
boolean equals(Object obj)
Definition: Id.java:139
void setLinkIds(final Id< Link > startLinkId, final List< Id< Link >> linkIds, final Id< Link > endLinkId)
static double calcTravelTime(final NetworkRoute networkRoute, final double relPosOnDepartureLink, final double relPosOnArrivalLink, double now, Person person, Vehicle vehicle, final Network network, TravelTime travelTime)
Map< Id< TransitLine >, TransitLine > getTransitLines()
static NetworkRoute getSubRoute(final NetworkRoute route, final Node fromNode, final Node toNode, final Network network)
Id< TransitStopFacility > getAccessStopId()