SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSSimpleTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // A fixed traffic light logic
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <utility>
37 #include <vector>
38 #include <bitset>
39 #include <sstream>
41 #include "MSTLLogicControl.h"
42 #include "MSTrafficLightLogic.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // member method definitions
52 // ===========================================================================
54  const std::string& id, const std::string& subid, const Phases& phases,
55  unsigned int step, SUMOTime delay,
56  const std::map<std::string, std::string>& parameters) :
57  MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters),
58  myPhases(phases),
59  myStep(step) {
60  for (size_t i = 0; i < myPhases.size(); i++) {
61  myDefaultCycleTime += myPhases[i]->duration;
62  }
63 }
64 
65 
67  deletePhases();
68 }
69 
70 
71 // ------------ Switching and setting current rows
74  // check whether the current duration shall be increased
78  return delay;
79  }
80 
81  // increment the index
82  myStep++;
83  // if the last phase was reached ...
84  if (myStep >= myPhases.size()) {
85  // ... set the index to the first phase
86  myStep = 0;
87  }
88  assert(myPhases.size() > myStep);
89  //stores the time the phase started
91  // check whether the next duration was overridden
92  if (myOverridingTimes.size() > 0) {
93  SUMOTime nextDuration = myOverridingTimes[0];
94  myOverridingTimes.erase(myOverridingTimes.begin());
95  return nextDuration;
96  }
97  // return offset to the next switch
98  return myPhases[myStep]->duration;
99 }
100 
101 
102 // ------------ Static Information Retrieval
103 unsigned int
105  return (unsigned int) myPhases.size();
106 }
107 
108 
111  return myPhases;
112 }
113 
114 
117  return myPhases;
118 }
119 
120 
121 const MSPhaseDefinition&
122 MSSimpleTrafficLightLogic::getPhase(unsigned int givenStep) const {
123  assert(myPhases.size() > givenStep);
124  return *myPhases[givenStep];
125 }
126 
127 
128 // ------------ Dynamic Information Retrieval
129 unsigned int
131  return myStep;
132 }
133 
134 
135 const MSPhaseDefinition&
137  return *myPhases[myStep];
138 }
139 
140 
141 // ------------ Conversion between time and phase
142 SUMOTime
144  SUMOTime position = 0;
145  if (myStep > 0) {
146  for (unsigned int i = 0; i < myStep; i++) {
147  position = position + getPhase(i).duration;
148  }
149  }
150  position = position + simStep - getPhase(myStep).myLastSwitch;
151  position = position % myDefaultCycleTime;
152  assert(position <= myDefaultCycleTime);
153  return position;
154 }
155 
156 
157 SUMOTime
159  assert(index < myPhases.size());
160  if (index == 0) {
161  return 0;
162  }
163  unsigned int pos = 0;
164  for (unsigned int i = 0; i < index; i++) {
165  pos += getPhase(i).duration;
166  }
167  return pos;
168 }
169 
170 
171 unsigned int
173  offset = offset % myDefaultCycleTime;
174  if (offset == myDefaultCycleTime) {
175  return 0;
176  }
177  SUMOTime testPos = 0;
178  for (unsigned int i = 0; i < myPhases.size(); i++) {
179  testPos = testPos + getPhase(i).duration;
180  if (testPos > offset) {
181  return i;
182  }
183  if (testPos == offset) {
184  assert(myPhases.size() > (i + 1));
185  return (i + 1);
186  }
187  }
188  return 0;
189 }
190 
191 
192 // ------------ Changing phases and phase durations
193 void
195  SUMOTime simStep, unsigned int step, SUMOTime stepDuration) {
197  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
198  if (step != myStep) {
199  myStep = step;
200  setTrafficLightSignals(simStep);
201  tlcontrol.get(getID()).executeOnSwitchActions();
202  }
204  mySwitchCommand, stepDuration + simStep,
206 }
207 
208 
209 void
210 MSSimpleTrafficLightLogic::setPhases(const Phases& phases, unsigned int step) {
211  assert(step < phases.size());
212  deletePhases();
213  myPhases = phases;
214  myStep = step;
215 }
216 
217 
218 void
220  for (size_t i = 0; i < myPhases.size(); i++) {
221  delete myPhases[i];
222  }
223 }
224 
225 
226 /****************************************************************************/
227 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, unsigned int step, SUMOTime stepDuration)
Changes the current phase and her duration.
unsigned int getPhaseNumber() const
Returns the number of phases.
unsigned int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
unsigned int myStep
The current step.
const Phases & getPhases() const
Returns the phases of this tls program.
SUMOTime trySwitch(bool isActive)
Switches to the next 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.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
SUMOTime getCurrentTimeStep() const
Returns the current simulation step (in s)
Definition: MSNet.cpp:502
A class that stores and controls tls and switching of their programs.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
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.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
const MSPhaseDefinition & getPhase(unsigned int givenstep) const
Returns the definition of the phase from the given position within the plan.
void deletePhases()
frees memory responsibilities
MSSimpleTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &subid, const Phases &phases, unsigned int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
virtual SUMOTime addEvent(Command *operation, SUMOTime execTimeStep, AdaptType type)
Adds an Event.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
unsigned int getCurrentPhaseIndex() const
Returns the current index within the program.
int SUMOTime
Definition: SUMOTime.h:43
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
Patch the time in a way that it is at least as high as the simulation begin time. ...
void setPhases(const Phases &phases, unsigned int index)
Replaces the phases and set the phase index.
MSEventControl & getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:324
SUMOTime getOffsetFromIndex(unsigned int index) const
Returns the position (start of a phase during a cycle) from of a given step.
The definition of a single phase of a tls logic.