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-2018 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 /****************************************************************************/
18 // Stores edges and lanes, performs moving of vehicle
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include "MSEdgeControl.h"
28 #include "MSGlobals.h"
29 #include "MSEdge.h"
30 #include "MSLane.h"
31 #include "MSVehicle.h"
32 #include <iostream>
33 #include <vector>
34 
35 
36 // ===========================================================================
37 // member method definitions
38 // ===========================================================================
39 MSEdgeControl::MSEdgeControl(const std::vector< MSEdge* >& edges)
40  : myEdges(edges),
41  myLanes(MSLane::dictSize()),
42  myLastLaneChange(MSEdge::dictSize()) {
43  // build the usage definitions for lanes
44  for (std::vector< MSEdge* >::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
45  const std::vector<MSLane*>& lanes = (*i)->getLanes();
46  if (!(*i)->hasLaneChanger()) {
47  int pos = (*lanes.begin())->getNumericalID();
48  myLanes[pos].lane = *(lanes.begin());
49  myLanes[pos].amActive = false;
50  myLanes[pos].haveNeighbors = false;
51  } else {
52  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
53  int pos = (*j)->getNumericalID();
54  myLanes[pos].lane = *j;
55  myLanes[pos].amActive = false;
56  myLanes[pos].haveNeighbors = true;
57  }
58  myLastLaneChange[(*i)->getNumericalID()] = -1;
59  }
60  }
61 }
62 
63 
65 }
66 
67 
68 void
70  for (std::set<MSLane*, ComparatorNumericalIdLess>::iterator i = myChangedStateLanes.begin(); i != myChangedStateLanes.end(); ++i) {
71  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
72  // if the lane was inactive but is now...
73  if (!lu.amActive && (*i)->getVehicleNumber() > 0) {
74  // ... add to active lanes and mark as such
75  if (lu.haveNeighbors) {
76  myActiveLanes.push_front(*i);
77  } else {
78  myActiveLanes.push_back(*i);
79  }
80  lu.amActive = true;
81  }
82  }
83  myChangedStateLanes.clear();
84 }
85 
86 void
88  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
89  if ((*i)->getVehicleNumber() == 0) {
90  myLanes[(*i)->getNumericalID()].amActive = false;
91  i = myActiveLanes.erase(i);
92  } else {
93  (*i)->planMovements(t);
94  ++i;
95  }
96  }
97 }
98 
99 void
101  for (MSLane* lane : myActiveLanes) {
102  lane->setJunctionApproaches(t);
103  }
104 }
105 
106 void
108  myWithVehicles2Integrate.clear();
109  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
110  if ((*i)->getVehicleNumber() == 0 || (*i)->executeMovements(t, myWithVehicles2Integrate)) {
111  myLanes[(*i)->getNumericalID()].amActive = false;
112  i = myActiveLanes.erase(i);
113  } else {
114  ++i;
115  }
116  }
117  for (std::vector<MSLane*>::iterator i = myWithVehicles2Integrate.begin(); i != myWithVehicles2Integrate.end(); ++i) {
118  if ((*i)->integrateNewVehicle(t)) {
119  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
120  if (!lu.amActive) {
121  if (lu.haveNeighbors) {
122  myActiveLanes.push_front(*i);
123  } else {
124  myActiveLanes.push_back(*i);
125  }
126  lu.amActive = true;
127  }
128  }
129  }
131  // multiple vehicle shadows may have entered an inactive lane and would
132  // not be sorted otherwise
133  for (LaneUsageVector::iterator it = myLanes.begin(); it != myLanes.end(); ++it) {
134  (*it).lane->sortPartialVehicles();
135  }
136  }
137 }
138 
139 
140 void
142  std::vector<MSLane*> toAdd;
143  MSGlobals::gComputeLC = true;
144  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
145  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
146  if (lu.haveNeighbors) {
147  MSEdge& edge = (*i)->getEdge();
148  if (myLastLaneChange[edge.getNumericalID()] != t) {
149  myLastLaneChange[edge.getNumericalID()] = t;
150  edge.changeLanes(t);
151  const std::vector<MSLane*>& lanes = edge.getLanes();
152  for (std::vector<MSLane*>::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
153  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
154  //if ((*i)->getID() == "disabled") {
155  // std::cout << SIMTIME << " vehicles=" << toString((*i)->getVehiclesSecure()) << "\n";
156  // (*i)->releaseVehicles();
157  //}
158  if ((*i)->getVehicleNumber() > 0 && !lu.amActive) {
159  toAdd.push_back(*i);
160  lu.amActive = true;
161  }
162  }
163  }
164  ++i;
165  } else {
166  i = myActiveLanes.end();
167  }
168  }
169  MSGlobals::gComputeLC = false;
170  for (std::vector<MSLane*>::iterator i = toAdd.begin(); i != toAdd.end(); ++i) {
171  myActiveLanes.push_front(*i);
172  }
174  // sort maneuver reservations
175  for (LaneUsageVector::iterator it = myLanes.begin(); it != myLanes.end(); ++it) {
176  (*it).lane->sortManeuverReservations();
177  }
178  }
179 }
180 
181 
182 void
183 MSEdgeControl::detectCollisions(SUMOTime timestep, const std::string& stage) {
184  // Detections is made by the edge's lanes, therefore hand over.
185  for (MSLane* lane : myActiveLanes) {
186  if (lane->needsCollisionCheck()) {
187  lane->detectCollisions(timestep, stage);
188  }
189  }
190 }
191 
192 
193 std::vector<std::string>
195  std::vector<std::string> ret;
196  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
197  ret.push_back((*i)->getID());
198  }
199  return ret;
200 }
201 
202 
203 void
205  myChangedStateLanes.insert(l);
206 }
207 
208 void
210  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
211  const std::vector<MSLane*>& lanes = (*i)->getLanes();
212  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
213  (*j)->initRestrictions();
214  }
215  }
216 }
217 
218 
219 /****************************************************************************/
220 
std::list< MSLane * > myActiveLanes
The list of active (not empty) lanes.
static double gLateralResolution
Definition: MSGlobals.h:85
long long int SUMOTime
Definition: SUMOTime.h:36
~MSEdgeControl()
Destructor.
std::set< MSLane *, ComparatorNumericalIdLess > myChangedStateLanes
Lanes which changed the state without informing the control.
void patchActiveLanes()
Resets information whether a lane is active for all lanes.
static bool gComputeLC
whether the simulationLoop is in the lane changing phase
Definition: MSGlobals.h:121
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:162
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:255
void setAdditionalRestrictions()
apply additional restrictions
std::vector< std::string > getEdgeNames() const
Returns the list of names of all known edges.
void setJunctionApproaches(SUMOTime t)
Register junction approaches for all vehicles after velocities have been planned. This is a prerequis...
A road/street connecting two junctions.
Definition: MSEdge.h:75
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...
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:691
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.
MSEdgeControl(const std::vector< MSEdge * > &edges)
Constructor.
Representation of a lane in the micro simulation.
Definition: MSLane.h:78