1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 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 /****************************************************************************/
10 /// @file    MSEdgeControl.h
11 /// @author  Christian Roessel
12 /// @author  Daniel Krajzewicz
13 /// @author  Jakob Erdmann
14 /// @author  Christoph Sommer
15 /// @author  Sascha Krieg
16 /// @author  Michael Behrisch
17 /// @date    Mon, 09 Apr 2001
18 /// @version $Id$
19 ///
20 // Stores edges and lanes, performs moving of vehicle
21 /****************************************************************************/
22 #ifndef MSEdgeControl_h
23 #define MSEdgeControl_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #include <config.h>
30 
31 #include <vector>
32 #include <map>
33 #include <string>
34 #include <iostream>
35 #include <list>
36 #include <set>
37 #include <queue>
38 #include <utils/common/SUMOTime.h>
39 #include <utils/common/Named.h>
40 #include <microsim/MSLane.h>
41 
42 #include <utils/foxtools/FXSynchQue.h>
43 #ifdef HAVE_FOX
44 #include <utils/foxtools/FXWorkerThread.h>
45 #endif
46 
47 
48 // ===========================================================================
49 // class declarations
50 // ===========================================================================
51 class MSEdge;
52 class MSLane;
53 class OutputDevice;
54 
55 typedef std::vector<MSEdge*> MSEdgeVector;
56 
57 // ===========================================================================
58 // class definitions
59 // ===========================================================================
60 /**
61  * @class MSEdgeControl
62  * @brief Stores edges and lanes, performs moving of vehicle
63  *
64  * In order to avoid touching all lanes, even the empty ones, this class stores
65  *  and updates the information about "active" lanes, those that have at least
66  *  one vehicle on them. During longitudinal movement, this can be simply
67  *  achieved through return values of the MSLane-methods, signalling either
68  *  that the lane got active or inactive. This is but not possible when
69  *  changing lanes, we have to go through the lanes, here. Also, we have to
70  *  add lanes on which a vehicle was inserted, separately, doing this into
71  *  ("myChangedStateLanes") which entries are integrated at the begin of is step
72  *  in "patchActiveLanes".
73  */
74 class MSEdgeControl {
75 
76 public:
77     /** @brief Constructor
78      *
79      * Builds LaneUsage information for each lane and assigns them to lanes.
80      *
81      * @param[in] edges The loaded edges
82      * @todo Assure both containers are not 0
83      */
84     MSEdgeControl(const std::vector< MSEdge* >& edges);
85 
86 
87     /// @brief Destructor.
88     ~MSEdgeControl();
89 
90 
91     /** @brief Resets information whether a lane is active for all lanes
92      *
93      * For each lane in "myChangedStateLanes": if the lane has at least one vehicle
94      *  and is not marked as being active, it is added to the list og active lanes
95      *  and marked as being active.
96      */
97     void patchActiveLanes();
98 
99 
100     /// @name Interfaces for longitudinal vehicle movement
101     /// @{
102 
103     /** @brief Compute safe velocities for all vehicles based on positions and
104      * speeds from the last time step. Also registers
105      * ApproachingVehicleInformation for all links
106      *
107      * This method goes through all active lanes calling their "planMovements" method.
108      * @see MSLane::planMovements
109      */
110     void planMovements(SUMOTime t);
111 
112     /** @brief Register junction approaches for all vehicles after velocities
113      * have been planned. This is a prerequisite for executeMovements
114      *
115      * This method goes through all active lanes calling their "setJunctionApproaches" method.
116      */
117     void setJunctionApproaches(SUMOTime t);
118 
119 
120     /** @brief Executes planned vehicle movements with regards to right-of-way
121      *
122      * This method goes through all active lanes calling their executeMovements
123      * method which causes vehicles to update their positions and speeds.
124      * Lanes which receive new vehicles are stored in myWithVehicles2Integrate
125      * After movements are executed the vehicles in myWithVehicles2Integrate are
126      * put onto their new lanes
127      * This method also updates the "active" status of lanes
128      *
129      * @see MSLane::executeMovements
130      * @see MSLane::integrateNewVehicle
131      */
132     void executeMovements(SUMOTime t);
133 
needsVehicleIntegration(MSLane * const l)134     void needsVehicleIntegration(MSLane* const l) {
135         myWithVehicles2Integrate.push_back(l);
136     }
137     /// @}
138 
139 
140     /** @brief Moves (precomputes) critical vehicles
141      *
142      * Calls "changeLanes" of each of the multi-lane edges. Check then for this
143      *  edge whether a lane got active, adding it to "myActiveLanes" and marking
144      *  it as active in such cases.
145      *
146      * @see MSEdge::changeLanes
147      */
148     void changeLanes(const SUMOTime t);
149 
150 
151     /** @brief Detect collisions
152      *
153      * Calls "detectCollisions" of each lane.
154      * Shouldn't be necessary if model-implementation is correct.
155      * The parameter is simply passed to the lane-instance for reporting.
156      *
157      * @param[in] timestep The current time step
158      * @param[in] stage The current stage within the simulation step
159      * @note see MSNet::simulationStep
160      */
161     void detectCollisions(SUMOTime timestep, const std::string& stage);
162 
163 
164     /** @brief Returns loaded edges
165      *
166      * @return the container storing one-lane edges
167      * @todo Check: Is this secure?
168      */
getEdges()169     const MSEdgeVector& getEdges() const {
170         return myEdges;
171     }
172 
173 
174     /** @brief Informs the control that the given lane got active
175      *
176      * @param[in] l The activated lane
177      * @todo Check for l==0?
178      */
179     void gotActive(MSLane* l);
180 
181     /// @brief trigger collision checking for inactive lane
182     void checkCollisionForInactive(MSLane* l);
183 
184     /// @brief apply additional restrictions
185     void setAdditionalRestrictions();
186 
187 
188 public:
189     /**
190      * @struct LaneUsage
191      * @brief A structure holding some basic information about a simulated lane
192      *
193      * To fasten up speed, this structure holds the number of vehicles using
194      *  a lane and the lane's neighbours. Only lanes that are occupied are
195      *  forced to compute the vehicles longitunidal movement.
196      *
197      * The information about a lane's neighbours speed up the computation
198      *  of the lane changing.
199      */
200     struct LaneUsage {
201         /// @brief The described lane
202         MSLane* lane;
203         /// @brief Information whether this lane is active
204         bool amActive;
205         /// @brief Information whether this lane belongs to a multi-lane edge
206         bool haveNeighbors;
207     };
208 
209 private:
210     /// @brief Loaded edges
211     MSEdgeVector myEdges;
212 
213     /// @brief Definition of a container about a lane's number of vehicles and neighbors
214     typedef std::vector<LaneUsage> LaneUsageVector;
215 
216     /// @brief Information about lanes' number of vehicles and neighbors
217     LaneUsageVector myLanes;
218 
219     /// @brief The list of active (not empty) lanes
220     std::list<MSLane*> myActiveLanes;
221 
222     /// @brief A storage for lanes which shall be integrated because vehicles have moved onto them
223     FXSynchQue<MSLane*, std::vector<MSLane*> > myWithVehicles2Integrate;
224 
225     /// @brief Lanes which changed the state without informing the control
226     std::set<MSLane*, ComparatorNumericalIdLess> myChangedStateLanes;
227 
228     /// @brief The list of active (not empty) lanes
229     std::vector<SUMOTime> myLastLaneChange;
230 
231     /// @brief Additional lanes for which collision checking must be performed
232     std::set<MSLane*, ComparatorNumericalIdLess> myInactiveCheckCollisions;
233 
234 #ifdef HAVE_FOX
235     FXWorkerThread::Pool myThreadPool;
236 #endif
237 
238     std::priority_queue<std::pair<int, int> > myRNGLoad;
239 
240 private:
241     /// @brief Copy constructor.
242     MSEdgeControl(const MSEdgeControl&);
243 
244     /// @brief Assignment operator.
245     MSEdgeControl& operator=(const MSEdgeControl&);
246 
247 };
248 
249 
250 #endif
251 
252 /****************************************************************************/
253 
254