Eclipse SUMO - Simulation of Urban MObility
MSCalibrator.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2022 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 /****************************************************************************/
20 // Calibrates the flow on an edge by removing an inserting vehicles
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <string>
26 #include <vector>
27 #include <utils/common/Command.h>
32 
33 
34 // ===========================================================================
35 // class declarations
36 // ===========================================================================
37 class OutputDevice;
38 class MSRouteProbe;
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
48 class MSCalibrator : public MSTrigger, public MSRouteHandler, public Command, public MSDetectorFileOutput {
49 public:
51  MSCalibrator(const std::string& id,
52  const MSEdge* const edge,
53  MSLane* lane,
54  const double pos,
55  const std::string& aXMLFilename,
56  const std::string& outputFilename,
57  const SUMOTime freq, const double length,
58  const MSRouteProbe* probe,
59  const double invalidJamThreshold,
60  const std::string& vTypes,
61  bool addLaneMeanData = true);
62 
64  virtual ~MSCalibrator();
65 
72  void writeXMLOutput(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
73 
74 
83  void writeXMLDetectorProlog(OutputDevice& dev) const;
84 
87  virtual SUMOTime execute(SUMOTime currentTime);
88 
89  const std::string& getID() const {
91  return MSTrigger::getID();
92  }
93 
95  static void cleanup();
96 
98  static const std::map<std::string, MSCalibrator*>& getInstances() {
99  return myInstances;
100  }
101 
102  struct AspiredState {
103  AspiredState() : begin(-1), end(-1), q(-1.), v(-1.), vehicleParameter(0) {}
106  double q;
107  double v;
109  };
110 
112 
113  const MSEdge* getEdge() const {
114  return myEdge;
115  }
116 
117  const MSLane* getLane() const {
118  return myLane;
119  }
120 
121  const MSRouteProbe* getRouteProbe() const {
122  return myProbe;
123  }
124 
125  inline virtual int passed() const {
126  // calibrator measures at start of segment
127  // vehicles drive to the end of an edge by default so they count as passed
128  // but vaporized vehicles do not count
129  // if the calibrator is located on a short edge, the vehicles are
130  // vaporized on the next edge so we cannot rely on myEdgeMeanData.nVehVaporized
132  }
133 
134  int getInserted() const {
136  }
137 
138  int getRemoved() const {
139  return myClearedInJam + myRemoved;
140  }
141 
142  void setFlow(SUMOTime begin, SUMOTime end, double vehsPerHour, double speed, SUMOVehicleParameter vehicleParameter);
143 
144 protected:
145  class CalibratorCommand : public Command {
146  public:
148  myCalibrator(cali) {}
149 
150  SUMOTime execute(SUMOTime currentTime) {
151  return myCalibrator->execute(currentTime);
152  }
153 
154  private:
156  };
157 
158 
160 
161 
169  virtual void myStartElement(int element,
170  const SUMOSAXAttributes& attrs);
171 
178  virtual void myEndElement(int element);
180 
181 
182 
184  public:
186  MSMoveReminder(parent->getID(), lane, true), myParent(parent) {}
187 
189 
190 
201  virtual bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
202 
203  void disable() {
204  myParent = 0;
205  }
206 
207  private:
209  };
210  friend class VehicleRemover;
211  friend class GUICalibrator;
212 
213  // @return whether the current state is active (GUI)
214  bool isActive() const {
215  return myAmActive;
216  }
217 
218 protected:
219 
220  void intervalEnd();
221 
222  bool isCurrentStateActive(SUMOTime time);
223 
224  bool tryEmit(MSLane* lane, MSVehicle* vehicle);
225 
226  void init();
227 
229  int totalWished() const;
230 
232  double currentFlow() const;
233 
235  double currentSpeed() const;
236 
237  /* @brief returns whether the lane is jammed although it should not be
238  * @param[in] lane The lane to check or all for negative values
239  */
240  bool invalidJam(int laneIndex) const;
241 
242  inline int inserted() const {
243  return myInserted;
244  }
245  inline int removed() const {
246  return myRemoved;
247  }
248  inline int clearedInJam() const {
249  return myClearedInJam;
250  }
251 
252  /* @brief returns the number of vehicles (of the current type) that still
253  * fit on the given lane
254  * @param[in] lane The lane to check (return the maximum of all lanes for negative values)
255  */
256  int remainingVehicleCapacity(int laneIndex) const;
257 
259  virtual void reset();
260 
262  virtual void updateMeanData();
263 
267  return myToRemove.insert(veh->getID()).second;
268  }
269 
270 
273  bool removePending();
274 
276  std::string getNewVehicleID();
277 
278 protected:
280  const MSEdge* const myEdge;
282  MSLane* const myLane;
284  const double myPos;
286  const MSRouteProbe* const myProbe;
290  std::vector<MSMeanData_Net::MSLaneMeanDataValues*> myLaneMeanData;
293 
295  std::vector<AspiredState> myIntervals;
297  std::vector<AspiredState>::const_iterator myCurrentStateInterval;
298 
299  std::vector<VehicleRemover*> myVehicleRemovers;
300 
305  std::set<std::string> myToRemove;
306 
309 
323  bool myDidInit;
328 
331 
334 
337 
338  /* @brief objects which need to live longer than the MSCalibrator
339  * instance which created them */
340  static std::vector<MSMoveReminder*> LeftoverReminders;
341  static std::vector<SUMOVehicleParameter*> LeftoverVehicleParameters;
342  static std::map<std::string, MSCalibrator*> myInstances;
343 
344 };
long long int SUMOTime
Definition: SUMOTime.h:32
Base (microsim) event class.
Definition: Command.h:50
Changes the speed allowed on a set of lanes (gui version)
Definition: GUICalibrator.h:42
CalibratorCommand(MSCalibrator *cali)
Definition: MSCalibrator.h:147
SUMOTime execute(SUMOTime currentTime)
Executes the command.
Definition: MSCalibrator.h:150
virtual bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Checks whether the reminder is activated by a vehicle entering the lane.
VehicleRemover(MSLane *lane, MSCalibrator *parent)
Definition: MSCalibrator.h:185
Calibrates the flow on a segment to a specified one.
Definition: MSCalibrator.h:48
static std::map< std::string, MSCalibrator * > myInstances
Definition: MSCalibrator.h:342
bool tryEmit(MSLane *lane, MSVehicle *vehicle)
OutputDevice * myOutput
The device for xml statistics.
Definition: MSCalibrator.h:308
double myInvalidJamThreshold
relative speed threshold for detecting and clearing invalid jam
Definition: MSCalibrator.h:333
int getInserted() const
Definition: MSCalibrator.h:134
double currentSpeed() const
measured speed in the current interval
const MSEdge *const myEdge
the edge on which this calibrator lies
Definition: MSCalibrator.h:280
MSMeanData_Net myMeanDataParent
dummy parent to retrieve vType filter
Definition: MSCalibrator.h:288
static const std::map< std::string, MSCalibrator * > & getInstances()
return all calibrator instances
Definition: MSCalibrator.h:98
static void cleanup()
cleanup remaining data structures
bool isActive() const
Definition: MSCalibrator.h:214
static std::vector< SUMOVehicleParameter * > LeftoverVehicleParameters
Definition: MSCalibrator.h:341
bool mySpeedIsDefault
The information whether the speed adaption has been reset.
Definition: MSCalibrator.h:319
virtual void updateMeanData()
aggregate lane values
const MSLane * getLane() const
Definition: MSCalibrator.h:117
int myRemoved
The number of vehicles that were removed in the current interval.
Definition: MSCalibrator.h:313
virtual int passed() const
Definition: MSCalibrator.h:125
const MSRouteProbe *const myProbe
the route probe to retrieve routes from
Definition: MSCalibrator.h:286
bool myAmActive
whether the calibrator was active when last checking
Definition: MSCalibrator.h:330
std::set< std::string > myToRemove
set of vehicle ids to remove
Definition: MSCalibrator.h:305
void setFlow(SUMOTime begin, SUMOTime end, double vehsPerHour, double speed, SUMOVehicleParameter vehicleParameter)
bool myHaveInvalidJam
whether the calibrator has registered an invalid jam in the last execution step
Definition: MSCalibrator.h:336
std::vector< MSMeanData_Net::MSLaneMeanDataValues * > myLaneMeanData
data collector for the calibrator
Definition: MSCalibrator.h:290
std::vector< AspiredState >::const_iterator myCurrentStateInterval
Iterator pointing to the current interval.
Definition: MSCalibrator.h:297
int clearedInJam() const
Definition: MSCalibrator.h:248
double currentFlow() const
flow in the current interval in veh/h
bool removePending()
remove any vehicles which are scheduled for removal. return true if removals took place
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
const std::string & getID() const
Definition: MSCalibrator.h:89
int myInserted
The number of vehicles that were inserted in the current interval.
Definition: MSCalibrator.h:315
bool myHaveWarnedAboutClearingJam
The default (maximum) speed on the segment.
Definition: MSCalibrator.h:327
std::vector< AspiredState > myIntervals
List of adaptation intervals.
Definition: MSCalibrator.h:295
const double myPos
the position on the edge where this calibrator lies
Definition: MSCalibrator.h:284
virtual void myEndElement(int element)
Called on the closing of a tag;.
int totalWished() const
number of vehicles expected to pass this interval
std::vector< VehicleRemover * > myVehicleRemovers
Definition: MSCalibrator.h:299
const MSRouteProbe * getRouteProbe() const
Definition: MSCalibrator.h:121
void writeXMLOutput(OutputDevice &dev, SUMOTime startTime, SUMOTime stopTime)
Write the generated output to the given device.
std::string getNewVehicleID()
determine id of new vehicle from calibrator state
MSMeanData_Net::MSLaneMeanDataValues myEdgeMeanData
accumlated data for the whole edge
Definition: MSCalibrator.h:292
static std::vector< MSMoveReminder * > LeftoverReminders
Definition: MSCalibrator.h:340
bool invalidJam(int laneIndex) const
int myClearedInJam
The number of vehicles that were removed when clearin a jam.
Definition: MSCalibrator.h:317
int getRemoved() const
Definition: MSCalibrator.h:138
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
virtual ~MSCalibrator()
double myDefaultSpeed
The default (maximum) speed on the segment.
Definition: MSCalibrator.h:325
virtual SUMOTime execute(SUMOTime currentTime)
MSLane *const myLane
the lane on which this calibrator lies (0 if the whole edge is covered at once)
Definition: MSCalibrator.h:282
bool scheduleRemoval(SUMOTrafficObject *veh)
try to schedule the given vehicle for removal. return true if it isn't already scheduled
Definition: MSCalibrator.h:266
bool myDidInit
The information whether init was called.
Definition: MSCalibrator.h:323
int removed() const
Definition: MSCalibrator.h:245
void intervalEnd()
int inserted() const
Definition: MSCalibrator.h:242
AspiredState getCurrentStateInterval() const
bool isCurrentStateActive(SUMOTime time)
const MSEdge * getEdge() const
Definition: MSCalibrator.h:113
SUMOTime myFrequency
The frequeny with which to check for calibration.
Definition: MSCalibrator.h:311
MSCalibrator(const std::string &id, const MSEdge *const edge, MSLane *lane, const double pos, const std::string &aXMLFilename, const std::string &outputFilename, const SUMOTime freq, const double length, const MSRouteProbe *probe, const double invalidJamThreshold, const std::string &vTypes, bool addLaneMeanData=true)
virtual void reset()
reset collected vehicle data
int remainingVehicleCapacity(int laneIndex) const
bool myDidSpeedAdaption
The information whether speed was adapted in the current interval.
Definition: MSCalibrator.h:321
Base of value-generating classes (detectors)
A road/street connecting two junctions.
Definition: MSEdge.h:77
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
Data structure for mean (aggregated) edge/lane values.
int nVehEntered
The number of vehicles that entered this lane within the sample interval.
Network state mean data collector for edges/lanes.
Something on a lane to be noticed about vehicle movement.
Notification
Definition of a vehicle state.
Parser and container for routes during their loading.
Writes routes of vehicles passing a certain edge.
Definition: MSRouteProbe.h:58
An abstract device that changes the state of the micro simulation.
Definition: MSTrigger.h:38
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:75
const std::string & getID() const
Returns the id.
Definition: Named.h:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
Encapsulated SAX-Attributes.
Representation of a vehicle, person, or container.
Structure representing possible vehicle parameter.
SUMOVehicleParameter * vehicleParameter
Definition: MSCalibrator.h:108