Eclipse SUMO - Simulation of Urban MObility
MSIdling.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2007-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
19 // An algorithm that performs Idling for the taxi device
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <limits>
24 #include <microsim/MSNet.h>
25 #include <microsim/MSEdge.h>
26 #include <microsim/MSLane.h>
27 #include <microsim/MSStop.h>
28 #include <microsim/MSParkingArea.h>
31 #include <mesosim/MELoop.h>
32 #include "MSRoutingEngine.h"
33 #include "MSIdling.h"
34 
35 //#define DEBUG_IDLING
36 //#define DEBUG_COND(obj) (obj->getHolder().getID() == "p0")
37 //#define DEBUG_COND(obj) (obj->getHolder().isSelected())
38 #define DEBUG_COND(obj) (true)
39 
40 
41 // ===========================================================================
42 // MSIdling_stop methods
43 // ===========================================================================
44 
45 void
47  if (!taxi->getHolder().hasStops()) {
48 #ifdef DEBUG_IDLING
49  if (DEBUG_COND(taxi)) {
50  std::cout << SIMTIME << " taxi=" << taxi->getHolder().getID() << " MSIdling_Stop add stop\n";
51  }
52 #endif
53  std::string errorOut;
54  double brakeGap = 0;
55  std::pair<const MSLane*, double> stopPos;
57  // stops are only checked in MESegment::receive so we need to put this onto the next segment
58  MSBaseVehicle& veh = dynamic_cast<MSBaseVehicle&>(taxi->getHolder());
61  MESegment* stopSeg = curSeg->getNextSegment();
62  if (stopSeg == nullptr) {
63  if ((ri + 1) != veh.getRoute().end()) {
64  stopSeg = MSGlobals::gMesoNet->getSegmentForEdge(**(ri + 1), 0);
65  } else {
66  WRITE_WARNINGF(TL("Idle taxi '%' has no next segment to stop. time=%."), taxi->getHolder().getID(), time2string(SIMSTEP));
67  return;
68  }
69  }
70  // determine offset of stopSeg
71  double stopOffset = 0;
72  const MSEdge& stopEdge = stopSeg->getEdge();
74  while (seg != stopSeg) {
75  stopOffset += seg->getLength();
76  seg = seg->getNextSegment();
77  }
78  stopPos = std::make_pair(stopEdge.getLanes()[0], stopOffset);
79  } else {
80  MSVehicle& veh = dynamic_cast<MSVehicle&>(taxi->getHolder());
81  brakeGap = veh.getCarFollowModel().brakeGap(veh.getSpeed());
82  stopPos = veh.getLanePosAfterDist(brakeGap);
83  }
84  if (stopPos.first != nullptr) {
87  stop.edge = stopPos.first->getEdge().getID();
88  } else {
89  stop.lane = stopPos.first->getID();
90  }
91  stop.startPos = stopPos.second;
92  stop.endPos = stopPos.second + POSITION_EPS;
93  if (taxi->getHolder().getVehicleType().getContainerCapacity() > 0) {
94  stop.containerTriggered = true;
95  } else {
96  stop.triggered = true;
97  }
98  stop.actType = "idling";
100  taxi->getHolder().addTraciStop(stop, errorOut);
101  if (errorOut != "") {
102  WRITE_WARNING(errorOut);
103  }
104  } else {
105  WRITE_WARNINGF(TL("Idle taxi '%' could not stop within %m"), taxi->getHolder().getID(), toString(brakeGap));
106  }
107  } else {
108  MSStop& stop = taxi->getHolder().getNextStop();
109 #ifdef DEBUG_IDLING
110  if (DEBUG_COND(taxi)) {
111  std::cout << SIMTIME << " taxi=" << taxi->getHolder().getID() << " MSIdling_Stop reusing stop with duration " << time2string(stop.duration) << "\n";
112  }
113 #endif
114  if (taxi->getHolder().getVehicleType().getContainerCapacity() > 0) {
115  stop.containerTriggered = true;
116  } else {
117  stop.triggered = true;
118  }
119  }
120 }
121 
122 
123 // ===========================================================================
124 // MSIdling_RandomCircling methods
125 // ===========================================================================
126 
127 void
129  SUMOVehicle& veh = taxi->getHolder();
130  ConstMSEdgeVector edges = veh.getRoute().getEdges();
131  ConstMSEdgeVector newEdges;
132  double remainingDist = -veh.getPositionOnLane();
133  int remainingEdges = 0;
134  int routePos = veh.getRoutePosition();
135  const int routeLength = (int)edges.size();
136  while (routePos + 1 < routeLength && (remainingEdges < 2 || remainingDist < 200)) {
137  const MSEdge* edge = edges[routePos];
138  remainingDist += edge->getLength();
139  remainingEdges++;
140  routePos++;
141  newEdges.push_back(edge);
142  }
143  const MSEdge* lastEdge = edges.back();
144  newEdges.push_back(lastEdge);
145  int added = 0;
146  while (remainingEdges < 2 || remainingDist < 200) {
147  remainingDist += lastEdge->getLength();
148  remainingEdges++;
149  MSEdgeVector successors = lastEdge->getSuccessors(veh.getVClass());
150  for (auto it = successors.begin(); it != successors.end();) {
151  if ((*it)->getFunction() == SumoXMLEdgeFunc::CONNECTOR) {
152  it = successors.erase(it);
153  } else {
154  it++;
155  }
156  }
157  if (successors.size() == 0) {
158  WRITE_WARNINGF(TL("Vehicle '%' ends idling in a cul-de-sac"), veh.getID());
159  break;
160  } else {
161  int nextIndex = RandHelper::rand((int)successors.size(), veh.getRNG());
162  newEdges.push_back(successors[nextIndex]);
163  lastEdge = newEdges.back();
164  added++;
165  }
166  }
167  if (added > 0) {
168  //std::cout << SIMTIME << " circleVeh=" << veh.getID() << " newEdges=" << toString(newEdges) << "\n";
169  veh.replaceRouteEdges(newEdges, -1, 0, "taxi:idling:randomCircling", false, false, false);
170  }
171 }
172 
173 // ===========================================================================
174 // MSIdling_TaxiStand methods
175 // ===========================================================================
176 
177 void
179  MSBaseVehicle& veh = dynamic_cast<MSBaseVehicle&>(taxi->getHolder());
180 
182  if (rerouteDef == nullptr || rerouteDef->parkProbs.getVals().size() == 0) {
183  if (!myHaveWarned) {
184  WRITE_WARNINGF(TL("Could not determine taxi stand for vehicle '%' at time=%"), veh.getID(), time2string(SIMSTEP));
185  myHaveWarned = true;
186  }
187  return;
188  }
189  MSStop* lastStop = nullptr;
190  if (veh.hasStops()) {
191  lastStop = &veh.getStop((int)veh.getStops().size() - 1);
192  }
193  if (lastStop == nullptr || lastStop->parkingarea == nullptr) {
194  const MSParkingArea* pa = rerouteDef->parkProbs.getVals().front().first;
196  stop.lane = pa->getLane().getID();
197  stop.startPos = pa->getBeginLanePosition();
198  stop.endPos = pa->getEndLanePosition();
199 
200  if (taxi->getHolder().getVehicleType().getContainerCapacity() > 0) {
201  stop.containerTriggered = true;
202  } else {
203  stop.triggered = true;
204  }
205  stop.actType = "idling";
206  stop.parkingarea = pa->getID();
208  const int nextStopIndex = (int)veh.getStops().size();
209  std::string error;
210  if (!veh.insertStop(nextStopIndex, stop, "taxi:taxistand", false, error)) {
211  WRITE_WARNING("Stop insertion failed for idling taxi '" + veh.getID() + "' (" + error + ").");
212  } else {
213  //std::cout << SIMTIME << " taxistandsVeh=" << veh.getID() << " driving to parkingArea " << pa->getID() << "\n";
215  }
216  } else {
217  //std::cout << SIMTIME << " taxistandsVeh=" << veh.getID() << " already driving to parkingArea\n";
218  }
219 }
220 
221 
222 
223 /****************************************************************************/
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:74
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
#define DEBUG_COND(obj)
Definition: MSIdling.cpp:38
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:56
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:296
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:295
#define TL(string)
Definition: MsgHandler.h:315
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
#define SIMSTEP
Definition: SUMOTime.h:61
#define SIMTIME
Definition: SUMOTime.h:62
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:325
A single mesoscopic segment (cell)
Definition: MESegment.h:49
double getLength() const
Returns the length of the segment in meters.
Definition: MESegment.h:242
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:234
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:359
The base class for microscopic and mesoscopic vehicles.
Definition: MSBaseVehicle.h:55
const std::list< MSStop > & getStops() const
const MSRouteIterator & getCurrentRouteEdge() const
Returns an iterator pointing to the current edge in this vehicles route.
bool hasStops() const
Returns whether the vehicle has to stop somewhere.
virtual void activateReminders(const MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
"Activates" all current move reminder
MSStop & getStop(int nextStopIndex)
bool insertStop(int nextStopIndex, SUMOVehicleParameter::Stop stop, const std::string &info, bool teleport, std::string &errorMsg)
const MSRoute & getRoute() const
Returns the current route.
double brakeGap(const double speed) const
Returns the distance the vehicle needs to halt including driver's reaction time tau (i....
Definition: MSCFModel.h:380
A device which collects info on the vehicle trip (mainly on departure and arrival)
Definition: MSDevice_Taxi.h:49
A road/street connecting two junctions.
Definition: MSEdge.h:77
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:168
double getLength() const
return the length of the edge
Definition: MSEdge.h:662
const MSEdgeVector & getSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
Returns the following edges, restricted by vClass.
Definition: MSEdge.cpp:1194
static bool gUseMesoSim
Definition: MSGlobals.h:103
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:109
void idle(MSDevice_Taxi *taxi)
computes Idling and updates reservations
Definition: MSIdling.cpp:128
void idle(MSDevice_Taxi *taxi)
computes Idling and updates reservations
Definition: MSIdling.cpp:46
MSTriggeredRerouter * myRerouter
Definition: MSIdling.h:71
void idle(MSDevice_Taxi *taxi)
computes Idling and updates reservations
Definition: MSIdling.cpp:178
@ NOTIFICATION_PARKING_REROUTE
The vehicle needs another parking area.
A lane area vehicles can halt at.
Definition: MSParkingArea.h:60
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:79
const ConstMSEdgeVector & getEdges() const
Definition: MSRoute.h:124
Definition: MSStop.h:44
bool triggered
whether an arriving person lets the vehicle continue
Definition: MSStop.h:69
bool containerTriggered
whether an arriving container lets the vehicle continue
Definition: MSStop.h:71
MSParkingArea * parkingarea
(Optional) parkingArea if one is assigned to the stop
Definition: MSStop.h:58
SUMOTime duration
The stopping duration.
Definition: MSStop.h:67
double getBeginLanePosition() const
Returns the begin position of this stop.
double getEndLanePosition() const
Returns the end position of this stop.
const MSLane & getLane() const
Returns the lane this stop is located at.
const RerouteInterval * getCurrentReroute(SUMOTime time, SUMOTrafficObject &obj) const
Returns the rerouting definition valid for the given time and object, nullptr if none.
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
std::pair< const MSLane *, double > getLanePosAfterDist(double distance) const
return lane and position along bestlanes at the given distance
Definition: MSVehicle.cpp:6436
double getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:493
const MSCFModel & getCarFollowModel() const
Returns the vehicle's car following model definition.
Definition: MSVehicle.h:974
int getContainerCapacity() const
Get this vehicle type's container capacity.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.cpp:94
const std::vector< T > & getVals() const
Returns the members of the distribution.
virtual SumoRNG * getRNG() const =0
Returns the associated RNG for this object.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual SUMOVehicleClass getVClass() const =0
Returns the object's access class.
virtual int getRoutePosition() const =0
return index of edge within route
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
virtual bool replaceRouteEdges(ConstMSEdgeVector &edges, double cost, double savings, const std::string &info, bool onInit=false, bool check=false, bool removeStops=true, std::string *msgReturn=nullptr)=0
Replaces the current route by the given edges.
virtual const MSRoute & getRoute() const =0
Returns the current route.
virtual bool hasStops() const =0
Returns whether the vehicle has to stop somewhere.
virtual MSStop & getNextStop()=0
virtual bool addTraciStop(SUMOVehicleParameter::Stop stop, std::string &errorMsg)=0
Definition of vehicle stop (position and duration)
std::string edge
The edge to stop at.
ParkingType parking
whether the vehicle is removed from the net while stopping
std::string lane
The lane to stop at.
std::string parkingarea
(Optional) parking area if one is assigned to the stop
double startPos
The stopping position start.
std::string actType
act Type (only used by Persons) (used by netedit)
bool triggered
whether an arriving person lets the vehicle continue
double endPos
The stopping position end.
bool containerTriggered
whether an arriving container lets the vehicle continue
RandomDistributor< ParkingAreaVisible > parkProbs
The distributions of new parking areas to use as destinations.