SUMO - Simulation of Urban MObility
MSEdgeControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Stores edges and lanes, performs moving of vehicle
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include "MSEdgeControl.h"
35 #include "MSGlobals.h"
36 #include "MSEdge.h"
37 #include "MSLane.h"
38 #include "MSVehicle.h"
39 #include <iostream>
40 #include <vector>
41 
42 
43 // ===========================================================================
44 // member method definitions
45 // ===========================================================================
46 MSEdgeControl::MSEdgeControl(const std::vector< MSEdge* >& edges)
47  : myEdges(edges),
48  myLanes(MSLane::dictSize()),
49  myLastLaneChange(MSEdge::dictSize()) {
50  // build the usage definitions for lanes
51  for (std::vector< MSEdge* >::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
52  const std::vector<MSLane*>& lanes = (*i)->getLanes();
53  if (!(*i)->hasLaneChanger()) {
54  int pos = (*lanes.begin())->getNumericalID();
55  myLanes[pos].lane = *(lanes.begin());
56  myLanes[pos].firstNeigh = lanes.end();
57  myLanes[pos].lastNeigh = lanes.end();
58  myLanes[pos].amActive = false;
59  myLanes[pos].haveNeighbors = false;
60  } else {
61  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
62  int pos = (*j)->getNumericalID();
63  myLanes[pos].lane = *j;
64  myLanes[pos].firstNeigh = (j + 1);
65  myLanes[pos].lastNeigh = lanes.end();
66  myLanes[pos].amActive = false;
67  myLanes[pos].haveNeighbors = true;
68  }
69  myLastLaneChange[(*i)->getNumericalID()] = -1;
70  }
71  }
72 }
73 
74 
76 }
77 
78 
79 void
81  for (std::set<MSLane*, Named::ComparatorIdLess>::iterator i = myChangedStateLanes.begin(); i != myChangedStateLanes.end(); ++i) {
82  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
83  // if the lane was inactive but is now...
84  if (!lu.amActive && (*i)->getVehicleNumber() > 0) {
85  // ... add to active lanes and mark as such
86  if (lu.haveNeighbors) {
87  myActiveLanes.push_front(*i);
88  } else {
89  myActiveLanes.push_back(*i);
90  }
91  lu.amActive = true;
92  }
93  }
94  myChangedStateLanes.clear();
95 }
96 
97 void
99  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
100  if ((*i)->getVehicleNumber() == 0) {
101  myLanes[(*i)->getNumericalID()].amActive = false;
102  i = myActiveLanes.erase(i);
103  } else {
104  (*i)->planMovements(t);
105  ++i;
106  }
107  }
108 }
109 
110 
111 void
113  myWithVehicles2Integrate.clear();
114  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
115  if ((*i)->getVehicleNumber() == 0 || (*i)->executeMovements(t, myWithVehicles2Integrate)) {
116  myLanes[(*i)->getNumericalID()].amActive = false;
117  i = myActiveLanes.erase(i);
118  } else {
119  ++i;
120  }
121  }
122  for (std::vector<MSLane*>::iterator i = myWithVehicles2Integrate.begin(); i != myWithVehicles2Integrate.end(); ++i) {
123  if ((*i)->integrateNewVehicle(t)) {
124  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
125  if (!lu.amActive) {
126  if (lu.haveNeighbors) {
127  myActiveLanes.push_front(*i);
128  } else {
129  myActiveLanes.push_back(*i);
130  }
131  lu.amActive = true;
132  }
133  }
134  }
136  // multiple vehicle shadows may have entered an inactive lane and would
137  // not be sorted otherwise
138  for (LaneUsageVector::iterator it = myLanes.begin(); it != myLanes.end(); ++it) {
139  (*it).lane->sortPartialVehicles();
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:92
~MSEdgeControl()
Destructor.
void patchActiveLanes()
Resets information whether a lane is active for all lanes.
std::set< MSLane *, Named::ComparatorIdLess > myChangedStateLanes
Lanes which changed the state without informing the control.
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:192
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:275
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...
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:640
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:52
MSEdgeControl(const std::vector< MSEdge * > &edges)
Constructor.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79