Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
markusstraub committed Nov 27, 2024
1 parent fffbb01 commit e96162d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
.idea

/TODO
/output*
/target
/data/floridsdorf/network.geojsonl
/data/vienna

dependency-reduced-pom.xml
/target
/output*
/test/output
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Zone> zoneId = getZoneId(request, isOriginZoneRegistry, zoneSystem);
Id<Zone> 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<Zone> zoneId = getZoneId(request, isOriginZoneRegistry, zoneSystem);
Id<Zone> zoneId = getZoneId(request);
if (requestsInZones.get(zoneId).remove(request.getId()) == null) {
throw new IllegalStateException(request + " is not in the registry");
}
}

public Stream<DrsRequest> findNearestRequests(Node node) {
return requestsInZones.get(getZoneId(node, zoneSystem)).values().stream();
return requestsInZones.get(getZoneId(node)).values().stream();
}

public Map<Id<Zone>, Map<Id<Request>, DrsRequest>> getRequestsInZones() {
return requestsInZones;
}

static Id<Zone> getZoneId(DrsRequest request, boolean isOriginZoneRegistry, ZoneSystem zoneSystem) {
public Id<Zone> getZoneId(DrsRequest request) {
Node node = isOriginZoneRegistry ? request.getFromLink().getFromNode() : request.getToLink().getToNode();
return getZoneId(node, zoneSystem);
return getZoneId(node);
}

private static Id<Zone> getZoneId(Node node, ZoneSystem zoneSystem) {
public Id<Zone> getZoneId(Node node) {
Optional<Zone> zone = zoneSystem.getZoneForNodeId(node.getId());
if (!zone.isPresent()) {
throw new IllegalArgumentException("no zone found for node " + node.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<DrsRiderRequest> 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
Expand All @@ -85,7 +83,7 @@ void testAddSameRequestTwice() {
void testRemoveRequestInRegistry() {
zoneRegistry.addRequest(request1);
zoneRegistry.addRequest(request2);
Id<Zone> zoneId = RequestZoneRegistry.getZoneId(request1, true, zoneSystem);
Id<Zone> zoneId = zoneRegistry.getZoneId(request1);
assertEquals(2, zoneRegistry.getRequestsInZones().get(zoneId).size());
zoneRegistry.removeRequest(request1);
assertEquals(1, zoneRegistry.getRequestsInZones().get(zoneId).size());
Expand Down

0 comments on commit e96162d

Please sign in to comment.