improvements to regular trip detection algorithm -> next step: write tests that verify approach
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-12 16:32:14 +01:00
parent 474813b2b9
commit 23da358686
@@ -33,7 +33,8 @@ public class IterativeClusterer implements Detector {
iterationResults.add(aggregateToSupportedDetections(iterationMap, trips, properties.getMinTripsForRegularity()));
}
return pickBestIteration(iterationResults);
List<DetectedRegularTripDTO> bestIteration = pickBestIteration(iterationResults);
return deduplicateOverlappingDetections(bestIteration);
}
/**
@@ -49,6 +50,37 @@ public class IterativeClusterer implements Detector {
.orElse(List.of());
}
/**
* Removes overlapping detections by greedily selecting the best non-overlapping set.
* Detections with more supporters are prioritized.
*/
private List<DetectedRegularTripDTO> deduplicateOverlappingDetections(List<DetectedRegularTripDTO> detections) {
// Sort by supporter count (descending)
List<DetectedRegularTripDTO> sorted = new ArrayList<>(detections);
sorted.sort(Comparator.comparingInt(d -> -d.getSupportingTrips().size()));
List<DetectedRegularTripDTO> result = new ArrayList<>();
Set<Long> usedTripIds = new HashSet<>();
for (DetectedRegularTripDTO detection : sorted) {
// Get all trip IDs involved in this detection
Set<Long> detectionTripIds = new HashSet<>();
for (RegularTripDetectionDTO trip : detection.getSupportingTrips()) {
detectionTripIds.add(trip.getTripId());
}
// Check if any trips are already used
boolean hasOverlap = detectionTripIds.stream().anyMatch(usedTripIds::contains);
if (!hasOverlap) {
result.add(detection);
usedTripIds.addAll(detectionTripIds);
}
}
return result;
}
/**
* Takes a map with potential detected regular trips and checks against a list of trips which are supported
* If a trip supports another detected regular trips, its own regular trip is removed.
@@ -60,14 +92,12 @@ public class IterativeClusterer implements Detector {
) {
// We need to dupe deletions with this list since we cant modify other list entries while iterating over it
Set<Long> deletedDetections = new HashSet<>();
Set<Long> deletedTrips = new HashSet<>();
for (Map.Entry<Long, DetectedRegularTripDTO> entry : potentialDetectedRegularTrips.entrySet()) {
if (deletedDetections.contains(entry.getKey())) continue;
// Check for all other trips if they support this detected regular trip
for (RegularTripDetectionDTO trip : trips) {
if (deletedTrips.contains(trip.getTripId())) continue;
// prevent checking against trip the detected regular trip stems from
if (entry.getKey() == trip.getTripId()) continue;
@@ -75,10 +105,6 @@ public class IterativeClusterer implements Detector {
entry.getValue().getSupportingTrips().add(trip);
// remove trip's own potential regular trip since its now a supporter
deletedDetections.add(trip.getTripId());
// block it from supporting more than one trips
deletedTrips.add(trip.getTripId());
// if a detectedtrip gets its first supporter, it should not be used to support other trips
deletedTrips.add(entry.getKey());
}
}