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-2015 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 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  const std::string& id, const std::string& subid,
56  const std::map<std::string, std::string>& parameters) :
57  MSTrafficLightLogic(tlcontrol, id, subid, DELTA_T, parameters),
58  myCurrentPhase(DELTA_T, "") {
60 }
61 
62 void
64  assert(myLanes.size() > 0);
65  LinkVectorVector::iterator i2; //iterator of the link indices of this junction (most likely there is just one link per index)
66  // find all outgoing lanes from the junction and its succeeding lanes leading to the next rail signal
67  // and find for every link at the junction all lanes leading from a previous signal to this link
68  for (i2 = myLinks.begin(); i2 != myLinks.end(); ++i2) { //for every link index
69  const LinkVector& links = *i2;
70  LinkVector::const_iterator i; //iterator of the links that belong to the same link index
71  for (i = links.begin(); i != links.end(); i++) { //for every link that belongs to the current index
72  MSLink* link = (*i);
73  MSLane* toLane = link->getLane(); //the lane this link is leading to
74  myLinksToLane[toLane].push_back(link);
75  myLinkIndices[link] = (unsigned int)std::distance(myLinks.begin(), i2); //assign the index of link to link
76 
77  //find all lanes leading from a previous signal to link (we presume that there exists only one path from a previous signal to link)
78  std::vector<MSLane*> afferentBlock; //the vector of lanes leading from a previous signal to link
79  bool noRailSignal = true; //true if the considered lane is not outgoing from a rail signal
80  //get the approaching lane of the link
81  MSLane* approachingLane = link->getApproachingLane(); //the lane this link is coming from
82  afferentBlock.push_back(approachingLane);
83  const MSLane* currentLane = approachingLane;
84  //look recursively for all lanes that lie before approachingLane and add them to afferentBlock until a rail signal is found
85  while (noRailSignal) {
86  std::vector<MSLane::IncomingLaneInfo> incomingLanes = currentLane->getIncomingLanes();
87  MSLane* precedentLane;
88  if (!incomingLanes.empty()) {
89  precedentLane = incomingLanes.front().lane;
90  } else {
91  precedentLane = 0;
92  }
93  if (precedentLane == 0) { //if there is no preceeding lane
94  noRailSignal = false;
95  } else {
96  const MSJunction* junction = precedentLane->getEdge().getToJunction();
97  if ((junction != 0) && (junction->getType() == NODETYPE_RAIL_SIGNAL)) { //if this junction exists and if it has a rail signal
98  noRailSignal = false;
99  } else {
100  afferentBlock.push_back(precedentLane);
101  currentLane = precedentLane;
102  }
103  }
104  }
105  myAfferentBlocks[link] = afferentBlock;
106 
107  //find all lanes leading from toLane to the next signal if it was not already done
108  if (std::find(myOutgoingLanes.begin(), myOutgoingLanes.end(), toLane) == myOutgoingLanes.end()) { //if toLane was not already contained in myOutgoingLanes
109  myOutgoingLanes.push_back(toLane);
110  std::vector<const MSLane*> succeedingBlock; //the vector of lanes leading to the next rail signal
111  succeedingBlock.push_back(toLane);
112  currentLane = toLane;
113  bool noRailSignal = true; //true if the considered lane is not ending at a rail signal
114  while (noRailSignal) {
115  //check first if the current lane is ending at a rail signal
116  std::vector<MSLink*> outGoingLinks = currentLane->getLinkCont();
117  std::vector<MSLink*>::const_iterator j;
118  for (j = outGoingLinks.begin(); j != outGoingLinks.end(); j++) {
119  const MSJunction* junction = currentLane->getEdge().getToJunction();
120  if ((junction != 0) && (junction->getType() == NODETYPE_RAIL_SIGNAL)) { //if this junctions exists and if it has a rail signal
121  noRailSignal = false;
122  break;
123  }
124  }
125  if (noRailSignal) { //if currentLane is not ending at a railSignal
126  //get the next lane
127  std::vector<const MSLane*> outGoingLanes = currentLane->getOutgoingLanes();
128  if (outGoingLanes.size() == 0) { //if the current lane has no outgoing lanes (deadend)
129  noRailSignal = false;
130  } else {
131  if (outGoingLanes.size() > 1) {
132  WRITE_WARNING("Rail lane '" + currentLane->getID() + "' has more than one outgoing lane but does not have a rail signal at its end");
133  }
134  const MSLane* nextLane = outGoingLanes.front();
135  succeedingBlock.push_back(nextLane);
136  currentLane = nextLane;
137  }
138  }
139  }
140  mySucceedingBlocks[toLane] = succeedingBlock;
141  }
142  }
143  }
144  updateCurrentPhase(); //check if this is necessary or if will be done already at another stage
145  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
146 }
147 
148 
150 
151 
152 // ----------- Handling of controlled links
153 void
157 }
158 
159 
160 // ------------ Switching and setting current rows
161 SUMOTime
164  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
165  return DELTA_T;
166 }
167 
168 std::string
170  std::string state(myLinks.size(), 'G'); //the state of the phase definition (all signal are green)
171  std::vector<MSLane*>::const_iterator i; //the iterator of outgoing lanes of this junction
172  for (i = myOutgoingLanes.begin(); i != myOutgoingLanes.end(); i++) { //for every outgoing lane
173  MSLane* lane = (*i);
174 
175  //check if the succeeding block is used by a train
176  bool succeedingBlockOccupied = false;
177  std::vector<const MSLane*>::const_iterator j;
178  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
179  if (!(*j)->isEmpty()) { //if this lane is not empty
180  succeedingBlockOccupied = true;
181  break;
182  }
183  }
184 
185  /*-if the succeeding block is occupied the signals for all links leading to lane will be set to red.
186  -if the succeeding block is not occupied and all blocks leading to lane are not occupied all signal
187  will keep green.
188  -if the succeeding block is not occupied and there is only one block leading to lane its signal will
189  keep green (no matter if this block is occupied or not).
190  -if the succeeding block is not occupied and there is more than one block leading to lane and some
191  of them are occupied the signals for all links leading to lane, except one whose corresponding block
192  is occupied, will be set to red. the signal for the remaining block will keep green*/
193  if (succeedingBlockOccupied) { //if the succeeding block is used by a train
194  std::vector<MSLink*>::const_iterator k;
195  for (k = myLinksToLane[lane].begin(); k != myLinksToLane[lane].end(); k++) { //for every link leading to this lane
196  state.replace(myLinkIndices[*k], 1, "r"); //set the signal of the link (*k) to red
197  }
198  } else {
199  if (myLinksToLane[lane].size() > 1) { //if there is more than one link leading to lane
200  bool hasOccupiedBlock = false;
201  std::vector<MSLink*>::const_iterator k;
202  for (k = myLinksToLane[lane].begin(); k != myLinksToLane[lane].end(); k++) { //for every link leading to lane
203  std::vector<MSLane*>::const_iterator l;
204  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)
205  if (!(*l)->isEmpty()) { //if this lane is not empty
206  hasOccupiedBlock = true;
207  //set the signals for all links leading to lane, except for (*k), to red; the signal for (*k) will remain green
208  std::vector<MSLink*>::const_iterator m;
209  for (m = myLinksToLane[lane].begin(); m != myLinksToLane[lane].end(); m++) { //for every link leading to lane
210  if (*m != *k) { //if this link is not the one corresponding to occupiedBlock
211  state.replace(myLinkIndices[*m], 1, "r"); //set the signal of this link to red
212  }
213  }
214  break; // we don't have to check the other lanes of this block anymore
215  }
216  }
217  if (hasOccupiedBlock) { //we don't have to check the other blocks anymore
218  break;
219  }
220  }
221  }
222  }
223  }
224  return state;
225 }
226 
227 
228 void
231 }
232 
233 
234 // ------------ Static Information Retrieval
235 unsigned int
237  return 0;
238 }
239 
242  return myPhases;
243 }
244 
245 const MSPhaseDefinition&
246 MSRailSignal::getPhase(unsigned int) const {
247  return myCurrentPhase;
248 }
249 
250 // ------------ Dynamic Information Retrieval
251 unsigned int
253  return 0;
254 }
255 
256 const MSPhaseDefinition&
258  return myCurrentPhase;
259 }
260 
261 // ------------ Conversion between time and phase
262 SUMOTime
264  return 0;
265 }
266 
267 SUMOTime
268 MSRailSignal::getOffsetFromIndex(unsigned int) const {
269  return 0;
270 }
271 
272 unsigned int
274  return 0;
275 }
276 
277 
278 /****************************************************************************/
279 
std::map< MSLink *, std::vector< 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
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:461
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:43
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
void updateCurrentPhase()
updates the current phase of the signal
const MSPhaseDefinition & getPhase(unsigned int givenstep) const
Returns the definition of the phase from the given position within the plan.
std::map< MSLink *, unsigned int > myLinkIndices
A map that maps a link to its link index.
Definition: MSRailSignal.h:216
The base class for an intersection.
Definition: MSJunction.h:61
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
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
#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...
A class that stores and controls tls and switching of their programs.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
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
SUMOTime getOffsetFromIndex(unsigned int index) const
Returns the position (start of a phase during a cycle) from of a given step.
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
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
SumoXMLNodeType getType() const
return the type of this Junction
Definition: MSJunction.h:119
SUMOTime trySwitch()
Switches to the next phase.
unsigned int getPhaseNumber() const
Returns the number of phases.
std::vector< MSLink * > LinkVector
Definition of the list of links that participate in this tl-light.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
unsigned int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
std::vector< const MSLane * > getOutgoingLanes() const
get the list of outgoing lanes
Definition: MSLane.cpp:1471
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index...
unsigned int getCurrentPhaseIndex() const
Returns the current index within the program.
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
const Phases & getPhases() const
Returns the phases of this tls program.
std::string getAppropriateState()
returns the state of the signal that actually required
The parent class for traffic light logics.
#define DELTA_T
Definition: SUMOTime.h:50
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:1059
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:237
const std::vector< IncomingLaneInfo > & getIncomingLanes() const
Definition: MSLane.h:590
The definition of a single phase of a tls logic.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
MSRailSignal(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &subid, const std::map< std::string, std::string > &parameters)
Constructor.
const MSJunction * getToJunction() const
Definition: MSEdge.h:349
~MSRailSignal()
Destructor.