MATSIM
CalcLegTimes.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CalcLegTimes.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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.analysis;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.matsim.api.core.v01.IdMap;
36 import org.matsim.core.utils.io.IOUtils;
37 import org.matsim.core.utils.misc.Time;
38 
39 import jakarta.inject.Inject;
40 import java.io.BufferedWriter;
41 import java.io.IOException;
42 import java.io.UncheckedIOException;
43 import java.util.Map;
44 import java.util.TreeMap;
45 
56 
57  private final static Logger log = LogManager.getLogger(CalcLegTimes.class);
58 
59  private static final int SLOT_SIZE = 300; // 5-min slots
60  private static final int MAXINDEX = 12; // slots 0..11 are regular slots, slot 12 is anything above
61 
63  private final IdMap<Person, Double> agentArrivals = new IdMap<>(Person.class);
64  private final Map<String, int[]> legStats = new TreeMap<>();
66  private double sumLegDurations = 0;
67  private int sumLegs = 0;
68 
69  @Inject
70  CalcLegTimes(EventsManager eventsManager) {
71  eventsManager.addHandler(this);
72  }
73 
74  public CalcLegTimes() {
75 
76  }
77 
78  @Override
79  public void handleEvent(ActivityEndEvent event) {
80  this.previousActivityTypes.put(event.getPersonId(), event.getActType());
81  }
82 
83  @Override
84  public void handleEvent(final PersonDepartureEvent event) {
85  this.agentDepartures.put(event.getPersonId(), event.getTime());
86  }
87 
88  @Override
89  public void handleEvent(final PersonArrivalEvent event) {
90  this.agentArrivals.put(event.getPersonId(), event.getTime());
91  }
92 
93 
94  @Override
95  public void handleEvent(ActivityStartEvent event) {
96  Double depTime = this.agentDepartures.remove(event.getPersonId());
97  Double arrTime = this.agentArrivals.remove(event.getPersonId());
98  if (depTime != null) {
99  double travTime = arrTime - depTime;
100  String fromActType = previousActivityTypes.remove(event.getPersonId());
101  String toActType = event.getActType();
102  String legType = fromActType + "---" + toActType;
103  int[] stats = this.legStats.get(legType);
104  if (stats == null) {
105  stats = new int[MAXINDEX + 1];
106  for (int i = 0; i <= MAXINDEX; i++) {
107  stats[i] = 0;
108  }
109  this.legStats.put(legType, stats);
110  }
111  stats[getTimeslotIndex(travTime)]++;
112 
113  this.sumLegDurations += travTime;
114  this.sumLegs++;
115  }
116  }
117 
118 
119  @Override
120  public void reset(final int iteration) {
121  this.previousActivityTypes.clear();
122  this.agentDepartures.clear();
123  this.legStats.clear();
124  this.sumLegDurations = 0;
125  this.sumLegs = 0;
126  }
127 
128  public Map<String, int[]> getLegStats() {
129  return this.legStats;
130  }
131 
132  public static int getTimeslotIndex(final double time_s) {
133  int idx = (int) (time_s / SLOT_SIZE);
134  if (idx > MAXINDEX) idx = MAXINDEX;
135  return idx;
136  }
137 
138  public double getAverageLegDuration() {
139  return (this.sumLegDurations / this.sumLegs);
140  }
141 
142  public void writeStats(final String filename) {
143  try (BufferedWriter legStatsFile = IOUtils.getBufferedWriter(filename)) {
144  writeStats(legStatsFile);
145  } catch (IOException e) {
146  log.error(e);
147  }
148  }
149 
150  public void writeStats(final java.io.Writer out) throws UncheckedIOException {
151  try {
152  boolean first = true;
153  for (Map.Entry<String, int[]> entry : this.legStats.entrySet()) {
154  String key = entry.getKey();
155  int[] counts = entry.getValue();
156  if (first) {
157  first = false;
158  out.write("pattern");
159  for (int i = 0; i < counts.length; i++) {
160  out.write("\t" + (i * SLOT_SIZE / 60) + "+");
161  }
162  out.write("\n");
163  }
164  out.write(key);
165  for (int count : counts) {
166  out.write("\t" + count);
167  }
168  out.write("\n");
169  }
170  out.write("\n");
171  if (this.sumLegs == 0) {
172  out.write("average legs duration: no legs!");
173  } else {
174  out.write("average leg duration: "
175  + (this.sumLegDurations / this.sumLegs)
176  + " seconds = "
177  + Time.writeTime(((int) (this.sumLegDurations / this.sumLegs))));
178  }
179  out.write("\n");
180  } catch (IOException e) {
181  throw new UncheckedIOException(e);
182  } finally {
183  try {
184  out.flush();
185  } catch (IOException e) {
186  log.error(e);
187  }
188  }
189  }
190 }
final IdMap< Person, Double > agentDepartures
void handleEvent(ActivityStartEvent event)
static int getTimeslotIndex(final double time_s)
void handleEvent(final PersonDepartureEvent event)
void writeStats(final String filename)
void addHandler(final EventHandler handler)
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390
final IdMap< Person, String > previousActivityTypes
void writeStats(final java.io.Writer out)
void handleEvent(ActivityEndEvent event)
void handleEvent(final PersonArrivalEvent event)
static final String writeTime(final double seconds, final String timeformat)
Definition: Time.java:80
Map< String, int[]> getLegStats()
final IdMap< Person, Double > agentArrivals
void reset(final int iteration)
V put(Id< T > key, V value)
Definition: IdMap.java:141
final Map< String, int[]> legStats