From d10e7b2aa426ba944189d6db6f48e6b8325908a9 Mon Sep 17 00:00:00 2001 From: steffenaxer <26229392+steffenaxer@users.noreply.github.com> Date: Sat, 23 Nov 2024 15:27:22 +0100 Subject: [PATCH 1/2] Improve SquareGridZoneSystem performance --- .../grid/square/SquareGridZoneSystem.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/square/SquareGridZoneSystem.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/square/SquareGridZoneSystem.java index b1319fed1a9..f96e7427b81 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/square/SquareGridZoneSystem.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/square/SquareGridZoneSystem.java @@ -36,6 +36,7 @@ import java.util.*; import java.util.function.Predicate; +import java.util.stream.Collectors; public class SquareGridZoneSystem implements GridZoneSystem { @@ -55,6 +56,7 @@ public class SquareGridZoneSystem implements GridZoneSystem { private final IdMap zones = new IdMap<>(Zone.class); private final IdMap> zoneToLinksMap = new IdMap<>(Zone.class); + private final Map> index2Link; private final Network network; @@ -76,6 +78,7 @@ public SquareGridZoneSystem(Network network, double cellSize, boolean filterByNe this.rows = Math.max(1, (int) Math.ceil((maxY - minY) / cellSize)); this.cols = Math.max(1, (int)Math.ceil((maxX - minX) / cellSize)); this.internalZones = new Zone[rows * cols +1]; + this.index2Link = getIndexToLink(network); if(filterByNetwork) { network.getLinks().values().forEach(l -> getOrCreateZone(l.getToNode().getCoord())); @@ -126,13 +129,7 @@ private Optional getOrCreateZone(Coord coord) { if(zoneFilter.test(zone)) { internalZones[index] = zone; zones.put(zone.getId(), zone); - - for (Link link : network.getLinks().values()) { - if (getIndex(link.getToNode().getCoord()) == index) { - List links = zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId -> new ArrayList<>()); - links.add(link); - } - } + zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId -> new ArrayList<>()).addAll(index2Link.get(index)); } else { return Optional.empty(); } @@ -140,6 +137,11 @@ private Optional getOrCreateZone(Coord coord) { return Optional.of(zone); } + private Map> getIndexToLink(Network network) { + return network.getLinks().values().stream() + .collect(Collectors.groupingBy(link -> getIndex(link.getToNode().getCoord()))); + } + private PreparedPolygon getGeometry(int r, int c) { List coords = new ArrayList<>(); coords.add(new Coord(minX + c * cellSize, minY + r * cellSize)); From f3ab912fbdba4d078793aa1f2f39d3b85cfff1c6 Mon Sep 17 00:00:00 2001 From: steffenaxer <26229392+steffenaxer@users.noreply.github.com> Date: Sat, 23 Nov 2024 15:51:20 +0100 Subject: [PATCH 2/2] Bugfix --- .../systems/grid/square/SquareGridZoneSystem.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/square/SquareGridZoneSystem.java b/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/square/SquareGridZoneSystem.java index f96e7427b81..f47ed5669c9 100644 --- a/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/square/SquareGridZoneSystem.java +++ b/contribs/common/src/main/java/org/matsim/contrib/common/zones/systems/grid/square/SquareGridZoneSystem.java @@ -56,7 +56,7 @@ public class SquareGridZoneSystem implements GridZoneSystem { private final IdMap zones = new IdMap<>(Zone.class); private final IdMap> zoneToLinksMap = new IdMap<>(Zone.class); - private final Map> index2Link; + private final Map> index2Links; private final Network network; @@ -78,7 +78,7 @@ public SquareGridZoneSystem(Network network, double cellSize, boolean filterByNe this.rows = Math.max(1, (int) Math.ceil((maxY - minY) / cellSize)); this.cols = Math.max(1, (int)Math.ceil((maxX - minX) / cellSize)); this.internalZones = new Zone[rows * cols +1]; - this.index2Link = getIndexToLink(network); + this.index2Links = getIndexToLink(network); if(filterByNetwork) { network.getLinks().values().forEach(l -> getOrCreateZone(l.getToNode().getCoord())); @@ -129,7 +129,12 @@ private Optional getOrCreateZone(Coord coord) { if(zoneFilter.test(zone)) { internalZones[index] = zone; zones.put(zone.getId(), zone); - zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId -> new ArrayList<>()).addAll(index2Link.get(index)); + List linkList = zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId -> new ArrayList<>()); + List links = index2Links.get(index); + if(links!=null) + { + linkList.addAll(links); + } } else { return Optional.empty(); }