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 (C) 2010-2017 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 
41 
42 // ===========================================================================
43 // member method definitions
44 // ===========================================================================
46  const std::string& id, const std::string& subid, const Phases& phases,
47  int step, SUMOTime delay,
48  const std::map<std::string, std::string>& parameters
49  )
50  : MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters), myPhases(phases),
51  myStep(step) {
52  for (int i = 0; i < (int)myPhases.size(); i++) {
53  myDefaultCycleTime += myPhases[i]->duration;
54  }
55 }
56 
57 
59  // MSPhasedTrafficLightLogic:deletePhases();
60  /*for (int i=0; i<myPhases.size(); i++) {
61  delete myPhases[i];
62  }*/
63 }
64 
65 
66 // ------------ Switching and setting current rows
68 /*SUMOTime
69 MSPhasedTrafficLightLogic::trySwitch(bool) {
70  // check whether the current duration shall be increased
71  if (myCurrentDurationIncrement>0) {
72  SUMOTime delay = myCurrentDurationIncrement;
73  myCurrentDurationIncrement = 0;
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
86  myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
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 
99 
101  setStep(myStep + 1);
102 
103 }
104 
106  step = step % myPhases.size();
107  if (myStep != step) {
108  myStep = step;
110  }
111 }
112 
113 // ------------ Static Information Retrieval
114 int
116  return (int)myPhases.size();
117 }
118 
119 
122  return myPhases;
123 }
124 
125 const MSPhaseDefinition&
127  assert((int)myPhases.size() > givenStep);
128  return *myPhases[givenStep];
129 }
130 
131 
132 // ------------ Dynamic Information Retrieval
133 int
135  return myStep;
136 }
137 
138 
139 const MSPhaseDefinition&
141  return *myPhases[myStep];
142 }
143 
144 
145 // ------------ Conversion between time and phase
146 SUMOTime
148  SUMOTime position = getOffsetFromIndex(myStep);
149  position += simStep - getPhase(myStep).myLastSwitch;
150  position = position % myDefaultCycleTime;
151  assert(position <= myDefaultCycleTime);
152  return position;
153 }
154 
155 
156 SUMOTime
158  assert(index < (int)myPhases.size());
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  assert(offset <= myDefaultCycleTime);
170  if (offset == myDefaultCycleTime) {
171  return 0;
172  }
173  SUMOTime pos = offset;
174  SUMOTime testPos = 0;
175  for (int i = 0; i < (int)myPhases.size(); i++) {
176  testPos += getPhase(i).duration;
177  if (testPos > pos) {
178  return i;
179  }
180  if (testPos == pos) {
181  assert((int)myPhases.size() > (i + 1));
182  return i + 1;
183  }
184  }
185  return 0;
186 }
187 
188 
189 // ------------ Changing phases and phase durations
190 void
192  SUMOTime simStep, int step, SUMOTime stepDuration) {
194  //delete mySwitchCommand;Consider this operation!!!
195  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
196  myStep = step;
198  mySwitchCommand, stepDuration + simStep);
199 }
200 
201 
202 /****************************************************************************/
203 void
205  assert(step < (int)phases.size());
206  deletePhases();
207  myPhases = phases;
208  myStep = step;
209 }
210 
211 
212 void
214  for (int i = 0; i < (int)myPhases.size(); i++) {
215  delete myPhases[i];
216  }
217 }
218 
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:158
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:403
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:257
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:52
The definition of a single phase of a tls logic.
void setStep(int step)
Forces a specific step.
void deletePhases()
frees memory responsibilities