Eclipse SUMO - Simulation of Urban MObility
NEMAController.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
19 // An actuated NEMA-phase-compliant traffic light logic
20 /****************************************************************************/
21 #pragma once
22 #include <config.h>
23 
24 #include <utility>
25 #include <vector>
26 #include <bitset>
27 #include <map>
32 #include <set>
33 
34 
35 // ===========================================================================
36 // class declarations
37 // ===========================================================================
38 class NLDetectorBuilder;
39 class MSE2Collector;
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
49 public:
50 
52  typedef std::map<MSLane*, MSE2Collector*> LaneDetectorMap;
53 
55  typedef std::map<MSE2Collector*, MSLane*, ComparatorIdLess> DetectorLaneMap;
56 
66  NEMALogic(MSTLLogicControl& tlcontrol,
67  const std::string& id, const std::string& programID,
68  const SUMOTime offset,
70  int step, SUMOTime delay,
71  const std::map<std::string, std::string>& parameter,
72  const std::string& basePath);
73 
74 
79  void init(NLDetectorBuilder& nb) override;
80 
82  ~NEMALogic();
83 
84 
85  SUMOTime trySwitch() override;
86 
89 
93  const MSPhaseDefinition& getCurrentPhaseDef() const override;
95 
97  void activateProgram() override;
98  void deactivateProgram() override;
99 
100  bool showDetectors() const {
101  return myShowDetectors;
102  }
103 
104  void setShowDetectors(bool show);
105 
106  std::map<int, std::vector<MSE2Collector*>> getPhase2DetectorMap() {
107  std::map<int, std::vector<MSE2Collector*>> temp;
108  for (auto const& detectInfo : phase2DetectorMap) {
109  temp[detectInfo.first] = detectInfo.second.detectors;
110  }
111  return temp;
112  }
113 
115  std::map<std::string, double> getDetectorStates() const override;
116 
117  // control logic
118  std::string NEMA_control();
119 
120  std::string combineStates(std::string state1, std::string state2);
121 
122  bool isDetectorActivated(int phaseNumber, int depth) const;
123 
124  int nextPhase(std::vector<int> ring, int phaseNum, int& distance, bool sameAllowed);
125 
126  std::tuple<int, int> getNextPhases(int currentR1Index, int currentR2Index, bool toUpdateR1, bool toUpdateR2, bool stayOk = false);
127 
128  double ModeCycle(double a, double b);
129 
130  std::string transitionState(std::string curState, int RYG);
131 
132  std::set<std::string> getLaneIDsFromNEMAState(std::string state);
133 
134  void setNewMaxGreens(std::vector<double> newMaxGreens);
135  void setNewSplits(std::vector<double> newSplits);
136  void setNewCycleLength(double newCycleLength);
137  void setNewOffset(double newOffset);
138 
139  // not using for now, but could be helpful for cycle change controller
141  return myCycleLength;
142  }
143 
144  void setCycleLength(double newCycleLength) {
145  myCycleLength = newCycleLength;
146  }
147 
148  bool isGreenPhase(std::string state) {
149  for (char ch : state) {
150  if (ch == 'g' || ch == 'G') {
151  return true;
152  }
153  }
154  return false;
155  }
156 
158  void setParameter(const std::string& key, const std::string& value) override;
159 
161  const std::string getParameter(const std::string& key, const std::string defaultValue = "") const override;
162 
163 protected:
164  // create a small datatype for mapping detector to phase index
165  // This is the one copied from MSActuatedTrafficLightLogic
166  // not used in our controller, but it is here for meeting the SUMO default traffic logic light check
167  // this one and related could be removed with extra efforts
168  struct DetectorInfo {
169  DetectorInfo(MSE2Collector* _det, int numPhases):
170  det(_det),
171  servedPhase(numPhases, false)
172  {}
175  std::vector<bool> servedPhase;
176  };
177 
178  typedef std::vector<std::vector<DetectorInfo*>> detectorMap;
179 
180  // Light Head State
181  enum lightState {
182  YELLOW = 0,
183  RED = 1,
184  GREEN = 2,
187  };
188 
190  bool hasMajor(const std::string& state, const LaneVector& lanes) const;
192  std::vector<int> readParaFromString(std::string s);
193 
194  //convert laneIDs string to string vector
195  std::vector<std::string> string2vector(std::string s);
196 
197  //decide whether the detector is for left turn lane
198  //if it is, use the detector length for left turn lane
199  bool isLeftTurnLane(const MSLane* const lane) const;
200 
201  //convert "1" to int 1
202  int string2int(std::string s);
203 
204  // protected:
206 
207  std::vector<DetectorInfo> myDetectorInfoVector;
208 
211 
214 
215  // detector length
217 
218  // detector length for left turn lane
220 
221  // total cycle length
223 
224  // total cycle length in the next cycle
226 
229 
231  std::string myFile;
232 
235 
237  std::string myVehicleTypes;
238 
239  //"3,4,1,2"
240  std::string ring1;
241  //"7,8,5,6"
242  std::string ring2;
243  /*
244  {
245  {3,4,1,2},
246  {7,8,5,6}
247  }
248  */
249  std::vector<std::vector<int>> rings;
250  // "4,8"
251  std::string barriers;
252  // "2,6"
253  std::string coordinates;
254 
255  //size = 2. 0->ring1 1->ring2
256  std::vector<int> barrierPhaseIndecies;
257  std::vector<int> coordinatePhaseIndecies;
258 
259  /*
260  This serves as a mapping to speed up phaseSelection
261  {
262  {{3, 4}, {1, 2}},
263  {{7, 8}, {5, 6}}
264  }
265  */
266  std::vector<std::vector<int>> myRingBarrierMapping[2];
267 
268  // Creating a small extensible datatype for including information about the phase's detectors
269  // this is different than DetectorInfo, as it is per-phase not per-detector.
270  // Purpose is that when we check detectors, we may need to have per-detector settings handy
273  detectors(),
274  cpdTarget(),
275  cpdSource()
276  {}
277  phaseDetectorInfo(int _cross_phase_source):
278  cpdSource(_cross_phase_source)
279  {}
280  std::vector<MSE2Collector*> detectors = {nullptr};
281  int cpdTarget = 0;
282  int cpdSource = 0;
283  };
284 
285  // myNextPhase needs to be presevered in memory because the phase is calculated at start of yellow
286  // but not implementend until the end of red
289 
290  bool minRecalls[8] {};
291  bool maxRecalls[8] {};
292  bool recall[8] {};
293 
294  // std::vector<std::vector<int>> barriers;
295  //init 0 and then 1,2,3,0
296  //redundant need to remove
299  //init 0. 0->barrierPhases; 1->coordinatePhases
300  // bool coordinatePhases;
301 
302  //rings[0][activeRing1Index]
304  //rings[1][activeRing1Index]
306 
307  /*
308  {
309  {1 : phaseDetectorInfo{
310  detectors: {det1, det2, ...},
311  crossPhaseDetector: 6
312  },
313  },
314  {2 : ...
315  }
316  */
317  std::map<int, phaseDetectorInfo> phase2DetectorMap;
318 
319  double minGreen[8] {};
320  double maxGreen[8] {};
321  double nextMaxGreen[8] {};
322  double vehExt[8] {};
323  double yellowTime[8] {};
324  double redTime[8] {};
325  double phaseStartTime[8] {};
326  double forceOffs[8] {};
327  double phaseCutOffs[8] {};
328  double phaseExpectedDuration[8] {};
329 
331 
335  double cycleRefPoint;// missing update
336  //activeR1phase
338  double offset;
339  double myNextOffset;
342 
343  std::map<int, std::vector<std::string>> phase2ControllerLanesMap;
344 
346 
347  std::string currentState;
348  std::string currentR1State;
349  std::string currentR2State;
350 
351  std::string outputStateFilePath;
352  std::ofstream outputStateFile;
354 
357 
359  void constructBarrierMap(int ring, std::vector<std::vector<int>>& barrierMap);
360  int findBarrier(int desiredPhase, int ring);
361 
362  // Green Transfer Option
364 
365  // handle error
366  void error_handle_not_set(std::string param_variable, std::string param_name);
367  void validate_timing();
368 };
std::vector< int > LaneVector
container for (sorted) lanes. The lanes are sorted from rightmost (id=0) to leftmost (id=nolanes-1)
Definition: NBCont.h:55
long long int SUMOTime
Definition: SUMOTime.h:32
An areal detector corresponding to a sequence of consecutive lanes.
Definition: MSE2Collector.h:79
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
The definition of a single phase of a tls logic.
A fixed traffic light logic.
A class that stores and controls tls and switching of their programs.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
A NEMA (adaptive) traffic light logic based on E2Detector.
bool recall[8]
double myNextOffset
std::string barriers
void setCycleLength(double newCycleLength)
std::vector< std::vector< DetectorInfo * > > detectorMap
std::map< int, phaseDetectorInfo > phase2DetectorMap
std::map< std::string, double > getDetectorStates() const override
retrieve all detectors used by this program
double phaseEndTimeR1
bool isDetectorActivated(int phaseNumber, int depth) const
int r2coordinatePhase
void setNewMaxGreens(std::vector< double > newMaxGreens)
double maxGreen[8]
const std::string getParameter(const std::string &key, const std::string defaultValue="") const override
try to get the value of the given parameter. Parameters prefixed with 'NEMA.' control functionality
std::vector< DetectorInfo > myDetectorInfoVector
void init(NLDetectorBuilder &nb) override
Initialises the tls with information about incoming lanes.
double cycleRefPoint
std::string myVehicleTypes
Whether detector output separates by vType.
std::string currentState
int activeRing2Index
std::string myFile
The output file for generated detectors.
std::string coordinates
double myDetectorLength
double yellowTime[8]
std::string combineStates(std::string state1, std::string state2)
void constructBarrierMap(int ring, std::vector< std::vector< int >> &barrierMap)
helps to construct myRingBarrierMapping
double redTime[8]
bool showDetectors() const
std::vector< std::vector< int > > rings
int activeRing1Index
SUMOTime myFreq
The frequency for aggregating detector output.
int r1coordinatePhase
DetectorLaneMap myDetectorLaneMap
A map from detectors to lanes.
double offset
SUMOTime trySwitch() override
Switches to the next phase.
std::vector< int > coordinatePhaseIndecies
void validate_timing()
std::string ring2
double minGreen[8]
int nextPhase(std::vector< int > ring, int phaseNum, int &distance, bool sameAllowed)
int findBarrier(int desiredPhase, int ring)
std::vector< std::string > string2vector(std::string s)
bool greenTransfer
std::string transitionState(std::string curState, int RYG)
NEMALogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const SUMOTime offset, const MSSimpleTrafficLightLogic::Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameter, const std::string &basePath)
Constructor.
void activateProgram() override
called when switching programs
void setShowDetectors(bool show)
double vehExt[8]
bool whetherOutputState
std::map< int, std::vector< MSE2Collector * > > getPhase2DetectorMap()
bool maxRecalls[8]
double phaseExpectedDuration[8]
lightState R2RYG
std::string outputStateFilePath
double phaseEndTimeR2
double nextMaxGreen[8]
bool fixForceOff
std::tuple< int, int > getNextPhases(int currentR1Index, int currentR2Index, bool toUpdateR1, bool toUpdateR2, bool stayOk=false)
std::string ring1
void setParameter(const std::string &key, const std::string &value) override
try to set the given parameter. Parameters prefixed with 'NEMA.' control functionality
void error_handle_not_set(std::string param_variable, std::string param_name)
std::string NEMA_control()
bool wait4R1Green
void setNewOffset(double newOffset)
std::set< std::string > getLaneIDsFromNEMAState(std::string state)
bool minRecalls[8]
std::map< MSLane *, MSE2Collector * > LaneDetectorMap
Definition of a map from lanes to corresponding area detectors.
double myNextCycleLength
std::string currentR2State
bool isGreenPhase(std::string state)
lightState R1RYG
std::vector< int > readParaFromString(std::string s)
std::map< MSE2Collector *, MSLane *, ComparatorIdLess > DetectorLaneMap
Definition of a map from detectors to corresponding lanes.
double phaseCutOffs[8]
std::string currentR1State
bool myShowDetectors
Whether the detectors shall be shown in the GUI.
int activeRing2Phase
detectorMap myDetectorForPhase
int string2int(std::string s)
bool hasMajor(const std::string &state, const LaneVector &lanes) const
return whether there is a major link from the given lane in the given phase
bool coordinateMode
void deactivateProgram() override
~NEMALogic()
Destructor.
int activeRing1Phase
bool wait4R2Green
MSPhaseDefinition myPhase
virtual phase that holds the current state
double forceOffs[8]
double phaseStartTime[8]
double myDetectorLengthLeftTurnLane
double myCycleLength
double getCurrentCycleLength()
std::ofstream outputStateFile
bool isLeftTurnLane(const MSLane *const lane) const
LaneDetectorMap myLaneDetectorMap
A map from lanes to detectors.
std::vector< int > barrierPhaseIndecies
double ModeCycle(double a, double b)
void setNewCycleLength(double newCycleLength)
std::vector< std::vector< int > > myRingBarrierMapping[2]
const MSPhaseDefinition & getCurrentPhaseDef() const override
Returns the definition of the current phase.
std::map< int, std::vector< std::string > > phase2ControllerLanesMap
void setNewSplits(std::vector< double > newSplits)
Builds detectors for microsim.
DetectorInfo(MSE2Collector *_det, int numPhases)
std::vector< bool > servedPhase
phaseDetectorInfo(int _cross_phase_source)
std::vector< MSE2Collector * > detectors