Eclipse SUMO - Simulation of Urban MObility
MSSimpleTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 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 /****************************************************************************/
22 // A fixed traffic light logic
23 /****************************************************************************/
24 #include <config.h>
25 
26 #include <cassert>
27 #include <utility>
28 #include <vector>
29 #include <bitset>
30 #include <sstream>
32 #include <microsim/MSNet.h>
33 #include "MSTLLogicControl.h"
34 #include "MSTrafficLightLogic.h"
36 
37 
38 // ===========================================================================
39 // member method definitions
40 // ===========================================================================
42  const std::string& id, const std::string& programID, const TrafficLightType logicType, const Phases& phases,
43  int step, SUMOTime delay,
44  const std::map<std::string, std::string>& parameters) :
45  MSTrafficLightLogic(tlcontrol, id, programID, logicType, delay, parameters),
46  myPhases(phases),
47  myStep(step) {
48  for (int i = 0; i < (int)myPhases.size(); i++) {
49  myDefaultCycleTime += myPhases[i]->duration;
50  }
51 }
52 
53 
55  deletePhases();
56 }
57 
58 
59 // ------------ Switching and setting current rows
62  // check whether the current duration shall be increased
66  return delay;
67  }
68 
69  // increment the index
70  if (myPhases[myStep]->nextPhases.size() > 0 && myPhases[myStep]->nextPhases.front() >= 0) {
71  myStep = myPhases[myStep]->nextPhases.front();
72  } else {
73  myStep++;
74  }
75  // if the last phase was reached ...
76  if (myStep >= (int)myPhases.size()) {
77  // ... set the index to the first phase
78  myStep = 0;
79  }
80  assert((int)myPhases.size() > myStep);
81  //stores the time the phase started
83  // check whether the next duration was overridden
84  if (myOverridingTimes.size() > 0) {
85  SUMOTime nextDuration = myOverridingTimes[0];
86  myOverridingTimes.erase(myOverridingTimes.begin());
87  return nextDuration;
88  }
89  // return offset to the next switch
90  return myPhases[myStep]->duration;
91 }
92 
93 
94 // ------------ Static Information Retrieval
95 int
97  return (int) myPhases.size();
98 }
99 
100 
103  return myPhases;
104 }
105 
106 
109  return myPhases;
110 }
111 
112 
113 const MSPhaseDefinition&
115  assert((int)myPhases.size() > givenStep);
116  return *myPhases[givenStep];
117 }
118 
119 
120 // ------------ Dynamic Information Retrieval
121 int
123  return myStep;
124 }
125 
126 
127 const MSPhaseDefinition&
129  return *myPhases[myStep];
130 }
131 
132 
133 // ------------ Conversion between time and phase
134 SUMOTime
136  SUMOTime position = 0;
137  if (myStep > 0) {
138  for (int i = 0; i < myStep; i++) {
139  position = position + getPhase(i).duration;
140  }
141  }
142  position = position + simStep - getPhase(myStep).myLastSwitch;
143  position = position % myDefaultCycleTime;
144  assert(position <= myDefaultCycleTime);
145  return position;
146 }
147 
148 
149 SUMOTime
151  assert(index < (int)myPhases.size());
152  if (index == 0) {
153  return 0;
154  }
155  SUMOTime pos = 0;
156  for (int i = 0; i < index; i++) {
157  pos += getPhase(i).duration;
158  }
159  return pos;
160 }
161 
162 
163 int
165  offset = offset % myDefaultCycleTime;
166  if (offset == myDefaultCycleTime) {
167  return 0;
168  }
169  SUMOTime testPos = 0;
170  for (int i = 0; i < (int)myPhases.size(); i++) {
171  testPos = testPos + getPhase(i).duration;
172  if (testPos > offset) {
173  return i;
174  }
175  if (testPos == offset) {
176  assert((int)myPhases.size() > (i + 1));
177  return (i + 1);
178  }
179  }
180  return 0;
181 }
182 
183 
184 // ------------ Changing phases and phase durations
185 void
187  SUMOTime simStep, int step, SUMOTime stepDuration) {
189  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
190  if (step != myStep) {
191  myStep = step;
193  setTrafficLightSignals(simStep);
194  tlcontrol.get(getID()).executeOnSwitchActions();
195  }
197  mySwitchCommand, stepDuration + simStep);
198 }
199 
200 
201 void
203  assert(step < (int)phases.size());
204  deletePhases();
205  myPhases = phases;
206  myStep = step;
207 }
208 
209 
210 void
212  for (int i = 0; i < (int)myPhases.size(); i++) {
213  delete myPhases[i];
214  }
215 }
216 
217 
218 /****************************************************************************/
long long int SUMOTime
Definition: SUMOTime.h:31
TrafficLightType
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:313
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:464
The definition of a single phase of a tls logic.
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
SUMOTime duration
The duration of the phase.
Phases myPhases
The list of phases this logic uses.
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
int getPhaseNumber() const
Returns the number of phases.
int getCurrentPhaseIndex() const
Returns the current index within the program.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
const Phases & getPhases() const
Returns the phases of this tls program.
MSSimpleTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const TrafficLightType logicType, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
void deletePhases()
frees memory responsibilities
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
virtual SUMOTime trySwitch()
Switches to the next phase.
A class that stores and controls tls and switching of their programs.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
Class realising the switch between the traffic light phases.
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
The parent class for traffic light logics.
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
SwitchCommand * mySwitchCommand
The current switch command.
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
const std::string & getID() const
Returns the id.
Definition: Named.h:73