private void unregisterAgentAtActivityLocation(final MobsimAgent agent) { Id<Person> agentId = agent.getId(); Id<Link> linkId = agent.getCurrentLinkId(); if (linkId != null) { // may be bushwacking internalInterface.unregisterAdditionalAgentOnLink(agentId, linkId); } }
/** * Informs the simulation that the specified agent wants to depart from its * current activity. The simulation can then put the agent onto its vehicle * on a link or teleport it to its destination. * */ private void arrangeAgentDeparture(final MobsimAgent agent) { double now = this.getSimTimer().getTimeOfDay(); Id<Link> linkId = agent.getCurrentLinkId(); Gbl.assertIf( linkId!=null ); events.processEvent(new PersonDepartureEvent(now, agent.getId(), linkId, agent.getMode())); for (DepartureHandler departureHandler : this.departureHandlers) { if (departureHandler.handleDeparture(now, agent, linkId)) { return; } } log.warn("no departure handler wanted to handle the departure of agent " + agent.getId()); // yy my intuition is that this should be followed by setting the agent state to abort. kai, nov'14 }
@Override public void handleEvent(ReplanningEvent event) { // check whether the agent is performing an activity Double activityEndTime = this.activityEndTimes.get(event.getPersonId()); if (activityEndTime != null) { // check whether the agent has changed its planned departure time MobsimAgent agent = this.mobsimDataProvider.getAgent(event.getPersonId()); if (activityEndTime != agent.getActivityEndTime()) { // Update the agent's activity end time. this.activityEndTimes.put(agent.getId(), agent.getActivityEndTime()); // Update the activity performing agents map. To do so, remove old entry and add new one. Map<Id<Person>, MobsimAgent> map; // remove map = this.getMapForTimeBin(this.getTimeBin(activityEndTime)); map.remove(agent.getId()); // add map = this.getMapForTimeBin(this.getTimeBin(agent.getActivityEndTime())); map.put(agent.getId(), agent); } } }
@Override public void registerAdditionalAgentOnLink(MobsimAgent planAgent) { this.additionalAgentsOnLink.put(planAgent.getId(), planAgent); }
private void arrangeNextAgentAction(final MobsimAgent agent) { switch( agent.getState() ) { case ACTIVITY: arrangeAgentActivity(agent); break ; case LEG: this.arrangeAgentDeparture(agent); break ; case ABORT: this.events.processEvent( new PersonStuckEvent(this.simTimer.getTimeOfDay(), agent.getId(), agent.getCurrentLinkId(), agent.getMode())); // NOTE: in the same way as one can register departure handler or activity handler, we could allow to // register abort handlers. If someone ever comes to this place here and needs this. kai, nov'17 this.agents.remove(agent.getId()) ; this.agentCounter.decLiving(); this.agentCounter.incLost(); break ; default: throw new RuntimeException("agent with unknown state (possibly null)") ; } }
@Override public boolean handleDeparture(double now, MobsimAgent agent, Id<Link> linkId) { if ( agent.getExpectedTravelTime()==null || agent.getExpectedTravelTime()==Time.UNDEFINED_TIME ) { Logger.getLogger( this.getClass() ).info( "mode: " + agent.getMode() ); throw new RuntimeException("teleportation does not work when travel time is undefined. There is also really no magic fix for this," + " since we cannot guess travel times for arbitrary modes and arbitrary landscapes. kai/mz, apr'15 & feb'16") ; } Double travelTime = agent.getExpectedTravelTime() ; if ( withTravelTimeCheck ) { Double speed = scenario.getConfig().plansCalcRoute().getTeleportedModeSpeeds().get( agent.getMode() ) ; Facility dpfac = agent.getCurrentFacility() ; Facility arfac = agent.getDestinationFacility() ; travelTime = DefaultTeleportationEngine.travelTimeCheck(travelTime, speed, dpfac, arfac); } double arrivalTime = now + travelTime ; this.teleportationList.add(new Tuple<>(arrivalTime, agent)); // === below here is only visualization, no dynamics === Id<Person> agentId = agent.getId(); Link currLink = this.scenario .getNetwork().getLinks().get(linkId); Link destLink = this.scenario .getNetwork().getLinks().get(agent.getDestinationLinkId()); Coord fromCoord = currLink.getToNode().getCoord(); Coord toCoord = destLink.getToNode().getCoord(); TeleportationVisData agentInfo = new TeleportationVisData(now, agentId, fromCoord, toCoord, travelTime); this.teleportationData.put(agentId, agentInfo); return true; }
private void handleTeleportationArrivals() { double now = internalInterface.getMobsim().getSimTimer().getTimeOfDay(); while (teleportationList.peek() != null) { Tuple<Double, MobsimAgent> entry = teleportationList.peek(); if (entry.getFirst() <= now) { teleportationList.poll(); MobsimAgent personAgent = entry.getSecond(); personAgent.notifyArrivalOnLinkByNonNetworkMode(personAgent .getDestinationLinkId()); double distance = personAgent.getExpectedTravelDistance(); this.eventsManager.processEvent(new TeleportationArrivalEvent(this.internalInterface.getMobsim().getSimTimer().getTimeOfDay(), personAgent.getId(), distance)); personAgent.endLegAndComputeNextState(now); this.teleportationData.remove(personAgent.getId()); internalInterface.arrangeNextAgentState(personAgent); } else { break; } } }
public void addDepartingAgent(MobsimAgent mobsimAgent, double now) { this.waitingAfterActivityAgents.add(mobsimAgent); this.activateLink(); this.simEngine.getEventsManager().processEvent( new PersonEntersVehicleEvent(now, mobsimAgent.getId(), Id.create(mobsimAgent.getId(), Vehicle.class))); this.simEngine.getEventsManager().processEvent( new VehicleEntersTrafficEvent(now, mobsimAgent.getId(), link.getId(), Id.create(mobsimAgent.getId(), Vehicle.class), mobsimAgent.getMode(), 1.0)); }
@Override public boolean applyAgentFilter(Id<Person> id, double time) { MobsimAgent agent = this.agents.get(id); if (!(links.contains(agent.getCurrentLinkId()))) return false; else return true; } }
@Override public void afterSim() { double now = internalInterface.getMobsim().getSimTimer().getTimeOfDay(); for (Tuple<Double, MobsimAgent> entry : teleportationList) { MobsimAgent agent = entry.getSecond(); eventsManager.processEvent(new PersonStuckEvent(now, agent.getId(), agent.getDestinationLinkId(), agent.getMode())); } teleportationList.clear(); }
@Override public boolean handleDeparture(double now, MobsimAgent agent, Id<Link> linkId) { if (this.transportModes.contains(agent.getMode())) { if ( agent instanceof MobsimDriverAgent ) { handleCarDeparture(now, (MobsimDriverAgent)agent, linkId); return true; } else { throw new UnsupportedOperationException("wrong agent type to depart on a network mode"); } } return false; }
private void moveToAbort(final MobsimAgent mobsimAgent, final double now, Link currentLink, Link nextLink) { log.error("Agent has no or wrong route! agentId=" + mobsimAgent.getId() + " currentLink=" + currentLink.getId().toString() + " nextLink=" + (nextLink!=null?nextLink.getId():"null") + " currentLinkToNode=" + currentLink.getToNode().getId() + " nextLinkFromNode=" + (nextLink!=null?nextLink.getFromNode().getId():"null") + ". The agent is removed from the simulation."); mobsimAgent.setStateToAbort(now); this.simEngine.internalInterface.arrangeNextAgentState(mobsimAgent); } }
@Override public boolean handleActivity(MobsimAgent agent) { if (!(agent instanceof DynAgent)) { return activityEngine.handleActivity(agent); } double endTime = agent.getActivityEndTime(); double currentTime = internalInterface.getMobsim().getSimTimer().getTimeOfDay(); if (endTime == Double.POSITIVE_INFINITY) { // This is the last planned activity. // So the agent goes to sleep. internalInterface.getMobsim().getAgentCounter().decLiving(); } else if (endTime <= currentTime && !beforeFirstSimStep) { // This activity is already over (planned for 0 duration) // So we proceed immediately. agent.endActivityAndComputeNextState(currentTime); internalInterface.arrangeNextAgentState(agent); } else { // The agent commences an activity on this link. if (beforeFirstSimStep) { dynAgents.add((DynAgent)agent); } else { newDynAgents.add((DynAgent)agent); } internalInterface.registerAdditionalAgentOnLink(agent); } return true; }
private double calculateFirstAgentStartTime() { double firstAgentStartTime = Double.POSITIVE_INFINITY; for (MobsimAgent agent : agents.values()) { firstAgentStartTime = Math.min(firstAgentStartTime, agent.getActivityEndTime()); } return firstAgentStartTime; }
@Override public boolean handlePassengerLeaving(PTPassengerAgent passenger, MobsimVehicle vehicle, Id<Link> toLinkId, double time) { boolean handled = vehicle.removePassenger(passenger); if(handled){ eventsManager.processEvent(new PersonLeavesVehicleEvent(time, passenger.getId(), vehicle.getVehicle().getId())); // from here on works only if PassengerAgent can be cast into MobsimAgent ... but this is how it was before. // kai, sep'12 MobsimAgent agent = (MobsimAgent) passenger ; agent.notifyArrivalOnLinkByNonNetworkMode(toLinkId); agent.endLegAndComputeNextState(time); this.internalInterface.arrangeNextAgentState(agent) ; // (cannot set trEngine to TransitQSimEngine because there are tests where this will not work. kai, dec'11) } return handled; }
if ( agent.getState()!=State.ACTIVITY ) { return ; double newActivityEndTime = agent.getActivityEndTime(); AgentEntry oldEntry = removeAgentFromQueue(agent);
@Override public boolean applyAgentFilter(Id<Person> id, double time) { MobsimAgent agent = this.agents.get(id); // check whether the agent is performing a leg if (!(agent.getState() == MobsimAgent.State.LEG)) return false; /* * Check whether the agent ends its leg on the current link. If * yes, remove the agent from the set. */ DriverAgent driver = (DriverAgent) agent; // Id<Link> nextLinkId = driver.chooseNextLinkId(); // if (nextLinkId == null) return false; if ( driver.isWantingToArriveOnCurrentLink() ) return false ; return true; }
@Override public void doSimStep(double time) { beforeFirstSimStep = false; while (activityEndsList.peek() != null) { if (activityEndsList.peek().activityEndTime <= time) { MobsimAgent agent = activityEndsList.poll().agent; unregisterAgentAtActivityLocation(agent); agent.endActivityAndComputeNextState(time); internalInterface.arrangeNextAgentState(agent); } else { return; } } }
private void moveVehicleFromInlinkToAbort(final QVehicle veh, final QLaneI fromLane, final double now, Id<Link> currentLinkId) { fromLane.popFirstVehicle(); // --> this.context.getEventsManager().processEvent(new LinkLeaveEvent(now, veh.getId(), currentLinkId)); // <-- // first treat the passengers: for ( PassengerAgent pp : veh.getPassengers() ) { if ( pp instanceof MobsimAgent ) { ((MobsimAgent)pp).setStateToAbort(now); netsimEngine.arrangeNextAgentState((MobsimAgent)pp) ; } else if ( wrnCnt < 1 ) { wrnCnt++ ; log.warn("encountering PassengerAgent that cannot be cast into a MobsimAgent; cannot say if this is a problem" ) ; log.warn(Gbl.ONLYONCE) ; } } // now treat the driver: veh.getDriver().setStateToAbort(now) ; netsimEngine.arrangeNextAgentState(veh.getDriver()) ; }