From 520172e948cc600dfe97f2efe62db296f894daff Mon Sep 17 00:00:00 2001 From: nkuehnel Date: Tue, 16 Jan 2024 15:37:07 +0100 Subject: [PATCH 1/2] make sharing metrics tracker more readable and expressive, improve plot readability --- .../SharingMetricsControlerListener.java | 4 +-- .../sharingmetrics/SharingMetricsTracker.java | 27 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsControlerListener.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsControlerListener.java index 9884fce28cc..a7a70f9e8ad 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsControlerListener.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsControlerListener.java @@ -105,8 +105,8 @@ private void writeAndPlotSharingMetrics(Map, Double> sharingFactorBy JFreeChart chartRides = ChartFactory.createBoxAndWhiskerChart("Sharing factor", "", "Factor", sharingFactorDataset, false); JFreeChart chartPooling = ChartFactory.createBoxAndWhiskerChart("Pooling rate", "", "Rate", poolingRateDataset, false); - ((BoxAndWhiskerRenderer) chartRides.getCategoryPlot().getRenderer()).setMeanVisible(true); - ((BoxAndWhiskerRenderer) chartPooling.getCategoryPlot().getRenderer()).setMeanVisible(true); + ((BoxAndWhiskerRenderer) chartRides.getCategoryPlot().getRenderer()).setMeanVisible(false); + ((BoxAndWhiskerRenderer) chartPooling.getCategoryPlot().getRenderer()).setMeanVisible(false); ChartUtils.writeChartAsPNG(new FileOutputStream(sharingFactors), chartRides, 1500, 1500); ChartUtils.writeChartAsPNG(new FileOutputStream(poolingRates), chartPooling, 1500, 1500); } diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java index 593361f0cdd..f59105b18d6 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java @@ -1,5 +1,7 @@ package org.matsim.contrib.drt.sharingmetrics; +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.Person; import org.matsim.contrib.drt.passenger.events.DrtRequestSubmittedEvent; @@ -10,6 +12,7 @@ import org.matsim.contrib.dvrp.passenger.PassengerDroppedOffEventHandler; import org.matsim.contrib.dvrp.passenger.PassengerPickedUpEvent; import org.matsim.contrib.dvrp.passenger.PassengerPickedUpEventHandler; +import org.matsim.core.gbl.Gbl; import org.matsim.core.mobsim.framework.events.MobsimBeforeCleanupEvent; import org.matsim.core.mobsim.framework.listeners.MobsimBeforeCleanupListener; @@ -20,10 +23,13 @@ */ public class SharingMetricsTracker implements DrtRequestSubmittedEventHandler, PassengerPickedUpEventHandler, PassengerDroppedOffEventHandler, MobsimBeforeCleanupListener { + private final static Logger logger = LogManager.getLogger(SharingMetricsTracker.class); + + record Segment(double start, int occupancy) { } - private final Map, List>> map = new HashMap<>(); + private final Map, List>> occupancyByVehicle = new HashMap<>(); private final Map, List> segments = new HashMap<>(); @@ -45,15 +51,22 @@ public void handleEvent(DrtRequestSubmittedEvent event) { public void handleEvent(PassengerDroppedOffEvent event) { List> passengers = knownGroups.get(event.getRequestId()); + Gbl.assertIf(passengers.contains(event.getPersonId())); passengers.remove(event.getPersonId()); if (passengers.isEmpty()) { // all passengers of the group have been dropped off knownGroups.remove(event.getRequestId()); - List> occupancy = map.get(event.getVehicleId()); + List> occupancy = occupancyByVehicle.get(event.getVehicleId()); occupancy.remove(event.getRequestId()); - occupancy.forEach(p -> segments.get(p).add(new Segment(event.getTime(), occupancy.size()))); + for (Id request : occupancy) { + if(segments.containsKey(request)) { + segments.get(request).add(new Segment(event.getTime(), occupancy.size())); + } else { + logger.warn("Missing segment info for request " + request.toString()); + } + } List finishedSegments = segments.remove(event.getRequestId()); @@ -89,7 +102,7 @@ public void handleEvent(PassengerDroppedOffEvent event) { @Override public void handleEvent(PassengerPickedUpEvent event) { - List> occupancy = map.computeIfAbsent(event.getVehicleId(), vehicleId -> new ArrayList<>()); + List> occupancy = occupancyByVehicle.computeIfAbsent(event.getVehicleId(), vehicleId -> new ArrayList<>()); if (occupancy.contains(event.getRequestId())) { if (knownGroups.get(event.getRequestId()).size() > 1) { //group request, skip for additional persons @@ -100,14 +113,16 @@ public void handleEvent(PassengerPickedUpEvent event) { } else { occupancy.add(event.getRequestId()); occupancy.forEach( - p -> segments.computeIfAbsent(p, requestId -> new ArrayList<>()).add(new Segment(event.getTime(), occupancy.size())) + request -> segments + .computeIfAbsent(request, requestId -> new ArrayList<>()) + .add(new Segment(event.getTime(), occupancy.size())) ); } } @Override public void notifyMobsimBeforeCleanup(MobsimBeforeCleanupEvent e) { - map.clear(); + occupancyByVehicle.clear(); segments.clear(); poolingRate.clear(); sharingFactors.clear(); From a2679f5c5412202974c2781c707f950b45fc66d4 Mon Sep 17 00:00:00 2001 From: nkuehnel Date: Tue, 16 Jan 2024 15:46:58 +0100 Subject: [PATCH 2/2] return early for missing sharing segment info --- .../matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java | 1 + 1 file changed, 1 insertion(+) diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java index f59105b18d6..47e8c415059 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/sharingmetrics/SharingMetricsTracker.java @@ -65,6 +65,7 @@ public void handleEvent(PassengerDroppedOffEvent event) { segments.get(request).add(new Segment(event.getTime(), occupancy.size())); } else { logger.warn("Missing segment info for request " + request.toString()); + return; } }