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.dev/sumo
3 // Copyright (C) 2005-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 /****************************************************************************/
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>
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class OutputDevice;
37 class MSRouteProbe;
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
47 class MSCalibrator : public MSRouteHandler, public Command, public MSDetectorFileOutput {
48 public:
50  MSCalibrator(const std::string& id,
51  MSEdge* const edge,
52  MSLane* const lane,
53  MSJunction* const node,
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  const bool local,
62  const bool addLaneMeanData);
63 
65  virtual ~MSCalibrator();
66 
73  void writeXMLOutput(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime);
74 
75 
84  void writeXMLDetectorProlog(OutputDevice& dev) const;
85 
88  virtual SUMOTime execute(SUMOTime currentTime);
89 
91  static void cleanup();
92 
94  static const std::map<std::string, MSCalibrator*>& getInstances() {
95  return myInstances;
96  }
97 
98  struct AspiredState {
99  AspiredState() : begin(-1), end(-1), q(-1.), v(-1.), vehicleParameter(0) {}
102  double q;
103  double v;
105  };
106 
108 
109  const MSEdge* getEdge() const {
110  return myEdge;
111  }
112 
113  const MSLane* getLane() const {
114  return myLane;
115  }
116 
117  const MSRouteProbe* getRouteProbe() const {
118  return myProbe;
119  }
120 
121  inline virtual int passed() const {
122  // calibrator measures at start of segment
123  // vehicles drive to the end of an edge by default so they count as passed
124  // but vaporized vehicles do not count
125  // if the calibrator is located on a short edge, the vehicles are
126  // vaporized on the next edge so we cannot rely on myEdgeMeanData.nVehVaporized
128  }
129 
130  int getInserted() const {
132  }
133 
134  int getRemoved() const {
135  return myClearedInJam + myRemoved;
136  }
137 
138  void setFlow(SUMOTime begin, SUMOTime end, double vehsPerHour, double speed, SUMOVehicleParameter vehicleParameter);
139 
140 protected:
141  class CalibratorCommand : public Command {
142  public:
144  myCalibrator(cali) {}
145 
146  SUMOTime execute(SUMOTime currentTime) {
147  return myCalibrator->execute(currentTime);
148  }
149 
161  SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime);
162 
163  private:
165  };
166 
167 
169 
170 
178  virtual void myStartElement(int element,
179  const SUMOSAXAttributes& attrs);
180 
187  virtual void myEndElement(int element);
189 
190 
191 
193  public:
194  VehicleRemover(MSLane* const lane, MSCalibrator* const parent, const bool undo = false) :
195  MSMoveReminder(parent->getID(), lane, true), myParent(parent), myUndoCalibration(undo) {}
196 
198 
199 
210  virtual bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = nullptr);
211 
221  virtual bool notifyLeave(SUMOTrafficObject& veh, double lastPos, Notification reason, const MSLane* enteredLane = nullptr);
222 
223  void disable() {
224  myParent = nullptr;
225  }
226 
227  private:
229  const bool myUndoCalibration;
230  };
231  friend class GUICalibrator;
232 
233  // @return whether the current state is active (GUI)
234  bool isActive() const {
235  return myAmActive;
236  }
237 
238 protected:
239 
240  void intervalEnd();
241 
242  bool isCurrentStateActive(SUMOTime time);
243 
244  bool tryEmit(MSLane* lane, MSVehicle* vehicle);
245 
246  void init();
247 
249  int totalWished() const;
250 
252  double currentFlow() const;
253 
255  double currentSpeed() const;
256 
257  /* @brief returns whether the lane is jammed although it should not be
258  * @param[in] lane The lane to check or all for negative values
259  */
260  bool invalidJam(int laneIndex) const;
261 
262  inline int inserted() const {
263  return myInserted;
264  }
265  inline int removed() const {
266  return myRemoved;
267  }
268  inline int clearedInJam() const {
269  return myClearedInJam;
270  }
271 
272  /* @brief returns the number of vehicles (of the current type) that still
273  * fit on the given lane
274  * @param[in] lane The lane to check (return the maximum of all lanes for negative values)
275  */
276  int remainingVehicleCapacity(int laneIndex) const;
277 
279  virtual void reset();
280 
282  virtual void updateMeanData();
283 
287  return myToRemove.insert(veh->getID()).second;
288  }
289 
290 
293  bool removePending();
294 
296  std::string getNewVehicleID();
297 
298 protected:
300  MSEdge* const myEdge;
302  MSLane* const myLane;
306  const double myPos;
308  const MSRouteProbe* const myProbe;
312  std::vector<MSMeanData_Net::MSLaneMeanDataValues*> myLaneMeanData;
315 
317  std::vector<AspiredState> myIntervals;
319  std::vector<AspiredState>::const_iterator myCurrentStateInterval;
320 
321  std::vector<VehicleRemover*> myVehicleRemovers;
322 
327  std::set<std::string> myToRemove;
328 
331 
345  bool myDidInit;
350 
353 
356 
358  bool myAmLocal;
359 
362 
363  /* @brief objects which need to live longer than the MSCalibrator
364  * instance which created them */
365  static std::vector<MSMoveReminder*> myLeftoverReminders;
366  static std::vector<SUMOVehicleParameter*> myLeftoverVehicleParameters;
367  static std::map<std::string, MSCalibrator*> myInstances;
368 
369 };
long long int SUMOTime
Definition: GUI.h:35
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:143
SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime)
Reschedule or deschedule the command when quick-loading state.
SUMOTime execute(SUMOTime currentTime)
Executes the command.
Definition: MSCalibrator.h:146
VehicleRemover(MSLane *const lane, MSCalibrator *const parent, const bool undo=false)
Definition: MSCalibrator.h:194
virtual bool notifyLeave(SUMOTrafficObject &veh, double lastPos, Notification reason, const MSLane *enteredLane=nullptr)
Checks whether any calibrations need to be undone.
virtual bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=nullptr)
Checks whether the reminder is activated by a vehicle entering the lane.
Calibrates the flow on a segment to a specified one.
Definition: MSCalibrator.h:47
static std::map< std::string, MSCalibrator * > myInstances
Definition: MSCalibrator.h:367
bool tryEmit(MSLane *lane, MSVehicle *vehicle)
OutputDevice * myOutput
The device for xml statistics.
Definition: MSCalibrator.h:330
double myInvalidJamThreshold
relative speed threshold for detecting and clearing invalid jam
Definition: MSCalibrator.h:355
int getInserted() const
Definition: MSCalibrator.h:130
double currentSpeed() const
measured speed in the current interval
MSMeanData_Net myMeanDataParent
dummy parent to retrieve vType filter
Definition: MSCalibrator.h:310
MSEdge *const myEdge
the edge on which this calibrator lies
Definition: MSCalibrator.h:300
static const std::map< std::string, MSCalibrator * > & getInstances()
return all calibrator instances
Definition: MSCalibrator.h:94
static void cleanup()
cleanup remaining data structures
bool isActive() const
Definition: MSCalibrator.h:234
bool mySpeedIsDefault
The information whether the speed adaption has been reset.
Definition: MSCalibrator.h:341
virtual void updateMeanData()
aggregate lane values
const MSLane * getLane() const
Definition: MSCalibrator.h:113
int myRemoved
The number of vehicles that were removed in the current interval.
Definition: MSCalibrator.h:335
virtual int passed() const
Definition: MSCalibrator.h:121
MSCalibrator(const std::string &id, MSEdge *const edge, MSLane *const lane, MSJunction *const node, 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, const bool local, const bool addLaneMeanData)
const MSRouteProbe *const myProbe
the route probe to retrieve routes from
Definition: MSCalibrator.h:308
bool myAmActive
whether the calibrator was active when last checking
Definition: MSCalibrator.h:352
std::set< std::string > myToRemove
set of vehicle ids to remove
Definition: MSCalibrator.h:327
static std::vector< MSMoveReminder * > myLeftoverReminders
Definition: MSCalibrator.h:365
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:361
std::vector< MSMeanData_Net::MSLaneMeanDataValues * > myLaneMeanData
data collector for the calibrator
Definition: MSCalibrator.h:312
std::vector< AspiredState >::const_iterator myCurrentStateInterval
Iterator pointing to the current interval.
Definition: MSCalibrator.h:319
int clearedInJam() const
Definition: MSCalibrator.h:268
double currentFlow() const
flow in the current interval in veh/h
static std::vector< SUMOVehicleParameter * > myLeftoverVehicleParameters
Definition: MSCalibrator.h:366
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;.
int myInserted
The number of vehicles that were inserted in the current interval.
Definition: MSCalibrator.h:337
bool myHaveWarnedAboutClearingJam
The default (maximum) speed on the segment.
Definition: MSCalibrator.h:349
std::vector< AspiredState > myIntervals
List of adaptation intervals.
Definition: MSCalibrator.h:317
const double myPos
the position on the edge where this calibrator lies
Definition: MSCalibrator.h:306
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:321
const MSRouteProbe * getRouteProbe() const
Definition: MSCalibrator.h:117
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:314
bool invalidJam(int laneIndex) const
int myClearedInJam
The number of vehicles that were removed when clearin a jam.
Definition: MSCalibrator.h:339
int getRemoved() const
Definition: MSCalibrator.h:134
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
virtual ~MSCalibrator()
double myDefaultSpeed
The default (maximum) speed on the segment.
Definition: MSCalibrator.h:347
virtual SUMOTime execute(SUMOTime currentTime)
MSLane *const myLane
the lane on which this calibrator lies (nullptr if the whole edge is covered at once)
Definition: MSCalibrator.h:302
bool scheduleRemoval(SUMOTrafficObject *veh)
try to schedule the given vehicle for removal. return true if it isn't already scheduled
Definition: MSCalibrator.h:286
bool myDidInit
The information whether init was called.
Definition: MSCalibrator.h:345
int removed() const
Definition: MSCalibrator.h:265
bool myAmLocal
whether the calibrator needs to undo the calibration after the edge / junction has been left
Definition: MSCalibrator.h:358
void intervalEnd()
int inserted() const
Definition: MSCalibrator.h:262
AspiredState getCurrentStateInterval() const
bool isCurrentStateActive(SUMOTime time)
const MSEdge * getEdge() const
Definition: MSCalibrator.h:109
MSJunction *const myNode
the junction on which this calibrator lies (nullptr if is edge or lane specific)
Definition: MSCalibrator.h:304
SUMOTime myFrequency
The frequeny with which to check for calibration.
Definition: MSCalibrator.h:333
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:343
Base of value-generating classes (detectors)
A road/street connecting two junctions.
Definition: MSEdge.h:77
The base class for an intersection.
Definition: MSJunction.h:58
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
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
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
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:104