Eclipse SUMO - Simulation of Urban MObility
MSSimpleTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
18 // A fixed traffic light logic
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <cassert>
28 #include <utility>
29 #include <vector>
30 #include <bitset>
31 #include <sstream>
33 #include <microsim/MSNet.h>
34 #include "MSTLLogicControl.h"
35 #include "MSTrafficLightLogic.h"
37 
38 
39 // ===========================================================================
40 // member method definitions
41 // ===========================================================================
43  const std::string& id, const std::string& programID, const TrafficLightType logicType, const Phases& phases,
44  int step, SUMOTime delay,
45  const std::map<std::string, std::string>& parameters) :
46  MSTrafficLightLogic(tlcontrol, id, programID, logicType, delay, parameters),
47  myPhases(phases),
48  myStep(step) {
49  for (int i = 0; i < (int)myPhases.size(); i++) {
50  myDefaultCycleTime += myPhases[i]->duration;
51  }
52 }
53 
54 
56  deletePhases();
57 }
58 
59 
60 // ------------ Switching and setting current rows
63  // check whether the current duration shall be increased
67  return delay;
68  }
69 
70  // increment the index
71  if (myPhases[myStep]->nextPhases.size() > 0 && myPhases[myStep]->nextPhases.front() >= 0) {
72  myStep = myPhases[myStep]->nextPhases.front();
73  } else {
74  myStep++;
75  }
76  // if the last phase was reached ...
77  if (myStep >= (int)myPhases.size()) {
78  // ... set the index to the first phase
79  myStep = 0;
80  }
81  assert((int)myPhases.size() > myStep);
82  //stores the time the phase started
84  // check whether the next duration was overridden
85  if (myOverridingTimes.size() > 0) {
86  SUMOTime nextDuration = myOverridingTimes[0];
87  myOverridingTimes.erase(myOverridingTimes.begin());
88  return nextDuration;
89  }
90  // return offset to the next switch
91  return myPhases[myStep]->duration;
92 }
93 
94 
95 // ------------ Static Information Retrieval
96 int
98  return (int) myPhases.size();
99 }
100 
101 
104  return myPhases;
105 }
106 
107 
110  return myPhases;
111 }
112 
113 
114 const MSPhaseDefinition&
116  assert((int)myPhases.size() > givenStep);
117  return *myPhases[givenStep];
118 }
119 
120 
121 // ------------ Dynamic Information Retrieval
122 int
124  return myStep;
125 }
126 
127 
128 const MSPhaseDefinition&
130  return *myPhases[myStep];
131 }
132 
133 
134 // ------------ Conversion between time and phase
135 SUMOTime
137  SUMOTime position = 0;
138  if (myStep > 0) {
139  for (int i = 0; i < myStep; i++) {
140  position = position + getPhase(i).duration;
141  }
142  }
143  position = position + simStep - getPhase(myStep).myLastSwitch;
144  position = position % myDefaultCycleTime;
145  assert(position <= myDefaultCycleTime);
146  return position;
147 }
148 
149 
150 SUMOTime
152  assert(index < (int)myPhases.size());
153  if (index == 0) {
154  return 0;
155  }
156  SUMOTime pos = 0;
157  for (int i = 0; i < index; i++) {
158  pos += getPhase(i).duration;
159  }
160  return pos;
161 }
162 
163 
164 int
166  offset = offset % myDefaultCycleTime;
167  if (offset == myDefaultCycleTime) {
168  return 0;
169  }
170  SUMOTime testPos = 0;
171  for (int i = 0; i < (int)myPhases.size(); i++) {
172  testPos = testPos + getPhase(i).duration;
173  if (testPos > offset) {
174  return i;
175  }
176  if (testPos == offset) {
177  assert((int)myPhases.size() > (i + 1));
178  return (i + 1);
179  }
180  }
181  return 0;
182 }
183 
184 
185 // ------------ Changing phases and phase durations
186 void
188  SUMOTime simStep, int step, SUMOTime stepDuration) {
190  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
191  if (step != myStep) {
192  myStep = step;
193  setTrafficLightSignals(simStep);
194  tlcontrol.get(getID()).executeOnSwitchActions();
195  }
197  mySwitchCommand, stepDuration + simStep);
198 }
199 
200 
201 void
203  assert(step < (int)phases.size());
204  deletePhases();
205  myPhases = phases;
206  myStep = step;
207 }
208 
209 
210 void
212  for (int i = 0; i < (int)myPhases.size(); i++) {
213  delete myPhases[i];
214  }
215 }
216 
217 
218 /****************************************************************************/
219 
MSSimpleTrafficLightLogic::setPhases
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
Definition: MSSimpleTrafficLightLogic.cpp:202
MSTrafficLightLogic::setTrafficLightSignals
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
Definition: MSTrafficLightLogic.cpp:235
MSSimpleTrafficLightLogic::getPhase
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
Definition: MSSimpleTrafficLightLogic.cpp:115
MSSimpleTrafficLightLogic::getPhaseNumber
int getPhaseNumber() const
Returns the number of phases.
Definition: MSSimpleTrafficLightLogic.cpp:97
MSPhaseDefinition::myLastSwitch
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
Definition: MSPhaseDefinition.h:82
MSTrafficLightLogic::myOverridingTimes
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
Definition: MSTrafficLightLogic.h:423
MSEventControl::addEvent
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Definition: MSEventControl.cpp:52
MSTLLogicControl.h
MSNet.h
MSSimpleTrafficLightLogic::getCurrentPhaseIndex
int getCurrentPhaseIndex() const
Returns the current index within the program.
Definition: MSSimpleTrafficLightLogic.cpp:123
MSSimpleTrafficLightLogic::MSSimpleTrafficLightLogic
MSSimpleTrafficLightLogic(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.
Definition: MSSimpleTrafficLightLogic.cpp:42
MSTrafficLightLogic::SwitchCommand
Class realising the switch between the traffic light phases.
Definition: MSTrafficLightLogic.h:351
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:61
MSNet::getBeginOfTimestepEvents
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:429
MSTrafficLightLogic::SwitchCommand::deschedule
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
Definition: MSTrafficLightLogic.cpp:89
TrafficLightType
TrafficLightType
Definition: SUMOXMLDefinitions.h:1197
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MSSimpleTrafficLightLogic::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSSimpleTrafficLightLogic.cpp:129
MSSimpleTrafficLightLogic.h
MSTrafficLightLogic::mySwitchCommand
SwitchCommand * mySwitchCommand
The current switch command.
Definition: MSTrafficLightLogic.h:429
MSTrafficLightLogic.h
MSTrafficLightLogic::myCurrentDurationIncrement
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
Definition: MSTrafficLightLogic.h:426
MSPhaseDefinition::duration
SUMOTime duration
The duration of the phase.
Definition: MSPhaseDefinition.h:70
MSSimpleTrafficLightLogic::trySwitch
virtual SUMOTime trySwitch()
Switches to the next phase.
Definition: MSSimpleTrafficLightLogic.cpp:62
MSTLLogicControl::get
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
Definition: MSTLLogicControl.cpp:589
MSSimpleTrafficLightLogic::getPhaseIndexAtTime
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
Definition: MSSimpleTrafficLightLogic.cpp:136
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:283
MSSimpleTrafficLightLogic::getOffsetFromIndex
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
Definition: MSSimpleTrafficLightLogic.cpp:151
MSSimpleTrafficLightLogic::getIndexFromOffset
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
Definition: MSSimpleTrafficLightLogic.cpp:165
MSTrafficLightLogic
The parent class for traffic light logics.
Definition: MSTrafficLightLogic.h:55
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSSimpleTrafficLightLogic::deletePhases
void deletePhases()
frees memory responsibilities
Definition: MSSimpleTrafficLightLogic.cpp:211
MSSimpleTrafficLightLogic::changeStepAndDuration
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Definition: MSSimpleTrafficLightLogic.cpp:187
MSSimpleTrafficLightLogic::~MSSimpleTrafficLightLogic
~MSSimpleTrafficLightLogic()
Destructor.
Definition: MSSimpleTrafficLightLogic.cpp:55
MSSimpleTrafficLightLogic::myPhases
Phases myPhases
The list of phases this logic uses.
Definition: MSSimpleTrafficLightLogic.h:198
MSSimpleTrafficLightLogic::getPhases
const Phases & getPhases() const
Returns the phases of this tls program.
Definition: MSSimpleTrafficLightLogic.cpp:103
config.h
MSTrafficLightLogic::myDefaultCycleTime
SUMOTime myDefaultCycleTime
The cycle time (without changes)
Definition: MSTrafficLightLogic.h:432
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:59
MSTLLogicControl::TLSLogicVariants::executeOnSwitchActions
void executeOnSwitchActions() const
Definition: MSTLLogicControl.cpp:217
MSEventControl.h
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:51
MSSimpleTrafficLightLogic::myStep
int myStep
The current step.
Definition: MSSimpleTrafficLightLogic.h:201
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76