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-2018 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& subid, const Phases& phases,
40  int step, SUMOTime delay,
41  const std::map<std::string, std::string>& parameters
42  )
43  : MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters), myPhases(phases),
44  myStep(step) {
45  for (int i = 0; i < (int)myPhases.size(); i++) {
46  myDefaultCycleTime += myPhases[i]->duration;
47  }
48 }
49 
50 
52  // MSPhasedTrafficLightLogic:deletePhases();
53  /*for (int i=0; i<myPhases.size(); i++) {
54  delete myPhases[i];
55  }*/
56 }
57 
58 
59 // ------------ Switching and setting current rows
61 /*SUMOTime
62 MSPhasedTrafficLightLogic::trySwitch(bool) {
63  // check whether the current duration shall be increased
64  if (myCurrentDurationIncrement>0) {
65  SUMOTime delay = myCurrentDurationIncrement;
66  myCurrentDurationIncrement = 0;
67  return delay;
68  }
69 
70  // increment the index
71  myStep++;
72  // if the last phase was reached ...
73  if (myStep==(int)myPhases.size()) {
74  // ... set the index to the first phase
75  myStep = 0;
76  }
77  assert((int)myPhases.size()>myStep);
78  //stores the time the phase started
79  myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
80  // check whether the next duration was overridden
81  if (myOverridingTimes.size()>0) {
82  SUMOTime nextDuration = myOverridingTimes[0];
83  myOverridingTimes.erase(myOverridingTimes.begin());
84  return nextDuration;
85  }
86  // return offset to the next switch
87  return myPhases[myStep]->duration;
88 }
89 */
90 
91 
92 
94  setStep(myStep + 1);
95 
96 }
97 
99  step = step % myPhases.size();
100  if (myStep != step) {
101  myStep = step;
103  }
104 }
105 
106 // ------------ Static Information Retrieval
107 int
109  return (int)myPhases.size();
110 }
111 
112 
115  return myPhases;
116 }
117 
118 const MSPhaseDefinition&
120  assert((int)myPhases.size() > givenStep);
121  return *myPhases[givenStep];
122 }
123 
124 
125 // ------------ Dynamic Information Retrieval
126 int
128  return myStep;
129 }
130 
131 
132 const MSPhaseDefinition&
134  return *myPhases[myStep];
135 }
136 
137 
138 // ------------ Conversion between time and phase
139 SUMOTime
141  SUMOTime position = getOffsetFromIndex(myStep);
142  position += simStep - getPhase(myStep).myLastSwitch;
143  position = position % myDefaultCycleTime;
144  assert(position <= myDefaultCycleTime);
145  return position;
146 }
147 
148 
149 SUMOTime
151  assert(index < (int)myPhases.size());
152  SUMOTime pos = 0;
153  for (int i = 0; i < index; i++) {
154  pos += getPhase(i).duration;
155  }
156  return pos;
157 }
158 
159 
160 int
162  assert(offset <= myDefaultCycleTime);
163  if (offset == myDefaultCycleTime) {
164  return 0;
165  }
166  SUMOTime pos = offset;
167  SUMOTime testPos = 0;
168  for (int i = 0; i < (int)myPhases.size(); i++) {
169  testPos += getPhase(i).duration;
170  if (testPos > pos) {
171  return i;
172  }
173  if (testPos == pos) {
174  assert((int)myPhases.size() > (i + 1));
175  return i + 1;
176  }
177  }
178  return 0;
179 }
180 
181 
182 // ------------ Changing phases and phase durations
183 void
185  SUMOTime simStep, int step, SUMOTime stepDuration) {
187  //delete mySwitchCommand;Consider this operation!!!
188  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
189  myStep = step;
191  mySwitchCommand, stepDuration + simStep);
192 }
193 
194 
195 /****************************************************************************/
196 void
198  assert(step < (int)phases.size());
199  deletePhases();
200  myPhases = phases;
201  myStep = step;
202 }
203 
204 
205 void
207  for (int i = 0; i < (int)myPhases.size(); i++) {
208  delete myPhases[i];
209  }
210 }
211 
long long int SUMOTime
Definition: SUMOTime.h:36
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:160
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:402
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:256
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.
void deletePhases()
frees memory responsibilities