Skip to content

Commit 4ad1e50

Browse files
authored
Improve error message of network link check (#3775)
* improve error message of network link check * add method to remove legs to clean population
1 parent 2acd683 commit 4ad1e50

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

contribs/application/src/main/java/org/matsim/application/prepare/population/CleanPopulation.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
import org.matsim.core.population.algorithms.PersonAlgorithm;
1010
import org.matsim.core.population.algorithms.TripsToLegsAlgorithm;
1111
import org.matsim.core.router.RoutingModeMainModeIdentifier;
12+
import org.matsim.core.router.TripStructureUtils;
1213
import picocli.CommandLine;
1314

1415
import java.nio.file.Files;
1516
import java.nio.file.Path;
17+
import java.util.List;
18+
import java.util.Optional;
19+
import java.util.Set;
1620

1721
/**
18-
* Removes information from a popoulation file.
22+
* Removes information from a population file.
1923
*
2024
* @author rakow
2125
*/
@@ -38,11 +42,17 @@ public class CleanPopulation implements MATSimAppCommand, PersonAlgorithm {
3842
@CommandLine.Option(names = "--remove-activity-location", description = "Remove link and facility from activities", defaultValue = "false")
3943
private boolean rmActivityLocations;
4044

45+
@CommandLine.Option(names = "--remove-activity-facilities", description = "Remove facility ids from activities", defaultValue = "false")
46+
private boolean rmActivityFacilities;
47+
4148
@CommandLine.Option(names = "--remove-routes", description = "Remove route information", defaultValue = "false")
4249
private boolean rmRoutes;
4350

44-
@CommandLine.Option(names = "--trips-to-legs", description = "Convert trips in older plan format to legs first (Removes routes implicitly).", defaultValue = "false")
45-
private boolean tripsToLegs;
51+
@CommandLine.Option(names = "--trips-to-legs", description = "Convert trips to single legs (Removes all routing and individual legs).", defaultValue = "false")
52+
private boolean tripsToLegs;
53+
54+
@CommandLine.Option(names = "--remove-legs", description = "Remove the legs of trips for given modes, e.g. pt", split = ",", defaultValue = "")
55+
private Set<String> rmLegs;
4656

4757
@CommandLine.Option(names = "--output", description = "Output file name", required = true)
4858
private Path output;
@@ -60,7 +70,7 @@ public Integer call() throws Exception {
6070

6171
Population population = PopulationUtils.readPopulation(plans.toString());
6272

63-
if (!rmRoutes && !rmActivityLocations && !rmUnselected && !tripsToLegs) {
73+
if (!rmRoutes && !rmActivityLocations && !rmUnselected && !tripsToLegs && !rmActivityFacilities && (rmLegs == null || rmLegs.isEmpty())) {
6474
log.error("None of the 'remove' commands specified.");
6575
return 2;
6676
}
@@ -87,6 +97,10 @@ public void run(Person person) {
8797
if (tripsToLegs)
8898
trips2Legs.run(plan);
8999

100+
if (rmLegs != null && !rmLegs.isEmpty()) {
101+
removeLegs(plan, rmLegs);
102+
}
103+
90104
for (PlanElement el : plan.getPlanElements()) {
91105
if (rmRoutes) {
92106
removeRouteFromLeg(el);
@@ -95,10 +109,44 @@ public void run(Person person) {
95109
if (rmActivityLocations) {
96110
removeActivityLocation(el);
97111
}
112+
113+
if (rmActivityFacilities && el instanceof Activity act) {
114+
act.setFacilityId(null);
115+
}
98116
}
99117
}
100118
}
101119

120+
/**
121+
* Remove the legs of trips that contain any of the given modes.
122+
*/
123+
public static void removeLegs(Plan plan, Set<String> modes) {
124+
125+
final List<PlanElement> planElements = plan.getPlanElements();
126+
127+
// Remove all pt trips
128+
for (TripStructureUtils.Trip trip : TripStructureUtils.getTrips(plan)) {
129+
130+
// Check if any of the modes is in the trip
131+
Optional<Leg> cleanLeg = trip.getLegsOnly().stream().filter(l -> modes.contains(l.getMode())).findFirst();
132+
133+
if (cleanLeg.isEmpty())
134+
continue;
135+
136+
// Replaces all trip elements and inserts single leg
137+
final List<PlanElement> fullTrip =
138+
planElements.subList(
139+
planElements.indexOf(trip.getOriginActivity()) + 1,
140+
planElements.indexOf(trip.getDestinationActivity()));
141+
142+
fullTrip.clear();
143+
144+
Leg leg = PopulationUtils.createLeg(cleanLeg.get().getMode());
145+
TripStructureUtils.setRoutingMode(leg, cleanLeg.get().getMode());
146+
fullTrip.add(leg);
147+
}
148+
}
149+
102150
/**
103151
* Remove link and facility information from activity.
104152
*/
@@ -128,4 +176,6 @@ public static void removeUnselectedPlans(Person person) {
128176
person.removePlan(plan);
129177
}
130178
}
179+
180+
131181
}

matsim/src/main/java/org/matsim/core/population/algorithms/PersonPrepareForSim.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.apache.logging.log4j.LogManager;
2424
import org.apache.logging.log4j.Logger;
25+
import org.matsim.api.core.v01.Id;
2526
import org.matsim.api.core.v01.Scenario;
2627
import org.matsim.api.core.v01.TransportMode;
2728
import org.matsim.api.core.v01.network.Link;
@@ -204,13 +205,15 @@ private void checkModeConsistent(Person person, Leg leg) {
204205
return;
205206
}
206207

207-
Optional<? extends Link> inconsistentLink = networkRoute.getLinkIds().stream()
208-
.map(l -> scenario.getNetwork().getLinks().get(l))
209-
.filter(l -> !l.getAllowedModes().contains(leg.getMode()))
208+
Optional<Id<Link>> inconsistentLink = networkRoute.getLinkIds().stream()
209+
.filter(linkId -> {
210+
Link link = scenario.getNetwork().getLinks().get(linkId);
211+
return link == null || !link.getAllowedModes().contains(leg.getMode());
212+
})
210213
.findFirst();
211214

212215
if (inconsistentLink.isPresent()) {
213-
String errorMessage = "Route inconsistent with link modes for: Link: " + inconsistentLink.get() + " Person " + person.getId() + "; Leg '" + leg + "'";
216+
String errorMessage = "Route inconsistent with link modes for link: " + inconsistentLink.get() + " Person " + person.getId() + "; Leg '" + leg + "'";
214217
log.error(errorMessage + "\n Consider cleaning inconsistent routes by using PopulationUtils.checkRouteModeAndReset()." +
215218
"\n If this is intended, set the routing config parameter 'networkRouteConsistencyCheck' to 'disable'.");
216219
throw new RuntimeException(errorMessage);

0 commit comments

Comments
 (0)