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-2016 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 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // member method definitions
49 // ===========================================================================
50 MSEdgeControl::MSEdgeControl(const std::vector< MSEdge* >& edges)
51  : myEdges(edges),
52  myLanes(MSLane::dictSize()),
53  myLastLaneChange(MSEdge::dictSize()) {
54  // build the usage definitions for lanes
55  for (std::vector< MSEdge* >::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
56  const std::vector<MSLane*>& lanes = (*i)->getLanes();
57  if (!(*i)->hasLaneChanger()) {
58  int pos = (*lanes.begin())->getNumericalID();
59  myLanes[pos].lane = *(lanes.begin());
60  myLanes[pos].firstNeigh = lanes.end();
61  myLanes[pos].lastNeigh = lanes.end();
62  myLanes[pos].amActive = false;
63  myLanes[pos].haveNeighbors = false;
64  } else {
65  for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
66  int pos = (*j)->getNumericalID();
67  myLanes[pos].lane = *j;
68  myLanes[pos].firstNeigh = (j + 1);
69  myLanes[pos].lastNeigh = lanes.end();
70  myLanes[pos].amActive = false;
71  myLanes[pos].haveNeighbors = true;
72  }
73  myLastLaneChange[(*i)->getNumericalID()] = -1;
74  }
75  }
76 }
77 
78 
80 }
81 
82 
83 void
85  for (std::set<MSLane*, Named::ComparatorIdLess>::iterator i = myChangedStateLanes.begin(); i != myChangedStateLanes.end(); ++i) {
86  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
87  // if the lane was inactive but is now...
88  if (!lu.amActive && (*i)->getVehicleNumber() > 0) {
89  // ... add to active lanes and mark as such
90  if (lu.haveNeighbors) {
91  myActiveLanes.push_front(*i);
92  } else {
93  myActiveLanes.push_back(*i);
94  }
95  lu.amActive = true;
96  }
97  }
98  myChangedStateLanes.clear();
99 }
100 
101 void
103  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
104  if ((*i)->getVehicleNumber() == 0) {
105  myLanes[(*i)->getNumericalID()].amActive = false;
106  i = myActiveLanes.erase(i);
107  } else {
108  (*i)->planMovements(t);
109  ++i;
110  }
111  }
112 }
113 
114 
115 void
117  myWithVehicles2Integrate.clear();
118  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
119  if ((*i)->getVehicleNumber() == 0 || (*i)->executeMovements(t, myWithVehicles2Integrate)) {
120  myLanes[(*i)->getNumericalID()].amActive = false;
121  i = myActiveLanes.erase(i);
122  } else {
123  ++i;
124  }
125  }
126  for (std::vector<MSLane*>::iterator i = myWithVehicles2Integrate.begin(); i != myWithVehicles2Integrate.end(); ++i) {
127  if ((*i)->integrateNewVehicle(t)) {
128  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
129  if (!lu.amActive) {
130  if (lu.haveNeighbors) {
131  myActiveLanes.push_front(*i);
132  } else {
133  myActiveLanes.push_back(*i);
134  }
135  lu.amActive = true;
136  }
137  }
138  }
140  // multiple vehicle shadows may have entered an inactive lane and would
141  // not be sorted otherwise
142  for (LaneUsageVector::iterator it = myLanes.begin(); it != myLanes.end(); ++it) {
143  (*it).lane->sortPartialVehicles();
144  }
145  }
146 }
147 
148 
149 void
151  std::vector<MSLane*> toAdd;
152  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
153  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
154  if (lu.haveNeighbors) {
155  MSEdge& edge = (*i)->getEdge();
156  if (myLastLaneChange[edge.getNumericalID()] != t) {
157  myLastLaneChange[edge.getNumericalID()] = t;
158  edge.changeLanes(t);
159  const std::vector<MSLane*>& lanes = edge.getLanes();
160  for (std::vector<MSLane*>::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
161  LaneUsage& lu = myLanes[(*i)->getNumericalID()];
162  //if ((*i)->getID() == "disabled") {
163  // std::cout << SIMTIME << " vehicles=" << toString((*i)->getVehiclesSecure()) << "\n";
164  // (*i)->releaseVehicles();
165  //}
166  if ((*i)->getVehicleNumber() > 0 && !lu.amActive) {
167  toAdd.push_back(*i);
168  lu.amActive = true;
169  }
170  }
171  }
172  ++i;
173  } else {
174  i = myActiveLanes.end();
175  }
176  }
177  for (std::vector<MSLane*>::iterator i = toAdd.begin(); i != toAdd.end(); ++i) {
178  myActiveLanes.push_front(*i);
179  }
180 }
181 
182 
183 void
184 MSEdgeControl::detectCollisions(SUMOTime timestep, const std::string& stage) {
185  // Detections is made by the edge's lanes, therefore hand over.
186  for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end(); ++i) {
187  (*i)->detectCollisions(timestep, stage);
188  }
189 }
190 
191 
192 std::vector<std::string>
194  std::vector<std::string> ret;
195  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
196  ret.push_back((*i)->getID());
197  }
198  return ret;
199 }
200 
201 
202 void
204  myChangedStateLanes.insert(l);
205 }
206 
207 
208 /****************************************************************************/
209 
std::list< MSLane * > myActiveLanes
The list of active (not empty) lanes.
long long int SUMOTime
Definition: SUMOTime.h:43
~MSEdgeControl()
Destructor.
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:192
std::vector< std::string > getEdgeNames() const
Returns the list of names of all known edges.
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.
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.
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.
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:271
virtual void changeLanes(SUMOTime t)
Performs lane changing on this edge.
Definition: MSEdge.cpp:571
static SUMOReal gLateralResolution
Definition: MSGlobals.h:84
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:79