From 192e3f9c14970a891449a607527594706e76da76 Mon Sep 17 00:00:00 2001 From: nkuehnel Date: Mon, 30 Dec 2024 15:30:42 +0100 Subject: [PATCH] merge disallowed links: catch case where restricted target link is replaced --- .../algorithms/NetworkMergeDoubleLinks.java | 88 ++++++++++++------- 1 file changed, 55 insertions(+), 33 deletions(-) diff --git a/matsim/src/main/java/org/matsim/core/network/algorithms/NetworkMergeDoubleLinks.java b/matsim/src/main/java/org/matsim/core/network/algorithms/NetworkMergeDoubleLinks.java index 30728ade18c..183eca29e45 100644 --- a/matsim/src/main/java/org/matsim/core/network/algorithms/NetworkMergeDoubleLinks.java +++ b/matsim/src/main/java/org/matsim/core/network/algorithms/NetworkMergeDoubleLinks.java @@ -105,7 +105,6 @@ private void mergeLink2IntoLink1(Link link1, Link link2, Network network) { link1.setFreespeed(fs); link1.setNumberOfLanes(lanes); link1.setLength(length); - mergeTurnRestrictions(link1, link2); network.removeLink(link2.getId()); } break; @@ -124,7 +123,6 @@ private void mergeLink2IntoLink1(Link link1, Link link2, Network network) { link1.setFreespeed(fs); link1.setNumberOfLanes(lanes); link1.setLength(length); - mergeTurnRestrictions(link1, link2); network.removeLink(link2.getId()); } break; @@ -133,32 +131,6 @@ private void mergeLink2IntoLink1(Link link1, Link link2, Network network) { } } - private static void mergeTurnRestrictions(Link link1, Link link2) { - DisallowedNextLinks disallowedNextLinks1 = NetworkUtils.getDisallowedNextLinks(link1); - DisallowedNextLinks disallowedNextLinks2 = NetworkUtils.getDisallowedNextLinks(link2); - if (disallowedNextLinks1 != null) { - if (disallowedNextLinks2 != null) { - Map>>> perMode1 = disallowedNextLinks1.getAsMap(); - Map>>> perMode2 = disallowedNextLinks2.getAsMap(); - Map>>> merged = new HashMap<>(); - for (Map.Entry>>> restrictionsPerMode : perMode1.entrySet()) { - if (perMode2.containsKey(restrictionsPerMode.getKey())) { - for (List> forbiddenSequence : restrictionsPerMode.getValue()) { - Id target = forbiddenSequence.getLast(); - if (perMode2.get(restrictionsPerMode.getKey()).stream().anyMatch(ls -> ls.getLast().equals(target))) { - merged.computeIfAbsent(restrictionsPerMode.getKey(), k -> new ArrayList<>()).add(forbiddenSequence); - } - } - } - } - disallowedNextLinks1.clear(); - merged.forEach((mode, sequences) -> sequences.forEach(sequence -> disallowedNextLinks1.addDisallowedLinkSequence(mode, sequence))); - } else { - NetworkUtils.removeDisallowedNextLinks(link1); - } - } - } - ////////////////////////////////////////////////////////////////////// // run methods ////////////////////////////////////////////////////////////////////// @@ -166,7 +138,7 @@ private static void mergeTurnRestrictions(Link link1, Link link2) { @Override public void run(Network network) { - Map, Id> replacedLinks = new HashMap<>(); + Map, Replacement> replacedLinks = new HashMap<>(); for (Node n : network.getNodes().values()) { Iterator l1_it = n.getOutLinks().values().iterator(); while (l1_it.hasNext()) { @@ -181,7 +153,7 @@ public void run(Network network) { } this.mergeLink2IntoLink1(l1, l2, network); - replacedLinks.put(l2.getId(), l1.getId()); + replacedLinks.put(l2.getId(), new Replacement(l2, l1)); // restart l1_it = n.getOutLinks().values().iterator(); l2_it = n.getOutLinks().values().iterator(); @@ -191,10 +163,15 @@ public void run(Network network) { } } - copyAndReplaceTurnRestrictions(network, replacedLinks); + handleTurnRestrictions(network, replacedLinks); } - private static void copyAndReplaceTurnRestrictions(Network network, Map, Id> replacedLinks) { + private void handleTurnRestrictions(Network network, Map, Replacement> replacedLinks) { + + for (Replacement mergedLinks : replacedLinks.values()) { + mergeTurnRestrictions(mergedLinks.substitute, mergedLinks.orginal, replacedLinks); + } + for (Link link : network.getLinks().values()) { DisallowedNextLinks disallowedNextLinks = NetworkUtils.getDisallowedNextLinks(link); @@ -208,7 +185,11 @@ private static void copyAndReplaceTurnRestrictions(Network network, Map for (List> restriction : restrictions.getValue()) { List> restrictionCopy = new ArrayList<>(); for (Id linkId : restriction) { - restrictionCopy.add(replacedLinks.getOrDefault(linkId, linkId)); + if(replacedLinks.containsKey(linkId)) { + restrictionCopy.add(replacedLinks.get(linkId).substitute.getId()); + } else { + restrictionCopy.add(linkId); + } } restrictionsCopy.add(restrictionCopy); } @@ -226,4 +207,45 @@ private static void copyAndReplaceTurnRestrictions(Network network, Map Verify.verify(DisallowedNextLinksUtils.isValid(network)); } + + private static void mergeTurnRestrictions(Link link1, Link link2, Map, Replacement> replacedLinks) { + DisallowedNextLinks disallowedNextLinks1 = NetworkUtils.getDisallowedNextLinks(link1); + DisallowedNextLinks disallowedNextLinks2 = NetworkUtils.getDisallowedNextLinks(link2); + if (disallowedNextLinks1 != null) { + if (disallowedNextLinks2 != null) { + + Map>>> perMode1 = disallowedNextLinks1.getAsMap(); + Map>>> perMode2 = disallowedNextLinks2.getAsMap(); + Map>>> merged = new HashMap<>(); + + for (Map.Entry>>> restrictionsPerMode : perMode1.entrySet()) { + if (perMode2.containsKey(restrictionsPerMode.getKey())) { + for (List> forbiddenSequence : restrictionsPerMode.getValue()) { + Id target = forbiddenSequence.getLast(); + if(replacedLinks.containsKey(target)) { + target = replacedLinks.get(target).substitute.getId(); + } + + for (List> forbiddenSequence2 : perMode2.get(restrictionsPerMode.getKey())) { + Id target2 = forbiddenSequence2.getLast(); + if(replacedLinks.containsKey(target2)) { + target2 = replacedLinks.get(target2).substitute.getId(); + } + if(target.equals(target2)) { + merged.computeIfAbsent(restrictionsPerMode.getKey(), k -> new ArrayList<>()).add(forbiddenSequence); + } + } + } + } + } + disallowedNextLinks1.clear(); + merged.forEach((mode, sequences) -> sequences.forEach(sequence -> disallowedNextLinks1.addDisallowedLinkSequence(mode, sequence))); + } else { + NetworkUtils.removeDisallowedNextLinks(link1); + } + } + } + + private record Replacement(Link orginal, Link substitute){} + }