Skip to content

Commit

Permalink
Merge pull request #3046 from matsim-org/coordWeightingInPTSkimCalcul…
Browse files Browse the repository at this point in the history
…ation

Coord weighting in pt skim calculation
  • Loading branch information
jfbischoff authored Jan 11, 2024
2 parents 56c3601 + 3cb51be commit ca20784
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ public final PTSkimMatrices.PtIndicators<String> calculatePTMatrices(String netw

log.info("calc PT matrices for " + Time.writeTime(startTime) + " - " + Time.writeTime(endTime));
PTSkimMatrices.PtIndicators<String> matrices = PTSkimMatrices.calculateSkimMatrices(
raptorData, this.coordsPerZone, startTime, endTime, 120, raptorParameters, this.numberOfThreads, trainDetector);
raptorData, this.coordsPerZone, startTime, endTime, 120, raptorParameters, this.numberOfThreads, trainDetector, new PTSkimMatrices.CoordAggregator() {
});
return matrices;

}
Expand All @@ -511,15 +512,6 @@ private String findZone(Coord coord, SpatialIndex zonesQt, String zonesIdAttribu
return null;
}

private static class WeightedCoord {

Coord coord;
double weight;

private WeightedCoord(Coord coord, double weight) {
this.coord = coord;
this.weight = weight;
}
}
public record WeightedCoord(Coord coord, double weight){}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,10 @@
import org.matsim.pt.transitSchedule.api.TransitRoute;
import org.matsim.pt.transitSchedule.api.TransitStopFacility;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

