MATSIM
OccupancyAnalyzer.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CalcTrRouteStats.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2011 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.counts;
22 
23 import org.apache.logging.log4j.Level;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.matsim.api.core.v01.Id;
27 import org.matsim.api.core.v01.IdMap;
40 import org.matsim.vehicles.Vehicle;
41 
42 import java.util.HashMap;
43 import java.util.HashSet;
44 import java.util.Map;
45 import java.util.Set;
46 import java.util.TreeSet;
47 
54 
55  private static final Logger log = LogManager.getLogger(OccupancyAnalyzer.class);
56 
57  private final int timeBinSize, maxSlotIndex;
58  private final double maxTime;
60  private Map<Id<TransitStopFacility>, int[]> boards, alights, occupancies;
61 
63 // private final IdMap<Vehicle, Id<TransitStopFacility>> vehStops = new IdMap<>(Vehicle.class, Id.class);
64  private final Map<Id<Vehicle>, Id<TransitStopFacility>> vehStops = new HashMap<>();
66  private final Map<Id<Vehicle>, Integer> vehPassengers = new HashMap<>();
67  private StringBuffer occupancyRecord = new StringBuffer("time\tvehId\tStopId\tno.ofPassengersInVeh\n");
68  private final Set<Id<Person>> transitDrivers = new HashSet<>();
69  private final Set<Id<Vehicle>> transitVehicles = new HashSet<>();
70 
71  public OccupancyAnalyzer(final int timeBinSize, final double maxTime) {
72  this.timeBinSize = timeBinSize;
73  this.maxTime = maxTime;
74  this.maxSlotIndex = ((int) this.maxTime) / this.timeBinSize + 1;
75  this.boards = new IdMap<>(TransitStopFacility.class);
76  this.alights = new IdMap<>(TransitStopFacility.class);
77  this.occupancies = new IdMap<>(TransitStopFacility.class);
78  }
79 
80  public void setBoards(Map<Id<TransitStopFacility>, int[]> boards) {
81  this.boards = boards;
82  }
83 
84  public void setAlights(Map<Id<TransitStopFacility>, int[]> alights) {
85  this.alights = alights;
86  }
87 
88  public void setOccupancies(Map<Id<TransitStopFacility>, int[]> occupancies) {
89  this.occupancies = occupancies;
90  }
91 
92  public int getTimeSlotIndex(final double time) {
93  if (time > this.maxTime) {
94  return this.maxSlotIndex;
95  }
96  return ((int) time / this.timeBinSize);
97  }
98 
99  @Override
100  public void reset(int iteration) {
101  this.boards.clear();
102  this.alights.clear();
103  this.occupancies.clear();
104  this.vehStops.clear();
105  this.vehPassengers.clear();
106  this.occupancyRecord = new StringBuffer("time\tvehId\tStopId\tno.ofPassengersInVeh\n");
107  this.transitDrivers.clear();
108  this.transitVehicles.clear();
109  }
110 
111  @Override
113  this.transitDrivers.add(event.getDriverId());
114  this.transitVehicles.add(event.getVehicleId());
115  }
116 
117  @Override
119  if (this.transitDrivers.contains(event.getPersonId()) || !this.transitVehicles.contains(event.getVehicleId())) {
120  return; // ignore transit drivers or persons entering non-transit vehicles
121  }
122 
123  Id<Vehicle> vehId = event.getVehicleId();
124  Id<TransitStopFacility> stopId = this.vehStops.get(vehId);
125  double time = event.getTime();
126  // --------------------------getOns---------------------------
127  int[] getOn = this.boards.get(stopId);
128  if (getOn == null) {
129  getOn = new int[this.maxSlotIndex + 1];
130  this.boards.put(stopId, getOn);
131  }
132  getOn[getTimeSlotIndex(time)]++;
133  // ------------------------veh_passenger---------------------------
134  Integer nPassengers = this.vehPassengers.get(vehId);
135  this.vehPassengers.put(vehId, (nPassengers != null) ? (nPassengers + 1) : 1);
136  this.occupancyRecord.append("time :\t").append(time).append(" veh :\t").append(vehId).append(" has Passenger\t").append(this.vehPassengers.get(vehId)).append(" \tat stop :\t").append(stopId).append(" ENTERING PERSON :\t").append(event.getPersonId()).append("\n");
137  }
138 
139  @Override
141  if (this.transitDrivers.contains(event.getPersonId()) || !this.transitVehicles.contains(event.getVehicleId())) {
142  return; // ignore transit drivers or persons entering non-transit vehicles
143  }
144 
145  Id<Vehicle> vehId = event.getVehicleId();
146  Id<TransitStopFacility> stopId = this.vehStops.get(vehId);
147  double time = event.getTime();
148  // --------------------------getDowns---------------------------
149  int[] getDown = this.alights.get(stopId);
150  if (getDown == null) {
151  getDown = new int[this.maxSlotIndex + 1];
152  this.alights.put(stopId, getDown);
153  }
154  getDown[getTimeSlotIndex(time)]++;
155  // ------------------------veh_passenger---------------------------
156  Integer nPassengers = this.vehPassengers.get(vehId);
157  if (nPassengers == null) {
158  log.error( "tests for `null' but exception says 'negative'??? kai, oct'10 ") ;
159  throw new RuntimeException("negative passenger-No. in vehicle?");
160  }
161  this.vehPassengers.put(vehId, nPassengers - 1);
162  if (this.vehPassengers.get(vehId).intValue() == 0) {
163  this.vehPassengers.remove(vehId);
164  }
165 
166  Integer passengers = this.vehPassengers.get(vehId);
167  this.occupancyRecord.append("time :\t").append(time).append(" veh :\t").append(vehId).append(" has Passenger\t").append((passengers != null) ? passengers : 0).append("\n");
168  }
169 
170  @Override
172  Id<TransitStopFacility> stopId = event.getFacilityId();
173  this.vehStops.put(event.getVehicleId(), stopId);
174  }
175 
176  @Override
178  Id<TransitStopFacility> stopId = event.getFacilityId();
179  Id<Vehicle> vehId = event.getVehicleId();
180  this.vehStops.remove(vehId);
181  // -----------------------occupancy--------------------------------
182  int[] occupancyAtStop = this.occupancies.get(stopId);
183 
184  if (occupancyAtStop == null) {
185  // no previous departure from this stop, therefore no occupancy record yet. Create this:
186  occupancyAtStop = new int[this.maxSlotIndex + 1];
187  this.occupancies.put(stopId, occupancyAtStop);
188  }
189 
190  Integer noPassengersInVeh = this.vehPassengers.get(vehId);
191 
192  if (noPassengersInVeh != null) {
193  occupancyAtStop[this.getTimeSlotIndex(event.getTime())] += noPassengersInVeh;
194 
195  this.occupancyRecord.append(event.getTime());
196  this.occupancyRecord.append("\t");
197  this.occupancyRecord.append(vehId);
198  this.occupancyRecord.append("\t");
199  this.occupancyRecord.append(stopId);
200  this.occupancyRecord.append("\t");
201  this.occupancyRecord.append(noPassengersInVeh);
202  this.occupancyRecord.append("\n");
203  }
204  }
205 
212  public int[] getBoardVolumesForStop(final Id<TransitStopFacility> stopId) {
213  int[] values = this.boards.get(stopId);
214  if (values == null) {
215  return new int[this.maxSlotIndex + 1];
216  }
217  return values;
218  }
219 
227  int[] values = this.alights.get(stopId);
228  if (values == null) {
229  return new int[this.maxSlotIndex + 1];
230  }
231  return values;
232  }
233 
241  int[] values = this.occupancies.get(stopId);
242  if (values == null) {
243  return new int[this.maxSlotIndex + 1];
244  }
245  return values;
246  }
247 
252  public Set<Id<TransitStopFacility>> getBoardStopIds() {
253  return this.boards.keySet();
254  }
255 
260  public Set<Id<TransitStopFacility>> getAlightStopIds() {
261  return this.alights.keySet();
262  }
263 
264  public Set<Id<TransitStopFacility>> getOccupancyStopIds() {
265  return this.occupancies.keySet();
266  }
267 
272  public Set<Id<TransitStopFacility>> getAllStopIds() {
273  Set<Id<TransitStopFacility>> allStopIds = new TreeSet<>();
274  allStopIds.addAll(getBoardStopIds());
275  allStopIds.addAll(getAlightStopIds());
276  allStopIds.addAll(getOccupancyStopIds());
277  return allStopIds;
278  }
279 
280  public void write(String filename) {
281  SimpleWriter writer = new SimpleWriter(filename);
282  // write filehead
283  writer.write("stopId\t");
284  for (int i = 0; i < 24; i++) {
285  writer.write("bo" + i + "-" + (i + 1) + "\t");
286  }
287  for (int i = 0; i < 24; i++) {
288  writer.write("al" + i + "-" + (i + 1) + "\t");
289  }
290  for (int i = 0; i < 24; i++) {
291  writer.write("oc" + i + "-" + (i + 1) + "\t");
292  }
293  writer.writeln();
294  // write content
295  for (Id<TransitStopFacility> stopId : getAllStopIds()) {
296  writer.write(stopId + "\t");
297 
298  int[] board = this.boards.get(stopId);
299  if (board == null){
300  log.debug("stopId:\t" + stopId + "\thas null boards!");
301  }
302  for (int i = 0; i < 24; i++) {
303  writer.write((board != null ? board[i] : 0) + "\t");
304  }
305 
306  int[] alight = this.alights.get(stopId);
307  if (alight == null) {
308  log.debug("stopId:\t" + stopId + "\thas null alights!");
309  }
310  for (int i = 0; i < 24; i++) {
311  writer.write((alight != null ? alight[i] : 0) + "\t");
312  }
313 
314  int[] ocuppancy = this.occupancies.get(stopId);
315  if (ocuppancy == null) {
316  log.debug("stopId:\t" + stopId + "\tthere aren't passengers in Bus after the transfer!");
317  }
318  for (int i = 0; i < 24; i++) {
319  writer.write((ocuppancy != null ? ocuppancy[i] : 0) + "\t");
320  }
321  writer.writeln();
322  }
323  writer.write(this.occupancyRecord.toString());
324  writer.close();
325  }
326 }
static< T > Id< T > get(int index, final Class< T > type)
Definition: Id.java:112
void setAlights(Map< Id< TransitStopFacility >, int[]> alights)
final Map< Id< Vehicle >, Integer > vehPassengers
Map< Id< TransitStopFacility >, int[]> boards
void handleEvent(PersonEntersVehicleEvent event)
final Set< Id< Person > > transitDrivers
Set< Id< TransitStopFacility > > getAllStopIds()
void handleEvent(VehicleArrivesAtFacilityEvent event)
void setOccupancies(Map< Id< TransitStopFacility >, int[]> occupancies)
int [] getOccupancyVolumesForStop(final Id< TransitStopFacility > stopId)
OccupancyAnalyzer(final int timeBinSize, final double maxTime)
int [] getAlightVolumesForStop(final Id< TransitStopFacility > stopId)
Set< Id< TransitStopFacility > > getAlightStopIds()
Set< Id< TransitStopFacility > > getBoardStopIds()
void setBoards(Map< Id< TransitStopFacility >, int[]> boards)
final Set< Id< Vehicle > > transitVehicles
final Map< Id< Vehicle >, Id< TransitStopFacility > > vehStops
Set< Id< TransitStopFacility > > getOccupancyStopIds()
void handleEvent(VehicleDepartsAtFacilityEvent event)
void handleEvent(TransitDriverStartsEvent event)
int [] getBoardVolumesForStop(final Id< TransitStopFacility > stopId)
void handleEvent(PersonLeavesVehicleEvent event)