MATSIM
Public Member Functions | Private Attributes | List of all members
org.matsim.core.network.algorithms.TransportModeNetworkFilter Class Reference

Public Member Functions

 TransportModeNetworkFilter (final Network fullNetwork)
 
void filter (final Network subNetwork, final Set< String > extractModes)
 

Private Attributes

final Network fullNetwork
 

Detailed Description

This class extracts a subnetwork from a given network containing only those links where at least one of the given transport modes are allowed. The resulting network will not contain any links where none of the specified transport modes are allowed. In addition, all links in the resulting network will have at most those modes specified for the extraction, additional modes are removed from the allowed set for each link.
This class makes no guarantee that the resulting network is strongly connected, not even when the input network was strongly connected.

Author
mrieser

Definition at line 50 of file TransportModeNetworkFilter.java.

Constructor & Destructor Documentation

◆ TransportModeNetworkFilter()

org.matsim.core.network.algorithms.TransportModeNetworkFilter.TransportModeNetworkFilter ( final Network  fullNetwork)

Member Function Documentation

◆ filter()

void org.matsim.core.network.algorithms.TransportModeNetworkFilter.filter ( final Network  subNetwork,
final Set< String >  extractModes 
)

Extracts a subnetwork containing only links with the specified modes.

I had to extend this method in order to keep the nodes in the same order as in the input network. Otherwise, some router tests failed since for some from-to-pairs, multiple routes with the same costs were found. In that case, the outcome depends on the order of the nodes and links in the network. This problem might occur also in other places, therefore, I fixed it here. cdobler, sep'17

Time-varying networks did not copy time-dependent information. This functionality is included now. sebhoerl, aug'24

Parameters
subNetworkthe network object where to store the extracted subnetwork
extractModesset of modes that should be contained in the subnetwork

Definition at line 74 of file TransportModeNetworkFilter.java.

References org.matsim.api.core.v01.IdSet< T >.add(), org.matsim.api.core.v01.network.Network.addLink(), org.matsim.api.core.v01.network.Network.addNode(), org.matsim.api.core.v01.IdSet< T >.contains(), org.matsim.utils.objectattributes.attributable.AttributesUtils.copyAttributesFromTo(), org.matsim.core.network.turnRestrictions.DisallowedNextLinks.copyOnlyModes(), org.matsim.api.core.v01.network.NetworkFactory.createLink(), org.matsim.api.core.v01.network.NetworkFactory.createNode(), org.matsim.core.network.NetworkUtils.getDisallowedNextLinks(), org.matsim.api.core.v01.network.Network.getFactory(), org.matsim.api.core.v01.network.Network.getLinks(), org.matsim.core.network.TimeDependentNetwork.getNetworkChangeEvents(), org.matsim.api.core.v01.network.Network.getNodes(), org.matsim.core.network.NetworkUtils.getType(), org.matsim.api.core.v01.network.Network.removeNode(), org.matsim.api.core.v01.IdSet< T >.retainAll(), org.matsim.api.core.v01.network.Link.setAllowedModes(), org.matsim.api.core.v01.network.Link.setCapacity(), org.matsim.core.network.NetworkUtils.setDisallowedNextLinks(), org.matsim.api.core.v01.network.Link.setFreespeed(), org.matsim.api.core.v01.network.Link.setLength(), org.matsim.api.core.v01.network.Link.setNumberOfLanes(), and org.matsim.core.network.NetworkUtils.setType().

Referenced by org.matsim.core.router.SingleModeNetworksCache.filterNetwork(), org.matsim.core.router.LinkToLinkRouting.get(), org.matsim.core.population.algorithms.PersonPrepareForSim.PersonPrepareForSim(), org.matsim.core.controler.PrepareForMobsimImpl.run(), and org.matsim.core.controler.PrepareForSimImpl.run().

