Skip to content

Commit

Permalink
Merge pull request #3056 from moia-oss/sharingMetricsUpdate
Browse files Browse the repository at this point in the history
Update sharing metrics tracker
  • Loading branch information
nkuehnel authored Jan 16, 2024
2 parents 52beb72 + a2679f5 commit 6b2da3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ private void writeAndPlotSharingMetrics(Map<Id<Request>, 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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<Id<DvrpVehicle>, List<Id<Request>>> map = new HashMap<>();
private final Map<Id<DvrpVehicle>, List<Id<Request>>> occupancyByVehicle = new HashMap<>();

private final Map<Id<Request>, List<Segment>> segments = new HashMap<>();

Expand All @@ -45,15 +51,23 @@ public void handleEvent(DrtRequestSubmittedEvent event) {
public void handleEvent(PassengerDroppedOffEvent event) {

List<Id<Person>> 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<Id<Request>> occupancy = map.get(event.getVehicleId());
List<Id<Request>> 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> 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());
return;
}
}

List<Segment> finishedSegments = segments.remove(event.getRequestId());

Expand Down Expand Up @@ -89,7 +103,7 @@ public void handleEvent(PassengerDroppedOffEvent event) {

@Override
public void handleEvent(PassengerPickedUpEvent event) {
List<Id<Request>> occupancy = map.computeIfAbsent(event.getVehicleId(), vehicleId -> new ArrayList<>());
List<Id<Request>> 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
Expand All @@ -100,14 +114,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();
Expand Down

0 comments on commit 6b2da3d

Please sign in to comment.