/**
* Calculates zone-to-zone matrices containing a number of performance indicators related to public transport.
Expand Down Expand Up @@ -76,7 +72,7 @@ private PTSkimMatrices() {
}

public static <T> PTSkimMatrices.PtIndicators<T> calculateSkimMatrices(SwissRailRaptorData raptorData, Map<T, Coord[]> coordsPerZone, double minDepartureTime, double maxDepartureTime,
double stepSize_seconds, RaptorParameters parameters, int numberOfThreads, BiPredicate<TransitLine, TransitRoute> trainDetector) {
double stepSize_seconds, RaptorParameters parameters, int numberOfThreads, BiPredicate<TransitLine, TransitRoute> trainDetector, CoordAggregator coordAggregator) {
// prepare calculation
Set<T> zoneIds = coordsPerZone.keySet();
PtIndicators<T> pti = new PtIndicators<>(zoneIds);
Expand All @@ -89,7 +85,7 @@ public static <T> PTSkimMatrices.PtIndicators<T> calculateSkimMatrices(SwissRail
Thread[] threads = new Thread[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
SwissRailRaptor raptor = new SwissRailRaptor.Builder(raptorData, config).build();
RowWorker<T> worker = new RowWorker<>(originZones, zoneIds, coordsPerZone, pti, raptor, parameters, minDepartureTime, maxDepartureTime, stepSize_seconds, counter, trainDetector);
RowWorker<T> worker = new RowWorker<>(originZones, zoneIds, coordsPerZone, pti, raptor, parameters, minDepartureTime, maxDepartureTime, stepSize_seconds, counter, trainDetector, coordAggregator);
threads[i] = new Thread(worker, "PT-FrequencyMatrix-" + Time.writeTime(minDepartureTime) + "-" + Time.writeTime(maxDepartureTime) + "-" + i);
threads[i].start();
}
Expand Down Expand Up @@ -148,9 +144,10 @@ static class RowWorker<T> implements Runnable {
private final double stepSize;
private final Counter counter;
private final BiPredicate<TransitLine, TransitRoute> trainDetector;
private final CoordAggregator coordAggregator;

RowWorker(ConcurrentLinkedQueue<T> originZones, Set<T> destinationZones, Map<T, Coord[]> coordsPerZone, PtIndicators<T> pti, SwissRailRaptor raptor, RaptorParameters parameters,
double minDepartureTime, double maxDepartureTime, double stepSize, Counter counter, BiPredicate<TransitLine, TransitRoute> trainDetector) {
RowWorker(ConcurrentLinkedQueue<T> originZones, Set<T> destinationZones, Map<T, Coord[]> coordsPerZone, PtIndicators<T> pti, SwissRailRaptor raptor, RaptorParameters parameters,
double minDepartureTime, double maxDepartureTime, double stepSize, Counter counter, BiPredicate<TransitLine, TransitRoute> trainDetector, CoordAggregator coordAggregator) {
this.originZones = originZones;
this.destinationZones = destinationZones;
this.coordsPerZone = coordsPerZone;
Expand All @@ -162,6 +159,7 @@ static class RowWorker<T> implements Runnable {
this.stepSize = stepSize;
this.counter = counter;
this.trainDetector = trainDetector;
this.coordAggregator = coordAggregator;
}

private static Collection<TransitStopFacility> findStopCandidates(Coord coord, SwissRailRaptor raptor, RaptorParameters parameters) {
Expand All @@ -185,14 +183,15 @@ public void run() {
this.counter.incCounter();
Coord[] fromCoords = this.coordsPerZone.get(fromZoneId);
if (fromCoords != null) {
for (Coord fromCoord : fromCoords) {
calcForRow(fromZoneId, fromCoord);
var weightedRelevantFromCoords = coordAggregator.aggregateCoords(fromCoords);
for (var fromCoord : weightedRelevantFromCoords) {
calcForRow(fromZoneId, fromCoord.coord(),fromCoord.weight());
}
}
}
}

private void calcForRow(T fromZoneId, Coord fromCoord) {
private void calcForRow(T fromZoneId, Coord fromCoord, double fromCoordWeight) {
double walkSpeed = this.parameters.getBeelineWalkSpeed();

Collection<TransitStopFacility> fromStops = findStopCandidates(fromCoord, this.raptor, this.parameters);
Expand All @@ -216,13 +215,13 @@ private void calcForRow(T fromZoneId, Coord fromCoord) {
Coord[] toCoords = this.coordsPerZone.get(toZoneId);
if (toCoords != null) {
for (Coord toCoord : toCoords) {
calcForOD(fromZoneId, toZoneId, toCoord, accessTimes, trees);
calcForOD(fromZoneId, toZoneId, toCoord, accessTimes, trees, (float) fromCoordWeight);
}
}
}
}

private void calcForOD(T fromZoneId, T toZoneId, Coord toCoord, Map<Id<TransitStopFacility>, Double> accessTimes, List<Map<Id<TransitStopFacility>, TravelInfo>> trees) {
private void calcForOD(T fromZoneId, T toZoneId, Coord toCoord, Map<Id<TransitStopFacility>, Double> accessTimes, List<Map<Id<TransitStopFacility>, TravelInfo>> trees, float fromCoordWeight) {
double walkSpeed = this.parameters.getBeelineWalkSpeed();

Collection<TransitStopFacility> toStops = findStopCandidates(toCoord, this.raptor, this.parameters);
Expand Down Expand Up @@ -297,15 +296,15 @@ private void calcForOD(T fromZoneId, T toZoneId, Coord toCoord, Map<Id<TransitSt
float trainShareByTravelTime = (float) (trainInVehTime / totalInVehTime);
float trainShareByDistance = (float) (trainDistance / totalDistance);

this.pti.accessTimeMatrix.add(fromZoneId, toZoneId, accessTime);
this.pti.egressTimeMatrix.add(fromZoneId, toZoneId, egressTime);
this.pti.transferCountMatrix.add(fromZoneId, toZoneId, transferCount);
this.pti.travelTimeMatrix.add(fromZoneId, toZoneId, travelTime);
this.pti.distanceMatrix.add(fromZoneId, toZoneId, (float) totalDistance);
this.pti.trainDistanceShareMatrix.add(fromZoneId, toZoneId, trainShareByDistance);
this.pti.trainTravelTimeShareMatrix.add(fromZoneId, toZoneId, trainShareByTravelTime);
this.pti.accessTimeMatrix.add(fromZoneId, toZoneId, accessTime*fromCoordWeight);
this.pti.egressTimeMatrix.add(fromZoneId, toZoneId, egressTime*fromCoordWeight);
this.pti.transferCountMatrix.add(fromZoneId, toZoneId, transferCount*fromCoordWeight);
this.pti.travelTimeMatrix.add(fromZoneId, toZoneId, travelTime*fromCoordWeight);
this.pti.distanceMatrix.add(fromZoneId, toZoneId, (float) totalDistance*fromCoordWeight);
this.pti.trainDistanceShareMatrix.add(fromZoneId, toZoneId, trainShareByDistance*fromCoordWeight);
this.pti.trainTravelTimeShareMatrix.add(fromZoneId, toZoneId, trainShareByTravelTime*fromCoordWeight);

this.pti.dataCountMatrix.add(fromZoneId, toZoneId, 1);
this.pti.dataCountMatrix.add(fromZoneId, toZoneId, fromCoordWeight);
}

private List<ODConnection> buildODConnections(List<Map<Id<TransitStopFacility>, TravelInfo>> trees, Map<Id<TransitStopFacility>, Double> accessTimes,
Expand Down Expand Up @@ -359,4 +358,10 @@ public static class PtIndicators<T> {
}
}

public interface CoordAggregator{
default List<CalculateSkimMatrices.WeightedCoord> aggregateCoords(Coord[] coords){
return Arrays.stream(coords).map(coord -> new CalculateSkimMatrices.WeightedCoord(coord,1.0)).collect(Collectors.toList());
}
}

}

0 comments on commit ca20784

Please sign in to comment.