Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 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 MSLCM_LC2013.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Friedemann Wesner
18 : /// @author Sascha Krieg
19 : /// @author Michael Behrisch
20 : /// @author Laura Bieker
21 : /// @author Leonhard Luecken
22 : /// @date Fri, 08.10.2013
23 : ///
24 : // A lane change model developed by J. Erdmann
25 : // based on the model of D. Krajzewicz developed between 2004 and 2011 (MSLCM_DK2004)
26 : /****************************************************************************/
27 : #include <config.h>
28 :
29 : #include <iostream>
30 : #include <utils/xml/SUMOSAXAttributes.h>
31 : #include <utils/common/RandHelper.h>
32 : #include <utils/common/StringUtils.h>
33 : #include <microsim/transportables/MSPModel.h>
34 : #include <microsim/transportables/MSTransportableControl.h>
35 : #include <microsim/MSEdge.h>
36 : #include <microsim/MSLane.h>
37 : #include <microsim/MSLink.h>
38 : #include <microsim/MSDriverState.h>
39 : #include <microsim/MSNet.h>
40 : #include <microsim/MSStop.h>
41 : #include "MSLCHelper.h"
42 : #include "MSLCM_LC2013.h"
43 :
44 :
45 : // ===========================================================================
46 : // variable definitions
47 : // ===========================================================================
48 : #define LOOK_FORWARD 10.
49 :
50 : #define JAM_FACTOR 1.
51 :
52 : #define LCA_RIGHT_IMPATIENCE -1.
53 : #define CUT_IN_LEFT_SPEED_THRESHOLD 27.
54 :
55 : #define LOOK_AHEAD_MIN_SPEED 0.0
56 : #define LOOK_AHEAD_SPEED_MEMORY 0.9
57 :
58 : #define HELP_DECEL_FACTOR 1.0
59 :
60 : #define HELP_OVERTAKE (10.0 / 3.6)
61 : #define MIN_FALLBEHIND (7.0 / 3.6)
62 :
63 : #define RELGAIN_NORMALIZATION_MIN_SPEED 10.0
64 : #define URGENCY 2.0
65 : #define OPPOSITE_URGENCY 5.0
66 :
67 : #define KEEP_RIGHT_TIME 5.0 // the number of seconds after which a vehicle should move to the right lane
68 :
69 : #define KEEP_RIGHT_HEADWAY 2.0
70 : #define MAX_ONRAMP_LENGTH 200.
71 : #define TURN_LANE_DIST 200.0 // the distance at which a lane leading elsewhere is considered to be a turn-lane that must be avoided
72 :
73 : #define LC_RESOLUTION_SPEED_LAT 0.5 // the lateral speed (in m/s) for a standing vehicle which was unable to finish a continuous LC in time (in case mySpeedLatStanding==0), see #3771
74 :
75 : #define REACT_TO_STOPPED_DISTANCE 100
76 : #define BLOCKER_IS_BLOCKED_TIME_THRESHOLD 5 // the time after which a blocking neighbor is treated similar to a stopped vehicle
77 :
78 : #define HYST_PRECISION 10000000
79 :
80 : // ===========================================================================
81 : // debug defines
82 : // ===========================================================================
83 : //#define DEBUG_CONSTRUCTOR
84 : //#define DEBUG_PATCH_SPEED
85 : //#define DEBUG_INFORMED
86 : //#define DEBUG_INFORMER
87 : //#define DEBUG_WANTS_CHANGE
88 : //#define DEBUG_SLOW_DOWN
89 : //#define DEBUG_COOPERATE
90 : //#define DEBUG_SAVE_BLOCKER_LENGTH
91 :
92 : //#define DEBUG_COND (myVehicle.getID() == "ego")
93 : #define DEBUG_COND (myVehicle.isSelected())
94 : //#define DEBUG_COND (true)
95 :
96 : // ===========================================================================
97 : // member method definitions
98 : // ===========================================================================
99 3708311 : MSLCM_LC2013::MSLCM_LC2013(MSVehicle& v) :
100 : MSAbstractLaneChangeModel(v, LaneChangeModel::LC2013),
101 3708311 : mySpeedGainProbabilityLeft(0),
102 3708311 : mySpeedGainProbabilityRight(0),
103 3708311 : myKeepRightProbability(0),
104 3708311 : myLeadingBlockerLength(0),
105 3708311 : myLeftSpace(0),
106 3708311 : myLookAheadSpeed(LOOK_AHEAD_MIN_SPEED),
107 3708311 : myDontBrake(false),
108 3708311 : myStrategicParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_STRATEGIC_PARAM, 1)),
109 3708311 : myCooperativeParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_PARAM, 1)),
110 3708311 : mySpeedGainParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_PARAM, 1)),
111 3708311 : myKeepRightParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_KEEPRIGHT_PARAM, 1)),
112 3708311 : myOppositeParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_OPPOSITE_PARAM, 1)),
113 3708311 : myLookaheadLeft(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_LOOKAHEADLEFT, 2.0)),
114 3708311 : mySpeedGainRight(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAINRIGHT, 0.1)),
115 3708311 : mySpeedGainLookahead(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD, 0)),
116 3708311 : mySpeedGainRemainTime(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME, 20)),
117 3708311 : mySpeedGainUrgency(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_URGENCY, 50)),
118 3708311 : myRoundaboutBonus(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_ROUNDABOUT, myCooperativeParam)),
119 3708311 : myCooperativeSpeed(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_SPEED, myCooperativeParam)),
120 3708311 : myKeepRightAcceptanceTime(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_KEEPRIGHT_ACCEPTANCE_TIME, -1)),
121 3708311 : myOvertakeDeltaSpeedFactor(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_OVERTAKE_DELTASPEED_FACTOR, 0)),
122 7416622 : myExperimentalParam1(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_EXPERIMENTAL1, 0)) {
123 3708311 : initDerivedParameters();
124 : #ifdef DEBUG_CONSTRUCTOR
125 : if (DEBUG_COND) {
126 : std::cout << SIMTIME
127 : << " create lcModel veh=" << myVehicle.getID()
128 : << " lcStrategic=" << myStrategicParam
129 : << " lcCooperative=" << myCooperativeParam
130 : << " lcSpeedGain=" << mySpeedGainParam
131 : << " lcKeepRight=" << myKeepRightParam
132 : << "\n";
133 : }
134 : #endif
135 3708311 : }
136 :
137 7416476 : MSLCM_LC2013::~MSLCM_LC2013() {
138 3708238 : changed();
139 7416476 : }
140 :
141 :
142 : void
143 3717413 : MSLCM_LC2013::initDerivedParameters() {
144 3717413 : if (mySpeedGainParam <= 0) {
145 7096 : myChangeProbThresholdRight = std::numeric_limits<long long int>::max();
146 7096 : myChangeProbThresholdLeft = std::numeric_limits<long long int>::max();
147 : } else {
148 3710317 : myChangeProbThresholdRight = (long long int)((0.2 / mySpeedGainRight) / mySpeedGainParam * HYST_PRECISION);
149 3710317 : myChangeProbThresholdLeft = (long long int)(0.2 / mySpeedGainParam * HYST_PRECISION);
150 : }
151 3717413 : }
152 :
153 :
154 : bool
155 8809 : MSLCM_LC2013::debugVehicle() const {
156 8809 : return DEBUG_COND;
157 : }
158 :
159 :
160 : int
161 241482062 : MSLCM_LC2013::wantsChange(
162 : int laneOffset,
163 : MSAbstractLaneChangeModel::MSLCMessager& msgPass,
164 : int blocked,
165 : const std::pair<MSVehicle*, double>& leader,
166 : const std::pair<MSVehicle*, double>& follower,
167 : const std::pair<MSVehicle*, double>& neighLead,
168 : const std::pair<MSVehicle*, double>& neighFollow,
169 : const MSLane& neighLane,
170 : const std::vector<MSVehicle::LaneQ>& preb,
171 : MSVehicle** lastBlocked,
172 : MSVehicle** firstBlocked) {
173 :
174 : #ifdef DEBUG_WANTS_CHANGE
175 : if (DEBUG_COND) {
176 : std::cout << "\nWANTS_CHANGE\n" << SIMTIME
177 : << std::setprecision(gPrecision)
178 : << " veh=" << myVehicle.getID()
179 : << " lane=" << myVehicle.getLane()->getID()
180 : << " pos=" << myVehicle.getPositionOnLane()
181 : << " posLat=" << myVehicle.getLateralPositionOnLane()
182 : << " speed=" << myVehicle.getSpeed()
183 : << " considerChangeTo=" << (laneOffset == -1 ? "right" : "left")
184 : << "\n";
185 : }
186 : #endif
187 :
188 241482062 : const int result = _wantsChange(laneOffset, msgPass, blocked, leader, follower, neighLead, neighFollow, neighLane, preb, *lastBlocked, *firstBlocked);
189 :
190 : #ifdef DEBUG_WANTS_CHANGE
191 : if (DEBUG_COND) {
192 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " result=" << toString((LaneChangeAction)result) << " blocked=" << toString((LaneChangeAction)blocked) << "\n\n\n";
193 : }
194 : #endif
195 :
196 241482062 : return result;
197 : }
198 :
199 :
200 : double
201 549334601 : MSLCM_LC2013::patchSpeed(const double min, const double wanted, const double max, const MSCFModel& cfModel) {
202 :
203 : #ifdef DEBUG_PATCH_SPEED
204 : if (DEBUG_COND) {
205 : std::cout << "\nPATCH_SPEED\n"
206 : << SIMTIME
207 : << " veh=" << myVehicle.getID()
208 : << " lane=" << myVehicle.getLane()->getID()
209 : << " pos=" << myVehicle.getPositionOnLane()
210 : << " v=" << myVehicle.getSpeed()
211 : << " min=" << min
212 : << " wanted=" << wanted
213 : << " max=" << max
214 : << "\n";
215 : }
216 : #endif
217 :
218 : // negative min speed may be passed when using ballistic updated
219 549334601 : const double newSpeed = _patchSpeed(MAX2(min, 0.0), wanted, max, cfModel);
220 :
221 : #ifdef DEBUG_PATCH_SPEED
222 : if (DEBUG_COND) {
223 : const std::string patched = (wanted != newSpeed ? " patched=" + toString(newSpeed) : "");
224 : std::cout << patched
225 : << "\n";
226 : }
227 : #endif
228 :
229 549334601 : return newSpeed;
230 : }
231 :
232 :
233 : double
234 549334601 : MSLCM_LC2013::_patchSpeed(double min, const double wanted, double max, const MSCFModel& cfModel) {
235 549334601 : int state = myOwnState;
236 : #ifdef DEBUG_PATCH_SPEED
237 : if (DEBUG_COND) {
238 : std::cout
239 : << "\n" << SIMTIME << std::setprecision(gPrecision)
240 : << " patchSpeed state=" << toString((LaneChangeAction)state) << " myLCAccelerationAdvices=" << toString(myLCAccelerationAdvices)
241 : << "\n speed=" << myVehicle.getSpeed() << " min=" << min << " wanted=" << wanted
242 : << "\n myLeadingBlockerLength=" << myLeadingBlockerLength
243 : << "\n";
244 : }
245 : #endif
246 :
247 : // letting vehicles merge in at the end of the lane in case of counter-lane change, step#2
248 : double nVSafe = wanted;
249 : bool gotOne = false;
250 : // if we want to change and have a blocking leader and there is enough room for him in front of us
251 549334601 : if (myLeadingBlockerLength != 0) {
252 543263 : double space = myLeftSpace - myLeadingBlockerLength - POSITION_EPS;
253 : #ifdef DEBUG_PATCH_SPEED
254 : if (DEBUG_COND) {
255 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " myLeftSpace=" << myLeftSpace << " myLeadingBlockerLength=" << myLeadingBlockerLength << " space=" << space << "\n";
256 : }
257 : #endif
258 543263 : if (space > 0 && (myVehicle.getLane()->isNormal() || myVehicle.getCurrentEdge()->isRoundabout())) {
259 : // compute speed for decelerating towards a place which allows the blocking leader to merge in in front
260 408667 : const double vMinEmergency = myVehicle.getCarFollowModel().minNextSpeedEmergency(myVehicle.getSpeed(), &myVehicle);
261 408667 : double safe = cfModel.stopSpeed(&myVehicle, myVehicle.getSpeed(), space, MSCFModel::CalcReason::LANE_CHANGE);
262 : max = MIN2(max, MAX2(safe, vMinEmergency));
263 : // if we are approaching this place
264 408667 : if (safe < wanted) {
265 : // return this speed as the speed to use
266 47210 : if (safe < min) {
267 4754 : if (safe >= vMinEmergency) {
268 : // permit harder braking if needed and helpful
269 : min = MAX2(vMinEmergency, safe);
270 : }
271 : }
272 : #ifdef DEBUG_PATCH_SPEED
273 : if (DEBUG_COND) {
274 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " slowing down for leading blocker, safe=" << safe << (safe + NUMERICAL_EPS < min ? " (not enough)" : "") << "\n";
275 : }
276 : #endif
277 : nVSafe = MAX2(min, safe);
278 : gotOne = true;
279 : }
280 : }
281 : }
282 :
283 549334601 : const double coopWeight = MAX2(0.0, MIN2(1.0, myCooperativeSpeed));
284 572216517 : for (auto i : myLCAccelerationAdvices) {
285 : double a = i.first;
286 22881916 : double v = myVehicle.getSpeed() + ACCEL2SPEED(a);
287 :
288 22881916 : if (v >= min && v <= max && (MSGlobals::gSemiImplicitEulerUpdate
289 : // ballistic update: (negative speeds may appear, e.g. min<0, v<0), BUT:
290 : // XXX: LaneChanging returns -1 to indicate no restrictions, which leads to probs here (Leo), refs. #2577
291 : // As a quick fix, we just dismiss cases where v=-1
292 : // VERY rarely (whenever a requested help-acceleration is really indicated by v=-1)
293 : // this can lead to failing lane-change attempts, though)
294 249214 : || v != -1)) {
295 5504549 : if (i.second & LCA_CHANGE_TO_HELP) {
296 2216741 : nVSafe = MIN2(v * coopWeight + (1 - coopWeight) * wanted, nVSafe);
297 : } else {
298 : // own advice, no scaling needed
299 : nVSafe = MIN2(v, nVSafe);
300 : }
301 : gotOne = true;
302 : #ifdef DEBUG_PATCH_SPEED
303 : if (DEBUG_COND) {
304 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " got nVSafe=" << nVSafe << " isOwn: " << i.second << " rawV=" << v << "\n";
305 : }
306 : #endif
307 : } else {
308 : if (v < min) {
309 : #ifdef DEBUG_PATCH_SPEED
310 : if (DEBUG_COND) {
311 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " ignoring low nVSafe=" << v << " min=" << min << "\n";
312 : }
313 : #endif
314 : } else {
315 : #ifdef DEBUG_PATCH_SPEED
316 : if (DEBUG_COND) {
317 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " ignoring high nVSafe=" << v << " max=" << max << "\n";
318 : }
319 : #endif
320 : }
321 : }
322 : }
323 : // myDontBrake is used in counter-lane-change situations with relief connection
324 549334601 : if (gotOne && !myDontBrake) {
325 : #ifdef DEBUG_PATCH_SPEED
326 : if (DEBUG_COND) {
327 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " got vSafe\n";
328 : }
329 : #endif
330 : return nVSafe;
331 : }
332 :
333 : // check whether the vehicle is blocked
334 544657755 : if ((state & LCA_WANTS_LANECHANGE) != 0 && (state & LCA_BLOCKED) != 0) {
335 13408210 : if ((state & LCA_STRATEGIC) != 0) {
336 : // necessary decelerations are controlled via vSafe. If there are
337 : // none it means we should speed up
338 : #ifdef DEBUG_PATCH_SPEED
339 : if (DEBUG_COND) {
340 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_WANTS_LANECHANGE (strat, no vSafe)\n";
341 : }
342 : #endif
343 2868102 : return (max + wanted) / 2.0;
344 10540108 : } else if ((state & LCA_COOPERATIVE) != 0) {
345 : // only minor adjustments in speed should be done
346 754007 : if ((state & LCA_BLOCKED_BY_LEADER) != 0) {
347 : #ifdef DEBUG_PATCH_SPEED
348 : if (DEBUG_COND) {
349 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_LEADER (coop)\n";
350 : }
351 : #endif
352 612789 : if (wanted >= 0.) {
353 612789 : return (MAX2(0., min) + wanted) / 2.0;
354 : } else {
355 : return wanted;
356 : }
357 : }
358 141218 : if ((state & LCA_BLOCKED_BY_FOLLOWER) != 0) {
359 : #ifdef DEBUG_PATCH_SPEED
360 : if (DEBUG_COND) {
361 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_FOLLOWER (coop)\n";
362 : }
363 : #endif
364 141218 : return (max + wanted) / 2.0;
365 : }
366 : //} else { // VARIANT_16
367 : // // only accelerations should be performed
368 : // if ((state & LCA_BLOCKED_BY_FOLLOWER) != 0) {
369 : // if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_FOLLOWER\n";
370 : // return (max + wanted) / 2.0;
371 : // }
372 : }
373 : }
374 :
375 : /*
376 : // decelerate if being a blocking follower
377 : // (and does not have to change lanes)
378 : if ((state & LCA_AMBLOCKINGFOLLOWER) != 0) {
379 : if (fabs(max - myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle)) < 0.001 && min == 0) { // !!! was standing
380 : if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER (standing)\n";
381 : return 0;
382 : }
383 : if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER\n";
384 :
385 : //return min; // VARIANT_3 (brakeStrong)
386 : return (min + wanted) / 2.0;
387 : }
388 : if ((state & LCA_AMBACKBLOCKER) != 0) {
389 : if (max <= myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle) && min == 0) { // !!! was standing
390 : if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBACKBLOCKER (standing)\n";
391 : //return min; VARIANT_9 (backBlockVSafe)
392 : return nVSafe;
393 : }
394 : }
395 : if ((state & LCA_AMBACKBLOCKER_STANDING) != 0) {
396 : if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBACKBLOCKER_STANDING\n";
397 : //return min;
398 : return nVSafe;
399 : }
400 : */
401 :
402 : // accelerate if being a blocking leader or blocking follower not able to brake
403 : // (and does not have to change lanes)
404 541035646 : if ((state & LCA_AMBLOCKINGLEADER) != 0 && myCooperativeSpeed >= 0) {
405 : #ifdef DEBUG_PATCH_SPEED
406 : if (DEBUG_COND) {
407 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGLEADER\n";
408 : }
409 : #endif
410 1523378 : return (max + wanted) / 2.0;
411 : }
412 :
413 : if ((state & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0) {
414 : #ifdef DEBUG_PATCH_SPEED
415 : if (DEBUG_COND) {
416 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER_DONTBRAKE\n";
417 : }
418 : #endif
419 : /*
420 : // VARIANT_4 (dontbrake)
421 : if (max <= myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle) && min == 0) { // !!! was standing
422 : return wanted;
423 : }
424 : return (min + wanted) / 2.0;
425 : */
426 : }
427 539512268 : if (!myVehicle.getLane()->getEdge().hasLaneChanger()) {
428 : // remove chaning information if on a road with a single lane
429 302140308 : changed();
430 : }
431 : return wanted;
432 : }
433 :
434 :
435 : void*
436 4553171 : MSLCM_LC2013::inform(void* info, MSVehicle* sender) {
437 : UNUSED_PARAMETER(sender);
438 : Info* pinfo = (Info*)info;
439 : assert(pinfo->first >= 0 || !MSGlobals::gSemiImplicitEulerUpdate);
440 4553171 : addLCSpeedAdvice(pinfo->first, LCA_CHANGE_TO_HELP);
441 4553171 : myOwnState |= pinfo->second;
442 : #ifdef DEBUG_INFORMED
443 : if (DEBUG_COND) {
444 : std::cout << SIMTIME
445 : << " veh=" << myVehicle.getID()
446 : << " informedBy=" << sender->getID()
447 : << " info=" << pinfo->second
448 : << " vSafe=" << pinfo->first
449 : << "\n";
450 : }
451 : #endif
452 4553171 : delete pinfo;
453 4553171 : return (void*) true;
454 : }
455 :
456 : double
457 4507148 : MSLCM_LC2013::overtakeDistance(const MSVehicle* follower, const MSVehicle* leader, const double gap, double followerSpeed, double leaderSpeed) {
458 4507148 : followerSpeed = followerSpeed == INVALID_SPEED ? follower->getSpeed() : followerSpeed;
459 4507148 : leaderSpeed = leaderSpeed == INVALID_SPEED ? leader->getSpeed() : leaderSpeed;
460 : double overtakeDist = (gap // drive to back of leader
461 4507148 : + leader->getVehicleType().getLengthWithGap() // drive to front of leader
462 4507148 : + follower->getVehicleType().getLength() // follower back reaches leader front
463 4507148 : + leader->getCarFollowModel().getSecureGap( // save gap to leader
464 4507148 : leader, follower, leaderSpeed, followerSpeed, follower->getCarFollowModel().getMaxDecel()));
465 4507148 : return MAX2(overtakeDist, 0.);
466 : }
467 :
468 :
469 : double
470 4663519 : MSLCM_LC2013::informLeader(MSAbstractLaneChangeModel::MSLCMessager& msgPass,
471 : int blocked,
472 : int dir,
473 : const std::pair<MSVehicle*, double>& neighLead,
474 : double remainingSeconds) {
475 4663519 : double plannedSpeed = myVehicle.getSpeed();
476 4663519 : if (!isOpposite()) {
477 4568471 : plannedSpeed = MIN2(plannedSpeed,
478 4568471 : myVehicle.getCarFollowModel().stopSpeed(&myVehicle, myVehicle.getSpeed(), myLeftSpace - myLeadingBlockerLength));
479 : }
480 5405269 : for (auto i : myLCAccelerationAdvices) {
481 : const double a = i.first;
482 741750 : if (a >= -myVehicle.getCarFollowModel().getMaxDecel()) {
483 741730 : plannedSpeed = MIN2(plannedSpeed, myVehicle.getSpeed() + ACCEL2SPEED(a));
484 : }
485 : }
486 : #ifdef DEBUG_INFORMER
487 : if (DEBUG_COND) {
488 : std::cout << "\nINFORM_LEADER"
489 : << "\nspeed=" << myVehicle.getSpeed() << " planned=" << plannedSpeed << "\n";
490 : }
491 : #endif
492 :
493 4663519 : const MSVehicle* const nv = neighLead.first;
494 4663519 : if (nv == nullptr) {
495 : // not overtaking
496 : return plannedSpeed;
497 : }
498 9207561 : const double neighNextSpeed = MIN2(nv->getSpeed() - ACCEL2SPEED(MAX2(1.0, -nv->getAcceleration())),
499 : // assume that we know when a neighboring vehicle intends to stop
500 8845522 : nv->nextStopDist() <= nv->getLane()->getLength() ? nv->getCarFollowModel().minNextSpeed(nv->getSpeed(), nv) : nv->getSpeed());
501 : double neighNextGap;
502 4422761 : if (MSGlobals::gSemiImplicitEulerUpdate) {
503 4193797 : neighNextGap = neighLead.second + SPEED2DIST(neighNextSpeed - plannedSpeed);
504 : } else {
505 228964 : neighNextGap = neighLead.second + SPEED2DIST((nv->getSpeed() + neighNextSpeed) / 2) - SPEED2DIST((myVehicle.getSpeed() + plannedSpeed) / 2);
506 : }
507 4422761 : if ((blocked & LCA_BLOCKED_BY_LEADER) != 0) {
508 3555722 : if (MSLCHelper::divergentRoute(myVehicle, *nv)) {
509 : //std::cout << SIMTIME << " ego=" << myVehicle.getID() << " ignoresDivergentBlockingLeader=" << nv->getID() << "\n";
510 : return plannedSpeed;
511 : }
512 : #ifdef DEBUG_INFORMER
513 : if (DEBUG_COND) {
514 : std::cout << " blocked by leader nv=" << nv->getID() << " nvSpeed=" << nv->getSpeed() << " needGap="
515 : << myVehicle.getCarFollowModel().getSecureGap(&myVehicle, nv, myVehicle.getSpeed(), nv->getSpeed(), nv->getCarFollowModel().getMaxDecel()) << "\n";
516 : }
517 : #endif
518 : // decide whether we want to overtake the leader or follow it
519 : double overtakeTime;
520 3551690 : const double overtakeDist = overtakeDistance(&myVehicle, nv, neighLead.second);
521 3551690 : const double dv = plannedSpeed - nv->getSpeed();
522 :
523 3551690 : if (dv > myOvertakeDeltaSpeedFactor * myVehicle.getLane()->getSpeedLimit()) {
524 1509228 : overtakeTime = overtakeDist / dv;
525 : } else if (nv->getWaitingSeconds() > BLOCKER_IS_BLOCKED_TIME_THRESHOLD
526 1157063 : && !isOpposite()
527 3173290 : && (myVehicle.getVehicleType().getLengthWithGap() + nv->getVehicleType().getLengthWithGap()) <= myLeftSpace) {
528 : // -> set overtakeTime to indicate possibility of overtaking (only if there is enough space)
529 986684 : overtakeTime = remainingSeconds - 1;
530 : } else {
531 : // -> set overtakeTime to something indicating impossibility of overtaking
532 1055778 : overtakeTime = remainingSeconds + 1;
533 : }
534 :
535 : #ifdef DEBUG_INFORMER
536 : if (DEBUG_COND) {
537 : std::cout << SIMTIME << " informLeader() of " << myVehicle.getID()
538 : << "\nnv = " << nv->getID()
539 : << "\nplannedSpeed = " << plannedSpeed
540 : << "\nleaderSpeed = " << nv->getSpeed()
541 : << "\nmyLeftSpace = " << myLeftSpace
542 : << "\nremainingSeconds = " << remainingSeconds
543 : << "\novertakeDist = " << overtakeDist
544 : << "\novertakeTime = " << overtakeTime
545 : << std::endl;
546 : }
547 : #endif
548 :
549 3551690 : if ((dv < myOvertakeDeltaSpeedFactor * myVehicle.getLane()->getSpeedLimit()
550 : // overtaking on the right on an uncongested highway is forbidden (noOvertakeLCLeft)
551 2681676 : || (dir == LCA_MLEFT && avoidOvertakeRight(neighLead.first))
552 : // not enough space to overtake?
553 5206138 : || (MSGlobals::gSemiImplicitEulerUpdate && myLeftSpace - myLeadingBlockerLength - myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed()) < overtakeDist)
554 : // using brakeGap() without headway seems adequate in a situation where the obstacle (the lane end) is not moving [XXX implemented in branch ticket860, can be used in general if desired, refs. #2575] (Leo).
555 2499238 : || (!MSGlobals::gSemiImplicitEulerUpdate && myLeftSpace - myLeadingBlockerLength - myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed(), getCarFollowModel().getMaxDecel(), 0.) < overtakeDist)
556 : // not enough time to overtake? (skipped for a stopped leader [currently only for ballistic update XXX: check if appropriate for euler, too, refs. #2575] to ensure that it can be overtaken if only enough space is exists) (Leo)
557 2482131 : || (remainingSeconds < overtakeTime && (MSGlobals::gSemiImplicitEulerUpdate || !nv->isStopped())))
558 : // opposite driving and must overtake
559 4279950 : && (!neighLead.first->isStopped() || (isOpposite() && neighLead.second >= 0))) {
560 : // cannot overtake
561 1595667 : msgPass.informNeighLeader(new Info(std::numeric_limits<double>::max(), dir | LCA_AMBLOCKINGLEADER), &myVehicle);
562 : // slow down smoothly to follow leader
563 : // account for minor decelerations by the leader (dawdling)
564 4787001 : double targetSpeed = MAX3(myVehicle.getCarFollowModel().minNextSpeed(myVehicle.getSpeed(), &myVehicle),
565 1595667 : getCarFollowModel().followSpeed(&myVehicle, myVehicle.getSpeed(), neighNextGap, neighNextSpeed, nv->getCarFollowModel().getMaxDecel()),
566 : // avoid changing on intersection
567 1615198 : (myVehicle.getLane()->isNormal() || myVehicle.getCurrentEdge()->isRoundabout()) ? 0 : ACCEL2SPEED(myVehicle.getCarFollowModel().getMaxAccel()));
568 1595667 : if (targetSpeed < myVehicle.getSpeed()) {
569 : // slow down smoothly to follow leader
570 525989 : const double decel = remainingSeconds == 0. ? myVehicle.getCarFollowModel().getMaxDecel() :
571 525989 : MIN2(myVehicle.getCarFollowModel().getMaxDecel(),
572 525989 : MAX2(MIN_FALLBEHIND, (myVehicle.getSpeed() - targetSpeed) / remainingSeconds));
573 525989 : const double nextSpeed = MIN2(plannedSpeed, MAX2(0.0, myVehicle.getSpeed() - ACCEL2SPEED(decel)));
574 : #ifdef DEBUG_INFORMER
575 : if (DEBUG_COND) {
576 : std::cout << SIMTIME
577 : << " cannot overtake leader nv=" << nv->getID()
578 : << " dv=" << dv
579 : << " myLookAheadSpeed=" << myLookAheadSpeed
580 : << " myLeftSpace=" << myLeftSpace
581 : << " overtakeDist=" << overtakeDist
582 : << " overtakeTime=" << overtakeTime
583 : << " remainingSeconds=" << remainingSeconds
584 : << " currentGap=" << neighLead.second
585 : << " brakeGap=" << myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed(), getCarFollowModel().getMaxDecel(), 0.)
586 : << " neighNextSpeed=" << neighNextSpeed
587 : << " neighNextGap=" << neighNextGap
588 : << " targetSpeed=" << targetSpeed
589 : << " nextSpeed=" << nextSpeed
590 : << "\n";
591 : }
592 : #endif
593 525989 : addLCSpeedAdvice(nextSpeed, dir);
594 525989 : return nextSpeed;
595 : } else {
596 : // leader is fast enough anyway
597 : #ifdef DEBUG_INFORMER
598 : if (DEBUG_COND) {
599 : std::cout << SIMTIME
600 : << " cannot overtake fast leader nv=" << nv->getID()
601 : << " dv=" << dv
602 : << " myLookAheadSpeed=" << myLookAheadSpeed
603 : << " myLeftSpace=" << myLeftSpace
604 : << " overtakeDist=" << overtakeDist
605 : << " myLeadingBlockerLength=" << myLeadingBlockerLength
606 : << " overtakeTime=" << overtakeTime
607 : << " remainingSeconds=" << remainingSeconds
608 : << " currentGap=" << neighLead.second
609 : << " neighNextSpeed=" << neighNextSpeed
610 : << " neighNextGap=" << neighNextGap
611 : << " targetSpeed=" << targetSpeed
612 : << "\n";
613 : }
614 : #endif
615 1069678 : addLCSpeedAdvice(targetSpeed, dir);
616 1069678 : return plannedSpeed;
617 : }
618 : } else {
619 : // overtaking, leader should not accelerate
620 : #ifdef DEBUG_INFORMER
621 : if (DEBUG_COND) {
622 : std::cout << SIMTIME
623 : << " wants to overtake leader nv=" << nv->getID()
624 : << " dv=" << dv
625 : << " overtakeDist=" << overtakeDist
626 : << " remainingSeconds=" << remainingSeconds
627 : << " overtakeTime=" << overtakeTime
628 : << " currentGap=" << neighLead.second
629 : << " secureGap=" << nv->getCarFollowModel().getSecureGap(nv, &myVehicle, nv->getSpeed(), myVehicle.getSpeed(), myVehicle.getCarFollowModel().getMaxDecel())
630 : << "\n";
631 : }
632 : #endif
633 : // no need to pass a message if the neighbor is waiting/stuck anyway (but sending it would risk deadlock)
634 1956023 : if (nv->getWaitingSeconds() <= BLOCKER_IS_BLOCKED_TIME_THRESHOLD) {
635 851247 : msgPass.informNeighLeader(new Info(nv->getSpeed(), dir | LCA_AMBLOCKINGLEADER), &myVehicle);
636 : }
637 1956023 : return -1; // XXX: using -1 is ambiguous for the ballistic update! Currently this is being catched in patchSpeed() (Leo), consider returning INVALID_SPEED, refs. #2577
638 : }
639 : } else { // (remainUnblocked)
640 : // we are not blocked now. make sure we stay far enough from the leader
641 1734078 : const double targetSpeed = MAX2(
642 867039 : myVehicle.getCarFollowModel().minNextSpeed(myVehicle.getSpeed(), &myVehicle),
643 867039 : getCarFollowModel().followSpeed(&myVehicle, myVehicle.getSpeed(), neighNextGap, neighNextSpeed, nv->getCarFollowModel().getMaxDecel()));
644 867039 : addLCSpeedAdvice(targetSpeed, dir);
645 : #ifdef DEBUG_INFORMER
646 : if (DEBUG_COND) {
647 : std::cout << " not blocked by leader nv=" << nv->getID()
648 : << " nvSpeed=" << nv->getSpeed()
649 : << " gap=" << neighLead.second
650 : << " neighNextSpeed=" << neighNextSpeed
651 : << " neighNextGap=" << neighNextGap
652 : << " needGap=" << myVehicle.getCarFollowModel().getSecureGap(&myVehicle, nv, myVehicle.getSpeed(), nv->getSpeed(), nv->getCarFollowModel().getMaxDecel())
653 : << " targetSpeed=" << targetSpeed
654 : << "\n";
655 : }
656 : #endif
657 : return MIN2(targetSpeed, plannedSpeed);
658 : }
659 : }
660 :
661 : void
662 2703410 : MSLCM_LC2013::informFollower(MSAbstractLaneChangeModel::MSLCMessager& msgPass,
663 : int blocked,
664 : int dir,
665 : const std::pair<MSVehicle*, double>& neighFollow,
666 : double remainingSeconds,
667 : double plannedSpeed) {
668 :
669 2703410 : MSVehicle* nv = neighFollow.first;
670 2703410 : const double plannedAccel = SPEED2ACCEL(MAX2(MIN2(getCarFollowModel().getMaxAccel(), plannedSpeed - myVehicle.getSpeed()), -getCarFollowModel().getMaxDecel()));
671 :
672 : #ifdef DEBUG_INFORMER
673 : if (DEBUG_COND) {
674 : std::cout << "\nINFORM_FOLLOWER"
675 : << "\nspeed=" << myVehicle.getSpeed() << " planned=" << plannedSpeed << "\n";
676 : }
677 : #endif
678 :
679 : // decide whether we will request help to cut in before the follower or allow to be overtaken
680 2703410 : if (nv != nullptr && MSLCHelper::unwillingToHelp(myVehicle, plannedSpeed, *nv)) {
681 : // @note: this check needs to come first because even if the follower is not blocking, getSpeedPreservingSecureGap may request a slow-down
682 : #ifdef DEBUG_INFORMER
683 : if (DEBUG_COND) {
684 : std::cout << "\n nv=" << nv->getID() << " not willing to help\n";
685 : }
686 : #endif
687 : return;
688 : }
689 :
690 2665568 : if ((blocked & LCA_BLOCKED_BY_FOLLOWER) != 0 && nv != nullptr) {
691 1547875 : if (MSLCHelper::divergentRoute(myVehicle, *nv)) {
692 : //std::cout << SIMTIME << " ego=" << myVehicle.getID() << " ignoresDivergentBlockingFollower=" << nv->getID() << "\n";
693 : return;
694 : }
695 : #ifdef DEBUG_INFORMER
696 : if (DEBUG_COND) {
697 : std::cout << " blocked by follower nv=" << nv->getID() << " nvSpeed=" << nv->getSpeed() << " needGap="
698 : << nv->getCarFollowModel().getSecureGap(nv, &myVehicle, nv->getSpeed(), myVehicle.getSpeed(), myVehicle.getCarFollowModel().getMaxDecel()) << " planned=" << plannedSpeed << "\n";
699 : }
700 : #endif
701 : // are we fast enough to cut in without any help?
702 1547776 : if (MAX2(plannedSpeed, 0.) - nv->getSpeed() >= HELP_OVERTAKE) {
703 88187 : const double neededGap = nv->getCarFollowModel().getSecureGap(nv, &myVehicle, nv->getSpeed(), plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
704 88187 : if ((neededGap - neighFollow.second) / remainingSeconds < (MAX2(plannedSpeed, 0.) - nv->getSpeed())) {
705 : #ifdef DEBUG_INFORMER
706 : if (DEBUG_COND) {
707 : std::cout << " wants to cut in before nv=" << nv->getID() << " without any help." << "\nneededGap = " << neededGap << "\n";
708 : }
709 : #endif
710 : // follower might even accelerate but not to much
711 : // XXX: I don't understand this. The needed gap was determined for nv->getSpeed(), not for (plannedSpeed - HELP_OVERTAKE)?! (Leo), refs. #2578
712 76693 : msgPass.informNeighFollower(new Info(MAX2(plannedSpeed, 0.) - HELP_OVERTAKE, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
713 76693 : return;
714 : }
715 : }
716 :
717 : // PARAMETERS
718 : // assume other vehicle will assume the equivalent of 1 second of
719 : // maximum deceleration to help us (will probably be spread over
720 : // multiple seconds)
721 : // -----------
722 : const double helpDecel = nv->getCarFollowModel().getMaxDecel() * HELP_DECEL_FACTOR;
723 :
724 : // follower's new speed in next step
725 : double neighNewSpeed;
726 : // follower's new speed after 1s.
727 : double neighNewSpeed1s;
728 : // velocity difference, gap after follower-deceleration
729 : double dv, decelGap;
730 :
731 1471083 : if (MSGlobals::gSemiImplicitEulerUpdate) {
732 : // euler
733 1338802 : neighNewSpeed = MAX2(0., nv->getSpeed() - ACCEL2SPEED(helpDecel));
734 1338802 : neighNewSpeed1s = MAX2(0., nv->getSpeed() - helpDecel); // TODO: consider introduction of a configurable anticipationTime here (see far below in the !blocked part). Refs. #2578
735 : // change in the gap between ego and blocker over 1 second (not STEP!)
736 : // XXX: though here it is calculated as if it were one step!? (Leo) Refs. #2578
737 1338802 : dv = plannedSpeed - neighNewSpeed1s; // XXX: what is this quantity (if TS!=1)?
738 : // new gap between follower and self in case the follower does brake for 1s
739 : // XXX: if the step-length is not 1s., this is not the gap after 1s. deceleration!
740 : // And this formula overestimates the real gap. Isn't that problematic? (Leo)
741 : // Below, it seems that decelGap > secureGap is taken to indicate the possibility
742 : // to cut in within the next time-step. However, this is not the case, if TS<1s.,
743 : // since decelGap is (not exactly, though!) the gap after 1s. Refs. #2578
744 1338802 : decelGap = neighFollow.second + dv;
745 : } else {
746 : // ballistic
747 : // negative newSpeed-extrapolation possible, if stop lies within the next time-step
748 : // XXX: this code should work for the euler case as well, since gapExtrapolation() takes
749 : // care of this, but for TS!=1 we will have different behavior (see previous remark) Refs. #2578
750 132281 : neighNewSpeed = nv->getSpeed() - ACCEL2SPEED(helpDecel);
751 132281 : neighNewSpeed1s = nv->getSpeed() - helpDecel;
752 :
753 132281 : dv = myVehicle.getSpeed() - nv->getSpeed(); // current velocity difference
754 264562 : decelGap = getCarFollowModel().gapExtrapolation(1., neighFollow.second, myVehicle.getSpeed(),
755 132281 : nv->getSpeed(), plannedAccel, -helpDecel, myVehicle.getMaxSpeedOnLane(), nv->getMaxSpeedOnLane());
756 : }
757 :
758 1471083 : const double secureGap = nv->getCarFollowModel().getSecureGap(nv, &myVehicle, MAX2(neighNewSpeed1s, 0.),
759 1471083 : MAX2(plannedSpeed, 0.), myVehicle.getCarFollowModel().getMaxDecel());
760 :
761 1471083 : const double onRampThreshold = myVehicle.getLane()->getSpeedLimit() * 0.8 * myExperimentalParam1 * (1 - myVehicle.getImpatience());
762 :
763 : #ifdef DEBUG_INFORMER
764 : if (DEBUG_COND) {
765 : std::cout << SIMTIME
766 : << " speed=" << myVehicle.getSpeed()
767 : << " plannedSpeed=" << plannedSpeed
768 : << " threshold=" << onRampThreshold
769 : << " neighNewSpeed=" << neighNewSpeed
770 : << " neighNewSpeed1s=" << neighNewSpeed1s
771 : << " dv=" << dv
772 : << " gap=" << neighFollow.second
773 : << " decelGap=" << decelGap
774 : << " secureGap=" << secureGap
775 : << "\n";
776 : }
777 : #endif
778 : // prevent vehicles on an on ramp stopping the main flow
779 : if (dir == LCA_MLEFT
780 593011 : && myVehicle.getLane()->isAccelLane()
781 1538118 : && neighNewSpeed1s < onRampThreshold) {
782 : return;
783 : }
784 :
785 1452163 : if (decelGap > 0 && decelGap >= secureGap) {
786 : // XXX: This does not assure that the leader can cut in in the next step if TS < 1 (see above)
787 : // this seems to be supposed in the following (euler code)...?! (Leo) Refs. #2578
788 :
789 : // if the blocking follower brakes it could help
790 : // how hard does it actually need to be?
791 : // to be safe in the next step the following equation has to hold for the follower's vsafe:
792 : // vsafe <= followSpeed(gap=currentGap - SPEED2DIST(vsafe), ...)
793 : double vsafe, vsafe1;
794 :
795 97785 : if (MSGlobals::gSemiImplicitEulerUpdate) {
796 : // euler
797 : // we compute an upper bound on vsafe by doing the computation twice
798 87942 : vsafe1 = MAX2(neighNewSpeed, nv->getCarFollowModel().followSpeed(
799 87942 : nv, nv->getSpeed(), neighFollow.second + SPEED2DIST(plannedSpeed), plannedSpeed, getCarFollowModel().getMaxDecel()));
800 87942 : vsafe = MAX2(neighNewSpeed, nv->getCarFollowModel().followSpeed(
801 87942 : nv, nv->getSpeed(), neighFollow.second + SPEED2DIST(plannedSpeed - vsafe1), plannedSpeed, getCarFollowModel().getMaxDecel()));
802 : //assert(vsafe <= vsafe1); assertion does not hold for models with randomness in followSpeed (W99)
803 : } else {
804 : // ballistic
805 :
806 : // XXX: This block should actually do as well for euler update (TODO: test!), refs #2575
807 : // we compute an upper bound on vsafe
808 : // next step's gap without help deceleration (nv's speed assumed constant)
809 9843 : double nextGap = getCarFollowModel().gapExtrapolation(TS,
810 9843 : neighFollow.second, myVehicle.getSpeed(),
811 9843 : nv->getSpeed(), plannedAccel, 0,
812 9843 : myVehicle.getMaxSpeedOnLane(), nv->getMaxSpeedOnLane());
813 : #ifdef DEBUG_INFORMER
814 : if (DEBUG_COND) {
815 : std::cout << "nextGap=" << nextGap << " (without help decel) \n";
816 : }
817 : #endif
818 :
819 : // NOTE: the second argument of MIN2() can get larger than nv->getSpeed()
820 19686 : vsafe1 = MIN2(nv->getSpeed(), MAX2(neighNewSpeed,
821 9843 : nv->getCarFollowModel().followSpeed(nv,
822 9843 : nv->getSpeed(), nextGap,
823 : MAX2(0., plannedSpeed),
824 : getCarFollowModel().getMaxDecel())));
825 :
826 :
827 : // next step's gap with possibly less than maximal help deceleration (in case vsafe1 > neighNewSpeed)
828 9843 : double decel2 = SPEED2ACCEL(nv->getSpeed() - vsafe1);
829 9843 : nextGap = getCarFollowModel().gapExtrapolation(TS,
830 9843 : neighFollow.second, myVehicle.getSpeed(),
831 9843 : nv->getSpeed(), plannedAccel, -decel2,
832 9843 : myVehicle.getMaxSpeedOnLane(), nv->getMaxSpeedOnLane());
833 :
834 : // vsafe = MAX(neighNewSpeed, safe speed assuming next_gap)
835 : // Thus, the gap resulting from vsafe is larger or equal to next_gap
836 : // in contrast to the euler case, where nv's follow speed doesn't depend on the actual speed,
837 : // we need to assure, that nv doesn't accelerate
838 19686 : vsafe = MIN2(nv->getSpeed(), MAX2(neighNewSpeed,
839 9843 : nv->getCarFollowModel().followSpeed(nv,
840 9843 : nv->getSpeed(), nextGap,
841 : MAX2(0., plannedSpeed),
842 : getCarFollowModel().getMaxDecel())));
843 :
844 : assert(vsafe >= vsafe1 - NUMERICAL_EPS);
845 :
846 : #ifdef DEBUG_INFORMER
847 : if (DEBUG_COND) {
848 : std::cout << "nextGap=" << nextGap
849 : << " (with vsafe1 and help decel) \nvsafe1=" << vsafe1
850 : << " vsafe=" << vsafe
851 : << "\n";
852 : }
853 : #endif
854 :
855 : // For subsecond simulation, this might not lead to secure gaps for a long time,
856 : // we seek to establish a secure gap as soon as possible
857 9843 : double nextSecureGap = nv->getCarFollowModel().getSecureGap(nv, &myVehicle, vsafe, plannedSpeed, getCarFollowModel().getMaxDecel());
858 :
859 9843 : if (nextGap < nextSecureGap) {
860 : // establish a secureGap as soon as possible
861 : vsafe = neighNewSpeed;
862 : }
863 :
864 : #ifdef DEBUG_INFORMER
865 : if (DEBUG_COND) {
866 : std::cout << "nextGap=" << nextGap
867 : << " minNextSecureGap=" << nextSecureGap
868 : << " vsafe=" << vsafe << "\n";
869 : }
870 : #endif
871 :
872 : }
873 97785 : msgPass.informNeighFollower(
874 97785 : new Info(vsafe, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
875 :
876 : #ifdef DEBUG_INFORMER
877 : if (DEBUG_COND) {
878 : std::cout << " wants to cut in before nv=" << nv->getID()
879 : << " vsafe1=" << vsafe1 << " vsafe=" << vsafe
880 : << " newSecGap="
881 : << nv->getCarFollowModel().getSecureGap(nv, &myVehicle, vsafe,
882 : plannedSpeed,
883 : myVehicle.getCarFollowModel().getMaxDecel())
884 : << "\n";
885 : }
886 : #endif
887 1354378 : } else if ((MSGlobals::gSemiImplicitEulerUpdate && dv > 0 && dv * remainingSeconds > (secureGap - decelGap + POSITION_EPS))
888 1276837 : || (!MSGlobals::gSemiImplicitEulerUpdate && dv > 0 && dv * (remainingSeconds - 1) > secureGap - decelGap + POSITION_EPS)
889 : ) {
890 :
891 : // XXX: Alternative formulation (encapsulating differences of euler and ballistic) TODO: test, refs. #2575
892 : // double eventualGap = getCarFollowModel().gapExtrapolation(remainingSeconds - 1., decelGap, plannedSpeed, neighNewSpeed1s);
893 : // } else if (eventualGap > secureGap + POSITION_EPS) {
894 :
895 :
896 : // NOTE: This case corresponds to the situation, where some time is left to perform the lc
897 : // For the ballistic case this is interpreted as follows:
898 : // If the follower breaks with helpDecel for one second, this vehicle maintains the plannedSpeed,
899 : // and both continue with their speeds for remainingSeconds seconds the gap will suffice for a laneChange
900 : // For the euler case we had the following comment:
901 : // 'decelerating once is sufficient to open up a large enough gap in time', but:
902 : // XXX: 1) Decelerating *once* does not necessarily lead to the gap decelGap! (if TS<1s.) (Leo)
903 : // 2) Probably, the if() for euler should test for dv * (remainingSeconds-1) > ..., too ?!, refs. #2578
904 80450 : msgPass.informNeighFollower(new Info(neighNewSpeed, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
905 : #ifdef DEBUG_INFORMER
906 : if (DEBUG_COND) {
907 : std::cout << " wants to cut in before nv=" << nv->getID() << " (eventually)\n";
908 : }
909 : #endif
910 1273928 : } else if (dir == LCA_MRIGHT && nv->getLaneChangeModel().avoidOvertakeRight(&myVehicle)) {
911 : // XXX: check if this requires a special treatment for the ballistic update, refs. #2575
912 : const double vhelp = MAX2(neighNewSpeed, HELP_OVERTAKE);
913 1305 : msgPass.informNeighFollower(new Info(vhelp, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
914 : #ifdef DEBUG_INFORMER
915 : if (DEBUG_COND) {
916 : std::cout << " wants to cut in before nv=" << nv->getID() << " (nv cannot overtake right)\n";
917 : }
918 : #endif
919 : } else {
920 1272623 : double vhelp = MAX2(nv->getSpeed(), myVehicle.getSpeed() + HELP_OVERTAKE);
921 : //if (dir == LCA_MRIGHT && myVehicle.getWaitingSeconds() > LCA_RIGHT_IMPATIENCE &&
922 : // nv->getSpeed() > myVehicle.getSpeed()) {
923 1272623 : if (nv->getSpeed() > myVehicle.getSpeed() &&
924 326017 : ((dir == LCA_MRIGHT && myVehicle.getWaitingSeconds() > LCA_RIGHT_IMPATIENCE) // NOTE: it might be considered to use myVehicle.getAccumulatedWaitingSeconds() > LCA_RIGHT_IMPATIENCE instead (Leo). Refs. #2578
925 228877 : || (dir == LCA_MLEFT && plannedSpeed > CUT_IN_LEFT_SPEED_THRESHOLD) // VARIANT_22 (slowDownLeft)
926 : // XXX this is a hack to determine whether the vehicles is on an on-ramp. This information should be retrieved from the network itself
927 228848 : || (dir == LCA_MLEFT && myVehicle.getLane()->getLength() > MAX_ONRAMP_LENGTH)
928 : )) {
929 : // let the follower slow down to increase the likelihood that later vehicles will be slow enough to help
930 : // follower should still be fast enough to open a gap
931 : // XXX: The probability for that success would be larger if the slow down of the appropriate following vehicle
932 : // would take place without the immediate follower slowing down. We might consider to model reactions of
933 : // vehicles that are not immediate followers. (Leo) -> see ticket #2532
934 373978 : vhelp = MAX2(neighNewSpeed, myVehicle.getSpeed() + HELP_OVERTAKE);
935 : #ifdef DEBUG_INFORMER
936 : if (DEBUG_COND) {
937 : // NOTE: the condition labeled "VARIANT_22" seems to imply that this could as well concern the *left* follower?! (Leo)
938 : // Further, vhelp might be larger than nv->getSpeed(), so the request issued below is not to slow down!? (see below) Refs. #2578
939 : std::cout << " wants right follower to slow down a bit\n";
940 : }
941 : #endif
942 373978 : if (MSGlobals::gSemiImplicitEulerUpdate) {
943 : // euler
944 322696 : if ((nv->getSpeed() - myVehicle.getSpeed()) / helpDecel < remainingSeconds) {
945 :
946 : #ifdef DEBUG_INFORMER
947 : if (DEBUG_COND) {
948 : // NOTE: the condition labeled "VARIANT_22" seems to imply that this could as well concern the *left* follower?! Refs. #2578
949 : std::cout << " wants to cut in before right follower nv=" << nv->getID() << " (eventually)\n";
950 : }
951 : #endif
952 : // XXX: I don't understand. This vhelp might be larger than nv->getSpeed() but the above condition seems to rely
953 : // on the reasoning that if nv breaks with helpDecel for remaining Seconds, nv will be so slow, that this
954 : // vehicle will be able to cut in. But nv might have overtaken this vehicle already (or am I missing sth?). (Leo)
955 : // Ad: To my impression, the intention behind allowing larger speeds for the blocking follower is to prevent a
956 : // situation, where an overlapping follower keeps blocking the ego vehicle. Refs. #2578
957 307960 : msgPass.informNeighFollower(new Info(vhelp, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
958 307960 : return;
959 : }
960 : } else {
961 :
962 : // ballistic (this block is a bit different to the logic in the euler part, but in general suited to work on euler as well.. must be tested <- TODO, refs. #2575)
963 : // estimate gap after remainingSeconds.
964 : // Assumptions:
965 : // (A1) leader continues with currentSpeed. (XXX: That might be wrong: Think of accelerating on an on-ramp or of a congested region ahead!)
966 : // (A2) follower breaks with helpDecel.
967 51282 : const double gapAfterRemainingSecs = getCarFollowModel().gapExtrapolation(
968 51282 : remainingSeconds, neighFollow.second, myVehicle.getSpeed(), nv->getSpeed(), 0, -helpDecel, myVehicle.getMaxSpeedOnLane(), nv->getMaxSpeedOnLane());
969 51282 : const double secureGapAfterRemainingSecs = nv->getCarFollowModel().getSecureGap(nv, &myVehicle,
970 51282 : MAX2(nv->getSpeed() - remainingSeconds * helpDecel, 0.), myVehicle.getSpeed(), myVehicle.getCarFollowModel().getMaxDecel());
971 51282 : if (gapAfterRemainingSecs >= secureGapAfterRemainingSecs) { // XXX: here it would be wise to check whether there is enough space for eventual braking if the maneuver doesn't succeed
972 : #ifdef DEBUG_INFORMER
973 : if (DEBUG_COND) {
974 : std::cout << " wants to cut in before follower nv=" << nv->getID() << " (eventually)\n";
975 : }
976 : #endif
977 : // NOTE: ballistic uses neighNewSpeed instead of vhelp, see my note above. (Leo)
978 : // TODO: recheck if this might cause suboptimal behaviour in some LC-situations. Refs. #2578
979 9205 : msgPass.informNeighFollower(new Info(neighNewSpeed, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
980 9205 : return;
981 : }
982 : }
983 :
984 :
985 : }
986 :
987 : #ifdef DEBUG_INFORMER
988 : if (DEBUG_COND) {
989 : std::cout << SIMTIME
990 : << " veh=" << myVehicle.getID()
991 : << " informs follower " << nv->getID()
992 : << " vhelp=" << vhelp
993 : << "\n";
994 : }
995 : #endif
996 :
997 955458 : msgPass.informNeighFollower(new Info(vhelp, dir | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
998 : // This follower is supposed to overtake us. Slow down smoothly to allow this.
999 955458 : const double overtakeDist = overtakeDistance(nv, &myVehicle, neighFollow.second, vhelp, plannedSpeed);
1000 : // speed difference to create a sufficiently large gap
1001 955458 : const double needDV = overtakeDist / remainingSeconds;
1002 : // make sure the deceleration is not to strong (XXX: should be assured in finalizeSpeed -> TODO: remove the MAX2 if agreed) -> prob with possibly non-existing maximal deceleration for som CF Models(?) Refs. #2578
1003 1068931 : addLCSpeedAdvice(MAX2(vhelp - needDV, myVehicle.getSpeed() - ACCEL2SPEED(myVehicle.getCarFollowModel().getMaxDecel())), dir);
1004 :
1005 : #ifdef DEBUG_INFORMER
1006 : if (DEBUG_COND) {
1007 : std::cout << SIMTIME
1008 : << " veh=" << myVehicle.getID()
1009 : << " wants to be overtaken by=" << nv->getID()
1010 : << " overtakeDist=" << overtakeDist
1011 : << " vneigh=" << nv->getSpeed()
1012 : << " vhelp=" << vhelp
1013 : << " needDV=" << needDV
1014 : << " vsafe=" << myLCAccelerationAdvices.back().first
1015 : << "\n";
1016 : }
1017 : #endif
1018 : }
1019 1117693 : } else if (neighFollow.first != nullptr && (blocked & LCA_BLOCKED_BY_LEADER)) {
1020 : // we are not blocked by the follower now, make sure it remains that way
1021 574948 : const double vsafe = MSLCHelper::getSpeedPreservingSecureGap(myVehicle, *neighFollow.first, neighFollow.second, plannedSpeed);
1022 574948 : msgPass.informNeighFollower(new Info(vsafe, dir), &myVehicle);
1023 :
1024 : #ifdef DEBUG_INFORMER
1025 : if (DEBUG_COND) {
1026 : std::cout << " wants to cut in before non-blocking follower nv=" << nv->getID() << "\n";
1027 : }
1028 : #endif
1029 : }
1030 : }
1031 :
1032 :
1033 : void
1034 546490227 : MSLCM_LC2013::prepareStep() {
1035 546490227 : MSAbstractLaneChangeModel::prepareStep();
1036 : // keep information about strategic change direction
1037 546490227 : if (!isChangingLanes()) {
1038 545947352 : myOwnState = (myOwnState & LCA_STRATEGIC) ? (myOwnState & LCA_WANTS_LANECHANGE) : 0;
1039 : }
1040 546490227 : myLeadingBlockerLength = 0;
1041 546490227 : myLeftSpace = 0;
1042 : myLCAccelerationAdvices.clear();
1043 546490227 : myDontBrake = false;
1044 : // truncate to work around numerical instability between different builds
1045 546490227 : if (mySigma > 0 && !isChangingLanes()) {
1046 : // disturb lateral position directly
1047 1154 : const double maxDist = SPEED2DIST(myVehicle.getVehicleType().getMaxSpeedLat());
1048 1154 : const double oldPosLat = myVehicle.getLateralPositionOnLane();
1049 1154 : const double overlap = myVehicle.getLateralOverlap();
1050 : double scaledDelta;
1051 1154 : if (overlap > 0) {
1052 : // return to within lane boundary
1053 : scaledDelta = MIN2(overlap, maxDist);
1054 23 : if (myVehicle.getLateralPositionOnLane() > 0) {
1055 7 : scaledDelta *= -1;
1056 : }
1057 : } else {
1058 : // random drift
1059 1131 : double deltaPosLat = OUProcess::step(oldPosLat,
1060 1131 : myVehicle.getActionStepLengthSecs(),
1061 1131 : MAX2(NUMERICAL_EPS, (1 - mySigma) * 100), mySigma) - oldPosLat;
1062 1131 : deltaPosLat = MAX2(MIN2(deltaPosLat, maxDist), -maxDist);
1063 1131 : scaledDelta = deltaPosLat * myVehicle.getSpeed() / myVehicle.getLane()->getSpeedLimit();
1064 : }
1065 1154 : myVehicle.setLateralPositionOnLane(oldPosLat + scaledDelta);
1066 1154 : setSpeedLat(DIST2SPEED(scaledDelta));
1067 : } else {
1068 546489073 : resetSpeedLat();
1069 : }
1070 546490227 : }
1071 :
1072 :
1073 : void
1074 306595503 : MSLCM_LC2013::changed() {
1075 306595503 : myOwnState = 0;
1076 306595503 : mySpeedGainProbabilityLeft = 0;
1077 306595503 : mySpeedGainProbabilityRight = 0;
1078 306595503 : myKeepRightProbability = 0;
1079 306595503 : if (myVehicle.getBestLaneOffset() == 0) {
1080 : // if we are not yet on our best lane there might still be unseen blockers
1081 : // (during patchSpeed)
1082 306470580 : myLeadingBlockerLength = 0;
1083 306470580 : myLeftSpace = 0;
1084 : }
1085 306595503 : myLookAheadSpeed = LOOK_AHEAD_MIN_SPEED;
1086 : myLCAccelerationAdvices.clear();
1087 306595503 : myDontBrake = false;
1088 306595503 : myLeadingBlockerLength = 0;
1089 306595503 : }
1090 :
1091 :
1092 : void
1093 8276 : MSLCM_LC2013::resetState() {
1094 8276 : myOwnState = 0;
1095 8276 : mySpeedGainProbabilityLeft = 0;
1096 8276 : mySpeedGainProbabilityRight = 0;
1097 8276 : myKeepRightProbability = 0;
1098 8276 : myLeadingBlockerLength = 0;
1099 8276 : myLeftSpace = 0;
1100 8276 : myLookAheadSpeed = LOOK_AHEAD_MIN_SPEED;
1101 : myLCAccelerationAdvices.clear();
1102 8276 : myDontBrake = false;
1103 8276 : }
1104 :
1105 :
1106 : int
1107 241482062 : MSLCM_LC2013::_wantsChange(
1108 : int laneOffset,
1109 : MSAbstractLaneChangeModel::MSLCMessager& msgPass,
1110 : int blocked,
1111 : const std::pair<MSVehicle*, double>& leader,
1112 : const std::pair<MSVehicle*, double>& follower,
1113 : const std::pair<MSVehicle*, double>& neighLead,
1114 : const std::pair<MSVehicle*, double>& neighFollow,
1115 : const MSLane& neighLane,
1116 : const std::vector<MSVehicle::LaneQ>& preb,
1117 : MSVehicle* lastBlocked,
1118 : MSVehicle* firstBlocked) {
1119 : assert(laneOffset == 1 || laneOffset == -1);
1120 241482062 : const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
1121 : // compute bestLaneOffset
1122 : MSVehicle::LaneQ curr, neigh, best;
1123 : int bestLaneOffset = 0;
1124 : // What do these "dists" mean? Please comment. (Leo) Ad: I now think the following:
1125 : // currentDist is the distance that the vehicle can go on its route without having to
1126 : // change lanes from the current lane. neighDist as currentDist for the considered target lane (i.e., neigh)
1127 : // If this is true I suggest to put this into the docu of wantsChange()
1128 : double currentDist = 0;
1129 : double neighDist = 0;
1130 : int currIdx = 0;
1131 241482062 : const bool checkOpposite = &neighLane.getEdge() != &myVehicle.getLane()->getEdge();
1132 241482062 : const MSLane* prebLane = myVehicle.getLane();
1133 241482062 : if (prebLane->getEdge().isInternal()) {
1134 : // internal edges are not kept inside the bestLanes structure
1135 1183264 : if (isOpposite()) {
1136 542 : prebLane = prebLane->getNormalPredecessorLane();
1137 : } else {
1138 1182722 : prebLane = prebLane->getLinkCont()[0]->getLane();
1139 : }
1140 : }
1141 : // special case: vehicle considers changing to the opposite direction edge
1142 : const int prebOffset = laneOffset;
1143 422285951 : for (int p = 0; p < (int) preb.size(); ++p) {
1144 : //if (DEBUG_COND) {
1145 : // std::cout << " p=" << p << " prebLane=" << prebLane->getID() << " preb.p=" << preb[p].lane->getID() << "\n";
1146 : //}
1147 422285951 : if (preb[p].lane == prebLane && p + laneOffset >= 0) {
1148 : assert(p + prebOffset < (int)preb.size());
1149 : curr = preb[p];
1150 241482062 : neigh = preb[p + prebOffset];
1151 241482062 : currentDist = curr.length;
1152 241482062 : neighDist = neigh.length;
1153 241482062 : bestLaneOffset = curr.bestLaneOffset;
1154 241482062 : if (bestLaneOffset == 0 && preb[p + prebOffset].bestLaneOffset == 0 && !checkOpposite) {
1155 : #ifdef DEBUG_WANTS_CHANGE
1156 : if (DEBUG_COND) {
1157 : std::cout << STEPS2TIME(currentTime)
1158 : << " veh=" << myVehicle.getID()
1159 : << " bestLaneOffsetOld=" << bestLaneOffset
1160 : << " bestLaneOffsetNew=" << laneOffset
1161 : << "\n";
1162 : }
1163 : #endif
1164 : bestLaneOffset = prebOffset;
1165 : }
1166 241482062 : best = preb[p + bestLaneOffset];
1167 : currIdx = p;
1168 : break;
1169 : }
1170 : }
1171 : assert(curr.lane != nullptr);
1172 : assert(neigh.lane != nullptr);
1173 : assert(best.lane != nullptr);
1174 : // direction specific constants
1175 241482062 : const bool right = (laneOffset == -1);
1176 241482062 : const double posOnLane = getForwardPos();
1177 : double driveToNextStop = -std::numeric_limits<double>::max();
1178 241482062 : if (myVehicle.nextStopDist() < std::numeric_limits<double>::max()
1179 241482062 : && &myVehicle.getNextStop().lane->getEdge() == &myVehicle.getLane()->getEdge()) {
1180 : // vehicle can always drive up to stop distance
1181 : // @note this information is dynamic and thus not available in updateBestLanes()
1182 : // @note: nextStopDist was compute before the vehicle moved
1183 1766046 : driveToNextStop = myVehicle.nextStopDist();
1184 1766046 : const double stopPos = posOnLane + myVehicle.nextStopDist() - myVehicle.getLastStepDist();
1185 : #ifdef DEBUG_WANTS_CHANGE
1186 : if (DEBUG_COND) {
1187 : std::cout << SIMTIME << std::setprecision(gPrecision) << " veh=" << myVehicle.getID()
1188 : << " stopDist=" << myVehicle.nextStopDist()
1189 : << " lastDist=" << myVehicle.getLastStepDist()
1190 : << " stopPos=" << stopPos
1191 : << " currentDist=" << currentDist
1192 : << " neighDist=" << neighDist
1193 : << "\n";
1194 : }
1195 : #endif
1196 : currentDist = MAX2(currentDist, stopPos);
1197 : neighDist = MAX2(neighDist, stopPos);
1198 : }
1199 241482062 : const int lca = (right ? LCA_RIGHT : LCA_LEFT);
1200 : const int myLca = (right ? LCA_MRIGHT : LCA_MLEFT);
1201 : const int lcaCounter = (right ? LCA_LEFT : LCA_RIGHT);
1202 241482062 : bool changeToBest = (right && bestLaneOffset < 0) || (!right && bestLaneOffset > 0);
1203 : // keep information about being a leader/follower
1204 241482062 : int ret = (myOwnState & 0xffff0000);
1205 : int req = 0; // the request to change or stay
1206 :
1207 241482062 : ret = slowDownForBlocked(lastBlocked, ret);
1208 241482062 : if (lastBlocked != firstBlocked) {
1209 7600166 : ret = slowDownForBlocked(firstBlocked, ret);
1210 : }
1211 :
1212 : #ifdef DEBUG_WANTS_CHANGE
1213 : if (DEBUG_COND) {
1214 : std::cout << SIMTIME
1215 : << " veh=" << myVehicle.getID()
1216 : << " _wantsChange state=" << myOwnState
1217 : << " myLCAccelerationAdvices=" << toString(myLCAccelerationAdvices)
1218 : << " firstBlocked=" << Named::getIDSecure(firstBlocked)
1219 : << " lastBlocked=" << Named::getIDSecure(lastBlocked)
1220 : << " leader=" << Named::getIDSecure(leader.first)
1221 : << " leaderGap=" << leader.second
1222 : << " follower=" << Named::getIDSecure(follower.first)
1223 : << " followerGap=" << follower.second
1224 : << " neighLead=" << Named::getIDSecure(neighLead.first)
1225 : << " neighLeadGap=" << neighLead.second
1226 : << " neighFollow=" << Named::getIDSecure(neighFollow.first)
1227 : << " neighFollowGap=" << neighFollow.second
1228 : << "\n";
1229 : }
1230 : #endif
1231 :
1232 : // we try to estimate the distance which is necessary to get on a lane
1233 : // we have to get on in order to keep our route
1234 : // we assume we need something that depends on our velocity
1235 : // and compare this with the free space on our wished lane
1236 : //
1237 : // if the free space is somehow(<-?) less than the space we need, we should
1238 : // definitely try to get to the desired lane
1239 : //
1240 : // this rule forces our vehicle to change the lane if a lane changing is necessary soon
1241 :
1242 :
1243 : // we do not want the lookahead distance to change all the time so we let it decay slowly
1244 : // (in contrast, growth is applied instantaneously)
1245 241482062 : if (myVehicle.getSpeed() > myLookAheadSpeed) {
1246 41152068 : myLookAheadSpeed = myVehicle.getSpeed();
1247 : } else {
1248 : // memory decay factor for this action step
1249 200329994 : const double memoryFactor = 1. - (1. - LOOK_AHEAD_SPEED_MEMORY) * myVehicle.getActionStepLengthSecs();
1250 : assert(memoryFactor > 0.);
1251 200329994 : myLookAheadSpeed = MAX2(LOOK_AHEAD_MIN_SPEED,
1252 200329994 : (memoryFactor * myLookAheadSpeed + (1 - memoryFactor) * myVehicle.getSpeed()));
1253 : }
1254 241482062 : double laDist = myLookAheadSpeed * LOOK_FORWARD * myStrategicParam * (right ? 1 : myLookaheadLeft);
1255 241482062 : laDist += myVehicle.getVehicleType().getLengthWithGap() * 2.;
1256 241482062 : const bool hasStoppedLeader = leader.first != 0 && leader.first->isStopped() && leader.second < (currentDist - posOnLane);
1257 241482062 : const bool hasBidiLeader = myVehicle.getLane()->getBidiLane() != nullptr && MSLCHelper::isBidiLeader(leader.first, curr.bestContinuations);
1258 241482062 : const bool hasBidiNeighLeader = neighLane.getBidiLane() != nullptr && MSLCHelper::isBidiLeader(neighLead.first, neigh.bestContinuations);
1259 :
1260 241482062 : if (bestLaneOffset == 0 && hasBidiLeader) {
1261 : // getting out of the way is enough to clear the blockage
1262 : laDist = 0;
1263 241480957 : } else if (bestLaneOffset == 0 && hasStoppedLeader) {
1264 : // react to a stopped leader on the current lane
1265 : // The value of laDist is doubled below for the check whether the lc-maneuver can be taken out
1266 : // on the remaining distance (because the vehicle has to change back and forth). Therefore multiply with 0.5.
1267 80058 : laDist = 0.5 * (myVehicle.getVehicleType().getLengthWithGap()
1268 80058 : + leader.first->getVehicleType().getLengthWithGap()
1269 80058 : + leader.second);
1270 241400899 : } else if (bestLaneOffset == laneOffset && neighLead.first != 0 && (neighLead.first->isStopped() || hasBidiNeighLeader) && neighLead.second < (currentDist - posOnLane)) {
1271 : // react to a stopped leader on the target lane (if it is the bestLane)
1272 285854 : if (isOpposite()) {
1273 : // always allow changing back
1274 51392 : laDist = (myVehicle.getVehicleType().getLengthWithGap()
1275 51392 : + neighLead.first->getVehicleType().getLengthWithGap()
1276 51392 : + neighLead.second);
1277 234462 : } else if (!hasStoppedLeader &&
1278 214899 : ((neighLead.second + myVehicle.getVehicleType().getLengthWithGap() + neighLead.first->getVehicleType().getLengthWithGap()) < (currentDist - posOnLane)
1279 6204 : || hasBidiNeighLeader)) {
1280 : // do not change to the target lane until passing the stopped vehicle
1281 : // (unless the vehicle blocks our intended stopping position, then we have to wait anyway)
1282 : changeToBest = false;
1283 : }
1284 : }
1285 241482062 : if (myStrategicParam < 0) {
1286 : laDist = -1e3; // never perform strategic change
1287 : }
1288 :
1289 : // free space that is available for changing
1290 : //const double neighSpeed = (neighLead.first != 0 ? neighLead.first->getSpeed() :
1291 : // neighFollow.first != 0 ? neighFollow.first->getSpeed() :
1292 : // best.lane->getSpeedLimit());
1293 : // @note: while this lets vehicles change earlier into the correct direction
1294 : // it also makes the vehicles more "selfish" and prevents changes which are necessary to help others
1295 :
1296 :
1297 :
1298 : // Next we assign to roundabout edges a larger distance than to normal edges
1299 : // in order to decrease sense of lc urgency and induce higher usage of inner roundabout lanes.
1300 241482062 : const double roundaboutBonus = MSLCHelper::getRoundaboutDistBonus(myVehicle, myRoundaboutBonus, curr, neigh, best);
1301 241482062 : currentDist += roundaboutBonus;
1302 241482062 : neighDist += roundaboutBonus;
1303 :
1304 241482062 : const double usableDist = MAX2(currentDist - posOnLane - best.occupation * JAM_FACTOR, driveToNextStop);
1305 : //- (best.lane->getVehicleNumber() * neighSpeed)); // VARIANT 9 jfSpeed
1306 241482062 : const double maxJam = MAX2(preb[currIdx + prebOffset].occupation, preb[currIdx].occupation);
1307 241482062 : const double vMax = myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle);
1308 241482062 : const double neighVMax = neighLane.getVehicleMaxSpeed(&myVehicle);
1309 : // upper bound which will be restricted successively
1310 241482062 : double thisLaneVSafe = vMax;
1311 241482062 : const bool checkOverTakeRight = avoidOvertakeRight(neighLead.first, true);
1312 :
1313 241482062 : double neighLeftPlace = MAX2(0.0, neighDist - posOnLane - maxJam);
1314 241482062 : if (neighLead.first != 0 && neighLead.first->isStopped()) {
1315 245944 : neighLeftPlace = MIN2(neighLeftPlace, neighLead.second);
1316 : }
1317 :
1318 : #ifdef DEBUG_WANTS_CHANGE
1319 : if (DEBUG_COND) {
1320 : std::cout << STEPS2TIME(currentTime)
1321 : << " veh=" << myVehicle.getID()
1322 : << " laSpeed=" << myLookAheadSpeed
1323 : << " laDist=" << laDist
1324 : << " currentDist=" << currentDist
1325 : << " usableDist=" << usableDist
1326 : << " bestLaneOffset=" << bestLaneOffset
1327 : << " best.occupation=" << best.occupation
1328 : << " best.length=" << best.length
1329 : << "\n roundaboutBonus=" << roundaboutBonus
1330 : << " maxJam=" << maxJam
1331 : << " neighDist=" << neighDist
1332 : << " neighLeftPlace=" << neighLeftPlace
1333 : << (hasBidiLeader ? " bidiLeader" : "")
1334 : << (hasBidiNeighLeader ? " bidiNeighLeader" : "")
1335 : << "\n";
1336 : }
1337 : #endif
1338 :
1339 : bool changeLeftToAvoidOvertakeRight = false;
1340 195324687 : if (changeToBest && bestLaneOffset == curr.bestLaneOffset
1341 252668069 : && currentDistDisallows(usableDist, bestLaneOffset, laDist)) {
1342 : /// @brief we urgently need to change lanes to follow our route
1343 4605607 : ret = ret | lca | LCA_STRATEGIC | LCA_URGENT;
1344 : } else {
1345 : // VARIANT_20 (noOvertakeRight)
1346 236876455 : if (neighLead.first != 0 && checkOverTakeRight && !right) {
1347 : // check for slower leader on the left. we should not overtake but
1348 : // rather move left ourselves (unless congested)
1349 : const MSVehicle* nv = neighLead.first;
1350 12030425 : double deltaV = 0.;
1351 12030425 : double vSafe = 0.;
1352 12030425 : if (canOvertakeRight(nv, neighLead.second, vMax - neighLane.getVehicleMaxSpeed(nv), HELP_OVERTAKE, vSafe, deltaV)) {
1353 2326593 : if (mySpeedGainProbabilityLeft < myChangeProbThresholdLeft) {
1354 2448603 : vSafe = MAX2(vSafe, nv->getSpeed());
1355 : }
1356 2326593 : thisLaneVSafe = MIN2(thisLaneVSafe, vSafe);
1357 2326593 : addLCSpeedAdvice(vSafe, myLca);
1358 : // only generate impulse for overtaking left shortly before braking would be necessary
1359 2326593 : const double deltaGapFuture = deltaV * 8;
1360 2326593 : const double vSafeFuture = getCarFollowModel().followSpeed(
1361 2326593 : &myVehicle, myVehicle.getSpeed(), neighLead.second - deltaGapFuture, nv->getSpeed(), nv->getCarFollowModel().getMaxDecel());
1362 2326593 : if (vSafeFuture < vSafe) {
1363 1918779 : const double relativeGain = deltaV / MAX2(vMax,
1364 1918779 : RELGAIN_NORMALIZATION_MIN_SPEED);
1365 1918779 : mySpeedGainProbabilityLeft += (long long int)(myVehicle.getActionStepLengthSecs() * relativeGain * HYST_PRECISION);
1366 : changeLeftToAvoidOvertakeRight = true;
1367 : }
1368 : #ifdef DEBUG_WANTS_CHANGE
1369 : if (DEBUG_COND) {
1370 : std::cout << STEPS2TIME(currentTime)
1371 : << " avoid overtaking on the right nv=" << nv->getID()
1372 : << " deltaV=" << deltaV
1373 : << " nvSpeed=" << nv->getSpeed()
1374 : << " speedGainL=" << mySpeedGainProbabilityLeft / HYST_PRECISION
1375 : << " speedGainR=" << mySpeedGainProbabilityRight / HYST_PRECISION
1376 : << " planned acceleration =" << myLCAccelerationAdvices.back().first
1377 : << "\n";
1378 : }
1379 : #endif
1380 : }
1381 : }
1382 236876455 : const bool currFreeUntilNeighEnd = leader.first == nullptr || neighDist - posOnLane <= leader.second;
1383 457856027 : const double overtakeDist = (leader.first == 0 || hasBidiLeader ? -1 :
1384 220979572 : leader.second + myVehicle.getVehicleType().getLength() + leader.first->getVehicleType().getLengthWithGap());
1385 237118508 : const double overtakeDist2 = (neighLead.first == 0 || !neighLead.first->isStopped() ? -1 :
1386 242053 : neighLead.second + myVehicle.getVehicleType().getLength() + neighLead.first->getVehicleType().getLengthWithGap());
1387 220984184 : if (leader.first != 0 && (leader.first->isStopped() || hasBidiLeader) && leader.second < REACT_TO_STOPPED_DISTANCE
1388 : // current destination leaves enough space to overtake the leader
1389 122302 : && MIN2(neighDist, currentDist) - posOnLane > overtakeDist
1390 : // maybe do not overtake on the right at high speed
1391 78747 : && (!checkOverTakeRight || !right)
1392 77008 : && myStrategicParam >= 0
1393 236952875 : && (neighLead.first == 0 || !neighLead.first->isStopped()
1394 : // neighboring stopped vehicle leaves enough space to overtake leader
1395 21444 : || neighLead.second > overtakeDist
1396 : // if we cannot pass neighLead before reaching leader we must find another free lane
1397 18606 : || (overtakeDist2 > leader.second && hasFreeLane(laneOffset, neighLead)))) {
1398 : // avoid becoming stuck behind a stopped leader
1399 58287 : currentDist = myVehicle.getPositionOnLane() + leader.second;
1400 : #ifdef DEBUG_WANTS_CHANGE
1401 : if (DEBUG_COND) {
1402 : std::cout << " veh=" << myVehicle.getID()
1403 : << " overtake " << (hasBidiLeader ? "bidi" : "stopped") << " leader=" << leader.first->getID()
1404 : << " overtakeDist=" << overtakeDist
1405 : << " overtakeDist2=" << overtakeDist
1406 : << " hasFreeLane=" << hasFreeLane(laneOffset, neighLead)
1407 : << " remaining=" << MIN2(neighDist, currentDist) - posOnLane
1408 : << "\n";
1409 : }
1410 : #endif
1411 58287 : ret = ret | lca | LCA_STRATEGIC | LCA_URGENT;
1412 236818168 : } else if (!changeToBest && currentDistDisallows(neighLeftPlace, abs(bestLaneOffset) + 2, laDist) && !hasBidiLeader) {
1413 : // the opposite lane-changing direction should be done than the one examined herein
1414 : // we'll check whether we assume we could change anyhow and get back in time...
1415 : //
1416 : // this rule prevents the vehicle from moving in opposite direction of the best lane
1417 : // unless the way till the end where the vehicle has to be on the best lane
1418 : // is long enough
1419 : #ifdef DEBUG_WANTS_CHANGE
1420 : if (DEBUG_COND) {
1421 : std::cout << " veh=" << myVehicle.getID() << " could not change back and forth in time (1) neighLeftPlace=" << neighLeftPlace << "\n";
1422 : }
1423 : #endif
1424 41640897 : ret = ret | LCA_STAY | LCA_STRATEGIC;
1425 195177271 : } else if (bestLaneOffset == 0 && (neighLeftPlace * 2. < laDist)) {
1426 : // the current lane is the best and a lane-changing would cause a situation
1427 : // of which we assume we will not be able to return to the lane we have to be on.
1428 : // this rule prevents the vehicle from leaving the current, best lane when it is
1429 : // close to this lane's end
1430 : #ifdef DEBUG_WANTS_CHANGE
1431 : if (DEBUG_COND) {
1432 : std::cout << " veh=" << myVehicle.getID() << " could not change back and forth in time (2) neighLeftPlace=" << neighLeftPlace << "\n";
1433 : }
1434 : #endif
1435 0 : ret = ret | LCA_STAY | LCA_STRATEGIC;
1436 : } else if (bestLaneOffset == 0
1437 4050296 : && (leader.first == 0 || !leader.first->isStopped())
1438 4040844 : && !hasBidiLeader
1439 4040841 : && neigh.bestContinuations.back()->getLinkCont().size() != 0
1440 2398188 : && roundaboutBonus == 0
1441 912024 : && !checkOpposite
1442 807408 : && ((myStrategicParam >= 0 && neighDist < TURN_LANE_DIST)
1443 : // lane changing cannot possibly help
1444 756447 : || (myStrategicParam < 0 && currFreeUntilNeighEnd))
1445 : ) {
1446 : // VARIANT_21 (stayOnBest)
1447 : // we do not want to leave the best lane for a lane which leads elsewhere
1448 : // unless our leader is stopped or we are approaching a roundabout
1449 : #ifdef DEBUG_WANTS_CHANGE
1450 : if (DEBUG_COND) {
1451 : std::cout << " veh=" << myVehicle.getID() << " does not want to leave the bestLane (neighDist=" << neighDist << ")\n";
1452 : }
1453 : #endif
1454 51044 : ret = ret | LCA_STAY | LCA_STRATEGIC;
1455 : }
1456 : }
1457 : // check for overriding TraCI requests
1458 : #ifdef DEBUG_WANTS_CHANGE
1459 : if (DEBUG_COND) {
1460 : std::cout << STEPS2TIME(currentTime) << " veh=" << myVehicle.getID() << " ret=" << toString((LaneChangeAction)ret);
1461 : }
1462 : #endif
1463 : // store state before canceling
1464 241482062 : getCanceledState(laneOffset) |= ret | blocked;
1465 241482062 : ret = myVehicle.influenceChangeDecision(ret);
1466 241482062 : if ((ret & lcaCounter) != 0) {
1467 : // we are not interested in traci requests for the opposite direction here
1468 27 : ret &= ~(LCA_TRACI | lcaCounter | LCA_URGENT);
1469 : }
1470 : #ifdef DEBUG_WANTS_CHANGE
1471 : if (DEBUG_COND) {
1472 : std::cout << " retAfterInfluence=" << toString((LaneChangeAction)ret) << "\n";
1473 : }
1474 : #endif
1475 :
1476 241482062 : if ((ret & LCA_STAY) != 0) {
1477 : // remove TraCI flags because it should not be included in "state-without-traci"
1478 41770506 : ret = getCanceledState(laneOffset);
1479 41770506 : return ret;
1480 : }
1481 199711556 : if ((ret & LCA_URGENT) != 0) {
1482 : // prepare urgent lane change maneuver
1483 : // save the left space
1484 4663610 : myLeftSpace = currentDist - posOnLane;
1485 4663610 : if (changeToBest && abs(bestLaneOffset) > 1 && myVehicle.getNumRemainingEdges() > 1) {
1486 : // there might be a vehicle which needs to counter-lane-change one lane further and we cannot see it yet
1487 691874 : myLeadingBlockerLength = MAX2(getExtraReservation(bestLaneOffset, neighDist - currentDist), myLeadingBlockerLength);
1488 : #ifdef DEBUG_SAVE_BLOCKER_LENGTH
1489 : if (DEBUG_COND) {
1490 : std::cout << " reserving space for unseen blockers myLeadingBlockerLength=" << myLeadingBlockerLength << "\n";
1491 : }
1492 : #endif
1493 : }
1494 :
1495 : // letting vehicles merge in at the end of the lane in case of counter-lane change, step#1
1496 : // if there is a leader and he wants to change to the opposite direction
1497 4663610 : const bool canContinue = curr.bestContinuations.size() > 1;
1498 4663610 : bool canReserve = MSLCHelper::updateBlockerLength(myVehicle, neighLead.first, lcaCounter, myLeftSpace - POSITION_EPS, canContinue, myLeadingBlockerLength);
1499 4663610 : if (firstBlocked != neighLead.first) {
1500 4379330 : canReserve &= MSLCHelper::updateBlockerLength(myVehicle, firstBlocked, lcaCounter, myLeftSpace - POSITION_EPS, canContinue, myLeadingBlockerLength);
1501 : }
1502 : #ifdef DEBUG_SAVE_BLOCKER_LENGTH
1503 : if (DEBUG_COND) {
1504 : std::cout << SIMTIME << " canReserve=" << canReserve << " canContinue=" << canContinue << "\n";
1505 : }
1506 : #endif
1507 4663610 : if (!canReserve && !isOpposite()) {
1508 : // we have a low-priority relief connection
1509 : // std::cout << SIMTIME << " veh=" << myVehicle.getID() << " cannotReserve for blockers\n";
1510 24394 : myDontBrake = canContinue;
1511 : }
1512 :
1513 4663610 : const int remainingLanes = MAX2(1, abs(bestLaneOffset));
1514 4663610 : const double urgency = isOpposite() ? OPPOSITE_URGENCY : URGENCY;
1515 4663610 : const double remainingSeconds = ((ret & LCA_TRACI) == 0 ?
1516 : //MAX2(STEPS2TIME(TS), (myLeftSpace-myLeadingBlockerLength) / MAX2(myLookAheadSpeed, NUMERICAL_EPS) / remainingLanes / urgency) :
1517 4914053 : MAX2(STEPS2TIME(TS), myLeftSpace / MAX2(myLookAheadSpeed, NUMERICAL_EPS) / remainingLanes / urgency) :
1518 180 : myVehicle.getInfluencer().changeRequestRemainingSeconds(currentTime));
1519 4663610 : if (!hasBidiNeighLeader) {
1520 4663519 : const double plannedSpeed = informLeader(msgPass, blocked, myLca, neighLead, remainingSeconds);
1521 : // NOTE: for the ballistic update case negative speeds may indicate a stop request,
1522 : // while informLeader returns -1 in that case. Refs. #2577
1523 4663519 : if (plannedSpeed >= 0 || (!MSGlobals::gSemiImplicitEulerUpdate && plannedSpeed != -1)) {
1524 : // maybe we need to deal with a blocking follower
1525 2707480 : const bool hasBidiNeighFollower = neighLane.getBidiLane() != nullptr && MSLCHelper::isBidiFollower(&myVehicle, neighFollow.first);
1526 : if (!hasBidiNeighFollower) {
1527 2703410 : informFollower(msgPass, blocked, myLca, neighFollow, remainingSeconds, plannedSpeed);
1528 : }
1529 : }
1530 : #ifdef DEBUG_WANTS_CHANGE
1531 : if (DEBUG_COND) {
1532 : std::cout << STEPS2TIME(currentTime)
1533 : << " veh=" << myVehicle.getID()
1534 : << " myLeftSpace=" << myLeftSpace
1535 : << " remainingSeconds=" << remainingSeconds
1536 : << " plannedSpeed=" << plannedSpeed
1537 : << "\n";
1538 : }
1539 : #endif
1540 : } else {
1541 : #ifdef DEBUG_WANTS_CHANGE
1542 : if (DEBUG_COND) {
1543 : std::cout << STEPS2TIME(currentTime)
1544 : << " veh=" << myVehicle.getID()
1545 : << " myLeftSpace=" << myLeftSpace
1546 : << " remainingSeconds=" << remainingSeconds
1547 : << " hasBidiNeighLeader\n";
1548 : }
1549 : #endif
1550 : }
1551 :
1552 :
1553 : // remove TraCI flags because it should not be included in "state-without-traci"
1554 4663610 : ret = getCanceledState(laneOffset);
1555 4663610 : return ret;
1556 : }
1557 :
1558 : // we wish to anticipate future speeds. This is difficult when the leading
1559 : // vehicles are still accelerating so we resort to comparing speeds for the near future (1s) in this case
1560 183121195 : const bool acceleratingLeader = (neighLead.first != 0 && neighLead.first->getAcceleration() > 0)
1561 285312019 : || (leader.first != 0 && leader.first->getAcceleration() > 0);
1562 195047946 : double neighLaneVSafe = MIN2(neighVMax, anticipateFollowSpeed(neighLead, neighDist, neighVMax, acceleratingLeader));
1563 195047946 : thisLaneVSafe = MIN2(thisLaneVSafe, anticipateFollowSpeed(leader, currentDist, vMax, acceleratingLeader));
1564 : //std::cout << SIMTIME << " veh=" << myVehicle.getID() << " thisLaneVSafe=" << thisLaneVSafe << " neighLaneVSafe=" << neighLaneVSafe << "\n";
1565 :
1566 :
1567 : // a high inconvenience prevents cooperative changes and the following things are inconvenient:
1568 : // - a desire to change in the opposite direction for speedGain
1569 : // - low anticipated speed on the neighboring lane
1570 : // - high occupancy on the neighboring lane while in a roundabout
1571 :
1572 : double inconvenience = laneOffset < 0
1573 195047946 : ? (double)mySpeedGainProbabilityLeft / (double)myChangeProbThresholdRight
1574 100892169 : : (double)mySpeedGainProbabilityRight / (double)myChangeProbThresholdLeft;
1575 :
1576 251337525 : const double relSpeedDiff = thisLaneVSafe == 0 ? 0 : (thisLaneVSafe - neighLaneVSafe) / MAX2(thisLaneVSafe, neighLaneVSafe);
1577 : inconvenience = MAX2(relSpeedDiff, inconvenience);
1578 : inconvenience = MIN2(1.0, inconvenience);
1579 :
1580 195047946 : const bool speedGainInconvenient = inconvenience > myCooperativeParam;
1581 195047946 : const bool neighOccupancyInconvenient = neigh.lane->getBruttoOccupancy() > curr.lane->getBruttoOccupancy();
1582 : #ifdef DEBUG_WANTS_CHANGE
1583 : if (DEBUG_COND) {
1584 : std::cout << STEPS2TIME(currentTime)
1585 : << " veh=" << myVehicle.getID()
1586 : << " speedGainL=" << mySpeedGainProbabilityLeft / HYST_PRECISION
1587 : << " speedGainR=" << mySpeedGainProbabilityRight / HYST_PRECISION
1588 : << " neighSpeedFactor=" << (thisLaneVSafe / neighLaneVSafe - 1)
1589 : << " inconvenience=" << inconvenience
1590 : << " speedInconv=" << speedGainInconvenient
1591 : << " occInconv=" << neighOccupancyInconvenient
1592 : << "\n";
1593 : }
1594 : #endif
1595 :
1596 : // VARIANT_15
1597 195047946 : if (roundaboutBonus > 0) {
1598 :
1599 : #ifdef DEBUG_WANTS_CHANGE
1600 : if (DEBUG_COND) {
1601 : std::cout << STEPS2TIME(currentTime)
1602 : << " veh=" << myVehicle.getID()
1603 : << " roundaboutBonus=" << roundaboutBonus
1604 : << " myLeftSpace=" << myLeftSpace
1605 : << "\n";
1606 : }
1607 : #endif
1608 : // try to use the inner lanes of a roundabout to increase throughput
1609 : // unless we are approaching the exit
1610 4672500 : if (lca == LCA_LEFT) {
1611 : // if inconvenience is not too high, request collaborative change (currently only for ballistic update)
1612 : // TODO: test this for euler update! Refs. #2575
1613 809030 : if (MSGlobals::gSemiImplicitEulerUpdate || !neighOccupancyInconvenient) {
1614 : // if(MSGlobals::gSemiImplicitEulerUpdate || !speedGainInconvenient){
1615 809030 : req = ret | lca | LCA_COOPERATIVE;
1616 : }
1617 : } else {
1618 : // if inconvenience is not too high, request collaborative change (currently only for ballistic update)
1619 3863470 : if (MSGlobals::gSemiImplicitEulerUpdate || neighOccupancyInconvenient) {
1620 : // if(MSGlobals::gSemiImplicitEulerUpdate || speedGainInconvenient){
1621 3863470 : req = ret | LCA_STAY | LCA_COOPERATIVE;
1622 : }
1623 : }
1624 4672500 : if (!cancelRequest(req, laneOffset)) {
1625 4672500 : return ret | req;
1626 : }
1627 : }
1628 :
1629 : // let's also regard the case where the vehicle is driving on a highway...
1630 : // in this case, we do not want to get to the dead-end of an on-ramp
1631 190375446 : if (right) {
1632 90292307 : if (bestLaneOffset == 0 && myVehicle.getLane()->getSpeedLimit() > 80. / 3.6 && myLookAheadSpeed > SUMO_const_haltingSpeed) {
1633 : #ifdef DEBUG_WANTS_CHANGE
1634 : if (DEBUG_COND) {
1635 : std::cout << " veh=" << myVehicle.getID() << " does not want to get stranded on the on-ramp of a highway\n";
1636 : }
1637 : #endif
1638 87738 : req = ret | LCA_STAY | LCA_STRATEGIC;
1639 87738 : if (!cancelRequest(req, laneOffset)) {
1640 : return ret | req;
1641 : }
1642 : }
1643 : }
1644 : // --------
1645 :
1646 : // -------- make place on current lane if blocking follower
1647 : //if (amBlockingFollowerPlusNB()) {
1648 : // std::cout << myVehicle.getID() << ", " << currentDistAllows(neighDist, bestLaneOffset, laDist)
1649 : // << " neighDist=" << neighDist
1650 : // << " currentDist=" << currentDist
1651 : // << "\n";
1652 : //}
1653 :
1654 : if (amBlockingFollowerPlusNB()
1655 149683 : && (!speedGainInconvenient)
1656 149170 : && ((myOwnState & myLca) != 0) // VARIANT_6 : counterNoHelp
1657 190336536 : && (changeToBest || currentDistAllows(neighDist, abs(bestLaneOffset) + 1, laDist))) {
1658 :
1659 : // VARIANT_2 (nbWhenChangingToHelp)
1660 : #ifdef DEBUG_COOPERATE
1661 : if (DEBUG_COND) {
1662 : std::cout << STEPS2TIME(currentTime)
1663 : << " veh=" << myVehicle.getID()
1664 : << " wantsChangeToHelp=" << (right ? "right" : "left")
1665 : << " state=" << myOwnState
1666 : << (((myOwnState & myLca) == 0) ? " (counter)" : "")
1667 : << "\n";
1668 : }
1669 : #endif
1670 48808 : req = ret | lca | LCA_COOPERATIVE | LCA_URGENT ;//| LCA_CHANGE_TO_HELP;
1671 48808 : if (!cancelRequest(req, laneOffset)) {
1672 48802 : if ((blocked & LCA_BLOCKED_BY_LEFT_FOLLOWER) && !right && mySpeedGainProbabilityLeft > (long long int)(mySpeedGainUrgency * HYST_PRECISION)) {
1673 288 : MSVehicle* nv = neighFollow.first;
1674 288 : const bool hasBidiNeighFollower = neighLane.getBidiLane() != nullptr && MSLCHelper::isBidiFollower(&myVehicle, nv);
1675 288 : if (nv != nullptr && !hasBidiNeighFollower && !MSLCHelper::unwillingToHelp(myVehicle, myVehicle.getSpeed(), *nv)) {
1676 288 : const double helpSpeed = MAX2(nv->getCarFollowModel().minNextSpeed(nv->getSpeed(), nv), myVehicle.getSpeed() - 1);
1677 288 : msgPass.informNeighFollower(new Info(helpSpeed, myLca | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
1678 : }
1679 : }
1680 48802 : return ret | req;
1681 : }
1682 : }
1683 :
1684 : // --------
1685 :
1686 :
1687 : //// -------- security checks for krauss
1688 : //// (vsafe fails when gap<0)
1689 : //if ((blocked & LCA_BLOCKED) != 0) {
1690 : // return ret;
1691 : //}
1692 : //// --------
1693 :
1694 : // -------- higher speed
1695 : //if ((congested(neighLead.first) && neighLead.second < 20) || predInteraction(leader.first)) { //!!!
1696 : // return ret;
1697 : //}
1698 :
1699 190238926 : if (neighLane.getEdge().getPersons().size() > 0) {
1700 : // react to pedestrians
1701 52178 : adaptSpeedToPedestrians(myVehicle.getLane(), thisLaneVSafe);
1702 52178 : adaptSpeedToPedestrians(&neighLane, neighLaneVSafe);
1703 : }
1704 :
1705 190238926 : const double relativeGain = (neighLaneVSafe - thisLaneVSafe) / MAX2(neighLaneVSafe,
1706 190238926 : RELGAIN_NORMALIZATION_MIN_SPEED);
1707 :
1708 : #ifdef DEBUG_WANTS_CHANGE
1709 : if (DEBUG_COND) {
1710 : std::cout << STEPS2TIME(currentTime)
1711 : << " veh=" << myVehicle.getID()
1712 : << " currentDist=" << currentDist
1713 : << " neighDist=" << neighDist
1714 : << " thisVSafe=" << thisLaneVSafe
1715 : << " neighVSafe=" << neighLaneVSafe
1716 : << " relGain=" << toString(relativeGain, 8)
1717 : << "\n";
1718 : }
1719 : #endif
1720 :
1721 190238926 : if (right) {
1722 : // ONLY FOR CHANGING TO THE RIGHT
1723 90200653 : if (thisLaneVSafe - 5 / 3.6 > neighLaneVSafe) {
1724 : // ok, the current lane is faster than the right one...
1725 60881640 : mySpeedGainProbabilityRight = (long long int)((double)mySpeedGainProbabilityRight * pow(0.5, myVehicle.getActionStepLengthSecs()));
1726 : //myKeepRightProbability /= 2.0;
1727 : } else {
1728 : // ok, the current lane is not (much) faster than the right one
1729 29319013 : mySpeedGainProbabilityRight += (long long int)(myVehicle.getActionStepLengthSecs() * relativeGain * HYST_PRECISION);
1730 :
1731 : // honor the obligation to keep right (Rechtsfahrgebot)
1732 29319013 : const double roadSpeedFactor = vMax / myVehicle.getLane()->getSpeedLimit(); // differse from speedFactor if vMax < speedLimit
1733 : double acceptanceTime;
1734 29319013 : if (myKeepRightAcceptanceTime == -1) {
1735 : // legacy behavior: scale acceptance time with current speed and
1736 : // use old hard-coded constant
1737 58635418 : acceptanceTime = 7 * roadSpeedFactor * MAX2(1.0, myVehicle.getSpeed());
1738 : } else {
1739 1304 : acceptanceTime = myKeepRightAcceptanceTime * roadSpeedFactor;
1740 1511 : if (follower.first != nullptr && follower.second < 2 * follower.first->getCarFollowModel().brakeGap(follower.first->getSpeed())) {
1741 : // reduce acceptanceTime if the follower vehicle is faster or wants to drive faster
1742 207 : if (follower.first->getSpeed() >= myVehicle.getSpeed()) {
1743 380 : acceptanceTime *= MAX2(1.0, myVehicle.getSpeed()) / MAX2(1.0, follower.first->getSpeed());
1744 190 : const double fRSF = follower.first->getLane()->getVehicleMaxSpeed(follower.first) / follower.first->getLane()->getSpeedLimit();
1745 190 : if (fRSF > roadSpeedFactor) {
1746 120 : acceptanceTime /= fRSF;
1747 : }
1748 : }
1749 : }
1750 : }
1751 29319013 : double fullSpeedGap = MAX2(0., neighDist - myVehicle.getCarFollowModel().brakeGap(vMax));
1752 29319013 : double fullSpeedDrivingSeconds = MIN2(acceptanceTime, fullSpeedGap / vMax);
1753 29319013 : if (neighLead.first != 0 && neighLead.first->getSpeed() < vMax) {
1754 23108011 : fullSpeedGap = MAX2(0., MIN2(fullSpeedGap,
1755 23108011 : neighLead.second - myVehicle.getCarFollowModel().getSecureGap(&myVehicle, neighLead.first,
1756 23108011 : vMax, neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel())));
1757 23108011 : fullSpeedDrivingSeconds = MIN2(fullSpeedDrivingSeconds, fullSpeedGap / (vMax - neighLead.first->getSpeed()));
1758 : }
1759 : // stay on the current lane if we cannot overtake a slow leader on the right
1760 4612337 : if (checkOverTakeRight && leader.first != 0
1761 33406101 : && leader.first->getLane()->getVehicleMaxSpeed(leader.first) < vMax) {
1762 1285283 : fullSpeedGap = MIN2(fullSpeedGap, leader.second);
1763 1285283 : fullSpeedDrivingSeconds = MIN2(fullSpeedDrivingSeconds, fullSpeedGap / (vMax - leader.first->getSpeed()));
1764 : }
1765 :
1766 29319013 : const double deltaProb = (myChangeProbThresholdRight == std::numeric_limits<long long int>::max()) ? 0 :
1767 29306111 : ((double)myChangeProbThresholdRight * (fullSpeedDrivingSeconds / acceptanceTime) / KEEP_RIGHT_TIME);
1768 29319013 : myKeepRightProbability -= (long long int)(myVehicle.getActionStepLengthSecs() * deltaProb);
1769 :
1770 : //std::cout << STEPS2TIME(currentTime)
1771 : // << " veh=" << myVehicle.getID()
1772 : // << " acceptanceTime=" << acceptanceTime
1773 : // << " fullSpeedDrivingSeconds=" << fullSpeedDrivingSeconds
1774 : // << " dProb=" << deltaProb
1775 : // << " myKeepRightProbability=" << myKeepRightProbability
1776 : // << "\n";
1777 :
1778 : #ifdef DEBUG_WANTS_CHANGE
1779 : if (DEBUG_COND) {
1780 : std::cout << STEPS2TIME(currentTime)
1781 : << " veh=" << myVehicle.getID()
1782 : << " vMax=" << vMax
1783 : << " neighDist=" << neighDist
1784 : << " brakeGap=" << myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed())
1785 : << " leaderSpeed=" << (neighLead.first == 0 ? -1 : neighLead.first->getSpeed())
1786 : << " secGap=" << (neighLead.first == 0 ? -1 : myVehicle.getCarFollowModel().getSecureGap(&myVehicle, neighLead.first,
1787 : myVehicle.getSpeed(), neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel()))
1788 : << " acceptanceTime=" << acceptanceTime
1789 : << " fullSpeedGap=" << fullSpeedGap
1790 : << " fullSpeedDrivingSeconds=" << fullSpeedDrivingSeconds
1791 : << " dProb=" << deltaProb / HYST_PRECISION
1792 : << " myKeepRightProbability=" << myKeepRightProbability / HYST_PRECISION
1793 : << "\n";
1794 : }
1795 : #endif
1796 29319013 : if ((long long int)((double)myKeepRightProbability * myKeepRightParam) < -myChangeProbThresholdRight) {
1797 3137740 : req = ret | lca | LCA_KEEPRIGHT;
1798 3137740 : if (!cancelRequest(req, laneOffset)) {
1799 3137340 : return ret | req;
1800 : }
1801 : }
1802 : }
1803 :
1804 : #ifdef DEBUG_WANTS_CHANGE
1805 : if (DEBUG_COND) {
1806 : std::cout << STEPS2TIME(currentTime)
1807 : << " veh=" << myVehicle.getID()
1808 : << " speed=" << myVehicle.getSpeed()
1809 : << " speedGainL=" << mySpeedGainProbabilityLeft / HYST_PRECISION
1810 : << " speedGainR=" << mySpeedGainProbabilityRight / HYST_PRECISION
1811 : << " thisLaneVSafe=" << thisLaneVSafe
1812 : << " neighLaneVSafe=" << neighLaneVSafe
1813 : << " relativeGain=" << relativeGain
1814 : << " blocked=" << blocked
1815 : << "\n";
1816 : }
1817 : #endif
1818 :
1819 87063313 : if (mySpeedGainProbabilityRight > myChangeProbThresholdRight
1820 87546476 : && neighDist / MAX2(.1, myVehicle.getSpeed()) > mySpeedGainRemainTime) { //./MAX2( .1, myVehicle.getSpeed())) { // -.1
1821 462535 : req = ret | lca | LCA_SPEEDGAIN;
1822 462535 : if (mySpeedGainProbabilityRight > (long long int)(mySpeedGainUrgency * HYST_PRECISION)) {
1823 350 : req |= LCA_URGENT;
1824 : }
1825 462535 : if (!cancelRequest(req, laneOffset)) {
1826 462535 : return ret | req;
1827 : }
1828 : }
1829 : } else {
1830 : // ONLY FOR CHANGING TO THE LEFT
1831 100038273 : if (thisLaneVSafe > neighLaneVSafe) {
1832 : // this lane is better
1833 59830447 : mySpeedGainProbabilityLeft = (long long int)((double)mySpeedGainProbabilityLeft * pow(0.5, myVehicle.getActionStepLengthSecs()));
1834 40207826 : } else if (thisLaneVSafe == neighLaneVSafe) {
1835 20127242 : mySpeedGainProbabilityLeft = (long long int)((double)mySpeedGainProbabilityLeft * pow(0.8, myVehicle.getActionStepLengthSecs()));
1836 : } else {
1837 20080584 : mySpeedGainProbabilityLeft += (long long int)(myVehicle.getActionStepLengthSecs() * relativeGain * HYST_PRECISION);
1838 : }
1839 : // VARIANT_19 (stayRight)
1840 : //if (neighFollow.first != 0) {
1841 : // MSVehicle* nv = neighFollow.first;
1842 : // const double secGap = nv->getCarFollowModel().getSecureGap(nv, &myVehicle, nv->getSpeed(), myVehicle.getSpeed(), myVehicle.getCarFollowModel().getMaxDecel());
1843 : // if (neighFollow.second < secGap * KEEP_RIGHT_HEADWAY) {
1844 : // // do not change left if it would inconvenience faster followers
1845 : // return ret | LCA_STAY | LCA_SPEEDGAIN;
1846 : // }
1847 : //}
1848 :
1849 : #ifdef DEBUG_WANTS_CHANGE
1850 : if (DEBUG_COND) {
1851 : std::cout << STEPS2TIME(currentTime)
1852 : << " veh=" << myVehicle.getID()
1853 : << " speed=" << myVehicle.getSpeed()
1854 : << " speedGainL=" << mySpeedGainProbabilityLeft / HYST_PRECISION
1855 : << " speedGainR=" << mySpeedGainProbabilityRight / HYST_PRECISION
1856 : << " thisLaneVSafe=" << thisLaneVSafe
1857 : << " neighLaneVSafe=" << neighLaneVSafe
1858 : << " relativeGain=" << relativeGain
1859 : << " blocked=" << blocked
1860 : << "\n";
1861 : }
1862 : #endif
1863 :
1864 100038273 : if (mySpeedGainProbabilityLeft > myChangeProbThresholdLeft
1865 10139715 : && (relativeGain > NUMERICAL_EPS || changeLeftToAvoidOvertakeRight)
1866 115411633 : && neighDist / MAX2(.1, myVehicle.getSpeed()) > mySpeedGainRemainTime) { // .1
1867 7380427 : req = ret | lca | LCA_SPEEDGAIN;
1868 7380427 : if (mySpeedGainProbabilityLeft > (long long int)(mySpeedGainUrgency * HYST_PRECISION)) {
1869 3347 : req |= LCA_URGENT;
1870 : }
1871 7380427 : if (!cancelRequest(req, laneOffset)) {
1872 7380311 : if ((req & LCA_URGENT) && (blocked & LCA_BLOCKED_BY_LEFT_FOLLOWER)) {
1873 3206 : MSVehicle* nv = neighFollow.first;
1874 3206 : const bool hasBidiNeighFollower = neighLane.getBidiLane() != nullptr && MSLCHelper::isBidiFollower(&myVehicle, nv);
1875 3206 : if (nv != nullptr && !hasBidiNeighFollower && !MSLCHelper::unwillingToHelp(myVehicle, myVehicle.getSpeed(), *nv)) {
1876 2165 : const double helpSpeed = MAX2(nv->getCarFollowModel().minNextSpeed(nv->getSpeed(), nv), myVehicle.getSpeed() - 1);
1877 2165 : msgPass.informNeighFollower(new Info(helpSpeed, myLca | LCA_AMBLOCKINGFOLLOWER), &myVehicle);
1878 : }
1879 : }
1880 7380311 : return ret | req;
1881 : }
1882 : }
1883 : }
1884 : // --------
1885 179258740 : if (changeToBest && bestLaneOffset == curr.bestLaneOffset
1886 2640065 : && myStrategicParam >= 0
1887 2633373 : && relativeGain >= 0
1888 915707 : && (right ? mySpeedGainProbabilityRight : mySpeedGainProbabilityLeft) > 0) {
1889 : // change towards the correct lane, speedwise it does not hurt
1890 130584 : req = ret | lca | LCA_STRATEGIC;
1891 130584 : if (!cancelRequest(req, laneOffset)) {
1892 130584 : return ret | req;
1893 : }
1894 : }
1895 : #ifdef DEBUG_WANTS_CHANGE
1896 : if (DEBUG_COND) {
1897 : std::cout << STEPS2TIME(currentTime)
1898 : << " veh=" << myVehicle.getID()
1899 : << " speedGainL=" << mySpeedGainProbabilityLeft / HYST_PRECISION
1900 : << " speedGainR=" << mySpeedGainProbabilityRight / HYST_PRECISION
1901 : << " myKeepRightProbability=" << myKeepRightProbability / HYST_PRECISION
1902 : << " thisLaneVSafe=" << thisLaneVSafe
1903 : << " neighLaneVSafe=" << neighLaneVSafe
1904 : << "\n";
1905 : }
1906 : #endif
1907 :
1908 : return ret;
1909 : }
1910 :
1911 :
1912 : double
1913 390095892 : MSLCM_LC2013::anticipateFollowSpeed(const std::pair<MSVehicle*, double>& leaderDist, double dist, double vMax, bool acceleratingLeader) {
1914 390095892 : const MSVehicle* leader = leaderDist.first;
1915 390095892 : const double gap = leaderDist.second;
1916 : double futureSpeed;
1917 390095892 : if (acceleratingLeader) {
1918 : // XXX see #6562
1919 270862720 : const double maxSpeed1s = (myVehicle.getSpeed() + myVehicle.getCarFollowModel().getMaxAccel()
1920 270862720 : - ACCEL2SPEED(myVehicle.getCarFollowModel().getMaxAccel()));
1921 270862720 : if (leader == nullptr) {
1922 4990641 : if (hasBlueLight()) {
1923 : // can continue from any lane if necessary
1924 : futureSpeed = vMax;
1925 : } else {
1926 4990599 : futureSpeed = getCarFollowModel().followSpeed(&myVehicle, maxSpeed1s, dist, 0, 0);
1927 : }
1928 : } else {
1929 265872079 : futureSpeed = getCarFollowModel().followSpeed(&myVehicle, maxSpeed1s, gap, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel());
1930 : }
1931 : } else {
1932 : // onInsertion = true because the vehicle has already moved
1933 119233172 : if (leader == nullptr) {
1934 17296027 : if (hasBlueLight()) {
1935 : // can continue from any lane if necessary
1936 : futureSpeed = vMax;
1937 : } else {
1938 17294452 : futureSpeed = getCarFollowModel().maximumSafeStopSpeed(dist, getCarFollowModel().getMaxDecel(), myVehicle.getSpeed(), true);
1939 : }
1940 : } else {
1941 101937145 : futureSpeed = getCarFollowModel().maximumSafeFollowSpeed(gap, myVehicle.getSpeed(), leader->getSpeed(), leader->getCarFollowModel().getMaxDecel(), true);
1942 : }
1943 : }
1944 : futureSpeed = MIN2(vMax, futureSpeed);
1945 390095892 : if (leader != nullptr && gap > 0 && mySpeedGainLookahead > 0) {
1946 1004 : const double futureLeaderSpeed = acceleratingLeader ? leader->getLane()->getVehicleMaxSpeed(leader) : leader->getSpeed();
1947 1004 : const double deltaV = vMax - futureLeaderSpeed;
1948 1004 : if (deltaV > 0 && gap > 0) {
1949 986 : const double secGap = getCarFollowModel().getSecureGap(&myVehicle, leader, futureSpeed, leader->getSpeed(), getCarFollowModel().getMaxDecel());
1950 986 : const double fullSpeedGap = gap - secGap;
1951 986 : if (fullSpeedGap / deltaV < mySpeedGainLookahead) {
1952 : // anticipate future braking by computing the average
1953 : // speed over the next few seconds
1954 : const double gapClosingTime = MAX2(0.0, fullSpeedGap / deltaV);
1955 190 : const double foreCastTime = mySpeedGainLookahead * 2;
1956 : //if (DEBUG_COND) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " leader=" << leader->getID() << " gap=" << gap << " deltaV=" << deltaV << " futureSpeed=" << futureSpeed << " futureLeaderSpeed=" << futureLeaderSpeed;
1957 190 : futureSpeed = MIN2(futureSpeed, (gapClosingTime * futureSpeed + (foreCastTime - gapClosingTime) * futureLeaderSpeed) / foreCastTime);
1958 : //if (DEBUG_COND) std::cout << " newFutureSpeed=" << futureSpeed << "\n";
1959 : }
1960 : }
1961 : }
1962 390095892 : return futureSpeed;
1963 : }
1964 :
1965 :
1966 : int
1967 249082228 : MSLCM_LC2013::slowDownForBlocked(MSVehicle* blocked, int state) {
1968 : // if this vehicle is blocking someone in front, we maybe decelerate to let him in
1969 249082228 : if (blocked != nullptr) {
1970 18366690 : double gap = blocked->getPositionOnLane() - blocked->getVehicleType().getLength() - myVehicle.getPositionOnLane() - myVehicle.getVehicleType().getMinGap();
1971 : #ifdef DEBUG_SLOW_DOWN
1972 : if (DEBUG_COND) {
1973 : std::cout << SIMTIME
1974 : << " veh=" << myVehicle.getID()
1975 : << " blocked=" << Named::getIDSecure(blocked)
1976 : << " gap=" << gap
1977 : << "\n";
1978 : }
1979 : #endif
1980 18366690 : if (gap > POSITION_EPS) {
1981 : //const bool blockedWantsUrgentRight = (((*blocked)->getLaneChangeModel().getOwnState() & LCA_RIGHT != 0)
1982 : // && ((*blocked)->getLaneChangeModel().getOwnState() & LCA_URGENT != 0));
1983 :
1984 14646728 : if (myVehicle.getSpeed() < myVehicle.getCarFollowModel().getMaxDecel()
1985 : //|| blockedWantsUrgentRight // VARIANT_10 (helpblockedRight)
1986 : ) {
1987 12893205 : if (blocked->getSpeed() < SUMO_const_haltingSpeed) {
1988 7312779 : state |= LCA_AMBACKBLOCKER_STANDING;
1989 : } else {
1990 5580426 : state |= LCA_AMBACKBLOCKER;
1991 : }
1992 25786410 : addLCSpeedAdvice(getCarFollowModel().followSpeed(
1993 12893205 : &myVehicle, myVehicle.getSpeed(),
1994 12893205 : gap - POSITION_EPS, blocked->getSpeed(),
1995 : blocked->getCarFollowModel().getMaxDecel()), LCA_CHANGE_TO_HELP);
1996 :
1997 : //(*blocked) = 0; // VARIANT_14 (furtherBlock)
1998 : #ifdef DEBUG_SLOW_DOWN
1999 : if (DEBUG_COND) {
2000 : std::cout << SIMTIME
2001 : << " veh=" << myVehicle.getID()
2002 : << " slowing down for"
2003 : << " blocked=" << Named::getIDSecure(blocked)
2004 : << " helpSpeed=" << myLCAccelerationAdvices.back().first
2005 : << "\n";
2006 : }
2007 : #endif
2008 : } /*else if ((*blocked)->getWaitingSeconds() > 30 && gap > myVehicle.getBrakeGap()) {
2009 : // experimental else-branch...
2010 :
2011 : state |= LCA_AMBACKBLOCKER;
2012 : addLCSpeedAdvice(getCarFollowModel().followSpeed(
2013 : &myVehicle, myVehicle.getSpeed(),
2014 : (gap - POSITION_EPS), (*blocked)->getSpeed(),
2015 : (*blocked)->getCarFollowModel().getMaxDecel()));
2016 : } */
2017 : }
2018 : }
2019 249082228 : return state;
2020 : }
2021 :
2022 :
2023 : void
2024 104356 : MSLCM_LC2013::adaptSpeedToPedestrians(const MSLane* lane, double& v) {
2025 104356 : if (lane->hasPedestrians()) {
2026 : #ifdef DEBUG_WANTS_CHANGE
2027 : if (DEBUG_COND) {
2028 : std::cout << SIMTIME << " adapt to pedestrians on lane=" << lane->getID() << "\n";
2029 : }
2030 : #endif
2031 20717 : PersonDist leader = lane->nextBlocking(myVehicle.getPositionOnLane(),
2032 20717 : myVehicle.getRightSideOnLane(), myVehicle.getRightSideOnLane() + myVehicle.getVehicleType().getWidth(),
2033 20717 : ceil(myVehicle.getSpeed() / myVehicle.getCarFollowModel().getMaxDecel()));
2034 20717 : if (leader.first != 0) {
2035 7685 : const double stopSpeed = myVehicle.getCarFollowModel().stopSpeed(&myVehicle, myVehicle.getSpeed(), leader.second - myVehicle.getVehicleType().getMinGap());
2036 12606 : v = MIN2(v, stopSpeed);
2037 : #ifdef DEBUG_WANTS_CHANGE
2038 : if (DEBUG_COND) {
2039 : std::cout << SIMTIME << " pedLeader=" << leader.first->getID() << " dist=" << leader.second << " v=" << v << "\n";
2040 : }
2041 : #endif
2042 : }
2043 : }
2044 104356 : }
2045 :
2046 :
2047 : double
2048 619783 : MSLCM_LC2013::computeSpeedLat(double latDist, double& maneuverDist, bool urgent) const {
2049 619783 : double result = MSAbstractLaneChangeModel::computeSpeedLat(latDist, maneuverDist, urgent);
2050 : #ifdef DEBUG_WANTS_CHANGE
2051 : if (DEBUG_COND) {
2052 : std::cout << SIMTIME << " veh=" << myVehicle.getID() << " myLeftSpace=" << myLeftSpace << " latDist=" << latDist << " maneuverDist=" << maneuverDist << " result=" << result << "\n";
2053 : }
2054 : #endif
2055 619783 : if (myLeftSpace > POSITION_EPS || !urgent) {
2056 504990 : double speedBound = myMaxSpeedLatStanding + myMaxSpeedLatFactor * myVehicle.getSpeed();
2057 504990 : if (isChangingLanes()) {
2058 : speedBound = MAX2(LC_RESOLUTION_SPEED_LAT, speedBound);
2059 : }
2060 504990 : result = MAX2(-speedBound, MIN2(speedBound, result));
2061 : }
2062 619783 : return result;
2063 : }
2064 :
2065 :
2066 : double
2067 503240429 : MSLCM_LC2013::getSafetyFactor() const {
2068 503240429 : return 1 / myAssertive;
2069 : }
2070 :
2071 : double
2072 8466899 : MSLCM_LC2013::getOppositeSafetyFactor() const {
2073 8466899 : return myOppositeParam <= 0 ? std::numeric_limits<double>::max() : 1 / myOppositeParam;
2074 : }
2075 :
2076 : bool
2077 154969 : MSLCM_LC2013::saveBlockerLength(double length, double foeLeftSpace) {
2078 154969 : const bool canReserve = MSLCHelper::canSaveBlockerLength(myVehicle, length, myLeftSpace);
2079 154969 : if (!isOpposite() && (canReserve || myLeftSpace > foeLeftSpace)) {
2080 130516 : myLeadingBlockerLength = MAX2(length, myLeadingBlockerLength);
2081 : #ifdef DEBUG_SAVE_BLOCKER_LENGTH
2082 : if (DEBUG_COND) {
2083 : std::cout << SIMTIME << " saveBlockerLength veh=" << myVehicle.getID() << " canReserve=" << canReserve << " myLeftSpace=" << myLeftSpace << " foeLeftSpace=" << foeLeftSpace << "\n";
2084 : }
2085 : #endif
2086 130516 : if (myLeftSpace == 0 && foeLeftSpace < 0) {
2087 : // called from opposite overtaking, myLeftSpace must be initialized
2088 127743 : myLeftSpace = myVehicle.getBestLanes()[myVehicle.getLane()->getIndex()].length - myVehicle.getPositionOnLane();
2089 : }
2090 130516 : return true;
2091 : } else {
2092 : return false;
2093 : }
2094 : }
2095 :
2096 :
2097 : bool
2098 25597 : MSLCM_LC2013::hasFreeLane(int laneOffset, const std::pair<MSVehicle*, double>& neighLeadStopped) const {
2099 25597 : if (neighLeadStopped.first == nullptr) {
2100 : return true;
2101 : }
2102 25597 : int dir = (laneOffset > 0 ? 1 : -1);
2103 25597 : const MSLane* neigh = myVehicle.getLane()->getParallelLane(laneOffset);
2104 25597 : if (dir > 0 && !neigh->allowsChangingLeft(myVehicle.getVClass())) {
2105 : return false;
2106 25597 : } else if (dir < 0 && !neigh->allowsChangingRight(myVehicle.getVClass())) {
2107 : return false;
2108 : }
2109 25597 : int nextOffset = laneOffset + dir;
2110 25597 : const MSLane* next = myVehicle.getLane()->getParallelLane(nextOffset);
2111 25597 : if (next == nullptr || !next->allowsVehicleClass(myVehicle.getVClass())) {
2112 18009 : return false;
2113 : }
2114 7588 : const double overtakeDist = neighLeadStopped.second + neighLeadStopped.first->getVehicleType().getLengthWithGap() + myVehicle.getLength() + POSITION_EPS;
2115 7588 : std::pair<MSVehicle* const, double> nextLead = next->getLeader(&myVehicle, myVehicle.getPositionOnLane(), myVehicle.getBestLanesContinuation(next), overtakeDist);
2116 8061 : return nextLead.first == nullptr || nextLead.second >= overtakeDist || hasFreeLane(nextOffset, nextLead);
2117 : }
2118 :
2119 :
2120 : std::string
2121 150 : MSLCM_LC2013::getParameter(const std::string& key) const {
2122 150 : if (key == toString(SUMO_ATTR_LCA_STRATEGIC_PARAM)) {
2123 36 : return toString(myStrategicParam);
2124 114 : } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_PARAM)) {
2125 36 : return toString(myCooperativeParam);
2126 78 : } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_PARAM)) {
2127 36 : return toString(mySpeedGainParam);
2128 42 : } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_PARAM)) {
2129 0 : return toString(myKeepRightParam);
2130 42 : } else if (key == toString(SUMO_ATTR_LCA_OPPOSITE_PARAM)) {
2131 0 : return toString(myOppositeParam);
2132 42 : } else if (key == toString(SUMO_ATTR_LCA_LOOKAHEADLEFT)) {
2133 0 : return toString(myLookaheadLeft);
2134 42 : } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAINRIGHT)) {
2135 0 : return toString(mySpeedGainRight);
2136 42 : } else if (key == toString(SUMO_ATTR_LCA_ASSERTIVE)) {
2137 0 : return toString(myAssertive);
2138 42 : } else if (key == toString(SUMO_ATTR_LCA_OVERTAKE_RIGHT)) {
2139 0 : return toString(myOvertakeRightParam);
2140 42 : } else if (key == toString(SUMO_ATTR_LCA_SIGMA)) {
2141 0 : return toString(mySigma);
2142 42 : } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_ACCEPTANCE_TIME)) {
2143 0 : return toString(myKeepRightAcceptanceTime);
2144 42 : } else if (key == toString(SUMO_ATTR_LCA_OVERTAKE_DELTASPEED_FACTOR)) {
2145 0 : return toString(myOvertakeDeltaSpeedFactor);
2146 42 : } else if (key == toString(SUMO_ATTR_LCA_STRATEGIC_LOOKAHEAD)) {
2147 0 : return toString(myStrategicLookahead);
2148 42 : } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD)) {
2149 0 : return toString(mySpeedGainLookahead);
2150 42 : } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME)) {
2151 0 : return toString(mySpeedGainRemainTime);
2152 42 : } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_ROUNDABOUT)) {
2153 0 : return toString(myRoundaboutBonus);
2154 42 : } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_SPEED)) {
2155 0 : return toString(myCooperativeSpeed);
2156 42 : } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATSTANDING)) {
2157 0 : return toString(myMaxSpeedLatStanding);
2158 42 : } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATFACTOR)) {
2159 0 : return toString(myMaxSpeedLatFactor);
2160 42 : } else if (key == toString(SUMO_ATTR_LCA_MAXDISTLATSTANDING)) {
2161 0 : return toString(myMaxDistLatStanding);
2162 : // access to internal state for debugging in sumo-gui (not documented since it may change at any time)
2163 42 : } else if (key == "speedGainProbabilityRight") {
2164 0 : return toString(mySpeedGainProbabilityRight / HYST_PRECISION);
2165 42 : } else if (key == "speedGainProbabilityLeft") {
2166 0 : return toString(mySpeedGainProbabilityLeft / HYST_PRECISION);
2167 42 : } else if (key == "keepRightProbability") {
2168 0 : return toString(-myKeepRightProbability / HYST_PRECISION);
2169 42 : } else if (key == "lookAheadSpeed") {
2170 0 : return toString(myLookAheadSpeed);
2171 : // motivation relative to threshold
2172 42 : } else if (key == "speedGainRP") {
2173 0 : return toString(mySpeedGainProbabilityRight / myChangeProbThresholdRight);
2174 42 : } else if (key == "speedGainLP") {
2175 0 : return toString(mySpeedGainProbabilityLeft / myChangeProbThresholdLeft);
2176 42 : } else if (key == "keepRightP") {
2177 0 : return toString((double)myKeepRightProbability * myKeepRightParam / -(double)myChangeProbThresholdRight);
2178 : }
2179 168 : throw InvalidArgument("Parameter '" + key + "' is not supported for laneChangeModel of type '" + toString(myModel) + "'");
2180 : }
2181 :
2182 :
2183 : void
2184 12052 : MSLCM_LC2013::setParameter(const std::string& key, const std::string& value) {
2185 : double doubleValue;
2186 : try {
2187 12052 : doubleValue = StringUtils::toDouble(value);
2188 0 : } catch (NumberFormatException&) {
2189 0 : throw InvalidArgument("Setting parameter '" + key + "' requires a number for laneChangeModel of type '" + toString(myModel) + "'");
2190 0 : }
2191 12052 : if (key == toString(SUMO_ATTR_LCA_STRATEGIC_PARAM)) {
2192 3174 : myStrategicParam = doubleValue;
2193 8878 : } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_PARAM)) {
2194 36 : myCooperativeParam = doubleValue;
2195 8842 : } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_PARAM)) {
2196 0 : mySpeedGainParam = doubleValue;
2197 8842 : } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_PARAM)) {
2198 0 : myKeepRightParam = doubleValue;
2199 8842 : } else if (key == toString(SUMO_ATTR_LCA_OPPOSITE_PARAM)) {
2200 0 : myOppositeParam = doubleValue;
2201 8842 : } else if (key == toString(SUMO_ATTR_LCA_LOOKAHEADLEFT)) {
2202 0 : myLookaheadLeft = doubleValue;
2203 8842 : } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAINRIGHT)) {
2204 0 : mySpeedGainRight = doubleValue;
2205 8842 : } else if (key == toString(SUMO_ATTR_LCA_ASSERTIVE)) {
2206 0 : myAssertive = doubleValue;
2207 8842 : } else if (key == toString(SUMO_ATTR_LCA_OVERTAKE_RIGHT)) {
2208 0 : myOvertakeRightParam = doubleValue;
2209 8842 : } else if (key == toString(SUMO_ATTR_LCA_SIGMA)) {
2210 0 : mySigma = doubleValue;
2211 8842 : } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_ACCEPTANCE_TIME)) {
2212 0 : myKeepRightAcceptanceTime = doubleValue;
2213 8842 : } else if (key == toString(SUMO_ATTR_LCA_OVERTAKE_DELTASPEED_FACTOR)) {
2214 0 : myOvertakeDeltaSpeedFactor = doubleValue;
2215 8842 : } else if (key == toString(SUMO_ATTR_LCA_STRATEGIC_LOOKAHEAD)) {
2216 0 : myStrategicLookahead = doubleValue;
2217 8842 : } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD)) {
2218 2946 : mySpeedGainLookahead = doubleValue;
2219 5896 : } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME)) {
2220 2946 : mySpeedGainRemainTime = doubleValue;
2221 2950 : } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_ROUNDABOUT)) {
2222 0 : myRoundaboutBonus = doubleValue;
2223 2950 : } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_SPEED)) {
2224 0 : myCooperativeSpeed = doubleValue;
2225 2950 : } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATSTANDING)) {
2226 0 : myMaxSpeedLatStanding = doubleValue;
2227 2950 : } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATFACTOR)) {
2228 0 : myMaxSpeedLatFactor = doubleValue;
2229 2950 : } else if (key == toString(SUMO_ATTR_LCA_MAXDISTLATSTANDING)) {
2230 0 : myMaxDistLatStanding = doubleValue;
2231 : // access to internal state
2232 2950 : } else if (key == "speedGainProbabilityRight") {
2233 0 : mySpeedGainProbabilityRight = (long long int)(doubleValue * HYST_PRECISION);
2234 2950 : } else if (key == "speedGainProbabilityLeft") {
2235 0 : mySpeedGainProbabilityLeft = (long long int)(doubleValue * HYST_PRECISION);
2236 2950 : } else if (key == "keepRightProbability") {
2237 0 : myKeepRightProbability = (long long int)(-doubleValue * HYST_PRECISION);
2238 2950 : } else if (key == "lookAheadSpeed") {
2239 0 : myLookAheadSpeed = doubleValue;
2240 : } else {
2241 11800 : throw InvalidArgument("Setting parameter '" + key + "' is not supported for laneChangeModel of type '" + toString(myModel) + "'");
2242 : }
2243 9102 : initDerivedParameters();
2244 9102 : }
2245 :
2246 :
2247 : void
2248 2559 : MSLCM_LC2013::saveState(OutputDevice& out) const {
2249 2559 : MSAbstractLaneChangeModel::saveState(out);
2250 : std::vector<long long int> lcState;
2251 2559 : lcState.push_back(mySpeedGainProbabilityLeft);
2252 2559 : lcState.push_back(mySpeedGainProbabilityRight);
2253 2559 : lcState.push_back(myKeepRightProbability);
2254 2559 : lcState.push_back((long long int)(myLookAheadSpeed * HYST_PRECISION));
2255 2559 : lcState.push_back(myDontBrake);
2256 2559 : out.writeAttr(SUMO_ATTR_LCSTATE2, lcState);
2257 2559 : }
2258 :
2259 :
2260 : void
2261 3502 : MSLCM_LC2013::loadState(const SUMOSAXAttributes& attrs) {
2262 3502 : MSAbstractLaneChangeModel::loadState(attrs);
2263 3502 : if (attrs.hasAttribute(SUMO_ATTR_LCSTATE2)) {
2264 3502 : std::istringstream bis(attrs.getString(SUMO_ATTR_LCSTATE2));
2265 3502 : bis >> mySpeedGainProbabilityLeft;
2266 3502 : bis >> mySpeedGainProbabilityRight;
2267 3502 : bis >> myKeepRightProbability;
2268 : long long laSpeed;
2269 : bis >> laSpeed;
2270 3502 : myLookAheadSpeed = (double)laSpeed / HYST_PRECISION;
2271 3502 : bis >> myDontBrake;
2272 3502 : }
2273 3502 : }
2274 :
2275 :
2276 :
2277 : /****************************************************************************/
|