SUMO - Simulation of Urban MObility
MSSimpleTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // A fixed traffic light logic
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <utility>
37 #include <vector>
38 #include <bitset>
39 #include <sstream>
41 #include <microsim/MSNet.h>
42 #include "MSTLLogicControl.h"
43 #include "MSTrafficLightLogic.h"
45 
46 
47 // ===========================================================================
48 // member method definitions
49 // ===========================================================================
51  const std::string& id, const std::string& subid, const Phases& phases,
52  int step, SUMOTime delay,
53  const std::map<std::string, std::string>& parameters) :
54  MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters),
55  myPhases(phases),
56  myStep(step) {
57  for (int i = 0; i < (int)myPhases.size(); i++) {
58  myDefaultCycleTime += myPhases[i]->duration;
59  }
60 }
61 
62 
64  deletePhases();
65 }
66 
67 
68 // ------------ Switching and setting current rows
71  // check whether the current duration shall be increased
75  return delay;
76  }
77 
78  // increment the index
79  myStep++;
80  // if the last phase was reached ...
81  if (myStep >= (int)myPhases.size()) {
82  // ... set the index to the first phase
83  myStep = 0;
84  }
85  assert((int)myPhases.size() > myStep);
86  //stores the time the phase started
88  // check whether the next duration was overridden
89  if (myOverridingTimes.size() > 0) {
90  SUMOTime nextDuration = myOverridingTimes[0];
91  myOverridingTimes.erase(myOverridingTimes.begin());
92  return nextDuration;
93  }
94  // return offset to the next switch
95  return myPhases[myStep]->duration;
96 }
97 
98 
99 // ------------ Static Information Retrieval
100 int
102  return (int) myPhases.size();
103 }
104 
105 
108  return myPhases;
109 }
110 
111 
114  return myPhases;
115 }
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 = 0;
142  if (myStep > 0) {
143  for (int i = 0; i < myStep; i++) {
144  position = position + getPhase(i).duration;
145  }
146  }
147  position = position + simStep - getPhase(myStep).myLastSwitch;
148  position = position % myDefaultCycleTime;
149  assert(position <= myDefaultCycleTime);
150  return position;
151 }
152 
153 
154 SUMOTime
156  assert(index < (int)myPhases.size());
157  if (index == 0) {
158  return 0;
159  }
160  SUMOTime pos = 0;
161  for (int i = 0; i < index; i++) {
162  pos += getPhase(i).duration;
163  }
164  return pos;
165 }
166 
167 
168 int
170  offset = offset % myDefaultCycleTime;
171  if (offset == myDefaultCycleTime) {
172  return 0;
173  }
174  SUMOTime testPos = 0;
175  for (int i = 0; i < (int)myPhases.size(); i++) {
176  testPos = testPos + getPhase(i).duration;
177  if (testPos > offset) {
178  return i;
179  }
180  if (testPos == offset) {
181  assert((int)myPhases.size() > (i + 1));
182  return (i + 1);
183  }
184  }
185  return 0;
186 }
187 
188 
189 // ------------ Changing phases and phase durations
190 void
192  SUMOTime simStep, int step, SUMOTime stepDuration) {
194  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
195  if (step != myStep) {
196  myStep = step;
197  setTrafficLightSignals(simStep);
198  tlcontrol.get(getID()).executeOnSwitchActions();
199  }
201  mySwitchCommand, stepDuration + simStep);
202 }
203 
204 
205 void
207  assert(step < (int)phases.size());
208  deletePhases();
209  myPhases = phases;
210  myStep = step;
211 }
212 
213 
214 void
216  for (int i = 0; i < (int)myPhases.size(); i++) {
217  delete myPhases[i];
218  }
219 }
220 
221 
222 /****************************************************************************/
223 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
MSSimpleTrafficLightLogic(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.
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Phases myPhases
The list of phases this logic uses.
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
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.
int getCurrentPhaseIndex() const
Returns the current index within the program.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:403
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
virtual SUMOTime trySwitch()
Switches to the next phase.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:257
void deletePhases()
frees memory responsibilities
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
int getPhaseNumber() const
Returns the number of phases.
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
long long int SUMOTime
Definition: TraCIDefs.h:52
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
const Phases & getPhases() const
Returns the phases of this tls program.
The definition of a single phase of a tls logic.
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.