SUMO - Simulation of Urban MObility
MSVehicleTransfer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // A mover of vehicles that got stucked due to grid locks
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <iostream>
29 #include "MSNet.h"
30 #include "MSLane.h"
31 #include "MSEdge.h"
32 #include "MSVehicle.h"
34 #include "MSVehicleControl.h"
35 #include "MSInsertionControl.h"
36 #include "MSVehicleTransfer.h"
37 
38 
39 // ===========================================================================
40 // static member definitions
41 // ===========================================================================
44 const std::set<const MSVehicle*> MSVehicleTransfer::myEmptyVehicleSet;
45 
46 // ===========================================================================
47 // member method definitions
48 // ===========================================================================
49 void
51  if (veh->isParking()) {
55  } else {
58  if (veh->succEdge(1) == nullptr) {
59  WRITE_WARNING("Vehicle '" + veh->getID() + "' teleports beyond arrival edge '" + veh->getEdge()->getID() + "', time " + time2string(t) + ".");
62  return;
63  }
65  veh->enterLaneAtMove(veh->succEdge(1)->getLanes()[0], true);
66  }
67  myVehicles.push_back(VehicleInformation(t, veh,
69  veh->isParking()));
70 }
71 
72 
73 void
75  for (VehicleInfVector::iterator i = myVehicles.begin(); i != myVehicles.end(); ++i) {
76  if (i->myVeh == veh) {
77  if (i->myParking) {
78  veh->getLane()->removeParking(veh);
79  }
80  myVehicles.erase(i);
81  break;
82  }
83  }
84 }
85 
86 
87 void
89  // go through vehicles
90  for (VehicleInfVector::iterator i = myVehicles.begin(); i != myVehicles.end();) {
91  // get the vehicle information
92  VehicleInformation& desc = *i;
93 
94  if (desc.myParking) {
95  // handle parking vehicles
96  if (time != desc.myTransferTime) {
97  // avoid calling processNextStop twice in the transfer step
98  desc.myVeh->processNextStop(1);
99  }
100  if (desc.myVeh->keepStopping(true)) {
101  i++;
102  continue;
103  }
104  // parking finished, head back into traffic
105  }
106  const SUMOVehicleClass vclass = desc.myVeh->getVehicleType().getVehicleClass();
107  const MSEdge* e = desc.myVeh->getEdge();
108  const MSEdge* nextEdge = desc.myVeh->succEdge(1);
109 
110  const double departPos = desc.myParking ? desc.myVeh->getPositionOnLane() : 0;
111 
112  if (desc.myParking) {
113  // handle parking vehicles
114  if (desc.myVeh->getLane()->isInsertionSuccess(desc.myVeh, 0, departPos, desc.myVeh->getLateralPositionOnLane(),
117  desc.myVeh->getLane()->removeParking(desc.myVeh);
118  i = myVehicles.erase(i);
119  } else {
120  i++;
121  }
122  } else {
123  // get the lane on which this vehicle should continue
124  // first select all the lanes which allow continuation onto nextEdge
125  // then pick the one which is least occupied
126  MSLane* l = (nextEdge != nullptr ? e->getFreeLane(e->allowedLanes(*nextEdge, vclass), vclass, departPos) :
127  e->getFreeLane(nullptr, vclass, departPos));
128  // handle teleporting vehicles, lane may be 0 because permissions were modified by a closing rerouter or TraCI
129  if (l != nullptr && l->freeInsertion(*(desc.myVeh), MIN2(l->getSpeedLimit(), desc.myVeh->getMaxSpeed()), 0, MSMoveReminder::NOTIFICATION_TELEPORT)) {
130  WRITE_WARNING("Vehicle '" + desc.myVeh->getID() + "' ends teleporting on edge '" + e->getID() + "', time " + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".");
132  i = myVehicles.erase(i);
133  } else {
134  // could not insert. maybe we should proceed in virtual space
135  if (desc.myProceedTime < time) {
136  if (desc.myVeh->succEdge(1) == nullptr) {
137  WRITE_WARNING("Vehicle '" + desc.myVeh->getID() + "' teleports beyond arrival edge '" + e->getID() + "', time " + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".");
140  i = myVehicles.erase(i);
141  continue;
142  }
143  // let the vehicle move to the next edge
145  // active move reminders (i.e. rerouters)
146  desc.myVeh->enterLaneAtMove(desc.myVeh->succEdge(1)->getLanes()[0], true);
147  // use current travel time to determine when to move the vehicle forward
149  }
150  ++i;
151  }
152  }
153  }
154 }
155 
156 
157 bool
159  return !myVehicles.empty();
160 }
161 
162 
165  if (myInstance == nullptr) {
167  }
168  return myInstance;
169 }
170 
171 
173 
174 
176  myInstance = nullptr;
177 }
178 
179 
180 void
182  for (VehicleInfVector::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
184  out.writeAttr(SUMO_ATTR_ID, it->myVeh->getID());
185  out.writeAttr(SUMO_ATTR_DEPART, it->myProceedTime);
186  if (it->myParking) {
187  out.writeAttr(SUMO_ATTR_PARKING, it->myVeh->getLane()->getID());
188  }
189  out.closeTag();
190  }
191 }
192 
193 
194 void
196  MSVehicle* veh = dynamic_cast<MSVehicle*>(vc.getVehicle(attrs.getString(SUMO_ATTR_ID)));
197  if (veh == nullptr) {
198  // deleted
199  return;
200  }
201  SUMOTime proceedTime = (SUMOTime)attrs.getLong(SUMO_ATTR_DEPART);
202  MSLane* parkingLane = attrs.hasAttribute(SUMO_ATTR_PARKING) ? MSLane::dictionary(attrs.getString(SUMO_ATTR_PARKING)) : nullptr;
203  myVehicles.push_back(VehicleInformation(-1, veh, proceedTime - offset, parkingLane != nullptr));
204  if (parkingLane != nullptr) {
205  parkingLane->addParking(veh);
206  veh->setTentativeLaneAndPosition(parkingLane, veh->getPositionOnLane());
207  veh->processNextStop(veh->getSpeed());
208  }
210 }
211 
212 
213 
214 /****************************************************************************/
215 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
bool enterLaneAtMove(MSLane *enteredLane, bool onTeleporting=false)
Update when the vehicle enters a new lane in the move step.
Definition: MSVehicle.cpp:4098
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79
long long int SUMOTime
Definition: SUMOTime.h:36
static MSVehicleTransfer * myInstance
The static singleton-instance.
void remove(MSVehicle *veh)
Remove a vehicle from this transfer object.
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to, const std::string &info="")
Informs all added listeners about a vehicle&#39;s state change.
Definition: MSNet.cpp:855
Holds the information needed to move the vehicle over the network.
virtual void removeParking(MSVehicle *veh)
remove parking vehicle. This must be syncrhonized when running with GUI
Definition: MSLane.cpp:2877
MSVehicleTransfer()
Constructor.
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:565
VehicleInfVector myVehicles
The information about stored vehicles to move virtually.
bool isInsertionSuccess(MSVehicle *vehicle, double speed, double pos, double posLat, bool recheckNextLanes, MSMoveReminder::Notification notification)
Tries to insert the given vehicle with the given state (speed and pos)
Definition: MSLane.cpp:606
double getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:403
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
virtual ~MSVehicleTransfer()
Destructor.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
const std::vector< MSLane * > * allowedLanes(const MSEdge &destination, SUMOVehicleClass vclass=SVC_IGNORING) const
Get the allowed lanes to reach the destination-edge.
Definition: MSEdge.cpp:390
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:162
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
const std::string & getID() const
Returns the id.
Definition: Named.h:78
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
bool myParking
whether the vehicle is or was parking
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:4307
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
double getCurrentTravelTime(const double minSpeed=NUMERICAL_EPS) const
Computes and returns the current travel time for this edge.
Definition: MSEdge.cpp:771
double getMaxSpeed() const
Returns the maximum speed.
A road/street connecting two junctions.
Definition: MSEdge.h:75
void leaveLane(const MSMoveReminder::Notification reason, const MSLane *approachedLane=0)
Update of members if vehicle leaves a new lane in the lane change step or at arrival.
Definition: MSVehicle.cpp:4257
void saveState(OutputDevice &out) const
Saves the current state into the given stream.
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
The vehicles starts to park.
Definition: MSNet.h:508
void checkInsertions(SUMOTime time)
Checks "movement" of stored vehicles.
Encapsulated SAX-Attributes.
bool hasPending() const
Checks whether stored vehicles are present.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:316
double getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:506
T MIN2(T a, T b)
Definition: StdDefs.h:70
void add(const SUMOTime t, MSVehicle *veh)
Adds a vehicle to this transfer object.
The vehicle started to teleport.
Definition: MSNet.h:500
void onRemovalFromNet(const MSMoveReminder::Notification reason)
Called when the vehicle is removed from the network.
Definition: MSVehicle.cpp:934
SUMOTime myProceedTime
The time at which the vehicle should be moved virtually one edge further.
static const std::set< const MSVehicle * > myEmptyVehicleSet
an empty set for convenience
void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset, MSVehicleControl &vc)
Loads one transfer vehicle state from the given descriptionn.
The vehicle ends to park.
Definition: MSNet.h:510
double getLateralPositionOnLane() const
Get the vehicle&#39;s lateral position on the lane.
Definition: MSVehicle.h:440
void alreadyDeparted(SUMOVehicle *veh)
stops trying to emit the given vehicle (because it already departed)
static MSVehicleTransfer * getInstance()
Returns the instance of this object.
bool freeInsertion(MSVehicle &veh, double speed, double posLat, MSMoveReminder::Notification notification=MSMoveReminder::NOTIFICATION_DEPARTED)
Tries to insert the given vehicle on any place.
Definition: MSLane.cpp:362
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1730
The vehicle starts or ends parking.
void scheduleVehicleRemoval(SUMOVehicle *veh)
Removes a vehicle after it has ended.
bool keepStopping(bool afterProcessing=false) const
Returns whether the vehicle is stopped and must continue to do so.
Definition: MSVehicle.cpp:1601
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
double processNextStop(double currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1646
void setTentativeLaneAndPosition(MSLane *lane, double pos, double posLat=0)
set tentative lane and position during insertion to ensure that all cfmodels work (some of them requi...
Definition: MSVehicle.cpp:4921
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:369
const MSEdge * succEdge(int nSuccs) const
Returns the nSuccs&#39;th successor of edge the vehicle is currently at.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
MSVehicle * myVeh
The vehicle itself.
SUMOTime myTransferTime
the time at which this vehicle was removed from the network
void addParking(MSVehicle *veh)
add parking vehicle. This should only used during state loading
Definition: MSLane.cpp:2871
The vehicle was teleported out of the net.
The class responsible for building and deletion of vehicles.
bool isParking() const
Returns whether the vehicle is parking.
Definition: MSVehicle.cpp:1627
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:483
const std::string & getID() const
Returns the name of the vehicle.
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
MSLane * getFreeLane(const std::vector< MSLane *> *allowed, const SUMOVehicleClass vclass, double departPos) const
Finds the emptiest lane allowing the vehicle class.
Definition: MSEdge.cpp:436
The vehicle ended being teleported.
Definition: MSNet.h:502
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
static const double TeleportMinSpeed
The minimum speed while teleporting.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
The vehicle is being teleported.
void endLaneChangeManeuver(const MSMoveReminder::Notification reason=MSMoveReminder::NOTIFICATION_LANE_CHANGE)