MATSIM
FreespeedFactorRoutingModule.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2 
3  * project: org.matsim.*
4  * LegRouterWrapper.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2012 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 package org.matsim.core.router;
22 
26 import org.matsim.api.core.v01.population.*;
32 
33 import java.util.Arrays;
34 import java.util.List;
35 
36 public final class FreespeedFactorRoutingModule implements RoutingModule {
37 
38  private final String mode;
40 
41  private final Network network;
44 
46  final String mode,
47  final PopulationFactory populationFactory,
48  final Network network,
49  final LeastCostPathCalculator routeAlgo,
50  TeleportedModeParams params ) {
51  this.network = network;
52  this.routeAlgo = routeAlgo;
53  this.params = params;
54  this.mode = mode;
55  this.populationFactory = populationFactory;
56  }
57 
58  @Override
59  public List<? extends PlanElement> calcRoute(RoutingRequest request) {
60  final Facility fromFacility = request.getFromFacility();
61  final Facility toFacility = request.getToFacility();
62  final double departureTime = request.getDepartureTime();
63  final Person person = request.getPerson();
64 
65  Leg newLeg = this.populationFactory.createLeg( this.mode );
66  newLeg.setDepartureTime( departureTime );
67 
68  double travTime = routeLeg(
69  person,
70  newLeg,
71  new FacilityWrapperActivity( fromFacility ),
72  new FacilityWrapperActivity( toFacility ),
73  departureTime);
74 
75  // otherwise, information may be lost
76  newLeg.setTravelTime( travTime );
77 
78  return Arrays.asList( newLeg );
79  }
80 
81  @Override
82  public String toString() {
83  return "[LegRouterWrapper: mode="+this.mode+"]";
84  }
85 
86  /* package (for tests) */ final double routeLeg(Person person, Leg leg, Activity fromAct, Activity toAct, double depTime) {
87  int travTime = 0;
88  final Link fromLink = this.network.getLinks().get(fromAct.getLinkId());
89  final Link toLink = this.network.getLinks().get(toAct.getLinkId());
90  if (fromLink == null) throw new RuntimeException("fromLink missing.");
91  if (toLink == null) throw new RuntimeException("toLink missing.");
92  if (toLink != fromLink) {
93  Node startNode = fromLink.getToNode(); // start at the end of the "current" link
94  Node endNode = toLink.getFromNode(); // the target is the start of the link
95  // do not drive/walk around, if we stay on the same link
96  Path path = this.routeAlgo.calcLeastCostPath(startNode, endNode, depTime, person, null);
97  if (path == null) throw new RuntimeException("No route found from node " + startNode.getId() + " to node " + endNode.getId() + ".");
98 
99  // we're still missing the time on the final link, which the agent has to drive on in the java mobsim
100  // so let's calculate the final part.
101  double speed = toLink.getFreespeed(depTime + path.travelTime);
102 
103  // correct by speed limit:
104  if ( speed > params.getTeleportedModeFreespeedLimit() ) {
105  speed = params.getTeleportedModeFreespeedLimit() ;
106  }
107 
108  // now correct the travel time:
109  double travelTimeLastLink = toLink.getLength() / speed;
110 
111  travTime = (int) (((int) path.travelTime + travelTimeLastLink) * this.params.getTeleportedModeFreespeedFactor());
112  Route route = this.populationFactory.getRouteFactories().createRoute(Route.class, fromLink.getId(), toLink.getId());
113  route.setTravelTime(travTime);
114 
115  // yyyyyy the following should actually rather come from the route! There is a RouteUtils.calcDistance( route ) . kai, nov'16
116  double dist = 0;
117  if ((fromAct.getCoord() != null) && (toAct.getCoord() != null)) {
118  dist = CoordUtils.calcEuclideanDistance(fromAct.getCoord(), toAct.getCoord());
119  } else {
120  dist = CoordUtils.calcEuclideanDistance(fromLink.getCoord(), toLink.getCoord());
121  }
122  route.setDistance(dist * this.params.getBeelineDistanceFactor());
123  leg.setRoute(route);
124  } else {
125  // create an empty route == staying on place if toLink == endLink
126  Route route = this.populationFactory.getRouteFactories().createRoute(Route.class, fromLink.getId(), toLink.getId());
127  route.setTravelTime(0);
128  route.setDistance(0.0);
129  leg.setRoute(route);
130  travTime = 0;
131  }
132  leg.setDepartureTime(depTime);
133  leg.setTravelTime(travTime);
134  Leg r = (leg);
135  r.setTravelTime( depTime + travTime - r.getDepartureTime().seconds()); // yy something needs to be done once there are alternative implementations of the interface. kai, apr'10
136  return travTime;
137  }
138 
139 }
void setDistance(final double distance)
static double calcEuclideanDistance(Coord coord, Coord other)
void setDepartureTime(final double seconds)
Path calcLeastCostPath(Node fromNode, Node toNode, double starttime, final Person person, final Vehicle vehicle)
List<? extends PlanElement > calcRoute(RoutingRequest request)
Map< Id< Link >, ? extends Link > getLinks()
void setTravelTime(final double seconds)
void setTravelTime(final double travelTime)