MATSIM
TransitLoad.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.pt.analysis;
21 
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 
27 import org.matsim.api.core.v01.Id;
44 import org.matsim.vehicles.Vehicle;
45 
53 
54  private final Map<Id<TransitLine>, LineData> lineData = new HashMap<>();
55 
56  private final Map<Id<Vehicle>, Id<TransitStopFacility>> vehicleFacilityMap = new HashMap<>();
57  private final Map<Id<Vehicle>, VehicleData> vehicleData = new HashMap<>();
58 
59  public TransitLoad() {
60  }
61 
71  @Deprecated
72  public int getLoadAtDeparture(final TransitLine line, final TransitRoute route, final TransitStopFacility stopFacility, final Departure departure) {
73  for (int i = 0; i < route.getStops().size(); i++) {
74  if (route.getStops().get(i).getStopFacility().getId().equals(stopFacility.getId())) {
75  return getLoadAtDeparture(line, route, i, departure);
76  }
77  }
78  return -1;
79  }
80 
81  public int getLoadAtDeparture(final TransitLine line, final TransitRoute route, final int transitRouteStopIndex, final Departure departure) {
82  int nOfPassengers = 0;
83 
84  /*
85  * count how often a stop was visited while following the route in
86  * route.getStops() to differentiate multiple servings of the same
87  * TransitStopFacility. Count from 0, so count equals index in list
88  */
89  Map<Id<TransitStopFacility>, Integer> stop2nrVisited = new HashMap<>();
90  for (int i = 0; i < route.getStops().size(); i++) {
91  TransitRouteStop stop = route.getStops().get(i);
92  Integer nrVisited = stop2nrVisited.get(stop.getStopFacility().getId());
93  if (nrVisited == null) {
94  nrVisited = 0;
95  } else {
96  nrVisited++;
97  }
98  stop2nrVisited.put(stop.getStopFacility().getId(), nrVisited);
99 
100  List<StopInformation> siList = getStopInformation(line.getId(), route.getId(), stop.getStopFacility().getId(), departure.getId(), false);
101  if (siList != null) {
102  StopInformation si = siList.get(nrVisited);
103  if (si != null) {
104  nOfPassengers -= si.nOfLeaving;
105  nOfPassengers += si.nOfEntering;
106  }
107  }
108  if (i == transitRouteStopIndex) {
109  return nOfPassengers;
110  }
111  }
112  return -1;
113  }
114 
115  public List<StopInformation> getDepartureStopInformation(final TransitLine line, final TransitRoute route, final TransitStopFacility stopFacility, final Departure departure) {
116  return getStopInformation(line.getId(), route.getId(), stopFacility.getId(), departure.getId(), false);
117  }
118 
119  @Override
121  this.vehicleData.put(event.getVehicleId(), new VehicleData(event.getVehicleId(), event.getTransitLineId(), event.getTransitRouteId(), event.getDepartureId(), event.getDriverId()));
122  }
123 
124  @Override
125  public void handleEvent(final VehicleArrivesAtFacilityEvent event) {
126  this.vehicleFacilityMap.put(event.getVehicleId(), event.getFacilityId());
127  VehicleData vData = this.vehicleData.get(event.getVehicleId());
128  if (vData != null) {
129  List<StopInformation> siList = getStopInformation(vData.lineId, vData.routeId, this.vehicleFacilityMap.get(event.getVehicleId()), vData.departureId, true);
130  // Vehicle arrives at facility -> begin of serving the stop (once more) -> create new StopInformation
131  StopInformation si = new StopInformation();
132  si.arrivalTime = event.getTime();
133  siList.add(si);
134  }
135  }
136 
137  @Override
138  public void handleEvent(final VehicleDepartsAtFacilityEvent event) {
139  Id<TransitStopFacility> stopId = this.vehicleFacilityMap.remove(event.getVehicleId());
140  VehicleData vData = this.vehicleData.get(event.getVehicleId());
141  if (vData != null) {
142  List<StopInformation> siList = getStopInformation(vData.lineId, vData.routeId, stopId, vData.departureId, true);
143  // Vehicle is already at facility -> add information to last StopInformation
144  StopInformation si = siList.get(siList.size() - 1);
145  si.departureTime = event.getTime();
146  }
147  }
148 
149  @Override
150  public void handleEvent(final PersonEntersVehicleEvent event) {
151  VehicleData vData = this.vehicleData.get(event.getVehicleId());
152  if (vData != null) {
153  if (!vData.driverId.equals(event.getPersonId())) {
154  List<StopInformation> siList = getStopInformation(vData.lineId, vData.routeId,
155  this.vehicleFacilityMap.get(event.getVehicleId()), vData.departureId, true);
156  // Vehicle is already at facility -> add information to last StopInformation
157  StopInformation si = siList.get(siList.size() - 1);
158  si.nOfEntering++;
159  }
160  }
161  }
162 
163  @Override
164  public void handleEvent(final PersonLeavesVehicleEvent event) {
165  VehicleData vData = this.vehicleData.get(event.getVehicleId());
166  if (vData != null) {
167  if (!vData.driverId.equals(event.getPersonId())) {
168  List<StopInformation> siList = getStopInformation(vData.lineId, vData.routeId,
169  this.vehicleFacilityMap.get(event.getVehicleId()), vData.departureId, true);
170  // Vehicle is already at facility -> add information to last StopInformation
171  StopInformation si = siList.get(siList.size() - 1);
172  si.nOfLeaving++;
173  }
174  }
175  }
176 
177  @Override
178  public void reset(final int iteration) {
179  this.vehicleFacilityMap.clear();
180  this.vehicleData.clear();
181  }
182 
183  private List<StopInformation> getStopInformation(final Id<TransitLine> lineId, final Id<TransitRoute> routeId, final Id<TransitStopFacility> stopFacilityId, final Id<Departure> departureId, final boolean createIfMissing) {
184  LineData ld = this.lineData.get(lineId);
185  if (ld == null) {
186  if (createIfMissing) {
187  ld = new LineData();
188  this.lineData.put(lineId, ld);
189  } else {
190  return null;
191  }
192  }
193 
194  RouteData rd = ld.routeData.get(routeId);
195  if (rd == null) {
196  if (createIfMissing) {
197  rd = new RouteData();
198  ld.routeData.put(routeId, rd);
199  } else {
200  return null;
201  }
202  }
203 
204  StopData sd = rd.stopData.get(stopFacilityId);
205  if (sd == null) {
206  if (createIfMissing) {
207  sd = new StopData();
208  rd.stopData.put(stopFacilityId, sd);
209  } else {
210  return null;
211  }
212  }
213 
214  List<StopInformation> siList = sd.departureData.get(departureId);
215  if (siList == null) {
216  if (createIfMissing) {
217  siList = new ArrayList<>();
218  sd.departureData.put(departureId, siList);
219  } else {
220  return null;
221  }
222  }
223  return siList;
224  }
225 
226  private static class VehicleData {
227  public final Id<Vehicle> vehicleId;
228  public final Id<TransitLine> lineId;
229  public final Id<TransitRoute> routeId;
231  public final Id<Person> driverId;
232 
233  public VehicleData(final Id<Vehicle> vehicleId, final Id<TransitLine> lineId, final Id<TransitRoute> routeId, final Id<Departure> departureId, final Id<Person> driverId) {
234  this.vehicleId = vehicleId;
235  this.lineId = lineId;
236  this.routeId = routeId;
237  this.departureId = departureId;
238  this.driverId = driverId;
239  }
240  }
241 
242  /*package*/ static class LineData {
243  public final Map<Id<TransitRoute>, RouteData> routeData = new HashMap<>();
244  }
245 
246  /*package*/ static class RouteData {
247  public final Map<Id<TransitStopFacility>, StopData> stopData = new HashMap<>(); // use stopFacilityId as key
248  }
249 
250  /*package*/ static class StopData {
251  public final Map<Id<Departure>, List<StopInformation>> departureData = new HashMap<>(); // use departure id as key
252  }
253 
254  public static class StopInformation {
255  public short nOfEntering = 0;
256  public short nOfLeaving = 0;
257  public double arrivalTime = Double.NaN;
258  public double departureTime = Double.NaN;
259  }
260 
261 }
int getLoadAtDeparture(final TransitLine line, final TransitRoute route, final TransitStopFacility stopFacility, final Departure departure)
List< StopInformation > getDepartureStopInformation(final TransitLine line, final TransitRoute route, final TransitStopFacility stopFacility, final Departure departure)
int getLoadAtDeparture(final TransitLine line, final TransitRoute route, final int transitRouteStopIndex, final Departure departure)
final Map< Id< Vehicle >, Id< TransitStopFacility > > vehicleFacilityMap
void handleEvent(final VehicleDepartsAtFacilityEvent event)
final Map< Id< Vehicle >, VehicleData > vehicleData
VehicleData(final Id< Vehicle > vehicleId, final Id< TransitLine > lineId, final Id< TransitRoute > routeId, final Id< Departure > departureId, final Id< Person > driverId)
void handleEvent(final VehicleArrivesAtFacilityEvent event)
final Map< Id< TransitLine >, LineData > lineData
abstract TransitStopFacility getStopFacility()
void handleEvent(final PersonEntersVehicleEvent event)
List< StopInformation > getStopInformation(final Id< TransitLine > lineId, final Id< TransitRoute > routeId, final Id< TransitStopFacility > stopFacilityId, final Id< Departure > departureId, final boolean createIfMissing)
void reset(final int iteration)
void handleEvent(TransitDriverStartsEvent event)
void handleEvent(final PersonLeavesVehicleEvent event)