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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // The base class for traffic light logic with phases
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <cassert>
25 #include <utility>
26 #include <vector>
27 #include <bitset>
28 #include <sstream>
30 #include "MSTrafficLightLogic.h"
32 
33 
34 
35 // ===========================================================================
36 // member method definitions
37 // ===========================================================================
39  const std::string& id, const std::string& programID, const TrafficLightType logicType, const Phases& phases,
40  int step, SUMOTime delay,
41  const std::map<std::string, std::string>& parameters)
42  : MSTrafficLightLogic(tlcontrol, id, programID, logicType, delay, parameters), myPhases(phases),
43  myStep(step) {
44  for (int i = 0; i < (int)myPhases.size(); i++) {
45  myDefaultCycleTime += myPhases[i]->duration;
46  }
47 }
48 
49 
51  // MSPhasedTrafficLightLogic:deletePhases();
52  /*for (int i=0; i<myPhases.size(); i++) {
53  delete myPhases[i];
54  }*/
55 }
56 
57 
58 // ------------ Switching and setting current rows
60 /*SUMOTime
61 MSPhasedTrafficLightLogic::trySwitch(bool) {
62  // check whether the current duration shall be increased
63  if (myCurrentDurationIncrement>0) {
64  SUMOTime delay = myCurrentDurationIncrement;
65  myCurrentDurationIncrement = 0;
66  return delay;
67  }
68 
69  // increment the index
70  myStep++;
71  // if the last phase was reached ...
72  if (myStep==(int)myPhases.size()) {
73  // ... set the index to the first phase
74  myStep = 0;
75  }
76  assert((int)myPhases.size()>myStep);
77  //stores the time the phase started
78  myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
79  // check whether the next duration was overridden
80  if (myOverridingTimes.size()>0) {
81  SUMOTime nextDuration = myOverridingTimes[0];
82  myOverridingTimes.erase(myOverridingTimes.begin());
83  return nextDuration;
84  }
85  // return offset to the next switch
86  return myPhases[myStep]->duration;
87 }
88 */
89 
90 
91 
93  setStep(myStep + 1);
94 
95 }
96 
98  step = step % myPhases.size();
99  if (myStep != step) {
100  myStep = step;
102  }
103 }
104 
105 // ------------ Static Information Retrieval
106 int
108  return (int)myPhases.size();
109 }
110 
111 
114  return myPhases;
115 }
116 
117 const MSPhaseDefinition&
119  assert((int)myPhases.size() > givenStep);
120  return *myPhases[givenStep];
121 }
122 
123 
124 // ------------ Dynamic Information Retrieval
125 int
127  return myStep;
128 }
129 
130 
131 const MSPhaseDefinition&
133  return *myPhases[myStep];
134 }
135 
136 
137 // ------------ Conversion between time and phase
138 SUMOTime
140  SUMOTime position = getOffsetFromIndex(myStep);
141  position += simStep - getPhase(myStep).myLastSwitch;
142  position = position % myDefaultCycleTime;
143  assert(position <= myDefaultCycleTime);
144  return position;
145 }
146 
147 
148 SUMOTime
150  assert(index < (int)myPhases.size());
151  SUMOTime pos = 0;
152  for (int i = 0; i < index; i++) {
153  pos += getPhase(i).duration;
154  }
155  return pos;
156 }
157 
158 
159 int
161  assert(offset <= myDefaultCycleTime);
162  if (offset == myDefaultCycleTime) {
163  return 0;
164  }
165  SUMOTime pos = offset;
166  SUMOTime testPos = 0;
167  for (int i = 0; i < (int)myPhases.size(); i++) {
168  testPos += getPhase(i).duration;
169  if (testPos > pos) {
170  return i;
171  }
172  if (testPos == pos) {
173  assert((int)myPhases.size() > (i + 1));
174  return i + 1;
175  }
176  }
177  return 0;
178 }
179 
180 
181 // ------------ Changing phases and phase durations
182 void
184  SUMOTime simStep, int step, SUMOTime stepDuration) {
186  //delete mySwitchCommand;Consider this operation!!!
187  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
188  myStep = step;
190  mySwitchCommand, stepDuration + simStep);
191 }
192 
193 
194 /****************************************************************************/
195 void
197  assert(step < (int)phases.size());
198  deletePhases();
199  myPhases = phases;
200  myStep = step;
201 }
202 
203 
204 void
206  for (int i = 0; i < (int)myPhases.size(); i++) {
207  delete myPhases[i];
208  }
209 }
210 
long long int SUMOTime
Definition: SUMOTime.h:35
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.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
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:430
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
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.
The definition of a single phase of a tls logic.
void setStep(int step)
Forces a specific step.
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 deletePhases()
frees memory responsibilities
TrafficLightType