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 
46 // ===========================================================================
47 // static member definitions
48 // ===========================================================================
51 const std::set<const MSVehicle*> MSVehicleTransfer::myEmptyVehicleSet;
52 
53 // ===========================================================================
54 // member method definitions
55 // ===========================================================================
56 void
58  if (veh->isParking()) {
62  myParkingVehicles[veh->getLane()].insert(veh); // initialized to empty set on first use
63  } else {
66  if (veh->succEdge(1) == 0) {
67  WRITE_WARNING("Vehicle '" + veh->getID() + "' teleports beyond arrival edge '" + veh->getEdge()->getID() + "', time " + time2string(t) + ".");
70  return;
71  }
73  veh->enterLaneAtMove(veh->succEdge(1)->getLanes()[0], true);
74  }
75  myVehicles.push_back(VehicleInformation(veh,
77  veh->isParking()));
78 }
79 
80 
81 void
83  for (VehicleInfVector::iterator i = myVehicles.begin(); i != myVehicles.end(); ++i) {
84  if (i->myVeh == veh) {
85  myVehicles.erase(i);
86  break;
87  }
88  }
89  if (veh->getLane() != 0) {
90  myParkingVehicles[veh->getLane()].erase(veh);
91  }
92 }
93 
94 
95 void
97  // go through vehicles
98  for (VehicleInfVector::iterator i = myVehicles.begin(); i != myVehicles.end();) {
99  // get the vehicle information
100  VehicleInformation& desc = *i;
101 
102  if (desc.myParking) {
103  // handle parking vehicles
104  if (desc.myVeh->processNextStop(1) <= 0) {
105  ++i;
106  continue;
107  }
108  // parking finished, head back into traffic
109  }
110  const SUMOVehicleClass vclass = desc.myVeh->getVehicleType().getVehicleClass();
111  const MSEdge* e = desc.myVeh->getEdge();
112  const MSEdge* nextEdge = desc.myVeh->succEdge(1);
113 
114  // get the lane on which this vehicle should continue
115  // first select all the lanes which allow continuation onto nextEdge
116  // then pick the one which is least occupied
117  // @todo maybe parking vehicles should always continue on the rightmost lane?
118  const MSLane* oldLane = desc.myVeh->getLane();
119  const double departPos = desc.myParking ? desc.myVeh->getPositionOnLane() : 0;
120  MSLane* l = (nextEdge != 0 ? e->getFreeLane(e->allowedLanes(*nextEdge, vclass), vclass, departPos) :
121  e->getFreeLane(0, vclass, departPos));
122 
123  if (desc.myParking) {
124  // handle parking vehicles
127  myParkingVehicles[oldLane].erase(desc.myVeh);
128  i = myVehicles.erase(i);
129  } else {
130  i++;
131  }
132  } else {
133  // handle teleporting vehicles, lane may be 0 because permissions were modified by a closing rerouter or TraCI
134  if (l != 0 && l->freeInsertion(*(desc.myVeh), MIN2(l->getSpeedLimit(), desc.myVeh->getMaxSpeed()), MSMoveReminder::NOTIFICATION_TELEPORT)) {
135  WRITE_WARNING("Vehicle '" + desc.myVeh->getID() + "' ends teleporting on edge '" + e->getID() + "', time " + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".");
137  i = myVehicles.erase(i);
138  } else {
139  // could not insert. maybe we should proceed in virtual space
140  if (desc.myProceedTime < time) {
141  if (desc.myVeh->succEdge(1) == 0) {
142  WRITE_WARNING("Vehicle '" + desc.myVeh->getID() + "' teleports beyond arrival edge '" + e->getID() + "', time " + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".");
145  i = myVehicles.erase(i);
146  continue;
147  }
148  // let the vehicle move to the next edge
150  // active move reminders (i.e. rerouters)
151  desc.myVeh->enterLaneAtMove(desc.myVeh->succEdge(1)->getLanes()[0], true);
152  // use current travel time to determine when to move the vehicle forward
154  }
155  ++i;
156  }
157  }
158  }
159 }
160 
161 
162 bool
164  return !myVehicles.empty();
165 }
166 
167 
170  if (myInstance == 0) {
172  }
173  return myInstance;
174 }
175 
176 
178 
179 
181  myInstance = 0;
182 }
183 
184 
185 const std::set<const MSVehicle*>&
187  ParkingVehicles::const_iterator it = myParkingVehicles.find(lane);
188  if (it != myParkingVehicles.end()) {
189  return it->second;
190  } else {
191  return myEmptyVehicleSet;
192  }
193 }
194 
195 
196 void
198  std::map<const MSVehicle*, const MSLane*> parkingLanes;
199  for (ParkingVehicles::const_iterator it = myParkingVehicles.begin(); it != myParkingVehicles.end(); ++it) {
200  const std::set<const MSVehicle*>& vehs = it->second;
201  for (std::set<const MSVehicle*>::const_iterator it2 = vehs.begin(); it2 != vehs.end(); ++it2) {
202  parkingLanes[*it2] = it->first;
203  }
204  }
205  for (VehicleInfVector::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
207  out.writeAttr(SUMO_ATTR_ID, it->myVeh->getID());
208  out.writeAttr(SUMO_ATTR_DEPART, it->myProceedTime);
209  if (it->myParking) {
210  out.writeAttr(SUMO_ATTR_PARKING, parkingLanes[it->myVeh]->getID());
211  }
212  out.closeTag();
213  }
214 }
215 
216 
217 void
219  MSVehicle* veh = dynamic_cast<MSVehicle*>(vc.getVehicle(attrs.getString(SUMO_ATTR_ID)));
220  if (veh == 0) {
221  // deleted
222  return;
223  }
224  SUMOTime proceedTime = (SUMOTime)attrs.getLong(SUMO_ATTR_DEPART);
226  myVehicles.push_back(VehicleInformation(veh, proceedTime - offset, parkingLane != 0));
227  if (parkingLane != 0) {
228  myParkingVehicles[parkingLane].insert(veh);
229  veh->setTentativeLaneAndPosition(parkingLane, veh->getPositionOnLane());
230  veh->processNextStop(veh->getSpeed());
231  }
233 }
234 
235 
236 
237 /****************************************************************************/
238 
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:2732
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
static MSVehicleTransfer * myInstance
The static singleton-instance.
void remove(MSVehicle *veh)
Remove a vehicle from this transfer object.
Holds the information needed to move the vehicle over the network.
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:488
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:535
bool freeInsertion(MSVehicle &veh, double speed, MSMoveReminder::Notification notification=MSMoveReminder::NOTIFICATION_DEPARTED)
Tries to insert the given vehicle on any place.
Definition: MSLane.cpp:313
double getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:375
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:60
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:292
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:192
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
const std::string & getID() const
Returns the id.
Definition: Named.h:66
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
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:200
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:2921
const std::set< const MSVehicle * > & getParkingVehicles(const MSLane *lane) const
return parking vehicles on the given lane
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:711
double getMaxSpeed() const
Returns the maximum speed.
A road/street connecting two junctions.
Definition: MSEdge.h:80
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:2872
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:590
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:310
double getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:476
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:582
void onRemovalFromNet(const MSMoveReminder::Notification reason)
Called when the vehicle is removed from the network.
Definition: MSVehicle.cpp:598
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:592
ParkingVehicles myParkingVehicles
double getLateralPositionOnLane() const
Get the vehicle&#39;s lateral position on the lane.
Definition: MSVehicle.h:407
void alreadyDeparted(SUMOVehicle *veh)
stops trying to emit the given vehicle (because it already departed)
static MSVehicleTransfer * getInstance()
Returns the instance of this object.
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1371
The vehicle starts or ends parking.
void scheduleVehicleRemoval(SUMOVehicle *veh)
Removes a vehicle after it has ended.
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:1145
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:3584
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:363
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to)
Informs all added listeners about a vehicle&#39;s state change.
Definition: MSNet.cpp:811
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:71
bool closeTag()
Closes the most recently opened tag.
long long int SUMOTime
Definition: TraCIDefs.h:52
MSVehicle * myVeh
The vehicle itself.
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:1127
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:442
const std::string & getID() const
Returns the name of the vehicle.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
MSLane * getFreeLane(const std::vector< MSLane *> *allowed, const SUMOVehicleClass vclass, double departPos) const
Finds the emptiest lane allowing the vehicle class.
Definition: MSEdge.cpp:389
The vehicle ended being teleported.
Definition: MSNet.h:584
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)