SUMO - Simulation of Urban MObility
MSPhasedTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // The base class for traffic light logic with phases
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright 2001-2009 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <cassert>
32 #include <utility>
33 #include <vector>
34 #include <bitset>
35 #include <sstream>
37 #include "MSTrafficLightLogic.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
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  )
54  : MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters), myPhases(phases),
55  myStep(step) {
56  for (int i = 0; i < myPhases.size(); i++) {
57  myDefaultCycleTime += myPhases[i]->duration;
58  }
59 }
60 
61 
63  // MSPhasedTrafficLightLogic:deletePhases();
64  /*for (int i=0; i<myPhases.size(); i++) {
65  delete myPhases[i];
66  }*/
67 }
68 
69 
70 // ------------ Switching and setting current rows
72 /*SUMOTime
73 MSPhasedTrafficLightLogic::trySwitch(bool) {
74  // check whether the current duration shall be increased
75  if (myCurrentDurationIncrement>0) {
76  SUMOTime delay = myCurrentDurationIncrement;
77  myCurrentDurationIncrement = 0;
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
90  myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
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 
103 
105  setStep(myStep + 1);
106 
107 }
108 
110  step = step % myPhases.size();
111  if (myStep != step) {
112  myStep = step;
114  }
115 }
116 
117 // ------------ Static Information Retrieval
118 int
120  return (int)myPhases.size();
121 }
122 
123 
126  return myPhases;
127 }
128 
129 const MSPhaseDefinition&
131  assert((int)myPhases.size() > givenStep);
132  return *myPhases[givenStep];
133 }
134 
135 
136 // ------------ Dynamic Information Retrieval
137 int
139  return myStep;
140 }
141 
142 
143 const MSPhaseDefinition&
145  return *myPhases[myStep];
146 }
147 
148 
149 // ------------ Conversion between time and phase
150 SUMOTime
152  SUMOTime position = getOffsetFromIndex(myStep);
153  position += simStep - getPhase(myStep).myLastSwitch;
154  position = position % myDefaultCycleTime;
155  assert(position <= myDefaultCycleTime);
156  return position;
157 }
158 
159 
160 SUMOTime
162  assert(index < myPhases.size());
163  SUMOTime pos = 0;
164  for (int i = 0; i < index; i++) {
165  pos += getPhase(i).duration;
166  }
167  return pos;
168 }
169 
170 
171 int
173  assert(offset <= myDefaultCycleTime);
174  if (offset == myDefaultCycleTime) {
175  return 0;
176  }
177  SUMOTime pos = offset;
178  SUMOTime testPos = 0;
179  for (int i = 0; i < myPhases.size(); i++) {
180  testPos += getPhase(i).duration;
181  if (testPos > pos) {
182  return i;
183  }
184  if (testPos == pos) {
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, int step, SUMOTime stepDuration) {
198  //delete mySwitchCommand;Consider this operation!!!
199  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
200  myStep = step;
202  mySwitchCommand, stepDuration + simStep,
204 }
205 
206 
207 /****************************************************************************/
208 void
210  assert(step < (int)phases.size());
211  deletePhases();
212  myPhases = phases;
213  myStep = step;
214 }
215 
216 
217 void
219  for (int i = 0; i < myPhases.size(); i++) {
220  delete myPhases[i];
221  }
222 }
223 
long long int SUMOTime
Definition: SUMOTime.h:43
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
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.
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:159
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
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.
int getCurrentPhaseIndex() const
Returns the current index within the program.
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.
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:400
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
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.
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.
const Phases & getPhases() const
Returns the phases of this tls program.
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. ...
The definition of a single phase of a tls logic.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
void setStep(int step)
Forces a specific step.
void deletePhases()
frees memory responsibilities