74  {
75  NetworkFactory factory = subNetwork.getFactory();
76 
77  // first, clone all nodes to ensure their order is not changed
78  for (Node node : this.fullNetwork.getNodes().values()) {
79  Node newNode = factory.createNode(node.getId(), node.getCoord());
80  AttributesUtils.copyAttributesFromTo(node, newNode);
81  subNetwork.addNode(newNode);
82  }
83 
84  // second, create clones of the links allowing the extracted modes
85  IdSet<Node> nodesToInclude = new IdSet<>(Node.class);
86  for (Link link : this.fullNetwork.getLinks().values()) {
87  Set<String> intersection = new HashSet<>(extractModes);
88  intersection.retainAll(link.getAllowedModes());
89  if (intersection.size() > 0) {
90  Id<Node> fromId = link.getFromNode().getId();
91  Id<Node> toId = link.getToNode().getId();
92  Node fromNode2 = subNetwork.getNodes().get(fromId);
93  Node toNode2 = subNetwork.getNodes().get(toId);
94  nodesToInclude.add(fromId);
95  nodesToInclude.add(toId);
96 
97  Link link2 = factory.createLink(link.getId(), fromNode2, toNode2);
98  link2.setAllowedModes(intersection);
99  link2.setCapacity(link.getCapacity());
100  link2.setFreespeed(link.getFreespeed());
101  link2.setLength(link.getLength());
102  link2.setNumberOfLanes(link.getNumberOfLanes());
103  NetworkUtils.setType(link2, NetworkUtils.getType(link));
104  AttributesUtils.copyAttributesFromTo(link, link2);
105  subNetwork.addLink(link2);
106 
107  DisallowedNextLinks disallowedNextLinks = NetworkUtils.getDisallowedNextLinks(link);
108  if (disallowedNextLinks != null) {
109  NetworkUtils.setDisallowedNextLinks(link2, disallowedNextLinks.copyOnlyModes(extractModes));
110  }
111  }
112  }
113 
114  // third, remove all nodes that are not used by the valid links
115  IdSet<Node> nodesToRemove = new IdSet<>(Node.class);
116  for (Node node : this.fullNetwork.getNodes().values()) {
117  if (!nodesToInclude.contains(node.getId())) nodesToRemove.add(node.getId());
118  }
119  for (Id<Node> nodeId : nodesToRemove) subNetwork.removeNode(nodeId);
120 
121  // fourth, recover the network change events
122  if (fullNetwork instanceof TimeDependentNetwork) {
123  TimeDependentNetwork fullTimeDependentNetwork = (TimeDependentNetwork) fullNetwork;
124 
125  if (fullTimeDependentNetwork.getNetworkChangeEvents().size() > 0) {
126  if (!(subNetwork instanceof TimeDependentNetwork)) {
127  throw new RuntimeException("Filtering time-dependent network with change events into a static network. Information on change events would be lost!");
128  } else {
129  TimeDependentNetwork timeDependentSubNetwork = (TimeDependentNetwork) subNetwork;
130 
131  for (NetworkChangeEvent event : fullTimeDependentNetwork.getNetworkChangeEvents()) {
132  NetworkChangeEvent subNetworkEvent = new NetworkChangeEvent(event.getStartTime());
133 
134  for (Link link : event.getLinks()) {
135  Link subNetworkLink = subNetwork.getLinks().get(link.getId());
136 
137  if (subNetworkLink != null) {
138  subNetworkEvent.addLink(subNetworkLink);
139  }
140  }
141 
142  subNetworkEvent.setFlowCapacityChange(event.getFlowCapacityChange());
143  subNetworkEvent.setFreespeedChange(event.getFreespeedChange());
144  subNetworkEvent.setLanesChange(event.getLanesChange());
145 
146  if (subNetwork.getLinks().size() > 0) {
147  timeDependentSubNetwork.addNetworkChangeEvent(subNetworkEvent);
148  }
149  }
150  }
151  }
152  }
153  }
Map< Id< Node >, ? extends Node > getNodes()
Map< Id< Link >, ? extends Link > getLinks()
Here is the call graph for this function:

Member Data Documentation

◆ fullNetwork

final Network org.matsim.core.network.algorithms.TransportModeNetworkFilter.fullNetwork
private

The documentation for this class was generated from the following file: