Skip to content

Commit

Permalink
Merge pull request #3581 from moia-oss/zonalRejectionRates
Browse files Browse the repository at this point in the history
DRT: add zonal rejection (rates) to output
  • Loading branch information
nkuehnel authored Nov 25, 2024
2 parents 135a241 + 6600189 commit f516d1e
Showing 1 changed file with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.matsim.contrib.drt.analysis.DrtEventSequenceCollector;
import org.matsim.contrib.drt.analysis.DrtEventSequenceCollector.EventSequence;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.dvrp.optimizer.Request;
import org.matsim.core.controler.events.IterationEndsEvent;
import org.matsim.core.controler.events.ShutdownEvent;
import org.matsim.core.controler.listener.IterationEndsListener;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void notifyIterationEnds(IterationEndsEvent event) {
}

public void write(String fileName) {
Map<Id<Zone>, DescriptiveStatistics> zoneStats = createZonalStats();
Map<Id<Zone>, ZonalStatistics> zoneStats = createZonalStats();
BufferedWriter bw = IOUtils.getBufferedWriter(fileName);
try {
DecimalFormat format = new DecimalFormat();
Expand All @@ -90,7 +91,8 @@ public void write(String fileName) {
String header = new StringJoiner(delimiter)
.add("zone").add("centerX").add("centerY").add("nRequests")
.add("sumWaitTime").add("meanWaitTime").add("min").add("max")
.add("p95").add("p90").add("p80").add("p75").add("p50").toString();
.add("p95").add("p90").add("p80").add("p75").add("p50")
.add("rejections").add("rejectionRate").toString();
bw.append(header);
// sorted output
SortedSet<Id<Zone>> zoneIdsAndOutside = new TreeSet<>(zones.getZones().keySet());
Expand All @@ -100,7 +102,8 @@ public void write(String fileName) {
Zone drtZone = zones.getZones().get(zoneId);
String centerX = drtZone != null ? String.valueOf(drtZone.getCentroid().getX()) : notAvailableString;
String centerY = drtZone != null ? String.valueOf(drtZone.getCentroid().getY()) : notAvailableString;
DescriptiveStatistics stats = zoneStats.get(zoneId);
DescriptiveStatistics stats = zoneStats.get(zoneId).waitStats;
Set<Id<Request>> rejections = zoneStats.get(zoneId).rejections;
bw.newLine();
bw.append(
new StringJoiner(delimiter)
Expand All @@ -116,7 +119,10 @@ public void write(String fileName) {
.add(String.valueOf(stats.getPercentile(90)))
.add(String.valueOf(stats.getPercentile(80)))
.add(String.valueOf(stats.getPercentile(75)))
.add(String.valueOf(stats.getPercentile(50))).toString()
.add(String.valueOf(stats.getPercentile(50)))
.add(String.valueOf(rejections.size()))
.add(String.valueOf(rejections.size() / (double) (rejections.size() + stats.getN())))
.toString()
);
}
bw.flush();
Expand All @@ -126,24 +132,33 @@ public void write(String fileName) {
}
}

private Map<Id<Zone>, DescriptiveStatistics> createZonalStats() {
Map<Id<Zone>, DescriptiveStatistics> zoneStats = new IdMap<>(Zone.class);
record ZonalStatistics(DescriptiveStatistics waitStats, Set<Id<Request>> rejections){}

private Map<Id<Zone>, ZonalStatistics> createZonalStats() {
Map<Id<Zone>, ZonalStatistics> zoneStats = new IdMap<>(Zone.class);
// prepare stats for all zones
for (Id<Zone> zoneId : zones.getZones().keySet()) {
zoneStats.put(zoneId, new DescriptiveStatistics());
zoneStats.put(zoneId, new ZonalStatistics(new DescriptiveStatistics(), new HashSet<>()));
}
zoneStats.put(zoneIdForOutsideOfZonalSystem, new DescriptiveStatistics());
zoneStats.put(zoneIdForOutsideOfZonalSystem, new ZonalStatistics(new DescriptiveStatistics(), new HashSet<>()));

for (EventSequence seq : requestAnalyzer.getPerformedRequestSequences().values()) {
for (Map.Entry<Id<Person>, EventSequence.PersonEvents> entry : seq.getPersonEvents().entrySet()) {
if(entry.getValue().getPickedUp().isPresent()) {
Id<Zone> zone = zones.getZoneForLinkId(seq.getSubmitted().getFromLinkId())
.map(Identifiable::getId).orElse(zoneIdForOutsideOfZonalSystem);
double waitTime = entry.getValue().getPickedUp().get() .getTime() - seq.getSubmitted().getTime();
zoneStats.get(zone).addValue(waitTime);
zoneStats.get(zone).waitStats.addValue(waitTime);
}
}
}

for (EventSequence seq : requestAnalyzer.getRejectedRequestSequences().values()) {
Id<Zone> zone = zones.getZoneForLinkId(seq.getSubmitted().getFromLinkId())
.map(Identifiable::getId).orElse(zoneIdForOutsideOfZonalSystem);
zoneStats.get(zone).rejections.add(seq.getSubmitted().getRequestId());
}

return zoneStats;
}

Expand Down Expand Up @@ -191,16 +206,19 @@ private Collection<SimpleFeature> convertGeometriesToSimpleFeatures(String targe
simpleFeatureBuilder.add("p80", Double.class);
simpleFeatureBuilder.add("p75", Double.class);
simpleFeatureBuilder.add("p50", Double.class);
simpleFeatureBuilder.add("rejections", Double.class);
simpleFeatureBuilder.add("rejectRate", Double.class);
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(simpleFeatureBuilder.buildFeatureType());

Collection<SimpleFeature> features = new ArrayList<>();

Map<Id<Zone>, DescriptiveStatistics> zoneStats = createZonalStats();
Map<Id<Zone>, ZonalStatistics> zoneStats = createZonalStats();

for (Zone zone : zones.getZones().values()) {
Object[] routeFeatureAttributes = new Object[14];
Object[] routeFeatureAttributes = new Object[16];
Geometry geometry = zone.getPreparedGeometry() != null ? zone.getPreparedGeometry().getGeometry() : null;
DescriptiveStatistics stats = zoneStats.get(zone.getId());
DescriptiveStatistics stats = zoneStats.get(zone.getId()).waitStats;
Set<Id<Request>> rejections = zoneStats.get(zone.getId()).rejections;
routeFeatureAttributes[0] = geometry;
routeFeatureAttributes[1] = zone.getId();
routeFeatureAttributes[2] = zone.getCentroid().getX();
Expand All @@ -215,6 +233,8 @@ private Collection<SimpleFeature> convertGeometriesToSimpleFeatures(String targe
routeFeatureAttributes[11] = stats.getPercentile(80);
routeFeatureAttributes[12] = stats.getPercentile(75);
routeFeatureAttributes[13] = stats.getPercentile(50);
routeFeatureAttributes[14] = rejections.size();
routeFeatureAttributes[15] = rejections.size() / (double) (rejections.size() + stats.getN());

try {
features.add(builder.buildFeature(zone.getId().toString(), routeFeatureAttributes));
Expand Down

0 comments on commit f516d1e

Please sign in to comment.