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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // A fixed traffic light logic
22 /****************************************************************************/
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 <cassert>
35 #include <utility>
36 #include <vector>
37 #include <bitset>
38 #include <sstream>
40 #include <microsim/MSNet.h>
41 #include "MSTLLogicControl.h"
42 #include "MSTrafficLightLogic.h"
44 
45 
46 // ===========================================================================
47 // member method definitions
48 // ===========================================================================
50  const std::string& id, const std::string& subid, const Phases& phases,
51  int step, SUMOTime delay,
52  const std::map<std::string, std::string>& parameters) :
53  MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters),
54  myPhases(phases),
55  myStep(step) {
56  for (int i = 0; i < (int)myPhases.size(); i++) {
57  myDefaultCycleTime += myPhases[i]->duration;
58  }
59 }
60 
61 
63  deletePhases();
64 }
65 
66 
67 // ------------ Switching and setting current rows
70  // check whether the current duration shall be increased
74  return delay;
75  }
76 
77  // increment the index
78  myStep++;
79  // if the last phase was reached ...
80  if (myStep >= (int)myPhases.size()) {
81  // ... set the index to the first phase
82  myStep = 0;
83  }
84  assert((int)myPhases.size() > myStep);
85  //stores the time the phase started
87  // check whether the next duration was overridden
88  if (myOverridingTimes.size() > 0) {
89  SUMOTime nextDuration = myOverridingTimes[0];
90  myOverridingTimes.erase(myOverridingTimes.begin());
91  return nextDuration;
92  }
93  // return offset to the next switch
94  return myPhases[myStep]->duration;
95 }
96 
97 
98 // ------------ Static Information Retrieval
99 int
101  return (int) myPhases.size();
102 }
103 
104 
107  return myPhases;
108 }
109 
110 
113  return myPhases;
114 }
115 
116 
117 const MSPhaseDefinition&
119  assert((int)myPhases.size() > givenStep);
120  return *myPhases[givenStep];
121 }
122 
123 
124 // ------------ Dynamic Information Retrieval
125 int
127  return myStep;
128 }
129 
130 
131 const MSPhaseDefinition&
133  return *myPhases[myStep];
134 }
135 
136 
137 // ------------ Conversion between time and phase
138 SUMOTime
140  SUMOTime position = 0;
141  if (myStep > 0) {
142  for (int i = 0; i < myStep; i++) {
143  position = position + getPhase(i).duration;
144  }
145  }
146  position = position + simStep - getPhase(myStep).myLastSwitch;
147  position = position % myDefaultCycleTime;
148  assert(position <= myDefaultCycleTime);
149  return position;
150 }
151 
152 
153 SUMOTime
155  assert(index < (int)myPhases.size());
156  if (index == 0) {
157  return 0;
158  }
159  SUMOTime pos = 0;
160  for (int i = 0; i < index; i++) {
161  pos += getPhase(i).duration;
162  }
163  return pos;
164 }
165 
166 
167 int
169  offset = offset % myDefaultCycleTime;
170  if (offset == myDefaultCycleTime) {
171  return 0;
172  }
173  SUMOTime testPos = 0;
174  for (int i = 0; i < (int)myPhases.size(); i++) {
175  testPos = testPos + getPhase(i).duration;
176  if (testPos > offset) {
177  return i;
178  }
179  if (testPos == offset) {
180  assert((int)myPhases.size() > (i + 1));
181  return (i + 1);
182  }
183  }
184  return 0;
185 }
186 
187 
188 // ------------ Changing phases and phase durations
189 void
191  SUMOTime simStep, int step, SUMOTime stepDuration) {
193  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
194  if (step != myStep) {
195  myStep = step;
196  setTrafficLightSignals(simStep);
197  tlcontrol.get(getID()).executeOnSwitchActions();
198  }
200  mySwitchCommand, stepDuration + simStep);
201 }
202 
203 
204 void
206  assert(step < (int)phases.size());
207  deletePhases();
208  myPhases = phases;
209  myStep = step;
210 }
211 
212 
213 void
215  for (int i = 0; i < (int)myPhases.size(); i++) {
216  delete myPhases[i];
217  }
218 }
219 
220 
221 /****************************************************************************/
222 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
MSSimpleTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &subid, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Phases myPhases
The list of phases this logic uses.
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
A class that stores and controls tls and switching of their programs.
SUMOTime duration
The duration of the phase.
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
Class realising the switch between the traffic light phases.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
int getCurrentPhaseIndex() const
Returns the current index within the program.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:399
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
virtual SUMOTime trySwitch()
Switches to the next phase.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
void deletePhases()
frees memory responsibilities
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
int getPhaseNumber() const
Returns the number of phases.
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
long long int SUMOTime
Definition: TraCIDefs.h:51
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
const Phases & getPhases() const
Returns the phases of this tls program.
The definition of a single phase of a tls logic.
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.