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 /****************************************************************************/
14 // The base class for traffic light logic with phases
15 /****************************************************************************/
16 
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
23 #include <cassert>
24 #include <utility>
25 #include <vector>
26 #include <bitset>
27 #include <sstream>
29 #include "MSTrafficLightLogic.h"
31 
32 
33 
34 // ===========================================================================
35 // member method definitions
36 // ===========================================================================
38  const std::string& id, const std::string& programID, const TrafficLightType logicType, const Phases& phases,
39  int step, SUMOTime delay,
40  const std::map<std::string, std::string>& parameters)
41  : MSTrafficLightLogic(tlcontrol, id, programID, logicType, delay, parameters), myPhases(phases),
42  myStep(step) {
43  for (int i = 0; i < (int)myPhases.size(); i++) {
44  myDefaultCycleTime += myPhases[i]->duration;
45  }
46 }
47 
48 
50  // MSPhasedTrafficLightLogic:deletePhases();
51  /*for (int i=0; i<myPhases.size(); i++) {
52  delete myPhases[i];
53  }*/
54 }
55 
56 
57 // ------------ Switching and setting current rows
59 /*SUMOTime
60 MSPhasedTrafficLightLogic::trySwitch(bool) {
61  // check whether the current duration shall be increased
62  if (myCurrentDurationIncrement>0) {
63  SUMOTime delay = myCurrentDurationIncrement;
64  myCurrentDurationIncrement = 0;
65  return delay;
66  }
67 
68  // increment the index
69  myStep++;
70  // if the last phase was reached ...
71  if (myStep==(int)myPhases.size()) {
72  // ... set the index to the first phase
73  myStep = 0;
74  }
75  assert((int)myPhases.size()>myStep);
76  //stores the time the phase started
77  myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
78  // check whether the next duration was overridden
79  if (myOverridingTimes.size()>0) {
80  SUMOTime nextDuration = myOverridingTimes[0];
81  myOverridingTimes.erase(myOverridingTimes.begin());
82  return nextDuration;
83  }
84  // return offset to the next switch
85  return myPhases[myStep]->duration;
86 }
87 */
88 
89 
90 
92  setStep(myStep + 1);
93 
94 }
95 
97  step = step % myPhases.size();
98  if (myStep != step) {
99  myStep = step;
101  }
102 }
103 
104 // ------------ Static Information Retrieval
105 int
107  return (int)myPhases.size();
108 }
109 
110 
113  return myPhases;
114 }
115 
116 const MSPhaseDefinition&
118  assert((int)myPhases.size() > givenStep);
119  return *myPhases[givenStep];
120 }
121 
122 
123 // ------------ Dynamic Information Retrieval
124 int
126  return myStep;
127 }
128 
129 
130 const MSPhaseDefinition&
132  return *myPhases[myStep];
133 }
134 
135 
136 // ------------ Conversion between time and phase
137 SUMOTime
139  SUMOTime position = getOffsetFromIndex(myStep);
140  position += simStep - getPhase(myStep).myLastSwitch;
141  position = position % myDefaultCycleTime;
142  assert(position <= myDefaultCycleTime);
143  return position;
144 }
145 
146 
147 SUMOTime
149  assert(index < (int)myPhases.size());
150  SUMOTime pos = 0;
151  for (int i = 0; i < index; i++) {
152  pos += getPhase(i).duration;
153  }
154  return pos;
155 }
156 
157 
158 int
160  assert(offset <= myDefaultCycleTime);
161  if (offset == myDefaultCycleTime) {
162  return 0;
163  }
164  SUMOTime pos = offset;
165  SUMOTime testPos = 0;
166  for (int i = 0; i < (int)myPhases.size(); i++) {
167  testPos += getPhase(i).duration;
168  if (testPos > pos) {
169  return i;
170  }
171  if (testPos == pos) {
172  assert((int)myPhases.size() > (i + 1));
173  return i + 1;
174  }
175  }
176  return 0;
177 }
178 
179 
180 // ------------ Changing phases and phase durations
181 void
183  SUMOTime simStep, int step, SUMOTime stepDuration) {
185  //delete mySwitchCommand;Consider this operation!!!
186  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
187  myStep = step;
189  mySwitchCommand, stepDuration + simStep);
190 }
191 
192 
193 /****************************************************************************/
194 void
196  assert(step < (int)phases.size());
197  deletePhases();
198  myPhases = phases;
199  myStep = step;
200 }
201 
202 
203 void
205  for (int i = 0; i < (int)myPhases.size(); i++) {
206  delete myPhases[i];
207  }
208 }
209 
MSPhaseDefinition::myLastSwitch
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
Definition: MSPhaseDefinition.h:82
MSEventControl::addEvent
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Definition: MSEventControl.cpp:52
MSPhasedTrafficLightLogic::myPhases
Phases myPhases
The list of phases this logic uses.
Definition: MSPhasedTrafficLightLogic.h:175
MSPhasedTrafficLightLogic::getPhase
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
Definition: MSPhasedTrafficLightLogic.cpp:117
MSTrafficLightLogic::SwitchCommand
Class realising the switch between the traffic light phases.
Definition: MSTrafficLightLogic.h:351
MSPhasedTrafficLightLogic::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSPhasedTrafficLightLogic.cpp:131
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
MSPhasedTrafficLightLogic::getPhaseNumber
int getPhaseNumber() const
Returns the number of phases.
Definition: MSPhasedTrafficLightLogic.cpp:106
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
MSPhasedTrafficLightLogic::setPhases
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
Definition: MSPhasedTrafficLightLogic.cpp:195
MSPhasedTrafficLightLogic.h
MSTrafficLightLogic::mySwitchCommand
SwitchCommand * mySwitchCommand
The current switch command.
Definition: MSTrafficLightLogic.h:429
MSPhasedTrafficLightLogic::setStep
void setStep(int step)
Forces a specific step.
Definition: MSPhasedTrafficLightLogic.cpp:96
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:37
MSTrafficLightLogic.h
MSPhasedTrafficLightLogic::proceedToNextStep
void proceedToNextStep()
Proceed to the next step.
Definition: MSPhasedTrafficLightLogic.cpp:91
MSPhaseDefinition::duration
SUMOTime duration
The duration of the phase.
Definition: MSPhaseDefinition.h:70
MSPhasedTrafficLightLogic::getPhaseIndexAtTime
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
Definition: MSPhasedTrafficLightLogic.cpp:138
MSPhasedTrafficLightLogic::deletePhases
void deletePhases()
frees memory responsibilities
Definition: MSPhasedTrafficLightLogic.cpp:204
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:283
MSPhasedTrafficLightLogic::getPhases
const Phases & getPhases() const
Returns the phases of this tls program.
Definition: MSPhasedTrafficLightLogic.cpp:112
MSPhasedTrafficLightLogic::~MSPhasedTrafficLightLogic
~MSPhasedTrafficLightLogic()
Destructor.
Definition: MSPhasedTrafficLightLogic.cpp:49
MSTrafficLightLogic
The parent class for traffic light logics.
Definition: MSTrafficLightLogic.h:55
MSPhasedTrafficLightLogic::getCurrentPhaseIndex
int getCurrentPhaseIndex() const
Returns the current index within the program.
Definition: MSPhasedTrafficLightLogic.cpp:125
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSPhasedTrafficLightLogic::getIndexFromOffset
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
Definition: MSPhasedTrafficLightLogic.cpp:159
MSPhasedTrafficLightLogic::changeStepAndDuration
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Definition: MSPhasedTrafficLightLogic.cpp:182
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:148
MSPhasedTrafficLightLogic::myStep
int myStep
The current step.
Definition: MSPhasedTrafficLightLogic.h:191
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
MSEventControl.h
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:51