21 package org.matsim.analysis;
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.AbstractMap;
29 import java.util.TreeMap;
30 import java.util.TreeSet;
31 import java.util.stream.Collectors;
32 import jakarta.inject.Inject;
33 import org.apache.commons.csv.CSVFormat;
34 import org.apache.commons.csv.CSVPrinter;
35 import org.apache.logging.log4j.LogManager;
36 import org.jfree.chart.axis.CategoryLabelPositions;
51 private final Map<Integer,Map<String,TravelTimeAndWaitTime>>
phtPerIteration =
new TreeMap<>();
54 private final static String
FILENAME =
"ph_modestats";
67 Map<String,TravelTimeAndWaitTime> phtbyMode = map.
values()
69 .flatMap(plan -> plan.getPlanElements().stream())
71 .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue, TravelTimeAndWaitTime::sum));
72 phtPerIteration.
put(iteration,phtbyMode);
76 if (pe instanceof
Leg leg) {
77 double travelTime = 0.0;
78 double waitTime = 0.0;
79 if (leg.getRoute()!=null) {
80 travelTime = leg.getRoute().getTravelTime().seconds();
81 double enterVehicleTime = Double.NaN;
84 enterVehicleTime = (Double) attr;
86 waitTime = enterVehicleTime - leg.getDepartureTime().seconds();
87 if (!Double.isFinite(waitTime)) {waitTime = 0.0;}
88 if (waitTime >= 0.0) {
89 travelTime -= waitTime;
91 throw new RuntimeException(
"negative wait time" + enterVehicleTime +
" " + leg.getDepartureTime()
95 if (Double.isNaN(travelTime)) {travelTime = 0.0; }
101 double duration = act.getEndTime().orElse(0) - act.getStartTime().orElse(0);
109 void writeOutput(
boolean writePng) {
112 new Thread(this::writePng).start();
119 BufferedWriter writer = Files.newBufferedWriter(Paths.get(controllerIO.
getOutputFilename(FILENAME +
".csv")));
120 CSVPrinter csvPrinter =
new CSVPrinter(writer, CSVFormat.Builder.create().setDelimiter((this.delimiter.charAt(0))).build());
124 }
catch (IOException e) {
125 LogManager.getLogger(getClass()).error(
"Could not write PH Modestats.");
129 private void writeValues(CSVPrinter csvPrinter, TreeSet<String> allModes)
throws IOException {
130 for (Map.Entry<Integer,Map<String,TravelTimeAndWaitTime>> e : phtPerIteration.entrySet()){
131 csvPrinter.print(e.getKey());
132 for (String mode : allModes){
134 csvPrinter.print((
int) Math.round(travelTimeAndWaitTime.travelTime / 3600.0));
135 csvPrinter.print((
int) Math.round(travelTimeAndWaitTime.waitTime / 3600.0));
137 csvPrinter.println();
141 private static void writeHeader(CSVPrinter csvPrinter, TreeSet<String> allModes)
throws IOException {
142 csvPrinter.print(
"Iteration");
143 for (String mode: allModes) {
144 csvPrinter.print(mode + TRAVEL_TIME_SUFFIX);
145 csvPrinter.print(mode + WAIT_TIME_SUFFIX);
147 csvPrinter.println();
152 String[] categories =
new String[phtPerIteration.size()];
154 for (Integer it : phtPerIteration.keySet()){
155 categories[i++] = it.toString();
160 chart.
getChart().getCategoryPlot().getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
162 for (String mode : allModes){
163 double[] valueTravelTime = phtPerIteration.values().stream()
166 chart.
addSeries(mode + TRAVEL_TIME_SUFFIX, valueTravelTime);
167 double[] valueWaitTime = phtPerIteration.values().stream()
170 chart.
addSeries(mode + WAIT_TIME_SUFFIX, valueWaitTime);
177 return this.phtPerIteration.values()
179 .flatMap(i -> i.keySet().stream())
180 .collect(Collectors.toCollection(TreeSet::new));
185 return new TravelTimeAndWaitTime(object1.travelTime + object2.travelTime, object1.waitTime + object2.waitTime);
static final String TRAVEL_TIME_SUFFIX
static AbstractMap.SimpleEntry< String, TravelTimeAndWaitTime > mapPlanElementToEntry(PlanElement pe)
static final String ENTER_VEHICLE_TIME_ATTRIBUTE_NAME
final OutputDirectoryHierarchy controllerIO
void addSeries(final String title, final double[] values)
static final String STAGE_ACTIVITY
static final boolean isStageActivity(final String activityType)
static final String FILENAME
void saveAsPng(final String filename, final int width, final int height)
void writeValues(CSVPrinter csvPrinter, TreeSet< String > allModes)
record TravelTimeAndWaitTime(double travelTime, double waitTime)
final Map< Integer, Map< String, TravelTimeAndWaitTime > > phtPerIteration
static void writeHeader(CSVPrinter csvPrinter, TreeSet< String > allModes)
V put(Id< T > key, V value)
final String getOutputFilename(final String filename)
static final String WAIT_TIME_SUFFIX
TreeSet< String > getAllModes()
String getDefaultDelimiter()