SUMO - Simulation of Urban MObility
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.dlr.de/
14 // Copyright (C) 2001-2016 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 <microsim/MSNet.h>
42 #include "MSTLLogicControl.h"
43 #include "MSTrafficLightLogic.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // member method definitions
53 // ===========================================================================
55  const std::string& id, const std::string& subid, const Phases& phases,
56  unsigned int step, SUMOTime delay,
57  const std::map<std::string, std::string>& parameters) :
58  MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters),
59  myPhases(phases),
60  myStep(step) {
61  for (size_t i = 0; i < myPhases.size(); i++) {
62  myDefaultCycleTime += myPhases[i]->duration;
63  }
64 }
65 
66 
68  deletePhases();
69 }
70 
71 
72 // ------------ Switching and setting current rows
75  // check whether the current duration shall be increased
79  return delay;
80  }
81 
82  // increment the index
83  myStep++;
84  // if the last phase was reached ...
85  if (myStep >= myPhases.size()) {
86  // ... set the index to the first phase
87  myStep = 0;
88  }
89  assert(myPhases.size() > myStep);
90  //stores the time the phase started
92  // check whether the next duration was overridden
93  if (myOverridingTimes.size() > 0) {
94  SUMOTime nextDuration = myOverridingTimes[0];
95  myOverridingTimes.erase(myOverridingTimes.begin());
96  return nextDuration;
97  }
98  // return offset to the next switch
99  return myPhases[myStep]->duration;
100 }
101 
102 
103 // ------------ Static Information Retrieval
104 int
106  return (int) myPhases.size();
107 }
108 
109 
112  return myPhases;
113 }
114 
115 
118  return myPhases;
119 }
120 
121 
122 const MSPhaseDefinition&
124  assert((int)myPhases.size() > givenStep);
125  return *myPhases[givenStep];
126 }
127 
128 
129 // ------------ Dynamic Information Retrieval
130 int
132  return myStep;
133 }
134 
135 
136 const MSPhaseDefinition&
138  return *myPhases[myStep];
139 }
140 
141 
142 // ------------ Conversion between time and phase
143 SUMOTime
145  SUMOTime position = 0;
146  if (myStep > 0) {
147  for (unsigned int i = 0; i < myStep; i++) {
148  position = position + getPhase(i).duration;
149  }
150  }
151  position = position + simStep - getPhase(myStep).myLastSwitch;
152  position = position % myDefaultCycleTime;
153  assert(position <= myDefaultCycleTime);
154  return position;
155 }
156 
157 
158 SUMOTime
160  assert(index < myPhases.size());
161  if (index == 0) {
162  return 0;
163  }
164  SUMOTime pos = 0;
165  for (unsigned int i = 0; i < index; i++) {
166  pos += getPhase(i).duration;
167  }
168  return pos;
169 }
170 
171 
172 unsigned int
174  offset = offset % myDefaultCycleTime;
175  if (offset == myDefaultCycleTime) {
176  return 0;
177  }
178  SUMOTime testPos = 0;
179  for (unsigned int i = 0; i < myPhases.size(); i++) {
180  testPos = testPos + getPhase(i).duration;
181  if (testPos > offset) {
182  return i;
183  }
184  if (testPos == offset) {
185  assert(myPhases.size() > (i + 1));
186  return (i + 1);
187  }
188  }
189  return 0;
190 }
191 
192 
193 // ------------ Changing phases and phase durations
194 void
196  SUMOTime simStep, unsigned int step, SUMOTime stepDuration) {
198  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
199  if (step != myStep) {
200  myStep = step;
201  setTrafficLightSignals(simStep);
202  tlcontrol.get(getID()).executeOnSwitchActions();
203  }
205  mySwitchCommand, stepDuration + simStep,
207 }
208 
209 
210 void
212  assert(step < (int)phases.size());
213  deletePhases();
214  myPhases = phases;
215  myStep = step;
216 }
217 
218 
219 void
221  for (size_t i = 0; i < myPhases.size(); i++) {
222  delete myPhases[i];
223  }
224 }
225 
226 
227 /****************************************************************************/
228 
int getCurrentPhaseIndex() const
Returns the current index within the program.
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 getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
long long int SUMOTime
Definition: SUMOTime.h:43
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.
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
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:160
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
A class that stores and controls tls and switching of their programs.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
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.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:389
virtual SUMOTime trySwitch()
Switches to the next phase.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
void deletePhases()
frees memory responsibilities
int getPhaseNumber() const
Returns the number of phases.
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.
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. ...
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.