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 
MSPhaseDefinition::myLastSwitch
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
Definition: MSPhaseDefinition.h:83
MSEventControl::addEvent
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Definition: MSEventControl.cpp:53
MSPhasedTrafficLightLogic::myPhases
Phases myPhases
The list of phases this logic uses.
Definition: MSPhasedTrafficLightLogic.h:176
MSPhasedTrafficLightLogic::getPhase
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
Definition: MSPhasedTrafficLightLogic.cpp:118
MSTrafficLightLogic::SwitchCommand
Class realising the switch between the traffic light phases.
Definition: MSTrafficLightLogic.h:349
MSPhasedTrafficLightLogic::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSPhasedTrafficLightLogic.cpp:132
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
MSPhasedTrafficLightLogic::getPhaseNumber
int getPhaseNumber() const
Returns the number of phases.
Definition: MSPhasedTrafficLightLogic.cpp:107
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
MSPhasedTrafficLightLogic::setPhases
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
Definition: MSPhasedTrafficLightLogic.cpp:196
MSPhasedTrafficLightLogic.h
MSTrafficLightLogic::mySwitchCommand
SwitchCommand * mySwitchCommand
The current switch command.
Definition: MSTrafficLightLogic.h:427
MSPhasedTrafficLightLogic::setStep
void setStep(int step)
Forces a specific step.
Definition: MSPhasedTrafficLightLogic.cpp:97
MSPhasedTrafficLightLogic::MSPhasedTrafficLightLogic
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.
Definition: MSPhasedTrafficLightLogic.cpp:38
MSTrafficLightLogic.h
MSPhasedTrafficLightLogic::proceedToNextStep
void proceedToNextStep()
Proceed to the next step.
Definition: MSPhasedTrafficLightLogic.cpp:92
MSPhaseDefinition::duration
SUMOTime duration
The duration of the phase.
Definition: MSPhaseDefinition.h:71
MSPhasedTrafficLightLogic::getPhaseIndexAtTime
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
Definition: MSPhasedTrafficLightLogic.cpp:139
MSPhasedTrafficLightLogic::deletePhases
void deletePhases()
frees memory responsibilities
Definition: MSPhasedTrafficLightLogic.cpp:205
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
MSPhasedTrafficLightLogic::getPhases
const Phases & getPhases() const
Returns the phases of this tls program.
Definition: MSPhasedTrafficLightLogic.cpp:113
MSPhasedTrafficLightLogic::~MSPhasedTrafficLightLogic
~MSPhasedTrafficLightLogic()
Destructor.
Definition: MSPhasedTrafficLightLogic.cpp:50
MSTrafficLightLogic
The parent class for traffic light logics.
Definition: MSTrafficLightLogic.h:56
MSPhasedTrafficLightLogic::getCurrentPhaseIndex
int getCurrentPhaseIndex() const
Returns the current index within the program.
Definition: MSPhasedTrafficLightLogic.cpp:126
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
MSPhasedTrafficLightLogic::getIndexFromOffset
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
Definition: MSPhasedTrafficLightLogic.cpp:160
MSPhasedTrafficLightLogic::changeStepAndDuration
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Definition: MSPhasedTrafficLightLogic.cpp:183
MSPhasedTrafficLightLogic::getOffsetFromIndex
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
Definition: MSPhasedTrafficLightLogic.cpp:149
MSPhasedTrafficLightLogic::myStep
int myStep
The current step.
Definition: MSPhasedTrafficLightLogic.h:192
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
MSEventControl.h
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:52