MATSIM
InvertedLeastPathCalculator.java
Go to the documentation of this file.
1 package org.matsim.core.router;
2 
3 import org.matsim.api.core.v01.Id;
9 import org.matsim.core.router.util.*;
10 import org.matsim.vehicles.Vehicle;
11 
12 import java.util.ArrayList;
13 import java.util.List;
14 
21 class InvertedLeastPathCalculator implements LeastCostPathCalculator {
22 
23  private final Network network;
24  private final LeastCostPathCalculator leastCostPathCalculator;
25 
26  InvertedLeastPathCalculator(Network network, LeastCostPathCalculator leastCostPathCalculator) {
27  this.network = network;
28  this.leastCostPathCalculator = leastCostPathCalculator;
29  }
30 
31 
35  static InvertedLeastPathCalculator create(LeastCostPathCalculatorFactory costFactory, TravelDisutilityFactory travelTimeFactory,
36  Network network, Network invertedNetwork, LinkToLinkTravelTime l2ltravelTimes) {
37 
38  // convert l2ltravelTimes into something that can be used by the inverted network router:
39  TravelTimesInvertedNetworkProxy invertedTravelTimes = new TravelTimesInvertedNetworkProxy(network, l2ltravelTimes);
40  // (method that takes a getLinkTravelTime( link , ...) with a link from the inverted network, converts it into links on the
41  // original network, and looks up the link2link tttime in the l2ltravelTimes data structure)
42 
43  TravelDisutility travelCost = travelTimeFactory.createTravelDisutility(invertedTravelTimes);
44 
45  return new InvertedLeastPathCalculator(network, costFactory.createPathCalculator(invertedNetwork, travelCost, invertedTravelTimes));
46  }
47 
48  @Override
49  public Path calcLeastCostPath(Node fromNode, Node toNode, double starttime, Person person, Vehicle vehicle) {
50  Path path = leastCostPathCalculator.calcLeastCostPath(fromNode, toNode, starttime, person, vehicle);
51  if (path == null)
52  return null;
53 
54  return invertPath(path);
55  }
56 
57 
58  private Path invertPath(Path invPath) {
59  int invLinkCount = invPath.links.size();//==> normal node count
60 
61  //path search is called only if fromLinkId != toLinkId
62  //see: org.matsim.core.router.NetworkRoutingModule.routeLeg()
63  //implies: fromInvNode != toInvNode
64  if (invLinkCount == 0) {
65  throw new RuntimeException(
66  "The path in the inverted network should consist of at least one link.");
67  }
68 
69  List<Link> links = new ArrayList<>(invLinkCount - 1);
70  for (int i = 1; i < invLinkCount; i++) {
71  Id<Link> linkId = Id.create(invPath.nodes.get(i).getId(), Link.class);
72  links.add(network.getLinks().get(linkId));
73  }
74 
75  List<Node> nodes = new ArrayList<>(invLinkCount);
76 // nodes.add(links.get(0).getFromNode());
77  /* use the first link of the inverted path instead of the first node of the just created link list. also works for invLinkCount 1. theresa, jan'17 */
78  nodes.add(network.getNodes().get(Id.create(invPath.links.get(0).getId(), Node.class)));
79  for (Link l : links) {
80  nodes.add(l.getToNode());
81  }
82 
83  return new Path(nodes, links, invPath.travelTime, invPath.travelCost);
84  }
85 
86  private static class TravelTimesInvertedNetworkProxy implements TravelTime {
87 
88  private final Network network;
90 
92  this.linkToLinkTravelTime = l2ltt;
93  this.network = network;
94  }
95 
102  @Override
103  public double getLinkTravelTime(Link invLink, double time, Person person, Vehicle vehicle) {
104  Link fromLink = network.getLinks()
105  .get(Id.create(invLink.getFromNode().getId(), Link.class));
106  Link toLink = network.getLinks()
107  .get(Id.create(invLink.getToNode().getId(), Link.class));
108  return linkToLinkTravelTime.getLinkToLinkTravelTime(fromLink, toLink, time, person, vehicle);
109  }
110  }
111 }
Map< Id< Node >, ? extends Node > getNodes()
double getLinkTravelTime(Link invLink, double time, Person person, Vehicle vehicle)
Path calcLeastCostPath(Node fromNode, Node toNode, double starttime, final Person person, final Vehicle vehicle)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
Map< Id< Link >, ? extends Link > getLinks()
LeastCostPathCalculator createPathCalculator(final Network network, final TravelDisutility travelCosts, final TravelTime travelTimes)