Skip to content

Commit

Permalink
Merge pull request #2955 from matsim-org/faster-mode-analysis
Browse files Browse the repository at this point in the history
Implement createGraphsInterval
  • Loading branch information
paulheinr authored Dec 7, 2023
2 parents a8cccff + b489fd4 commit fcd4f03
Show file tree
Hide file tree
Showing 31 changed files with 893 additions and 679 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public void notifyIterationEnds(IterationEndsEvent event) {
}
}

if (this.matsimServices.getConfig().controller().isCreateGraphs()) {
int createGraphsInterval = this.matsimServices.getConfig().controller().getCreateGraphsInterval();
if (createGraphsInterval > 0 && event.getIteration() % createGraphsInterval == 0) {
DefaultTableXYDataset xyDataset = createXYDataset(times, profiles);
generateImage(xyDataset, TimeProfileCharts.ChartType.Line);
generateImage(xyDataset, TimeProfileCharts.ChartType.StackedArea);
Expand All @@ -103,7 +104,7 @@ private DefaultTableXYDataset createXYDataset(double[] times, Map<String, double
profiles.forEach((name, profile) -> {
XYSeries series = new XYSeries(name, true, false);
for (int i = 0; i < times.length; i++) {
series.add((double)times[i] / 3600, profile[i]);
series.add((double) times[i] / 3600, profile[i]);
}
seriesList.add(series);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ public void notifyMobsimBeforeCleanup(@SuppressWarnings("rawtypes") MobsimBefore
}
}

for (ChartType t : chartTypes) {
generateImage(header, t);
int createGraphsInterval = matsimServices.getConfig().controller().getCreateGraphsInterval();
boolean createGraphs = createGraphsInterval >0 && matsimServices.getIterationNumber() % createGraphsInterval == 0;

if(createGraphs){
for (ChartType t : chartTypes) {
generateImage(header, t);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public ShiftAnalysisControlerListener(Config config, DrtConfigGroup drtConfigGro

@Override
public void notifyIterationEnds(IterationEndsEvent event) {
boolean createGraphs = event.getServices().getConfig().controller().isCreateGraphs();
int createGraphsInterval = event.getServices().getConfig().controller().getCreateGraphsInterval();
boolean createGraphs = createGraphsInterval >0 && event.getIteration() % createGraphsInterval == 0;

writeAndPlotShiftDurationComparison(shiftDurationXY.getShift2plannedVsActualDuration(),
writeAndPlotShiftDurationComparison(shiftDurationXY.getShift2plannedVsActualDuration(),
filename(event, "shiftDurationComparison", ".png"),
filename(event, "shiftDurationComparison", ".csv"),
createGraphs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public void notifyIterationStarts(final IterationStartsEvent event) {
public void notifyIterationEnds(final IterationEndsEvent event) {
this.shiftHistogram.write(matsimServices.getControlerIO().getIterationFilename(event.getIteration(), drtConfigGroup.getMode() + "_" + "shiftHistogram.txt"));
this.printStats();
boolean createGraphs = event.getServices().getConfig().controller().isCreateGraphs();
int createGraphsInterval = event.getServices().getConfig().controller().getCreateGraphsInterval();
boolean createGraphs = createGraphsInterval >0 && event.getIteration() % createGraphsInterval == 0;

if (createGraphs) {
ShiftHistogramChart.writeGraphic(this.shiftHistogram, matsimServices.getControlerIO().getIterationFilename(event.getIteration(),drtConfigGroup.getMode() + "_" + "shiftHistogram.png"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public ShiftEfficiencyAnalysisControlerListener(DrtConfigGroup drtConfigGroup,

@Override
public void notifyIterationEnds(IterationEndsEvent event) {
boolean createGraphs = event.getServices().getConfig().controller().isCreateGraphs();
int createGraphsInterval = event.getServices().getConfig().controller().getCreateGraphsInterval();
boolean createGraphs = createGraphsInterval >0 && event.getIteration() % createGraphsInterval == 0;

writeAndPlotShiftEfficiency(
writeAndPlotShiftEfficiency(
shiftEfficiencyTracker.getCurrentRecord().getRevenueByShift(),
shiftEfficiencyTracker.getCurrentRecord().getRequestsByShift(),
shiftEfficiencyTracker.getCurrentRecord().getFinishedShifts(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ private static List<DrtLeg> newDrtLegs(EventSequence sequence, Function<Id<Link>

@Override
public void notifyIterationEnds(IterationEndsEvent event) {
boolean createGraphs = event.getServices().getConfig().controller().isCreateGraphs();
int createGraphsInterval = event.getServices().getConfig().controller().getCreateGraphsInterval();
boolean createGraphs = createGraphsInterval >0 && event.getIteration() % createGraphsInterval == 0;

writeAndPlotWaitTimeEstimateComparison(drtEventSequenceCollector.getPerformedRequestSequences().values(),
filename(event, "waitTimeComparison", ".png"), filename(event, "waitTimeComparison", ".csv"), createGraphs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public MobsimListener get() {
};

var collector = new TimeProfileCollector(header, calc, 300, "charger_occupancy_time_profiles", matsimServices);
if (matsimServices.getConfig().controller().isCreateGraphs()) {
if (matsimServices.getConfig().controller().getCreateGraphsInterval()>0) {
collector.setChartTypes(ChartType.Line, ChartType.StackedArea);
} else {
collector.setChartTypes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@
public class CourtesyHistogramListener implements IterationEndsListener, IterationStartsListener {

private final CourtesyHistogram histogram;
private final boolean outputGraph;

private static final Logger log = LogManager.getLogger(CourtesyHistogramListener.class);
private final OutputDirectoryHierarchy controlerIO;

@Inject
CourtesyHistogramListener(Config config, CourtesyHistogram histogram, OutputDirectoryHierarchy controlerIO) {
CourtesyHistogramListener(CourtesyHistogram histogram, OutputDirectoryHierarchy controlerIO) {
this.controlerIO = controlerIO;
this.histogram = histogram;
this.outputGraph = config.controller().isCreateGraphs();
}

@Override
Expand All @@ -70,7 +68,10 @@ public void notifyIterationStarts(final IterationStartsEvent event) {
@Override
public void notifyIterationEnds(final IterationEndsEvent event) {
this.histogram.write(controlerIO.getIterationFilename(event.getIteration(), "courtesyHistogram.txt"));
if (this.outputGraph) {

int createGraphsInterval = event.getServices().getConfig().controller().getCreateGraphsInterval();
boolean createGraphs = createGraphsInterval >0 && event.getIteration() % createGraphsInterval == 0;
if (createGraphs) {
for ( String type : histogram.getDataFrames().keySet() ) {
writeGraphic(
this.histogram,
Expand Down
120 changes: 120 additions & 0 deletions matsim/src/main/java/org/matsim/analysis/ActivityWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.matsim.analysis;

import jakarta.inject.Inject;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.core.config.Config;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.scoring.ExperiencedPlansService;
import org.matsim.core.utils.io.IOUtils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ActivityWriter {

@Inject
private Config config;

@Inject
private ExperiencedPlansService experiencedPlansService;

@Inject
private OutputDirectoryHierarchy outputDirectoryHierarchy;

private static final Logger log = LogManager.getLogger(ActivityWriter.class);

void writeCsv(int iteration) {
log.info("Writing all Activities to " + Controler.DefaultFiles.activitiescsv);

List<String> attributes = prepareAttributes();
String[] header = prepareHeader(attributes);

try{
BufferedWriter bufferedWriter = IOUtils.getBufferedWriter(outputDirectoryHierarchy.getIterationFilename(iteration, Controler.DefaultFiles.activitiescsv));
CSVPrinter csvPrinter = new CSVPrinter(bufferedWriter, CSVFormat.Builder.create()
.setDelimiter(config.global().getDefaultDelimiter().charAt(0))
.setHeader(header).build());

for (Map.Entry<Id<Person>, Plan> e : experiencedPlansService.getExperiencedPlans().entrySet()) {
writeActivitiesPerPerson(e.getKey(), e.getValue(), attributes, csvPrinter);
}

csvPrinter.close();
} catch (IOException e) {
e.printStackTrace();
}

log.info("...done");
}

private static void writeActivitiesPerPerson(Id<Person> personId, Plan plan, List<String> attributes, CSVPrinter csvPrinter) throws IOException {
int i = 0;
for (Activity act : TripStructureUtils.getActivities(plan, TripStructureUtils.StageActivityHandling.ExcludeStageActivities)) {

List<Object> line = new ArrayList<>();
int id = ++i;
line.add(personId);
line.add(id);
line.add(personId.toString() + "_" + id);
line.add(act.getType());

line.add(act.getStartTime().isDefined() ? act.getStartTime().seconds() : "");
line.add(act.getEndTime().isDefined() ? act.getEndTime().seconds() : "");
line.add(act.getMaximumDuration().isDefined() ? act.getMaximumDuration().seconds() : "");
line.add(act.getLinkId() != null ? act.getLinkId() : "");
line.add(act.getFacilityId() != null ? act.getFacilityId(): "");

if (act.getCoord() != null) {
line.add(act.getCoord().getX());
line.add(act.getCoord().getY());
} else {
line.add("");
line.add("");
}

for (String attribute : attributes) {
Object value = plan.getAttributes().getAttribute(attribute);
String result = value != null ? String.valueOf(value) : "";
line.add(result);
}

csvPrinter.printRecord(line);
}
}

private List<String> prepareAttributes() {
return experiencedPlansService.getExperiencedPlans().values().stream()
.flatMap(p -> TripStructureUtils.getActivities(p, TripStructureUtils.StageActivityHandling.ExcludeStageActivities).stream())
.flatMap(act -> act.getAttributes().getAsMap().keySet().stream())
.sorted().distinct().toList();
}

private static String[] prepareHeader(List<String> attributes) {
List<String> header = new ArrayList<>();
header.add("person");
header.add("activity_number");
header.add("activity_id");
header.add("activity_type");
header.add("start_time");
header.add("end_time");
header.add("maximum_duration");
header.add("link_id");
header.add("facility_id");
header.add("coord_x");
header.add("coord_y");
header.addAll(attributes);
return header.toArray(String[]::new);
}
}
Loading

0 comments on commit fcd4f03

Please sign in to comment.