Skip to content

Commit

Permalink
merge disallowed links: catch case where restricted target link is re…
Browse files Browse the repository at this point in the history
…placed
  • Loading branch information
nkuehnel committed Dec 30, 2024
1 parent dc7502c commit 192e3f9
Showing 1 changed file with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -133,40 +131,14 @@ 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<String, List<List<Id<Link>>>> perMode1 = disallowedNextLinks1.getAsMap();
Map<String, List<List<Id<Link>>>> perMode2 = disallowedNextLinks2.getAsMap();
Map<String, List<List<Id<Link>>>> merged = new HashMap<>();
for (Map.Entry<String, List<List<Id<Link>>>> restrictionsPerMode : perMode1.entrySet()) {
if (perMode2.containsKey(restrictionsPerMode.getKey())) {
for (List<Id<Link>> forbiddenSequence : restrictionsPerMode.getValue()) {
Id<Link> 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
//////////////////////////////////////////////////////////////////////

@Override
public void run(Network network) {

Map<Id<Link>, Id<Link>> replacedLinks = new HashMap<>();
Map<Id<Link>, Replacement> replacedLinks = new HashMap<>();
for (Node n : network.getNodes().values()) {
Iterator<? extends Link> l1_it = n.getOutLinks().values().iterator();
while (l1_it.hasNext()) {
Expand All @@ -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();
Expand All @@ -191,10 +163,15 @@ public void run(Network network) {
}
}

copyAndReplaceTurnRestrictions(network, replacedLinks);
handleTurnRestrictions(network, replacedLinks);
}

private static void copyAndReplaceTurnRestrictions(Network network, Map<Id<Link>, Id<Link>> replacedLinks) {
private void handleTurnRestrictions(Network network, Map<Id<Link>, Replacement> replacedLinks) {

for (Replacement mergedLinks : replacedLinks.values()) {
mergeTurnRestrictions(mergedLinks.substitute, mergedLinks.orginal, replacedLinks);
}

for (Link link : network.getLinks().values()) {
DisallowedNextLinks disallowedNextLinks = NetworkUtils.getDisallowedNextLinks(link);

Expand All @@ -208,7 +185,11 @@ private static void copyAndReplaceTurnRestrictions(Network network, Map<Id<Link>
for (List<Id<Link>> restriction : restrictions.getValue()) {
List<Id<Link>> restrictionCopy = new ArrayList<>();
for (Id<Link> 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);
}
Expand All @@ -226,4 +207,45 @@ private static void copyAndReplaceTurnRestrictions(Network network, Map<Id<Link>

Verify.verify(DisallowedNextLinksUtils.isValid(network));
}

private static void mergeTurnRestrictions(Link link1, Link link2, Map<Id<Link>, Replacement> replacedLinks) {
DisallowedNextLinks disallowedNextLinks1 = NetworkUtils.getDisallowedNextLinks(link1);
DisallowedNextLinks disallowedNextLinks2 = NetworkUtils.getDisallowedNextLinks(link2);
if (disallowedNextLinks1 != null) {
if (disallowedNextLinks2 != null) {

Map<String, List<List<Id<Link>>>> perMode1 = disallowedNextLinks1.getAsMap();
Map<String, List<List<Id<Link>>>> perMode2 = disallowedNextLinks2.getAsMap();
Map<String, List<List<Id<Link>>>> merged = new HashMap<>();

for (Map.Entry<String, List<List<Id<Link>>>> restrictionsPerMode : perMode1.entrySet()) {
if (perMode2.containsKey(restrictionsPerMode.getKey())) {
for (List<Id<Link>> forbiddenSequence : restrictionsPerMode.getValue()) {
Id<Link> target = forbiddenSequence.getLast();
if(replacedLinks.containsKey(target)) {
target = replacedLinks.get(target).substitute.getId();
}

for (List<Id<Link>> forbiddenSequence2 : perMode2.get(restrictionsPerMode.getKey())) {
Id<Link> 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){}

}

0 comments on commit 192e3f9

Please sign in to comment.