Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SquareGridZoneSystem performance #3578

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class SquareGridZoneSystem implements GridZoneSystem {

Expand All @@ -55,6 +56,7 @@ public class SquareGridZoneSystem implements GridZoneSystem {
private final IdMap<Zone, Zone> zones = new IdMap<>(Zone.class);

private final IdMap<Zone, List<Link>> zoneToLinksMap = new IdMap<>(Zone.class);
private final Map<Integer, List<Link>> index2Links;
private final Network network;


Expand All @@ -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.index2Links = getIndexToLink(network);

if(filterByNetwork) {
network.getLinks().values().forEach(l -> getOrCreateZone(l.getToNode().getCoord()));
Expand Down Expand Up @@ -126,12 +129,11 @@ private Optional<Zone> 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<Link> links = zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId -> new ArrayList<>());
links.add(link);
}
List<Link> linkList = zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId -> new ArrayList<>());
List<Link> links = index2Links.get(index);
if(links!=null)
{
linkList.addAll(links);
}
} else {
return Optional.empty();
Expand All @@ -140,6 +142,11 @@ private Optional<Zone> getOrCreateZone(Coord coord) {
return Optional.of(zone);
}

private Map<Integer, List<Link>> getIndexToLink(Network network) {
return network.getLinks().values().stream()
.collect(Collectors.groupingBy(link -> getIndex(link.getToNode().getCoord())));
}

private PreparedPolygon getGeometry(int r, int c) {
List<Coord> coords = new ArrayList<>();
coords.add(new Coord(minX + c * cellSize, minY + r * cellSize));
Expand Down
Loading