20 package org.matsim.core.router;
23 import java.util.function.Predicate;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
69 public static List<Leg>
getLegs(
final List<? extends PlanElement> planElements ) {
70 final List<Leg> legs =
new ArrayList<>();
73 if ( !(pe instanceof
Leg) )
continue;
78 return Collections.unmodifiableList( legs );
86 stageActivityHandling);
90 final List<? extends PlanElement> planElements,
92 final List<Activity> activities =
new ArrayList<>();
95 if ( !(pe instanceof
Activity) )
continue;
96 final Activity act = (Activity) pe;
98 switch (stageActivityHandling) {
99 case StagesAsNormalActivities:
102 case ExcludeStageActivities:
113 return Collections.unmodifiableList( activities );
121 public static List<Trip>
getTrips(
final Plan plan,
final Predicate<String> isStageActivity) {
125 public static List<Trip>
getTrips(
final List<? extends PlanElement> planElements) {
131 final List<? extends PlanElement> planElements,
132 final Predicate<String> isStageActivity ) {
133 final List<Trip> trips =
new ArrayList<>();
135 int originActivityIndex = -1;
136 int currentIndex = -1;
140 if ( !(pe instanceof
Activity) )
continue;
141 final Activity act = (Activity) pe;
147 if ( isStageActivity.test( act.
getType() ) ) {
151 if ( currentIndex - originActivityIndex > 1 ) {
156 (Activity) planElements.get( originActivityIndex ),
161 Collections.unmodifiableList(
163 planElements.subList(
164 originActivityIndex + 1,
169 originActivityIndex = currentIndex;
172 return Collections.unmodifiableList( trips );
179 public static Collection<Subtour>
getSubtours(
final Plan plan,
double coordDistance) {
217 public static Collection<Subtour>
getSubtours(
final List<? extends PlanElement> planElements,
double coordDistance) {
233 public static Collection<Subtour>
getSubtours(
final Plan plan,
final Predicate<String> isStageActivity) {
238 final List<? extends PlanElement> planElements,
239 final Predicate<String> isStageActivity,
double coordDistance) {
245 final List<Subtour> subtours =
new ArrayList<>();
246 Object destinationId = null;
249 final List<Object> originIds =
new ArrayList<>();
250 final List<Trip> nonAllocatedTrips =
new ArrayList<>( trips );
252 for (
Trip trip : trips) {
253 final Object originId;
255 if (trip.getOriginActivity().getFacilityId() != null) {
256 originId = trip.getOriginActivity().getFacilityId();
257 }
else if (coordDistance > 0 && trip.getOriginActivity().getCoord() != null) {
258 originId = trip.getOriginActivity().getCoord();
260 originId = trip.getOriginActivity().getLinkId();
263 if ( originId == null ) {
264 throw new NullPointerException(
"Facility id, link id and coordinates for origin activity "+trip.getOriginActivity()+
268 if (destinationId != null && !originId.equals( destinationId )) {
269 throw new RuntimeException(
"unconsistent trip location sequence: "+destinationId+
" != "+originId );
272 if (trip.getDestinationActivity().getFacilityId() != null) {
273 destinationId = trip.getDestinationActivity().getFacilityId();
274 }
else if (coordDistance > 0 && trip.getDestinationActivity().getCoord() != null) {
275 destinationId = trip.getDestinationActivity().getCoord();
277 destinationId = trip.getDestinationActivity().getLinkId();
280 if ( destinationId == null ) {
281 throw new NullPointerException(
"Facility id, and link id and coordinates for destination activity "+trip.getDestinationActivity()+
285 originIds.add( originId );
287 int lastIdx = originIds.lastIndexOf(destinationId);
290 if (coordDistance > 0 && destinationId instanceof
Coord destinationCoord) {
291 for (
int i = originIds.size() - 1; i >= 0; i--) {
293 Object cmp = originIds.get(i);
294 if (cmp instanceof
Coord cmpCoord) {
305 final int subtourStartIndex = lastIdx;
306 final int subtourEndIndex = originIds.size();
308 final List<Trip> subtour =
new ArrayList<>( trips.subList( subtourStartIndex , subtourEndIndex ) );
309 nonAllocatedTrips.removeAll( subtour );
313 for (
int i=subtourStartIndex; i < subtourEndIndex; i++) {
314 originIds.set( i , null );
327 if (nonAllocatedTrips.size() != 0) {
335 new ArrayList<>( trips ),
339 return Collections.unmodifiableList( subtours );
343 final List<Subtour> subtours,
346 for (
Subtour existingSubtour : subtours) {
347 if ( existingSubtour.parent != null )
continue;
348 if ( existingSubtour.startIndex < newSubtour.
startIndex )
continue;
349 if ( existingSubtour.endIndex < newSubtour.
startIndex )
continue;
354 assert existingSubtour.startIndex < newSubtour.
endIndex;
355 assert existingSubtour.endIndex <= newSubtour.
endIndex;
357 existingSubtour.parent = newSubtour;
358 newSubtour.children.add( existingSubtour );
360 subtours.add( newSubtour );
382 public final static class Trip {
385 private final List<PlanElement>
trip;
389 final List<PlanElement> trip,
390 final Activity destinationActivity) {
397 private static List<Leg>
extractLegs(
final List<PlanElement> trip ) {
398 final List<Leg> legs =
new ArrayList<>();
401 if ( pe instanceof
Leg ) {
402 legs.add( (Leg) pe );
406 return Collections.unmodifiableList( legs );
434 return "{Trip: origin="+originActivity+
"; "+
436 "destination="+destinationActivity +
"; " +
441 public boolean equals(
final Object other) {
442 if ( !(other instanceof
Trip) )
return false;
444 final Trip otherTrip = (Trip) other;
446 otherTrip.
trip.equals( trip ) &&
452 return originActivity.hashCode() + trip.hashCode() + destinationActivity.hashCode();
467 final List<Subtour> children =
new ArrayList<>();
470 Subtour(
final List<Trip> trips,
final boolean isClosed) {
471 this( -1 , -1 , trips , isClosed );
476 final List<Trip> trips,
477 final boolean isClosed) {
478 this.startIndex = startIndex;
479 this.endIndex = endIndex;
480 this.trips = Collections.unmodifiableList( trips );
481 this.isClosed = isClosed;
489 final List<Trip> list =
new ArrayList<>();
491 for (
Trip t : trips) {
492 boolean isInChildSt =
false;
493 for (
Subtour child : children) {
494 if ( child.contains( t ) ) {
500 if ( !isInChildSt ) {
509 return trips.contains( t );
517 return Collections.unmodifiableList( children );
525 public boolean equals(
final Object other) {
526 if (other == null)
return false;
527 if ( !other.getClass().equals( getClass() ) )
return false;
529 return s.
trips.equals( trips ) &&
530 areChildrenCompatible( children , s.children ) &&
531 (s.parent == null ? parent == null : s.parent.
equals( parent )) &&
536 final List<Subtour> children2,
537 final List<Subtour> children3) {
538 return children2.size() == children3.size();
544 return trips.hashCode();
549 return "Subtour: "+trips.toString();
562 if ( currentPlanElement instanceof
Activity ) {
564 Gbl.
assertIf( isStageActivity.test( ((Activity)currentPlanElement).getType() ) ) ;
568 int index =
trip.getTripElements().indexOf( currentPlanElement ) ;
580 if ( activity.equals(
trip.getDestinationActivity() ) ) {
589 List<Trip> trips =
getTrips( plan ) ;
591 if (
trip.getOriginActivity().equals( activity ) ) {
615 if ( mode == null && tripElements.size()==1 ) {
616 mode = ((
Leg) tripElements.get(0)).getMode() ;
619 log.error(
"Could not find routing mode for trip " + tripElements);
Activity getDestinationActivity()
static Subtour getUnclosedRootSubtour(final Plan plan)
static String identifyMainMode(final List<? extends PlanElement > tripElements)
static String getRoutingMode(Leg leg)
Attributes getAttributes()
static Collection< Subtour > getSubtours(final List<? extends PlanElement > planElements, double coordDistance)
Collection< Subtour > getChildren()
static List< Leg > getLegs(final List<? extends PlanElement > planElements)
static List< Trip > getTrips(final List<? extends PlanElement > planElements)
static double calcEuclideanDistance(Coord coord, Coord other)
static void assertIf(boolean flag)
static List< Trip > getTrips(final Plan plan, final Predicate< String > isStageActivity)
Activity getOriginActivity()
List< Leg > getLegsOnly()
static boolean isStageActivityType(String activityType)
static List< Activity > getActivities(final Plan plan, final StageActivityHandling stageActivityHandling)
static Collection< Subtour > getSubtours(final List<? extends PlanElement > planElements, final Predicate< String > isStageActivity, double coordDistance)
static List< Activity > getActivities(final List<? extends PlanElement > planElements, final StageActivityHandling stageActivityHandling)
static MainModeIdentifier getRoutingModeIdentifier()
static Trip findCurrentTrip(PlanElement pe, Plan plan)
OptionalTime getDepartureTime()
boolean contains(final Trip t)
static Trip findTripAtPlanElement(PlanElement currentPlanElement, Plan plan, Predicate< String > isStageActivity)
static Trip findTripEndingAtActivity(Activity activity, Plan plan)
static final String routingMode
static void setRoutingMode(Leg leg, String mode)
Attributes getTripAttributes()
static final boolean isStageActivity(final String activityType)
final Activity originActivity
static boolean areChildrenCompatible(final List< Subtour > children2, final List< Subtour > children3)
boolean equals(final Object other)
static List< Trip > getTrips(final Plan plan)
List< PlanElement > getPlanElements()
List< Trip > getTripsWithoutSubSubtours()
static Collection< Subtour > getSubtours(final Plan plan)
void setRoutingMode(String routingMode)
static final String NOT_IMPLEMENTED
static List< Leg > extractLegs(final List< PlanElement > trip)
boolean equals(final Object other)
static Trip findTripAtPlanElement(PlanElement currentPlanElement, Plan plan)
static Collection< Subtour > getSubtours(final Plan plan, final Predicate< String > isStageActivity)
static String createStageActivityType(String mode)
static String createStageActivityType(String mode)
Subtour(final int startIndex, final int endIndex, final List< Trip > trips, final boolean isClosed)
static Collection< Subtour > getSubtours(final Plan plan, double coordDistance)
final Activity destinationActivity
final List< PlanElement > trip
static void addSubtourAndUpdateParents(final List< Subtour > subtours, final Subtour newSubtour)
static OptionalTime getDepartureTime(Trip trip)
List< PlanElement > getTripElements()
static List< Leg > getLegs(final Plan plan)
static List< Trip > getTrips(final List<? extends PlanElement > planElements, final Predicate< String > isStageActivity)
static Trip findTripStartingAtActivity(final Activity activity, final Plan plan)
static Collection< Subtour > getSubtoursFromTrips(List< Trip > trips, double coordDistance)