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