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.org/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>
28#include <mesosim/MELoop.h>
29#include "MSRoutingEngine.h"
30#include "MSIdling.h"
31
32//#define DEBUG_IDLING
33//#define DEBUG_COND(obj) (obj->getHolder().getID() == "p0")
34//#define DEBUG_COND(obj) (obj->getHolder().isSelected())
35#define DEBUG_COND(obj) (true)
36
37
38// ===========================================================================
39// MSIdling_stop methods
40// ===========================================================================
41
42void
44 if (!taxi->getHolder().hasStops()) {
45#ifdef DEBUG_IDLING
46 if (DEBUG_COND(taxi)) {
47 std::cout << SIMTIME << " taxi=" << taxi->getHolder().getID() << " MSIdling_Stop add stop\n";
48 }
49#endif
50 std::string errorOut;
51 double brakeGap = 0;
52 std::pair<const MSLane*, double> stopPos;
54 // stops are only checked in MESegment::receive so we need to put this onto the next segment
55 MSBaseVehicle& veh = dynamic_cast<MSBaseVehicle&>(taxi->getHolder());
58 MESegment* stopSeg = curSeg->getNextSegment();
59 if (stopSeg == nullptr) {
60 if ((ri + 1) != veh.getRoute().end()) {
61 stopSeg = MSGlobals::gMesoNet->getSegmentForEdge(**(ri + 1), 0);
62 } else {
63 WRITE_WARNINGF(TL("Idle taxi '%' has no next segment to stop. time=%."), taxi->getHolder().getID(), time2string(SIMSTEP));
64 return;
65 }
66 }
67 // determine offset of stopSeg
68 double stopOffset = 0;
69 const MSEdge& stopEdge = stopSeg->getEdge();
71 while (seg != stopSeg) {
72 stopOffset += seg->getLength();
73 seg = seg->getNextSegment();
74 }
75 stopPos = std::make_pair(stopEdge.getLanes()[0], stopOffset);
76 } else {
77 MSVehicle& veh = dynamic_cast<MSVehicle&>(taxi->getHolder());
78 brakeGap = veh.getCarFollowModel().brakeGap(veh.getSpeed());
79 stopPos = veh.getLanePosAfterDist(brakeGap);
80 }
81 if (stopPos.first != nullptr) {
84 stop.edge = stopPos.first->getEdge().getID();
85 } else {
86 stop.lane = stopPos.first->getID();
87 }
88 stop.startPos = stopPos.second;
89 stop.endPos = stopPos.second + POSITION_EPS;
90 if (taxi->getHolder().getVehicleType().getContainerCapacity() > 0) {
91 stop.containerTriggered = true;
92 } else {
93 stop.triggered = true;
94 }
95 stop.actType = "idling";
97 taxi->getHolder().addTraciStop(stop, errorOut);
98 if (errorOut != "") {
99 WRITE_WARNING(errorOut);
100 }
101 } else {
102 WRITE_WARNINGF(TL("Idle taxi '%' could not stop within %m"), taxi->getHolder().getID(), toString(brakeGap));
103 }
104 } else {
105 MSStop& stop = taxi->getHolder().getNextStop();
106#ifdef DEBUG_IDLING
107 if (DEBUG_COND(taxi)) {
108 std::cout << SIMTIME << " taxi=" << taxi->getHolder().getID() << " MSIdling_Stop reusing stop with duration " << time2string(stop.duration) << "\n";
109 }
110#endif
111 if (taxi->getHolder().getVehicleType().getContainerCapacity() > 0) {
112 stop.containerTriggered = true;
113 } else {
114 stop.triggered = true;
115 }
116 }
117}
118
119
120// ===========================================================================
121// MSIdling_RandomCircling methods
122// ===========================================================================
123
124void
126 SUMOVehicle& veh = taxi->getHolder();
127 ConstMSEdgeVector edges = veh.getRoute().getEdges();
128 ConstMSEdgeVector newEdges;
129 double remainingDist = -veh.getPositionOnLane();
130 int remainingEdges = 0;
131 int routePos = veh.getRoutePosition();
132 const int routeLength = (int)edges.size();
133 while (routePos + 1 < routeLength && (remainingEdges < 2 || remainingDist < 200)) {
134 const MSEdge* edge = edges[routePos];
135 remainingDist += edge->getLength();
136 remainingEdges++;
137 routePos++;
138 newEdges.push_back(edge);
139 }
140 const MSEdge* lastEdge = edges.back();
141 newEdges.push_back(lastEdge);
142 int added = 0;
143 while (remainingEdges < 2 || remainingDist < 200) {
144 remainingDist += lastEdge->getLength();
145 remainingEdges++;
146 MSEdgeVector successors = lastEdge->getSuccessors(veh.getVClass());
147 for (auto it = successors.begin(); it != successors.end();) {
148 if ((*it)->getFunction() == SumoXMLEdgeFunc::CONNECTOR) {
149 it = successors.erase(it);
150 } else {
151 it++;
152 }
153 }
154 if (successors.size() == 0) {
155 WRITE_WARNINGF(TL("Vehicle '%' ends idling in a cul-de-sac"), veh.getID());
156 break;
157 } else {
158 int nextIndex = RandHelper::rand((int)successors.size(), veh.getRNG());
159 newEdges.push_back(successors[nextIndex]);
160 lastEdge = newEdges.back();
161 added++;
162 }
163 }
164 if (added > 0) {
165 //std::cout << SIMTIME << " circleVeh=" << veh.getID() << " newEdges=" << toString(newEdges) << "\n";
166 veh.replaceRouteEdges(newEdges, -1, 0, "taxi:idling:randomCircling", false, false, false);
167 }
168}
169
170
171/****************************************************************************/
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:74
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
#define DEBUG_COND(obj)
Definition: MSIdling.cpp:35
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:56
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:268
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:267
#define TL(string)
Definition: MsgHandler.h:284
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
#define SIMSTEP
Definition: SUMOTime.h:60
#define SIMTIME
Definition: SUMOTime.h:61
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 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:125
void idle(MSDevice_Taxi *taxi)
computes Idling and updates reservations
Definition: MSIdling.cpp:43
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
SUMOTime duration
The stopping duration.
Definition: MSStop.h:67
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:6211
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:973
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
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.
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