Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MSLaneChanger.h
15 : /// @author Christian Roessel
16 : /// @author Daniel Krajzewicz
17 : /// @author Michael Behrisch
18 : /// @author Jakob Erdmann
19 : /// @date Fri, 01 Feb 2002
20 : ///
21 : // Performs lane changing of vehicles
22 : /****************************************************************************/
23 : #pragma once
24 : #include <config.h>
25 :
26 : #include "MSLane.h"
27 : #include "MSEdge.h"
28 : #include "MSVehicle.h"
29 : #include <vector>
30 : #include <utils/iodevices/OutputDevice.h>
31 :
32 :
33 : // ===========================================================================
34 : // class declarations
35 : // ===========================================================================
36 :
37 :
38 : // ===========================================================================
39 : // class definitions
40 : // ===========================================================================
41 : /**
42 : * @class MSLaneChanger
43 : * @brief Performs lane changing of vehicles
44 : */
45 : class MSLaneChanger {
46 : public:
47 : /// Constructor
48 : MSLaneChanger(const std::vector<MSLane*>* lanes, bool allowChanging);
49 :
50 : /// Destructor.
51 : virtual ~MSLaneChanger();
52 :
53 : /// Start lane-change-process for all vehicles on the edge'e lanes.
54 : void laneChange(SUMOTime t);
55 :
56 : public:
57 : /** Structure used for lane-change. For every lane you have to
58 : know four vehicles, the change-candidate veh and its follower
59 : and leader. Further, information about the last vehicle that changed
60 : into this lane is needed */
61 : struct ChangeElem {
62 :
63 : ChangeElem(MSLane* _lane);
64 :
65 : /// @brief Register that vehicle belongs to Changer Item to after LC decisions
66 : void registerHop(MSVehicle* vehicle);
67 :
68 : ///@brief the leader vehicle for the current change candidate
69 : MSVehicle* lead;
70 : ///@brief the lane corresponding to this ChangeElem (the current change candidate is on this lane)
71 : MSLane* lane;
72 : ///@brief last vehicle that changed into this lane
73 : MSVehicle* hoppedVeh;
74 : /// @brief the next vehicle downstream of the ego vehicle that is blocked from changing to this lane
75 : MSVehicle* lastBlocked;
76 : /// @brief the farthest downstream vehicle on this edge that is blocked from changing to this lane
77 : MSVehicle* firstBlocked;
78 : /// @brief the next vehicle downstream of the ego vehicle that is stopped (and thus an obstacle)
79 : MSVehicle* lastStopped;
80 :
81 : double dens;
82 :
83 : /// @brief whether changing is possible to either direction
84 : bool mayChangeRight;
85 : bool mayChangeLeft;
86 :
87 : /// relative indices of internal lanes with the same origin lane (siblings)
88 : /// only used for changes on internal edges
89 : std::vector<int> siblings;
90 :
91 : /// @name Members which are used only by MSLaneChangerSublane
92 : /// @{
93 : // the vehicles in front of the current vehicle (only on the current edge, continously updated during change() )
94 : MSLeaderInfo ahead;
95 :
96 : // the vehicles in front of the current vehicle (including those on the next edge, contiously update during change() ))
97 : MSLeaderDistanceInfo aheadNext;
98 :
99 : /// vehicles that cannot be stored in ahead because they are outside the lane bounds
100 : std::vector<MSVehicle*> outsideBounds;
101 : ///@}
102 :
103 : };
104 :
105 : public:
106 : /** @brief The list of changers;
107 : For each lane, a ChangeElem is being build */
108 : typedef std::vector< ChangeElem > Changer;
109 :
110 : /// the iterator moving over the ChangeElems
111 : typedef Changer::iterator ChangerIt;
112 :
113 : /// the iterator moving over the ChangeElems
114 : typedef Changer::const_iterator ConstChangerIt;
115 :
116 : /// @brief return changer (only to be used by MSLaneChangerSublane from another instance)
117 : Changer& getChanger() {
118 : return myChanger;
119 : }
120 :
121 :
122 : protected:
123 : /// Initialize the changer before looping over all vehicles.
124 : virtual void initChanger();
125 :
126 : /** @brief Check if there is a single change-candidate in the changer.
127 : Returns true if there is one. */
128 : bool vehInChanger() const {
129 : // If there is at least one valid vehicle under the veh's in myChanger
130 : // return true.
131 459432863 : for (ConstChangerIt ce = myChanger.begin(); ce != myChanger.end(); ++ce) {
132 311889873 : if (veh(ce) != 0) {
133 : return true;
134 : }
135 : }
136 : return false;
137 : }
138 :
139 : /** Returns the furthes unhandled vehicle on this change-elements lane
140 : or 0 if there is none. */
141 : MSVehicle* veh(ConstChangerIt ce) const {
142 : // If ce has a valid vehicle, return it. Otherwise return 0.
143 4745495647 : if (!ce->lane->myVehicles.empty()) {
144 4521637282 : return ce->lane->myVehicles.back();
145 : } else {
146 : return 0;
147 : }
148 : }
149 :
150 :
151 : /** Find a new candidate and try to change it. */
152 : virtual bool change();
153 :
154 :
155 : /** try changing to the opposite direction edge. */
156 : bool changeOpposite(MSVehicle* vehicle, std::pair<MSVehicle*, double> leader, MSVehicle* lastStopped);
157 :
158 : std::pair<MSVehicle* const, double> getOncomingVehicle(const MSLane* opposite, std::pair<MSVehicle*,
159 : double> neighOncoming, double searchDist, double& vMax, const MSVehicle* overtaken = nullptr,
160 : MSLane::MinorLinkMode mLinkMode = MSLane::MinorLinkMode::FOLLOW_NEVER);
161 :
162 : std::pair<MSVehicle* const, double> getOncomingOppositeVehicle(const MSVehicle* vehicle,
163 : std::pair<MSVehicle*, double> overtaken, double searchDist);
164 :
165 : /** Update changer for vehicles that did not change */
166 : void registerUnchanged(MSVehicle* vehicle);
167 :
168 : /// @brief Take into account traci LC-commands.
169 : /// @note This is currently only used within non-actionsteps.
170 : void checkTraCICommands(MSVehicle* vehicle);
171 :
172 : /// @brief Execute TraCI LC-commands.
173 : /// @note This is currently only used within non-actionsteps for the non-sublane model.
174 : /// @return whether lane was changed
175 : bool applyTraCICommands(MSVehicle* vehicle);
176 :
177 : /** After the possible change, update the changer. */
178 : virtual void updateChanger(bool vehHasChanged);
179 :
180 : /** During lane-change a temporary vehicle container is filled within
181 : the lanes (bad practice to modify foreign members, I know). Swap
182 : this container with the real one. */
183 : void updateLanes(SUMOTime t);
184 :
185 : /** @brief Find current candidate.
186 : If there is none, myChanger.end() is returned. */
187 : ChangerIt findCandidate();
188 :
189 : /* @brief check whether lane changing in the given direction is desirable
190 : * and possible */
191 : int checkChangeWithinEdge(
192 : int laneOffset,
193 : const std::pair<MSVehicle* const, double>& leader,
194 : const std::vector<MSVehicle::LaneQ>& preb) const;
195 :
196 : /* @brief check whether lane changing in the given direction is desirable
197 : * and possible */
198 : int checkChange(
199 : int laneOffset,
200 : const MSLane* targetLane,
201 : const std::pair<MSVehicle* const, double>& leader,
202 : const std::pair<MSVehicle* const, double>& follower,
203 : const std::pair<MSVehicle* const, double>& neighLead,
204 : const std::pair<MSVehicle* const, double>& neighFollow,
205 : const std::vector<MSVehicle::LaneQ>& preb) const;
206 :
207 : /* @brief call lanechange model to check the merits of an opposite-direction
208 : * change and update state accordingly */
209 : virtual bool checkChangeOpposite(
210 : MSVehicle* vehicle,
211 : int laneOffset,
212 : MSLane* targetLane,
213 : const std::pair<MSVehicle* const, double>& leader,
214 : const std::pair<MSVehicle* const, double>& neighLead,
215 : const std::pair<MSVehicle* const, double>& neighFollow,
216 : const std::vector<MSVehicle::LaneQ>& preb);
217 :
218 :
219 : /* @brief start the lane change maneuver (and finish it instantly if gLaneChangeDuration == 0)
220 : * @return False when aborting the change due to being remote controlled*/
221 : bool startChange(MSVehicle* vehicle, ChangerIt& from, int direction);
222 :
223 : /// @brief continue a lane change maneuver and return whether the vehicle has completely moved onto the new lane (used if gLaneChangeDuration > 0)
224 : bool continueChange(MSVehicle* vehicle, ChangerIt& from);
225 :
226 : std::pair<MSVehicle* const, double> getRealFollower(const ChangerIt& target) const;
227 :
228 : std::pair<MSVehicle* const, double> getRealLeader(const ChangerIt& target) const;
229 :
230 : /// @brief whether changing to the lane in the given direction should be considered
231 : bool mayChange(int direction) const;
232 :
233 : /// @brief return the closer follower of ego
234 : static MSVehicle* getCloserFollower(const double maxPos, MSVehicle* follow1, MSVehicle* follow2);
235 :
236 : /** @brief Compute the time and space required for overtaking the given leader
237 : * @param[in] vehicle The vehicle that wants to overtake
238 : * @param[in] leader The vehicle to be overtaken
239 : * @param[in] gap The gap between vehicle and leader
240 : * @param[out] timeToOvertake The time for overtaking
241 : * @param[out] spaceToOvertake The space for overtaking
242 : */
243 : static void computeOvertakingTime(const MSVehicle* vehicle, double vMax, const MSVehicle* leader, double gap, double& timeToOvertake, double& spaceToOvertake);
244 :
245 : /** @brief return leader vehicle that is to be overtaken
246 : * @param[out] maxSpace The maxium space that can be used for the overtaking maneuver (limits speed)
247 : * @param[in] vehicle The vehicle that wants to overtake
248 : * @param[in] leader The vehicle to be overtaken and the gap to this vehicle
249 : * @param[in] maxLookAhead The maximum lookahead distance
250 : *
251 : * This methods calls itself recursively to find the leader of a column of
252 : * vehicles to be overtaken (if there is no sufficient gap for stopping in between)
253 : */
254 : static std::pair<MSVehicle*, double> getColumnleader(double& maxSpace, MSVehicle* vehicle, std::pair<MSVehicle*, double> leader, double maxLookAhead = std::numeric_limits<double>::max());
255 :
256 : /// @brief return the next lane in conts beyond lane or nullptr
257 : static const MSLane* getLaneAfter(const MSLane* lane, const std::vector<MSLane*>& conts, bool allowMinor, bool& contsEnd);
258 :
259 : /// @brief whether vehicle has an opposite-direction stop within relevant range
260 : static bool hasOppositeStop(MSVehicle* vehicle);
261 :
262 : /// @brief decide whether to change (back or forth) for an opposite stop
263 : bool checkOppositeStop(MSVehicle* vehicle, const MSLane* oncomingLane, const MSLane* opposite, std::pair<MSVehicle*, double> leader);
264 :
265 : /** @brief avoid opposite-diretion deadlock when vehicles are stopped on both sides of the road
266 : * The method may call saveBlockerLength to affect vehicle speed in the next step
267 : */
268 : bool avoidDeadlock(MSVehicle* vehicle,
269 : std::pair<MSVehicle*, double> neighLead,
270 : std::pair<MSVehicle*, double> overtaken,
271 : std::pair<MSVehicle*, double> leader);
272 :
273 : /** @brief keep stopping to resolve opposite-diretion deadlock while there is oncoming traffic
274 : * The method may call saveBlockerLength to affect vehicle speed in the next step
275 : */
276 : bool resolveDeadlock(MSVehicle* vehicle,
277 : std::pair<MSVehicle* const, double> leader,
278 : std::pair<MSVehicle*, double> neighLead,
279 : std::pair<MSVehicle*, double> overtaken);
280 :
281 : /// @brief check whether to keep stopping for oncoming vehicles in the deadlock zone
282 : bool yieldToDeadlockOncoming(const MSVehicle* vehicle, const MSVehicle* stoppedNeigh, double dist);
283 :
284 : /// @brief check whether to yield for oncoming vehicles that have waited longer for opposite overtaking
285 : bool yieldToOppositeWaiting(const MSVehicle* vehicle, const MSVehicle* stoppedNeigh, double dist, SUMOTime deltaWait = 0);
286 :
287 : /// @brief determine for how long the vehicle can drive safely on the opposite side
288 : double computeSafeOppositeLength(MSVehicle* vehicle, double oppositeLength, const MSLane* source, double usableDist,
289 : std::pair<MSVehicle*, double> oncoming, double vMax, double oncomingSpeed,
290 : std::pair<MSVehicle*, double> neighLead,
291 : std::pair<MSVehicle*, double> overtaken,
292 : std::pair<MSVehicle*, double> neighFollow,
293 : double surplusGap, const MSLane* opposite,
294 : bool canOvertake);
295 :
296 : // @brief compute distance that can safely be driven on the opposite side
297 : static double computeSurplusGap(const MSVehicle* vehicle, const MSLane* opposite, std::pair<MSVehicle*, double> oncoming, double timeToOvertake,
298 : double spaceToOvertake, double& oncomingSpeed, bool oncomingOpposite = false);
299 :
300 : // @brief find hilltop within searchDistance
301 : static bool foundHilltop(MSVehicle* vehicle, bool foundHill, double searchDist, const std::vector<MSLane*>& bestLanes, int view, double pos, double lastMax, double hilltopThreshold);
302 :
303 : /// @brief add LaneQ for opposite lanes
304 : static std::vector<MSVehicle::LaneQ> getBestLanesOpposite(MSVehicle* vehicle, const MSLane* stopLane, double oppositeLength);
305 :
306 : /// @brief compute maximum maneuver speed
307 : static double getMaxOvertakingSpeed(const MSVehicle* vehicle, double maxSpaceToOvertake);
308 :
309 : protected:
310 : /// Container for ChangeElemements, one for every lane in the edge.
311 : Changer myChanger;
312 :
313 : /** Change-candidate. Last of the vehicles in changer. Only this one
314 : will try to change. Every vehicle on the edge will be a candidate
315 : once in the change-process. */
316 : ChangerIt myCandi;
317 :
318 : /* @brief Whether vehicles may start to change lanes on this edge
319 : * (finishing a change in progress is always permitted) */
320 : const bool myAllowsChanging;
321 :
322 : /// @brief whether this edge allows changing to the opposite direction edge
323 : const bool myChangeToOpposite;
324 :
325 : private:
326 : /// Default constructor.
327 : MSLaneChanger();
328 :
329 : /// Copy constructor.
330 : MSLaneChanger(const MSLaneChanger&);
331 :
332 : /// Assignment operator.
333 : MSLaneChanger& operator=(const MSLaneChanger&);
334 : };
|