diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystem.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystem.java index 85bb71e0c17..99a8d542d59 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystem.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystem.java @@ -4,15 +4,14 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.network.Node; -import javax.annotation.Nullable; import java.util.List; import java.util.Map; +import java.util.Optional; public interface ZoneSystem { - @Nullable - Zone getZoneForLinkId(Id link); + Optional getZoneForLinkId(Id link); - Zone getZoneForNodeId(Node node); + Optional getZoneForNodeId(Node node); List getLinksForZoneId(Id zone); diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystemImpl.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystemImpl.java index a52c53af6e1..9cff58eee13 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystemImpl.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystemImpl.java @@ -7,8 +7,8 @@ import org.matsim.api.core.v01.network.Node; import org.matsim.contrib.common.zones.util.ZoneFinder; -import javax.annotation.Nullable; import java.util.*; +import java.util.function.Consumer; public class ZoneSystemImpl implements ZoneSystem { @@ -32,11 +32,10 @@ public ZoneSystemImpl(Collection zones, ZoneFinder zoneFinder, Network net this.link2zone.putAll(linkToZoneMap); for (Link link : network.getLinks().values()) { - Zone zone = zoneFinder.findZone(link.getToNode().getCoord()); - if(zone != null) { + zoneFinder.findZone(link.getToNode().getCoord()).ifPresent(zone -> { List links = zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId1 -> new ArrayList<>()); - links.add(link); - } + links.add(link); + }); } } @@ -46,14 +45,13 @@ public ZoneSystemImpl(Collection zones, ZoneFinder zoneFinder, Network net * Result may be null in case the given link is outside of the service area. */ @Override - @Nullable - public Zone getZoneForLinkId(Id link) { - return link2zone.get(link); + public Optional getZoneForLinkId(Id link) { + return Optional.ofNullable(link2zone.get(link)); } @Override - public Zone getZoneForNodeId(Node node) { - return nodeToZoneMap.get(node.getId()); + public Optional getZoneForNodeId(Node node) { + return Optional.ofNullable(nodeToZoneMap.get(node.getId())); } @Override diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystemUtils.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystemUtils.java index 662c830dbd8..6439fabe0f4 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystemUtils.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystemUtils.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import static java.util.stream.Collectors.toList; @@ -97,14 +98,16 @@ public static Map, Zone> readZones(URL zonesXmlUrl, URL zonesShpUrl) { public static IdMap createLinkToZoneMap(Network network, ZoneFinder zoneFinder) { return EntryStream.of(network.getLinks()) .mapValues(link -> zoneFinder.findZone(link.getToNode().getCoord())) - .filterValues(Objects::nonNull) + .filterValues(Optional::isPresent) + .mapValues(Optional::get) .collect(IdCollectors.toIdMap(Link.class, Map.Entry::getKey, Map.Entry::getValue)); } public static IdMap createNodeToZoneMap(Network network, ZoneFinder zoneFinder) { return EntryStream.of(network.getNodes()) .mapValues(node -> zoneFinder.findZone(node.getCoord())) - .filterValues(Objects::nonNull) + .filterValues(Optional::isPresent) + .mapValues(Optional::get) .collect(IdCollectors.toIdMap(Node.class, Map.Entry::getKey, Map.Entry::getValue)); } diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZonalSystems.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystems.java similarity index 90% rename from contribs/common/src/main/java/org/matsim/contrib/common/zones/ZonalSystems.java rename to contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystems.java index 3d717fad792..f5aeb412587 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZonalSystems.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/ZoneSystems.java @@ -21,11 +21,7 @@ import static java.util.stream.Collectors.*; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.function.BinaryOperator; import java.util.stream.Collectors; @@ -39,9 +35,9 @@ import org.matsim.core.utils.geometry.geotools.MGC; //TODO add zone indexing? -public class ZonalSystems { +public class ZoneSystems { public static Set filterZonesWithNodes(Collection nodes, ZoneSystem zoneSystem) { - return nodes.stream().map(zoneSystem::getZoneForNodeId).collect(toSet()); + return nodes.stream().map(zoneSystem::getZoneForNodeId).filter(Optional::isPresent).map(Optional::get).collect(toSet()); } public static List selectNodesWithinArea(Collection nodes, List areaGeoms) { @@ -53,12 +49,12 @@ public static List selectNodesWithinArea(Collection nodes, public static Map computeMostCentralNodes(Collection nodes, ZoneSystem zoneSystem) { BinaryOperator chooseMoreCentralNode = (n1, n2) -> { - Zone zone = zoneSystem.getZoneForNodeId(n1); + Zone zone = zoneSystem.getZoneForNodeId(n1).orElseThrow(); return DistanceUtils.calculateSquaredDistance(n1, zone) <= DistanceUtils.calculateSquaredDistance(n2, zone) ? n1 : n2; }; return nodes.stream() - .map(n -> Pair.of(n, zoneSystem.getZoneForNodeId(n))) + .map(n -> Pair.of(n, zoneSystem.getZoneForNodeId(n).orElseThrow())) .collect(toMap(Pair::getValue, Pair::getKey, chooseMoreCentralNode)); } diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/SquareGridSystem.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/SquareGridZoneSystem.java similarity index 87% rename from contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/SquareGridSystem.java rename to contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/SquareGridZoneSystem.java index 64dd44f7465..3d2b2db686e 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/SquareGridSystem.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/SquareGridZoneSystem.java @@ -27,12 +27,11 @@ import org.matsim.contrib.common.zones.Zone; import org.matsim.contrib.common.zones.ZoneSystem; -import javax.annotation.Nullable; import java.util.*; import static java.util.stream.Collectors.toMap; -public class SquareGridSystem implements ZoneSystem { +public class SquareGridZoneSystem implements ZoneSystem { private final SquareGrid grid; private final Map, Zone> zones; @@ -40,7 +39,7 @@ public class SquareGridSystem implements ZoneSystem { private final Network network; - public SquareGridSystem(Network network, double cellSize) { + public SquareGridZoneSystem(Network network, double cellSize) { this.network = network; this.grid = new SquareGrid(network.getNodes().values(), cellSize); zones = network.getNodes().values().stream() @@ -58,15 +57,14 @@ public Map, Zone> getZones() { return Collections.unmodifiableMap(zones); } - @Nullable @Override - public Zone getZoneForLinkId(Id link) { - return grid.getZone(network.getLinks().get(link).getToNode().getCoord()); + public Optional getZoneForLinkId(Id link) { + return Optional.ofNullable(grid.getZone(network.getLinks().get(link).getToNode().getCoord())); } @Override - public Zone getZoneForNodeId(Node node) { - return grid.getZone(node.getCoord()); + public Optional getZoneForNodeId(Node node) { + return Optional.ofNullable(grid.getZone(node.getCoord())); } @Override diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/h3/H3ZoneSystemUtils.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/h3/H3ZoneSystemUtils.java index 6d0692ff784..6c8fb1abb82 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/h3/H3ZoneSystemUtils.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/h3/H3ZoneSystemUtils.java @@ -17,7 +17,7 @@ import org.matsim.core.utils.geometry.transformations.TransformationFactory; import java.util.Map; -import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -41,8 +41,8 @@ public static ZoneSystem createFromPreparedGeometries(Network network, Map> requiredZones = network.getLinks().values().stream() .map(link -> h3ZoneFinder.findZone(link.getToNode().getCoord())) - .filter(Objects::nonNull) // Filter out any null zones - .map(Identifiable::getId) // Convert Zone to Id, adjust this line if findZone() directly returns Id + .filter(Optional::isPresent) // Filter out any null zones + .map(op -> op.get().getId()) // Convert Zone to Id, adjust this line if findZone() directly returns Id .collect(Collectors.toSet()); // Collect unique Ids into a Set zones.keySet().retainAll(requiredZones); @@ -67,10 +67,10 @@ private static class H3ZoneFinder implements ZoneFinder { } @Override - public Zone findZone(Coord coord) { + public Optional findZone(Coord coord) { LatLng latLng = H3GridUtils.coordToLatLng(ct.transform(coord)); Id zoneId = ZoneSystemUtils.createZoneId(h3.latLngToCellAddress(latLng.lat, latLng.lng, resolution)); - return zones.getOrDefault(zoneId, null); + return Optional.ofNullable(zones.get(zoneId)); } } } diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/util/ZoneFinder.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/util/ZoneFinder.java index c4c59f94177..95f0a9ce53c 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/util/ZoneFinder.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/util/ZoneFinder.java @@ -22,6 +22,8 @@ import org.matsim.api.core.v01.Coord; import org.matsim.contrib.common.zones.Zone; +import java.util.Optional; + public interface ZoneFinder { - Zone findZone(Coord coord); + Optional findZone(Coord coord); } diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/util/ZoneFinderImpl.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/util/ZoneFinderImpl.java index 6b9aaebea5a..fbe32f45ac5 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/util/ZoneFinderImpl.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/util/ZoneFinderImpl.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; +import java.util.Optional; import org.locationtech.jts.geom.Envelope; import org.locationtech.jts.geom.Point; @@ -52,13 +53,13 @@ public ZoneFinderImpl(Map, Zone> zones) { @Override @SuppressWarnings("unchecked") - public Zone findZone(Coord coord) { + public Optional findZone(Coord coord) { Point point = MGC.coord2Point(coord); Envelope env = point.getEnvelopeInternal(); Zone zone = getSmallestZoneContainingPoint(quadTree.query(env), point); if (zone != null) { - return zone; + return Optional.of(zone); } if (expansionDistance > 0) { @@ -66,7 +67,7 @@ public Zone findZone(Coord coord) { zone = getNearestZone(quadTree.query(env), point); } - return zone; + return Optional.ofNullable(zone); } private Zone getSmallestZoneContainingPoint(List zones, Point point) { diff --git a/contribs/drt-extensions/src/test/java/org/matsim/contrib/drt/extension/h3/drtZone/H3DrtZonalSystemTest.java b/contribs/drt-extensions/src/test/java/org/matsim/contrib/drt/extension/h3/drtZone/H3DrtZonalSystemTest.java index 649b8399a33..81b886f6c37 100644 --- a/contribs/drt-extensions/src/test/java/org/matsim/contrib/drt/extension/h3/drtZone/H3DrtZonalSystemTest.java +++ b/contribs/drt-extensions/src/test/java/org/matsim/contrib/drt/extension/h3/drtZone/H3DrtZonalSystemTest.java @@ -55,11 +55,11 @@ void test_Holzkirchen_Resolution3() { assertThat(drtZonalSystem.getZones().containsKey(createZoneId("831f8dfffffffff"))).isTrue(); // center of Holzkirchen - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).getId()).isEqualTo(createZoneId("831f8dfffffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).orElseThrow().getId()).isEqualTo(createZoneId("831f8dfffffffff")); // Thanning (Western border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).getId()).isEqualTo(createZoneId("831f8dfffffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).orElseThrow().getId()).isEqualTo(createZoneId("831f8dfffffffff")); // between Gross- and Kleinpienzenau (Southeastern border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).getId()).isEqualTo(createZoneId("831f89fffffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).orElseThrow().getId()).isEqualTo(createZoneId("831f89fffffffff")); //check all links are mapped for (Link link : network.getLinks().values()) { @@ -81,11 +81,11 @@ void test_Holzkirchen_Resolution5() { assertThat(drtZonalSystem.getZones().containsKey(createZoneId("851f89d3fffffff"))).isTrue(); // center of Holzkirchen - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).getId()).isEqualTo(createZoneId("851f8d6bfffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).orElseThrow().getId()).isEqualTo(createZoneId("851f8d6bfffffff")); // Thanning (Western border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).getId()).isEqualTo(createZoneId("851f88b7fffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).orElseThrow().getId()).isEqualTo(createZoneId("851f88b7fffffff")); // between Gross- and Kleinpienzenau (Southeastern border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).getId()).isEqualTo(createZoneId("851f89d3fffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).orElseThrow().getId()).isEqualTo(createZoneId("851f89d3fffffff")); //check all links are mapped for (Link link : network.getLinks().values()) { @@ -109,11 +109,11 @@ void test_Holzkirchen_Resolution6() { assertThat(drtZonalSystem.getZones().containsKey(createZoneId("861f89d37ffffff"))).isTrue(); // center of Holzkirchen - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).getId()).isEqualTo(createZoneId("861f8d697ffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).orElseThrow().getId()).isEqualTo(createZoneId("861f8d697ffffff")); // Thanning (Western border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).getId()).isEqualTo(createZoneId("861f88b47ffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).orElseThrow().getId()).isEqualTo(createZoneId("861f88b47ffffff")); // between Gross- and Kleinpienzenau (Southeastern border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).getId()).isEqualTo(createZoneId("861f89d07ffffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).orElseThrow().getId()).isEqualTo(createZoneId("861f89d07ffffff")); //check all links are mapped for (Link link : network.getLinks().values()) { @@ -130,11 +130,11 @@ void test_Holzkirchen_Resolution10() { H3GridUtils.createH3GridFromNetwork(network, resolution, crs), crs, resolution); // center of Holzkirchen - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).getId()).isEqualTo(createZoneId("8a1f8d6930b7fff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).orElseThrow().getId()).isEqualTo(createZoneId("8a1f8d6930b7fff")); // Thanning (Western border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).getId()).isEqualTo(createZoneId("8a1f88b4025ffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).orElseThrow().getId()).isEqualTo(createZoneId("8a1f88b4025ffff")); // between Gross- and Kleinpienzenau (Southeastern border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).getId()).isEqualTo(createZoneId("8a1f89d06d5ffff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).orElseThrow().getId()).isEqualTo(createZoneId("8a1f89d06d5ffff")); //check all links are mapped for (Link link : network.getLinks().values()) { @@ -151,11 +151,11 @@ void test_Holzkirchen_Resolution12() { H3GridUtils.createH3GridFromNetwork(network, resolution, crs), crs, resolution); // center of Holzkirchen - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).getId()).isEqualTo(createZoneId("8c1f8d6930b63ff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(358598)).orElseThrow().getId()).isEqualTo(createZoneId("8c1f8d6930b63ff")); // Thanning (Western border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).getId()).isEqualTo(createZoneId("8c1f88b4025d1ff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(78976)).orElseThrow().getId()).isEqualTo(createZoneId("8c1f88b4025d1ff")); // between Gross- and Kleinpienzenau (Southeastern border of network) - assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).getId()).isEqualTo(createZoneId("8c1f89d06d581ff")); + assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId(59914)).orElseThrow().getId()).isEqualTo(createZoneId("8c1f89d06d581ff")); //check all links are mapped for (Link link : network.getLinks().values()) { diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/DrtZonalWaitTimesAnalyzer.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/DrtZonalWaitTimesAnalyzer.java index 4972db0a280..f86d7759e37 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/DrtZonalWaitTimesAnalyzer.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/DrtZonalWaitTimesAnalyzer.java @@ -29,6 +29,7 @@ import org.locationtech.jts.geom.Polygon; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.IdMap; +import org.matsim.api.core.v01.Identifiable; import org.matsim.api.core.v01.population.Person; import org.matsim.contrib.common.zones.Zone; import org.matsim.contrib.common.zones.ZoneSystem; @@ -139,10 +140,10 @@ private Map, DescriptiveStatistics> createZonalStats() { for (EventSequence seq : requestAnalyzer.getPerformedRequestSequences().values()) { for (Map.Entry, EventSequence.PersonEvents> entry : seq.getPersonEvents().entrySet()) { if(entry.getValue().getPickedUp().isPresent()) { - Zone zone = zones.getZoneForLinkId(seq.getSubmitted().getFromLinkId()); - final Id zoneStr = zone != null ? zone.getId() : zoneIdForOutsideOfZonalSystem; + Id zone = zones.getZoneForLinkId(seq.getSubmitted().getFromLinkId()) + .map(Identifiable::getId).orElse(zoneIdForOutsideOfZonalSystem); double waitTime = entry.getValue().getPickedUp().get() .getTime() - seq.getSubmitted().getTime(); - zoneStats.get(zoneStr).addValue(waitTime); + zoneStats.get(zone).addValue(waitTime); } } } diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/ZonalIdleVehicleCollector.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/ZonalIdleVehicleCollector.java index 2626e21fc3f..2b965ce7f6b 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/ZonalIdleVehicleCollector.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/ZonalIdleVehicleCollector.java @@ -64,10 +64,7 @@ public void handleEvent(TaskEndedEvent event) { private void handleEvent(AbstractTaskEvent event, Consumer handler) { if (event.getDvrpMode().equals(dvrpMode) && event.getTaskType().equals(DrtStayTask.TYPE)) { - Zone zone = zonalSystem.getZoneForLinkId(event.getLinkId()); - if (zone != null) { - handler.accept(zone); - } + zonalSystem.getZoneForLinkId(event.getLinkId()).ifPresent(handler); } } diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/ZonalIdleVehicleXYVisualiser.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/ZonalIdleVehicleXYVisualiser.java index 8813f7673ab..4d00795c440 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/ZonalIdleVehicleXYVisualiser.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/zonal/ZonalIdleVehicleXYVisualiser.java @@ -83,10 +83,7 @@ public void handleEvent(TaskEndedEvent event) { private void handleEvent(AbstractTaskEvent event, Consumer handler) { if (event.getDvrpMode().equals(mode) && event.getTaskType().equals(DrtStayTask.TYPE)) { - Zone zone = zonalSystem.getZoneForLinkId(event.getLinkId()); - if (zone != null) { - handler.accept(zone); - } + zonalSystem.getZoneForLinkId(event.getLinkId()).ifPresent(handler); } } diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/RebalancingUtils.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/RebalancingUtils.java index cd6310d8521..bc712ab59f9 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/RebalancingUtils.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/RebalancingUtils.java @@ -47,11 +47,9 @@ public static Map> groupRebalancableVehicles(ZoneSystem Map> rebalancableVehiclesPerZone = new HashMap<>(); rebalancableVehicles.filter(v -> v.getServiceEndTime() > time + params.minServiceTime).forEach(v -> { Link link = ((StayTask)v.getSchedule().getCurrentTask()).getLink(); - Zone zone = zoneSystem.getZoneForLinkId(link.getId()); - if (zone == null) { - zone = ZoneImpl.createDummyZone(Id.create("single-vehicle-zone-" + v.getId(), Zone.class), - link.getToNode().getCoord()); - } + Zone zone = zoneSystem.getZoneForLinkId(link.getId()) + .orElse(ZoneImpl.createDummyZone(Id.create("single-vehicle-zone-" + v.getId(), Zone.class), + link.getToNode().getCoord())); rebalancableVehiclesPerZone.computeIfAbsent(zone, z -> new ArrayList<>()).add(v); }); return rebalancableVehiclesPerZone; @@ -67,10 +65,10 @@ public static Map> groupSoonIdleVehicles(ZoneSystem zone if (stayTask.getStatus() == Task.TaskStatus.PLANNED && stayTask.getBeginTime() < time + params.maxTimeBeforeIdle && v.getServiceEndTime() > time + params.minServiceTime) { - Zone zone = zoneSystem.getZoneForLinkId(stayTask.getLink().getId()); - if (zone != null) { - soonIdleVehiclesPerZone.computeIfAbsent(zone, z -> new ArrayList<>()).add(v); - } + zoneSystem.getZoneForLinkId(stayTask.getLink().getId()) + .ifPresent( + zone -> soonIdleVehiclesPerZone.computeIfAbsent(zone, z -> new ArrayList<>()).add(v) + ); } } return soonIdleVehiclesPerZone; diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/NetDepartureReplenishDemandEstimator.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/NetDepartureReplenishDemandEstimator.java index cc1c0b41dd5..0ebce86e3de 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/NetDepartureReplenishDemandEstimator.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/NetDepartureReplenishDemandEstimator.java @@ -52,8 +52,8 @@ public void handleEvent(DrtRequestSubmittedEvent event) { if (event.getMode().equals(mode)) { // At the submission time, this is only a potential trip. int timeBin = (int)Math.floor(event.getTime() / timeBinSize); - Zone departureZoneId = zonalSystem.getZoneForLinkId(event.getFromLinkId()); - Zone arrivalZoneId = zonalSystem.getZoneForLinkId(event.getToLinkId()); + Zone departureZoneId = zonalSystem.getZoneForLinkId(event.getFromLinkId()).orElseThrow(); + Zone arrivalZoneId = zonalSystem.getZoneForLinkId(event.getToLinkId()).orElseThrow(); potentialDrtTripsMap.put(event.getRequestId(), new Trip(timeBin, departureZoneId, arrivalZoneId)); } } diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/PreviousIterationDrtDemandEstimator.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/PreviousIterationDrtDemandEstimator.java index f6d2f17745d..5c7aa4ca6f4 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/PreviousIterationDrtDemandEstimator.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/PreviousIterationDrtDemandEstimator.java @@ -69,17 +69,16 @@ public void reset(int iteration) { @Override public void handleEvent(PersonDepartureEvent event) { if (event.getLegMode().equals(mode)) { - Zone zone = zonalSystem.getZoneForLinkId(event.getLinkId()); - if (zone == null) { + zonalSystem.getZoneForLinkId(event.getLinkId()).ifPresentOrElse( + zone -> { + int timeBin = getBinForTime(event.getTime()); + currentIterationDepartures.computeIfAbsent(timeBin, v -> new HashMap<>()) + .computeIfAbsent(zone, z -> new MutableInt()) + .increment(); + }, //might be that somebody walks into the service area or that service area is larger/different than DrtZonalSystem... - logger.warn("No zone found for linkId " + event.getLinkId().toString()); - return; - } - - int timeBin = getBinForTime(event.getTime()); - currentIterationDepartures.computeIfAbsent(timeBin, v -> new HashMap<>()) - .computeIfAbsent(zone, z -> new MutableInt()) - .increment(); + () -> logger.warn("No zone found for linkId " + event.getLinkId().toString()) + ); } } diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/targetcalculator/EqualVehiclesToPopulationRatioTargetCalculator.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/targetcalculator/EqualVehiclesToPopulationRatioTargetCalculator.java index 7968af6e705..a3e587983b9 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/targetcalculator/EqualVehiclesToPopulationRatioTargetCalculator.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/rebalancing/targetcalculator/EqualVehiclesToPopulationRatioTargetCalculator.java @@ -35,7 +35,7 @@ import java.util.List; import java.util.Map; -import java.util.Objects; +import java.util.Optional; import java.util.function.ToDoubleFunction; import java.util.stream.Collectors; @@ -73,7 +73,8 @@ private Map countFirstActsPerZone(ZoneSystem zonalSystem, Populat .stream() .map(person -> (Activity)person.getSelectedPlan().getPlanElements().get(0)) .map(activity -> zonalSystem.getZoneForLinkId(activity.getLinkId())) - .filter(Objects::nonNull) + .filter(Optional::isPresent) + .map(Optional::get) .collect(Collectors.groupingBy(zone -> zone, collectingAndThen(counting(), Long::intValue))); } diff --git a/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/DrtZonalSystemTest.java b/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/DrtZonalSystemTest.java index 6cc68be7cbf..82fdc7d4129 100644 --- a/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/DrtZonalSystemTest.java +++ b/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/DrtZonalSystemTest.java @@ -49,14 +49,14 @@ public class DrtZonalSystemTest { void test_cellSize100() { ZoneSystem drtZonalSystem = createFromPreparedGeometries(createNetwork(), DrtGridUtils.createGridFromNetwork(createNetwork(), 100)); - Assertions.assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId("ab")).getId().toString()).isEqualTo("10"); + Assertions.assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId("ab")).orElseThrow().getId().toString()).isEqualTo("10"); } @Test void test_cellSize700() { ZoneSystem drtZonalSystem = createFromPreparedGeometries(createNetwork(), DrtGridUtils.createGridFromNetwork(createNetwork(), 700)); - Assertions.assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId("ab")).getId().toString()).isEqualTo("2"); + Assertions.assertThat(drtZonalSystem.getZoneForLinkId(Id.createLinkId("ab")).orElseThrow().getId().toString()).isEqualTo("2"); } @Test @@ -72,7 +72,7 @@ void test_gridWithinServiceArea(){ //link 'da' is outside of the service area Id id = Id.createLinkId("da"); - Assertions.assertThat(zonalSystem.getZoneForLinkId(id)).isNull(); + Assertions.assertThat(zonalSystem.getZoneForLinkId(id)).isNotPresent(); } @Test diff --git a/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/RandomDrtZoneTargetLinkSelectorTest.java b/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/RandomDrtZoneTargetLinkSelectorTest.java index fcaec083823..8a31e2470db 100644 --- a/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/RandomDrtZoneTargetLinkSelectorTest.java +++ b/contribs/drt/src/test/java/org/matsim/contrib/drt/analysis/zonal/RandomDrtZoneTargetLinkSelectorTest.java @@ -33,6 +33,7 @@ import org.mockito.ArgumentCaptor; import java.util.List; +import java.util.Optional; import java.util.function.IntUnaryOperator; import static org.assertj.core.api.Assertions.assertThat; @@ -61,7 +62,7 @@ void testSelectTargetLink_fourLinks() { when(random.applyAsInt(boundCaptor.capture())).thenReturn(0, 3, 1, 2); //test selected target links - RandomDrtZoneTargetLinkSelector selector = new RandomDrtZoneTargetLinkSelector(new ZoneSystemImpl(List.of(zone), coord -> zone, network), random); + RandomDrtZoneTargetLinkSelector selector = new RandomDrtZoneTargetLinkSelector(new ZoneSystemImpl(List.of(zone), coord -> Optional.of(zone), network), random); assertThat(selector.selectTargetLink(zone)).isEqualTo(network.getLinks().get(LINK_ID_0)); assertThat(selector.selectTargetLink(zone)).isEqualTo(network.getLinks().get(LINK_ID_3)); assertThat(selector.selectTargetLink(zone)).isEqualTo(network.getLinks().get(LINK_ID_1)); diff --git a/contribs/drt/src/test/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/PreviousIterationDrtDemandEstimatorTest.java b/contribs/drt/src/test/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/PreviousIterationDrtDemandEstimatorTest.java index 4b7c3b6638e..7f43fd42d8b 100644 --- a/contribs/drt/src/test/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/PreviousIterationDrtDemandEstimatorTest.java +++ b/contribs/drt/src/test/java/org/matsim/contrib/drt/optimizer/rebalancing/demandestimator/PreviousIterationDrtDemandEstimatorTest.java @@ -20,10 +20,6 @@ package org.matsim.contrib.drt.optimizer.rebalancing.demandestimator; -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.List; - import org.junit.jupiter.api.Test; import org.matsim.api.core.v01.Coord; import org.matsim.api.core.v01.Id; @@ -36,11 +32,14 @@ import org.matsim.contrib.common.zones.ZoneImpl; import org.matsim.contrib.common.zones.ZoneSystem; import org.matsim.contrib.common.zones.ZoneSystemImpl; -import org.matsim.contrib.common.zones.util.ZoneFinder; import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingParams; import org.matsim.contrib.drt.run.DrtConfigGroup; import org.matsim.core.network.NetworkUtils; -import org.matsim.testcases.fakes.FakeLink; + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; /** * @author michalm (Michal Maciejewski) @@ -58,9 +57,9 @@ public class PreviousIterationDrtDemandEstimatorTest { private final Zone zone2 = ZoneImpl.createDummyZone(Id.create("zone_2", Zone.class), new Coord()); private final ZoneSystem zonalSystem = new ZoneSystemImpl(List.of(zone1, zone2), coord -> { if(coord == link1.getToNode().getCoord()) { - return zone1; + return Optional.of(zone1); } else if(coord == link2.getToNode().getCoord()) { - return zone2; + return Optional.of(zone2); } else { throw new RuntimeException(); } diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/AdaptiveTravelTimeMatrixImpl.java b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/AdaptiveTravelTimeMatrixImpl.java index 591ace72ff5..ba82f64ba86 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/AdaptiveTravelTimeMatrixImpl.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/AdaptiveTravelTimeMatrixImpl.java @@ -23,8 +23,8 @@ import org.matsim.api.core.v01.network.Node; import org.matsim.contrib.common.util.DistanceUtils; import org.matsim.contrib.common.zones.Zone; -import org.matsim.contrib.common.zones.systems.grid.SquareGridSystem; -import org.matsim.contrib.common.zones.ZonalSystems; +import org.matsim.contrib.common.zones.systems.grid.SquareGridZoneSystem; +import org.matsim.contrib.common.zones.ZoneSystems; import java.util.List; import java.util.Map; @@ -38,7 +38,7 @@ public class AdaptiveTravelTimeMatrixImpl implements AdaptiveTravelTimeMatrix { private final double TIME_INTERVAL = 3600.; private final List timeDependentMatrix; - private final SquareGridSystem gridSystem; + private final SquareGridZoneSystem gridSystem; private final double alpha; private final Map centralNodes; private final int numberOfBins; @@ -49,8 +49,8 @@ public AdaptiveTravelTimeMatrixImpl(double maxTime, Network dvrpNetwork, DvrpTra TravelTimeMatrix freeSpeedMatrix, double alpha) { this.alpha = alpha; this.numberOfBins = numberOfBins(maxTime); - this.gridSystem = new SquareGridSystem(dvrpNetwork, params.cellSize); - this.centralNodes = ZonalSystems.computeMostCentralNodes(dvrpNetwork.getNodes().values(), this.gridSystem); + this.gridSystem = new SquareGridZoneSystem(dvrpNetwork, params.cellSize); + this.centralNodes = ZoneSystems.computeMostCentralNodes(dvrpNetwork.getNodes().values(), this.gridSystem); this.timeDependentMatrix = IntStream.range(0, numberOfBins).mapToObj(i -> new Matrix(centralNodes.keySet())) .toList(); this.params = params; @@ -113,7 +113,7 @@ public double getTravelTime(Node fromNode, Node toNode, double departureTime) { if (sparseValue != null) { return sparseValue; } - return this.timeDependentMatrix.get(bin).get(this.gridSystem.getZoneForNodeId(fromNode), this.gridSystem.getZoneForNodeId(toNode)); + return this.timeDependentMatrix.get(bin).get(this.gridSystem.getZoneForNodeId(fromNode).orElseThrow(), this.gridSystem.getZoneForNodeId(toNode).orElseThrow()); } int getBin(double departureTime) { @@ -135,7 +135,7 @@ public void setTravelTime(Node fromNode, Node toNode, double routeEstimate, doub } else { double currentTravelTimeEstimate = this.getTravelTime(fromNode, toNode, departureTime); double value = getUpdatedValue(currentTravelTimeEstimate, routeEstimate, this.alpha); - this.timeDependentMatrix.get(bin).set(this.gridSystem.getZoneForNodeId(fromNode), this.gridSystem.getZoneForNodeId(toNode), + this.timeDependentMatrix.get(bin).set(this.gridSystem.getZoneForNodeId(fromNode).orElseThrow(), this.gridSystem.getZoneForNodeId(toNode).orElseThrow(), value); } diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrix.java b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrix.java index 1ba73bafdd3..7a8e027c523 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrix.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrix.java @@ -22,10 +22,11 @@ import org.matsim.api.core.v01.network.Network; import org.matsim.api.core.v01.network.Node; +import org.matsim.contrib.common.zones.ZoneSystems; +import org.matsim.contrib.common.zones.ZoneSystem; +import org.matsim.contrib.common.zones.systems.grid.SquareGridZoneSystem; import org.matsim.contrib.dvrp.router.TimeAsTravelDisutility; import org.matsim.contrib.dvrp.trafficmonitoring.QSimFreeSpeedTravelTime; -import org.matsim.contrib.common.zones.systems.grid.SquareGridSystem; -import org.matsim.contrib.common.zones.ZonalSystems; import org.matsim.core.router.util.TravelTime; /** @@ -37,13 +38,13 @@ public static FreeSpeedTravelTimeMatrix createFreeSpeedMatrix(Network dvrpNetwor return new FreeSpeedTravelTimeMatrix(dvrpNetwork, params, numberOfThreads, new QSimFreeSpeedTravelTime(qSimTimeStepSize)); } - private final SquareGridSystem gridSystem; + private final ZoneSystem zoneSystem; private final Matrix freeSpeedTravelTimeMatrix; private final SparseMatrix freeSpeedTravelTimeSparseMatrix; public FreeSpeedTravelTimeMatrix(Network dvrpNetwork, DvrpTravelTimeMatrixParams params, int numberOfThreads, TravelTime travelTime) { - gridSystem = new SquareGridSystem(dvrpNetwork, params.cellSize); - var centralNodes = ZonalSystems.computeMostCentralNodes(dvrpNetwork.getNodes().values(), gridSystem); + zoneSystem = new SquareGridZoneSystem(dvrpNetwork, params.cellSize); + var centralNodes = ZoneSystems.computeMostCentralNodes(dvrpNetwork.getNodes().values(), zoneSystem); var travelDisutility = new TimeAsTravelDisutility(travelTime); var routingParams = new TravelTimeMatrices.RoutingParams(dvrpNetwork, travelTime, travelDisutility, numberOfThreads); freeSpeedTravelTimeMatrix = TravelTimeMatrices.calculateTravelTimeMatrix(routingParams, centralNodes, 0); @@ -62,10 +63,10 @@ public int getTravelTime(Node fromNode, Node toNode, double departureTime) { return time; } } - return freeSpeedTravelTimeMatrix.get(gridSystem.getZoneForNodeId(fromNode), gridSystem.getZoneForNodeId(toNode)); + return freeSpeedTravelTimeMatrix.get(zoneSystem.getZoneForNodeId(fromNode).orElseThrow(), zoneSystem.getZoneForNodeId(toNode).orElseThrow()); } public int getZonalTravelTime(Node fromNode, Node toNode, double departureTime) { - return freeSpeedTravelTimeMatrix.get(gridSystem.getZoneForNodeId(fromNode), gridSystem.getZoneForNodeId(toNode)); + return freeSpeedTravelTimeMatrix.get(zoneSystem.getZoneForNodeId(fromNode).orElseThrow(), zoneSystem.getZoneForNodeId(toNode).orElseThrow()); } } diff --git a/contribs/taxi/src/main/java/org/matsim/contrib/etaxi/optimizer/ETaxiOptimizerProvider.java b/contribs/taxi/src/main/java/org/matsim/contrib/etaxi/optimizer/ETaxiOptimizerProvider.java index 2e399fb0736..ebc724ff7eb 100644 --- a/contribs/taxi/src/main/java/org/matsim/contrib/etaxi/optimizer/ETaxiOptimizerProvider.java +++ b/contribs/taxi/src/main/java/org/matsim/contrib/etaxi/optimizer/ETaxiOptimizerProvider.java @@ -37,7 +37,7 @@ import org.matsim.contrib.taxi.optimizer.rules.UnplannedRequestZonalRegistry; import org.matsim.contrib.taxi.optimizer.rules.ZonalRegisters; import org.matsim.contrib.taxi.run.TaxiConfigGroup; -import org.matsim.contrib.common.zones.systems.grid.SquareGridSystem; +import org.matsim.contrib.common.zones.systems.grid.SquareGridZoneSystem; import org.matsim.core.api.experimental.events.EventsManager; import org.matsim.core.mobsim.framework.MobsimTimer; import org.matsim.core.router.speedy.SpeedyALTFactory; @@ -100,7 +100,7 @@ public TaxiOptimizer get() { } private ZonalRegisters createZonalRegisters(RuleBasedTaxiOptimizerParams params) { - ZoneSystem zoneSystem = new SquareGridSystem(network, params.cellSize); + ZoneSystem zoneSystem = new SquareGridZoneSystem(network, params.cellSize); IdleTaxiZonalRegistry idleTaxiRegistry = new IdleTaxiZonalRegistry(zoneSystem, eScheduler.getScheduleInquiry()); UnplannedRequestZonalRegistry unplannedRequestRegistry = new UnplannedRequestZonalRegistry(zoneSystem); diff --git a/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/DefaultTaxiOptimizerProvider.java b/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/DefaultTaxiOptimizerProvider.java index 66f8eecb6fb..1033f1cee4d 100644 --- a/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/DefaultTaxiOptimizerProvider.java +++ b/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/DefaultTaxiOptimizerProvider.java @@ -39,7 +39,7 @@ import org.matsim.contrib.taxi.optimizer.zonal.ZonalTaxiOptimizerParams; import org.matsim.contrib.taxi.run.TaxiConfigGroup; import org.matsim.contrib.taxi.scheduler.TaxiScheduler; -import org.matsim.contrib.common.zones.systems.grid.SquareGridSystem; +import org.matsim.contrib.common.zones.systems.grid.SquareGridZoneSystem; import org.matsim.core.api.experimental.events.EventsManager; import org.matsim.core.mobsim.framework.MobsimTimer; import org.matsim.core.router.util.TravelDisutility; @@ -116,7 +116,7 @@ yield new RuleBasedTaxiOptimizer(eventsManager, taxiCfg, fleet, scheduler, sched } private ZonalRegisters createZonalRegisters(RuleBasedTaxiOptimizerParams params) { - ZoneSystem zoneSystem = new SquareGridSystem(network, params.cellSize); + ZoneSystem zoneSystem = new SquareGridZoneSystem(network, params.cellSize); IdleTaxiZonalRegistry idleTaxiRegistry = new IdleTaxiZonalRegistry(zoneSystem, scheduler.getScheduleInquiry()); UnplannedRequestZonalRegistry unplannedRequestRegistry = new UnplannedRequestZonalRegistry(zoneSystem); return new ZonalRegisters(idleTaxiRegistry, unplannedRequestRegistry); diff --git a/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/rules/IdleTaxiZonalRegistry.java b/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/rules/IdleTaxiZonalRegistry.java index 05b0b3b6ad8..5cde6234784 100644 --- a/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/rules/IdleTaxiZonalRegistry.java +++ b/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/rules/IdleTaxiZonalRegistry.java @@ -34,7 +34,7 @@ import org.matsim.contrib.dvrp.schedule.ScheduleInquiry; import org.matsim.contrib.dvrp.schedule.Schedules; import org.matsim.contrib.taxi.schedule.TaxiStayTask; -import org.matsim.contrib.common.zones.ZonalSystems; +import org.matsim.contrib.common.zones.ZoneSystems; public class IdleTaxiZonalRegistry { private final ScheduleInquiry scheduleInquiry; @@ -49,7 +49,7 @@ public IdleTaxiZonalRegistry(ZoneSystem zoneSystem, ScheduleInquiry scheduleInqu this.scheduleInquiry = scheduleInquiry; this.zoneSystem = zoneSystem; - zonesSortedByDistance = ZonalSystems.initZonesByDistance(zoneSystem.getZones()); + zonesSortedByDistance = ZoneSystems.initZonesByDistance(zoneSystem.getZones()); for (Id id : zoneSystem.getZones().keySet()) { vehiclesInZones.put(id, new LinkedHashMap<>());//LinkedHashMap to preserve iteration order @@ -91,7 +91,7 @@ public Stream findNearestVehicles(Node node, int minCount, Predicat return minCount >= vehicles.size() ? vehicles.values().stream().filter(idleVehicleFilter) : - zonesSortedByDistance.get(zoneSystem.getZoneForNodeId(node).getId()) + zonesSortedByDistance.get(zoneSystem.getZoneForNodeId(node).orElseThrow().getId()) .stream() .flatMap(z -> vehiclesInZones.get(z.getId()).values().stream()) .filter(idleVehicleFilter) @@ -99,7 +99,7 @@ public Stream findNearestVehicles(Node node, int minCount, Predicat } private Id getZoneId(TaxiStayTask stayTask) { - return zoneSystem.getZoneForLinkId(stayTask.getLink().getId()).getId(); + return zoneSystem.getZoneForLinkId(stayTask.getLink().getId()).orElseThrow().getId(); } public Stream vehicles() { diff --git a/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/rules/UnplannedRequestZonalRegistry.java b/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/rules/UnplannedRequestZonalRegistry.java index 84c0ea97740..60d73d1f00d 100644 --- a/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/rules/UnplannedRequestZonalRegistry.java +++ b/contribs/taxi/src/main/java/org/matsim/contrib/taxi/optimizer/rules/UnplannedRequestZonalRegistry.java @@ -31,7 +31,7 @@ import org.matsim.contrib.common.zones.ZoneSystem; import org.matsim.contrib.drt.passenger.DrtRequest; import org.matsim.contrib.dvrp.optimizer.Request; -import org.matsim.contrib.common.zones.ZonalSystems; +import org.matsim.contrib.common.zones.ZoneSystems; public class UnplannedRequestZonalRegistry { private final ZoneSystem zoneSystem; @@ -42,7 +42,7 @@ public class UnplannedRequestZonalRegistry { public UnplannedRequestZonalRegistry(ZoneSystem zoneSystem) { this.zoneSystem = zoneSystem; - zonesSortedByDistance = ZonalSystems.initZonesByDistance(zoneSystem.getZones()); + zonesSortedByDistance = ZoneSystems.initZonesByDistance(zoneSystem.getZones()); for (Id id : zoneSystem.getZones().keySet()) { requestsInZones.put(id, new LinkedHashMap<>());//LinkedHashMap to preserve iteration order @@ -72,14 +72,14 @@ public void removeRequest(DrtRequest request) { } public Stream findNearestRequests(Node node, int minCount) { - return zonesSortedByDistance.get(zoneSystem.getZoneForNodeId(node).getId()) + return zonesSortedByDistance.get(zoneSystem.getZoneForNodeId(node).orElseThrow().getId()) .stream() .flatMap(z -> requestsInZones.get(z.getId()).values().stream()) .limit(minCount); } private Id getZoneId(DrtRequest request) { - return zoneSystem.getZoneForNodeId(request.getFromLink().getFromNode()).getId(); + return zoneSystem.getZoneForNodeId(request.getFromLink().getFromNode()).orElseThrow().getId(); } public int getRequestCount() {