SUMO - Simulation of Urban MObility
MSDevice_SSM.h
Go to the documentation of this file.
1 /****************************************************************************/
8 // A device which stands as an implementation example and which outputs movereminder calls
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 200132014 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 #ifndef MSDevice_SSM_h
22 #define MSDevice_SSM_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include "MSDevice.h"
35 #include <utils/common/SUMOTime.h>
37 #include <utils/geom/Position.h>
38 
39 
40 // ===========================================================================
41 // class declarations
42 // ===========================================================================
43 class SUMOVehicle;
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
58 class MSCrossSection;
59 
60 class MSDevice_SSM : public MSDevice {
61 
62 private:
64  static std::set<MSDevice*>* instances;
65 
69  // Other vehicle is closer than range, but not on a conflicting lane
71  // Ego and foe vehicles' edges form a part of a consecutive sequence of edges
72  // This type may be specified further by ENCOUNTER_TYPE_FOLLOWING_LEADER or ENCOUNTER_TYPE_FOLLOWING_FOLLOWER
74  // Ego vehicle is on an edge that has a sequence of successors connected to the other vehicle's edge
76  // Other vehicle is on an edge that has a sequence of successors connected to the ego vehicle's current edge
78  // Ego and foe share an upcoming edge of their routes while the merging point for the routes is still ahead
79  // This type may be specified further by ENCOUNTER_TYPE_MERGING_LEADER or ENCOUNTER_TYPE_MERGING_FOLLOWER
81  // Other vehicle is on an edge that has a sequence of successors connected to an edge on the ego vehicle's route
82  // and the estimated arrival vehicle at the merge point is earlier for the ego than for the foe
84  // Other vehicle is on an edge that has a sequence of successors connected to an edge on the ego vehicle's route
85  // and the estimated arrival vehicle at the merge point is earlier for the foe than for the ego
87  // Ego's and foe's routes have crossing edges
88  // This type may be specified further by ENCOUNTER_TYPE_CROSSING_LEADER or ENCOUNTER_TYPE_CROSSING_FOLLOWER
90  // Other vehicle is on an edge that has a sequence of successors leading to an internal edge that crosses the ego vehicle's edge at a junction
91  // and the estimated arrival vehicle at the merge point is earlier for the ego than for the foe
93  // Other vehicle is on an edge that has a sequence of successors leading to an internal edge that crosses the ego vehicle's edge at a junction
94  // and the estimated arrival vehicle at the merge point is earlier for the foe than for the ego
96  };
97 
98 
99 
100 private:
103  class Encounter {
104  private:
107  struct Trajectory {
108  // positions
110  // momentary speeds
112  };
113 
114  public:
116  Encounter(const MSVehicle* _ego, const MSVehicle* const _foe, double _begin);
118  ~Encounter();
120  void add(double time, Position egoX, Position egoV, Position foeX, Position foeV);
122  struct compare {
123  typedef bool value_type;
124  bool operator()(Encounter* e1, Encounter* e2) {
125  return e1->begin <= e2->begin;
126  };
127  };
128 
129 
130 
131  public:
132  const MSVehicle* ego;
133  const MSVehicle* const foe;
134  double begin, end;
136 
137 // /// @brief tuple time x point of entry to the potential conflict
138 // std::pair<double, MSCrossSection> egoConflictEntry;
139 // std::pair<double, MSCrossSection> foeConflictEntry;
140 // std::pair<double, MSCrossSection> egoConflictExit;
141 // std::pair<double, MSCrossSection> foeConflictExit;
142 
144  std::vector<double> timespan;
149 
151  std::vector<double> TTCspan;
153  std::vector<double> DRACspan;
154 
157  std::pair<double, double> maxDRAC;
158  std::pair<double, double> minTTC;
159  std::pair<double, double> PET;
161 
162  private:
164  Encounter(const Encounter&);
166  Encounter& operator=(const Encounter&);
168  };
169 
173  struct FoeInfo {
176  };
177  // TODO: consider introducing a class foeCollector, which holds the foe info content
178  // plus a vehicle container to be used in findSurrounding vehicles.
179  // findSurroundingVehicles() would then deliver a vector of such foeCollectors
180  // (one for each possible egoConflictLane) instead of a map vehicle->foeInfo
181 
182 
183  typedef std::priority_queue<Encounter*, std::vector<Encounter*>, Encounter::compare> EncounterQueue;
184  typedef std::vector<Encounter*> EncounterVector;
185  typedef std::map<const MSVehicle*, FoeInfo*> FoeInfoMap;
186 public:
187 
191  static void insertOptions(OptionsCont& oc);
192 
193 
204  static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into);
205 
206 
209  static const std::set<MSDevice*>& getInstances();
210 
216  void updateAndWriteOutput();
217 
218 private:
219  void update();
220  void writeOutConflict(Encounter* e);
221 
222 public:
225  static void cleanup();
226 
227 
228 public:
230  ~MSDevice_SSM();
231 
232 
244  static void findSurroundingVehicles(const MSVehicle& veh, double range, FoeInfoMap& foeCollector);
245 
248  static void getUpstreamVehicles(const MSEdge* edge, double pos, double range, double egoDistToConflictLane, const MSLane* const egoConflictLane, FoeInfoMap& foeCollector);
249 
252  static void getVehiclesOnJunction(const MSJunction*, double egoDistToConflictLane, const MSLane* const egoConflictLane, FoeInfoMap& foeCollector);
253 
254 
257 
267  bool notifyMove(SUMOVehicle& veh, double oldPos,
268  double newPos, double newSpeed);
269 
270 
280  bool notifyEnter(SUMOVehicle& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
281 
282 
291  bool notifyLeave(SUMOVehicle& veh, double lastPos,
292  MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
294 
295 
297  const std::string deviceName() const {
298  return "ssm";
299  }
300 
307  void generateOutput() const;
308 
309 
310 
311 private:
322  MSDevice_SSM(SUMOVehicle& holder, const std::string& id, std::string outputFilename, std::vector<std::string> measures, std::vector<double> thresholds,
323  bool trajectories, double frequency, double range);
324 
325 
331  void processEncounters(FoeInfoMap& foes);
332 
335  void createEncounters(FoeInfoMap& foes);
336 
337 
340  void resetEncounters();
341 
345  void flushConflicts(bool all = false);
346 
349  void updateEncounter(Encounter* e, FoeInfo* foeInfo);
350 
359  const MSLane* findFoeConflictLane(const MSVehicle* foe, const MSLane* egoConflictLane, double& distToConflictLane);
360 
363  void closeEncounter(Encounter* e);
364 
368 
373  void computeSSMs(Encounter* e);
374 
375 
378  static std::string getOutputFilename(const SUMOVehicle& v, std::string deviceID);
379  static double getDetectionRange(const SUMOVehicle& v);
380  static double getLoggingFrequency(const SUMOVehicle& v);
381  static bool requestsTrajectories(const SUMOVehicle& v);
382  static bool getMeasuresAndThresholds(const SUMOVehicle& v, std::string deviceID,
383  std::vector<double>& thresholds, std::vector<std::string>& measures);
385 
386 private:
389  std::vector<std::string> myMeasures;
390  std::vector<double> myThresholds;
394  double myFrequency;
395  double myRange;
402 
403 
407  EncounterVector myActiveEncounters;
409  EncounterQueue myPastConflicts;
411 
414 
415 private:
417  MSDevice_SSM(const MSDevice_SSM&);
418 
421 
422 
423 };
424 
425 #endif
426 
427 /****************************************************************************/
428 
void processEncounters(FoeInfoMap &foes)
Finds encounters for which the foe vehicle has disappeared from range and if an ended encounter is qu...
std::vector< double > timespan
time points corresponding to the trajectories
Definition: MSDevice_SSM.h:144
static const std::set< MSDevice * > & getInstances()
returns all currently existing SSM devices
static void getVehiclesOnJunction(const MSJunction *, double egoDistToConflictLane, const MSLane *const egoConflictLane, FoeInfoMap &foeCollector)
Collects all vehicles on the junction into foeCollector.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
static bool getMeasuresAndThresholds(const SUMOVehicle &v, std::string deviceID, std::vector< double > &thresholds, std::vector< std::string > &measures)
void createEncounters(FoeInfoMap &foes)
Makes new encounters for all given vehicles (these should be the ones entering the device&#39;s range in ...
bool notifyLeave(SUMOVehicle &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Called whenever the holder leaves a lane.
bool operator()(Encounter *e1, Encounter *e2)
Definition: MSDevice_SSM.h:124
A simple description of a position on a lane (crossing of a lane)
std::pair< double, double > maxDRAC
Definition: MSDevice_SSM.h:157
static void cleanup()
Clean up remaining devices instances.
EncounterType
Different types of encounters corresponding to relative positions of the vehicles. The name describes the type from the ego perspective.
Definition: MSDevice_SSM.h:68
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_SSM-options.
MSVehicle * myHolderMS
Definition: MSDevice_SSM.h:400
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
Definition: MSDevice_SSM.h:60
The base class for an intersection.
Definition: MSJunction.h:64
EncounterVector myActiveEncounters
Definition: MSDevice_SSM.h:407
const MSLane * findFoeConflictLane(const MSVehicle *foe, const MSLane *egoConflictLane, double &distToConflictLane)
Computes the conflict lane for the foe.
static double getLoggingFrequency(const SUMOVehicle &v)
Notification
Definition of a vehicle state.
void closeEncounter(Encounter *e)
Finalizes the encounter and calculates SSM values.
static double getDetectionRange(const SUMOVehicle &v)
std::vector< double > TTCspan
All values for TTC.
Definition: MSDevice_SSM.h:151
ENCOUNTER_TYPE_CROSSING.
Definition: MSDevice_SSM.h:89
void updateEncounter(Encounter *e, FoeInfo *foeInfo)
Updates the encounter (adds a new trajectory point) and deletes the foeInfo.
void generateOutput() const
Finalizes output. Called on vehicle removal.
ENCOUNTER_TYPE_CROSSING_FOLLOWER.
Definition: MSDevice_SSM.h:95
static bool requestsTrajectories(const SUMOVehicle &v)
ENCOUNTER_TYPE_CROSSING_LEADER.
Definition: MSDevice_SSM.h:92
std::vector< double > DRACspan
All values for DRAC.
Definition: MSDevice_SSM.h:153
ENCOUNTER_TYPE_FOLLOWING_LEADER.
Definition: MSDevice_SSM.h:77
A road/street connecting two junctions.
Definition: MSEdge.h:80
int maxTrajectorySize
Corresponding maximal trajectory size in points, derived from myFrequency.
Definition: MSDevice_SSM.h:397
void add(double time, Position egoX, Position egoV, Position foeX, Position foeV)
add a new data point
ENCOUNTER_TYPE_MERGING_FOLLOWER.
Definition: MSDevice_SSM.h:86
Encounter(const MSVehicle *_ego, const MSVehicle *const _foe, double _begin)
Constructor.
ENCOUNTER_TYPE_MERGING.
Definition: MSDevice_SSM.h:80
void computeSSMs(Encounter *e)
double myFrequency
Maximal timespan duration for a single encounter.
Definition: MSDevice_SSM.h:394
static void getUpstreamVehicles(const MSEdge *edge, double pos, double range, double egoDistToConflictLane, const MSLane *const egoConflictLane, FoeInfoMap &foeCollector)
Collects all vehicles within range &#39;range&#39; upstream of the position &#39;pos&#39; on the edge &#39;edge&#39; into foe...
Representation of a vehicle.
Definition: SUMOVehicle.h:67
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A trajectory encloses a series of positions x and speeds v for one vehicle (the times are stored only...
Definition: MSDevice_SSM.h:107
bool myComputeTTC
Flags for switching on / off comutation of different SSMs, derived from myMeasures.
Definition: MSDevice_SSM.h:399
A list of positions.
OutputDevice * myOutputFile
Output device.
Definition: MSDevice_SSM.h:413
void resetEncounters()
Closes all current Encounters and moves conflicts to myPastConflicts,.
double myRange
Definition: MSDevice_SSM.h:395
ENCOUNTER_TYPE_FOLLOWING_FOLLOWER.
Definition: MSDevice_SSM.h:75
MSDevice_SSM(SUMOVehicle &holder, const std::string &id, std::string outputFilename, std::vector< std::string > measures, std::vector< double > thresholds, bool trajectories, double frequency, double range)
Constructor.
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Called whenever the holder enteres a lane.
bool qualifiesAsConflict(Encounter *e)
Tests if the SSM values exceed the threshold for qualification as conflict.
ENCOUNTER_TYPE_MERGING_LEADER.
Definition: MSDevice_SSM.h:83
void updateAndWriteOutput()
This is called once per time step in MSNet::writeOutput() and collects the surrounding vehicles...
ENCOUNTER_TYPE_FOLLOWING.
Definition: MSDevice_SSM.h:73
bool mySaveTrajectories
Definition: MSDevice_SSM.h:391
static std::string getOutputFilename(const SUMOVehicle &v, std::string deviceID)
std::vector< std::string > myMeasures
Definition: MSDevice_SSM.h:389
const MSVehicle * ego
Definition: MSDevice_SSM.h:132
void flushConflicts(bool all=false)
Writes out all past conflicts that have begun earlier than time t-myFrequency (i.e. no active encounter can have an earlier begin)
Compares encounters regarding to their start time.
Definition: MSDevice_SSM.h:122
Abstract in-vehicle device.
Definition: MSDevice.h:71
bool myComputeDRAC
Definition: MSDevice_SSM.h:399
std::vector< double > myThresholds
Definition: MSDevice_SSM.h:390
std::vector< Encounter * > EncounterVector
Definition: MSDevice_SSM.h:184
std::map< const MSVehicle *, FoeInfo * > FoeInfoMap
Definition: MSDevice_SSM.h:185
const MSLane * egoConflictLane
Definition: MSDevice_SSM.h:174
Trajectory egoTrajectory
Trajectory of the ego vehicle.
Definition: MSDevice_SSM.h:146
static std::set< MSDevice * > * instances
All currently existing SSM devices.
Definition: MSDevice_SSM.h:64
~MSDevice_SSM()
Destructor.
A storage for options typed value containers)
Definition: OptionsCont.h:99
Encounter & operator=(const Encounter &)
Invalidated assignment operator.
ENCOUNTER_TYPE_NOCONFLICT.
Definition: MSDevice_SSM.h:70
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
An encounter is an episode involving two vehicles, which are closer to each other than some specified...
Definition: MSDevice_SSM.h:103
std::pair< double, double > minTTC
Definition: MSDevice_SSM.h:158
std::pair< double, double > PET
Definition: MSDevice_SSM.h:159
void writeOutConflict(Encounter *e)
bool notifyMove(SUMOVehicle &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
const MSVehicle *const foe
Definition: MSDevice_SSM.h:133
EncounterQueue myPastConflicts
Past encounters that where qualified as conflicts and are not yet flushed to the output file...
Definition: MSDevice_SSM.h:409
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
Trajectory foeTrajectory
Trajectory of the foe vehicle.
Definition: MSDevice_SSM.h:148
static void findSurroundingVehicles(const MSVehicle &veh, double range, FoeInfoMap &foeCollector)
Returns all vehicles, which are within the given range of the given vehicle.
std::priority_queue< Encounter *, std::vector< Encounter * >, Encounter::compare > EncounterQueue
Definition: MSDevice_SSM.h:183
const std::string deviceName() const
return the name for this type of device
Definition: MSDevice_SSM.h:297