Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSLCM_SL2015.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2013-2025 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/****************************************************************************/
18// A lane change model for heterogeneous traffic (based on sub-lanes)
19/****************************************************************************/
20#include <config.h>
21
22#include <iostream>
25#include <microsim/MSEdge.h>
26#include <microsim/MSLane.h>
27#include <microsim/MSLink.h>
28#include <microsim/MSNet.h>
30#include <microsim/MSGlobals.h>
31#include <microsim/MSStop.h>
34#include "MSLCHelper.h"
35#include "MSLCM_SL2015.h"
36
37// ===========================================================================
38// variable definitions
39// ===========================================================================
40#define MAGIC_OFFSET 1.
41#define LOOK_FORWARD 10.
42
43#define JAM_FACTOR 1.
44
45#define LCA_RIGHT_IMPATIENCE -1.
46#define CUT_IN_LEFT_SPEED_THRESHOLD 27.
47#define MAX_ONRAMP_LENGTH 200.
48
49#define LOOK_AHEAD_MIN_SPEED 0.0
50#define LOOK_AHEAD_SPEED_MEMORY 0.9
51
52#define HELP_DECEL_FACTOR 1.0
53
54#define HELP_OVERTAKE (10.0 / 3.6)
55#define MIN_FALLBEHIND (7.0 / 3.6)
56
57#define URGENCY 2.0
58
59#define KEEP_RIGHT_TIME 5.0 // the number of seconds after which a vehicle should move to the right lane
60
61#define RELGAIN_NORMALIZATION_MIN_SPEED 10.0
62
63#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
64#define GAIN_PERCEPTION_THRESHOLD 0.05 // the minimum relative speed gain which affects the behavior
65
66#define ARRIVALPOS_LAT_THRESHOLD 100.0
67
68// the speed at which the desired lateral gap grows now further
69#define LATGAP_SPEED_THRESHOLD (50 / 3.6)
70// the speed at which the desired lateral gap shrinks now further.
71// @note: when setting LATGAP_SPEED_THRESHOLD = LATGAP_SPEED_THRESHOLD2, no speed-specif reduction of minGapLat is done
72#define LATGAP_SPEED_THRESHOLD2 (50 / 3.6)
73
74// intention to change decays over time
75#define SPEEDGAIN_DECAY_FACTOR 0.5
76// exponential averaging factor for expected sublane speeds
77#define SPEEDGAIN_MEMORY_FACTOR 0.5
78
79#define REACT_TO_STOPPED_DISTANCE 100
80
81
82// ===========================================================================
83// Debug flags
84// ===========================================================================
85//#define DEBUG_MANEUVER
86//#define DEBUG_WANTSCHANGE
87//#define DEBUG_DECISION
88//#define DEBUG_STRATEGIC_CHANGE
89//#define DEBUG_KEEP_LATGAP
90//#define DEBUG_STATE
91//#define DEBUG_ACTIONSTEPS
92//#define DEBUG_COMMITTED_SPEED
93//#define DEBUG_PATCHSPEED
94//#define DEBUG_INFORM
95//#define DEBUG_ROUNDABOUTS
96//#define DEBUG_COOPERATE
97//#define DEBUG_SLOWDOWN
98//#define DEBUG_SAVE_BLOCKER_LENGTH
99//#define DEBUG_BLOCKING
100//#define DEBUG_TRACI
101//#define DEBUG_EXPECTED_SLSPEED
102//#define DEBUG_SLIDING
103//#define DEBUG_COND (myVehicle.getID() == "moped.18" || myVehicle.getID() == "moped.16")
104//#define DEBUG_COND (myVehicle.getID() == "Togliatti_71_0")
105#define DEBUG_COND (myVehicle.isSelected())
106//#define DEBUG_COND (myVehicle.getID() == "pkw150478" || myVehicle.getID() == "pkw150494" || myVehicle.getID() == "pkw150289")
107//#define DEBUG_COND (myVehicle.getID() == "A" || myVehicle.getID() == "B") // fail change to left
108//#define DEBUG_COND (myVehicle.getID() == "disabled") // test stops_overtaking
109//#define DEBUG_COND true
110
111
112// ===========================================================================
113// member method definitions
114// ===========================================================================
117 mySpeedGainProbabilityRight(0),
118 mySpeedGainProbabilityLeft(0),
119 myKeepRightProbability(0),
120 myLeadingBlockerLength(0),
121 myLeftSpace(0),
122 myLookAheadSpeed(LOOK_AHEAD_MIN_SPEED),
123 myLastEdge(nullptr),
124 myCanChangeFully(true),
125 mySafeLatDistRight(0),
126 mySafeLatDistLeft(0),
127 myStrategicParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_STRATEGIC_PARAM, 1)),
128 myCooperativeParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_PARAM, 1)),
129 mySpeedGainParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_PARAM, 1)),
130 myKeepRightParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_KEEPRIGHT_PARAM, 1)),
131 myOppositeParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_OPPOSITE_PARAM, 1)),
132 mySublaneParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SUBLANE_PARAM, 1)),
133 // by default use SUMO_ATTR_LCA_PUSHY. If that is not set, try SUMO_ATTR_LCA_PUSHYGAP
134 myMinGapLat(v.getVehicleType().getMinGapLat()),
135 myPushy(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_PUSHY,
136 1 - (v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_PUSHYGAP,
137 MAX2(NUMERICAL_EPS, myMinGapLat)) /
138 MAX2(NUMERICAL_EPS, myMinGapLat)))),
139 myImpatience(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_IMPATIENCE, 0)),
140 myMinImpatience(myImpatience),
141 myTimeToImpatience(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_TIME_TO_IMPATIENCE, std::numeric_limits<double>::max())),
142 myAccelLat(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_ACCEL_LAT, 1.0)),
143 myTurnAlignmentDist(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_TURN_ALIGNMENT_DISTANCE, 0.0)),
144 myLookaheadLeft(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_LOOKAHEADLEFT, 2.0)),
145 mySpeedGainRight(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAINRIGHT, 0.1)),
146 myLaneDiscipline(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_LANE_DISCIPLINE, 0.0)),
147 mySpeedGainLookahead(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD, 5)),
148 mySpeedGainRemainTime(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME, 20)),
149 myRoundaboutBonus(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_ROUNDABOUT, myCooperativeParam)),
150 myCooperativeSpeed(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_SPEED, myCooperativeParam)),
151 myKeepRightAcceptanceTime(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_KEEPRIGHT_ACCEPTANCE_TIME, -1)),
152 myOvertakeDeltaSpeedFactor(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_OVERTAKE_DELTASPEED_FACTOR, 0)),
153 mySigmaState(0) {
155}
156
160
161
162void
164 if (mySpeedGainParam <= 0) {
165 myChangeProbThresholdRight = std::numeric_limits<double>::max();
166 myChangeProbThresholdLeft = std::numeric_limits<double>::max();
167 } else {
170 }
172}
173
174
175bool
177 return DEBUG_COND;
178}
179
180
181int
183 int laneOffset,
184 LaneChangeAction alternatives,
185 const MSLeaderDistanceInfo& leaders,
186 const MSLeaderDistanceInfo& followers,
187 const MSLeaderDistanceInfo& blockers,
188 const MSLeaderDistanceInfo& neighLeaders,
189 const MSLeaderDistanceInfo& neighFollowers,
190 const MSLeaderDistanceInfo& neighBlockers,
191 const MSLane& neighLane,
192 const std::vector<MSVehicle::LaneQ>& preb,
193 MSVehicle** lastBlocked,
194 MSVehicle** firstBlocked,
195 double& latDist, double& maneuverDist, int& blocked) {
196
198 const std::string changeType = laneOffset == -1 ? "right" : (laneOffset == 1 ? "left" : "current");
199
200#ifdef DEBUG_MANEUVER
201 if (gDebugFlag2) {
202 std::cout << "\n" << SIMTIME
203 << std::setprecision(gPrecision)
204 << " veh=" << myVehicle.getID()
205 << " lane=" << myVehicle.getLane()->getID()
206 << " neigh=" << neighLane.getID()
207 << " pos=" << myVehicle.getPositionOnLane()
208 << " posLat=" << myVehicle.getLateralPositionOnLane()
209 << " posLatError=" << mySigmaState
210 << " speed=" << myVehicle.getSpeed()
211 << " considerChangeTo=" << changeType
212 << "\n";
213 }
214#endif
215
216 int result = _wantsChangeSublane(laneOffset,
217 alternatives,
218 leaders, followers, blockers,
219 neighLeaders, neighFollowers, neighBlockers,
220 neighLane, preb,
221 lastBlocked, firstBlocked, latDist, maneuverDist, blocked);
222
223 result = keepLatGap(result, leaders, followers, blockers,
224 neighLeaders, neighFollowers, neighBlockers,
225 neighLane, laneOffset, latDist, maneuverDist, blocked);
226
227 result |= getLCA(result, latDist);
228 // take into account lateral acceleration
229#if defined(DEBUG_MANEUVER) || defined(DEBUG_STATE)
230 double latDistTmp = latDist;
231#endif
232 latDist = SPEED2DIST(computeSpeedLat(latDist, maneuverDist, (result & LCA_URGENT) != 0));
233#if defined(DEBUG_MANEUVER) || defined(DEBUG_STATE)
234 if (gDebugFlag2 && latDist != latDistTmp) {
235 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " maneuverDist=" << maneuverDist << " latDist=" << latDistTmp << " mySpeedPrev=" << mySpeedLat << " speedLat=" << DIST2SPEED(latDist) << " latDist2=" << latDist << "\n";
236 }
237
238 if (gDebugFlag2) {
239 if (result & LCA_WANTS_LANECHANGE) {
240 std::cout << SIMTIME
241 << " veh=" << myVehicle.getID()
242 << " wantsChangeTo=" << changeType
243 << " latDist=" << latDist
244 << " maneuverDist=" << maneuverDist
245 << " state=" << toString((LaneChangeAction)result)
246 << ((blocked & LCA_BLOCKED) ? " (blocked)" : "")
247 << ((blocked & LCA_OVERLAPPING) ? " (overlap)" : "")
248 << "\n\n";
249 } else {
250 std::cout << SIMTIME
251 << " veh=" << myVehicle.getID()
252 << " wantsNoChangeTo=" << changeType
253 << " state=" << toString((LaneChangeAction)result)
254 << "\n\n";
255 }
256 }
257#endif
258 gDebugFlag2 = false;
259 return result;
260}
261
262void
265 if (myVehicle.isActive()) {
266 if ((state & (LCA_STRATEGIC | LCA_SPEEDGAIN)) != 0 && (state & LCA_BLOCKED) != 0) {
268 } else {
269 // impatience decays only to the driver-specific level
271 }
272#ifdef DEBUG_STATE
273 if (DEBUG_COND) {
274 std::cout << SIMTIME << " veh=" << myVehicle.getID()
275 << " setOwnState=" << toString((LaneChangeAction)state)
276 << " myMinImpatience=" << myMinImpatience
277 << " myImpatience=" << myImpatience
278 << "\n";
279 }
280#endif
281 if ((state & LCA_STAY) != 0) {
282 myCanChangeFully = true;
283// if (DEBUG_COND) {
284// std::cout << " myCanChangeFully=true\n";
285// }
286 }
287 }
288}
289
290
291void
292MSLCM_SL2015::updateSafeLatDist(const double travelledLatDist) {
293 mySafeLatDistLeft -= travelledLatDist;
294 mySafeLatDistRight += travelledLatDist;
295
296 if (fabs(mySafeLatDistLeft) < NUMERICAL_EPS) {
298 }
299 if (fabs(mySafeLatDistRight) < NUMERICAL_EPS) {
301 }
302}
303
304
305double
306MSLCM_SL2015::patchSpeed(const double min, const double wanted, const double max, const MSCFModel& cfModel) {
308 // negative min speed may be passed when using ballistic updated
309 const double newSpeed = _patchSpeed(MAX2(min, 0.0), wanted, max, cfModel);
310#ifdef DEBUG_PATCHSPEED
311 if (gDebugFlag2) {
312 const std::string patched = (wanted != newSpeed ? " patched=" + toString(newSpeed) : "");
313 std::cout << SIMTIME
314 << " veh=" << myVehicle.getID()
315 << " lane=" << myVehicle.getLane()->getID()
316 << " pos=" << myVehicle.getPositionOnLane()
317 << " v=" << myVehicle.getSpeed()
318 << " min=" << min
319 << " wanted=" << wanted
320 << " max=" << max
321 << patched
322 << "\n\n";
323 }
324#endif
325 gDebugFlag2 = false;
326 return newSpeed;
327}
328
329
330double
331MSLCM_SL2015::_patchSpeed(double min, const double wanted, double max, const MSCFModel& cfModel) {
332 if (wanted <= 0) {
333 return wanted;
334 }
335
336 int state = myOwnState;
337
338 double nVSafe = wanted;
339 bool gotOne = false;
340 // letting vehicles merge in at the end of the lane in case of counter-lane change, step#2
341 // if we want to change and have a blocking leader and there is enough room for him in front of us
342 if (myLeadingBlockerLength != 0) {
344#ifdef DEBUG_PATCHSPEED
345 if (gDebugFlag2) {
346 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " myLeadingBlockerLength=" << myLeadingBlockerLength << " space=" << space << "\n";
347 }
348#endif
349 if (space >= 0) { // XXX space > -MAGIC_OFFSET
350 // compute speed for decelerating towards a place which allows the blocking leader to merge in in front
352 max = MIN2(max, safe);
353 // if we are approaching this place
354 if (safe < wanted) {
355 if (safe < min) {
357 if (safe >= vMinEmergency) {
358 // permit harder braking if needed and helpful
359 min = MAX2(vMinEmergency, safe);
360 }
361 }
362#ifdef DEBUG_PATCHSPEED
363 if (gDebugFlag2) {
364 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " slowing down for leading blocker, safe=" << safe << (safe + NUMERICAL_EPS < min ? " (not enough)" : "") << "\n";
365 }
366#endif
367 nVSafe = MAX2(min, safe);
368 gotOne = true;
369 }
370 }
371 }
372 const double coopWeight = MAX2(0.0, MIN2(1.0, myCooperativeSpeed));
373 for (auto i : myLCAccelerationAdvices) {
374 double accel = i.first;
375 double v = myVehicle.getSpeed() + ACCEL2SPEED(accel);
376 if (v >= min && v <= max) {
377 if (i.second) {
378 // own advice, no scaling needed
379 nVSafe = MIN2(v, nVSafe);
380 } else {
381 nVSafe = MIN2(v * coopWeight + (1 - coopWeight) * wanted, nVSafe);
382 }
383 gotOne = true;
384#ifdef DEBUG_PATCHSPEED
385 if (gDebugFlag2) {
386 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " got accel=" << accel << " nVSafe=" << nVSafe << "\n";
387 }
388#endif
389 } else {
390#ifdef DEBUG_PATCHSPEED
391 if (v < min) {
392 if (gDebugFlag2) {
393 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " ignoring low nVSafe=" << v << " (accel=" << accel << ") min=" << min << "\n";
394 }
395 } else {
396 if (gDebugFlag2) {
397 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " ignoring high nVSafe=" << v << " (accel=" << accel << ") max=" << max << "\n";
398 }
399 }
400#endif
401 }
402 }
403
404 if (gotOne && !myDontBrake) {
405#ifdef DEBUG_PATCHSPEED
406 if (gDebugFlag2) {
407 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " got vSafe\n";
408 }
409#endif
410 return nVSafe;
411 }
412
413 // check whether the vehicle is blocked
414 if ((state & LCA_WANTS_LANECHANGE) != 0 && (state & LCA_BLOCKED) != 0) {
415 if ((state & LCA_STRATEGIC) != 0) {
416 // necessary decelerations are controlled via vSafe. If there are
417 // none it means we should speed up
418#if defined(DEBUG_PATCHSPEED) || defined(DEBUG_STATE)
419 if (gDebugFlag2) {
420 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_WANTS_LANECHANGE (strat, no vSafe)\n";
421 }
422#endif
423 return (max + wanted) / 2.0;
424 } else if ((state & LCA_COOPERATIVE) != 0) {
425 // only minor adjustments in speed should be done
426 if ((state & LCA_BLOCKED_BY_LEADER) != 0) {
427#if defined(DEBUG_PATCHSPEED) || defined(DEBUG_STATE)
428 if (gDebugFlag2) {
429 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_LEADER (coop)\n";
430 }
431#endif
432 return (min + wanted) / 2.0;
433 }
434 if ((state & LCA_BLOCKED_BY_FOLLOWER) != 0) {
435#if defined(DEBUG_PATCHSPEED) || defined(DEBUG_STATE)
436 if (gDebugFlag2) {
437 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_FOLLOWER (coop)\n";
438 }
439#endif
440 return (max + wanted) / 2.0;
441 }
442 //} else { // VARIANT_16
443 // // only accelerations should be performed
444 // if ((state & LCA_BLOCKED_BY_FOLLOWER) != 0) {
445 // if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_BLOCKED_BY_FOLLOWER\n";
446 // return (max + wanted) / 2.0;
447 // }
448 }
449 }
450
451 /*
452 // decelerate if being a blocking follower
453 // (and does not have to change lanes)
454 if ((state & LCA_AMBLOCKINGFOLLOWER) != 0) {
455 if (fabs(max - myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle)) < 0.001 && min == 0) { // !!! was standing
456 if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER (standing)\n";
457 return 0;
458 }
459 if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER\n";
460
461 //return min; // VARIANT_3 (brakeStrong)
462 return (min + wanted) / 2.0;
463 }
464 if ((state & LCA_AMBACKBLOCKER) != 0) {
465 if (max <= myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle) && min == 0) { // !!! was standing
466 if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBACKBLOCKER (standing)\n";
467 //return min; VARIANT_9 (backBlockVSafe)
468 return nVSafe;
469 }
470 }
471 if ((state & LCA_AMBACKBLOCKER_STANDING) != 0) {
472 if (gDebugFlag2) std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBACKBLOCKER_STANDING\n";
473 //return min;
474 return nVSafe;
475 }
476 */
477
478 // accelerate if being a blocking leader or blocking follower not able to brake
479 // (and does not have to change lanes)
480 if ((state & LCA_AMBLOCKINGLEADER) != 0) {
481#if defined(DEBUG_PATCHSPEED) || defined(DEBUG_STATE)
482 if (gDebugFlag2) {
483 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGLEADER\n";
484 }
485#endif
486 return (max + wanted) / 2.0;
487 }
488
489 if ((state & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0) {
490#if defined(DEBUG_PATCHSPEED) || defined(DEBUG_STATE)
491 if (gDebugFlag2) {
492 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " LCA_AMBLOCKINGFOLLOWER_DONTBRAKE\n";
493 }
494#endif
495 /*
496 // VARIANT_4 (dontbrake)
497 if (max <= myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle) && min == 0) { // !!! was standing
498 return wanted;
499 }
500 return (min + wanted) / 2.0;
501 */
502 }
503 return wanted;
504}
505
506
507void*
508MSLCM_SL2015::inform(void* info, MSVehicle* sender) {
509 Info* pinfo = (Info*) info;
510 if (pinfo->first >= 0) {
511 addLCSpeedAdvice(pinfo->first, false);
512 }
513 //myOwnState &= 0xffffffff; // reset all bits of MyLCAEnum but only those
514 myOwnState |= pinfo->second;
515#ifdef DEBUG_INFORM
517 std::cout << SIMTIME
518 << " veh=" << myVehicle.getID()
519 << " informedBy=" << sender->getID()
520 << " info=" << pinfo->second
521 << " vSafe=" << pinfo->first
522 << "\n";
523 }
524#else
525 UNUSED_PARAMETER(sender);
526#endif
527 delete pinfo;
528 return (void*) true;
529}
530
531
532void
533MSLCM_SL2015::msg(const CLeaderDist& cld, double speed, int state) {
534 assert(cld.first != 0);
535 ((MSVehicle*)cld.first)->getLaneChangeModel().inform(new Info(speed, state), &myVehicle);
536}
537
538
539double
541 int dir,
542 const CLeaderDist& neighLead,
543 double remainingSeconds) {
544 double plannedSpeed = MIN2(myVehicle.getSpeed(),
546 for (auto i : myLCAccelerationAdvices) {
547 double v = myVehicle.getSpeed() + ACCEL2SPEED(i.first);
549 plannedSpeed = MIN2(plannedSpeed, v);
550 }
551 }
552#ifdef DEBUG_INFORM
553 if (gDebugFlag2) {
554 std::cout << " informLeader speed=" << myVehicle.getSpeed() << " planned=" << plannedSpeed << "\n";
555 }
556#endif
557
558 if ((blocked & LCA_BLOCKED_BY_LEADER) != 0 && neighLead.first != 0) {
559 const MSVehicle* nv = neighLead.first;
561 //std::cout << SIMTIME << " ego=" << myVehicle.getID() << " ignoresDivergentBlockingLeader=" << nv->getID() << "\n";
562 return plannedSpeed;
563 }
564#ifdef DEBUG_INFORM
565 if (gDebugFlag2) std::cout << " blocked by leader nv=" << nv->getID() << " nvSpeed=" << nv->getSpeed() << " needGap="
567#endif
568 // decide whether we want to overtake the leader or follow it
569 const double dv = plannedSpeed - nv->getSpeed();
570 const double overtakeDist = (neighLead.second // drive to back of follower
571 + nv->getVehicleType().getLengthWithGap() // drive to front of follower
572 + myVehicle.getVehicleType().getLength() // ego back reaches follower front
573 + nv->getCarFollowModel().getSecureGap( // save gap to follower
575
576 if ((dv < myOvertakeDeltaSpeedFactor * myVehicle.getLane()->getSpeedLimit() + NUMERICAL_EPS
577 // overtaking on the right on an uncongested highway is forbidden (noOvertakeLCLeft)
579 // not enough space to overtake? (we will start to brake when approaching a dead end)
581 // not enough time to overtake?
582 || dv * remainingSeconds < overtakeDist)
583 && (!neighLead.first->isStopped() || (isOpposite() && neighLead.second >= 0))) {
584 // cannot overtake
585 msg(neighLead, -1, dir | LCA_AMBLOCKINGLEADER);
586 // slow down smoothly to follow leader
587 const double targetSpeed = getCarFollowModel().followSpeed(
588 &myVehicle, myVehicle.getSpeed(), neighLead.second, nv->getSpeed(), nv->getCarFollowModel().getMaxDecel());
589 if (targetSpeed < myVehicle.getSpeed()) {
590 // slow down smoothly to follow leader
591 const double decel = ACCEL2SPEED(MIN2(myVehicle.getCarFollowModel().getMaxDecel(),
592 MAX2(MIN_FALLBEHIND, (myVehicle.getSpeed() - targetSpeed) / remainingSeconds)));
593 //const double nextSpeed = MAX2(0., MIN2(plannedSpeed, myVehicle.getSpeed() - decel));
594 const double nextSpeed = MIN2(plannedSpeed, MAX2(0.0, myVehicle.getSpeed() - decel));
595#ifdef DEBUG_INFORM
596 if (gDebugFlag2) {
597 std::cout << SIMTIME
598 << " cannot overtake leader nv=" << nv->getID()
599 << " dv=" << dv
600 << " remainingSeconds=" << remainingSeconds
601 << " targetSpeed=" << targetSpeed
602 << " nextSpeed=" << nextSpeed
603 << "\n";
604 }
605#endif
606 addLCSpeedAdvice(nextSpeed);
607 return nextSpeed;
608 } else {
609 // leader is fast enough anyway
610#ifdef DEBUG_INFORM
611 if (gDebugFlag2) {
612 std::cout << SIMTIME
613 << " cannot overtake fast leader nv=" << nv->getID()
614 << " dv=" << dv
615 << " remainingSeconds=" << remainingSeconds
616 << " targetSpeed=" << targetSpeed
617 << "\n";
618 }
619#endif
620 addLCSpeedAdvice(targetSpeed);
621 return plannedSpeed;
622 }
623 } else {
624#ifdef DEBUG_INFORM
625 if (gDebugFlag2) {
626 std::cout << SIMTIME
627 << " wants to overtake leader nv=" << nv->getID()
628 << " dv=" << dv
629 << " remainingSeconds=" << remainingSeconds
630 << " currentGap=" << neighLead.second
632 << " overtakeDist=" << overtakeDist
633 << " leftSpace=" << myLeftSpace
634 << " blockerLength=" << myLeadingBlockerLength
635 << "\n";
636 }
637#endif
638 // overtaking, leader should not accelerate
639 msg(neighLead, nv->getSpeed(), dir | LCA_AMBLOCKINGLEADER);
640 return -1;
641 }
642 } else if (neighLead.first != 0) { // (remainUnblocked)
643 // we are not blocked now. make sure we stay far enough from the leader
644 const MSVehicle* nv = neighLead.first;
645 double dv, nextNVSpeed;
647 // XXX: the decrement (HELP_OVERTAKE) should be scaled with timestep length, I think.
648 // It seems to function as an estimate nv's speed in the next simstep!? (so HELP_OVERTAKE should be an acceleration value.)
649 nextNVSpeed = nv->getSpeed() - HELP_OVERTAKE; // conservative
650 dv = SPEED2DIST(myVehicle.getSpeed() - nextNVSpeed);
651 } else {
652 // Estimate neigh's speed after actionstep length
653 // @note The possible breaking can be underestimated by the formula, so this is a potential
654 // source of collisions if actionsteplength>simsteplength.
655 const double nvMaxDecel = HELP_OVERTAKE;
656 nextNVSpeed = nv->getSpeed() - nvMaxDecel * myVehicle.getActionStepLengthSecs(); // conservative
657 // Estimated gap reduction until next action step if own speed stays constant
658 dv = SPEED2DIST(myVehicle.getSpeed() - nextNVSpeed);
659 }
660 const double targetSpeed = getCarFollowModel().followSpeed(
661 &myVehicle, myVehicle.getSpeed(), neighLead.second - dv, nextNVSpeed, nv->getCarFollowModel().getMaxDecel());
662 addLCSpeedAdvice(targetSpeed);
663#ifdef DEBUG_INFORM
664 if (gDebugFlag2) {
665 std::cout << " not blocked by leader nv=" << nv->getID()
666 << " nvSpeed=" << nv->getSpeed()
667 << " gap=" << neighLead.second
668 << " nextGap=" << neighLead.second - dv
670 << " targetSpeed=" << targetSpeed
671 << "\n";
672 }
673#endif
674 return MIN2(targetSpeed, plannedSpeed);
675 } else {
676 // not overtaking
677 return plannedSpeed;
678 }
679}
680
681
682void
684 int dir,
685 const CLeaderDist& neighFollow,
686 double remainingSeconds,
687 double plannedSpeed) {
688 if ((blocked & LCA_BLOCKED_BY_FOLLOWER) != 0 && neighFollow.first != 0) {
689 const MSVehicle* nv = neighFollow.first;
691 //std::cout << SIMTIME << " ego=" << myVehicle.getID() << " ignoresDivergentBlockingFollower=" << nv->getID() << "\n";
692 return;
693 }
694#ifdef DEBUG_INFORM
695 if (gDebugFlag2) std::cout << " blocked by follower nv=" << nv->getID() << " nvSpeed=" << nv->getSpeed() << " needGap="
697#endif
698
699 // are we fast enough to cut in without any help?
700 if (plannedSpeed - nv->getSpeed() >= HELP_OVERTAKE) {
701 const double neededGap = nv->getCarFollowModel().getSecureGap(nv, &myVehicle, nv->getSpeed(), plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
702 if ((neededGap - neighFollow.second) / remainingSeconds < (plannedSpeed - nv->getSpeed())) {
703#ifdef DEBUG_INFORM
704 if (gDebugFlag2) {
705 std::cout << " wants to cut in before nv=" << nv->getID() << " without any help neededGap=" << neededGap << "\n";
706 }
707#endif
708 // follower might even accelerate but not to much
709 msg(neighFollow, plannedSpeed - HELP_OVERTAKE, dir | LCA_AMBLOCKINGFOLLOWER);
710 return;
711 }
712 }
713 // decide whether we will request help to cut in before the follower or allow to be overtaken
714
715 // PARAMETERS
716 // assume other vehicle will assume the equivalent of 1 second of
717 // maximum deceleration to help us (will probably be spread over
718 // multiple seconds)
719 // -----------
720 const double helpDecel = nv->getCarFollowModel().getMaxDecel() * HELP_DECEL_FACTOR ;
721
722 // change in the gap between ego and blocker over 1 second (not STEP!)
723 const double neighNewSpeed = MAX2(0., nv->getSpeed() - ACCEL2SPEED(helpDecel));
724 const double neighNewSpeed1s = MAX2(0., nv->getSpeed() - helpDecel);
725 const double dv = plannedSpeed - neighNewSpeed1s;
726 // new gap between follower and self in case the follower does brake for 1s
727 const double decelGap = neighFollow.second + dv;
728 const double secureGap = nv->getCarFollowModel().getSecureGap(nv, &myVehicle, neighNewSpeed1s, plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
729#ifdef DEBUG_INFORM
730 if (gDebugFlag2) {
731 std::cout << SIMTIME
732 << " egoV=" << myVehicle.getSpeed()
733 << " egoNV=" << plannedSpeed
734 << " nvNewSpeed=" << neighNewSpeed
735 << " nvNewSpeed1s=" << neighNewSpeed1s
736 << " deltaGap=" << dv
737 << " decelGap=" << decelGap
738 << " secGap=" << secureGap
739 << "\n";
740 }
741#endif
742 if (decelGap > 0 && decelGap >= secureGap) {
743 // if the blocking neighbor brakes it could actually help
744 // how hard does it actually need to be?
745 // to be safe in the next step the following equation has to hold:
746 // vsafe <= followSpeed(gap=currentGap - SPEED2DIST(vsafe), ...)
747 // we compute an upper bound on vsafe by doing the computation twice
748 const double vsafe1 = MAX2(neighNewSpeed, nv->getCarFollowModel().followSpeed(
749 nv, nv->getSpeed(), neighFollow.second + SPEED2DIST(plannedSpeed), plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel()));
750 const double vsafe = MAX2(neighNewSpeed, nv->getCarFollowModel().followSpeed(
751 nv, nv->getSpeed(), neighFollow.second + SPEED2DIST(plannedSpeed - vsafe1), plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel()));
752 // the following assertion cannot be guaranteed because the CFModel handles small gaps differently, see MSCFModel::maximumSafeStopSpeed
753 // assert(vsafe <= vsafe1);
754 msg(neighFollow, vsafe, dir | LCA_AMBLOCKINGFOLLOWER);
755#ifdef DEBUG_INFORM
756 if (gDebugFlag2) {
757 std::cout << " wants to cut in before nv=" << nv->getID()
758 << " vsafe1=" << vsafe1
759 << " vsafe=" << vsafe
760 << " newSecGap=" << nv->getCarFollowModel().getSecureGap(nv, &myVehicle, vsafe, plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel())
761 << "\n";
762 }
763#endif
764 } else if (dv > 0 && dv * remainingSeconds > (secureGap - decelGap + POSITION_EPS)) {
765 // decelerating once is sufficient to open up a large enough gap in time
766 msg(neighFollow, neighNewSpeed, dir | LCA_AMBLOCKINGFOLLOWER);
767#ifdef DEBUG_INFORM
768 if (gDebugFlag2) {
769 std::cout << " wants to cut in before nv=" << nv->getID() << " (eventually)\n";
770 }
771#endif
772 } else if (dir == LCA_MRIGHT && !myAllowOvertakingRight && !nv->congested()) {
773 const double vhelp = MAX2(neighNewSpeed, HELP_OVERTAKE);
774 msg(neighFollow, vhelp, dir | LCA_AMBLOCKINGFOLLOWER);
775#ifdef DEBUG_INFORM
776 if (gDebugFlag2) {
777 std::cout << " wants to cut in before nv=" << nv->getID() << " (nv cannot overtake right)\n";
778 }
779#endif
780 } else {
781 double vhelp = MAX2(nv->getSpeed(), myVehicle.getSpeed() + HELP_OVERTAKE);
782 if (nv->getSpeed() > myVehicle.getSpeed() &&
784 || (dir == LCA_MLEFT && plannedSpeed > CUT_IN_LEFT_SPEED_THRESHOLD) // VARIANT_22 (slowDownLeft)
785 // XXX this is a hack to determine whether the vehicles is on an on-ramp. This information should be retrieved from the network itself
787 )) {
788 // let the follower slow down to increase the likelyhood that later vehicles will be slow enough to help
789 // follower should still be fast enough to open a gap
790 vhelp = MAX2(neighNewSpeed, myVehicle.getSpeed() + HELP_OVERTAKE);
791#ifdef DEBUG_INFORM
792 if (gDebugFlag2) {
793 std::cout << " wants right follower to slow down a bit\n";
794 }
795#endif
796 if ((nv->getSpeed() - myVehicle.getSpeed()) / helpDecel < remainingSeconds) {
797#ifdef DEBUG_INFORM
798 if (gDebugFlag2) {
799 std::cout << " wants to cut in before right follower nv=" << nv->getID() << " (eventually)\n";
800 }
801#endif
802 msg(neighFollow, vhelp, dir | LCA_AMBLOCKINGFOLLOWER);
803 return;
804 }
805 }
806 msg(neighFollow, vhelp, dir | LCA_AMBLOCKINGFOLLOWER);
807 // this follower is supposed to overtake us. slow down smoothly to allow this
808 const double overtakeDist = (neighFollow.second // follower reaches ego back
809 + myVehicle.getVehicleType().getLengthWithGap() // follower reaches ego front
810 + nv->getVehicleType().getLength() // follower back at ego front
811 + myVehicle.getCarFollowModel().getSecureGap( // follower has safe dist to ego
812 &myVehicle, nv, plannedSpeed, vhelp, nv->getCarFollowModel().getMaxDecel()));
813 // speed difference to create a sufficiently large gap
814 const double needDV = overtakeDist / remainingSeconds;
815 // make sure the deceleration is not to strong
817
818#ifdef DEBUG_INFORM
819 if (gDebugFlag2) {
820 std::cout << SIMTIME
821 << " veh=" << myVehicle.getID()
822 << " wants to be overtaken by=" << nv->getID()
823 << " overtakeDist=" << overtakeDist
824 << " vneigh=" << nv->getSpeed()
825 << " vhelp=" << vhelp
826 << " needDV=" << needDV
827 << " vsafe=" << myVehicle.getSpeed() + ACCEL2SPEED(myLCAccelerationAdvices.back().first)
828 << "\n";
829 }
830#endif
831 }
832 } else if (neighFollow.first != 0) {
833 const double vsafe = MSLCHelper::getSpeedPreservingSecureGap(myVehicle, *neighFollow.first, neighFollow.second, plannedSpeed);
834 msg(neighFollow, vsafe, dir | LCA_AMBLOCKINGFOLLOWER);
835#ifdef DEBUG_INFORM
836 if (gDebugFlag2) {
837 std::cout << " wants to cut in before non-blocking follower nv=" << neighFollow.first->getID() << "\n";
838 }
839#endif
840 }
841}
842
843double
844MSLCM_SL2015::informLeaders(int blocked, int dir,
845 const std::vector<CLeaderDist>& blockers,
846 double remainingSeconds) {
847 double plannedSpeed = myVehicle.getSpeed();
848 double space = myLeftSpace;
849 if (myLeadingBlockerLength != 0) {
850 // see patchSpeed @todo: refactor
852 if (space <= 0) {
853 // ignore leading blocker
854 space = myLeftSpace;
855 }
856 }
858 plannedSpeed = MIN2(plannedSpeed, safe);
859
860 for (std::vector<CLeaderDist>::const_iterator it = blockers.begin(); it != blockers.end(); ++it) {
861 plannedSpeed = MIN2(plannedSpeed, informLeader(blocked, dir, *it, remainingSeconds));
862 }
863 return plannedSpeed;
864}
865
866
867void
868MSLCM_SL2015::informFollowers(int blocked, int dir,
869 const std::vector<CLeaderDist>& blockers,
870 double remainingSeconds,
871 double plannedSpeed) {
872 // #3727
873 for (std::vector<CLeaderDist>::const_iterator it = blockers.begin(); it != blockers.end(); ++it) {
874 informFollower(blocked, dir, *it, remainingSeconds, plannedSpeed);
875 }
876}
877
878
879void
882 // keep information about strategic change direction
884#ifdef DEBUG_INFORM
885 if (debugVehicle()) {
886 std::cout << SIMTIME
887 << " veh=" << myVehicle.getID()
888 << " prepareStep"
889 << " myCanChangeFully=" << myCanChangeFully
890 << "\n";
891 }
892#endif
894 myLeftSpace = 0;
896 myDontBrake = false;
897 myCFRelated.clear();
898 myCFRelatedReady = false;
899 const double halfWidth = getWidth() * 0.5;
900 // only permit changing within lane bounds but open up the range depending on the checked duration in _wantsChangeSublane()
903 if (isOpposite()) {
905 }
906 // truncate to work around numerical instability between different builds
907 mySpeedGainProbabilityRight = ceil(mySpeedGainProbabilityRight * 100000.0) * 0.00001;
908 mySpeedGainProbabilityLeft = ceil(mySpeedGainProbabilityLeft * 100000.0) * 0.00001;
909 myKeepRightProbability = ceil(myKeepRightProbability * 100000.0) * 0.00001;
910 // updated myExpectedSublaneSpeeds
911 // XXX only do this when (sub)lane changing is possible
912 std::vector<double> newExpectedSpeeds;
913#ifdef DEBUG_INFORM
914 if (DEBUG_COND) {
915 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " myExpectedSublaneSpeeds=" << toString(myExpectedSublaneSpeeds) << "\n";
916 }
917#endif
919 // initialize
920 const MSEdge* currEdge = &myVehicle.getLane()->getEdge();
921 const std::vector<MSLane*>& lanes = currEdge->getLanes();
922 for (std::vector<MSLane*>::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); ++it_lane) {
923 const int subLanes = MAX2(1, int(ceil((*it_lane)->getWidth() / MSGlobals::gLateralResolution)));
924 for (int i = 0; i < subLanes; ++i) {
925 newExpectedSpeeds.push_back((*it_lane)->getVehicleMaxSpeed(&myVehicle));
926 }
927 }
928 if (currEdge->canChangeToOpposite()) {
929 MSLane* opposite = lanes.back()->getOpposite();
930 const int subLanes = MAX2(1, int(ceil(opposite->getWidth() / MSGlobals::gLateralResolution)));
931 for (int i = 0; i < subLanes; ++i) {
932 newExpectedSpeeds.push_back(lanes.back()->getVehicleMaxSpeed(&myVehicle));
933 }
934 }
935 if (myExpectedSublaneSpeeds.size() > 0) {
936 // copy old values
937 assert(myLastEdge != 0);
938 if (myLastEdge->getSubLaneSides().size() == myExpectedSublaneSpeeds.size()) {
939 const int subLaneShift = computeSublaneShift(myLastEdge, currEdge);
940 if (subLaneShift < std::numeric_limits<int>::max()) {
941 for (int i = 0; i < (int)myExpectedSublaneSpeeds.size(); ++i) {
942 const int newI = i + subLaneShift;
943 if (newI > 0 && newI < (int)newExpectedSpeeds.size()) {
944 newExpectedSpeeds[newI] = myExpectedSublaneSpeeds[i];
945 }
946 }
947 }
948 }
949 }
950 myExpectedSublaneSpeeds = newExpectedSpeeds;
951 myLastEdge = currEdge;
952 }
953 assert(myExpectedSublaneSpeeds.size() == myVehicle.getLane()->getEdge().getSubLaneSides().size());
954 if (mySigma > 0) {
956 }
957}
958
959double
961 //OUProcess::step(double state, double dt, double timeScale, double noiseIntensity)
962 const double deltaState = OUProcess::step(mySigmaState,
964 MAX2(NUMERICAL_EPS, (1 - mySigma) * 100), mySigma) - mySigmaState;
965 const double scaledDelta = deltaState * myVehicle.getSpeed() / myVehicle.getLane()->getSpeedLimit();
966 return scaledDelta;
967}
968
969double
973
974int
975MSLCM_SL2015::computeSublaneShift(const MSEdge* prevEdge, const MSEdge* curEdge) {
976 // find the first lane that targets the new edge
977 int prevShift = 0;
978 for (const MSLane* const lane : prevEdge->getLanes()) {
979 for (const MSLink* const link : lane->getLinkCont()) {
980 if (&link->getLane()->getEdge() == curEdge) {
981 int curShift = 0;
982 const MSLane* target = link->getLane();
983 const std::vector<MSLane*>& lanes2 = curEdge->getLanes();
984 for (std::vector<MSLane*>::const_iterator it_lane2 = lanes2.begin(); it_lane2 != lanes2.end(); ++it_lane2) {
985 const MSLane* lane2 = *it_lane2;
986 if (lane2 == target) {
987 return prevShift + curShift;
988 }
989 MSLeaderInfo ahead(lane2->getWidth());
990 curShift += ahead.numSublanes();
991 }
992 assert(false);
993 }
994 }
995 MSLeaderInfo ahead(lane->getWidth());
996 prevShift -= ahead.numSublanes();
997 }
998 return std::numeric_limits<int>::max();
999}
1000
1001
1002void
1004 if (!myCanChangeFully) {
1005 // do not reset state yet so we can continue our maneuver but acknowledge
1006 // a change to the right (movement should continue due to lane alignment desire)
1007 if (getManeuverDist() < 0) {
1009 }
1010#ifdef DEBUG_STATE
1011 if (DEBUG_COND) {
1012 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " state not reset. maneuverDist=" << getManeuverDist() << "\n";
1013 }
1014#endif
1015 return;
1016 }
1017 myOwnState = 0;
1018 // XX do not reset values for unfinished maneuvers
1022
1023 if (myVehicle.getBestLaneOffset() == 0) {
1024 // if we are not yet on our best lane there might still be unseen blockers
1025 // (during patchSpeed)
1027 myLeftSpace = 0;
1028 }
1031 myDontBrake = false;
1032#if defined(DEBUG_MANEUVER) || defined(DEBUG_STATE)
1033 if (DEBUG_COND) {
1034 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " changed()\n";
1035 }
1036#endif
1037}
1038
1039
1040void
1052
1053
1054int
1056 int laneOffset,
1057 LaneChangeAction alternatives,
1058 const MSLeaderDistanceInfo& leaders,
1059 const MSLeaderDistanceInfo& followers,
1060 const MSLeaderDistanceInfo& blockers,
1061 const MSLeaderDistanceInfo& neighLeaders,
1062 const MSLeaderDistanceInfo& neighFollowers,
1063 const MSLeaderDistanceInfo& neighBlockers,
1064 const MSLane& neighLane,
1065 const std::vector<MSVehicle::LaneQ>& preb,
1066 MSVehicle** lastBlocked,
1067 MSVehicle** firstBlocked,
1068 double& latDist, double& maneuverDist, int& blocked) {
1069
1070 if (laneOffset != 0) {
1071 // update mySafeLatDist w.r.t. the direction being checkd
1072 const double halfWidth = getWidth() * 0.5;
1073 double center = getVehicleCenter();
1074 if (laneOffset < 0) {
1075 mySafeLatDistRight = center - halfWidth;
1076 } else {
1077 mySafeLatDistLeft = getLeftBorder() - center - halfWidth;
1078 }
1079 }
1080
1081 const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
1082 // compute bestLaneOffset
1083 MSVehicle::LaneQ curr, neigh, best;
1084 int bestLaneOffset = 0;
1085 double currentDist = 0;
1086 double neighDist = 0;
1087 const MSLane* prebLane = myVehicle.getLane();
1088 if (prebLane->getEdge().isInternal()) {
1089 // internal edges are not kept inside the bestLanes structure
1090 if (isOpposite()) {
1091 prebLane = prebLane->getNormalPredecessorLane();
1092 } else {
1093 prebLane = prebLane->getLinkCont()[0]->getLane();
1094 }
1095 }
1096 // special case: vehicle considers changing to the opposite direction edge
1097 const bool checkOpposite = &neighLane.getEdge() != &myVehicle.getLane()->getEdge();
1098 const int prebOffset = (checkOpposite ? 0 : laneOffset);
1099 for (int p = 0; p < (int) preb.size(); ++p) {
1100 if (preb[p].lane == prebLane && p + laneOffset >= 0) {
1101 assert(p + prebOffset < (int)preb.size());
1102 curr = preb[p];
1103 neigh = preb[p + prebOffset];
1104 currentDist = curr.length;
1105 neighDist = neigh.length;
1106 bestLaneOffset = curr.bestLaneOffset;
1107 // VARIANT_13 (equalBest)
1108 if (bestLaneOffset == 0 && preb[p + prebOffset].bestLaneOffset == 0 && !checkOpposite) {
1109#ifdef DEBUG_WANTSCHANGE
1110 if (gDebugFlag2) {
1111 std::cout << STEPS2TIME(currentTime)
1112 << " veh=" << myVehicle.getID()
1113 << " bestLaneOffsetOld=" << bestLaneOffset
1114 << " bestLaneOffsetNew=" << laneOffset
1115 << "\n";
1116 }
1117#endif
1118 bestLaneOffset = prebOffset;
1119 }
1120 best = preb[p + bestLaneOffset];
1121 break;
1122 }
1123 }
1124 assert(curr.lane != nullptr);
1125 assert(neigh.lane != nullptr);
1126 assert(best.lane != nullptr);
1127 double driveToNextStop = -std::numeric_limits<double>::max();
1128 UNUSED_PARAMETER(driveToNextStop); // XXX use when computing usableDist
1129 if (myVehicle.nextStopDist() < std::numeric_limits<double>::max()
1131 // vehicle can always drive up to stop distance
1132 // @note this information is dynamic and thus not available in updateBestLanes()
1133 // @note: nextStopDist was compute before the vehicle moved
1134 driveToNextStop = myVehicle.nextStopDist();
1135 const double stopPos = getForwardPos() + myVehicle.nextStopDist() - myVehicle.getLastStepDist();
1136#ifdef DEBUG_WANTS_CHANGE
1137 if (DEBUG_COND) {
1138 std::cout << SIMTIME << std::setprecision(gPrecision) << " veh=" << myVehicle.getID()
1139 << " stopDist=" << myVehicle.nextStopDist()
1140 << " lastDist=" << myVehicle.getLastStepDist()
1141 << " stopPos=" << stopPos
1142 << " currentDist=" << currentDist
1143 << " neighDist=" << neighDist
1144 << "\n";
1145 }
1146#endif
1147 currentDist = MAX2(currentDist, stopPos);
1148 neighDist = MAX2(neighDist, stopPos);
1149 }
1150 // direction specific constants
1151 const bool right = (laneOffset == -1);
1152 const bool left = (laneOffset == 1);
1153 const int myLca = (right ? LCA_MRIGHT : (left ? LCA_MLEFT : 0));
1154 const int lcaCounter = (right ? LCA_LEFT : (left ? LCA_RIGHT : LCA_NONE));
1155 const bool changeToBest = (right && bestLaneOffset < 0) || (left && bestLaneOffset > 0) || (laneOffset == 0 && bestLaneOffset == 0);
1156 // keep information about being a leader/follower but remove information
1157 // about previous lane change request or urgency
1158 int ret = (myOwnState & 0xffff0000);
1159
1160 // compute the distance when changing to the neighboring lane
1161 // (ensure we do not lap into the line behind neighLane since there might be unseen blockers)
1162 // minimum distance to move the vehicle fully onto the new lane
1163 double latLaneDist = laneOffset == 0 ? 0. : myVehicle.lateralDistanceToLane(laneOffset);
1164
1165 // VARIANT_5 (disableAMBACKBLOCKER1)
1166 /*
1167 if (leader.first != 0
1168 && (myOwnState & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0
1169 && (leader.first->getLaneChangeModel().getOwnState() & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0) {
1170
1171 myOwnState &= (0xffffffff - LCA_AMBLOCKINGFOLLOWER_DONTBRAKE);
1172 if (myVehicle.getSpeed() > SUMO_const_haltingSpeed) {
1173 myOwnState |= LCA_AMBACKBLOCKER;
1174 } else {
1175 ret |= LCA_AMBACKBLOCKER;
1176 myDontBrake = true;
1177 }
1178 }
1179 */
1180
1181#ifdef DEBUG_WANTSCHANGE
1182 if (gDebugFlag2) {
1183 std::cout << STEPS2TIME(currentTime)
1184 << " veh=" << myVehicle.getID()
1185 << " myState=" << toString((LaneChangeAction)myOwnState)
1186 << " firstBlocked=" << Named::getIDSecure(*firstBlocked)
1187 << " lastBlocked=" << Named::getIDSecure(*lastBlocked)
1188 << "\n leaders=" << leaders.toString()
1189 << "\n followers=" << followers.toString()
1190 << "\n blockers=" << blockers.toString()
1191 << "\n neighLeaders=" << neighLeaders.toString()
1192 << "\n neighFollowers=" << neighFollowers.toString()
1193 << "\n neighBlockers=" << neighBlockers.toString()
1194 << "\n changeToBest=" << changeToBest
1195 << " latLaneDist=" << latLaneDist
1196 << " alts=" << toString((LaneChangeAction)alternatives)
1197 << "\n expectedSpeeds=" << toString(myExpectedSublaneSpeeds)
1198 << std::endl;
1199 }
1200#endif
1201
1202 ret = slowDownForBlocked(lastBlocked, ret);
1203 // VARIANT_14 (furtherBlock)
1204 if (lastBlocked != firstBlocked) {
1205 ret = slowDownForBlocked(firstBlocked, ret);
1206 }
1207
1208
1209 // we try to estimate the distance which is necessary to get on a lane
1210 // we have to get on in order to keep our route
1211 // we assume we need something that depends on our velocity
1212 // and compare this with the free space on our wished lane
1213 //
1214 // if the free space is somehow less than the space we need, we should
1215 // definitely try to get to the desired lane
1216 //
1217 // this rule forces our vehicle to change the lane if a lane changing is necessary soon
1218 // lookAheadDistance:
1219 // we do not want the lookahead distance to change all the time so we discrectize the speed a bit
1220
1221 // VARIANT_18 (laHyst)
1224 } else {
1225 // FIXME: This strongly dependent on the value of TS, see LC2013 for the fix (l.1153, currently)
1228 }
1229 //myLookAheadSpeed = myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle);
1230
1231 //double laDist = laSpeed > LOOK_FORWARD_SPEED_DIVIDER
1232 // ? laSpeed * LOOK_FORWARD_FAR
1233 // : laSpeed * LOOK_FORWARD_NEAR;
1234 double laDist = myLookAheadSpeed * LOOK_FORWARD * myStrategicParam * (right ? 1 : myLookaheadLeft);
1235 laDist += myVehicle.getVehicleType().getLengthWithGap() * 2.;
1236 // aggressive drivers may elect to use reduced strategic lookahead to optimize speed
1237 /*
1238 if (mySpeedGainProbabilityRight > myChangeProbThresholdRight
1239 || mySpeedGainProbabilityLeft > myChangeProbThresholdLeft) {
1240 laDist *= MAX2(0.0, (1 - myPushy));
1241 laDist *= MAX2(0,0, (1 - myAssertive));
1242 laDist *= MAX2(0,0, (2 - mySpeedGainParam));
1243 }
1244 */
1245
1246 // react to a stopped leader on the current lane
1247 if (bestLaneOffset == 0 && leaders.hasStoppedVehicle()) {
1248 // value is doubled for the check since we change back and forth
1249 // laDist = 0.5 * (myVehicle.getVehicleType().getLengthWithGap() + leader.first->getVehicleType().getLengthWithGap());
1250 // XXX determine length of longest stopped vehicle
1252 } else if (checkOpposite && isOpposite() && neighLeaders.hasStoppedVehicle()) {
1253 // compute exact distance to overtake stopped vehicle
1254 laDist = 0;
1255 for (int i = 0; i < neighLeaders.numSublanes(); ++i) {
1256 CLeaderDist vehDist = neighLeaders[i];
1257 if (vehDist.first != nullptr && vehDist.first->isStopped()) {
1258 laDist = MAX2(laDist, myVehicle.getVehicleType().getMinGap() + vehDist.second + vehDist.first->getVehicleType().getLengthWithGap());
1259 }
1260 }
1261 laDist += myVehicle.getVehicleType().getLength();
1262 }
1263 if (myStrategicParam < 0) {
1264 laDist = -1e3; // never perform strategic change
1265 }
1266
1267 // free space that is available for changing
1268 //const double neighSpeed = (neighLead.first != 0 ? neighLead.first->getSpeed() :
1269 // neighFollow.first != 0 ? neighFollow.first->getSpeed() :
1270 // best.lane->getSpeedLimit());
1271 // @note: while this lets vehicles change earlier into the correct direction
1272 // it also makes the vehicles more "selfish" and prevents changes which are necessary to help others
1273
1274 const double roundaboutBonus = MSLCHelper::getRoundaboutDistBonus(myVehicle, myRoundaboutBonus, curr, neigh, best);
1275 currentDist += roundaboutBonus;
1276 neighDist += roundaboutBonus;
1277
1278 if (laneOffset != 0) {
1279 ret = checkStrategicChange(ret,
1280 neighLane,
1281 laneOffset,
1282 leaders,
1283 neighLeaders,
1284 curr, neigh, best,
1285 bestLaneOffset,
1286 changeToBest,
1287 currentDist,
1288 neighDist,
1289 laDist,
1290 roundaboutBonus,
1291 latLaneDist,
1292 checkOpposite,
1293 latDist);
1294 }
1295
1296 if ((ret & LCA_STAY) != 0 && latDist == 0) {
1297 // ensure that mySafeLatDistLeft / mySafeLatDistRight are up to date for the
1298 // subsquent check with laneOffset = 0
1299 const double center = myVehicle.getCenterOnEdge();
1300 const double neighRight = getNeighRight(neighLane);
1301 updateGaps(neighLeaders, neighRight, center, 1.0, mySafeLatDistRight, mySafeLatDistLeft);
1302 updateGaps(neighFollowers, neighRight, center, 1.0, mySafeLatDistRight, mySafeLatDistLeft);
1303 // remove TraCI flags because it should not be included in "state-without-traci"
1304 ret = getCanceledState(laneOffset);
1305 return ret;
1306 }
1307 if ((ret & LCA_URGENT) != 0) {
1308 // prepare urgent lane change maneuver
1309 if (changeToBest && abs(bestLaneOffset) > 1
1310 && curr.bestContinuations.back()->getLinkCont().size() != 0
1311 ) {
1312 // there might be a vehicle which needs to counter-lane-change one lane further and we cannot see it yet
1313 const double reserve = MIN2(myLeftSpace - MAGIC_OFFSET - myVehicle.getVehicleType().getMinGap(), getExtraReservation(bestLaneOffset, neighDist - currentDist));
1315#ifdef DEBUG_WANTSCHANGE
1316 if (gDebugFlag2) {
1317 std::cout << " reserving space for unseen blockers myLeadingBlockerLength=" << myLeadingBlockerLength << "\n";
1318 }
1319#endif
1320 }
1321
1322 // letting vehicles merge in at the end of the lane in case of counter-lane change, step#1
1323 // if there is a leader and he wants to change to the opposite direction
1324 MSVehicle* neighLeadLongest = const_cast<MSVehicle*>(getLongest(neighLeaders).first);
1325 const bool canContinue = curr.bestContinuations.size() > 1;
1326#ifdef DEBUG_WANTSCHANGE
1327 if (DEBUG_COND) {
1328 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " neighLeaders=" << neighLeaders.toString() << " longest=" << Named::getIDSecure(neighLeadLongest) << " firstBlocked=" << Named::getIDSecure(*firstBlocked) << "\n";
1329 }
1330#endif
1331 bool canReserve = MSLCHelper::updateBlockerLength(myVehicle, neighLeadLongest, lcaCounter, myLeftSpace - MAGIC_OFFSET, canContinue, myLeadingBlockerLength);
1332 if (*firstBlocked != neighLeadLongest && tieBrakeLeader(*firstBlocked)) {
1333 canReserve &= MSLCHelper::updateBlockerLength(myVehicle, *firstBlocked, lcaCounter, myLeftSpace - MAGIC_OFFSET, canContinue, myLeadingBlockerLength);
1334 }
1335 if (!canReserve && !isOpposite()) {
1336 // we have a low-priority relief connection
1337 // std::cout << SIMTIME << " veh=" << myVehicle.getID() << " cannotReserve for blockers\n";
1338 myDontBrake = canContinue;
1339 }
1340
1341 std::vector<CLeaderDist> collectLeadBlockers;
1342 std::vector<CLeaderDist> collectFollowBlockers;
1343 int blockedFully = 0; // wether execution of the full maneuver is blocked
1344 maneuverDist = latDist;
1345 const double gapFactor = computeGapFactor(LCA_STRATEGIC);
1346 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
1347 leaders, followers, blockers,
1348 neighLeaders, neighFollowers, neighBlockers, &collectLeadBlockers, &collectFollowBlockers,
1349 false, gapFactor, &blockedFully);
1350
1351 const double absLaneOffset = fabs(bestLaneOffset != 0 ? bestLaneOffset : latDist / SUMO_const_laneWidth);
1352 const double remainingSeconds = ((ret & LCA_TRACI) == 0 ?
1353 MAX2(STEPS2TIME(TS), myLeftSpace / MAX2(myLookAheadSpeed, NUMERICAL_EPS) / absLaneOffset / URGENCY) :
1355 const double plannedSpeed = informLeaders(blocked, myLca, collectLeadBlockers, remainingSeconds);
1356 // coordinate with direct obstructions
1357 if (plannedSpeed >= 0) {
1358 // maybe we need to deal with a blocking follower
1359 informFollowers(blocked, myLca, collectFollowBlockers, remainingSeconds, plannedSpeed);
1360 }
1361 if (plannedSpeed > 0) {
1362 commitManoeuvre(blocked, blockedFully, leaders, neighLeaders, neighLane, maneuverDist);
1363 }
1364#if defined(DEBUG_WANTSCHANGE) || defined(DEBUG_STATE)
1365 if (gDebugFlag2) {
1366 std::cout << STEPS2TIME(currentTime)
1367 << " veh=" << myVehicle.getID()
1368 << " myLeftSpace=" << myLeftSpace
1369 << " changeFully=" << myCanChangeFully
1370 << " blockedFully=" << toString((LaneChangeAction)blockedFully)
1371 << " remainingSeconds=" << remainingSeconds
1372 << " plannedSpeed=" << plannedSpeed
1373 << " mySafeLatDistRight=" << mySafeLatDistRight
1374 << " mySafeLatDistLeft=" << mySafeLatDistLeft
1375 << "\n";
1376 }
1377#endif
1378 // remove TraCI flags because it should not be included in "state-without-traci"
1379 ret = getCanceledState(laneOffset);
1380 return ret;
1381 }
1382 // VARIANT_15
1383 if (roundaboutBonus > 0) {
1384
1385#ifdef DEBUG_WANTS_CHANGE
1386 if (DEBUG_COND) {
1387 std::cout << STEPS2TIME(currentTime)
1388 << " veh=" << myVehicle.getID()
1389 << " roundaboutBonus=" << roundaboutBonus
1390 << " myLeftSpace=" << myLeftSpace
1391 << "\n";
1392 }
1393#endif
1394 // try to use the inner lanes of a roundabout to increase throughput
1395 // unless we are approaching the exit
1396 if (left) {
1397 ret |= LCA_COOPERATIVE;
1398 if (!cancelRequest(ret | LCA_LEFT, laneOffset)) {
1399 if ((ret & LCA_STAY) == 0) {
1400 latDist = latLaneDist;
1401 maneuverDist = latLaneDist;
1402 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
1403 leaders, followers, blockers,
1404 neighLeaders, neighFollowers, neighBlockers);
1405 }
1406 return ret;
1407 } else {
1408 ret &= ~LCA_COOPERATIVE;
1409 }
1410 } else {
1412 }
1413 }
1414
1415 // --------
1416
1417 // -------- make place on current lane if blocking follower
1418 //if (amBlockingFollowerPlusNB()) {
1419 // std::cout << myVehicle.getID() << ", " << currentDistAllows(neighDist, bestLaneOffset, laDist)
1420 // << " neighDist=" << neighDist
1421 // << " currentDist=" << currentDist
1422 // << "\n";
1423 //}
1424 const double inconvenience = (latLaneDist < 0
1427#ifdef DEBUG_COOPERATE
1428 if (gDebugFlag2) {
1429 std::cout << STEPS2TIME(currentTime)
1430 << " veh=" << myVehicle.getID()
1431 << " amBlocking=" << amBlockingFollowerPlusNB()
1432 << " state=" << toString((LaneChangeAction)myOwnState)
1433 << " myLca=" << toString((LaneChangeAction)myLca)
1434 << " prevState=" << toString((LaneChangeAction)myPreviousState)
1435 << " inconvenience=" << inconvenience
1436 << " origLatDist=" << getManeuverDist()
1437 << " wantsChangeToHelp=" << (right ? "right" : "left")
1438 << " state=" << myOwnState
1439 << "\n";
1440 }
1441#endif
1442
1443 if (laneOffset != 0
1445 // VARIANT_6 : counterNoHelp
1446 && ((myOwnState & myLca) != 0))
1447 ||
1448 // continue previous cooperative change
1451 // change is in the right direction
1452 && (laneOffset * getManeuverDist() > 0)))
1453 && (inconvenience < myCooperativeParam)
1454 && (changeToBest || currentDistAllows(neighDist, abs(bestLaneOffset) + 1, laDist))) {
1455
1456 // VARIANT_2 (nbWhenChangingToHelp)
1457#ifdef DEBUG_COOPERATE
1458 if (gDebugFlag2) {
1459 std::cout << " wants cooperative change\n";
1460 }
1461#endif
1462
1463 ret |= LCA_COOPERATIVE | LCA_URGENT ;//| LCA_CHANGE_TO_HELP;
1464 if (!cancelRequest(ret | getLCA(ret, latLaneDist), laneOffset)) {
1465 latDist = amBlockingFollowerPlusNB() ? latLaneDist : getManeuverDist();
1466 maneuverDist = latDist;
1467 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
1468 leaders, followers, blockers,
1469 neighLeaders, neighFollowers, neighBlockers);
1470 return ret;
1471 } else {
1472 ret &= ~(LCA_COOPERATIVE | LCA_URGENT);
1473 }
1474 }
1475
1476 // --------
1477
1478
1481 //if ((blocked & LCA_BLOCKED) != 0) {
1482 // return ret;
1483 //}
1485
1486 // -------- higher speed
1487 //if ((congested(neighLead.first) && neighLead.second < 20) || predInteraction(leader.first)) { //!!!
1488 // return ret;
1489 //}
1490
1491 // iterate over all possible combinations of sublanes this vehicle might cover and check the potential speed
1492 const MSEdge& edge = (isOpposite() ? myVehicle.getLane()->getParallelOpposite() : myVehicle.getLane())->getEdge();
1493 const std::vector<double>& sublaneSides = edge.getSubLaneSides();
1494 assert(sublaneSides.size() == myExpectedSublaneSpeeds.size());
1495 const double vehWidth = getWidth();
1496 const double rightVehSide = getVehicleCenter() - 0.5 * vehWidth;
1497 const double leftVehSide = rightVehSide + vehWidth;
1498 // figure out next speed when staying where we are
1499 double defaultNextSpeed = std::numeric_limits<double>::max();
1501 int leftmostOnEdge = (int)sublaneSides.size() - 1;
1502 while (leftmostOnEdge > 0 && sublaneSides[leftmostOnEdge] > leftVehSide) {
1503 leftmostOnEdge--;
1504 }
1505 int rightmostOnEdge = leftmostOnEdge;
1506 while (rightmostOnEdge > 0 && sublaneSides[rightmostOnEdge] > rightVehSide + NUMERICAL_EPS) {
1507 defaultNextSpeed = MIN2(defaultNextSpeed, myExpectedSublaneSpeeds[rightmostOnEdge]);
1508#ifdef DEBUG_WANTSCHANGE
1509 if (gDebugFlag2) {
1510 std::cout << " adapted to current sublane=" << rightmostOnEdge << " defaultNextSpeed=" << defaultNextSpeed << "\n";
1511 std::cout << " sublaneSides[rightmostOnEdge]=" << sublaneSides[rightmostOnEdge] << " rightVehSide=" << rightVehSide << "\n";
1512 }
1513#endif
1514 rightmostOnEdge--;
1515 }
1516 defaultNextSpeed = MIN2(defaultNextSpeed, myExpectedSublaneSpeeds[rightmostOnEdge]);
1517#ifdef DEBUG_WANTSCHANGE
1518 if (gDebugFlag2) {
1519 std::cout << " adapted to current sublane=" << rightmostOnEdge << " defaultNextSpeed=" << defaultNextSpeed << "\n";
1520 std::cout << " sublaneSides[rightmostOnEdge]=" << sublaneSides[rightmostOnEdge] << " rightVehSide=" << rightVehSide << "\n";
1521 }
1522#endif
1523 double maxGain = -std::numeric_limits<double>::max();
1524 double maxGainRight = -std::numeric_limits<double>::max();
1525 double maxGainLeft = -std::numeric_limits<double>::max();
1526 double latDistNice = std::numeric_limits<double>::max();
1527
1528 const int iMin = MIN2(myVehicle.getLane()->getRightmostSublane(), neighLane.getRightmostSublane());
1529 double leftMax = MAX2(
1531 neighLane.getRightSideOnEdge() + neighLane.getWidth());
1532 double rightMin = MIN2(myVehicle.getLane()->getRightSideOnEdge(), neighLane.getRightSideOnEdge());
1533 if (checkOpposite || isOpposite()) {
1534 leftMax = getLeftBorder();
1535 } else {
1536 assert(leftMax <= edge.getWidth());
1537 }
1538 int sublaneCompact = MAX2(iMin, rightmostOnEdge - 1); // try to compactify to the right by default
1539
1540 const double laneBoundary = laneOffset < 0 ? myVehicle.getLane()->getRightSideOnEdge() : neighLane.getRightSideOnEdge();
1541 // if there is a neighboring lane we could change to, check sublanes on all lanes of the edge
1542 // but restrict maneuver to the currently visible lanes (current, neigh) to ensure safety
1543 // This way we can discover a fast lane beyond the immediate neighbor lane
1544 const double maxLatDist = leftMax - leftVehSide;
1545 const double minLatDist = rightMin - rightVehSide;
1546 const int iStart = laneOffset == 0 ? iMin : 0;
1547 const double rightEnd = laneOffset == 0 ? leftMax : (checkOpposite ? getLeftBorder() : edge.getWidth());
1548#ifdef DEBUG_WANTSCHANGE
1549 if (gDebugFlag2) std::cout
1550 << " checking sublanes rightmostOnEdge=" << rightmostOnEdge
1551 << " rightEnd=" << rightEnd
1552 << " leftmostOnEdge=" << leftmostOnEdge
1553 << " iStart=" << iStart
1554 << " iMin=" << iMin
1555 << " sublaneSides=" << sublaneSides.size()
1556 << " leftMax=" << leftMax
1557 << " minLatDist=" << minLatDist
1558 << " maxLatDist=" << maxLatDist
1559 << " sublaneCompact=" << sublaneCompact
1560 << "\n";
1561#endif
1562 for (int i = iStart; i < (int)sublaneSides.size(); ++i) {
1563 if (sublaneSides[i] + vehWidth < rightEnd) {
1564 // i is the rightmost sublane and the left side of vehicles still fits on the edge,
1565 // compute min speed of all sublanes covered by the vehicle in this case
1566 double vMin = myExpectedSublaneSpeeds[i];
1567 //std::cout << " i=" << i << "\n";
1568 int j = i;
1569 while (vMin > 0 && j < (int)sublaneSides.size() && sublaneSides[j] < sublaneSides[i] + vehWidth) {
1570 vMin = MIN2(vMin, myExpectedSublaneSpeeds[j]);
1571#ifdef DEBUG_WANTSCHANGE
1572 if (gDebugFlag2) {
1573 //std::cout << " j=" << j << " vMin=" << vMin << " sublaneSides[j]=" << sublaneSides[j] << " leftVehSide=" << leftVehSide << " rightVehSide=" << rightVehSide << "\n";
1574 }
1575#endif
1576 ++j;
1577 }
1578 // check whether the vehicle is between lanes
1579 if (laneOffset != 0 && overlap(sublaneSides[i], sublaneSides[i] + vehWidth, laneBoundary, laneBoundary)) {
1580 vMin *= (1 - myLaneDiscipline);
1581 }
1582 double relativeGain = (vMin - defaultNextSpeed) / MAX2(vMin, RELGAIN_NORMALIZATION_MIN_SPEED);
1583 double currentLatDist = sublaneSides[i] - rightVehSide;
1584 if ((laneOffset == 0 && (currentLatDist > maxLatDist || currentLatDist < minLatDist))
1585 || (laneOffset < 0 && currentLatDist > maxLatDist)
1586 || (laneOffset > 0 && currentLatDist < minLatDist)) {
1587#ifdef DEBUG_WANTSCHANGE
1588 if (gDebugFlag2) {
1589 std::cout << " i=" << i << " currentLatDist=" << currentLatDist << " outOfBounds\n";
1590 }
1591#endif
1592 continue;
1593 }
1594 currentLatDist = MIN2(MAX2(currentLatDist, minLatDist), maxLatDist);
1595 if (currentLatDist > 0 && myVehicle.getLane()->getBidiLane() != nullptr) {
1596 // penalize overtaking on the left if the lane is used in both
1597 // directions
1598 relativeGain *= 0.5;
1599 }
1600 // @note only consider change if it is compatible with the current direction (same sign or laneOffset == 0)
1601 if (relativeGain > maxGain && currentLatDist * laneOffset >= 0) {
1602 maxGain = relativeGain;
1603 if (maxGain > GAIN_PERCEPTION_THRESHOLD) {
1604 sublaneCompact = i;
1605 latDist = currentLatDist;
1606#ifdef DEBUG_WANTSCHANGE
1607 if (gDebugFlag2) {
1608 std::cout << " i=" << i << " vMin=" << vMin << " newLatDist=" << latDist << " relGain=" << relativeGain << "\n";
1609 }
1610#endif
1611 }
1612 } else {
1613 // if anticipated gains to the left are higher then to the right and current gains are equal, prefer left
1614 if (currentLatDist > 0
1615 //&& latDist < 0 // #7184 compensates for #7185
1617 && relativeGain > GAIN_PERCEPTION_THRESHOLD
1618 && maxGain - relativeGain < NUMERICAL_EPS) {
1619 latDist = currentLatDist;
1620 }
1621 }
1622#ifdef DEBUG_WANTSCHANGE
1623 if (gDebugFlag2) {
1624 std::cout << " i=" << i << " rightmostOnEdge=" << rightmostOnEdge << " vMin=" << vMin << " relGain=" << relativeGain << " sublaneCompact=" << sublaneCompact << " curLatDist=" << currentLatDist << "\n";
1625 }
1626#endif
1627 if (currentLatDist < -NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
1628 maxGainRight = MAX2(maxGainRight, relativeGain);
1629 } else if (currentLatDist > NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
1630 maxGainLeft = MAX2(maxGainLeft, relativeGain);
1631 }
1632 const double subAlignDist = sublaneSides[i] - rightVehSide;
1633 if (fabs(subAlignDist) < fabs(latDistNice)) {
1634 latDistNice = subAlignDist;
1635#ifdef DEBUG_WANTSCHANGE
1636 if (gDebugFlag2) std::cout
1637 << " nicest sublane=" << i
1638 << " side=" << sublaneSides[i]
1639 << " rightSide=" << rightVehSide
1640 << " latDistNice=" << latDistNice
1641 << " maxGainR=" << (maxGainRight == -std::numeric_limits<double>::max() ? "n/a" : toString(maxGainRight))
1642 << " maxGainL=" << (maxGainLeft == -std::numeric_limits<double>::max() ? "n/a" : toString(maxGainLeft))
1643 << "\n";
1644#endif
1645 }
1646 }
1647 }
1648 // updated change probabilities
1649 if (maxGainRight != -std::numeric_limits<double>::max()) {
1650#ifdef DEBUG_WANTSCHANGE
1651 if (gDebugFlag2) {
1652 std::cout << " speedGainR_old=" << mySpeedGainProbabilityRight;
1653 }
1654#endif
1656#ifdef DEBUG_WANTSCHANGE
1657 if (gDebugFlag2) {
1658 std::cout << " speedGainR_new=" << mySpeedGainProbabilityRight << "\n";
1659 }
1660#endif
1661 }
1662 if (maxGainLeft != -std::numeric_limits<double>::max()) {
1663#ifdef DEBUG_WANTSCHANGE
1664 if (gDebugFlag2) {
1665 std::cout << " speedGainL_old=" << mySpeedGainProbabilityLeft;
1666 }
1667#endif
1669#ifdef DEBUG_WANTSCHANGE
1670 if (gDebugFlag2) {
1671 std::cout << " speedGainL_new=" << mySpeedGainProbabilityLeft << "\n";
1672 }
1673#endif
1674 }
1675 // decay if there is no reason for or against changing (only if we have enough information)
1676 if ((fabs(maxGainRight) < NUMERICAL_EPS || maxGainRight == -std::numeric_limits<double>::max())
1677 && (right || (alternatives & LCA_RIGHT) == 0)) {
1679 }
1680 if ((fabs(maxGainLeft) < NUMERICAL_EPS || maxGainLeft == -std::numeric_limits<double>::max())
1681 && (left || (alternatives & LCA_LEFT) == 0)) {
1683 }
1684
1685
1686#ifdef DEBUG_WANTSCHANGE
1687 if (gDebugFlag2) std::cout << SIMTIME
1688 << " veh=" << myVehicle.getID()
1689 << " defaultNextSpeed=" << defaultNextSpeed
1690 << " maxGain=" << maxGain
1691 << " maxGainRight=" << maxGainRight
1692 << " maxGainLeft=" << maxGainLeft
1693 << " probRight=" << mySpeedGainProbabilityRight
1694 << " probLeft=" << mySpeedGainProbabilityLeft
1695 << " latDist=" << latDist
1696 << " latDistNice=" << latDistNice
1697 << " sublaneCompact=" << sublaneCompact
1698 << "\n";
1699#endif
1700
1701 if (!left) {
1702 // ONLY FOR CHANGING TO THE RIGHT
1703 // start keepRight maneuver when no speed loss is expected and continue
1704 // started maneuvers if the loss isn't too big
1705 if (right && myVehicle.getSpeed() > 0 && (maxGainRight >= 0
1706 || ((myPreviousState & LCA_KEEPRIGHT) != 0 && maxGainRight >= -myKeepRightParam))) {
1707 // honor the obligation to keep right (Rechtsfahrgebot)
1708 const double vMax = myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle);
1709 const double roadSpeedFactor = vMax / myVehicle.getLane()->getSpeedLimit(); // differse from speedFactor if vMax < speedLimit
1710 double acceptanceTime;
1711 if (myKeepRightAcceptanceTime == -1) {
1712 // legacy behavior: scale acceptance time with current speed and
1713 // use old hard-coded constant
1714 acceptanceTime = 7 * roadSpeedFactor * MAX2(1.0, myVehicle.getSpeed());
1715 } else {
1716 acceptanceTime = myKeepRightAcceptanceTime * roadSpeedFactor;
1717 if (followers.hasVehicles()) {
1718 // reduce acceptanceTime if a follower vehicle is faster or wants to drive faster
1719 double minFactor = 1.0;
1720 for (int i = 0; i < followers.numSublanes(); ++i) {
1721 CLeaderDist follower = followers[i];
1722 if (follower.first != nullptr && follower.second < 2 * follower.first->getCarFollowModel().brakeGap(follower.first->getSpeed())) {
1723 if (follower.first->getSpeed() >= myVehicle.getSpeed()) {
1724 double factor = MAX2(1.0, myVehicle.getSpeed()) / MAX2(1.0, follower.first->getSpeed());
1725 const double fRSF = follower.first->getLane()->getVehicleMaxSpeed(follower.first) / follower.first->getLane()->getSpeedLimit();
1726 if (fRSF > roadSpeedFactor) {
1727 factor /= fRSF;
1728 }
1729 if (factor < minFactor) {
1730 minFactor = factor;
1731 }
1732 }
1733 }
1734 }
1735 acceptanceTime *= minFactor;
1736 }
1737 }
1738 double fullSpeedGap = MAX2(0., neighDist - myVehicle.getCarFollowModel().brakeGap(vMax));
1739 double fullSpeedDrivingSeconds = MIN2(acceptanceTime, fullSpeedGap / vMax);
1740 CLeaderDist neighLead = getSlowest(neighLeaders);
1741 if (neighLead.first != 0 && neighLead.first->getSpeed() < vMax) {
1742 fullSpeedGap = MAX2(0., MIN2(fullSpeedGap,
1743 neighLead.second - myVehicle.getCarFollowModel().getSecureGap(&myVehicle, neighLead.first,
1744 vMax, neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel())));
1745 fullSpeedDrivingSeconds = MIN2(fullSpeedDrivingSeconds, fullSpeedGap / (vMax - neighLead.first->getSpeed()));
1746 }
1747 const double deltaProb = (myChangeProbThresholdRight * (fullSpeedDrivingSeconds / acceptanceTime) / KEEP_RIGHT_TIME) * myVehicle.getActionStepLengthSecs();
1748 const bool isSlide = preventSliding(latLaneDist);
1749 // stay below threshold
1750 if (!isSlide || !wantsKeepRight(myKeepRightProbability + deltaProb)) {
1751 myKeepRightProbability += deltaProb;
1752 }
1753
1754#ifdef DEBUG_WANTSCHANGE
1755 if (gDebugFlag2) {
1756 std::cout << STEPS2TIME(currentTime)
1757 << " considering keepRight:"
1758 << " vMax=" << vMax
1759 << " neighDist=" << neighDist
1760 << " brakeGap=" << myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed())
1761 << " leaderSpeed=" << (neighLead.first == 0 ? -1 : neighLead.first->getSpeed())
1762 << " secGap=" << (neighLead.first == 0 ? -1 : myVehicle.getCarFollowModel().getSecureGap(&myVehicle, neighLead.first,
1763 myVehicle.getSpeed(), neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel()))
1764 << " acceptanceTime=" << acceptanceTime
1765 << " fullSpeedGap=" << fullSpeedGap
1766 << " fullSpeedDrivingSeconds=" << fullSpeedDrivingSeconds
1767 << " dProb=" << deltaProb
1768 << " isSlide=" << isSlide
1769 << " keepRight=" << myKeepRightProbability
1770 << " speedGainL=" << mySpeedGainProbabilityLeft
1771 << "\n";
1772 }
1773#endif
1775 /*&& latLaneDist <= -NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()*/) {
1776 ret |= LCA_KEEPRIGHT;
1777 assert(myVehicle.getLane()->getIndex() > neighLane.getIndex() || isOpposite());
1778 if (!cancelRequest(ret | LCA_RIGHT, laneOffset)) {
1779 latDist = latLaneDist;
1780 maneuverDist = latLaneDist;
1781 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
1782 leaders, followers, blockers,
1783 neighLeaders, neighFollowers, neighBlockers);
1784 return ret;
1785 } else {
1786 ret &= ~LCA_KEEPRIGHT;
1787 }
1788 }
1789 }
1790
1791 const double bidiRightFactor = myVehicle.getLane()->getBidiLane() == nullptr && !isOpposite() ? 1 : 0.05;
1792#ifdef DEBUG_WANTSCHANGE
1793 if (gDebugFlag2) {
1794 std::cout << STEPS2TIME(currentTime)
1795 << " speedGainR=" << mySpeedGainProbabilityRight
1796 << " speedGainL=" << mySpeedGainProbabilityLeft
1797 << " neighDist=" << neighDist
1798 << " neighTime=" << neighDist / MAX2(.1, myVehicle.getSpeed())
1799 << " rThresh=" << myChangeProbThresholdRight
1800 << " rThresh2=" << myChangeProbThresholdRight* bidiRightFactor
1801 << " latDist=" << latDist
1802 << "\n";
1803 }
1804#endif
1805
1806 // make changing on the right more attractive on bidi edges
1807 if (latDist < 0 && mySpeedGainProbabilityRight >= myChangeProbThresholdRight * bidiRightFactor
1808 && neighDist / MAX2(.1, myVehicle.getSpeed()) > mySpeedGainRemainTime) {
1809 ret |= LCA_SPEEDGAIN;
1810 if (!cancelRequest(ret | getLCA(ret, latDist), laneOffset)) {
1811 int blockedFully = 0;
1812 maneuverDist = latDist;
1813 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
1814 leaders, followers, blockers,
1815 neighLeaders, neighFollowers, neighBlockers,
1816 nullptr, nullptr, false, 0, &blockedFully);
1817 //commitManoeuvre(blocked, blockedFully, leaders, neighLeaders, neighLane);
1818 return ret;
1819 } else {
1820 // @note: restore ret so subsequent calls to cancelRequest work correctly
1821 latDist = 0;
1822 ret &= ~LCA_SPEEDGAIN;
1823 }
1824 }
1825 }
1826 if (!right || isOpposite()) {
1827
1828 const bool stayInLane = myVehicle.getLateralPositionOnLane() + latDist < 0.5 * myVehicle.getLane()->getWidth();
1829#ifdef DEBUG_WANTSCHANGE
1830 if (gDebugFlag2) {
1831 std::cout << STEPS2TIME(currentTime)
1832 << " speedGainL=" << mySpeedGainProbabilityLeft
1833 << " speedGainR=" << mySpeedGainProbabilityRight
1834 << " latDist=" << latDist
1835 << " neighDist=" << neighDist
1836 << " neighTime=" << neighDist / MAX2(.1, myVehicle.getSpeed())
1837 << " lThresh=" << myChangeProbThresholdLeft
1838 << " stayInLane=" << stayInLane
1839 << "\n";
1840 }
1841#endif
1842
1844 // if we leave our lane, we should be able to stay in the new
1845 // lane for some time
1846 (stayInLane || neighDist / MAX2(.1, myVehicle.getSpeed()) > mySpeedGainRemainTime)) {
1847 ret |= LCA_SPEEDGAIN;
1848 if (!cancelRequest(ret + getLCA(ret, latDist), laneOffset)) {
1849 int blockedFully = 0;
1850 maneuverDist = latDist;
1851 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
1852 leaders, followers, blockers,
1853 neighLeaders, neighFollowers, neighBlockers,
1854 nullptr, nullptr, false, 0, &blockedFully);
1855 //commitManoeuvre(blocked, blockedFully, leaders, neighLeaders, neighLane);
1856 return ret;
1857 } else {
1858 latDist = 0;
1859 ret &= ~LCA_SPEEDGAIN;
1860 }
1861 }
1862 }
1863
1864 double latDistSublane = 0.;
1865 const double halfLaneWidth = myVehicle.getLane()->getWidth() * 0.5;
1866 const double halfVehWidth = getWidth() * 0.5;
1869 && bestLaneOffset == 0
1871 // vehicle is on its final edge, on the correct lane and close to
1872 // its arrival position. Change to the desired lateral position
1876 break;
1878 latDistSublane = -halfLaneWidth + halfVehWidth - myVehicle.getLateralPositionOnLane();
1879 break;
1881 latDistSublane = -myVehicle.getLateralPositionOnLane();
1882 break;
1884 latDistSublane = halfLaneWidth - halfVehWidth - myVehicle.getLateralPositionOnLane();
1885 break;
1886 default:
1887 assert(false);
1888 }
1889#ifdef DEBUG_WANTSCHANGE
1890 if (gDebugFlag2) std::cout << SIMTIME
1891 << " arrivalPosLatProcedure=" << (int)myVehicle.getParameter().arrivalPosLatProcedure
1892 << " arrivalPosLat=" << myVehicle.getParameter().arrivalPosLat << "\n";
1893#endif
1894
1895 } else {
1896
1898 switch (align) {
1900 latDistSublane = -halfLaneWidth + halfVehWidth - getPosLat();
1901 break;
1903 latDistSublane = halfLaneWidth - halfVehWidth - getPosLat();
1904 break;
1907 latDistSublane = -getPosLat();
1908 break;
1910 latDistSublane = latDistNice;
1911 break;
1913 latDistSublane = sublaneSides[sublaneCompact] - rightVehSide;
1914 break;
1916 latDistSublane = myVehicle.getLateralPositionOnLane() - getPosLat();
1917 const double hLW = myVehicle.getLane()->getWidth() * 0.5;
1918 const double posLat = myVehicle.getLateralPositionOnLane();
1919 if (fabs(posLat) > hLW) {
1920 // vehicle is not within it's current lane
1921 if (posLat > 0) {
1922 latDistSublane -= (posLat - hLW);
1923 } else {
1924 latDistSublane += (-posLat - hLW);
1925 }
1926 } else {
1927 const double edgeWidth = myVehicle.getCurrentEdge()->getWidth();
1928 if (getWidth() < edgeWidth) {
1929 if (rightVehSide < 0) {
1930 latDistSublane -= rightVehSide;
1931 } else if (leftVehSide > edgeWidth) {
1932 latDistSublane -= leftVehSide - edgeWidth;
1933 }
1934 }
1935 }
1936 break;
1937 }
1939 // sublane alignment should not cause the vehicle to leave the lane
1940 const double hw = myVehicle.getLane()->getWidth() / 2 - NUMERICAL_EPS;
1941 const double offset = MAX2(-hw, MIN2(hw, myVehicle.getVehicleType().getPreferredLateralAlignmentOffset()));
1942 latDistSublane = -getPosLat() + offset;
1943 }
1944 break;
1945 default:
1946 break;
1947 }
1948 }
1949 // only factor in preferred lateral alignment if there is no speedGain motivation or it runs in the same direction
1950 if (fabs(latDist) <= NUMERICAL_EPS * myVehicle.getActionStepLengthSecs() ||
1951 latDistSublane * latDist > 0) {
1952
1953#if defined(DEBUG_WANTSCHANGE) || defined(DEBUG_STATE) || defined(DEBUG_MANEUVER)
1954 if (gDebugFlag2) std::cout << SIMTIME
1956 << " mySpeedGainR=" << mySpeedGainProbabilityRight
1957 << " mySpeedGainL=" << mySpeedGainProbabilityLeft
1958 << " latDist=" << latDist
1959 << " latDistSublane=" << latDistSublane
1960 << " relGainSublane=" << computeSpeedGain(latDistSublane, defaultNextSpeed)
1961 << " maneuverDist=" << maneuverDist
1962 << " myCanChangeFully=" << myCanChangeFully
1963 << " myTurnAlignmentDist=" << myTurnAlignmentDist
1964 << " nextTurn=" << myVehicle.getNextTurn().first << ":" << toString(myVehicle.getNextTurn().second)
1965 << " prevState=" << toString((LaneChangeAction)myPreviousState)
1966 << "\n";
1967#endif
1968
1969 if ((latDistSublane < 0 && mySpeedGainProbabilityRight < mySpeedLossProbThreshold)
1970 || (latDistSublane > 0 && mySpeedGainProbabilityLeft < mySpeedLossProbThreshold)
1971 || computeSpeedGain(latDistSublane, defaultNextSpeed) < -mySublaneParam) {
1972 // do not risk losing speed
1973#if defined(DEBUG_WANTSCHANGE)
1974 if (gDebugFlag2) std::cout << " aborting sublane change to avoid speed loss (mySpeedLossProbThreshold=" << mySpeedLossProbThreshold
1975 << " speedGain=" << computeSpeedGain(latDistSublane, defaultNextSpeed) << ")\n";
1976#endif
1977 latDistSublane = 0;
1978 }
1979 // Ignore preferred lateral alignment if we are in the middle of an unfinished non-alignment maneuver into the opposite direction
1980 if (!myCanChangeFully
1982 && ((getManeuverDist() < 0 && latDistSublane > 0) || (getManeuverDist() > 0 && latDistSublane < 0))) {
1983#if defined(DEBUG_WANTSCHANGE)
1984 if (gDebugFlag2) {
1985 std::cout << " aborting sublane change due to prior maneuver\n";
1986 }
1987#endif
1988 latDistSublane = 0;
1989 }
1990 latDist = latDistSublane * (isOpposite() ? -1 : 1);
1991 // XXX first compute preferred adaptation and then override with speed
1992 // (this way adaptation is still done if changing for speedgain is
1993 // blocked)
1994 if (fabs(latDist) >= NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
1995#ifdef DEBUG_WANTSCHANGE
1996 if (gDebugFlag2) std::cout << SIMTIME
1997 << " adapting to preferred alignment=" << toString(myVehicle.getVehicleType().getPreferredLateralAlignment())
1998 << " latDist=" << latDist
1999 << "\n";
2000#endif
2001 ret |= LCA_SUBLANE;
2002 // include prior motivation when sublane-change is part of finishing an ongoing maneuver in the same direction
2003 if (getPreviousManeuverDist() * latDist > 0) {
2004 int priorReason = (myPreviousState & LCA_CHANGE_REASONS & ~LCA_SUBLANE);
2005 ret |= priorReason;
2006#ifdef DEBUG_WANTSCHANGE
2007 if (gDebugFlag2 && priorReason != 0) std::cout << " including prior reason " << toString((LaneChangeAction)priorReason)
2008 << " prevManeuverDist=" << getPreviousManeuverDist() << "\n";
2009#endif
2010 }
2011 if (!cancelRequest(ret + getLCA(ret, latDist), laneOffset)) {
2012 maneuverDist = latDist;
2013 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
2014 leaders, followers, blockers,
2015 neighLeaders, neighFollowers, neighBlockers);
2016 return ret;
2017 } else {
2018 ret &= ~LCA_SUBLANE;
2019 }
2020 } else {
2021 return ret | LCA_SUBLANE | LCA_STAY;
2022 }
2023 }
2024 latDist = 0;
2025
2026
2027 // --------
2028 /*
2029 if (changeToBest && bestLaneOffset == curr.bestLaneOffset && laneOffset != 0
2030 && (right
2031 ? mySpeedGainProbabilityRight > MAX2(0., mySpeedGainProbabilityLeft)
2032 : mySpeedGainProbabilityLeft > MAX2(0., mySpeedGainProbabilityRight))) {
2033 // change towards the correct lane, speedwise it does not hurt
2034 ret |= LCA_STRATEGIC;
2035 if (!cancelRequest(ret, laneOffset)) {
2036 latDist = latLaneDist;
2037 blocked = checkBlocking(neighLane, latDist, laneOffset,
2038 leaders, followers, blockers,
2039 neighLeaders, neighFollowers, neighBlockers);
2040 return ret;
2041 }
2042 }
2043 */
2044#ifdef DEBUG_WANTSCHANGE
2045 if (gDebugFlag2) {
2046 std::cout << STEPS2TIME(currentTime)
2047 << " veh=" << myVehicle.getID()
2048 << " mySpeedGainR=" << mySpeedGainProbabilityRight
2049 << " mySpeedGainL=" << mySpeedGainProbabilityLeft
2050 << " myKeepRight=" << myKeepRightProbability
2051 << "\n";
2052 }
2053#endif
2054 return ret;
2055}
2056
2057
2058int
2060 // if this vehicle is blocking someone in front, we maybe decelerate to let him in
2061 if ((*blocked) != nullptr) {
2062 double gap = (*blocked)->getPositionOnLane() - (*blocked)->getVehicleType().getLength() - myVehicle.getPositionOnLane() - myVehicle.getVehicleType().getMinGap();
2063#ifdef DEBUG_SLOWDOWN
2064 if (gDebugFlag2) {
2065 std::cout << SIMTIME
2066 << " veh=" << myVehicle.getID()
2067 << " blocked=" << Named::getIDSecure(*blocked)
2068 << " gap=" << gap
2069 << "\n";
2070 }
2071#endif
2072 if (gap > POSITION_EPS) {
2073 //const bool blockedWantsUrgentRight = (((*blocked)->getLaneChangeModel().getOwnState() & LCA_RIGHT != 0)
2074 // && ((*blocked)->getLaneChangeModel().getOwnState() & LCA_URGENT != 0));
2075
2077 //|| blockedWantsUrgentRight // VARIANT_10 (helpblockedRight)
2078 ) {
2079 if ((*blocked)->getSpeed() < SUMO_const_haltingSpeed) {
2081 } else {
2082 state |= LCA_AMBACKBLOCKER;
2083 }
2084 addLCSpeedAdvice(getCarFollowModel().followSpeed(
2086 (gap - POSITION_EPS), (*blocked)->getSpeed(),
2087 (*blocked)->getCarFollowModel().getMaxDecel()), false);
2088 //(*blocked) = 0; // VARIANT_14 (furtherBlock)
2089 }
2090 }
2091 }
2092 return state;
2093}
2094
2095
2096bool
2097MSLCM_SL2015::isBidi(const MSLane* lane) const {
2098 if (!MSNet::getInstance()->hasBidiEdges()) {
2099 return false;
2100 }
2101 if (lane == myVehicle.getLane()->getBidiLane()) {
2102 return true;
2103 }
2104 for (const MSLane* cand : myVehicle.getBestLanesContinuation()) {
2105 if (cand != nullptr && cand->getBidiLane() == lane) {
2106 return true;
2107 }
2108 }
2109 return false;
2110}
2111
2112void
2113MSLCM_SL2015::updateExpectedSublaneSpeeds(const MSLeaderDistanceInfo& ahead, int sublaneOffset, int laneIndex) {
2114 const std::vector<MSLane*>& lanes = myVehicle.getLane()->getEdge().getLanes();
2115 const std::vector<MSVehicle::LaneQ>& preb = myVehicle.getBestLanes();
2116 const MSLane* lane = isOpposite() ? myVehicle.getLane()->getParallelOpposite() : lanes[laneIndex];
2117 const MSLane* bidi = myVehicle.getLane()->getBidiLane();
2118 const double vMax = lane->getVehicleMaxSpeed(&myVehicle);
2119 assert(preb.size() == lanes.size() || isOpposite());
2120#ifdef DEBUG_EXPECTED_SLSPEED
2121 if (DEBUG_COND) {
2122 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " updateExpectedSublaneSpeeds opposite=" << isOpposite()
2123 << " sublaneOffset=" << sublaneOffset << " laneIndex=" << laneIndex << " lane=" << lane->getID() << " ahead=" << ahead.toString() << "\n";
2124 }
2125#endif
2126
2127 for (int sublane = 0; sublane < (int)ahead.numSublanes(); ++sublane) {
2128 const int edgeSublane = sublane + sublaneOffset;
2129 if (edgeSublane >= (int)myExpectedSublaneSpeeds.size()) {
2130 // this may happen if a sibling lane is wider than the changer lane
2131 continue;
2132 }
2134 // lane allowed, find potential leaders and compute safe speeds
2135 // XXX anticipate future braking if leader has a lower speed than myVehicle
2136 const MSVehicle* leader = ahead[sublane].first;
2137 const double gap = ahead[sublane].second;
2138 double vSafe;
2139 if (leader == nullptr) {
2140 if (hasBlueLight()) {
2141 // can continue from any lane if necessary
2142 vSafe = vMax;
2143 } else {
2144 const int prebIndex = isOpposite() ? (int)preb.size() - 1 : laneIndex;
2145 const double dist = preb[prebIndex].length - myVehicle.getPositionOnLane();
2146 vSafe = getCarFollowModel().followSpeed(&myVehicle, vMax, dist, 0, 0);
2147 }
2148 } else if (bidi != nullptr && leader->getLane()->getBidiLane() != nullptr && isBidi(leader->getLane())) {
2149 // oncoming
2150 if (gap < (1 + mySpeedGainLookahead * 2) * (vMax + leader->getSpeed())) {
2151 vSafe = 0;
2152 } else {
2153 vSafe = vMax;
2154 }
2155#ifdef DEBUG_EXPECTED_SLSPEED
2156 if (DEBUG_COND) {
2157 std::cout << SIMTIME << " updateExpectedSublaneSpeeds sublane=" << sublane << " leader=" << leader->getID() << " bidi=" << bidi->getID() << " gap=" << gap << " vSafe=" << vSafe << "\n";
2158 }
2159#endif
2160 } else {
2161 if (leader->getAcceleration() > 0.5 * leader->getCarFollowModel().getMaxAccel()) {
2162 // assume that the leader will continue accelerating to its maximum speed
2163 vSafe = leader->getLane()->getVehicleMaxSpeed(leader);
2164 } else {
2166 &myVehicle, vMax, gap, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel());
2167#ifdef DEBUG_EXPECTED_SLSPEED
2168 if (DEBUG_COND) {
2169 std::cout << " updateExpectedSublaneSpeeds edgeSublane=" << edgeSublane << " leader=" << leader->getID() << " gap=" << gap << " vSafe=" << vSafe << "\n";
2170 }
2171#endif
2172 vSafe = forecastAverageSpeed(vSafe, vMax, gap, leader->getSpeed());
2173 }
2174 }
2175 // take pedestrians into account
2176 if (lane->getEdge().getPersons().size() > 0 && lane->hasPedestrians()) {
2178 double foeRight, foeLeft;
2179 ahead.getSublaneBorders(sublane, 0, foeRight, foeLeft);
2180 // get all leaders ahead or overlapping
2181 const PersonDist pedLeader = lane->nextBlocking(myVehicle.getPositionOnLane() - myVehicle.getVehicleType().getLength(), foeRight, foeLeft);
2182 if (pedLeader.first != 0) {
2183 const double pedGap = pedLeader.second - myVehicle.getVehicleType().getMinGap() - myVehicle.getVehicleType().getLength();
2184 // we do not know the walking direction here so we take the pedestrian speed as 0
2185 vSafe = MIN2(getCarFollowModel().stopSpeed(&myVehicle, vMax, pedGap),
2186 forecastAverageSpeed(vSafe, vMax, pedGap, 0));
2187#ifdef DEBUG_EXPECTED_SLSPEED
2188 if (DEBUG_COND) {
2189 std::cout << " updateExpectedSublaneSpeeds edgeSublane=" << edgeSublane << " pedLeader=" << pedLeader.first->getID() << " gap=" << pedGap << " vSafe=" << vSafe << "\n";
2190 }
2191#endif
2192 }
2193 }
2194 // take bidi pedestrians into account
2195 if (bidi != nullptr && bidi->getEdge().getPersons().size() > 0 && bidi->hasPedestrians()) {
2197 double foeRight, foeLeft;
2198 ahead.getSublaneBorders(sublane, 0, foeRight, foeLeft);
2199 const double foeRightBidi = bidi->getWidth() - foeLeft;
2200 const double foeLeftBidi = bidi->getWidth() - foeRight;
2201 // get all leaders ahead or overlapping
2202 const double relativeBackPos = myVehicle.getLane()->getLength() - myVehicle.getPositionOnLane() + myVehicle.getLength();
2203 const double stopTime = ceil(myVehicle.getSpeed() / myVehicle.getCarFollowModel().getMaxDecel());
2204 PersonDist pedLeader = bidi->nextBlocking(relativeBackPos, foeRightBidi, foeLeftBidi, stopTime, true);
2205 if (pedLeader.first != 0) {
2206 const double pedGap = pedLeader.second - myVehicle.getVehicleType().getMinGap() - myVehicle.getVehicleType().getLength();
2207 // we do not know the walking direction here so we take the pedestrian speed as 0
2208 vSafe = MIN2(getCarFollowModel().stopSpeed(&myVehicle, vMax, pedGap),
2209 forecastAverageSpeed(vSafe, vMax, pedGap, 0));
2210#ifdef DEBUG_EXPECTED_SLSPEED
2211 if (DEBUG_COND) {
2212 std::cout << " updateExpectedSublaneSpeeds edgeSublane=" << edgeSublane << " pedLeader=" << pedLeader.first->getID() << " (bidi) gap=" << pedGap << " vSafe=" << vSafe << "\n";
2213 }
2214#endif
2215 }
2216 }
2217 vSafe = MIN2(vMax, vSafe);
2218 // forget old data when on the opposite side
2219 const double memoryFactor = isOpposite() ? 0 : pow(SPEEDGAIN_MEMORY_FACTOR, myVehicle.getActionStepLengthSecs());
2220 myExpectedSublaneSpeeds[edgeSublane] = memoryFactor * myExpectedSublaneSpeeds[edgeSublane] + (1 - memoryFactor) * vSafe;
2221 } else {
2222 // lane forbidden
2223 myExpectedSublaneSpeeds[edgeSublane] = -1;
2224 }
2225 }
2226 // XXX deal with leaders on subsequent lanes based on preb
2227}
2228
2229
2230double
2231MSLCM_SL2015::forecastAverageSpeed(double vSafe, double vMax, double gap, double vLeader) const {
2232 const double deltaV = vMax - vLeader;
2233 if (deltaV > 0 && gap / deltaV < mySpeedGainLookahead && mySpeedGainLookahead > 0) {
2234 // anticipate future braking by computing the average
2235 // speed over the next few seconds
2236 const double foreCastTime = mySpeedGainLookahead * 2;
2237 const double gapClosingTime = MAX2(0.0, gap / deltaV);
2238 const double vSafe2 = (gapClosingTime * vSafe + (foreCastTime - gapClosingTime) * vLeader) / foreCastTime;
2239#ifdef DEBUG_EXPECTED_SLSPEED
2240 if (DEBUG_COND && vSafe2 != vSafe) {
2241 std::cout << " foreCastTime=" << foreCastTime << " gapClosingTime=" << gapClosingTime << " extrapolated vSafe=" << vSafe2 << "\n";
2242 }
2243#endif
2244 vSafe = vSafe2;
2245 }
2246 return vSafe;
2247}
2248
2249
2250double
2251MSLCM_SL2015::computeSpeedGain(double latDistSublane, double defaultNextSpeed) const {
2252 double result = std::numeric_limits<double>::max();
2253 const std::vector<double>& sublaneSides = myVehicle.getLane()->getEdge().getSubLaneSides();
2254 const double vehWidth = getWidth();
2255 const double rightVehSide = myVehicle.getCenterOnEdge() - vehWidth * 0.5 + latDistSublane;
2256 const double leftVehSide = rightVehSide + vehWidth;
2257 for (int i = 0; i < (int)sublaneSides.size(); ++i) {
2258 const double leftSide = i + 1 < (int)sublaneSides.size() ? sublaneSides[i + 1] : MAX2(myVehicle.getLane()->getEdge().getWidth(), sublaneSides[i] + POSITION_EPS);
2259 if (overlap(rightVehSide, leftVehSide, sublaneSides[i], leftSide)) {
2260 result = MIN2(result, myExpectedSublaneSpeeds[i]);
2261 }
2262 //std::cout << " i=" << i << " rightVehSide=" << rightVehSide << " leftVehSide=" << leftVehSide << " sublaneR=" << sublaneSides[i] << " sublaneL=" << leftSide << " overlap=" << overlap(rightVehSide, leftVehSide, sublaneSides[i], leftSide) << " speed=" << myExpectedSublaneSpeeds[i] << " result=" << result << "\n";
2263 }
2264 return result - defaultNextSpeed;
2265}
2266
2267
2270 int iMax = -1;
2271 double maxLength = -1;
2272 for (int i = 0; i < ldi.numSublanes(); ++i) {
2273 const MSVehicle* veh = ldi[i].first;
2274 if (veh) {
2275 const double length = veh->getVehicleType().getLength();
2276 if (length > maxLength && tieBrakeLeader(veh)) {
2277 maxLength = length;
2278 iMax = i;
2279 }
2280 }
2281 }
2282 return iMax >= 0 ? ldi[iMax] : std::make_pair(nullptr, -1);
2283}
2284
2285
2286bool
2288 // tie braker if the leader is at the same lane position
2289 return veh != nullptr && (veh->getPositionOnLane() != myVehicle.getPositionOnLane()
2290 || veh->getSpeed() < myVehicle.getSpeed()
2291 || &veh->getLane()->getEdge() != &myVehicle.getLane()->getEdge()
2292 || veh->getLane()->getIndex() > myVehicle.getLane()->getIndex());
2293}
2294
2295
2298 int iMax = 0;
2299 double minSpeed = std::numeric_limits<double>::max();
2300 for (int i = 0; i < ldi.numSublanes(); ++i) {
2301 if (ldi[i].first != 0) {
2302 const double speed = ldi[i].first->getSpeed();
2303 if (speed < minSpeed) {
2304 minSpeed = speed;
2305 iMax = i;
2306 }
2307 }
2308 }
2309 return ldi[iMax];
2310}
2311
2312
2313int
2314MSLCM_SL2015::checkBlocking(const MSLane& neighLane, double& latDist, double maneuverDist, int laneOffset,
2315 const MSLeaderDistanceInfo& leaders,
2316 const MSLeaderDistanceInfo& followers,
2317 const MSLeaderDistanceInfo& /*blockers */,
2318 const MSLeaderDistanceInfo& neighLeaders,
2319 const MSLeaderDistanceInfo& neighFollowers,
2320 const MSLeaderDistanceInfo& /* neighBlockers */,
2321 std::vector<CLeaderDist>* collectLeadBlockers,
2322 std::vector<CLeaderDist>* collectFollowBlockers,
2323 bool keepLatGapManeuver,
2324 double gapFactor,
2325 int* retBlockedFully) {
2326 // truncate latDist according to maxSpeedLat
2327 const double maxDist = SPEED2DIST(getMaxSpeedLat2());
2328 latDist = MAX2(MIN2(latDist, maxDist), -maxDist);
2330 return 0;
2331 }
2332
2333 const double neighRight = getNeighRight(neighLane);
2334 if (!myCFRelatedReady) {
2335 updateCFRelated(followers, myVehicle.getLane()->getRightSideOnEdge(), false);
2337 if (laneOffset != 0) {
2338 updateCFRelated(neighFollowers, neighRight, false);
2339 updateCFRelated(neighLeaders, neighRight, true);
2340 }
2341 myCFRelatedReady = true;
2342 }
2343
2344 // reduce latDist to avoid blockage with overlapping vehicles (no minGapLat constraints)
2345 const double center = myVehicle.getCenterOnEdge();
2346 updateGaps(leaders, myVehicle.getLane()->getRightSideOnEdge(), center, gapFactor, mySafeLatDistRight, mySafeLatDistLeft, false, 0, latDist, collectLeadBlockers);
2347 updateGaps(followers, myVehicle.getLane()->getRightSideOnEdge(), center, gapFactor, mySafeLatDistRight, mySafeLatDistLeft, false, 0, latDist, collectFollowBlockers);
2348 if (laneOffset != 0) {
2349 updateGaps(neighLeaders, neighRight, center, gapFactor, mySafeLatDistRight, mySafeLatDistLeft, false, 0, latDist, collectLeadBlockers);
2350 updateGaps(neighFollowers, neighRight, center, gapFactor, mySafeLatDistRight, mySafeLatDistLeft, false, 0, latDist, collectFollowBlockers);
2351 }
2352#ifdef DEBUG_BLOCKING
2353 if (gDebugFlag2) {
2354 std::cout << " checkBlocking latDist=" << latDist << " mySafeLatDistRight=" << mySafeLatDistRight << " mySafeLatDistLeft=" << mySafeLatDistLeft << "\n";
2355 }
2356#endif
2357 // if we can move at least a little bit in the desired direction, do so (rather than block)
2358 const bool forcedTraCIChange = (myVehicle.hasInfluencer()
2361 if (latDist < 0) {
2362 if (mySafeLatDistRight <= NUMERICAL_EPS) {
2364 } else if (!forcedTraCIChange) {
2365 latDist = MAX2(latDist, -mySafeLatDistRight);
2366 }
2367 } else {
2368 if (mySafeLatDistLeft <= NUMERICAL_EPS) {
2370 } else if (!forcedTraCIChange) {
2371 latDist = MIN2(latDist, mySafeLatDistLeft);
2372 }
2373 }
2374
2375 myCanChangeFully = (maneuverDist == 0 || latDist == maneuverDist);
2376#ifdef DEBUG_BLOCKING
2377 if (gDebugFlag2) {
2378 std::cout << " checkBlocking fully=" << myCanChangeFully << " latDist=" << latDist << " maneuverDist=" << maneuverDist << "\n";
2379 }
2380#endif
2381 // destination sublanes must be safe
2382 // intermediate sublanes must not be blocked by overlapping vehicles
2383
2384 // XXX avoid checking the same leader multiple times
2385 // XXX ensure that only changes within the same lane are undertaken if laneOffset = 0
2386
2387 int blocked = 0;
2388 blocked |= checkBlockingVehicles(&myVehicle, leaders, laneOffset, latDist, myVehicle.getLane()->getRightSideOnEdge(), true,
2389 mySafeLatDistRight, mySafeLatDistLeft, collectLeadBlockers);
2390 blocked |= checkBlockingVehicles(&myVehicle, followers, laneOffset, latDist, myVehicle.getLane()->getRightSideOnEdge(), false,
2391 mySafeLatDistRight, mySafeLatDistLeft, collectFollowBlockers);
2392 if (laneOffset != 0) {
2393 blocked |= checkBlockingVehicles(&myVehicle, neighLeaders, laneOffset, latDist, neighRight, true,
2394 mySafeLatDistRight, mySafeLatDistLeft, collectLeadBlockers);
2395 blocked |= checkBlockingVehicles(&myVehicle, neighFollowers, laneOffset, latDist, neighRight, false,
2396 mySafeLatDistRight, mySafeLatDistLeft, collectFollowBlockers);
2397 }
2398
2399 int blockedFully = 0;
2400 blockedFully |= checkBlockingVehicles(&myVehicle, leaders, laneOffset, maneuverDist, myVehicle.getLane()->getRightSideOnEdge(), true,
2401 mySafeLatDistRight, mySafeLatDistLeft, collectLeadBlockers);
2402 blockedFully |= checkBlockingVehicles(&myVehicle, followers, laneOffset, maneuverDist, myVehicle.getLane()->getRightSideOnEdge(), false,
2403 mySafeLatDistRight, mySafeLatDistLeft, collectFollowBlockers);
2404 if (laneOffset != 0) {
2405 blockedFully |= checkBlockingVehicles(&myVehicle, neighLeaders, laneOffset, maneuverDist, neighRight, true,
2406 mySafeLatDistRight, mySafeLatDistLeft, collectLeadBlockers);
2407 blockedFully |= checkBlockingVehicles(&myVehicle, neighFollowers, laneOffset, maneuverDist, neighRight, false,
2408 mySafeLatDistRight, mySafeLatDistLeft, collectFollowBlockers);
2409 }
2410 if (retBlockedFully != nullptr) {
2411 *retBlockedFully = blockedFully;
2412 }
2413 if (blocked == 0 && !myCanChangeFully && myPushy == 0 && !keepLatGapManeuver) {
2414 // aggressive drivers immediately start moving towards potential
2415 // blockers and only check that the start of their maneuver (latDist) is safe. In
2416 // contrast, cautious drivers need to check latDist and origLatDist to
2417 // ensure that the maneuver can be finished without encroaching on other vehicles.
2418 blocked |= blockedFully;
2419 } else {
2420 // XXX: in case of action step length > simulation step length, pushing may lead to collisions,
2421 // because maneuver is continued until maneuverDist is reached (perhaps set maneuverDist=latDist)
2422 }
2423 if (collectFollowBlockers != nullptr && collectLeadBlockers != nullptr) {
2424 // prevent vehicles from being classified as leader and follower simultaneously
2425 for (std::vector<CLeaderDist>::const_iterator it2 = collectLeadBlockers->begin(); it2 != collectLeadBlockers->end(); ++it2) {
2426 for (std::vector<CLeaderDist>::iterator it = collectFollowBlockers->begin(); it != collectFollowBlockers->end();) {
2427 if ((*it2).first == (*it).first) {
2428#ifdef DEBUG_BLOCKING
2429 if (gDebugFlag2) {
2430 std::cout << " removed follower " << (*it).first->getID() << " because it is already a leader\n";
2431 }
2432#endif
2433 it = collectFollowBlockers->erase(it);
2434 } else {
2435 ++it;
2436 }
2437 }
2438 }
2439 }
2440 return blocked;
2441}
2442
2443
2444int
2446 const MSVehicle* ego, const MSLeaderDistanceInfo& vehicles,
2447 int laneOffset, double latDist, double foeOffset, bool leaders,
2448 double& safeLatGapRight, double& safeLatGapLeft,
2449 std::vector<CLeaderDist>* collectBlockers) const {
2450 // determine borders where safety/no-overlap conditions must hold
2451 const LaneChangeAction blockType = (laneOffset == 0
2453 : (laneOffset > 0
2456 const double vehWidth = getWidth();
2457 const double rightVehSide = ego->getRightSideOnEdge();
2458 const double leftVehSide = rightVehSide + vehWidth;
2459 const double rightVehSideDest = rightVehSide + latDist;
2460 const double leftVehSideDest = leftVehSide + latDist;
2461 const double rightNoOverlap = MIN2(rightVehSideDest, rightVehSide);
2462 const double leftNoOverlap = MAX2(leftVehSideDest, leftVehSide);
2463#ifdef DEBUG_BLOCKING
2464 if (gDebugFlag2) {
2465 std::cout << " checkBlockingVehicles"
2466 << " laneOffset=" << laneOffset
2467 << " latDist=" << latDist
2468 << " foeOffset=" << foeOffset
2469 << " vehRight=" << rightVehSide
2470 << " vehLeft=" << leftVehSide
2471 << " rightNoOverlap=" << rightNoOverlap
2472 << " leftNoOverlap=" << leftNoOverlap
2473 << " destRight=" << rightVehSideDest
2474 << " destLeft=" << leftVehSideDest
2475 << " leaders=" << leaders
2476 << " blockType=" << toString((LaneChangeAction) blockType)
2477 << "\n";
2478 }
2479#endif
2480 int result = 0;
2481 for (int i = 0; i < vehicles.numSublanes(); ++i) {
2482 CLeaderDist vehDist = vehicles[i];
2483 if (vehDist.first != 0 && myCFRelated.count(vehDist.first) == 0) {
2484 const MSVehicle* leader = vehDist.first;
2485 const MSVehicle* follower = ego;
2486 if (!leaders) {
2487 std::swap(leader, follower);
2488 }
2489 // only check the current stripe occupied by foe (transform into edge-coordinates)
2490 double foeRight, foeLeft;
2491 vehicles.getSublaneBorders(i, foeOffset, foeRight, foeLeft);
2492 const bool overlapBefore = overlap(rightVehSide, leftVehSide, foeRight, foeLeft);
2493 const bool overlapDest = overlap(rightVehSideDest, leftVehSideDest, foeRight, foeLeft);
2494 const bool overlapAny = overlap(rightNoOverlap, leftNoOverlap, foeRight, foeLeft);
2495#ifdef DEBUG_BLOCKING
2496 if (gDebugFlag2) {
2497 std::cout << " foe=" << vehDist.first->getID()
2498 << " gap=" << vehDist.second
2499 << " secGap=" << follower->getCarFollowModel().getSecureGap(follower, leader, follower->getSpeed(), leader->getSpeed(), leader->getCarFollowModel().getMaxDecel())
2500 << " foeRight=" << foeRight
2501 << " foeLeft=" << foeLeft
2502 << " overlapBefore=" << overlapBefore
2503 << " overlap=" << overlapAny
2504 << " overlapDest=" << overlapDest
2505 << "\n";
2506 }
2507#endif
2508 if (overlapAny) {
2509 if (vehDist.second < 0) {
2510 if (overlapBefore && !overlapDest && !outsideEdge()) {
2511#ifdef DEBUG_BLOCKING
2512 if (gDebugFlag2) {
2513 std::cout << " ignoring current overlap to come clear\n";
2514 }
2515#endif
2516 } else {
2517#ifdef DEBUG_BLOCKING
2518 if (gDebugFlag2) {
2519 std::cout << " overlap (" << toString((LaneChangeAction)blockType) << ")\n";
2520 }
2521#endif
2522 result |= (blockType | LCA_OVERLAPPING);
2523 if (collectBlockers == nullptr) {
2524 return result;
2525 } else {
2526 collectBlockers->push_back(vehDist);
2527 }
2528 }
2529 } else if (overlapDest || !myCanChangeFully) {
2530 // Estimate state after actionstep (follower may be accelerating!)
2531 // A comparison between secure gap depending on the expected speeds and the extrapolated gap
2532 // determines whether the s is blocking the lane change.
2533 // (Note that the longitudinal state update has already taken effect before LC dynamics (thus "-TS" below), would be affected by #3665)
2534
2535 // Use conservative estimate for time until next action step
2536 // (XXX: how can the ego know the foe's action step length?)
2537 const double timeTillAction = MAX2(follower->getActionStepLengthSecs(), leader->getActionStepLengthSecs()) - TS;
2538 // Ignore decel for follower
2539 const double followerAccel = MAX2(0., follower->getAcceleration());
2540 const double leaderAccel = leader->getAcceleration();
2541 // Expected gap after next actionsteps
2542 const double expectedGap = MSCFModel::gapExtrapolation(timeTillAction, vehDist.second, leader->getSpeed(), follower->getSpeed(), leaderAccel, followerAccel, std::numeric_limits<double>::max(), std::numeric_limits<double>::max());
2543
2544 // Determine expected speeds and corresponding secure gap at the extrapolated timepoint
2545 const double followerExpectedSpeed = follower->getSpeed() + timeTillAction * followerAccel;
2546 const double leaderExpectedSpeed = MAX2(0., leader->getSpeed() + timeTillAction * leaderAccel);
2547 const double expectedSecureGap = follower->getCarFollowModel().getSecureGap(follower, leader, followerExpectedSpeed, leaderExpectedSpeed, leader->getCarFollowModel().getMaxDecel());
2548
2549#if defined(DEBUG_ACTIONSTEPS) && defined(DEBUG_BLOCKING)
2550 if (gDebugFlag2) {
2551 std::cout << " timeTillAction=" << timeTillAction
2552 << " followerAccel=" << followerAccel
2553 << " followerExpectedSpeed=" << followerExpectedSpeed
2554 << " leaderAccel=" << leaderAccel
2555 << " leaderExpectedSpeed=" << leaderExpectedSpeed
2556 << "\n gap=" << vehDist.second
2557 << " gapChange=" << (expectedGap - vehDist.second)
2558 << " expectedGap=" << expectedGap
2559 << " expectedSecureGap=" << expectedSecureGap
2560 << " safeLatGapLeft=" << safeLatGapLeft
2561 << " safeLatGapRight=" << safeLatGapRight
2562 << std::endl;
2563 }
2564#endif
2565
2566 // @note for euler-update, a different value for secureGap2 may be obtained when applying safetyFactor to followerDecel rather than secureGap
2567 const double secureGap2 = expectedSecureGap * getSafetyFactor();
2568 if (expectedGap < secureGap2) {
2569 // Foe is a blocker. Update lateral safe gaps accordingly.
2570 if (foeRight > leftVehSide) {
2571 safeLatGapLeft = MIN2(safeLatGapLeft, foeRight - leftVehSide);
2572 } else if (foeLeft < rightVehSide) {
2573 safeLatGapRight = MIN2(safeLatGapRight, rightVehSide - foeLeft);
2574 }
2575
2576#ifdef DEBUG_BLOCKING
2577 if (gDebugFlag2) {
2578 std::cout << " blocked by " << vehDist.first->getID() << " gap=" << vehDist.second << " expectedGap=" << expectedGap
2579 << " expectedSecureGap=" << expectedSecureGap << " secGap2=" << secureGap2 << " safetyFactor=" << getSafetyFactor()
2580 << " safeLatGapLeft=" << safeLatGapLeft << " safeLatGapRight=" << safeLatGapRight
2581 << "\n";
2582 }
2583#endif
2584 result |= blockType;
2585 if (collectBlockers == nullptr) {
2586 return result;
2587 }
2588#ifdef DEBUG_BLOCKING
2589 } else if (gDebugFlag2 && expectedGap < expectedSecureGap) {
2590 std::cout << " ignore blocker " << vehDist.first->getID() << " gap=" << vehDist.second << " expectedGap=" << expectedGap
2591 << " expectedSecureGap=" << expectedSecureGap << " secGap2=" << secureGap2 << " safetyFactor=" << getSafetyFactor() << "\n";
2592#endif
2593 }
2594 if (collectBlockers != nullptr) {
2595 // collect non-blocking followers as well to make sure
2596 // they remain non-blocking
2597 collectBlockers->push_back(vehDist);
2598 }
2599 }
2600 }
2601 }
2602 }
2603 return result;
2604
2605}
2606
2607
2608void
2609MSLCM_SL2015::updateCFRelated(const MSLeaderDistanceInfo& vehicles, double foeOffset, bool leaders) {
2610 // to ensure that we do not ignore the wrong vehicles due to numerical
2611 // instability we slightly reduce the width
2612 const double vehWidth = myVehicle.getVehicleType().getWidth() - NUMERICAL_EPS;
2613 const double rightVehSide = myVehicle.getCenterOnEdge() - 0.5 * vehWidth;
2614 const double leftVehSide = rightVehSide + vehWidth;
2615#ifdef DEBUG_BLOCKING
2616 if (gDebugFlag2) {
2617 std::cout << " updateCFRelated foeOffset=" << foeOffset << " vehicles=" << vehicles.toString() << "\n";
2618 }
2619#endif
2620 for (int i = 0; i < vehicles.numSublanes(); ++i) {
2621 CLeaderDist vehDist = vehicles[i];
2622 if (vehDist.first != 0 && (myCFRelated.count(vehDist.first) == 0 || vehDist.second < 0)) {
2623 double foeRight, foeLeft;
2624 vehicles.getSublaneBorders(i, foeOffset, foeRight, foeLeft);
2625#ifdef DEBUG_BLOCKING
2626 if (gDebugFlag2) {
2627 std::cout << " foe=" << vehDist.first->getID() << " gap=" << vehDist.second
2628 << " sublane=" << i
2629 << " foeOffset=" << foeOffset
2630 << " egoR=" << rightVehSide << " egoL=" << leftVehSide
2631 << " iR=" << foeRight << " iL=" << foeLeft
2632 << " egoV=" << myVehicle.getSpeed() << " foeV=" << vehDist.first->getSpeed()
2633 << " egoE=" << myVehicle.getLane()->getEdge().getID() << " foeE=" << vehDist.first->getLane()->getEdge().getID()
2634 << "\n";
2635 }
2636#endif
2637 if (overlap(rightVehSide, leftVehSide, foeRight, foeLeft) && !outsideEdge() && (vehDist.second >= 0
2638 // avoid deadlock due to #3729
2639 || (!leaders
2642 && vehDist.first->getSpeed() < SUMO_const_haltingSpeed
2643 && -vehDist.second < vehDist.first->getVehicleType().getMinGap()
2644 && &(myVehicle.getLane()->getEdge()) != &(vehDist.first->getLane()->getEdge()))
2645 )) {
2646#ifdef DEBUG_BLOCKING
2647 if (gDebugFlag2) {
2648 std::cout << " ignoring cfrelated foe=" << vehDist.first->getID() << "\n";
2649 }
2650#endif
2651 myCFRelated.insert(vehDist.first);
2652 } else {
2653 const int erased = (int)myCFRelated.erase(vehDist.first);
2654#ifdef DEBUG_BLOCKING
2655 if (gDebugFlag2 && erased > 0) {
2656 std::cout << " restoring cfrelated foe=" << vehDist.first->getID() << "\n";
2657 }
2658#else
2659 UNUSED_PARAMETER(erased);
2660#endif
2661 }
2662 }
2663 }
2664}
2665
2666
2667bool
2668MSLCM_SL2015::overlap(double right, double left, double right2, double left2) {
2669 assert(right <= left);
2670 assert(right2 <= left2);
2671 return left2 >= right + NUMERICAL_EPS && left >= right2 + NUMERICAL_EPS;
2672}
2673
2674
2675int
2676MSLCM_SL2015::lowest_bit(int changeReason) {
2677 if ((changeReason & LCA_STRATEGIC) != 0) {
2678 return LCA_STRATEGIC;
2679 }
2680 if ((changeReason & LCA_COOPERATIVE) != 0) {
2681 return LCA_COOPERATIVE;
2682 }
2683 if ((changeReason & LCA_SPEEDGAIN) != 0) {
2684 return LCA_SPEEDGAIN;
2685 }
2686 if ((changeReason & LCA_KEEPRIGHT) != 0) {
2687 return LCA_KEEPRIGHT;
2688 }
2689 if ((changeReason & LCA_TRACI) != 0) {
2690 return LCA_TRACI;
2691 }
2692 return changeReason;
2693}
2694
2695
2698 // ignore dummy decisions (returned if mayChange() failes)
2699 if (sd1.state == 0) {
2700 return sd2;
2701 } else if (sd2.state == 0) {
2702 return sd1;
2703 }
2704 // LCA_SUBLANE is special because LCA_STAY|LCA_SUBLANE may override another LCA_SUBLANE command
2705 const bool want1 = ((sd1.state & LCA_WANTS_LANECHANGE) != 0) || ((sd1.state & LCA_SUBLANE) != 0 && (sd1.state & LCA_STAY) != 0);
2706 const bool want2 = ((sd2.state & LCA_WANTS_LANECHANGE) != 0) || ((sd2.state & LCA_SUBLANE) != 0 && (sd2.state & LCA_STAY) != 0);
2707 const bool can1 = ((sd1.state & LCA_BLOCKED) == 0);
2708 const bool can2 = ((sd2.state & LCA_BLOCKED) == 0);
2709 int reason1 = lowest_bit(sd1.state & LCA_CHANGE_REASONS);
2710 int reason2 = lowest_bit(sd2.state & LCA_CHANGE_REASONS);
2711#ifdef DEBUG_DECISION
2712 if (DEBUG_COND) std::cout << SIMTIME
2713 << " veh=" << myVehicle.getID()
2714 << " state1=" << toString((LaneChangeAction)sd1.state)
2715 << " want1=" << (sd1.state & LCA_WANTS_LANECHANGE)
2716 << " dist1=" << sd1.latDist
2717 << " dir1=" << sd1.dir
2718 << " state2=" << toString((LaneChangeAction)sd2.state)
2719 << " want2=" << (sd2.state & LCA_WANTS_LANECHANGE)
2720 << " dist2=" << sd2.latDist
2721 << " dir2=" << sd2.dir
2722 << " reason1=" << toString((LaneChangeAction)reason1)
2723 << " reason2=" << toString((LaneChangeAction)reason2)
2724 << "\n";
2725#endif
2726 if (want1) {
2727 if (want2) {
2728 if ((sd1.state & LCA_TRACI) != 0 && (sd2.state & LCA_TRACI) != 0) {
2729 // influencer may assign LCA_WANTS_LANECHANGE despite latDist = 0
2730 if (sd1.latDist == 0 && sd2.latDist != 0) {
2731 return sd2;
2732 } else if (sd2.latDist == 0 && sd1.latDist != 0) {
2733 return sd1;
2734 }
2735 }
2736 // decide whether right or left has higher priority (lower value in enum LaneChangeAction)
2737 if (reason1 < reason2) {
2738 //if (DEBUG_COND) std::cout << " " << (sd1.state & LCA_CHANGE_REASONS) << " < " << (sd2.state & LCA_CHANGE_REASONS) << "\n";
2739 return (!can1 && can2 && sd1.sameDirection(sd2)) ? sd2 : sd1;
2740 //return sd1;
2741 } else if (reason1 > reason2) {
2742 //if (DEBUG_COND) std::cout << " " << (sd1.state & LCA_CHANGE_REASONS) << " > " << (sd2.state & LCA_CHANGE_REASONS) << "\n";
2743 return (!can2 && can1 && sd1.sameDirection(sd2)) ? sd1 : sd2;
2744 //return sd2;
2745 } else {
2746 // same priority.
2747 if ((sd1.state & LCA_SUBLANE) != 0) {
2748 // special treatment: prefer action with dir != 0
2749 if (sd1.dir == 0) {
2750 return sd2;
2751 } else if (sd2.dir == 0) {
2752 return sd1;
2753 } else {
2754 // prefer action that knows more about the desired direction
2755 // @note when deciding between right and left, right is always given as sd1
2756 assert(sd1.dir == -1);
2757 assert(sd2.dir == 1);
2758 if (sd1.latDist <= 0) {
2759 return sd1;
2760 } else if (sd2.latDist >= 0) {
2761 return sd2;
2762 }
2763 // when in doubt, prefer moving to the right
2764 return sd1.latDist <= sd2.latDist ? sd1 : sd2;
2765 }
2766 } else {
2767 if (can1) {
2768 if (can2) {
2769 // break strategic ties with tactial concerns
2770 if (reason1 == LCA_STRATEGIC) {
2771 if (sd1.latDist <= sd2.latDist) {
2773 } else {
2775 }
2776 } else {
2777 // finish the shorter maneuver (i.e. continue the current maneuver)
2778 return fabs(sd1.maneuverDist) < fabs(sd2.maneuverDist) ? sd1 : sd2;
2779 }
2780 } else {
2781 return sd1;
2782 }
2783 } else {
2784 return sd2;
2785 }
2786 }
2787 }
2788 } else {
2789 return sd1;
2790 }
2791 } else {
2792 return sd2;
2793 }
2794
2795}
2796
2797
2799MSLCM_SL2015::getLCA(int state, double latDist) {
2800 return ((latDist == 0 || (state & LCA_CHANGE_REASONS) == 0)
2801 ? LCA_NONE : (latDist < 0 ? LCA_RIGHT : LCA_LEFT));
2802}
2803
2804
2805int
2807 const MSLane& neighLane,
2808 int laneOffset,
2809 const MSLeaderDistanceInfo& leaders,
2810 const MSLeaderDistanceInfo& neighLeaders,
2811 const MSVehicle::LaneQ& curr,
2812 const MSVehicle::LaneQ& neigh,
2813 const MSVehicle::LaneQ& best,
2814 int bestLaneOffset,
2815 bool changeToBest,
2816 double& currentDist,
2817 double neighDist,
2818 double laDist,
2819 double roundaboutBonus,
2820 double latLaneDist,
2821 bool checkOpposite,
2822 double& latDist
2823 ) {
2824 const bool right = (laneOffset == -1);
2825 const bool left = (laneOffset == 1);
2826
2827 const double forwardPos = getForwardPos();
2828 myLeftSpace = currentDist - forwardPos;
2829 const double usableDist = (currentDist - forwardPos - best.occupation * JAM_FACTOR);
2830 //- (best.lane->getVehicleNumber() * neighSpeed)); // VARIANT 9 jfSpeed
2831 const double maxJam = MAX2(neigh.occupation, curr.occupation);
2832 const double neighLeftPlace = MAX2(0., neighDist - forwardPos - maxJam);
2833 // save the left space
2834
2835#ifdef DEBUG_STRATEGIC_CHANGE
2836 if (gDebugFlag2) {
2837 std::cout << SIMTIME
2838 << " veh=" << myVehicle.getID()
2839 << " forwardPos=" << forwardPos
2840 << " laSpeed=" << myLookAheadSpeed
2841 << " laDist=" << laDist
2842 << " currentDist=" << currentDist
2843 << " usableDist=" << usableDist
2844 << " bestLaneOffset=" << bestLaneOffset
2845 << " best.length=" << best.length
2846 << " maxJam=" << maxJam
2847 << " neighLeftPlace=" << neighLeftPlace
2848 << " myLeftSpace=" << myLeftSpace
2849 << "\n";
2850 }
2851#endif
2852
2853 if (laneOffset != 0 && changeToBest && bestLaneOffset == curr.bestLaneOffset
2854 && currentDistDisallows(usableDist, bestLaneOffset, laDist)) {
2856 if (!mustOvertakeStopped(false, neighLane, neighLeaders, leaders, forwardPos, neighDist, right, latLaneDist, currentDist, latDist)) {
2857 latDist = latLaneDist;
2858 ret |= LCA_STRATEGIC | LCA_URGENT;
2859#ifdef DEBUG_STRATEGIC_CHANGE
2860 if (gDebugFlag2) {
2861 std::cout << SIMTIME << " mustChangeToBest\n";
2862 }
2863#endif
2864 } else {
2865#ifdef DEBUG_STRATEGIC_CHANGE
2866 if (gDebugFlag2) {
2867 std::cout << " veh=" << myVehicle.getID() << " avoidStoppedNeigh\n";
2868 }
2869#endif
2870 }
2871 } else {
2872 // VARIANT_20 (noOvertakeRight)
2873 if (left && avoidOvertakeRight() && neighLeaders.hasVehicles()) {
2874 // check for slower leader on the left. we should not overtake but
2875 // rather move left ourselves (unless congested)
2876 // XXX only adapt as much as possible to get a lateral gap
2877 CLeaderDist cld = getSlowest(neighLeaders);
2878 const MSVehicle* nv = cld.first;
2879 if (nv->getSpeed() < myVehicle.getSpeed()) {
2880 const double vSafe = getCarFollowModel().followSpeed(
2881 &myVehicle, myVehicle.getSpeed(), cld.second, nv->getSpeed(), nv->getCarFollowModel().getMaxDecel());
2882 addLCSpeedAdvice(vSafe);
2883 if (vSafe < myVehicle.getSpeed()) {
2885 }
2886#ifdef DEBUG_STRATEGIC_CHANGE
2887 if (gDebugFlag2) {
2888 std::cout << SIMTIME
2889 << " avoid overtaking on the right nv=" << nv->getID()
2890 << " nvSpeed=" << nv->getSpeed()
2891 << " mySpeedGainProbabilityR=" << mySpeedGainProbabilityRight
2892 << " plannedSpeed=" << myVehicle.getSpeed() + ACCEL2SPEED(myLCAccelerationAdvices.back().first)
2893 << "\n";
2894 }
2895#endif
2896 }
2897 }
2898
2899 // handling reaction to stopped for opposite direction driving NYI
2900 const bool noOpposites = &myVehicle.getLane()->getEdge() == &neighLane.getEdge();
2901 if (laneOffset != 0 && myStrategicParam >= 0 && noOpposites && mustOvertakeStopped(true, neighLane, leaders, neighLeaders, forwardPos, neighDist, right, latLaneDist, currentDist, latDist)) {
2902#ifdef DEBUG_STRATEGIC_CHANGE
2903 if (gDebugFlag2) {
2904 std::cout << " veh=" << myVehicle.getID() << " mustOvertakeStopped\n";
2905 }
2906#endif
2907 if (latDist == 0) {
2908 ret |= LCA_STAY | LCA_STRATEGIC;
2909 } else {
2910 ret |= LCA_STRATEGIC | LCA_URGENT;
2911 }
2912
2913 } else if (!changeToBest && (currentDistDisallows(neighLeftPlace, abs(bestLaneOffset) + 2, laDist))) {
2914 // the opposite lane-changing direction should be done than the one examined herein
2915 // we'll check whether we assume we could change anyhow and get back in time...
2916 //
2917 // this rule prevents the vehicle from moving in opposite direction of the best lane
2918 // unless the way till the end where the vehicle has to be on the best lane
2919 // is long enough
2920#ifdef DEBUG_STRATEGIC_CHANGE
2921 if (gDebugFlag2) {
2922 std::cout << " veh=" << myVehicle.getID() << " could not change back and forth in time (1) neighLeftPlace=" << neighLeftPlace << "\n";
2923 }
2924#endif
2925 ret |= LCA_STAY | LCA_STRATEGIC;
2926 } else if (
2927 laneOffset != 0
2928 && bestLaneOffset == 0
2929 && !leaders.hasStoppedVehicle()
2930 && neigh.bestContinuations.back()->getLinkCont().size() != 0
2931 && roundaboutBonus == 0
2932 && !checkOpposite
2933 && neighDist < TURN_LANE_DIST
2934 && myStrategicParam >= 0) {
2935 // VARIANT_21 (stayOnBest)
2936 // we do not want to leave the best lane for a lane which leads elsewhere
2937 // unless our leader is stopped or we are approaching a roundabout
2938#ifdef DEBUG_STRATEGIC_CHANGE
2939 if (gDebugFlag2) {
2940 std::cout << " veh=" << myVehicle.getID() << " does not want to leave the bestLane (neighDist=" << neighDist << ")\n";
2941 }
2942#endif
2943 ret |= LCA_STAY | LCA_STRATEGIC;
2944 } else if (right
2945 && bestLaneOffset == 0
2946 && myVehicle.getLane()->getSpeedLimit() > 80. / 3.6
2948 ) {
2949 // let's also regard the case where the vehicle is driving on a highway...
2950 // in this case, we do not want to get to the dead-end of an on-ramp
2951#ifdef DEBUG_STRATEGIC_CHANGE
2952 if (gDebugFlag2) {
2953 std::cout << " veh=" << myVehicle.getID() << " does not want to get stranded on the on-ramp of a highway\n";
2954 }
2955#endif
2956 ret |= LCA_STAY | LCA_STRATEGIC;
2957 }
2958 }
2959 if ((ret & LCA_URGENT) == 0 && getShadowLane() != nullptr &&
2960 // ignore overlap if it goes in the correct direction
2961 bestLaneOffset * myVehicle.getLateralPositionOnLane() <= 0) {
2962 // no decision or decision to stay
2963 // make sure to stay within lane bounds in case the shadow lane ends
2964 //const double requiredDist = MAX2(2 * myVehicle.getLateralOverlap(), getSublaneWidth()) / SUMO_const_laneWidth * laDist;
2965 const double requiredDist = 2 * myVehicle.getLateralOverlap() / SUMO_const_laneWidth * laDist;
2966 double currentShadowDist = -myVehicle.getPositionOnLane();
2967 MSLane* shadowPrev = nullptr;
2968 for (std::vector<MSLane*>::const_iterator it = curr.bestContinuations.begin(); it != curr.bestContinuations.end(); ++it) {
2969 if (*it == nullptr) {
2970 continue;
2971 }
2972 MSLane* shadow = getShadowLane(*it);
2973 if (shadow == nullptr || currentShadowDist >= requiredDist) {
2974 break;
2975 }
2976 if (shadowPrev != nullptr) {
2977 currentShadowDist += shadowPrev->getEdge().getInternalFollowingLengthTo(&shadow->getEdge(), myVehicle.getVClass());
2978 }
2979 currentShadowDist += shadow->getLength();
2980 shadowPrev = shadow;
2981#ifdef DEBUG_STRATEGIC_CHANGE
2982 if (gDebugFlag2) {
2983 std::cout << " shadow=" << shadow->getID() << " currentShadowDist=" << currentShadowDist << "\n";
2984 }
2985#endif
2986 }
2987#ifdef DEBUG_STRATEGIC_CHANGE
2988 if (gDebugFlag2) {
2989 std::cout << " veh=" << myVehicle.getID() << " currentShadowDist=" << currentShadowDist << " requiredDist=" << requiredDist << " overlap=" << myVehicle.getLateralOverlap() << "\n";
2990 }
2991#endif
2992 if (currentShadowDist < requiredDist && currentShadowDist < usableDist) {
2993 myLeftSpace = currentShadowDist;
2995#ifdef DEBUG_STRATEGIC_CHANGE
2996 if (gDebugFlag2) {
2997 std::cout << " must change for shadowLane end latDist=" << latDist << " myLeftSpace=" << myLeftSpace << "\n";
2998 }
2999#endif
3000 ret |= LCA_STRATEGIC | LCA_URGENT | LCA_STAY ;
3001 }
3002 }
3003
3004 // check for overriding TraCI requests
3005#if defined(DEBUG_STRATEGIC_CHANGE) || defined(DEBUG_TRACI)
3006 if (gDebugFlag2) {
3007 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " ret=" << ret;
3008 }
3009#endif
3010 // store state before canceling
3011 getCanceledState(laneOffset) |= ret;
3012 int retTraCI = myVehicle.influenceChangeDecision(ret);
3013 if ((retTraCI & LCA_TRACI) != 0) {
3014 if ((retTraCI & LCA_STAY) != 0) {
3015 ret = retTraCI;
3016 latDist = 0;
3017 } else if (((retTraCI & LCA_RIGHT) != 0 && laneOffset < 0)
3018 || ((retTraCI & LCA_LEFT) != 0 && laneOffset > 0)) {
3019 ret = retTraCI;
3020 latDist = latLaneDist;
3021 }
3022 }
3023#if defined(DEBUG_STRATEGIC_CHANGE) || defined(DEBUG_TRACI)
3024 if (gDebugFlag2) {
3025 std::cout << " reqAfterInfluence=" << toString((LaneChangeAction)retTraCI) << " ret=" << toString((LaneChangeAction)ret) << "\n";
3026 }
3027#endif
3028 return ret;
3029}
3030
3031
3032bool
3033MSLCM_SL2015::mustOvertakeStopped(bool checkCurrent, const MSLane& neighLane, const MSLeaderDistanceInfo& leaders, const MSLeaderDistanceInfo& neighLead,
3034 double posOnLane, double neighDist, bool right, double latLaneDist, double& currentDist, double& latDist) {
3035 bool mustOvertake = false;
3036 const bool checkOverTakeRight = avoidOvertakeRight();
3037 int rightmost;
3038 int leftmost;
3039 const bool curHasStopped = leaders.hasStoppedVehicle();
3040 const int dir = latLaneDist < 0 ? -1 : 1;
3041 const MSLane* neighBeyond = neighLane.getParallelLane(dir);
3042 const bool hasLaneBeyond = checkCurrent && neighBeyond != nullptr && neighBeyond->allowsVehicleClass(myVehicle.getVClass());
3043 UNUSED_PARAMETER(hasLaneBeyond);
3044 if (curHasStopped) {
3045 leaders.getSubLanes(&myVehicle, 0, rightmost, leftmost);
3046 for (int i = rightmost; i <= leftmost; i++) {
3047 const CLeaderDist& leader = leaders[i];
3048 if (leader.first != 0 && leader.first->isStopped() && leader.second < REACT_TO_STOPPED_DISTANCE) {
3049 const double overtakeDist = leader.second + myVehicle.getVehicleType().getLength() + leader.first->getVehicleType().getLengthWithGap();
3050 const double remaining = MIN2(neighDist, currentDist) - posOnLane;
3051#ifdef DEBUG_STRATEGIC_CHANGE
3052 if (DEBUG_COND) {
3053 std::cout << " overtakeDist=" << overtakeDist << " remaining=" << remaining
3054 << " minDistToStopped=" << neighLead.getMinDistToStopped()
3055 << " hasLaneBeyond=" << hasLaneBeyond
3056 << "\n";
3057 }
3058#endif
3059 if (// current destination leaves enough space to overtake the leader
3060 remaining > overtakeDist
3061 // maybe do not overtake on the right at high speed
3062 && (!checkCurrent || !checkOverTakeRight || !right)
3063 && (!neighLead.hasStoppedVehicle() || neighLead.getMinDistToStopped() > overtakeDist /*|| (hasLaneBeyond && hasFreeLaneBeyond(neighBeyond, dir))*/)
3064 //&& (neighLead.first == 0 || !neighLead.first->isStopped()
3065 // // neighboring stopped vehicle leaves enough space to overtake leader
3066 // || neighLead.second > overtakeDist))
3067 ) {
3068 // avoid becoming stuck behind a stopped leader
3069 currentDist = myVehicle.getPositionOnLane() + leader.second;
3070 myLeftSpace = currentDist - posOnLane;
3071 latDist = latLaneDist;
3072 mustOvertake = true;
3073#ifdef DEBUG_STRATEGIC_CHANGE
3074 if (DEBUG_COND) {
3075 std::cout << " veh=" << myVehicle.getID() << " overtake stopped leader=" << leader.first->getID()
3076 << " newCurrentDist=" << currentDist
3077 << " overtakeDist=" << overtakeDist
3078 << " remaining=" << remaining
3079 << "\n";
3080 }
3081#endif
3082 }
3083 }
3084
3085 }
3086 }
3087 if (!mustOvertake && !curHasStopped && neighLead.hasStoppedVehicle()) {
3088 // #todo fix this if the neigh lane has a different width
3089 const double offset = (latLaneDist < 0 ? -1 : 1) * myVehicle.getLane()->getWidth();
3090 neighLead.getSubLanes(&myVehicle, offset, rightmost, leftmost);
3091 for (int i = 0; i < leaders.numSublanes(); i++) {
3092 const CLeaderDist& leader = leaders[i];
3093 if (leader.first != 0 && leader.first->isStopped() && leader.second < REACT_TO_STOPPED_DISTANCE) {
3094 mustOvertake = true;
3095 if (i >= rightmost && i <= leftmost) {
3096 latDist = myVehicle.getLateralOverlap() * (latLaneDist > 0 ? -1 : 1);
3097 break;
3098 }
3099 }
3100 }
3101 }
3102 return mustOvertake;
3103}
3104
3105
3106double
3108 return (state & LCA_STRATEGIC) != 0 ? MAX2(0.0, (1.0 - myPushy * (1 + 0.5 * myImpatience))) : 1.0;
3109}
3110
3111
3112int
3114 const MSLeaderDistanceInfo& leaders,
3115 const MSLeaderDistanceInfo& followers,
3116 const MSLeaderDistanceInfo& blockers,
3117 const MSLeaderDistanceInfo& neighLeaders,
3118 const MSLeaderDistanceInfo& neighFollowers,
3119 const MSLeaderDistanceInfo& neighBlockers,
3120 const MSLane& neighLane,
3121 int laneOffset,
3122 double& latDist,
3123 double& maneuverDist,
3124 int& blocked) {
3125
3126 /* @notes
3127 * vehicles may need to compromise between fulfilling lane change objectives
3128 * (LCA_STRATEGIC, LCA_SPEED etc) and maintaining lateral gap. The minimum
3129 * acceptable lateral gap depends on
3130 * - the cultural context (China vs Europe)
3131 * - the driver agressiveness (willingness to encroach on other vehicles to force them to move laterally as well)
3132 * - see @note in checkBlocking
3133 * - the vehicle type (car vs motorcycle)
3134 * - the current speed
3135 * - the speed difference
3136 * - the importance / urgency of the desired maneuver
3137 *
3138 * the object of this method is to evaluate the above circumstances and
3139 * either:
3140 * - allow the current maneuver (state, latDist)
3141 * - to override the current maneuver with a distance-keeping maneuver
3142 *
3143 *
3144 * laneChangeModel/driver parameters
3145 * - bool pushy (willingness to encroach)
3146 * - float minGap at 100km/h (to be interpolated for lower speeds (assume 0 at speed 0)
3147 * - gapFactors (a factor for each of the change reasons
3148 *
3149 * further assumptions
3150 * - the maximum of egoSpeed and deltaSpeed can be used when interpolating minGap
3151 * - distance keeping to the edges of the road can be ignored (for now)
3152 *
3153 * currentMinGap = minGap * min(1.0, max(v, abs(v - vOther)) / 100) * gapFactor[lc_reason]
3154 *
3155 * */
3156
3158 double gapFactor = computeGapFactor(state);
3159 const double oldLatDist = latDist;
3160 const double oldManeuverDist = maneuverDist;
3162 const int traciState = myVehicle.influenceChangeDecision(state);
3163
3164 // compute gaps after maneuver
3165 const double halfWidth = getWidth() * 0.5;
3166 // if the current maneuver is blocked we will stay where we are
3167 const double oldCenter = myVehicle.getCenterOnEdge();
3168 // surplus gaps. these are used to collect various constraints
3169 // if they do not permit the desired maneuvre, should override it to better maintain distance
3170 // stay within the current edge
3171 double surplusGapRight = oldCenter - halfWidth;
3172 double surplusGapLeft = getLeftBorder(laneOffset != 0) - oldCenter - halfWidth;
3173 const bool stayInLane = (laneOffset == 0
3174 || ((traciState & LCA_STRATEGIC) != 0
3175 && (traciState & LCA_STAY) != 0
3176 // permit wide vehicles to stay on the road
3177 && (surplusGapLeft >= 0 && surplusGapRight >= 0)));
3178
3179 if (isOpposite()) {
3180 std::swap(surplusGapLeft, surplusGapRight);
3181 }
3182#ifdef DEBUG_KEEP_LATGAP
3183 if (gDebugFlag2) {
3184 std::cout << "\n " << SIMTIME << " keepLatGap() laneOffset=" << laneOffset
3185 << " latDist=" << latDist
3186 << " maneuverDist=" << maneuverDist
3187 << " state=" << toString((LaneChangeAction)state)
3188 << " traciState=" << toString((LaneChangeAction)traciState)
3189 << " blocked=" << toString((LaneChangeAction)blocked)
3190 << " gapFactor=" << gapFactor
3191 << " stayInLane=" << stayInLane << "\n"
3192 << " stayInEdge: surplusGapRight=" << surplusGapRight << " surplusGapLeft=" << surplusGapLeft << "\n";
3193 }
3194#endif
3195 // staying within the edge overrides all minGap considerations
3196 if (surplusGapLeft < 0 || surplusGapRight < 0) {
3197 gapFactor = 0;
3198 }
3199
3200 // maintain gaps to vehicles on the current lane
3201 // ignore vehicles that are too far behind
3202 const double netOverlap = -myVehicle.getVehicleType().getLength() * 0.5;
3203 updateGaps(leaders, myVehicle.getLane()->getRightSideOnEdge(), oldCenter, gapFactor, surplusGapRight, surplusGapLeft, true);
3204 updateGaps(followers, myVehicle.getLane()->getRightSideOnEdge(), oldCenter, gapFactor, surplusGapRight, surplusGapLeft, true, netOverlap);
3205
3206 if (laneOffset != 0) {
3207 // maintain gaps to vehicles on the target lane
3208 const double neighRight = getNeighRight(neighLane);
3209 updateGaps(neighLeaders, neighRight, oldCenter, gapFactor, surplusGapRight, surplusGapLeft, true);
3210 updateGaps(neighFollowers, neighRight, oldCenter, gapFactor, surplusGapRight, surplusGapLeft, true, netOverlap);
3211 }
3212#ifdef DEBUG_KEEP_LATGAP
3213 if (gDebugFlag2) {
3214 std::cout << " minGapLat: surplusGapRight=" << surplusGapRight << " surplusGapLeft=" << surplusGapLeft << "\n"
3215 << " lastGaps: right=" << myLastLateralGapRight << " left=" << myLastLateralGapLeft << "\n";
3216 }
3217#endif
3218 // we also need to track the physical gap, in addition to the psychological gap
3219 double physicalGapLeft = myLastLateralGapLeft == NO_NEIGHBOR ? surplusGapLeft : myLastLateralGapLeft;
3220 double physicalGapRight = myLastLateralGapRight == NO_NEIGHBOR ? surplusGapRight : myLastLateralGapRight;
3221
3222 const double halfLaneWidth = myVehicle.getLane()->getWidth() * 0.5;
3223 const double posLat = myVehicle.getLateralPositionOnLane() * (isOpposite() ? -1 : 1);
3224 if (stayInLane || laneOffset == 1) {
3225 // do not move past the right boundary of the current lane (traffic wasn't checked there)
3226 // but assume it's ok to be where we are in case we are already beyond
3227 surplusGapRight = MIN2(surplusGapRight, MAX2(0.0, halfLaneWidth + posLat - halfWidth));
3228 physicalGapRight = MIN2(physicalGapRight, MAX2(0.0, halfLaneWidth + posLat - halfWidth));
3229 }
3230 if (stayInLane || laneOffset == -1) {
3231 // do not move past the left boundary of the current lane (traffic wasn't checked there)
3232 // but assume it's ok to be where we are in case we are already beyond
3233 surplusGapLeft = MIN2(surplusGapLeft, MAX2(0.0, halfLaneWidth - posLat - halfWidth));
3234 physicalGapLeft = MIN2(physicalGapLeft, MAX2(0.0, halfLaneWidth - posLat - halfWidth));
3235 }
3236#ifdef DEBUG_KEEP_LATGAP
3237 if (gDebugFlag2) {
3238 std::cout << " stayInLane: surplusGapRight=" << surplusGapRight << " surplusGapLeft=" << surplusGapLeft << "\n";
3239 }
3240#endif
3241
3242 if (surplusGapRight + surplusGapLeft < 0) {
3243 // insufficient lateral space to fulfill all requirements. apportion space proportionally
3244 if ((state & LCA_CHANGE_REASONS) == 0) {
3245 state |= LCA_SUBLANE;
3246 }
3247 const double equalDeficit = 0.5 * (surplusGapLeft + surplusGapRight);
3248 if (surplusGapRight < surplusGapLeft) {
3249 // shift further to the left but no further than there is physical space
3250 const double delta = MIN2(equalDeficit - surplusGapRight, physicalGapLeft);
3251 latDist = delta;
3252 maneuverDist = delta;
3253#ifdef DEBUG_KEEP_LATGAP
3254 if (gDebugFlag2) {
3255 std::cout << " insufficient latSpace, move left: delta=" << delta << "\n";
3256 }
3257#endif
3258 } else {
3259 // shift further to the right but no further than there is physical space
3260 const double delta = MIN2(equalDeficit - surplusGapLeft, physicalGapRight);
3261 latDist = -delta;
3262 maneuverDist = -delta;
3263#ifdef DEBUG_KEEP_LATGAP
3264 if (gDebugFlag2) {
3265 std::cout << " insufficient latSpace, move right: delta=" << delta << "\n";
3266 }
3267#endif
3268 }
3269 } else {
3270 // sufficient space. move as far as the gaps permit
3271 latDist = MAX2(MIN2(latDist, surplusGapLeft), -surplusGapRight);
3272 maneuverDist = MAX2(MIN2(maneuverDist, surplusGapLeft), -surplusGapRight);
3273 if ((state & LCA_KEEPRIGHT) != 0 && maneuverDist != oldManeuverDist) {
3274 // don't start keepRight unless it can be completed
3275 latDist = oldLatDist;
3276 maneuverDist = oldManeuverDist;
3277 }
3278#ifdef DEBUG_KEEP_LATGAP
3279 if (gDebugFlag2) {
3280 std::cout << " adapted latDist=" << latDist << " maneuverDist=" << maneuverDist << " (old=" << oldLatDist << ")\n";
3281 }
3282#endif
3283 }
3284 // take into account overriding traci sublane-request
3286 // @note: the influence is reset in MSAbstractLaneChangeModel::setOwnState at the end of the lane-changing code for this vehicle
3287 latDist = myVehicle.getInfluencer().getLatDist();
3288 maneuverDist = myVehicle.getInfluencer().getLatDist();
3289 if (latDist < 0) {
3291 } else {
3293 }
3294 state |= LCA_TRACI;
3295#ifdef DEBUG_KEEP_LATGAP
3296 if (gDebugFlag2) {
3297 std::cout << " traci influenced latDist=" << latDist << "\n";
3298 }
3299#endif
3300 }
3301 // if we cannot move in the desired direction, consider the maneuver blocked anyway
3302 const bool nonSublaneChange = (state & (LCA_STRATEGIC | LCA_COOPERATIVE | LCA_SPEEDGAIN | LCA_KEEPRIGHT)) != 0;
3303 const bool traciChange = ((state | traciState) & LCA_TRACI) != 0;
3304 if (nonSublaneChange && !traciChange) {
3305 if ((latDist < NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) && (oldLatDist > 0)) {
3306#ifdef DEBUG_KEEP_LATGAP
3307 if (gDebugFlag2) {
3308 std::cout << " wanted changeToLeft oldLatDist=" << oldLatDist << ", blocked latGap changeToRight\n";
3309 }
3310#endif
3311 latDist = oldLatDist; // restore old request for usage in decideDirection()
3313 } else if ((latDist > -NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) && (oldLatDist < 0)) {
3314#ifdef DEBUG_KEEP_LATGAP
3315 if (gDebugFlag2) {
3316 std::cout << " wanted changeToRight oldLatDist=" << oldLatDist << ", blocked latGap changeToLeft\n";
3317 }
3318#endif
3319 latDist = oldLatDist; // restore old request for usage in decideDirection()
3321 }
3322 }
3323 // if we move, even though we wish to stay, update the change reason (except for TraCI)
3324 if (fabs(latDist) > NUMERICAL_EPS * myVehicle.getActionStepLengthSecs() && oldLatDist == 0) {
3325 state &= (~(LCA_CHANGE_REASONS | LCA_STAY) | LCA_TRACI);
3326 }
3327 // update blocked status
3328 if (fabs(latDist - oldLatDist) > NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
3329#ifdef DEBUG_KEEP_LATGAP
3330 if (gDebugFlag2) {
3331 std::cout << " latDistUpdated=" << latDist << " oldLatDist=" << oldLatDist << "\n";
3332 }
3333#endif
3334 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset, leaders, followers, blockers, neighLeaders, neighFollowers, neighBlockers, nullptr, nullptr, nonSublaneChange);
3335 }
3336 if (fabs(latDist) > NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
3337 state = (state & ~LCA_STAY);
3338 if ((state & LCA_CHANGE_REASONS) == 0) {
3339 state |= LCA_SUBLANE;
3340 }
3341 } else {
3342 if ((state & LCA_SUBLANE) != 0) {
3343 state |= LCA_STAY;
3344 }
3345 // avoid setting blinker due to numerical issues
3346 latDist = 0;
3347 }
3348#if defined(DEBUG_KEEP_LATGAP) || defined(DEBUG_STATE)
3349 if (gDebugFlag2) {
3350 std::cout << " latDist2=" << latDist
3351 << " state2=" << toString((LaneChangeAction)state)
3352 << " lastGapLeft=" << myLastLateralGapLeft
3353 << " lastGapRight=" << myLastLateralGapRight
3354 << " blockedAfter=" << toString((LaneChangeAction)blocked)
3355 << "\n";
3356 }
3357#endif
3358 return state;
3359}
3360
3361
3362void
3363MSLCM_SL2015::updateGaps(const MSLeaderDistanceInfo& others, double foeOffset, double oldCenter, double gapFactor,
3364 double& surplusGapRight, double& surplusGapLeft,
3365 bool saveMinGap, double netOverlap,
3366 double latDist,
3367 std::vector<CLeaderDist>* collectBlockers) {
3368 if (others.hasVehicles()) {
3369 const double halfWidth = getWidth() * 0.5 + NUMERICAL_EPS;
3370 const double baseMinGap = myMinGapLat;
3371 for (int i = 0; i < others.numSublanes(); ++i) {
3372 if (others[i].first != 0 && others[i].second <= 0
3373 && myCFRelated.count(others[i].first) == 0
3374 && (netOverlap == 0 || others[i].second + others[i].first->getVehicleType().getMinGap() < netOverlap)) {
3376 const MSVehicle* foe = others[i].first;
3377 const double res = MSGlobals::gLateralResolution > 0 ? MSGlobals::gLateralResolution : others[i].first->getLane()->getWidth();
3378 double foeRight, foeLeft;
3379 others.getSublaneBorders(i, foeOffset, foeRight, foeLeft);
3380 const double foeCenter = foeRight + 0.5 * res;
3381 const double gap = MIN2(fabs(foeRight - oldCenter), fabs(foeLeft - oldCenter)) - halfWidth;
3383 const double desiredMinGap = baseMinGap * deltaV / LATGAP_SPEED_THRESHOLD;
3384 const double currentMinGap = desiredMinGap * gapFactor; // pushy vehicles may accept a lower lateral gap temporarily
3385 /*
3386 if (netOverlap != 0) {
3387 // foe vehicle is follower with its front ahead of the ego midpoint
3388 // scale gap requirements so it gets lower for foe which are further behind ego
3389 //
3390 // relOverlap approaches 0 as the foe gets closer to the midpoint and it equals 1 if the foe is driving head-to-head
3391 const double relOverlap = 1 - (others[i].second + others[i].first->getVehicleType().getMinGap()) / netOverlap;
3392 currentMinGap *= currOverlap * relOverlap;
3393 }
3394 */
3395#if defined(DEBUG_BLOCKING) || defined(DEBUG_KEEP_LATGAP)
3396 if (debugVehicle()) {
3397 std::cout << " updateGaps"
3398 << " i=" << i
3399 << " foe=" << foe->getID()
3400 << " foeRight=" << foeRight
3401 << " foeLeft=" << foeLeft
3402 << " oldCenter=" << oldCenter
3403 << " gap=" << others[i].second
3404 << " latgap=" << gap
3405 << " currentMinGap=" << currentMinGap
3406 << " surplusGapRight=" << surplusGapRight
3407 << " surplusGapLeft=" << surplusGapLeft
3408 << "\n";
3409 }
3410#endif
3411
3412 // If foe is maneuvering towards ego, reserve some additional distance.
3413 // But don't expect the foe to come closer than currentMinGap if it isn't already there.
3414 // (XXX: How can the ego know the foe's maneuver dist?)
3415 if (foeCenter < oldCenter) { // && foe->getLaneChangeModel().getSpeedLat() > 0) {
3416 const double foeManeuverDist = MAX2(0., foe->getLaneChangeModel().getManeuverDist());
3417 surplusGapRight = MIN3(surplusGapRight, gap - currentMinGap, MAX2(currentMinGap, gap - foeManeuverDist));
3418 } else { //if (foeCenter > oldCenter && foe->getLaneChangeModel().getSpeedLat() < 0) {
3419 const double foeManeuverDist = -MIN2(0., foe->getLaneChangeModel().getManeuverDist());
3420 surplusGapLeft = MIN3(surplusGapLeft, gap - currentMinGap, MAX2(currentMinGap, gap - foeManeuverDist));
3421 }
3422 if (saveMinGap) {
3423 if (foeCenter < oldCenter) {
3424#if defined(DEBUG_BLOCKING) || defined(DEBUG_KEEP_LATGAP)
3425 if (gDebugFlag2 && gap < myLastLateralGapRight) {
3426 std::cout << " new minimum rightGap=" << gap << "\n";
3427 }
3428#endif
3430 } else {
3431#if defined(DEBUG_BLOCKING) || defined(DEBUG_KEEP_LATGAP)
3432 if (gDebugFlag2 && gap < myLastLateralGapLeft) {
3433 std::cout << " new minimum leftGap=" << gap << "\n";
3434 }
3435#endif
3437 }
3438 }
3439 if (collectBlockers != nullptr) {
3440 // check if the vehicle is blocking a desire lane change
3441 if ((foeCenter < oldCenter && latDist < 0 && gap < (desiredMinGap - latDist))
3442 || (foeCenter > oldCenter && latDist > 0 && gap < (desiredMinGap + latDist))) {
3443 collectBlockers->push_back(others[i]);
3444 }
3445 }
3446 }
3447 }
3448 }
3449}
3450
3451
3452double
3454 return myVehicle.getVehicleType().getWidth() + NUMERICAL_EPS;
3455}
3456
3457
3458double
3459MSLCM_SL2015::computeSpeedLat(double latDist, double& maneuverDist, bool urgent) const {
3460 int currentDirection = mySpeedLat >= 0 ? 1 : -1;
3461 int directionWish = latDist >= 0 ? 1 : -1;
3462 double maxSpeedLat = myVehicle.getVehicleType().getMaxSpeedLat();
3463 double accelLat = myAccelLat;
3464 if (!urgent && (myLeftSpace > POSITION_EPS || myMaxSpeedLatFactor < 0)) {
3465 const double speedBound = myMaxSpeedLatStanding + myMaxSpeedLatFactor * myVehicle.getSpeed();
3466 if (myMaxSpeedLatFactor >= 0) {
3467 // speedbound increases with speed and needs an upper bound
3468 maxSpeedLat = MIN2(maxSpeedLat, speedBound);
3469 } else {
3470 // speedbound decreases with speed and needs a lower bound
3471 // (only useful if myMaxSpeedLatStanding > maxSpeedLat)
3472 maxSpeedLat = MAX2(maxSpeedLat, speedBound);
3473 // increase (never decrease) lateral acceleration in proportion
3474 accelLat *= MAX2(1.0, speedBound / myVehicle.getVehicleType().getMaxSpeedLat());
3475 }
3476 }
3478 const double rightVehSide = myVehicle.getRightSideOnEdge();
3479 const double edgeOverlap = MAX2(-rightVehSide, rightVehSide + myVehicle.getVehicleType().getWidth() - myVehicle.getCurrentEdge()->getWidth());
3480 // if vehicle is outside edge bounds. Permit stronger lateral maneuvering
3481 accelLat = MAX2(accelLat, 2 * edgeOverlap);
3482 maxSpeedLat = MAX2(maxSpeedLat, edgeOverlap);
3483 }
3484
3485#ifdef DEBUG_MANEUVER
3486 if (debugVehicle()) {
3487 std::cout << SIMTIME
3488 << " veh=" << myVehicle.getID()
3489 << " computeSpeedLat()"
3490 << " latDist=" << latDist
3491 << " maneuverDist=" << maneuverDist
3492 << " urgent=" << urgent
3493 << " speedLat=" << mySpeedLat
3494 << " currentDirection=" << currentDirection
3495 << " directionWish=" << directionWish
3496 << " myLeftSpace=" << myLeftSpace
3497 << " maxSpeedLat=" << maxSpeedLat
3498 << std::endl;
3499 }
3500#endif
3501 // reduced lateral speed (in the desired direction). Don't change direction against desired.
3502 double speedDecel;
3503 if (directionWish == 1) {
3504 speedDecel = MAX2(mySpeedLat - ACCEL2SPEED(accelLat), 0.);
3505 } else {
3506 speedDecel = MIN2(mySpeedLat + ACCEL2SPEED(accelLat), 0.);
3507 }
3508 // increased lateral speed (in the desired direction)
3509 double speedAccel = MAX2(MIN2(mySpeedLat + directionWish * ACCEL2SPEED(accelLat), maxSpeedLat), -maxSpeedLat);
3510
3511 // can we reach the target distance in a single step? (XXX: assumes "Euler" update)
3512 double speedBound = DIST2SPEED(latDist);
3513 // for lat-gap keeping maneuvres myOrigLatDist may be 0
3514 const double fullLatDist = latDist > 0 ? MIN2(mySafeLatDistLeft, MAX2(maneuverDist, latDist)) : MAX2(-mySafeLatDistRight, MIN2(maneuverDist, latDist));
3515
3516 // update maneuverDist, if safety constraints apply in its direction
3517 if (maneuverDist * latDist > 0) {
3518 maneuverDist = fullLatDist;
3519 }
3520
3521#ifdef DEBUG_MANEUVER
3522 if (debugVehicle()) {
3523 std::cout << " mySafeLatDistRight=" << mySafeLatDistRight
3524 << " mySafeLatDistLeft=" << mySafeLatDistLeft
3525 << " fullLatDist=" << fullLatDist
3526 << " speedAccel=" << speedAccel
3527 << " speedDecel=" << speedDecel
3528 << " speedBound=" << speedBound
3529 << std::endl;
3530 }
3531#endif
3532 if (speedDecel * speedAccel <= 0 && (
3533 // speedAccel and speedDecel bracket speed 0. This means we can end the maneuver
3534 (latDist >= 0 && speedAccel >= speedBound && speedBound >= speedDecel)
3535 || (latDist <= 0 && speedAccel <= speedBound && speedBound <= speedDecel))) {
3536 // we can reach the desired value in this step
3537#ifdef DEBUG_MANEUVER
3538 if (debugVehicle()) {
3539 std::cout << " computeSpeedLat a)\n";
3540 }
3541#endif
3542 return speedBound;
3543 }
3544 // are we currently moving in the wrong direction?
3545 if (latDist * mySpeedLat < 0) {
3546#ifdef DEBUG_MANEUVER
3547 if (debugVehicle()) {
3548 std::cout << " computeSpeedLat b)\n";
3549 }
3550#endif
3551 return emergencySpeedLat(speedAccel);
3552 }
3553 // check if the remaining distance allows to accelerate laterally
3554 double minDistAccel = SPEED2DIST(speedAccel) + currentDirection * MSCFModel::brakeGapEuler(fabs(speedAccel), accelLat, 0); // most we can move in the target direction
3555 if ((fabs(minDistAccel) < fabs(fullLatDist)) || (fabs(minDistAccel - fullLatDist) < NUMERICAL_EPS)) {
3556#ifdef DEBUG_MANEUVER
3557 if (debugVehicle()) {
3558 std::cout << " computeSpeedLat c)\n";
3559 }
3560#endif
3561 return speedAccel;
3562 } else {
3563#ifdef DEBUG_MANEUVER
3564 if (debugVehicle()) {
3565 std::cout << " minDistAccel=" << minDistAccel << "\n";
3566 }
3567#endif
3568 // check if the remaining distance allows to maintain current lateral speed
3569 double minDistCurrent = SPEED2DIST(mySpeedLat) + currentDirection * MSCFModel::brakeGapEuler(fabs(mySpeedLat), accelLat, 0);
3570 if ((fabs(minDistCurrent) < fabs(fullLatDist)) || (fabs(minDistCurrent - fullLatDist) < NUMERICAL_EPS)) {
3571#ifdef DEBUG_MANEUVER
3572 if (debugVehicle()) {
3573 std::cout << " computeSpeedLat d)\n";
3574 }
3575#endif
3576 return mySpeedLat;
3577 }
3578 }
3579 // reduce lateral speed
3580#ifdef DEBUG_MANEUVER
3581 if (debugVehicle()) {
3582 std::cout << " computeSpeedLat e)\n";
3583 }
3584#endif
3585 return emergencySpeedLat(speedDecel);
3586}
3587
3588
3589double
3590MSLCM_SL2015::emergencySpeedLat(double speedLat) const {
3591 // reduce lateral speed for safety purposes
3592 if (speedLat < 0 && SPEED2DIST(-speedLat) > mySafeLatDistRight) {
3593 speedLat = -DIST2SPEED(mySafeLatDistRight);
3594#ifdef DEBUG_MANEUVER
3595 if (debugVehicle()) {
3596 std::cout << " rightDanger speedLat=" << speedLat << "\n";
3597 }
3598#endif
3599 } else if (speedLat > 0 && SPEED2DIST(speedLat) > mySafeLatDistLeft) {
3600 speedLat = DIST2SPEED(mySafeLatDistLeft);
3601#ifdef DEBUG_MANEUVER
3602 if (debugVehicle()) {
3603 std::cout << " leftDanger speedLat=" << speedLat << "\n";
3604 }
3605#endif
3606 }
3607 return speedLat;
3608}
3609
3610
3614 // Check whether the vehicle should adapt its alignment to an upcoming turn
3615 if (myTurnAlignmentDist > 0) {
3616 const std::pair<double, const MSLink*>& turnInfo = myVehicle.getNextTurn();
3617 const LinkDirection turnDir = turnInfo.second == nullptr ? LinkDirection::NODIR : turnInfo.second->getDirection();
3618 const bool indirect = turnInfo.second == nullptr ? false : turnInfo.second->isIndirect();
3619 if (turnInfo.first < myTurnAlignmentDist) {
3620 // Vehicle is close enough to the link to change its default alignment
3621 switch (turnDir) {
3625 if (myVehicle.getLane()->getBidiLane() == nullptr) {
3626 // no left alignment on bidi lane to avoid blocking oncoming traffic
3628 }
3629 break;
3634 break;
3637 default:
3638 break;
3639 }
3640 }
3641 }
3642 return align;
3643}
3644
3645
3646void
3647MSLCM_SL2015::commitManoeuvre(int blocked, int blockedFully,
3648 const MSLeaderDistanceInfo& leaders,
3649 const MSLeaderDistanceInfo& neighLeaders,
3650 const MSLane& neighLane,
3651 double maneuverDist) {
3652 if (!blocked && !blockedFully && !myCanChangeFully) {
3653 // round to full action steps
3654 double secondsToLeaveLane;
3656 secondsToLeaveLane = ceil(fabs(maneuverDist) / myVehicle.getVehicleType().getMaxSpeedLat() / myVehicle.getActionStepLengthSecs()) * myVehicle.getActionStepLengthSecs();
3657 // XXX myAccelLat must be taken into account (refs #3601, see ballistic case for solution)
3658
3659 // XXX This also causes probs: if the difference between the current speed and the committed is higher than the maximal decel,
3660 // the vehicle may pass myLeftSpace before completing the maneuver.
3661 myCommittedSpeed = MIN3(myLeftSpace / secondsToLeaveLane,
3664#if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
3665 if (debugVehicle()) {
3666 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " myCommittedSpeed=" << myCommittedSpeed << " leftSpace=" << myLeftSpace << " secondsToLeave=" << secondsToLeaveLane << "\n";
3667 }
3668#endif
3669 } else {
3670
3671 // Calculate seconds needed for leaving lane assuming start from lateral speed zero, and lat.accel == -lat.decel
3672 secondsToLeaveLane = MSCFModel::estimateArrivalTime(fabs(maneuverDist), 0., 0., myVehicle.getVehicleType().getMaxSpeedLat(), myAccelLat, myAccelLat);
3673 // round to full action steps
3674 secondsToLeaveLane = ceil(secondsToLeaveLane / myVehicle.getActionStepLengthSecs()) * myVehicle.getActionStepLengthSecs();
3675
3676 // committed speed will eventually be pushed into a drive item during the next planMove() step. This item
3677 // will not be read before the next action step at current time + actionStepLength-TS, so we need to schedule the corresponding speed.
3678 const double timeTillActionStep = myVehicle.getActionStepLengthSecs() - TS;
3679 const double nextActionStepSpeed = MAX2(0., myVehicle.getSpeed() + timeTillActionStep * myVehicle.getAcceleration());
3680 double nextLeftSpace;
3681 if (nextActionStepSpeed > 0.) {
3682 nextLeftSpace = myLeftSpace - timeTillActionStep * (myVehicle.getSpeed() + nextActionStepSpeed) * 0.5;
3683 } else if (myVehicle.getAcceleration() == 0) {
3684 nextLeftSpace = myLeftSpace;
3685 } else {
3686 assert(myVehicle.getAcceleration() < 0.);
3687 nextLeftSpace = myLeftSpace + (myVehicle.getSpeed() * myVehicle.getSpeed() / myVehicle.getAcceleration()) * 0.5;
3688 }
3689 const double avoidArrivalSpeed = nextActionStepSpeed + ACCEL2SPEED(MSCFModel::avoidArrivalAccel(
3690 nextLeftSpace, secondsToLeaveLane - timeTillActionStep, nextActionStepSpeed, myVehicle.getCarFollowModel().getEmergencyDecel()));
3691
3692 myCommittedSpeed = MIN3(avoidArrivalSpeed,
3695
3696#if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
3697 if (gDebugFlag2) {
3698 std::cout << SIMTIME
3699 << " veh=" << myVehicle.getID()
3700 << " avoidArrivalSpeed=" << avoidArrivalSpeed
3701 << " currentSpeed=" << myVehicle.getSpeed()
3702 << " myLeftSpace=" << myLeftSpace
3703 << "\n nextLeftSpace=" << nextLeftSpace
3704 << " nextActionStepSpeed=" << nextActionStepSpeed
3705 << " nextActionStepRemainingSeconds=" << secondsToLeaveLane - timeTillActionStep
3706 << "\n";
3707 }
3708#endif
3709 }
3710 myCommittedSpeed = commitFollowSpeed(myCommittedSpeed, maneuverDist, secondsToLeaveLane, leaders, myVehicle.getLane()->getRightSideOnEdge());
3711 myCommittedSpeed = commitFollowSpeed(myCommittedSpeed, maneuverDist, secondsToLeaveLane, neighLeaders, neighLane.getRightSideOnEdge());
3713 myCommittedSpeed = 0;
3714 }
3715#if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
3716 if (gDebugFlag2) {
3717 std::cout << SIMTIME
3718 << " veh=" << myVehicle.getID()
3719 << " secondsToLeave=" << secondsToLeaveLane
3721 << " committed=" << myCommittedSpeed
3722 << "\n";
3723 }
3724#endif
3725 }
3726}
3727
3728double
3729MSLCM_SL2015::commitFollowSpeed(double speed, double latDist, double secondsToLeaveLane, const MSLeaderDistanceInfo& leaders, double foeOffset) const {
3730 if (leaders.hasVehicles()) {
3731 // we distinguish 3 cases
3732 // - vehicles with lateral overlap at the end of the maneuver: try to follow safely
3733 // - vehicles with overlap at the start of the maneuver: avoid collision within secondsToLeaveLane
3734 // - vehicles without overlap: ignore
3735
3736 const double maxDecel = myVehicle.getCarFollowModel().getMaxDecel();
3737 // temporarily use another decel value
3738 MSCFModel& cfmodel = const_cast<MSCFModel&>(myVehicle.getCarFollowModel());
3739 cfmodel.setMaxDecel(maxDecel / getSafetyFactor());
3740
3741 const double vehWidth = getWidth();
3742 const double rightVehSide = myVehicle.getCenterOnEdge() - 0.5 * vehWidth;
3743 const double leftVehSide = rightVehSide + vehWidth;
3744 const double rightVehSideDest = rightVehSide + latDist;
3745 const double leftVehSideDest = leftVehSide + latDist;
3746#if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
3747 if (gDebugFlag2) {
3748 std::cout << " commitFollowSpeed"
3749 << " latDist=" << latDist
3750 << " foeOffset=" << foeOffset
3751 << " vehRight=" << rightVehSide
3752 << " vehLeft=" << leftVehSide
3753 << " destRight=" << rightVehSideDest
3754 << " destLeft=" << leftVehSideDest
3755 << "\n";
3756 }
3757#endif
3758 for (int i = 0; i < leaders.numSublanes(); ++i) {
3759 CLeaderDist vehDist = leaders[i];
3760 if (vehDist.first != 0) {
3761 const MSVehicle* leader = vehDist.first;
3762 // only check the current stripe occuped by foe (transform into edge-coordinates)
3763 double foeRight, foeLeft;
3764 leaders.getSublaneBorders(i, foeOffset, foeRight, foeLeft);
3765#if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
3766 if (gDebugFlag2) {
3767 std::cout << " foe=" << vehDist.first->getID()
3768 << " gap=" << vehDist.second
3769 << " secGap=" << myVehicle.getCarFollowModel().getSecureGap(&myVehicle, leader, myVehicle.getSpeed(), leader->getSpeed(), leader->getCarFollowModel().getMaxDecel())
3770 << " foeRight=" << foeRight
3771 << " foeLeft=" << foeLeft
3772 << " overlapBefore=" << overlap(rightVehSide, leftVehSide, foeRight, foeLeft)
3773 << " overlapDest=" << overlap(rightVehSideDest, leftVehSideDest, foeRight, foeLeft)
3774 << "\n";
3775 }
3776#endif
3777 if (overlap(rightVehSideDest, leftVehSideDest, foeRight, foeLeft)) {
3778 // case 1
3779 const double vSafe = myVehicle.getCarFollowModel().followSpeed(
3780 &myVehicle, speed, vehDist.second, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel());
3781 speed = MIN2(speed, vSafe);
3782#if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
3783 if (gDebugFlag2) {
3784 std::cout << " case1 vsafe=" << vSafe << " speed=" << speed << "\n";
3785 }
3786#endif
3787 } else if (overlap(rightVehSide, leftVehSide, foeRight, foeLeft)) {
3788 // case 2
3789 const double vSafe = myVehicle.getCarFollowModel().followSpeedTransient(
3790 secondsToLeaveLane,
3791 &myVehicle, speed, vehDist.second, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel());
3792 speed = MIN2(speed, vSafe);
3793#if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
3794 if (gDebugFlag2) {
3795 std::cout << " case2 vsafe=" << vSafe << " speed=" << speed << "\n";
3796 }
3797#endif
3798 }
3799 }
3800 }
3801 // restore original deceleration
3802 cfmodel.setMaxDecel(maxDecel);
3803
3804 }
3805 return speed;
3806}
3807
3808double
3810 return 1 / ((1 + 0.5 * myImpatience) * myAssertive);
3811}
3812
3813double
3815 return myOppositeParam <= 0 ? std::numeric_limits<double>::max() : 1 / myOppositeParam;
3816}
3817
3818
3819std::string
3820MSLCM_SL2015::getParameter(const std::string& key) const {
3822 return toString(myStrategicParam);
3823 } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_PARAM)) {
3825 } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_PARAM)) {
3826 return toString(mySpeedGainParam);
3827 } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_PARAM)) {
3828 return toString(myKeepRightParam);
3829 } else if (key == toString(SUMO_ATTR_LCA_OPPOSITE_PARAM)) {
3830 return toString(myOppositeParam);
3831 } else if (key == toString(SUMO_ATTR_LCA_SUBLANE_PARAM)) {
3832 return toString(mySublaneParam);
3833 } else if (key == toString(SUMO_ATTR_MINGAP_LAT)) {
3834 return toString(myMinGapLat);
3835 } else if (key == toString(SUMO_ATTR_LCA_PUSHY)) {
3836 return toString(myPushy);
3837 } else if (key == toString(SUMO_ATTR_LCA_PUSHYGAP)) {
3838 return toString((myPushy - 1) * myMinGapLat);
3839 } else if (key == toString(SUMO_ATTR_LCA_ASSERTIVE)) {
3840 return toString(myAssertive);
3841 } else if (key == toString(SUMO_ATTR_LCA_IMPATIENCE)) {
3842 return toString(myImpatience);
3843 } else if (key == toString(SUMO_ATTR_LCA_TIME_TO_IMPATIENCE)) {
3845 } else if (key == toString(SUMO_ATTR_LCA_ACCEL_LAT)) {
3846 return toString(myAccelLat);
3847 } else if (key == toString(SUMO_ATTR_LCA_LOOKAHEADLEFT)) {
3848 return toString(myLookaheadLeft);
3849 } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAINRIGHT)) {
3850 return toString(mySpeedGainRight);
3851 } else if (key == toString(SUMO_ATTR_LCA_LANE_DISCIPLINE)) {
3852 return toString(myLaneDiscipline);
3853 } else if (key == toString(SUMO_ATTR_LCA_SIGMA)) {
3854 return toString(mySigma);
3859 } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD)) {
3861 } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME)) {
3865 } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_SPEED)) {
3867 } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATSTANDING)) {
3869 } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATFACTOR)) {
3871 } else if (key == toString(SUMO_ATTR_LCA_MAXDISTLATSTANDING)) {
3873 // access to internal state for debugging in sumo-gui (not documented since it may change at any time)
3874 } else if (key == "speedGainProbabilityRight") {
3876 } else if (key == "speedGainProbabilityLeft") {
3878 } else if (key == "keepRightProbability") {
3880 } else if (key == "lookAheadSpeed") {
3881 return toString(myLookAheadSpeed);
3882 } else if (key == "sigmaState") {
3883 return toString(mySigmaState);
3884 // motivation relative to threshold
3885 } else if (key == "speedGainRP") {
3887 } else if (key == "speedGainLP") {
3889 } else if (key == "keepRightP") {
3891 }
3892 throw InvalidArgument("Parameter '" + key + "' is not supported for laneChangeModel of type '" + toString(myModel) + "'");
3893}
3894
3895void
3896MSLCM_SL2015::setParameter(const std::string& key, const std::string& value) {
3897 double doubleValue;
3898 try {
3899 doubleValue = StringUtils::toDouble(value);
3900 } catch (NumberFormatException&) {
3901 throw InvalidArgument("Setting parameter '" + key + "' requires a number for laneChangeModel of type '" + toString(myModel) + "'");
3902 }
3904 myStrategicParam = doubleValue;
3905 } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_PARAM)) {
3906 myCooperativeParam = doubleValue;
3907 } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_PARAM)) {
3908 mySpeedGainParam = doubleValue;
3909 } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_PARAM)) {
3910 myKeepRightParam = doubleValue;
3911 } else if (key == toString(SUMO_ATTR_LCA_OPPOSITE_PARAM)) {
3912 myOppositeParam = doubleValue;
3913 } else if (key == toString(SUMO_ATTR_LCA_SUBLANE_PARAM)) {
3914 mySublaneParam = doubleValue;
3915 } else if (key == toString(SUMO_ATTR_MINGAP_LAT)) {
3916 myMinGapLat = doubleValue;
3917 } else if (key == toString(SUMO_ATTR_LCA_PUSHY)) {
3918 myPushy = doubleValue;
3919 } else if (key == toString(SUMO_ATTR_LCA_PUSHYGAP)) {
3920 myPushy = 1 - doubleValue / myMinGapLat;
3921 } else if (key == toString(SUMO_ATTR_LCA_ASSERTIVE)) {
3922 myAssertive = doubleValue;
3923 } else if (key == toString(SUMO_ATTR_LCA_IMPATIENCE)) {
3924 myImpatience = doubleValue;
3925 myMinImpatience = doubleValue;
3926 } else if (key == toString(SUMO_ATTR_LCA_TIME_TO_IMPATIENCE)) {
3927 myTimeToImpatience = doubleValue;
3928 } else if (key == toString(SUMO_ATTR_LCA_ACCEL_LAT)) {
3929 myAccelLat = doubleValue;
3931 myTurnAlignmentDist = doubleValue;
3932 } else if (key == toString(SUMO_ATTR_LCA_LOOKAHEADLEFT)) {
3933 myLookaheadLeft = doubleValue;
3934 } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAINRIGHT)) {
3935 mySpeedGainRight = doubleValue;
3936 } else if (key == toString(SUMO_ATTR_LCA_LANE_DISCIPLINE)) {
3937 myLaneDiscipline = doubleValue;
3938 } else if (key == toString(SUMO_ATTR_LCA_SIGMA)) {
3939 mySigma = doubleValue;
3941 myKeepRightAcceptanceTime = doubleValue;
3943 myOvertakeDeltaSpeedFactor = doubleValue;
3944 } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD)) {
3945 mySpeedGainLookahead = doubleValue;
3946 } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME)) {
3947 mySpeedGainRemainTime = doubleValue;
3949 myRoundaboutBonus = doubleValue;
3950 } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_SPEED)) {
3951 myCooperativeSpeed = doubleValue;
3952 } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATSTANDING)) {
3953 myMaxSpeedLatStanding = doubleValue;
3954 } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATFACTOR)) {
3955 myMaxSpeedLatFactor = doubleValue;
3956 } else if (key == toString(SUMO_ATTR_LCA_MAXDISTLATSTANDING)) {
3957 myMaxDistLatStanding = doubleValue;
3958 // access to internal state
3959 } else if (key == "speedGainProbabilityRight") {
3960 mySpeedGainProbabilityRight = doubleValue;
3961 } else if (key == "speedGainProbabilityLeft") {
3962 mySpeedGainProbabilityLeft = doubleValue;
3963 } else if (key == "keepRightProbability") {
3964 myKeepRightProbability = doubleValue;
3965 } else if (key == "lookAheadSpeed") {
3966 myLookAheadSpeed = doubleValue;
3967 } else if (key == "sigmaState") {
3968 mySigmaState = doubleValue;
3969 } else {
3970 throw InvalidArgument("Setting parameter '" + key + "' is not supported for laneChangeModel of type '" + toString(myModel) + "'");
3971 }
3973}
3974
3975
3976int
3978 int laneOffset,
3980 int blocked,
3981 const std::pair<MSVehicle*, double>& leader,
3982 const std::pair<MSVehicle*, double>& follower,
3983 const std::pair<MSVehicle*, double>& neighLead,
3984 const std::pair<MSVehicle*, double>& neighFollow,
3985 const MSLane& neighLane,
3986 const std::vector<MSVehicle::LaneQ>& preb,
3987 MSVehicle** lastBlocked,
3988 MSVehicle** firstBlocked) {
3989
3990 const LaneChangeAction alternatives = LCA_NONE; // @todo pas this data
3991
3992#ifdef DEBUG_WANTSCHANGE
3993 if (DEBUG_COND) {
3994 std::cout << "\nWANTS_CHANGE\n" << SIMTIME
3995 //<< std::setprecision(10)
3996 << " veh=" << myVehicle.getID()
3997 << " lane=" << myVehicle.getLane()->getID()
3998 << " neigh=" << neighLane.getID()
3999 << " pos=" << myVehicle.getPositionOnLane()
4000 << " posLat=" << myVehicle.getLateralPositionOnLane()
4001 << " speed=" << myVehicle.getSpeed()
4002 << " considerChangeTo=" << (laneOffset == -1 ? "right" : "left")
4003 << "\n";
4004 }
4005#endif
4006
4007 double latDist = 0;
4008 const double laneWidth = myVehicle.getLane()->getWidth();
4009 MSLeaderDistanceInfo leaders(leader, laneWidth);
4010 MSLeaderDistanceInfo followers(follower, laneWidth);
4011 MSLeaderDistanceInfo blockers(std::make_pair((MSVehicle*)nullptr, -1), laneWidth);
4012 MSLeaderDistanceInfo neighLeaders(neighLead, laneWidth);
4013 MSLeaderDistanceInfo neighFollowers(neighFollow, laneWidth);
4014 MSLeaderDistanceInfo neighBlockers(std::make_pair((MSVehicle*)nullptr, -1), laneWidth);
4015
4016 double maneuverDist;
4017 int result = _wantsChangeSublane(laneOffset,
4018 alternatives,
4019 leaders, followers, blockers,
4020 neighLeaders, neighFollowers, neighBlockers,
4021 neighLane, preb,
4022 lastBlocked, firstBlocked, latDist, maneuverDist, blocked);
4023
4024 myCanChangeFully = true;
4025 // ignore sublane motivation
4026 result &= ~LCA_SUBLANE;
4027 result |= getLCA(result, latDist);
4028
4029#if defined(DEBUG_WANTSCHANGE) || defined(DEBUG_STATE)
4030 if (DEBUG_COND) {
4031 if (result & LCA_WANTS_LANECHANGE) {
4032 std::cout << SIMTIME
4033 << " veh=" << myVehicle.getID()
4034 << " wantsChangeTo=" << (laneOffset == -1 ? "right" : "left")
4035 << ((result & LCA_URGENT) ? " (urgent)" : "")
4036 << ((result & LCA_CHANGE_TO_HELP) ? " (toHelp)" : "")
4037 << ((result & LCA_STRATEGIC) ? " (strat)" : "")
4038 << ((result & LCA_COOPERATIVE) ? " (coop)" : "")
4039 << ((result & LCA_SPEEDGAIN) ? " (speed)" : "")
4040 << ((result & LCA_KEEPRIGHT) ? " (keepright)" : "")
4041 << ((result & LCA_TRACI) ? " (traci)" : "")
4042 << ((blocked & LCA_BLOCKED) ? " (blocked)" : "")
4043 << ((blocked & LCA_OVERLAPPING) ? " (overlap)" : "")
4044 << "\n\n\n";
4045 }
4046 }
4047#endif
4048
4049 return result;
4050}
4051
4052
4053double
4054MSLCM_SL2015::getLeftBorder(bool checkOpposite) const {
4055 return (myVehicle.getLane()->getEdge().getWidth()
4056 + ((myVehicle.getLane()->getParallelOpposite() != nullptr && checkOpposite) ? myVehicle.getLane()->getParallelOpposite()->getEdge().getWidth() : 0));
4057}
4058
4059double
4061 if (isOpposite()) {
4063 } else {
4064 return myVehicle.getCenterOnEdge();
4065 }
4066}
4067
4068double
4069MSLCM_SL2015::getNeighRight(const MSLane& neighLane) const {
4070 if (isOpposite()) {
4072 } else if ((&myVehicle.getLane()->getEdge() != &neighLane.getEdge())) {
4074 } else {
4075 // the normal case
4076 return neighLane.getRightSideOnEdge();
4077 }
4078}
4079
4080
4081bool
4082MSLCM_SL2015::preventSliding(double maneuverDist) const {
4083 // prevent wide maneuvers with unsufficient forward space
4084 if (fabs(maneuverDist) > myMaxDistLatStanding) {
4085 // emergency vehicles should not be restricted (TODO solve this with LCA_URGENT)
4087 return false;
4088 }
4089 const double brakeGap = myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed());
4090 const bool isSlide = fabs(maneuverDist) > myMaxDistLatStanding + brakeGap * fabs(myMaxSpeedLatFactor);
4091#ifdef DEBUG_SLIDING
4092 if (gDebugFlag2) {
4093 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " bgap=" << brakeGap << " maneuverDist=" << maneuverDist
4094 << " mds=" << myMaxDistLatStanding << " isSlide=" << isSlide << "\n";
4095 }
4096#endif
4097 return isSlide;
4098 }
4099 return false;
4100}
4101
4102bool
4103MSLCM_SL2015::wantsKeepRight(double keepRightProb) const {
4105}
4106
4107
4108bool
4109MSLCM_SL2015::saveBlockerLength(double length, double foeLeftSpace) {
4110 const bool canReserve = MSLCHelper::canSaveBlockerLength(myVehicle, length, myLeftSpace);
4111 if (!isOpposite() && (canReserve || myLeftSpace > foeLeftSpace)) {
4113 if (myLeftSpace == 0 && foeLeftSpace < 0) {
4114 // called from opposite overtaking, myLeftSpace must be initialized
4116 }
4117 return true;
4118 } else {
4119 return false;
4120 }
4121}
4122
4123
4124bool
4128/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define JAM_FACTOR
#define MAGIC_OFFSET
#define HELP_DECEL_FACTOR
#define LOOK_AHEAD_MIN_SPEED
#define LCA_RIGHT_IMPATIENCE
#define LOOK_FORWARD
#define HELP_OVERTAKE
#define REACT_TO_STOPPED_DISTANCE
#define KEEP_RIGHT_TIME
#define RELGAIN_NORMALIZATION_MIN_SPEED
#define CUT_IN_LEFT_SPEED_THRESHOLD
#define MAX_ONRAMP_LENGTH
#define MIN_FALLBEHIND
#define URGENCY
#define LOOK_AHEAD_SPEED_MEMORY
#define TURN_LANE_DIST
#define ARRIVALPOS_LAT_THRESHOLD
#define SPEEDGAIN_MEMORY_FACTOR
#define LOOK_AHEAD_MIN_SPEED
#define SPEEDGAIN_DECAY_FACTOR
#define LATGAP_SPEED_THRESHOLD
#define GAIN_PERCEPTION_THRESHOLD
#define LATGAP_SPEED_THRESHOLD2
std::pair< const MSVehicle *, double > CLeaderDist
std::pair< const MSPerson *, double > PersonDist
Definition MSPModel.h:41
#define STEPS2TIME(x)
Definition SUMOTime.h:55
#define SPEED2DIST(x)
Definition SUMOTime.h:45
#define ACCEL2SPEED(x)
Definition SUMOTime.h:51
#define TS
Definition SUMOTime.h:42
#define SIMTIME
Definition SUMOTime.h:62
#define DIST2SPEED(x)
Definition SUMOTime.h:47
LatAlignmentDefinition
Possible ways to choose the lateral alignment, i.e., how vehicles align themselves within their lane.
@ RIGHT
drive on the right side
@ GIVEN
The alignment as offset is given.
@ DEFAULT
No information given; use default.
@ LEFT
drive on the left side
@ ARBITRARY
maintain the current alignment
@ NICE
align with the closest sublane border
@ COMPACT
align with the rightmost sublane that allows keeping the current speed
@ CENTER
drive in the middle
@ SVC_EMERGENCY
public emergency vehicles
@ RIGHT
At the rightmost side of the lane.
@ GIVEN
The position is given.
@ DEFAULT
No information given; use default.
@ LEFT
At the leftmost side of the lane.
@ CENTER
At the center of the lane.
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)....
@ PARTLEFT
The link is a partial left direction.
@ RIGHT
The link is a (hard) right direction.
@ TURN
The link is a 180 degree turn.
@ LEFT
The link is a (hard) left direction.
@ STRAIGHT
The link is a straight direction.
@ TURN_LEFTHAND
The link is a 180 degree turn (left-hand network)
@ PARTRIGHT
The link is a partial right direction.
@ NODIR
The link has no direction (is a dead end link)
LaneChangeAction
The state of a vehicle's lane-change behavior.
@ LCA_BLOCKED_LEFT
blocked left
@ LCA_KEEPRIGHT
The action is due to the default of keeping right "Rechtsfahrgebot".
@ LCA_CHANGE_TO_HELP
@ LCA_BLOCKED
blocked in all directions
@ LCA_URGENT
The action is urgent (to be defined by lc-model)
@ LCA_BLOCKED_BY_RIGHT_LEADER
The vehicle is blocked by right leader.
@ LCA_STAY
Needs to stay on the current lane.
@ LCA_SUBLANE
used by the sublane model
@ LCA_BLOCKED_BY_LEADER
blocked by leader
@ LCA_AMBACKBLOCKER
@ LCA_BLOCKED_BY_LEFT_FOLLOWER
The vehicle is blocked by left follower.
@ LCA_AMBLOCKINGLEADER
@ LCA_AMBLOCKINGFOLLOWER_DONTBRAKE
@ LCA_COOPERATIVE
The action is done to help someone else.
@ LCA_OVERLAPPING
The vehicle is blocked being overlapping.
@ LCA_LEFT
Wants go to the left.
@ LCA_BLOCKED_RIGHT
blocked right
@ LCA_BLOCKED_BY_RIGHT_FOLLOWER
The vehicle is blocked by right follower.
@ LCA_STRATEGIC
The action is needed to follow the route (navigational lc)
@ LCA_AMBACKBLOCKER_STANDING
@ LCA_CHANGE_REASONS
reasons of lane change
@ LCA_TRACI
The action is due to a TraCI request.
@ LCA_SPEEDGAIN
The action is due to the wish to be faster (tactical lc)
@ LCA_WANTS_LANECHANGE
lane can change
@ LCA_RIGHT
Wants go to the right.
@ LCA_BLOCKED_BY_FOLLOWER
blocker by follower
@ LCA_BLOCKED_BY_LEFT_LEADER
@ LCA_AMBLOCKINGFOLLOWER
@ SUMO_ATTR_LCA_PUSHY
@ SUMO_ATTR_LCA_COOPERATIVE_SPEED
@ SUMO_ATTR_LCA_ASSERTIVE
@ SUMO_ATTR_LCA_LANE_DISCIPLINE
@ SUMO_ATTR_LCA_TURN_ALIGNMENT_DISTANCE
@ SUMO_ATTR_LCA_PUSHYGAP
@ SUMO_ATTR_LCA_LOOKAHEADLEFT
@ SUMO_ATTR_LCA_SPEEDGAIN_PARAM
@ SUMO_ATTR_LCA_MAXDISTLATSTANDING
@ SUMO_ATTR_LCA_IMPATIENCE
@ SUMO_ATTR_LCA_COOPERATIVE_ROUNDABOUT
@ SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD
@ SUMO_ATTR_LCA_MAXSPEEDLATFACTOR
@ SUMO_ATTR_LCA_MAXSPEEDLATSTANDING
@ SUMO_ATTR_LCA_KEEPRIGHT_PARAM
@ SUMO_ATTR_LCA_COOPERATIVE_PARAM
@ SUMO_ATTR_LCA_OPPOSITE_PARAM
@ SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME
@ SUMO_ATTR_MINGAP_LAT
@ SUMO_ATTR_LCA_OVERTAKE_DELTASPEED_FACTOR
@ SUMO_ATTR_LCA_SUBLANE_PARAM
@ SUMO_ATTR_LCA_SIGMA
@ SUMO_ATTR_LCA_ACCEL_LAT
@ SUMO_ATTR_LCA_STRATEGIC_PARAM
@ SUMO_ATTR_LCA_KEEPRIGHT_ACCEPTANCE_TIME
@ SUMO_ATTR_LCA_TIME_TO_IMPATIENCE
@ SUMO_ATTR_LCA_SPEEDGAINRIGHT
int gPrecision
the precision for floating point outputs
Definition StdDefs.cpp:27
bool gDebugFlag2
Definition StdDefs.cpp:44
const double SUMO_const_laneWidth
Definition StdDefs.h:52
T MIN3(T a, T b, T c)
Definition StdDefs.h:93
T MIN2(T a, T b)
Definition StdDefs.h:80
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition StdDefs.h:62
T MAX2(T a, T b)
Definition StdDefs.h:86
T MAX3(T a, T b, T c)
Definition StdDefs.h:100
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
A class responsible for exchanging messages between cars involved in lane-change interaction.
Interface for lane-change models.
double getForwardPos() const
get vehicle position relative to the forward direction lane
virtual double getExtraReservation(int bestLaneOffset, double neighExtraDist=0) const
virtual void setOwnState(const int state)
int myPreviousState
lane changing state from the previous simulation step
double getManeuverDist() const
Returns the remaining unblocked distance for the current maneuver. (only used by sublane model)
int myOwnState
The current state of the vehicle.
MSLane * getShadowLane() const
Returns the lane the vehicle's shadow is on during continuous/sublane lane change.
double myCommittedSpeed
the speed when committing to a change maneuver
virtual LatAlignmentDefinition getDesiredAlignment() const
static bool myAllowOvertakingRight
whether overtaking on the right is permitted
void addLCSpeedAdvice(const double vSafe, bool ownAdvice=true)
Takes a vSafe (speed advice for speed in the next simulation step), converts it into an acceleration ...
const LaneChangeModel myModel
the type of this model
bool cancelRequest(int state, int laneOffset)
whether the influencer cancels the given request
std::vector< std::pair< double, bool > > myLCAccelerationAdvices
double getMaxSpeedLat2() const
return the max of maxSpeedLat and lcMaxSpeedLatStanding
const MSCFModel & getCarFollowModel() const
The vehicle's car following model.
double mySpeedLat
the current lateral speed
MSVehicle & myVehicle
The vehicle this lane-changer belongs to.
double myLastLateralGapLeft
the minimum lateral gaps to other vehicles that were found when last changing to the left and right
virtual bool debugVehicle() const
whether the current vehicles shall be debugged
virtual double getArrivalPos() const
Returns this vehicle's desired arrivalPos for its current route (may change on reroute)
const SUMOVehicleParameter & getParameter() const
Returns the vehicle's parameter (including departure definition)
double getLength() const
Returns the vehicle's length.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
double getWaitingSeconds() const
Returns the number of seconds waited (speed was lesser than 0.1m/s)
const MSStop & getNextStop() const
SUMOVehicleClass getVClass() const
Returns the vehicle's access class.
const MSRoute & getRoute() const
Returns the current route.
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
The car-following model abstraction.
Definition MSCFModel.h:57
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
static double gapExtrapolation(const double duration, const double currentGap, double v1, double v2, double a1=0, double a2=0, const double maxV1=std::numeric_limits< double >::max(), const double maxV2=std::numeric_limits< double >::max())
return the resulting gap if, starting with gap currentGap, two vehicles continue with constant accele...
virtual double minNextSpeedEmergency(double speed, const MSVehicle *const veh=0) const
Returns the minimum speed after emergency braking, given the current speed (depends on the numerical ...
virtual double followSpeedTransient(double duration, const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel) const
Computes the vehicle's follow speed that avoids a collision for the given amount of time.
double getEmergencyDecel() const
Get the vehicle type's maximal physically possible deceleration [m/s^2].
Definition MSCFModel.h:277
static double brakeGapEuler(const double speed, const double decel, const double headwayTime)
Definition MSCFModel.cpp:92
static double avoidArrivalAccel(double dist, double time, double speed, double maxDecel)
Computes the acceleration needed to arrive not before the given time.
virtual double minNextSpeed(double speed, const MSVehicle *const veh=0) const
Returns the minimum speed given the current speed (depends on the numerical update scheme and its ste...
virtual void setMaxDecel(double decel)
Sets a new value for maximal comfortable deceleration [m/s^2].
Definition MSCFModel.h:564
@ LANE_CHANGE
the return value is used for lane change calculations
Definition MSCFModel.h:87
virtual double getSecureGap(const MSVehicle *const veh, const MSVehicle *const, const double speed, const double leaderSpeed, const double leaderMaxDecel) const
Returns the minimum gap to reserve if the leader is braking at maximum (>=0)
double getMaxAccel() const
Get the vehicle type's maximum acceleration [m/s^2].
Definition MSCFModel.h:261
double brakeGap(const double speed) const
Returns the distance the vehicle needs to halt including driver's reaction time tau (i....
Definition MSCFModel.h:408
double getMaxDecel() const
Get the vehicle type's maximal comfortable deceleration [m/s^2].
Definition MSCFModel.h:269
static double estimateArrivalTime(double dist, double speed, double maxSpeed, double accel)
Computes the time needed to travel a distance dist given an initial speed and constant acceleration....
virtual double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0, const CalcReason usage=CalcReason::CURRENT) const =0
Computes the vehicle's follow speed (no dawdling)
double stopSpeed(const MSVehicle *const veh, const double speed, double gap, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling)
Definition MSCFModel.h:173
A road/street connecting two junctions.
Definition MSEdge.h:77
const std::set< MSTransportable *, ComparatorNumericalIdLess > & getPersons() const
Returns this edge's persons set.
Definition MSEdge.h:204
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition MSEdge.h:168
double getInternalFollowingLengthTo(const MSEdge *followerAfterInternal, SUMOVehicleClass vClass) const
returns the length of all internal edges on the junction until reaching the non-internal edge followe...
Definition MSEdge.cpp:932
bool canChangeToOpposite() const
whether this edge allows changing to the opposite direction edge
Definition MSEdge.cpp:1340
bool isInternal() const
return whether this edge is an internal edge
Definition MSEdge.h:268
double getWidth() const
Returns the edges's width (sum over all lanes)
Definition MSEdge.h:664
const std::vector< double > getSubLaneSides() const
Returns the right side offsets of this edge's sublanes.
Definition MSEdge.h:669
static double gLateralResolution
Definition MSGlobals.h:100
static bool gSemiImplicitEulerUpdate
Definition MSGlobals.h:53
static bool gLefthand
Whether lefthand-drive is being simulated.
Definition MSGlobals.h:174
static bool canSaveBlockerLength(const MSVehicle &veh, double requested, double leftSpace)
static double getSpeedPreservingSecureGap(const MSVehicle &leader, const MSVehicle &follower, double currentGap, double leaderPlannedSpeed)
static double getRoundaboutDistBonus(const MSVehicle &veh, double bonusParam, const MSVehicle::LaneQ &curr, const MSVehicle::LaneQ &neigh, const MSVehicle::LaneQ &best)
static bool updateBlockerLength(const MSVehicle &veh, MSVehicle *blocker, int lcaCounter, double leftSpace, bool reliefConnection, double &leadingBlockerLength)
static bool divergentRoute(const MSVehicle &v1, const MSVehicle &v2)
return whether the vehicles are on the same junction but on divergent paths
double mySafeLatDistRight
the lateral distance the vehicle can safely move in the currently considered direction
static bool overlap(double right, double left, double right2, double left2)
return whether the given intervals overlap
double _patchSpeed(double min, const double wanted, double max, const MSCFModel &cfModel)
double informLeaders(int blocked, int dir, const std::vector< CLeaderDist > &blockers, double remainingSeconds)
double myRoundaboutBonus
void commitManoeuvre(int blocked, int blockedFully, const MSLeaderDistanceInfo &leaders, const MSLeaderDistanceInfo &neighLeaders, const MSLane &neighLane, double maneuverDist)
commit to lane change maneuver potentially overriding safe speed
std::set< const MSVehicle * > myCFRelated
set of vehicles that are in a car-following relationship with ego (leader of followers)
void prepareStep() override
double myKeepRightProbability
double myMinImpatience
double commitFollowSpeed(double speed, double latDist, double secondsToLeaveLane, const MSLeaderDistanceInfo &leaders, double foeOffset) const
compute speed when committing to an urgent change that is safe in regard to leading vehicles
double getLeftBorder(bool checkOpposite=true) const
return current edge width optionally extended by opposite direction lane width
double myChangeProbThresholdRight
double informLeader(int blocked, int dir, const CLeaderDist &neighLead, double remainingSeconds)
double mySafeLatDistLeft
MSLCM_SL2015(MSVehicle &v)
int computeSublaneShift(const MSEdge *prevEdge, const MSEdge *curEdge)
compute shift so that prevSublane + shift = newSublane
double patchSpeed(const double min, const double wanted, const double max, const MSCFModel &cfModel) override
Called to adapt the speed in order to allow a lane change. It uses information on LC-related desired ...
double getSafetyFactor() const override
return factor for modifying the safety constraints of the car-following model
double myCooperativeSpeed
double computeSpeedLat(double latDist, double &maneuverDist, bool urgent) const override
decides the next lateral speed depending on the remaining lane change distance to be covered and upda...
std::vector< double > myExpectedSublaneSpeeds
expected travel speeds on all sublanes on the current edge(!)
double getWidth() const
return the width of this vehicle (padded for numerical stability)
bool myCanChangeFully
whether the current lane changing maneuver can be finished in a single step
void changed() override
int wantsChange(int laneOffset, MSAbstractLaneChangeModel::MSLCMessager &msgPass, int blocked, const std::pair< MSVehicle *, double > &leader, const std::pair< MSVehicle *, double > &follower, const std::pair< MSVehicle *, double > &neighLead, const std::pair< MSVehicle *, double > &neighFollow, const MSLane &neighLane, const std::vector< MSVehicle::LaneQ > &preb, MSVehicle **lastBlocked, MSVehicle **firstBlocked) override
Called to examine whether the vehicle wants to change using the given laneOffset (this is a wrapper a...
bool mustOvertakeStopped(bool checkCurrent, const MSLane &neighLane, const MSLeaderDistanceInfo &leaders, const MSLeaderDistanceInfo &neighLead, double posOnLane, double neighDist, bool right, double latLaneDist, double &currentDist, double &latDist)
double myLaneDiscipline
bool outsideEdge() const
whether the ego vehicle is driving outside edgebounds
bool myDontBrake
flag to prevent speed adaptation by slowing down
std::string getParameter(const std::string &key) const override
try to retrieve the given parameter from this device. Throw exception for unsupported key
bool wantsKeepRight(double keepRightProb) const
check against thresholds
double forecastAverageSpeed(double vSafe, double vMax, double gap, double vLeader) const
estimate average speed over mySpeedGainLookahead time
int checkStrategicChange(int ret, const MSLane &neighLane, int laneOffset, const MSLeaderDistanceInfo &leaders, const MSLeaderDistanceInfo &neighLeaders, const MSVehicle::LaneQ &curr, const MSVehicle::LaneQ &neigh, const MSVehicle::LaneQ &best, int bestLaneOffset, bool changeToBest, double &currentDist, double neighDist, double laDist, double roundaboutBonus, double latLaneDist, bool checkOpposite, double &latDist)
compute strategic lane change actions TODO: Better documentation, refs #2
void updateCFRelated(const MSLeaderDistanceInfo &vehicles, double foeOffset, bool leaders)
find leaders/followers that are already in a car-following relationship with ego
bool debugVehicle() const override
whether the current vehicles shall be debugged
double myAccelLat
int wantsChangeSublane(int laneOffset, LaneChangeAction alternatives, const MSLeaderDistanceInfo &leaders, const MSLeaderDistanceInfo &followers, const MSLeaderDistanceInfo &blockers, const MSLeaderDistanceInfo &neighLeaders, const MSLeaderDistanceInfo &neighFollowers, const MSLeaderDistanceInfo &neighBlockers, const MSLane &neighLane, const std::vector< MSVehicle::LaneQ > &preb, MSVehicle **lastBlocked, MSVehicle **firstBlocked, double &latDist, double &maneuverDist, int &blocked) override
Called to examine whether the vehicle wants to change with the given laneOffset (using the sublane mo...
LatAlignmentDefinition getDesiredAlignment() const override
double mySpeedGainProbabilityRight
a value for tracking the probability that a change to the right is beneficial
double myLookAheadSpeed
int slowDownForBlocked(MSVehicle **blocked, int state)
compute useful slowdowns for blocked vehicles
void initDerivedParameters()
init cached parameters derived directly from model parameters
int keepLatGap(int state, const MSLeaderDistanceInfo &leaders, const MSLeaderDistanceInfo &followers, const MSLeaderDistanceInfo &blockers, const MSLeaderDistanceInfo &neighLeaders, const MSLeaderDistanceInfo &neighFollowers, const MSLeaderDistanceInfo &neighBlockers, const MSLane &neighLane, int laneOffset, double &latDist, double &maneuverDist, int &blocked)
check whether lateral gap requirements are met override the current maneuver if necessary
bool tieBrakeLeader(const MSVehicle *veh) const
bool currentDistAllows(double dist, int laneOffset, double lookForwardDist)
CLeaderDist getLongest(const MSLeaderDistanceInfo &ldi) const
get the longest vehicle in the given info
double myCooperativeParam
double getNeighRight(const MSLane &neighLane) const
return the right offset of the neighboring lane relative to the current edge
double computeSpeedGain(double latDistSublane, double defaultNextSpeed) const
compute speedGain when moving by the given amount
double emergencySpeedLat(double speedLat) const
avoid unsafe lateral speed (overruling lcAccelLat)
double myKeepRightAcceptanceTime
double mySigmaState
void updateGaps(const MSLeaderDistanceInfo &others, double foeOffset, double oldCenter, double gapFactor, double &surplusGapRight, double &surplusGapLeft, bool saveMinGap=false, double netOverlap=0, double latDist=0, std::vector< CLeaderDist > *collectBlockers=0)
check remaining lateral gaps for the given foe vehicles and optionally update minimum lateral gaps
double mySpeedGainParam
virtual void updateSafeLatDist(const double travelledLatDist) override
Updates the value of safe lateral distances (mySafeLatDistLeft and mySafeLatDistRight) during maneuve...
const MSEdge * myLastEdge
expected travel speeds on all sublanes on the current edge(!)
double getOppositeSafetyFactor() const override
return factor for modifying the safety constraints for opposite-diretction overtaking of the car-foll...
StateAndDist decideDirection(StateAndDist sd1, StateAndDist sd2) const override
decide in which direction to move in case both directions are desirable
double myImpatience
double myOppositeParam
double myLeftSpace
std::pair< double, int > Info
information regarding save velocity (unused) and state flags of the ego vehicle
void msg(const CLeaderDist &cld, double speed, int state)
send a speed recommendation to the given vehicle
double mySpeedGainRemainTime
double myLookaheadLeft
int checkBlocking(const MSLane &neighLane, double &latDist, double maneuverDist, int laneOffset, const MSLeaderDistanceInfo &leaders, const MSLeaderDistanceInfo &followers, const MSLeaderDistanceInfo &blockers, const MSLeaderDistanceInfo &neighLeaders, const MSLeaderDistanceInfo &neighFollowers, const MSLeaderDistanceInfo &neighBlockers, std::vector< CLeaderDist > *collectLeadBlockers=0, std::vector< CLeaderDist > *collectFollowBlockers=0, bool keepLatGapManeuver=false, double gapFactor=0, int *retBlockedFully=0)
restrict latDist to permissible speed and determine blocking state depending on that distance
double myStrategicParam
double getVehicleCenter() const
return vehicle position relative to the current edge (extend by another virtual lane for opposite-dir...
int _wantsChangeSublane(int laneOffset, LaneChangeAction alternatives, const MSLeaderDistanceInfo &leaders, const MSLeaderDistanceInfo &followers, const MSLeaderDistanceInfo &blockers, const MSLeaderDistanceInfo &neighLeaders, const MSLeaderDistanceInfo &neighFollowers, const MSLeaderDistanceInfo &neighBlockers, const MSLane &neighLane, const std::vector< MSVehicle::LaneQ > &preb, MSVehicle **lastBlocked, MSVehicle **firstBlocked, double &latDist, double &maneuverDist, int &blocked)
helper function for doing the actual work
double getLateralDrift()
get lateral drift for the current step
double computeGapFactor(int state) const
compute the gap factor for the given state
double getPosLat()
get lateral position of this vehicle
bool preventSliding(double maneuverDist) const
bool isBidi(const MSLane *lane) const
check whether lane is an upcoming bidi lane
void * inform(void *info, MSVehicle *sender) override
double myMinGapLat
void informFollower(int blocked, int dir, const CLeaderDist &neighFollow, double remainingSeconds, double plannedSpeed)
decide whether we will try cut in before the follower or allow to be overtaken
void setParameter(const std::string &key, const std::string &value) override
try to set the given parameter for this laneChangeModel. Throw exception for unsupported key
bool saveBlockerLength(double length, double foeLeftSpace) override
reserve space at the end of the lane to avoid dead locks
double myOvertakeDeltaSpeedFactor
double myTurnAlignmentDist
double myLeadingBlockerLength
void setOwnState(const int state) override
int checkBlockingVehicles(const MSVehicle *ego, const MSLeaderDistanceInfo &vehicles, int laneOffset, double latDist, double foeOffset, bool leaders, double &safeLatGapRight, double &safeLatGapLeft, std::vector< CLeaderDist > *collectBlockers=0) const
check whether any of the vehicles overlaps with ego
void informFollowers(int blocked, int dir, const std::vector< CLeaderDist > &blockers, double remainingSeconds, double plannedSpeed)
call informFollower for multiple followers
double mySpeedGainLookahead
virtual ~MSLCM_SL2015()
double mySpeedLossProbThreshold
void resetState() override
bool myCFRelatedReady
double mySpeedGainProbabilityLeft
a value for tracking the probability that a change to the left is beneficial
double mySpeedGainRight
static LaneChangeAction getLCA(int state, double latDist)
compute lane change action from desired lateral distance
double myChangeProbThresholdLeft
void updateExpectedSublaneSpeeds(const MSLeaderDistanceInfo &ahead, int sublaneOffset, int laneIndex) override
update expected speeds for each sublane of the current edge
bool currentDistDisallows(double dist, int laneOffset, double lookForwardDist)
double myTimeToImpatience
static int lowest_bit(int changeReason)
return the most important change reason
static CLeaderDist getSlowest(const MSLeaderDistanceInfo &ldi)
get the slowest vehicle in the given info
bool amBlockingFollowerPlusNB()
double myKeepRightParam
double mySublaneParam
Representation of a lane in the micro simulation.
Definition MSLane.h:84
std::pair< const MSPerson *, double > nextBlocking(double minPos, double minRight, double maxLeft, double stopTime=0, bool bidi=false) const
This is just a wrapper around MSPModel::nextBlocking. You should always check using hasPedestrians be...
Definition MSLane.cpp:4575
MSLane * getParallelLane(int offset, bool includeOpposite=true) const
Returns the lane with the given offset parallel to this one or 0 if it does not exist.
Definition MSLane.cpp:2851
double getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition MSLane.h:597
double getLength() const
Returns the lane's length.
Definition MSLane.h:611
bool allowsVehicleClass(SUMOVehicleClass vclass) const
Definition MSLane.h:930
double getVehicleMaxSpeed(const SUMOTrafficObject *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
Definition MSLane.h:574
double getRightSideOnEdge() const
Definition MSLane.h:1200
bool hasPedestrians() const
whether the lane has pedestrians on it
Definition MSLane.cpp:4568
int getIndex() const
Returns the lane's index.
Definition MSLane.h:647
MSLane * getOpposite() const
return the neighboring opposite direction lane for lane changing or nullptr
Definition MSLane.cpp:4384
MSLane * getBidiLane() const
retrieve bidirectional lane or nullptr
Definition MSLane.cpp:4680
MSLane * getParallelOpposite() const
return the opposite direction lane of this lanes edge or nullptr
Definition MSLane.cpp:4390
MSEdge & getEdge() const
Returns the lane's edge.
Definition MSLane.h:769
const MSLane * getNormalPredecessorLane() const
get normal lane leading to this internal lane, for normal lanes, the lane itself is returned
Definition MSLane.cpp:3242
double getWidth() const
Returns the lane's width.
Definition MSLane.h:640
const std::vector< MSLink * > & getLinkCont() const
returns the container with all links !!!
Definition MSLane.h:729
int getRightmostSublane() const
Definition MSLane.h:1204
saves leader/follower vehicles and their distances relative to an ego vehicle
virtual std::string toString() const
print a debugging representation
double getMinDistToStopped() const
return minimum distance to a stopped vehicle or max double
bool hasStoppedVehicle() const
whether a stopped vehicle is leader
void getSublaneBorders(int sublane, double latOffset, double &rightSide, double &leftSide) const
int numSublanes() const
bool hasVehicles() const
void getSubLanes(const MSVehicle *veh, double latOffset, int &rightmost, int &leftmost) const
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition MSNet.h:334
const MSEdge * getLastEdge() const
returns the destination edge
Definition MSRoute.cpp:91
const MSLane * lane
The lane to stop at (microsim only)
Definition MSStop.h:50
double getLatDist() const
Definition MSVehicle.h:1591
double changeRequestRemainingSeconds(const SUMOTime currentTime) const
Return the remaining number of seconds of the current laneTimeLine assuming one exists.
bool ignoreOverlap() const
Definition MSVehicle.h:1599
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
double getRightSideOnEdge(const MSLane *lane=0) const
Get the vehicle's lateral position on the edge of the given lane (or its current edge if lane == 0)
double getLeftSideOnEdge(const MSLane *lane=0) const
Get the vehicle's lateral position on the edge of the given lane (or its current edge if lane == 0)
bool isActive() const
Returns whether the current simulation step is an action point for the vehicle.
Definition MSVehicle.h:628
const std::pair< double, const MSLink * > & getNextTurn()
Get the distance and direction of the next upcoming turn for the vehicle (within its look-ahead range...
Definition MSVehicle.h:827
MSAbstractLaneChangeModel & getLaneChangeModel()
double getActionStepLengthSecs() const
Returns the vehicle's action step length in secs, i.e. the interval between two action points.
Definition MSVehicle.h:533
int influenceChangeDecision(int state)
allow TraCI to influence a lane change decision
double nextStopDist() const
return the distance to the next stop or doubleMax if there is none.
Definition MSVehicle.h:1035
double getAcceleration() const
Returns the vehicle's acceleration in m/s (this is computed as the last step's mean acceleration in c...
Definition MSVehicle.h:514
const std::vector< MSLane * > & getBestLanesContinuation() const
Returns the best sequence of lanes to continue the route starting at myLane.
int getBestLaneOffset() const
double lateralDistanceToLane(const int offset) const
Get the minimal lateral distance required to move fully onto the lane at given offset.
const MSEdge * getCurrentEdge() const
Returns the edge the vehicle is currently at (possibly an internal edge or nullptr)
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition MSVehicle.h:581
double getLastStepDist() const
Get the distance the vehicle covered in the previous timestep.
Definition MSVehicle.h:381
Influencer & getInfluencer()
double getLateralPositionOnLane() const
Get the vehicle's lateral position on the lane.
Definition MSVehicle.h:413
bool congested() const
double getSpeed() const
Returns the vehicle's current speed.
Definition MSVehicle.h:490
const std::vector< LaneQ > & getBestLanes() const
Returns the description of best lanes to use in order to continue the route.
const MSCFModel & getCarFollowModel() const
Returns the vehicle's car following model definition.
Definition MSVehicle.h:969
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition MSVehicle.h:374
double getLateralOverlap() const
return the amount by which the vehicle extends laterally outside it's primary lane
bool hasInfluencer() const
whether the vehicle is individually influenced (via TraCI or special parameters)
Definition MSVehicle.h:1690
double getCenterOnEdge(const MSLane *lane=0) const
Get the vehicle's lateral position on the edge of the given lane (or its current edge if lane == 0)
double getLengthWithGap() const
Get vehicle's length including the minimum gap [m].
double getWidth() const
Get the width which vehicles of this class shall have when being drawn.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type's vehicle class.
const LatAlignmentDefinition & getPreferredLateralAlignment() const
Get vehicle's preferred lateral alignment procedure.
double getMinGap() const
Get the free space in front of vehicles of this class.
double getMaxSpeedLat() const
Get vehicle's maximum lateral speed [m/s].
double getLength() const
Get vehicle's length [m].
double getPreferredLateralAlignmentOffset() const
Get vehicle's preferred lateral alignment offset (in m from center line)
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition Named.h:67
const std::string & getID() const
Returns the id.
Definition Named.h:74
void step(double dt)
evolve for a time step of length dt.
double arrivalPosLat
(optional) The lateral position the vehicle shall arrive on
ArrivalPosLatDefinition arrivalPosLatProcedure
Information how the vehicle shall choose the lateral arrival position.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
#define UNUSED_PARAMETER(x)
#define DEBUG_COND
Definition json.hpp:4471
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:21884
bool sameDirection(const StateAndDist &other) const
A structure representing the best lanes for continuing the current route starting at 'lane'.
Definition MSVehicle.h:857
double length
The overall length which may be driven when using this lane without a lane change.
Definition MSVehicle.h:861
std::vector< MSLane * > bestContinuations
Definition MSVehicle.h:877
MSLane * lane
The described lane.
Definition MSVehicle.h:859
int bestLaneOffset
The (signed) number of lanes to be crossed to get to the lane which allows to continue the drive.
Definition MSVehicle.h:869
double occupation
The overall vehicle sum on consecutive lanes which can be passed without a lane change.
Definition MSVehicle.h:865