SUMO - Simulation of Urban MObility
MSEdgeControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // Stores edges and lanes, performs moving of vehicle
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "MSEdgeControl.h"
34 #include "MSGlobals.h"
35 #include "MSEdge.h"
36 #include "MSLane.h"
37 #include "MSVehicle.h"
38 #include <iostream>
39 #include <vector>
40 
41 
42 // ===========================================================================
43 // member method definitions
44 // ===========================================================================
45 MSEdgeControl::MSEdgeControl(const std::vector< MSEdge* >& edges)
46  : myEdges(edges),
47  myLanes(MSLane::dictSize()),
48  myLastLaneChange(MSEdge::dictSize()) {
49  // build the usage definitions for lanes
50  for (std::vector< MSEdge* >::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
51  const std::vector<MSLane*>& lanes = (*i)->getLanes();
52  if (!(*i)->hasLaneChanger()) {
53  int pos = (*lanes.begin())->getNumericalID();
54  myLanes[pos].lane = *(lanes.begin());
55  myLanes[pos].firstNeigh = lanes.end();
56  myLanes[pos].lastNeigh = lanes.end();
57  myLanes[pos].amActive = false;
58  myLanes[pos].haveNeighbors = false;
59  } else {
60  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
61  int pos = (*j)->getNumericalID();
62  myLanes[pos].lane = *j;
63  myLanes[pos].firstNeigh = (j + 1);
64  myLanes[pos].lastNeigh = lanes.end();
65  myLanes[pos].amActive = false;
66  myLanes[pos].haveNeighbors = true;
67  }
68  myLastLaneChange[(*i)->getNumericalID()] = -1;
69  }
70  }
71 }
72 
73 
75 }
76 
77 
78 void
80  for (std::set<MSLane*, ComparatorIdLess>::iterator i = myChangedStateLanes.begin(); i != myChangedStateLanes.end(); ++i) {
81  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
82  // if the lane was inactive but is now...
83  if (!lu.amActive && (*i)->getVehicleNumber() > 0) {
84  // ... add to active lanes and mark as such
85  if (lu.haveNeighbors) {
86  myActiveLanes.push_front(*i);
87  } else {
88  myActiveLanes.push_back(*i);
89  }
90  lu.amActive = true;
91  }
92  }
93  myChangedStateLanes.clear();
94 }
95 
96 void
98  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
99  if ((*i)->getVehicleNumber() == 0) {
100  myLanes[(*i)->getNumericalID()].amActive = false;
101  i = myActiveLanes.erase(i);
102  } else {
103  (*i)->planMovements(t);
104  ++i;
105  }
106  }
107 }
108 
109 
110 void
112  myWithVehicles2Integrate.clear();
113  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
114  if ((*i)->getVehicleNumber() == 0 || (*i)->executeMovements(t, myWithVehicles2Integrate)) {
115  myLanes[(*i)->getNumericalID()].amActive = false;
116  i = myActiveLanes.erase(i);
117  } else {
118  ++i;
119  }
120  }
121  for (std::vector<MSLane*>::iterator i = myWithVehicles2Integrate.begin(); i != myWithVehicles2Integrate.end(); ++i) {
122  if ((*i)->integrateNewVehicle(t)) {
123  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
124  if (!lu.amActive) {
125  if (lu.haveNeighbors) {
126  myActiveLanes.push_front(*i);
127  } else {
128  myActiveLanes.push_back(*i);
129  }
130  lu.amActive = true;
131  }
132  }
133  }
135  // multiple vehicle shadows may have entered an inactive lane and would
136  // not be sorted otherwise
137  for (LaneUsageVector::iterator it = myLanes.begin(); it != myLanes.end(); ++it) {
138  (*it).lane->sortPartialVehicles();
139  (*it).lane->sortManeuverReservations();
140  }
141  }
142 }
143 
144 
145 void
147  std::vector<MSLane*> toAdd;
148  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
149  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
150  if (lu.haveNeighbors) {
151  MSEdge& edge = (*i)->getEdge();
152  if (myLastLaneChange[edge.getNumericalID()] != t) {
153  myLastLaneChange[edge.getNumericalID()] = t;
154  edge.changeLanes(t);
155  const std::vector<MSLane*>& lanes = edge.getLanes();
156  for (std::vector<MSLane*>::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
157  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
158  //if ((*i)->getID() == "disabled") {
159  // std::cout << SIMTIME << " vehicles=" << toString((*i)->getVehiclesSecure()) << "\n";
160  // (*i)->releaseVehicles();
161  //}
162  if ((*i)->getVehicleNumber() > 0 && !lu.amActive) {
163  toAdd.push_back(*i);
164  lu.amActive = true;
165  }
166  }
167  }
168  ++i;
169  } else {
170  i = myActiveLanes.end();
171  }
172  }
173  for (std::vector<MSLane*>::iterator i = toAdd.begin(); i != toAdd.end(); ++i) {
174  myActiveLanes.push_front(*i);
175  }
176 }
177 
178 
179 void
180 MSEdgeControl::detectCollisions(SUMOTime timestep, const std::string& stage) {
181  // Detections is made by the edge's lanes, therefore hand over.
182  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end(); ++i) {
183  (*i)->detectCollisions(timestep, stage);
184  }
185 }
186 
187 
188 std::vector<std::string>
190  std::vector<std::string> ret;
191  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
192  ret.push_back((*i)->getID());
193  }
194  return ret;
195 }
196 
197 
198 void
200  myChangedStateLanes.insert(l);
201 }
202 
203 void
205  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
206  const std::vector<MSLane*>& lanes = (*i)->getLanes();
207  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
208  (*j)->initRestrictions();
209  }
210  }
211 }
212 
213 
214 /****************************************************************************/
215 
std::list< MSLane * > myActiveLanes
The list of active (not empty) lanes.
static double gLateralResolution
Definition: MSGlobals.h:91
~MSEdgeControl()
Destructor.
void patchActiveLanes()
Resets information whether a lane is active for all lanes.
LaneUsageVector myLanes
Information about lanes&#39; number of vehicles and neighbors.
void changeLanes(SUMOTime t)
Moves (precomputes) critical vehicles.
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:167
bool amActive
Information whether this lane is active.
A structure holding some basic information about a simulated lane.
void gotActive(MSLane *l)
Informs the control that the given lane got active.
void detectCollisions(SUMOTime timestep, const std::string &stage)
Detect collisions.
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:259
void setAdditionalRestrictions()
apply additional restrictions
std::vector< std::string > getEdgeNames() const
Returns the list of names of all known edges.
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::vector< SUMOTime > myLastLaneChange
The list of active (not empty) lanes.
MSEdgeVector myEdges
Loaded edges.
void planMovements(SUMOTime t)
Compute safe velocities for all vehicles based on positions and speeds from the last time step...
std::set< MSLane *, ComparatorIdLess > myChangedStateLanes
Lanes which changed the state without informing the control.
void executeMovements(SUMOTime t)
Executes planned vehicle movements with regards to right-of-way.
virtual void changeLanes(SUMOTime t)
Performs lane changing on this edge.
Definition: MSEdge.cpp:646
bool haveNeighbors
Information whether this lane belongs to a multi-lane edge.
std::vector< MSLane * > myWithVehicles2Integrate
A storage for lanes which shall be integrated because vehicles have moved onto them.
long long int SUMOTime
Definition: TraCIDefs.h:51
MSEdgeControl(const std::vector< MSEdge * > &edges)
Constructor.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77