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-2023 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/****************************************************************************/
18// An algorithm that performs Idling for the taxi device
19/****************************************************************************/
20#include <config.h>
21
22#include <limits>
23#include <microsim/MSNet.h>
24#include <microsim/MSEdge.h>
25#include <microsim/MSLane.h>
26#include <microsim/MSStop.h>
30#include <mesosim/MELoop.h>
31#include "MSRoutingEngine.h"
32#include "MSIdling.h"
33
34//#define DEBUG_IDLING
35//#define DEBUG_COND(obj) (obj->getHolder().getID() == "p0")
36//#define DEBUG_COND(obj) (obj->getHolder().isSelected())
37#define DEBUG_COND(obj) (true)
38
39
40// ===========================================================================
41// MSIdling_stop methods
42// ===========================================================================
43
44void
46 if (!taxi->getHolder().hasStops()) {
47#ifdef DEBUG_IDLING
48 if (DEBUG_COND(taxi)) {
49 std::cout << SIMTIME << " taxi=" << taxi->getHolder().getID() << " MSIdling_Stop add stop\n";
50 }
51#endif
52 std::string errorOut;
53 double brakeGap = 0;
54 std::pair<const MSLane*, double> stopPos;
56 // stops are only checked in MESegment::receive so we need to put this onto the next segment
57 MSBaseVehicle& veh = dynamic_cast<MSBaseVehicle&>(taxi->getHolder());
60 MESegment* stopSeg = curSeg->getNextSegment();
61 if (stopSeg == nullptr) {
62 if ((ri + 1) != veh.getRoute().end()) {
63 stopSeg = MSGlobals::gMesoNet->getSegmentForEdge(**(ri + 1), 0);
64 } else {
65 WRITE_WARNINGF(TL("Idle taxi '%' has no next segment to stop. time=%."), taxi->getHolder().getID(), time2string(SIMSTEP));
66 return;
67 }
68 }
69 // determine offset of stopSeg
70 double stopOffset = 0;
71 const MSEdge& stopEdge = stopSeg->getEdge();
73 while (seg != stopSeg) {
74 stopOffset += seg->getLength();
75 seg = seg->getNextSegment();
76 }
77 stopPos = std::make_pair(stopEdge.getLanes()[0], stopOffset);
78 } else {
79 MSVehicle& veh = dynamic_cast<MSVehicle&>(taxi->getHolder());
80 brakeGap = veh.getCarFollowModel().brakeGap(veh.getSpeed());
81 stopPos = veh.getLanePosAfterDist(brakeGap);
82 }
83 if (stopPos.first != nullptr) {
86 stop.edge = stopPos.first->getEdge().getID();
87 } else {
88 stop.lane = stopPos.first->getID();
89 }
90 stop.startPos = stopPos.second;
91 stop.endPos = stopPos.second + POSITION_EPS;
92 if (taxi->getHolder().getVehicleType().getContainerCapacity() > 0) {
93 stop.containerTriggered = true;
94 } else {
95 stop.triggered = true;
96 }
97 stop.actType = "idling";
99 taxi->getHolder().addTraciStop(stop, errorOut);
100 if (errorOut != "") {
101 WRITE_WARNING(errorOut);
102 }
103 } else {
104 WRITE_WARNINGF(TL("Idle taxi '%' could not stop within %m"), taxi->getHolder().getID(), toString(brakeGap));
105 }
106 } else {
107 MSStop& stop = taxi->getHolder().getNextStop();
108#ifdef DEBUG_IDLING
109 if (DEBUG_COND(taxi)) {
110 std::cout << SIMTIME << " taxi=" << taxi->getHolder().getID() << " MSIdling_Stop reusing stop with duration " << time2string(stop.duration) << "\n";
111 }
112#endif
113 if (taxi->getHolder().getVehicleType().getContainerCapacity() > 0) {
114 stop.containerTriggered = true;
115 } else {
116 stop.triggered = true;
117 }
118 }
119}
120
121
122// ===========================================================================
123// MSIdling_RandomCircling methods
124// ===========================================================================
125
126void
128 SUMOVehicle& veh = taxi->getHolder();
129 ConstMSEdgeVector edges = veh.getRoute().getEdges();
130 ConstMSEdgeVector newEdges;
131 double remainingDist = -veh.getPositionOnLane();
132 int remainingEdges = 0;
133 int routePos = veh.getRoutePosition();
134 const int routeLength = (int)edges.size();
135 while (routePos + 1 < routeLength && (remainingEdges < 2 || remainingDist < 200)) {
136 const MSEdge* edge = edges[routePos];
137 remainingDist += edge->getLength();
138 remainingEdges++;
139 routePos++;
140 newEdges.push_back(edge);
141 }
142 const MSEdge* lastEdge = edges.back();
143 newEdges.push_back(lastEdge);
144 int added = 0;
145 while (remainingEdges < 2 || remainingDist < 200) {
146 remainingDist += lastEdge->getLength();
147 remainingEdges++;
148 MSEdgeVector successors = lastEdge->getSuccessors(veh.getVClass());
149 for (auto it = successors.begin(); it != successors.end();) {
150 if ((*it)->getFunction() == SumoXMLEdgeFunc::CONNECTOR) {
151 it = successors.erase(it);
152 } else {
153 it++;
154 }
155 }
156 if (successors.size() == 0) {
157 WRITE_WARNINGF(TL("Vehicle '%' ends idling in a cul-de-sac"), veh.getID());
158 break;
159 } else {
160 int nextIndex = RandHelper::rand((int)successors.size(), veh.getRNG());
161 newEdges.push_back(successors[nextIndex]);
162 lastEdge = newEdges.back();
163 added++;
164 }
165 }
166 if (added > 0) {
167 //std::cout << SIMTIME << " circleVeh=" << veh.getID() << " newEdges=" << toString(newEdges) << "\n";
168 veh.replaceRouteEdges(newEdges, -1, 0, "taxi:idling:randomCircling", false, false, false);
169 }
170}
171
172// ===========================================================================
173// MSIdling_TaxiStand methods
174// ===========================================================================
175
176void
178 MSBaseVehicle& veh = dynamic_cast<MSBaseVehicle&>(taxi->getHolder());
179
181 if (rerouteDef == nullptr || rerouteDef->parkProbs.getVals().size() == 0) {
182 if (!myHaveWarned) {
183 WRITE_WARNINGF(TL("Could not determine taxi stand for vehicle '%' at time=%"), veh.getID(), time2string(SIMSTEP));
184 myHaveWarned = true;
185 }
186 return;
187 }
188 MSStop* lastStop = nullptr;
189 if (veh.hasStops()) {
190 lastStop = &veh.getStop((int)veh.getStops().size() - 1);
191 }
192 if (lastStop == nullptr || lastStop->parkingarea == nullptr) {
193 const MSParkingArea* pa = rerouteDef->parkProbs.getVals().front().first;
195 stop.lane = pa->getLane().getID();
196 stop.startPos = pa->getBeginLanePosition();
197 stop.endPos = pa->getEndLanePosition();
198
199 if (taxi->getHolder().getVehicleType().getContainerCapacity() > 0) {
200 stop.containerTriggered = true;
201 } else {
202 stop.triggered = true;
203 }
204 stop.actType = "idling";
205 stop.parkingarea = pa->getID();
207 const int nextStopIndex = (int)veh.getStops().size();
208 std::string error;
209 if (!veh.insertStop(nextStopIndex, stop, "taxi:taxistand", false, error)) {
210 WRITE_WARNING("Stop insertion failed for idling taxi '" + veh.getID() + "' (" + error + ").");
211 }
212 //std::cout << SIMTIME << " taxistandsVeh=" << veh.getID() << " driving to parkingArea " << pa->getID() << "\n";
214 } else {
215 //std::cout << SIMTIME << " taxistandsVeh=" << veh.getID() << " already driving to parkingArea\n";
216 }
217}
218
219
220
221/****************************************************************************/
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:74
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
#define DEBUG_COND(obj)
Definition: MSIdling.cpp:37
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:56
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:271
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:270
#define TL(string)
Definition: MsgHandler.h:287
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
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:359
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:234
The base class for microscopic and mesoscopic vehicles.
Definition: MSBaseVehicle.h:55
const MSRouteIterator & getCurrentRouteEdge() const
Returns an iterator pointing to the current edge in this vehicles route.
const std::list< MSStop > & getStops() const
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:658
const MSEdgeVector & getSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
Returns the following edges, restricted by vClass.
Definition: MSEdge.cpp:1156
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:127
void idle(MSDevice_Taxi *taxi)
computes Idling and updates reservations
Definition: MSIdling.cpp:45
MSTriggeredRerouter * myRerouter
Definition: MSIdling.h:71
void idle(MSDevice_Taxi *taxi)
computes Idling and updates reservations
Definition: MSIdling.cpp:177
@ NOTIFICATION_PARKING_REROUTE
The vehicle needs another parking area.
A lane area vehicles can halt at.
Definition: MSParkingArea.h:58
const ConstMSEdgeVector & getEdges() const
Definition: MSRoute.h:124
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:79
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, SUMOVehicle &veh) const
Returns the rerouting definition valid for the given time and vehicle, 0 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:6321
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:978
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 const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual SUMOVehicleClass getVClass() const =0
Returns the object's access class.
virtual SumoRNG * getRNG() const =0
Returns the associated RNG for this object.
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:62
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 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
virtual const MSRoute & getRoute() const =0
Returns the current route.
Definition of vehicle stop (position and duration)
std::string edge
The edge to stop at (used only in netedit)
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.