From 23da35868688c9c30b921b6e63fbfe7976e80466 Mon Sep 17 00:00:00 2001 From: David Neuhauss Date: Mon, 12 Jan 2026 16:32:14 +0100 Subject: [PATCH] improvements to regular trip detection algorithm -> next step: write tests that verify approach --- .../IterativeClusterer.java | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/SuMoC/backend/regulartrip/detectionapproaches/IterativeClusterer.java b/src/main/java/SuMoC/backend/regulartrip/detectionapproaches/IterativeClusterer.java index a242c71..18cfed0 100644 --- a/src/main/java/SuMoC/backend/regulartrip/detectionapproaches/IterativeClusterer.java +++ b/src/main/java/SuMoC/backend/regulartrip/detectionapproaches/IterativeClusterer.java @@ -33,7 +33,8 @@ public class IterativeClusterer implements Detector { iterationResults.add(aggregateToSupportedDetections(iterationMap, trips, properties.getMinTripsForRegularity())); } - return pickBestIteration(iterationResults); + List 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 deduplicateOverlappingDetections(List detections) { + // Sort by supporter count (descending) + List sorted = new ArrayList<>(detections); + sorted.sort(Comparator.comparingInt(d -> -d.getSupportingTrips().size())); + + List result = new ArrayList<>(); + Set usedTripIds = new HashSet<>(); + + for (DetectedRegularTripDTO detection : sorted) { + // Get all trip IDs involved in this detection + Set 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 deletedDetections = new HashSet<>(); - Set deletedTrips = new HashSet<>(); for (Map.Entry 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()); } }