SUMO - Simulation of Urban MObility
MSVehicleTransfer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A mover of vehicles that got stucked due to grid locks
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
36 #include "MSNet.h"
37 #include "MSLane.h"
38 #include "MSEdge.h"
39 #include "MSVehicle.h"
41 #include "MSVehicleControl.h"
42 #include "MSInsertionControl.h"
43 #include "MSVehicleTransfer.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // static member definitions
52 // ===========================================================================
55 const std::set<const MSVehicle*> MSVehicleTransfer::myEmptyVehicleSet;
56 
57 // ===========================================================================
58 // member method definitions
59 // ===========================================================================
60 void
62  if (veh->isParking()) {
66  myParkingVehicles[veh->getLane()].insert(veh); // initialized to empty set on first use
67  } else {
70  if (veh->succEdge(1) == 0) {
71  WRITE_WARNING("Vehicle '" + veh->getID() + "' teleports beyond arrival edge '" + veh->getEdge()->getID() + "', time " + time2string(t) + ".");
74  return;
75  }
77  veh->enterLaneAtMove(veh->succEdge(1)->getLanes()[0], true);
78  }
79  myVehicles.push_back(VehicleInformation(veh,
81  veh->isParking()));
82 }
83 
84 
85 void
87  for (VehicleInfVector::iterator i = myVehicles.begin(); i != myVehicles.end(); ++i) {
88  if (i->myVeh == veh) {
89  myVehicles.erase(i);
90  break;
91  }
92  }
93  if (veh->getLane() != 0) {
94  myParkingVehicles[veh->getLane()].erase(veh);
95  }
96 }
97 
98 
99 void
101  // go through vehicles
102  for (VehicleInfVector::iterator i = myVehicles.begin(); i != myVehicles.end();) {
103  // get the vehicle information
104  VehicleInformation& desc = *i;
105 
106  if (desc.myParking) {
107  // handle parking vehicles
108  if (desc.myVeh->processNextStop(1) <= 0) {
109  ++i;
110  continue;
111  }
112  // parking finished, head back into traffic
113  }
114  const SUMOVehicleClass vclass = desc.myVeh->getVehicleType().getVehicleClass();
115  const MSEdge* e = desc.myVeh->getEdge();
116  const MSEdge* nextEdge = desc.myVeh->succEdge(1);
117 
118  // get the lane on which this vehicle should continue
119  // first select all the lanes which allow continuation onto nextEdge
120  // then pick the one which is least occupied
121  // @todo maybe parking vehicles should always continue on the rightmost lane?
122  const MSLane* oldLane = desc.myVeh->getLane();
123  MSLane* l = (nextEdge != 0 ? e->getFreeLane(e->allowedLanes(*nextEdge, vclass), vclass) :
124  e->getFreeLane(0, vclass));
125 
126  if (desc.myParking) {
127  // handle parking vehicles
130  myParkingVehicles[oldLane].erase(desc.myVeh);
131  i = myVehicles.erase(i);
132  } else {
133  i++;
134  }
135  } else {
136  // handle teleporting vehicles, lane may be 0 because permissions were modified by a closing rerouter or TraCI
137  if (l != 0 && l->freeInsertion(*(desc.myVeh), MIN2(l->getSpeedLimit(), desc.myVeh->getMaxSpeed()), MSMoveReminder::NOTIFICATION_TELEPORT)) {
138  WRITE_WARNING("Vehicle '" + desc.myVeh->getID() + "' ends teleporting on edge '" + e->getID() + "', time " + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".");
140  i = myVehicles.erase(i);
141  } else {
142  // could not insert. maybe we should proceed in virtual space
143  if (desc.myProceedTime < time) {
144  if (desc.myVeh->succEdge(1) == 0) {
145  WRITE_WARNING("Vehicle '" + desc.myVeh->getID() + "' teleports beyond arrival edge '" + e->getID() + "', time " + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".");
148  i = myVehicles.erase(i);
149  continue;
150  }
151  // let the vehicle move to the next edge
153  // active move reminders (i.e. rerouters)
154  desc.myVeh->enterLaneAtMove(desc.myVeh->succEdge(1)->getLanes()[0], true);
155  // use current travel time to determine when to move the vehicle forward
157  }
158  ++i;
159  }
160  }
161  }
162 }
163 
164 
165 bool
167  return !myVehicles.empty();
168 }
169 
170 
173  if (myInstance == 0) {
175  }
176  return myInstance;
177 }
178 
179 
181 
182 
184  myInstance = 0;
185 }
186 
187 
188 const std::set<const MSVehicle*>&
190  ParkingVehicles::const_iterator it = myParkingVehicles.find(lane);
191  if (it != myParkingVehicles.end()) {
192  return it->second;
193  } else {
194  return myEmptyVehicleSet;
195  }
196 }
197 
198 
199 void
201  std::map<const MSVehicle*, const MSLane*> parkingLanes;
202  for (ParkingVehicles::const_iterator it = myParkingVehicles.begin(); it != myParkingVehicles.end(); ++it) {
203  const std::set<const MSVehicle*>& vehs = it->second;
204  for (std::set<const MSVehicle*>::const_iterator it2 = vehs.begin(); it2 != vehs.end(); ++it2) {
205  parkingLanes[*it2] = it->first;
206  }
207  }
208  for (VehicleInfVector::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
210  out.writeAttr(SUMO_ATTR_ID, it->myVeh->getID());
211  out.writeAttr(SUMO_ATTR_DEPART, it->myProceedTime);
212  if (it->myParking) {
213  out.writeAttr(SUMO_ATTR_PARKING, parkingLanes[it->myVeh]->getID());
214  }
215  out.closeTag();
216  }
217 }
218 
219 
220 void
222  MSVehicle* veh = dynamic_cast<MSVehicle*>(vc.getVehicle(attrs.getString(SUMO_ATTR_ID)));
223  if (veh == 0) {
224  // deleted
225  return;
226  }
227  SUMOTime proceedTime = (SUMOTime)attrs.getLong(SUMO_ATTR_DEPART);
229  myVehicles.push_back(VehicleInformation(veh, proceedTime + offset, parkingLane != 0));
230  if (parkingLane != 0) {
231  myParkingVehicles[parkingLane].insert(veh);
232  veh->setTentativeLaneAndPosition(parkingLane, veh->getPositionOnLane());
233  veh->processNextStop(veh->getSpeed());
234  }
236 }
237 
238 
239 
240 /****************************************************************************/
241 
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:2689
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
long long int SUMOTime
Definition: SUMOTime.h:43
static MSVehicleTransfer * myInstance
The static singleton-instance.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
void remove(MSVehicle *veh)
Remove a vehicle from this transfer object.
Holds the information needed to move the vehicle over the network.
static const SUMOReal TeleportMinSpeed
The minimum speed while teleporting.
MSVehicleTransfer()
Constructor.
void setTentativeLaneAndPosition(MSLane *lane, SUMOReal pos, SUMOReal posLat=0)
set tentative lane and position during insertion to ensure that all cfmodels work (some of them requi...
Definition: MSVehicle.cpp:3547
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
SUMOReal getMaxSpeed() const
Returns the maximum speed.
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:192
VehicleInfVector myVehicles
The information about stored vehicles to move virtually.
bool hasPending() const
Checks whether stored vehicles are present.
void saveState(OutputDevice &out) const
Saves the current state into the given stream.
virtual ~MSVehicleTransfer()
Destructor.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
MSLane * getFreeLane(const std::vector< MSLane * > *allowed, const SUMOVehicleClass vclass) const
Finds the emptiest lane allowing the vehicle class.
Definition: MSEdge.cpp:396
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
SUMOReal getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:375
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
SUMOReal processNextStop(SUMOReal currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1126
bool freeInsertion(MSVehicle &veh, SUMOReal speed, MSMoveReminder::Notification notification=MSMoveReminder::NOTIFICATION_DEPARTED)
Tries to insert the given vehicle on any place.
Definition: MSLane.cpp:304
bool myParking
whether the vehicle is or was parking
const std::set< const MSVehicle * > & getParkingVehicles(const MSLane *lane) const
return parking vehicles on the given lane
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:2881
SUMOReal getLateralPositionOnLane() const
Get the vehicle&#39;s lateral position on the lane.
Definition: MSVehicle.h:407
const std::string & getID() const
Returns the id.
Definition: Named.h:66
A road/street connecting two junctions.
Definition: MSEdge.h:80
The vehicles starts to park.
Definition: MSNet.h:582
void checkInsertions(SUMOTime time)
Checks "movement" of stored vehicles.
Encapsulated SAX-Attributes.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:308
T MIN2(T a, T b)
Definition: StdDefs.h:64
void add(const SUMOTime t, MSVehicle *veh)
Adds a vehicle to this transfer object.
The vehicle started to teleport.
Definition: MSNet.h:574
SUMOReal getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:472
void onRemovalFromNet(const MSMoveReminder::Notification reason)
Called when the vehicle is removed from the network.
Definition: MSVehicle.cpp:590
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:584
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
bool isParking() const
Returns whether the vehicle is parking.
Definition: MSVehicle.cpp:1108
ParkingVehicles myParkingVehicles
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
void alreadyDeparted(SUMOVehicle *veh)
stops trying to emit the given vehicle (because it already departed)
static MSVehicleTransfer * getInstance()
Returns the instance of this object.
void leaveLane(const MSMoveReminder::Notification reason)
Update of members if vehicle leaves a new lane in the lane change step or at arrival.
Definition: MSVehicle.cpp:2832
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1317
The vehicle starts or ends parking.
void scheduleVehicleRemoval(SUMOVehicle *veh)
Removes a vehicle after it has ended.
const MSEdge * succEdge(int nSuccs) const
Returns the nSuccs&#39;th successor of edge the vehicle is currently at.
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:361
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:299
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
SUMOReal getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:442
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to)
Informs all added listeners about a vehicle&#39;s state change.
Definition: MSNet.cpp:791
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:208
MSVehicle * myVeh
The vehicle itself.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
bool isInsertionSuccess(MSVehicle *vehicle, SUMOReal speed, SUMOReal pos, SUMOReal posLat, bool recheckNextLanes, MSMoveReminder::Notification notification)
Tries to insert the given vehicle with the given state (speed and pos)
Definition: MSLane.cpp:526
The vehicle was teleported out of the net.
The class responsible for building and deletion of vehicles.
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:488
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
The vehicle ended being teleported.
Definition: MSNet.h:576
SUMOReal getCurrentTravelTime(const SUMOReal minSpeed=NUMERICAL_EPS) const
Computes and returns the current travel time for this edge.
Definition: MSEdge.cpp:676
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
The vehicle is being teleported.
const std::string & getID() const
Returns the name of the vehicle.
void endLaneChangeManeuver(const MSMoveReminder::Notification reason=MSMoveReminder::NOTIFICATION_LANE_CHANGE)