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].firstNeigh = lanes.end();
50  myLanes[pos].lastNeigh = lanes.end();
51  myLanes[pos].amActive = false;
52  myLanes[pos].haveNeighbors = false;
53  } else {
54  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
55  int pos = (*j)->getNumericalID();
56  myLanes[pos].lane = *j;
57  myLanes[pos].firstNeigh = (j + 1);
58  myLanes[pos].lastNeigh = lanes.end();
59  myLanes[pos].amActive = false;
60  myLanes[pos].haveNeighbors = true;
61  }
62  myLastLaneChange[(*i)->getNumericalID()] = -1;
63  }
64  }
65 }
66 
67 
69 }
70 
71 
72 void
74  for (std::set<MSLane*, ComparatorIdLess>::iterator i = myChangedStateLanes.begin(); i != myChangedStateLanes.end(); ++i) {
75  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
76  // if the lane was inactive but is now...
77  if (!lu.amActive && (*i)->getVehicleNumber() > 0) {
78  // ... add to active lanes and mark as such
79  if (lu.haveNeighbors) {
80  myActiveLanes.push_front(*i);
81  } else {
82  myActiveLanes.push_back(*i);
83  }
84  lu.amActive = true;
85  }
86  }
87  myChangedStateLanes.clear();
88 }
89 
90 void
92  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
93  if ((*i)->getVehicleNumber() == 0) {
94  myLanes[(*i)->getNumericalID()].amActive = false;
95  i = myActiveLanes.erase(i);
96  } else {
97  (*i)->planMovements(t);
98  ++i;
99  }
100  }
101 }
102 
103 
104 void
106  myWithVehicles2Integrate.clear();
107  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
108  if ((*i)->getVehicleNumber() == 0 || (*i)->executeMovements(t, myWithVehicles2Integrate)) {
109  myLanes[(*i)->getNumericalID()].amActive = false;
110  i = myActiveLanes.erase(i);
111  } else {
112  ++i;
113  }
114  }
115  for (std::vector<MSLane*>::iterator i = myWithVehicles2Integrate.begin(); i != myWithVehicles2Integrate.end(); ++i) {
116  if ((*i)->integrateNewVehicle(t)) {
117  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
118  if (!lu.amActive) {
119  if (lu.haveNeighbors) {
120  myActiveLanes.push_front(*i);
121  } else {
122  myActiveLanes.push_back(*i);
123  }
124  lu.amActive = true;
125  }
126  }
127  }
129  // multiple vehicle shadows may have entered an inactive lane and would
130  // not be sorted otherwise
131  for (LaneUsageVector::iterator it = myLanes.begin(); it != myLanes.end(); ++it) {
132  (*it).lane->sortPartialVehicles();
133  }
134  }
135 }
136 
137 
138 void
140  std::vector<MSLane*> toAdd;
141  MSGlobals::gComputeLC = true;
142  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
143  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
144  if (lu.haveNeighbors) {
145  MSEdge& edge = (*i)->getEdge();
146  if (myLastLaneChange[edge.getNumericalID()] != t) {
147  myLastLaneChange[edge.getNumericalID()] = t;
148  edge.changeLanes(t);
149  const std::vector<MSLane*>& lanes = edge.getLanes();
150  for (std::vector<MSLane*>::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
151  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
152  //if ((*i)->getID() == "disabled") {
153  // std::cout << SIMTIME << " vehicles=" << toString((*i)->getVehiclesSecure()) << "\n";
154  // (*i)->releaseVehicles();
155  //}
156  if ((*i)->getVehicleNumber() > 0 && !lu.amActive) {
157  toAdd.push_back(*i);
158  lu.amActive = true;
159  }
160  }
161  }
162  ++i;
163  } else {
164  i = myActiveLanes.end();
165  }
166  }
167  MSGlobals::gComputeLC = false;
168  for (std::vector<MSLane*>::iterator i = toAdd.begin(); i != toAdd.end(); ++i) {
169  myActiveLanes.push_front(*i);
170  }
172  // sort maneuver reservations
173  for (LaneUsageVector::iterator it = myLanes.begin(); it != myLanes.end(); ++it) {
174  (*it).lane->sortManeuverReservations();
175  }
176  }
177 }
178 
179 
180 void
181 MSEdgeControl::detectCollisions(SUMOTime timestep, const std::string& stage) {
182  // Detections is made by the edge's lanes, therefore hand over.
183  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end(); ++i) {
184  (*i)->detectCollisions(timestep, stage);
185  }
186 }
187 
188 
189 std::vector<std::string>
191  std::vector<std::string> ret;
192  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
193  ret.push_back((*i)->getID());
194  }
195  return ret;
196 }
197 
198 
199 void
201  myChangedStateLanes.insert(l);
202 }
203 
204 void
206  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
207  const std::vector<MSLane*>& lanes = (*i)->getLanes();
208  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
209  (*j)->initRestrictions();
210  }
211  }
212 }
213 
214 
215 /****************************************************************************/
216 
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.
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:254
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: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...
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:642
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:71