21 package org.matsim.withinday.utils;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.stream.Collectors;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
83 private static final Logger
log = LogManager.getLogger(
EditTrips.class) ;
99 if (internalInterface == null) {
100 log.warn(
"InternalInterface is null. Replanning of pt/transit legs will not work properly and will likely fail.");
105 transitAgentTracker = (TransitStopAgentTracker) tracker;
117 if (transitAgentTracker == null) {
118 log.warn(
"no TransitStopAgentTracker found in qsim. Replanning of pt/transit legs will not work properly and will likely fail.");
129 for (
Trip trip : trips ) {
145 log.debug(
"entering replanCurrentTrip with routingMode=" + routingMode) ;
155 int tripElementsIndex = tripElements.indexOf( currentPlanElement ) ;
157 if ( currentPlanElement instanceof
Activity ) {
166 if (eventsManager != null) {
174 log.debug(
"entering replanCurrentTripFromLeg for agent" + agent.
getId());
175 Leg currentLeg = (
Leg) currentPlanElement ;
191 log.debug(
"entering replanCurrentLegWithNetworkRoute for agent" + agent.
getId()) ;
200 List<? extends PlanElement> newTripElements =
newTripToNewActivity(currentLocationFacility, newAct, mainMode, now, person, routingAttributes );
210 while ( !planElements.get(pos).equals(newAct) ) {
211 planElements.remove(pos) ;
215 for (
int ijk = 1 ; ijk < newTripElements.size() ; ijk++ ) {
216 planElements.add( pos, newTripElements.get(ijk) ) ;
223 log.debug(
"entering replanCurrentLegWithTransitRoute for agentId=" + agent.
getId()) ;
232 throw new RuntimeException(
"transit leg, but agent is not an PTPassengerAgent: not implemented! agent id: " + agent.
getId());
234 PTPassengerAgent ptPassengerAgent = (PTPassengerAgent) agent;
275 boolean wantsToLeaveStop = false ;
278 List<? extends PlanElement> newTripElements = null;
280 if (mobsimVehicle == null) {
282 log.debug(
"agent with ID=" + agent.
getId() +
" is waiting at a stop=" + currentOrNextStop ) ;
283 newTripElements =
newTripToNewActivity(currentOrNextStop, newAct, routingMode, now, person, routingAttributes );
286 wantsToLeaveStop = true ;
287 if (newTripElements.size() >= 2) {
289 Leg newPtLeg = (
Leg) newTripElements.get(2);
294 log.debug(
"agent with ID=" + agent.
getId() +
" will wait for vehicle departing at the same stop facility." ) ;
304 newTripElements.remove(0);
305 newTripElements.remove(0);
306 newTripElements.remove(0);
307 wantsToLeaveStop =
false;
314 if (wantsToLeaveStop) {
317 log.debug(
"agent with ID=" + agent.
getId() +
" will leave the stop facility.") ;
322 log.debug(
"agent with ID=" + agent.
getId() +
" is on a vehicle" );
337 double reRoutingTime;
339 TransitDriverAgentImpl driverImpl = (TransitDriverAgentImpl) driver;
342 reRoutingTime = departureFirstTransitRouteStop + arrivalOffsetNextTransitRouteStop;
344 throw new RuntimeException(
"transit driver is not a TransitDriverAgentImpl, not implemented!");
346 log.debug(
"agent with ID=" + agent.
getId() +
" is re-routed from next stop " + currentOrNextStop.
getId() +
" scheduled arrival at " + reRoutingTime);
347 newTripElements =
newTripToNewActivity(currentOrNextStop, newAct, routingMode, reRoutingTime, person, routingAttributes );
350 boolean agentStaysOnSameVehicle =
false;
351 if (newTripElements.size() >= 2) {
353 Leg firstPtLeg = (
Leg) newTripElements.get(2);
385 newTripElements.remove(0);
386 newTripElements.remove(0);
387 newTripElements.remove(0);
389 agentStaysOnSameVehicle =
true;
396 if (!agentStaysOnSameVehicle) {
402 log.debug(
"newTrip for agentId=" + agent.
getId() );
404 log.debug(planElement) ;
411 while ( !planElements.get(pos).equals(newAct) ) {
412 planElements.remove(pos) ;
416 for (
int ijk = 0 ; ijk < newTripElements.size() ; ijk++ ) {
417 planElements.add( pos + ijk, newTripElements.get(ijk) ) ;
422 StringBuilder stb =
new StringBuilder();
423 boolean first =
true;
430 if( planElement instanceof
Leg ){
431 stb.append( ((Leg) planElement).getMode() );
432 }
else if( planElement instanceof
Activity ){
433 stb.append( ((Activity) planElement).getType() );
437 log.debug(
"agent" + agent.
getId() +
" new plan: " + stb.toString() );
440 log.debug(
"agent" + agent.
getId() +
" new plan: " ) ;
442 log.debug(planElement) ;
448 if ( wantsToLeaveStop ) {
449 if (transitAgentTracker == null) {
450 log.error(
"Replanning a pt/transit leg, but there is no TransitStopAgentTracker found in qsim. Failing...");
455 ((
MobsimAgent) ptPassengerAgent).endLegAndComputeNextState( now );
516 double departureTime = now + 0.5 * travelTime;
521 double departureTimeAccordingToPlannedActivityEnd = previousActivity.
getEndTime().
seconds() + travelTime;
523 if (departureTimeAccordingToPlannedActivityEnd > now) {
524 departureTime = departureTimeAccordingToPlannedActivityEnd;
535 .collect(Collectors.toList());
550 log.debug(
"entering newTripToNewActivity") ;
554 return tripRouter.
calcRoute(mainMode, currentLocationFacility, toFacility, now, person, routingAttributes )
561 .collect(Collectors.toList());
568 log.debug(
"entering replanCurrentTripFromStageActivity for agent" + agent.
getId()) ;
575 throw new RuntimeException(
"Expected a stage activity as current plan element");
594 planElements.remove(pos) ;
598 for (
int ijk = 1 ; ijk < newTripElements.size() ; ijk++ ) {
599 planElements.add( pos + ijk, newTripElements.get(ijk) ) ;
605 List<Leg> list = Collections.singletonList( pf.
createLeg( mainMode ) ) ;
610 return insertEmptyTrip( plan, fromActivity, toActivity, mainMode, this.pf ) ;
621 double departureTime) {
622 return replanFutureTrip(trip, plan, routingMode, departureTime, tripRouter, scenario );
627 Leg newCurrentLeg = (
Leg) newTrip.get(0) ;
637 final List<Id<Link>> newLinksIds = newNWRoute.
getLinkIds().subList(0,newNWRoute.
getLinkIds().size()) ;
655 Leg newCurrentLeg = (
Leg) newTrip.get(0);
657 List<PlanElement> newPlanElementsAfterMerge =
new ArrayList<>();
685 newPlanElementsAfterMerge.add(act);
689 for (
int ijk = 0; ijk < newTrip.size(); ijk++) {
690 newPlanElementsAfterMerge.add(newPlanElementsAfterMerge.size(), newTrip.get(ijk));
693 return newPlanElementsAfterMerge;
698 boolean afterCurrentStop =
false;
700 if ( currentStopId.
equals(stop.getStopFacility().getId()) ) {
701 afterCurrentStop =
true;
703 if ( afterCurrentStop && egressStopId.
equals(stop.getStopFacility().getId()) ) {
719 log.debug(
"entering replanFutureTrip for agentid=" + plan.
getPerson().getId() ) ;
726 final List<? extends PlanElement> newTrip = tripRouter.
calcRoute(routingMode, fromFacility, toFacility, departureTime, person, trip.
getTripAttributes())
733 .collect(Collectors.toList());
753 return replanFutureTrip(trip, plan, mainMode, departureTime, tripRouter, scenario );
OptionalTime getEndTime()
static Trip findTripAtPlanElementIndex(MobsimAgent agent, int index)
synchronized List<? extends PlanElement > calcRoute(final String mainMode, final Facility fromFacility, final Facility toFacility, final double departureTime, final Person person, final Attributes routingAttributes)
Activity getDestinationActivity()
void setBoardingTime(double boardingTime)
static Integer getCurrentRouteLinkIdIndex(MobsimAgent agent)
void replanCurrentTripFromStageActivity(Trip trip, int tripElementsIndex, String mainMode, double now, MobsimAgent agent)
Id< Link > getStartLinkId()
EventsManager eventsManager
TransitStopAgentTracker transitAgentTracker
Map< Id< TransitStopFacility >, TransitStopFacility > getFacilities()
static void assertIf(boolean flag)
static Facility wrapLink(final Link link)
Activity getOriginActivity()
void replanCurrentLegWithNetworkRoute(Activity newAct, String mainMode, Leg currentLeg, double now, MobsimAgent agent, Attributes routingAttributes)
static Trip findCurrentTrip(MobsimAgent agent)
final void setTravelTime(final double travTime)
static List< PlanElement > insertTrip(final Plan plan, final Activity origin, final List<? extends PlanElement > trip, final Activity destination)
Leg createLeg(String legMode)
final PopulationFactory pf
double getDepartureTime()
final List<? extends PlanElement > replanFutureTrip(Trip trip, Plan plan, String routingMode, double departureTime)
static Integer getCurrentPlanElementIndex(MobsimAgent agent)
void replanCurrentTripFromLeg(Activity newAct, final PlanElement currentPlanElement, final String routingMode, double now, MobsimAgent agent, Attributes routingAttributes)
static PlanElement getCurrentPlanElement(MobsimAgent agent)
void arrangeNextAgentState(MobsimAgent agent)
Id< TransitRoute > getRouteId()
static final String TRANSIT_ACTIVITY_TYPE
TransitRouteStop getStop(final TransitStopFacility stop)
static List<? extends PlanElement > replanFutureTrip(Trip trip, Plan plan, String routingMode, double departureTime, TripRouter tripRouter, Scenario scenario)
final boolean insertEmptyTrip(Plan plan, Activity fromActivity, Activity toActivity, String mainMode)
Id< Link > getCurrentLinkId()
Trip findTripAfterActivity(Plan plan, Activity activity)
static void putPersonAttribute(HasPlansAndId<?, ?> person, String key, Object value)
TransitRoute getTransitRoute()
List< Id< Link > > getLinkIds()
static Trip findTripAtPlanElement(MobsimAgent agent, PlanElement pe)
Collection< AgentTracker > getAgentTrackers()
void processEvent(final Event event)
abstract OptionalTime getArrivalOffset()
void removeAgentFromStop(final PTPassengerAgent agent, final Id< TransitStopFacility > stopId)
final TimeInterpretation timeInterpretation
OptionalTime decideOnLegTravelTime(Leg leg)
Attributes getTripAttributes()
void replanCurrentLegWithGenericRoute(Activity newAct, String routingMode, Leg currentLeg, double now, MobsimAgent agent)
static final boolean isStageActivity(final String activityType)
OptionalTime getTravelTime()
OptionalTime decideOnActivityEndTimeAlongPlan(Activity activity, Plan plan)
void replanCurrentLegWithTransitRoute(Activity newAct, String routingMode, Leg currentLeg, double now, MobsimAgent agent, Attributes routingAttributes)
void setMaximumDuration(double seconds)
Population getPopulation()
Id< TransitStopFacility > getEgressStopId()
final List<? extends PlanElement > replanFutureTrip(Trip trip, Plan plan, String mainMode)
boolean transitRouteLaterStopsAt(Id< TransitLine > lineId, Id< TransitRoute > routeId, Id< TransitStopFacility > currentStopId, Id< TransitStopFacility > egressStopId)
void setRoute(Route route)
static List< Trip > getTrips(final Plan plan)
List< PlanElement > getPlanElements()
Id< TransitLine > getLineId()
static final String NOT_IMPLEMENTED
static void assertNotNull(Object obj)
EventsManager getEventsManager()
static void resetCaches(MobsimAgent agent)
static boolean insertEmptyTrip(Plan plan, Activity fromActivity, Activity toActivity, String mainMode, PopulationFactory pf)
Map< Id< Link >, ? extends Link > getLinks()
static Activity createActivityFromCoordAndLinkId(String type, Coord coord, Id< Link > linkId)
EditTrips(TripRouter tripRouter, Scenario scenario, InternalInterface internalInterface, TimeInterpretation timeInterpretation)
MobsimDriverAgent getDriver()
TransitStopFacility getNextTransitStop()
ActivityFacilities getActivityFacilities()
static List<? extends PlanElement > relocateFutureTrip(Trip trip, Plan plan, String mainMode, double departureTime, TripRouter tripRouter, Scenario scenario)
static Activity createActivity(Activity act)
PopulationFactory getFactory()
List<? extends PlanElement > newTripToNewActivity(Facility currentLocationFacility, Activity newAct, String mainMode, double now, Person person, Attributes routingAttributes)
TransitSchedule getTransitSchedule()
boolean equals(Object obj)
Id< Link > getEndLinkId()
Map< Id< TransitLine >, TransitLine > getTransitLines()
List< PlanElement > defaultMergeOldAndNewCurrentPtLeg(Leg currentLeg, List<? extends PlanElement > newTrip, MobsimAgent agent, TransitStopFacility nextStop, TransitPassengerRoute oldPtRoute)
static void replaceRemainderOfCurrentRoute(Leg currentLeg, List<? extends PlanElement > newTrip, MobsimAgent agent)
List< PlanElement > getTripElements()
static Facility toFacility(final Activity toWrap, ActivityFacilities activityFacilities)
final boolean replanCurrentTrip(MobsimAgent agent, double now, String routingMode)
static Trip findTripStartingAtActivity(final Activity activity, final Plan plan)
Id< TransitStopFacility > getAccessStopId()
final InternalInterface internalInterface
final TripRouter tripRouter
static Plan getModifiablePlan(MobsimAgent agent)
OptionalTime getBoardingTime()