Skip to content

Commit

Permalink
Merge pull request #3712 from moia-oss/updateTurnRestrictionsNetworkC…
Browse files Browse the repository at this point in the history
…leaner

Update turn restrictions network cleaner
  • Loading branch information
nkuehnel authored Feb 7, 2025
2 parents c8fc702 + b75a0dc commit 07604db
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.matsim.core.utils.geometry.CoordUtils;
import org.matsim.core.utils.geometry.CoordinateTransformation;
import org.matsim.core.utils.misc.OptionalTime;
import org.matsim.utils.objectattributes.attributable.AttributesUtils;

/**
* Contains several helper methods for working with {@link Network networks}.
Expand Down Expand Up @@ -1039,6 +1040,10 @@ public static void removeDisallowedNextLinks(Link link) {
link.getAttributes().removeAttribute(DISALLOWED_NEXT_LINKS_ATTRIBUTE);
}

public static void copyAttributesExceptDisallowedNextLinks(Link from, Link to) {
AttributesUtils.copyAttributesFromToExcept(from, to, DISALLOWED_NEXT_LINKS_ATTRIBUTE);
}

public static void addAllowedMode(Link link, String mode) {
Set<String> modes = new HashSet<>(link.getAllowedModes());
modes.add(mode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Verify;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Identifiable;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.network.Node;
Expand Down Expand Up @@ -140,6 +141,8 @@ private void checkRealLinkExistence(Network network, TurnRestrictionsContext.Col
link.getCapacity(),
link.getNumberOfLanes()
);
// copy all attributes except initial turn restrictions
NetworkUtils.copyAttributesExceptDisallowedNextLinks(coloredLink.link, linkCopy);
network.addLink(linkCopy);
}
}
Expand All @@ -157,7 +160,7 @@ private void reapplyRestrictions(Network network, TurnRestrictionsContext turnRe
}

private void advance(TurnRestrictionsContext.ColoredLink coloredLink, List<Id<Link>> currentPath,
Link replacedStartLink, Network network, TurnRestrictionsContext turnRestrictions) {
Link replacedStartLink, Network network, TurnRestrictionsContext turnRestrictions) {

if (!network.getLinks().containsKey(coloredLink.link.getId())) {
// link sequence is not part of the network anymore and doesn't need to be explored.
Expand All @@ -166,23 +169,32 @@ private void advance(TurnRestrictionsContext.ColoredLink coloredLink, List<Id<Li

if (coloredLink.toColoredNode != null) {
Node node = coloredLink.toColoredNode.node();
Set<Link> unrestrictedReachableLinks = new HashSet<>(node.getOutLinks().values()

// set of reachable links from the original node
// use id, as the link object may have been deleted and re-inserted as a copy during the process
Set<Id<Link>> unrestrictedReachableLinks = new HashSet<>(node.getOutLinks().values()
.stream()
.filter(link -> network.getLinks().containsKey(link.getId()))
.map(Identifiable::getId)
.filter(link -> network.getLinks().containsKey(link))
.collect(Collectors.toSet()));


List<TurnRestrictionsContext.ColoredLink> toAdvance = new ArrayList<>();
for (TurnRestrictionsContext.ColoredLink outLink : coloredLink.toColoredNode.outLinks()) {
unrestrictedReachableLinks.remove(outLink.link);
// remove from the set all links that are also reachable from within the colored subgraph
unrestrictedReachableLinks.remove(outLink.link.getId());
if (outLink.toColoredNode != null) {
toAdvance.add(outLink);
}
}

// any remaining link is _not_ reachable from within the colored subgraph
// -> there must be a turn restriction from the current start link across the path
if (!unrestrictedReachableLinks.isEmpty()) {
DisallowedNextLinks disallowedNextLinks = NetworkUtils.getOrCreateDisallowedNextLinks(replacedStartLink);
for (Link unrestrictedReachableLink : unrestrictedReachableLinks) {
for (Id<Link> unrestrictedReachableLink : unrestrictedReachableLinks) {
List<Id<Link>> path = new ArrayList<>(currentPath);
path.add(unrestrictedReachableLink.getId());
path.add(unrestrictedReachableLink);
disallowedNextLinks.addDisallowedLinkSequence("car", path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public static void copyTo( Attributes from , Attributes to ) {
}
}

/**
* Adds the mappings from "from" to "to". Nothing is done to copy the Object
* themselves, which should be fine for 99.9% of the usecases of Attributes
* (value objects).
*
* @param from
* @param to
* @param exceptAttribute does not copy attribute with that name
*/
public static void copyToExcept(Attributes from, Attributes to, String exceptAttribute) {
for (var entry : from.getAsMap().entrySet()) {
String key = entry.getKey();
if (!key.equals(exceptAttribute)) {
to.putAttribute(key, entry.getValue());
}
}
}

/**
* Adds the mappings from "from" to "to". Nothing is done to copy the Object themselves,
* which should be fine for 99.9% of the usecases of Attributes (value objects)
Expand All @@ -46,6 +64,19 @@ public static <T extends Attributable> void copyAttributesFromTo( T from , T to
copyTo( from.getAttributes() , to.getAttributes() );
}

/**
* Adds the mappings from "from" to "to". Nothing is done to copy the Object
* themselves, which should be fine for 99.9% of the usecases of Attributes
* (value objects).
*
* @param from
* @param to
* @param exceptAttribute does not copy attribute with that name
*/
public static <T extends Attributable> void copyAttributesFromToExcept(T from, T to, String exceptAttribute) {
copyToExcept(from.getAttributes(), to.getAttributes(), exceptAttribute);
}

/**
* @param attributes collection of attributes
* @return <code>true</code> if the attributes collection does not contain any attribute
Expand Down

0 comments on commit 07604db

Please sign in to comment.