SUMO - Simulation of Urban MObility
MSPhasedTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2010-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 /****************************************************************************/
17 // The base class for traffic light logic with phases
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <cassert>
31 #include <utility>
32 #include <vector>
33 #include <bitset>
34 #include <sstream>
36 #include "MSTrafficLightLogic.h"
38 
39 
40 
41 // ===========================================================================
42 // member method definitions
43 // ===========================================================================
45  const std::string& id, const std::string& subid, const Phases& phases,
46  int step, SUMOTime delay,
47  const std::map<std::string, std::string>& parameters
48  )
49  : MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters), myPhases(phases),
50  myStep(step) {
51  for (int i = 0; i < (int)myPhases.size(); i++) {
52  myDefaultCycleTime += myPhases[i]->duration;
53  }
54 }
55 
56 
58  // MSPhasedTrafficLightLogic:deletePhases();
59  /*for (int i=0; i<myPhases.size(); i++) {
60  delete myPhases[i];
61  }*/
62 }
63 
64 
65 // ------------ Switching and setting current rows
67 /*SUMOTime
68 MSPhasedTrafficLightLogic::trySwitch(bool) {
69  // check whether the current duration shall be increased
70  if (myCurrentDurationIncrement>0) {
71  SUMOTime delay = myCurrentDurationIncrement;
72  myCurrentDurationIncrement = 0;
73  return delay;
74  }
75 
76  // increment the index
77  myStep++;
78  // if the last phase was reached ...
79  if (myStep==(int)myPhases.size()) {
80  // ... set the index to the first phase
81  myStep = 0;
82  }
83  assert((int)myPhases.size()>myStep);
84  //stores the time the phase started
85  myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
86  // check whether the next duration was overridden
87  if (myOverridingTimes.size()>0) {
88  SUMOTime nextDuration = myOverridingTimes[0];
89  myOverridingTimes.erase(myOverridingTimes.begin());
90  return nextDuration;
91  }
92  // return offset to the next switch
93  return myPhases[myStep]->duration;
94 }
95 */
96 
97 
98 
100  setStep(myStep + 1);
101 
102 }
103 
105  step = step % myPhases.size();
106  if (myStep != step) {
107  myStep = step;
109  }
110 }
111 
112 // ------------ Static Information Retrieval
113 int
115  return (int)myPhases.size();
116 }
117 
118 
121  return myPhases;
122 }
123 
124 const MSPhaseDefinition&
126  assert((int)myPhases.size() > givenStep);
127  return *myPhases[givenStep];
128 }
129 
130 
131 // ------------ Dynamic Information Retrieval
132 int
134  return myStep;
135 }
136 
137 
138 const MSPhaseDefinition&
140  return *myPhases[myStep];
141 }
142 
143 
144 // ------------ Conversion between time and phase
145 SUMOTime
147  SUMOTime position = getOffsetFromIndex(myStep);
148  position += simStep - getPhase(myStep).myLastSwitch;
149  position = position % myDefaultCycleTime;
150  assert(position <= myDefaultCycleTime);
151  return position;
152 }
153 
154 
155 SUMOTime
157  assert(index < (int)myPhases.size());
158  SUMOTime pos = 0;
159  for (int i = 0; i < index; i++) {
160  pos += getPhase(i).duration;
161  }
162  return pos;
163 }
164 
165 
166 int
168  assert(offset <= myDefaultCycleTime);
169  if (offset == myDefaultCycleTime) {
170  return 0;
171  }
172  SUMOTime pos = offset;
173  SUMOTime testPos = 0;
174  for (int i = 0; i < (int)myPhases.size(); i++) {
175  testPos += getPhase(i).duration;
176  if (testPos > pos) {
177  return i;
178  }
179  if (testPos == pos) {
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  //delete mySwitchCommand;Consider this operation!!!
194  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
195  myStep = step;
197  mySwitchCommand, stepDuration + simStep);
198 }
199 
200 
201 /****************************************************************************/
202 void
204  assert(step < (int)phases.size());
205  deletePhases();
206  myPhases = phases;
207  myStep = step;
208 }
209 
210 
211 void
213  for (int i = 0; i < (int)myPhases.size(); i++) {
214  delete myPhases[i];
215  }
216 }
217 
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
MSPhasedTrafficLightLogic(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.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
int getPhaseNumber() const
Returns the number of phases.
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
void proceedToNextStep()
Proceed to the next step.
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.
Phases myPhases
The list of phases this logic uses.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
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.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
int getCurrentPhaseIndex() const
Returns the current index within the program.
const Phases & getPhases() const
Returns the phases of this tls program.
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
long long int SUMOTime
Definition: TraCIDefs.h:51
The definition of a single phase of a tls logic.
void setStep(int step)
Forces a specific step.
void deletePhases()
frees memory responsibilities