From e96162d353de305ce2d74ccbe653a4f396fa69dc Mon Sep 17 00:00:00 2001 From: Markus Straub Date: Wed, 27 Nov 2024 15:39:35 +0100 Subject: [PATCH] refactoring --- .gitignore | 5 ++-- .../matsim/drs/optimizer/DrsOptimizer.java | 7 ++--- .../drs/optimizer/RequestZoneRegistry.java | 21 ++++++++------ .../optimizer/RequestZoneRegistryTest.java | 28 +++++++++---------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 7d16e17..62212db 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,10 @@ .idea /TODO -/output* -/target +/data/floridsdorf/network.geojsonl /data/vienna dependency-reduced-pom.xml +/target +/output* /test/output diff --git a/src/main/java/at/ac/ait/matsim/drs/optimizer/DrsOptimizer.java b/src/main/java/at/ac/ait/matsim/drs/optimizer/DrsOptimizer.java index 94b21e7..20bbead 100644 --- a/src/main/java/at/ac/ait/matsim/drs/optimizer/DrsOptimizer.java +++ b/src/main/java/at/ac/ait/matsim/drs/optimizer/DrsOptimizer.java @@ -27,10 +27,9 @@ public DrsOptimizer(DrsData drsData, public MatchingResult optimize() { LOGGER.info("Matching process started!"); - RequestZoneRegistry originZoneRegistry = RequestZoneRegistry.createRequestZoneRegistry( - drsData.getZoneSystem(), true); - RequestZoneRegistry destinationZoneRegistry = RequestZoneRegistry.createRequestZoneRegistry( - drsData.getZoneSystem(), false); + RequestZoneRegistry originZoneRegistry = RequestZoneRegistry.forOrigins(drsData.getZoneSystem()); + RequestZoneRegistry destinationZoneRegistry = RequestZoneRegistry + .forDestinations(drsData.getZoneSystem()); RequestTimeSegmentRegistry timeSegmentRegistry = new RequestTimeSegmentRegistry(drsConfig); RequestsCollector requestsCollector = new RequestsCollector(drsConfig, population, drsData.getDrsNetwork(), driverRouter); diff --git a/src/main/java/at/ac/ait/matsim/drs/optimizer/RequestZoneRegistry.java b/src/main/java/at/ac/ait/matsim/drs/optimizer/RequestZoneRegistry.java index 318233e..f5b810d 100644 --- a/src/main/java/at/ac/ait/matsim/drs/optimizer/RequestZoneRegistry.java +++ b/src/main/java/at/ac/ait/matsim/drs/optimizer/RequestZoneRegistry.java @@ -30,39 +30,42 @@ private RequestZoneRegistry(ZoneSystem zoneSystem, boolean isOriginZoneRegistry) } } - public static RequestZoneRegistry createRequestZoneRegistry(ZoneSystem zoneSystem, - boolean isOriginZoneRegistry) { - return new RequestZoneRegistry(zoneSystem, isOriginZoneRegistry); + public static RequestZoneRegistry forOrigins(ZoneSystem zoneSystem) { + return new RequestZoneRegistry(zoneSystem, true); + } + + public static RequestZoneRegistry forDestinations(ZoneSystem zoneSystem) { + return new RequestZoneRegistry(zoneSystem, false); } public void addRequest(DrsRequest request) { - Id zoneId = getZoneId(request, isOriginZoneRegistry, zoneSystem); + Id zoneId = getZoneId(request); if (requestsInZones.get(zoneId).put(request.getId(), request) != null) { throw new IllegalStateException(request + " is already in the registry"); } } public void removeRequest(DrsRequest request) { - Id zoneId = getZoneId(request, isOriginZoneRegistry, zoneSystem); + Id zoneId = getZoneId(request); if (requestsInZones.get(zoneId).remove(request.getId()) == null) { throw new IllegalStateException(request + " is not in the registry"); } } public Stream findNearestRequests(Node node) { - return requestsInZones.get(getZoneId(node, zoneSystem)).values().stream(); + return requestsInZones.get(getZoneId(node)).values().stream(); } public Map, Map, DrsRequest>> getRequestsInZones() { return requestsInZones; } - static Id getZoneId(DrsRequest request, boolean isOriginZoneRegistry, ZoneSystem zoneSystem) { + public Id getZoneId(DrsRequest request) { Node node = isOriginZoneRegistry ? request.getFromLink().getFromNode() : request.getToLink().getToNode(); - return getZoneId(node, zoneSystem); + return getZoneId(node); } - private static Id getZoneId(Node node, ZoneSystem zoneSystem) { + public Id getZoneId(Node node) { Optional zone = zoneSystem.getZoneForNodeId(node.getId()); if (!zone.isPresent()) { throw new IllegalArgumentException("no zone found for node " + node.getId()); diff --git a/src/test/java/at/ac/ait/matsim/drs/optimizer/RequestZoneRegistryTest.java b/src/test/java/at/ac/ait/matsim/drs/optimizer/RequestZoneRegistryTest.java index a75e5dc..12a9ef9 100644 --- a/src/test/java/at/ac/ait/matsim/drs/optimizer/RequestZoneRegistryTest.java +++ b/src/test/java/at/ac/ait/matsim/drs/optimizer/RequestZoneRegistryTest.java @@ -1,8 +1,7 @@ package at.ac.ait.matsim.drs.optimizer; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.BeforeAll; @@ -37,40 +36,39 @@ public static void setup() { @BeforeEach public void beforeEach() { zoneSystem = new SquareGridZoneSystem(network, 800); - zoneRegistry = RequestZoneRegistry.createRequestZoneRegistry(zoneSystem, true); + zoneRegistry = RequestZoneRegistry.forOrigins(zoneSystem); request1 = req(1, 1540); request2 = req(2, 1037); request3 = req(3, 1674); } - @Test - public void testAllRequestsWithinDistance() { - - } + // @Test + // public void testAllRequestsWithinDistance() { + // List requests = List.of(req(1084), req(1085), req(1086), + // req(1159), req(1160), req(1161)); + // } @Test void testGetZoneIdForNeighboringRequests() { - assertSame(RequestZoneRegistry.getZoneId(request1, true, zoneSystem), - RequestZoneRegistry.getZoneId(request2, true, zoneSystem)); + assertEquals(zoneRegistry.getZoneId(request1), zoneRegistry.getZoneId(request2)); } @Test void testGetZoneIdForNotNeighboringRequests() { - assertNotSame(RequestZoneRegistry.getZoneId(request1, true, zoneSystem), - RequestZoneRegistry.getZoneId(request3, true, zoneSystem)); + assertNotEquals(zoneRegistry.getZoneId(request1), zoneRegistry.getZoneId(request3)); } @Test void testAddRequestsToZones() { zoneRegistry.addRequest(request1); assertEquals(1, zoneRegistry.getRequestsInZones() - .get(RequestZoneRegistry.getZoneId(request1, true, zoneSystem)).size()); + .get(zoneRegistry.getZoneId(request1)).size()); zoneRegistry.addRequest(request2); assertEquals(2, zoneRegistry.getRequestsInZones() - .get(RequestZoneRegistry.getZoneId(request1, true, zoneSystem)).size()); + .get(zoneRegistry.getZoneId(request1)).size()); zoneRegistry.addRequest(request3); assertEquals(2, zoneRegistry.getRequestsInZones() - .get(RequestZoneRegistry.getZoneId(request1, true, zoneSystem)).size()); + .get(zoneRegistry.getZoneId(request1)).size()); } @Test @@ -85,7 +83,7 @@ void testAddSameRequestTwice() { void testRemoveRequestInRegistry() { zoneRegistry.addRequest(request1); zoneRegistry.addRequest(request2); - Id zoneId = RequestZoneRegistry.getZoneId(request1, true, zoneSystem); + Id zoneId = zoneRegistry.getZoneId(request1); assertEquals(2, zoneRegistry.getRequestsInZones().get(zoneId).size()); zoneRegistry.removeRequest(request1); assertEquals(1, zoneRegistry.getRequestsInZones().get(zoneId).size());