Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // The base class for traffic light logic with phases
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <cassert>
23 #include <utility>
24 #include <vector>
25 #include <bitset>
26 #include <sstream>
28 #include "MSTrafficLightLogic.h"
30 
31 
32 
33 // ===========================================================================
34 // member method definitions
35 // ===========================================================================
37  const std::string& id, const std::string& programID, const TrafficLightType logicType, const Phases& phases,
38  int step, SUMOTime delay,
39  const std::map<std::string, std::string>& parameters)
40  : MSTrafficLightLogic(tlcontrol, id, programID, logicType, delay, parameters), myPhases(phases),
41  myStep(step) {
42  for (int i = 0; i < (int)myPhases.size(); i++) {
43  myDefaultCycleTime += myPhases[i]->duration;
44  }
45 }
46 
47 
49  // MSPhasedTrafficLightLogic:deletePhases();
50  /*for (int i=0; i<myPhases.size(); i++) {
51  delete myPhases[i];
52  }*/
53 }
54 
55 
56 // ------------ Switching and setting current rows
58 /*SUMOTime
59 MSPhasedTrafficLightLogic::trySwitch(bool) {
60  // check whether the current duration shall be increased
61  if (myCurrentDurationIncrement>0) {
62  SUMOTime delay = myCurrentDurationIncrement;
63  myCurrentDurationIncrement = 0;
64  return delay;
65  }
66 
67  // increment the index
68  myStep++;
69  // if the last phase was reached ...
70  if (myStep==(int)myPhases.size()) {
71  // ... set the index to the first phase
72  myStep = 0;
73  }
74  assert((int)myPhases.size()>myStep);
75  //stores the time the phase started
76  myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
77  // check whether the next duration was overridden
78  if (myOverridingTimes.size()>0) {
79  SUMOTime nextDuration = myOverridingTimes[0];
80  myOverridingTimes.erase(myOverridingTimes.begin());
81  return nextDuration;
82  }
83  // return offset to the next switch
84  return myPhases[myStep]->duration;
85 }
86 */
87 
88 
89 
91  setStep(myStep + 1);
92 
93 }
94 
96  step = step % myPhases.size();
97  if (myStep != step) {
98  myStep = step;
100  }
101 }
102 
103 // ------------ Static Information Retrieval
104 int
106  return (int)myPhases.size();
107 }
108 
109 
112  return myPhases;
113 }
114 
115 const MSPhaseDefinition&
117  assert((int)myPhases.size() > givenStep);
118  return *myPhases[givenStep];
119 }
120 
121 
122 // ------------ Dynamic Information Retrieval
123 int
125  return myStep;
126 }
127 
128 
129 const MSPhaseDefinition&
131  return *myPhases[myStep];
132 }
133 
134 
135 // ------------ Conversion between time and phase
136 SUMOTime
138  SUMOTime position = getOffsetFromIndex(myStep);
139  position += simStep - getPhase(myStep).myLastSwitch;
140  position = position % myDefaultCycleTime;
141  assert(position <= myDefaultCycleTime);
142  return position;
143 }
144 
145 
146 SUMOTime
148  assert(index < (int)myPhases.size());
149  SUMOTime pos = 0;
150  for (int i = 0; i < index; i++) {
151  pos += getPhase(i).duration;
152  }
153  return pos;
154 }
155 
156 
157 int
159  assert(offset <= myDefaultCycleTime);
160  if (offset == myDefaultCycleTime) {
161  return 0;
162  }
163  SUMOTime pos = offset;
164  SUMOTime testPos = 0;
165  for (int i = 0; i < (int)myPhases.size(); i++) {
166  testPos += getPhase(i).duration;
167  if (testPos > pos) {
168  return i;
169  }
170  if (testPos == pos) {
171  assert((int)myPhases.size() > (i + 1));
172  return i + 1;
173  }
174  }
175  return 0;
176 }
177 
178 
179 // ------------ Changing phases and phase durations
180 void
182  SUMOTime simStep, int step, SUMOTime stepDuration) {
184  //delete mySwitchCommand;Consider this operation!!!
185  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
186  myStep = step;
188  mySwitchCommand, stepDuration + simStep);
189 }
190 
191 
192 /****************************************************************************/
193 void
195  assert(step < (int)phases.size());
196  deletePhases();
197  myPhases = phases;
198  myStep = step;
199 }
200 
201 
202 void
204  for (int i = 0; i < (int)myPhases.size(); i++) {
205  delete myPhases[i];
206  }
207 }
208 
long long int SUMOTime
Definition: SUMOTime.h:31
TrafficLightType
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:313
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:464
The definition of a single phase of a tls logic.
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
SUMOTime duration
The duration of the phase.
Phases myPhases
The list of phases this logic uses.
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
void proceedToNextStep()
Proceed to the next step.
void deletePhases()
frees memory responsibilities
MSPhasedTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const TrafficLightType logicType, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
const Phases & getPhases() const
Returns the phases of this tls program.
int getCurrentPhaseIndex() const
Returns the current index within the program.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
int getPhaseNumber() const
Returns the number of phases.
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
void setStep(int step)
Forces a specific step.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
A class that stores and controls tls and switching of their programs.
Class realising the switch between the traffic light phases.
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
The parent class for traffic light logics.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
SwitchCommand * mySwitchCommand
The current switch command.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.