SUMO - Simulation of Urban MObility
MSRailSignal.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // A rail signal logic
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <cassert>
33 #include <utility>
34 #include <vector>
35 #include <bitset>
37 #include <microsim/MSNet.h>
38 #include <microsim/MSEdge.h>
39 #include "MSTrafficLightLogic.h"
40 #include "MSRailSignal.h"
41 #include <microsim/MSLane.h>
42 #include "MSPhaseDefinition.h"
43 #include "MSTLLogicControl.h"
44 
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51  const std::string& id, const std::string& subid,
52  const std::map<std::string, std::string>& parameters) :
53  MSTrafficLightLogic(tlcontrol, id, subid, DELTA_T, parameters),
54  myCurrentPhase(DELTA_T, std::string(SUMO_MAX_CONNECTIONS, 'X')) { // dummy phase
56 }
57 
58 void
60  assert(myLanes.size() > 0);
61  LinkVectorVector::iterator i2; //iterator of the link indices of this junction (most likely there is just one link per index)
62  // find all outgoing lanes from the junction and its succeeding lanes leading to the next rail signal
63  // and find for every link at the junction all lanes leading from a previous signal to this link
64  for (i2 = myLinks.begin(); i2 != myLinks.end(); ++i2) { //for every link index
65  const LinkVector& links = *i2;
66  LinkVector::const_iterator i; //iterator of the links that belong to the same link index
67  for (i = links.begin(); i != links.end(); i++) { //for every link that belongs to the current index
68  MSLink* link = (*i);
69  MSLane* toLane = link->getLane(); //the lane this link is leading to
70  myLinksToLane[toLane].push_back(link);
71  myLinkIndices[link] = (int)std::distance(myLinks.begin(), i2); //assign the index of link to link
72 
73  //find all lanes leading from a previous signal to link (we presume that there exists only one path from a previous signal to link)
74  std::vector<const MSLane*> afferentBlock; //the vector of lanes leading from a previous signal to link
75  bool noRailSignal = true; //true if the considered lane is not outgoing from a rail signal
76  //get the approaching lane of the link
77  const MSLane* approachingLane = link->getLaneBefore(); //the lane this link is coming from
78  afferentBlock.push_back(approachingLane);
79  const MSLane* currentLane = approachingLane;
80  //look recursively for all lanes that lie before approachingLane and add them to afferentBlock until a rail signal is found
81  while (noRailSignal) {
82  std::vector<MSLane::IncomingLaneInfo> incomingLanes = currentLane->getIncomingLanes();
83  MSLane* precedentLane;
84  if (!incomingLanes.empty()) {
85  precedentLane = incomingLanes.front().lane;
86  } else {
87  precedentLane = 0;
88  }
89  if (precedentLane == 0) { //if there is no preceeding lane
90  noRailSignal = false;
91  } else {
92  const MSJunction* junction = precedentLane->getEdge().getToJunction();
93  if ((junction != 0) && (junction->getType() == NODETYPE_RAIL_SIGNAL)) { //if this junction exists and if it has a rail signal
94  noRailSignal = false;
95  } else {
96  afferentBlock.push_back(precedentLane);
97  currentLane = precedentLane;
98  }
99  }
100  }
101  myAfferentBlocks[link] = afferentBlock;
102 
103  //find all lanes leading from toLane to the next signal if it was not already done
104  if (std::find(myOutgoingLanes.begin(), myOutgoingLanes.end(), toLane) == myOutgoingLanes.end()) { //if toLane was not already contained in myOutgoingLanes
105  myOutgoingLanes.push_back(toLane);
106  std::vector<const MSLane*> succeedingBlock; //the vector of lanes leading to the next rail signal
107  succeedingBlock.push_back(toLane);
108  currentLane = toLane;
109  bool noRailSignal = true; //true if the considered lane is not ending at a rail signal
110  while (noRailSignal) {
111  //check first if the current lane is ending at a rail signal
112  std::vector<MSLink*> outGoingLinks = currentLane->getLinkCont();
113  std::vector<MSLink*>::const_iterator j;
114  for (j = outGoingLinks.begin(); j != outGoingLinks.end(); j++) {
115  const MSJunction* junction = currentLane->getEdge().getToJunction();
116  if ((junction != 0) && (junction->getType() == NODETYPE_RAIL_SIGNAL)) { //if this junctions exists and if it has a rail signal
117  noRailSignal = false;
118  break;
119  }
120  }
121  if (noRailSignal) { //if currentLane is not ending at a railSignal
122  //get the next lane
123  std::vector<const MSLane*> outGoingLanes = currentLane->getOutgoingLanes();
124  if (outGoingLanes.size() == 0) { //if the current lane has no outgoing lanes (deadend)
125  noRailSignal = false;
126  } else {
127  if (outGoingLanes.size() > 1) {
128  WRITE_WARNING("Rail lane '" + currentLane->getID() + "' has more than one outgoing lane but does not have a rail signal at its end");
129  }
130  const MSLane* nextLane = outGoingLanes.front();
131  succeedingBlock.push_back(nextLane);
132  currentLane = nextLane;
133  }
134  }
135  }
136  mySucceedingBlocks[toLane] = succeedingBlock;
137  }
138  }
139  }
140  updateCurrentPhase(); //check if this is necessary or if will be done already at another stage
141  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
142 }
143 
144 
146 
147 
148 // ----------- Handling of controlled links
149 void
153 }
154 
155 
156 // ------------ Switching and setting current rows
157 SUMOTime
160  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
161  return DELTA_T;
162 }
163 
164 std::string
166  std::string state(myLinks.size(), 'G'); //the state of the phase definition (all signal are green)
167  std::vector<MSLane*>::const_iterator i; //the iterator of outgoing lanes of this junction
168  for (i = myOutgoingLanes.begin(); i != myOutgoingLanes.end(); i++) { //for every outgoing lane
169  MSLane* lane = (*i);
170 
171  //check if the succeeding block is used by a train
172  bool succeedingBlockOccupied = false;
173  std::vector<const MSLane*>::const_iterator j;
174  for (j = mySucceedingBlocks.at(lane).begin(); j != mySucceedingBlocks.at(lane).end(); j++) { //for every lane in the block between the current signal and the next signal
175  if (!(*j)->isEmpty()) { //if this lane is not empty
176  succeedingBlockOccupied = true;
177  break;
178  }
179  }
180 
181  /*-if the succeeding block is occupied the signals for all links leading to lane will be set to red.
182  -if the succeeding block is not occupied and all blocks leading to lane are not occupied all signal
183  will keep green.
184  -if the succeeding block is not occupied and there is only one block leading to lane its signal will
185  keep green (no matter if this block is occupied or not).
186  -if the succeeding block is not occupied and there is more than one block leading to lane and some
187  of them are occupied the signals for all links leading to lane, except one whose corresponding block
188  is occupied, will be set to red. the signal for the remaining block will keep green*/
189  if (succeedingBlockOccupied) { //if the succeeding block is used by a train
190  std::vector<MSLink*>::const_iterator k;
191  for (k = myLinksToLane[lane].begin(); k != myLinksToLane[lane].end(); k++) { //for every link leading to this lane
192  state.replace(myLinkIndices[*k], 1, "r"); //set the signal of the link (*k) to red
193  }
194  } else {
195  if (myLinksToLane[lane].size() > 1) { //if there is more than one link leading to lane
196  bool hasOccupiedBlock = false;
197  std::vector<MSLink*>::const_iterator k;
198  for (k = myLinksToLane[lane].begin(); k != myLinksToLane[lane].end(); k++) { //for every link leading to lane
199  std::vector<const MSLane*>::const_iterator l;
200  for (l = myAfferentBlocks[(*k)].begin(); l != myAfferentBlocks[(*k)].end(); l++) { //for every lane of the block leading from a previous signal to the link (*k)
201  if (!(*l)->isEmpty()) { //if this lane is not empty
202  hasOccupiedBlock = true;
203  //set the signals for all links leading to lane, except for (*k), to red; the signal for (*k) will remain green
204  std::vector<MSLink*>::const_iterator m;
205  for (m = myLinksToLane[lane].begin(); m != myLinksToLane[lane].end(); m++) { //for every link leading to lane
206  if (*m != *k) { //if this link is not the one corresponding to occupiedBlock
207  state.replace(myLinkIndices[*m], 1, "r"); //set the signal of this link to red
208  }
209  }
210  break; // we don't have to check the other lanes of this block anymore
211  }
212  }
213  if (hasOccupiedBlock) { //we don't have to check the other blocks anymore
214  break;
215  }
216  }
217  }
218  }
219  }
220  return state;
221 }
222 
223 
224 void
227 }
228 
229 
230 // ------------ Static Information Retrieval
231 int
233  return 0;
234 }
235 
238  return myPhases;
239 }
240 
241 const MSPhaseDefinition&
243  return myCurrentPhase;
244 }
245 
246 // ------------ Dynamic Information Retrieval
247 int
249  return 0;
250 }
251 
252 const MSPhaseDefinition&
254  return myCurrentPhase;
255 }
256 
257 // ------------ Conversion between time and phase
258 SUMOTime
260  return 0;
261 }
262 
263 SUMOTime
265  return 0;
266 }
267 
268 int
270  return 0;
271 }
272 
273 
274 /****************************************************************************/
275 
const std::vector< IncomingLaneInfo > & getIncomingLanes() const
Definition: MSLane.h:719
Builds detectors for microsim.
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
void updateCurrentPhase()
updates the current phase of the signal
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
The base class for an intersection.
Definition: MSJunction.h:64
std::vector< const MSLane * > getOutgoingLanes() const
get the list of outgoing lanes
Definition: MSLane.cpp:2070
std::map< MSLane *, std::vector< MSLink * > > myLinksToLane
A map that maps an outgoing lane from the junction to its set of links that lead to this lane...
Definition: MSRailSignal.h:219
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
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:41
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
const std::string & getID() const
Returns the id.
Definition: Named.h:66
const MSJunction * getToJunction() const
Definition: MSEdge.h:372
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index...
std::map< MSLink *, int > myLinkIndices
A map that maps a link to its link index.
Definition: MSRailSignal.h:216
A class that stores and controls tls and switching of their programs.
std::vector< MSLane * > myOutgoingLanes
The set of lanes going out from the junction.
Definition: MSRailSignal.h:213
SUMOTime myDefaultCycleTime
The cycle time (without changes)
void init(NLDetectorBuilder &nb)
Initialises the rail signal with information about adjacent rail signals.
Phases myPhases
The list of phases this logic uses.
Definition: MSRailSignal.h:234
const Phases & getPhases() const
Returns the phases of this tls program.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
std::map< MSLane *, std::vector< const MSLane * > > mySucceedingBlocks
A map that maps an outgoing lane from the junction to its vector of lanes leading to the next signal...
Definition: MSRailSignal.h:225
int getPhaseNumber() const
Returns the number of phases.
SUMOTime trySwitch()
Switches to the next phase.
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index...
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
std::string getAppropriateState()
returns the state of the signal that actually required
The parent class for traffic light logics.
long long int SUMOTime
Definition: TraCIDefs.h:52
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:237
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:1597
std::map< MSLink *, std::vector< const MSLane * > > myAfferentBlocks
A map that maps a link from the junction to its vector of lanes leading from a previous signal to thi...
Definition: MSRailSignal.h:222
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
The definition of a single phase of a tls logic.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
MSRailSignal(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &subid, const std::map< std::string, std::string > &parameters)
Constructor.
int getCurrentPhaseIndex() const
Returns the current index within the program.
SumoXMLNodeType getType() const
return the type of this Junction
Definition: MSJunction.h:128
~MSRailSignal()
Destructor.