Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sharing metrics tracker #3056

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading