LCOV - code coverage report
Current view: top level - src/microsim/lcmodels - MSLCM_SL2015.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 91.9 % 1401 1288
Test Date: 2026-05-24 16:29:35 Functions: 100.0 % 62 62

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2013-2026 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    MSLCM_SL2015.cpp
      15              : /// @author  Jakob Erdmann
      16              : /// @date    Tue, 06.10.2015
      17              : ///
      18              : // A lane change model for heterogeneous traffic (based on sub-lanes)
      19              : /****************************************************************************/
      20              : #include <config.h>
      21              : 
      22              : #include <iostream>
      23              : #include <utils/common/RandHelper.h>
      24              : #include <utils/common/StringUtils.h>
      25              : #include <microsim/MSEdge.h>
      26              : #include <microsim/MSLane.h>
      27              : #include <microsim/MSLink.h>
      28              : #include <microsim/MSNet.h>
      29              : #include <microsim/MSDriverState.h>
      30              : #include <microsim/MSGlobals.h>
      31              : #include <microsim/MSStop.h>
      32              : #include <microsim/MSStoppingPlace.h>
      33              : #include <microsim/transportables/MSTransportableControl.h>
      34              : #include <microsim/transportables/MSPModel.h>
      35              : #include "MSLCHelper.h"
      36              : #include "MSLCM_SL2015.h"
      37              : 
      38              : // ===========================================================================
      39              : // variable definitions
      40              : // ===========================================================================
      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              : // ===========================================================================
     115       825893 : MSLCM_SL2015::MSLCM_SL2015(MSVehicle& v) :
     116              :     MSAbstractLaneChangeModel(v, LaneChangeModel::SL2015),
     117       825893 :     mySpeedGainProbabilityRight(0),
     118       825893 :     mySpeedGainProbabilityLeft(0),
     119       825893 :     myKeepRightProbability(0),
     120       825893 :     myLeadingBlockerLength(0),
     121       825893 :     myLeftSpace(0),
     122       825893 :     myLookAheadSpeed(LOOK_AHEAD_MIN_SPEED),
     123       825893 :     myLastEdge(nullptr),
     124       825893 :     myCanChangeFully(true),
     125       825893 :     mySafeLatDistRight(0),
     126       825893 :     mySafeLatDistLeft(0),
     127       825893 :     myStrategicParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_STRATEGIC_PARAM, 1)),
     128       825893 :     myCooperativeParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_PARAM, 1)),
     129       825893 :     mySpeedGainParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_PARAM, 1)),
     130       825893 :     myKeepRightParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_KEEPRIGHT_PARAM, 1)),
     131       825893 :     myOppositeParam(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_OPPOSITE_PARAM, 1)),
     132       825893 :     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       825893 :     myMinGapLat(v.getVehicleType().getMinGapLat()),
     135       825893 :     myPushy(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_PUSHY,
     136      1651689 :             1 - (v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_PUSHYGAP,
     137       825893 :                     MAX2(NUMERICAL_EPS, myMinGapLat)) /
     138       825893 :                  MAX2(NUMERICAL_EPS, myMinGapLat)))),
     139       825893 :     myImpatience(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_IMPATIENCE, 0)),
     140       825893 :     myMinImpatience(myImpatience),
     141       825893 :     myTimeToImpatience(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_TIME_TO_IMPATIENCE, std::numeric_limits<double>::max())),
     142       825893 :     myAccelLat(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_ACCEL_LAT, 1.0)),
     143       825893 :     myTurnAlignmentDist(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_TURN_ALIGNMENT_DISTANCE, 0.0)),
     144       825893 :     myLookaheadLeft(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_LOOKAHEADLEFT, 2.0)),
     145       825893 :     mySpeedGainRight(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAINRIGHT, 0.1)),
     146       825893 :     myLaneDiscipline(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_LANE_DISCIPLINE, 0.0)),
     147       825893 :     mySpeedGainLookahead(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD, 5)),
     148       825893 :     mySpeedGainRemainTime(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME, 20)),
     149       825893 :     myRoundaboutBonus(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_ROUNDABOUT, myCooperativeParam)),
     150       825893 :     myCooperativeSpeed(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_COOPERATIVE_SPEED, myCooperativeParam)),
     151       825893 :     myKeepRightAcceptanceTime(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_KEEPRIGHT_ACCEPTANCE_TIME, -1)),
     152       825893 :     myOvertakeDeltaSpeedFactor(v.getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_OVERTAKE_DELTASPEED_FACTOR, 0)),
     153       825893 :     mySigmaState(0) {
     154       825893 :     initDerivedParameters();
     155       825893 : }
     156              : 
     157      1651770 : MSLCM_SL2015::~MSLCM_SL2015() {
     158       825885 :     changed();
     159      1651770 : }
     160              : 
     161              : 
     162              : void
     163      1103583 : MSLCM_SL2015::initDerivedParameters() {
     164      1103583 :     if (mySpeedGainParam <= 0) {
     165         2235 :         myChangeProbThresholdRight = std::numeric_limits<double>::max();
     166         2235 :         myChangeProbThresholdLeft = std::numeric_limits<double>::max();
     167              :     } else {
     168      1101348 :         myChangeProbThresholdRight = (0.2 / mySpeedGainRight) / mySpeedGainParam;
     169      1101348 :         myChangeProbThresholdLeft = 0.2 / mySpeedGainParam;
     170              :     }
     171      1103583 :     mySpeedLossProbThreshold = (-0.1 + (1 - mySublaneParam));
     172      1103583 : }
     173              : 
     174              : 
     175              : bool
     176        14927 : MSLCM_SL2015::debugVehicle() const {
     177        14927 :     return DEBUG_COND;
     178              : }
     179              : 
     180              : 
     181              : int
     182    117006016 : MSLCM_SL2015::wantsChangeSublane(
     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              : 
     197    117006016 :     gDebugFlag2 = DEBUG_COND;
     198    197867713 :     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    117006016 :     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    117006016 :     result = keepLatGap(result, leaders, followers, blockers,
     224              :                         neighLeaders, neighFollowers, neighBlockers,
     225              :                         neighLane, laneOffset, latDist, maneuverDist, blocked);
     226              : 
     227    117006016 :     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    117006016 :     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    117006016 :     gDebugFlag2 = false;
     259    117006016 :     return result;
     260              : }
     261              : 
     262              : void
     263     80922311 : MSLCM_SL2015::setOwnState(const int state) {
     264     80922311 :     MSAbstractLaneChangeModel::setOwnState(state);
     265     80922311 :     if (myVehicle.isActive()) {
     266     80922295 :         if ((state & (LCA_STRATEGIC | LCA_SPEEDGAIN)) != 0 && (state & LCA_BLOCKED) != 0) {
     267      8651609 :             myImpatience = MIN2(1.0, myImpatience + myVehicle.getActionStepLengthSecs() / myTimeToImpatience);
     268              :         } else {
     269              :             // impatience decays only to the driver-specific level
     270     79675999 :             myImpatience = MAX2(myMinImpatience, myImpatience - myVehicle.getActionStepLengthSecs() / myTimeToImpatience);
     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     80922295 :         if ((state & LCA_STAY) != 0) {
     282     70377603 :             myCanChangeFully = true;
     283              : //            if (DEBUG_COND) {
     284              : //                std::cout << "    myCanChangeFully=true\n";
     285              : //            }
     286              :         }
     287              :     }
     288     80922311 : }
     289              : 
     290              : 
     291              : void
     292      3970211 : MSLCM_SL2015::updateSafeLatDist(const double travelledLatDist) {
     293      3970211 :     mySafeLatDistLeft -= travelledLatDist;
     294      3970211 :     mySafeLatDistRight += travelledLatDist;
     295              : 
     296      3970211 :     if (fabs(mySafeLatDistLeft) < NUMERICAL_EPS) {
     297        53943 :         mySafeLatDistLeft = 0.;
     298              :     }
     299      3970211 :     if (fabs(mySafeLatDistRight) < NUMERICAL_EPS) {
     300        91586 :         mySafeLatDistRight = 0.;
     301              :     }
     302      3970211 : }
     303              : 
     304              : 
     305              : double
     306     82350167 : MSLCM_SL2015::patchSpeed(const double min, const double wanted, const double max, const MSCFModel& cfModel) {
     307     82350167 :     gDebugFlag2 = DEBUG_COND;
     308              :     // negative min speed may be passed when using ballistic updated
     309     82350167 :     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     82350167 :     gDebugFlag2 = false;
     326     82350167 :     return newSpeed;
     327              : }
     328              : 
     329              : 
     330              : double
     331     82350167 : MSLCM_SL2015::_patchSpeed(double min, const double wanted, double max, const MSCFModel& cfModel) {
     332     82350167 :     if (wanted <= 0) {
     333              :         return wanted;
     334              :     }
     335              : 
     336     78137395 :     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     78137395 :     if (myLeadingBlockerLength != 0) {
     343       347096 :         double space = myLeftSpace - myLeadingBlockerLength - POSITION_EPS;
     344              : #ifdef DEBUG_PATCHSPEED
     345              :         if (gDebugFlag2) {
     346              :             std::cout << SIMTIME << " veh=" << myVehicle.getID() << " myLeadingBlockerLength=" << myLeadingBlockerLength << " space=" << space << "\n";
     347              :         }
     348              : #endif
     349       347096 :         if (space >= 0) {
     350              :             // compute speed for decelerating towards a place which allows the blocking leader to merge in in front
     351       342199 :             double safe = cfModel.stopSpeed(&myVehicle, myVehicle.getSpeed(), space, MSCFModel::CalcReason::LANE_CHANGE);
     352              :             max = MIN2(max, safe);
     353              :             // if we are approaching this place
     354       342199 :             if (safe < wanted) {
     355        46759 :                 if (safe < min) {
     356         2433 :                     const double vMinEmergency = myVehicle.getCarFollowModel().minNextSpeedEmergency(myVehicle.getSpeed(), &myVehicle);
     357         2433 :                     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     78137395 :     const double coopWeight = MAX2(0.0, MIN2(1.0, myCooperativeSpeed));
     373    103786019 :     for (auto i : myLCAccelerationAdvices) {
     374              :         double accel = i.first;
     375     25648624 :         double v = myVehicle.getSpeed() + ACCEL2SPEED(accel);
     376     25648624 :         if (v >= min && v <= max) {
     377      8709435 :             if (i.second) {
     378              :                 // own advice, no scaling needed
     379              :                 nVSafe = MIN2(v, nVSafe);
     380              :             } else {
     381      3799524 :                 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     78137395 :     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     75711677 :     if ((state & LCA_WANTS_LANECHANGE) != 0 && (state & LCA_BLOCKED) != 0) {
     415      4976857 :         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       580539 :             return (max + wanted) / 2.0;
     424      4396318 :         } else if ((state & LCA_COOPERATIVE) != 0) {
     425              :             // only minor adjustments in speed should be done
     426       447691 :             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       411196 :                 return (min + wanted) / 2.0;
     433              :             }
     434        36495 :             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        36495 :                 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     74683447 :     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       944451 :         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              : 
     507              : void*
     508     11986297 : MSLCM_SL2015::inform(void* info, MSVehicle* sender) {
     509              :     Info* pinfo = (Info*) info;
     510     11986297 :     if (pinfo->first >= 0) {
     511      7333291 :         addLCSpeedAdvice(pinfo->first, false);
     512              :     }
     513              :     //myOwnState &= 0xffffffff; // reset all bits of MyLCAEnum but only those
     514     11986297 :     myOwnState |= pinfo->second;
     515              : #ifdef DEBUG_INFORM
     516              :     if (gDebugFlag2 || DEBUG_COND || sender->getLaneChangeModel().debugVehicle()) {
     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     11986297 :     delete pinfo;
     528     11986297 :     return (void*) true;
     529              : }
     530              : 
     531              : 
     532              : void
     533     11986297 : MSLCM_SL2015::msg(const CLeaderDist& cld, double speed, int state) {
     534              :     assert(cld.first != 0);
     535     11986297 :     ((MSVehicle*)cld.first)->getLaneChangeModel().inform(new Info(speed, state), &myVehicle);
     536     11986297 : }
     537              : 
     538              : 
     539              : double
     540      7634856 : MSLCM_SL2015::informLeader(int blocked,
     541              :                            int dir,
     542              :                            const CLeaderDist& neighLead,
     543              :                            double remainingSeconds) {
     544     15269712 :     double plannedSpeed = MIN2(myVehicle.getSpeed(),
     545      7634856 :                                myVehicle.getCarFollowModel().stopSpeed(&myVehicle, myVehicle.getSpeed(), myLeftSpace - myLeadingBlockerLength));
     546     20322323 :     for (auto i : myLCAccelerationAdvices) {
     547     12687467 :         double v = myVehicle.getSpeed() + ACCEL2SPEED(i.first);
     548     12687467 :         if (v >= myVehicle.getSpeed() - ACCEL2SPEED(myVehicle.getCarFollowModel().getMaxDecel())) {
     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      7634856 :     if ((blocked & LCA_BLOCKED_BY_LEADER) != 0 && neighLead.first != 0) {
     559              :         const MSVehicle* nv = neighLead.first;
     560      6306812 :         if (MSLCHelper::divergentRoute(myVehicle, *nv)) {
     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="
     566              :                                        << myVehicle.getCarFollowModel().getSecureGap(&myVehicle, nv, myVehicle.getSpeed(), nv->getSpeed(), nv->getCarFollowModel().getMaxDecel()) << "\n";
     567              : #endif
     568              :         // decide whether we want to overtake the leader or follow it
     569      6302787 :         const double dv = plannedSpeed - nv->getSpeed();
     570      6302787 :         const double overtakeDist = (neighLead.second // drive to back of follower
     571      6302787 :                                      + nv->getVehicleType().getLengthWithGap() // drive to front of follower
     572      6302787 :                                      + myVehicle.getVehicleType().getLength() // ego back reaches follower front
     573     18908361 :                                      + nv->getCarFollowModel().getSecureGap( // save gap to follower
     574      6302787 :                                          nv, &myVehicle, nv->getSpeed(), myVehicle.getSpeed(), myVehicle.getCarFollowModel().getMaxDecel()));
     575              : 
     576      6302787 :         if ((dv < myOvertakeDeltaSpeedFactor * myVehicle.getLane()->getSpeedLimit() + NUMERICAL_EPS
     577              :                 // overtaking on the right on an uncongested highway is forbidden (noOvertakeLCLeft)
     578      2066097 :                 || (dir == LCA_MLEFT && !myVehicle.congested() && !myAllowOvertakingRight)
     579              :                 // not enough space to overtake? (we will start to brake when approaching a dead end)
     580      4103490 :                 || myLeftSpace - myLeadingBlockerLength - myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed()) < overtakeDist
     581              :                 // not enough time to overtake?
     582      1913309 :                 || dv * remainingSeconds < overtakeDist)
     583      6722055 :                 && (!neighLead.first->isStopped() || (isOpposite() && neighLead.second >= 0))) {
     584              :             // cannot overtake
     585      4650600 :             msg(neighLead, -1, dir | LCA_AMBLOCKINGLEADER);
     586              :             // slow down smoothly to follow leader
     587      4650600 :             const double targetSpeed = getCarFollowModel().followSpeed(
     588      4650600 :                                            &myVehicle, myVehicle.getSpeed(), neighLead.second, nv->getSpeed(), nv->getCarFollowModel().getMaxDecel());
     589      4650600 :             if (targetSpeed < myVehicle.getSpeed()) {
     590              :                 // slow down smoothly to follow leader
     591       970054 :                 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       728605 :                 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       728605 :                 addLCSpeedAdvice(nextSpeed);
     607       728605 :                 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      3921995 :                 addLCSpeedAdvice(targetSpeed);
     621      3921995 :                 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
     631              :                           << " secureGap=" << nv->getCarFollowModel().getSecureGap(nv, &myVehicle, nv->getSpeed(), myVehicle.getSpeed(), myVehicle.getCarFollowModel().getMaxDecel())
     632              :                           << " overtakeDist=" << overtakeDist
     633              :                           << " leftSpace=" << myLeftSpace
     634              :                           << " blockerLength=" << myLeadingBlockerLength
     635              :                           << "\n";
     636              :             }
     637              : #endif
     638              :             // overtaking, leader should not accelerate
     639      1652187 :             msg(neighLead, nv->getSpeed(), dir | LCA_AMBLOCKINGLEADER);
     640      1652187 :             return -1;
     641              :         }
     642      1328044 :     } 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;
     646      1328044 :         if (MSGlobals::gSemiImplicitEulerUpdate) {
     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      1102775 :             nextNVSpeed = nv->getSpeed() - HELP_OVERTAKE; // conservative
     650      1102775 :             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       225269 :             nextNVSpeed = nv->getSpeed() - nvMaxDecel * myVehicle.getActionStepLengthSecs(); // conservative
     657              :             // Estimated gap reduction until next action step if own speed stays constant
     658       225269 :             dv = SPEED2DIST(myVehicle.getSpeed() - nextNVSpeed);
     659              :         }
     660      1328044 :         const double targetSpeed = getCarFollowModel().followSpeed(
     661      1328044 :                                        &myVehicle, myVehicle.getSpeed(), neighLead.second - dv, nextNVSpeed, nv->getCarFollowModel().getMaxDecel());
     662      1328044 :         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
     669              :                       << " needGap=" << myVehicle.getCarFollowModel().getSecureGap(&myVehicle, nv, myVehicle.getSpeed(), nv->getSpeed(), nv->getCarFollowModel().getMaxDecel())
     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              : 
     682              : void
     683      5683804 : MSLCM_SL2015::informFollower(int blocked,
     684              :                              int dir,
     685              :                              const CLeaderDist& neighFollow,
     686              :                              double remainingSeconds,
     687              :                              double plannedSpeed) {
     688      5683804 :     if ((blocked & LCA_BLOCKED_BY_FOLLOWER) != 0 && neighFollow.first != 0) {
     689              :         const MSVehicle* nv = neighFollow.first;
     690      4180313 :         if (MSLCHelper::divergentRoute(myVehicle, *nv)) {
     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="
     696              :                                        << nv->getCarFollowModel().getSecureGap(nv, &myVehicle, nv->getSpeed(), myVehicle.getSpeed(), myVehicle.getCarFollowModel().getMaxDecel()) << "\n";
     697              : #endif
     698              : 
     699              :         // are we fast enough to cut in without any help?
     700      4180019 :         if (plannedSpeed - nv->getSpeed() >= HELP_OVERTAKE) {
     701       217676 :             const double neededGap = nv->getCarFollowModel().getSecureGap(nv, &myVehicle, nv->getSpeed(), plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel());
     702       217676 :             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       195352 :                 msg(neighFollow, plannedSpeed - HELP_OVERTAKE, dir | LCA_AMBLOCKINGFOLLOWER);
     710       195352 :                 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      3984667 :         const double neighNewSpeed = MAX2(0., nv->getSpeed() - ACCEL2SPEED(helpDecel));
     724      3984667 :         const double neighNewSpeed1s = MAX2(0., nv->getSpeed() - helpDecel);
     725      3984667 :         const double dv = plannedSpeed - neighNewSpeed1s;
     726              :         // new gap between follower and self in case the follower does brake for 1s
     727      3984667 :         const double decelGap = neighFollow.second + dv;
     728      3984667 :         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      3984667 :         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       589782 :             const double vsafe1 = MAX2(neighNewSpeed, nv->getCarFollowModel().followSpeed(
     749       589782 :                                            nv, nv->getSpeed(), neighFollow.second + SPEED2DIST(plannedSpeed), plannedSpeed, myVehicle.getCarFollowModel().getMaxDecel()));
     750       589782 :             const double vsafe = MAX2(neighNewSpeed, nv->getCarFollowModel().followSpeed(
     751       589782 :                                           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       589782 :             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      3984667 :         } 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       175806 :             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      3219079 :         } else if (dir == LCA_MRIGHT && !myAllowOvertakingRight && !nv->congested()) {
     773              :             const double vhelp = MAX2(neighNewSpeed, HELP_OVERTAKE);
     774         1246 :             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      3217833 :             double vhelp = MAX2(nv->getSpeed(), myVehicle.getSpeed() + HELP_OVERTAKE);
     782      3217833 :             if (nv->getSpeed() > myVehicle.getSpeed() &&
     783       429466 :                     ((dir == LCA_MRIGHT && myVehicle.getWaitingSeconds() > LCA_RIGHT_IMPATIENCE)
     784       398190 :                      || (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
     786       393336 :                      || (dir == LCA_MLEFT && myLeftSpace > MAX_ONRAMP_LENGTH)
     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       439417 :                 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       439417 :                 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       425470 :                     msg(neighFollow, vhelp, dir | LCA_AMBLOCKINGFOLLOWER);
     803       425470 :                     return;
     804              :                 }
     805              :             }
     806      2792363 :             msg(neighFollow, vhelp, dir | LCA_AMBLOCKINGFOLLOWER);
     807              :             // this follower is supposed to overtake us. slow down smoothly to allow this
     808      2792363 :             const double overtakeDist = (neighFollow.second // follower reaches ego back
     809      2792363 :                                          + myVehicle.getVehicleType().getLengthWithGap() // follower reaches ego front
     810      2792363 :                                          + nv->getVehicleType().getLength() // follower back at ego front
     811      2792363 :                                          + myVehicle.getCarFollowModel().getSecureGap( // follower has safe dist to ego
     812      2792363 :                                              &myVehicle, nv, plannedSpeed, vhelp, nv->getCarFollowModel().getMaxDecel()));
     813              :             // speed difference to create a sufficiently large gap
     814      2792363 :             const double needDV = overtakeDist / remainingSeconds;
     815              :             // make sure the deceleration is not to strong
     816      3046143 :             addLCSpeedAdvice(MAX2(vhelp - needDV, myVehicle.getSpeed() - ACCEL2SPEED(myVehicle.getCarFollowModel().getMaxDecel())));
     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      1503491 :     } else if (neighFollow.first != 0) {
     833      1503491 :         const double vsafe = MSLCHelper::getSpeedPreservingSecureGap(myVehicle, *neighFollow.first, neighFollow.second, plannedSpeed);
     834      1503491 :         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              : 
     843              : double
     844      2602436 : MSLCM_SL2015::informLeaders(int blocked, int dir,
     845              :                             const std::vector<CLeaderDist>& blockers,
     846              :                             double remainingSeconds) {
     847      2602436 :     double plannedSpeed = myVehicle.getSpeed();
     848      2602436 :     double space = myLeftSpace;
     849      2602436 :     if (myLeadingBlockerLength != 0) {
     850              :         // see patchSpeed @todo: refactor
     851       344631 :         space -= myLeadingBlockerLength - POSITION_EPS - myVehicle.getVehicleType().getMinGap();
     852       344631 :         if (space <= 0) {
     853              :             // ignore leading blocker
     854         1148 :             space = myLeftSpace;
     855              :         }
     856              :     }
     857      2602436 :     double safe = myVehicle.getCarFollowModel().stopSpeed(&myVehicle, myVehicle.getSpeed(), space);
     858              :     plannedSpeed = MIN2(plannedSpeed, safe);
     859              : 
     860     10237292 :     for (std::vector<CLeaderDist>::const_iterator it = blockers.begin(); it != blockers.end(); ++it) {
     861      7634856 :         plannedSpeed = MIN2(plannedSpeed, informLeader(blocked, dir, *it, remainingSeconds));
     862              :     }
     863      2602436 :     return plannedSpeed;
     864              : }
     865              : 
     866              : 
     867              : void
     868      2178678 : MSLCM_SL2015::informFollowers(int blocked, int dir,
     869              :                               const std::vector<CLeaderDist>& blockers,
     870              :                               double remainingSeconds,
     871              :                               double plannedSpeed) {
     872              :     // #3727
     873      7862482 :     for (std::vector<CLeaderDist>::const_iterator it = blockers.begin(); it != blockers.end(); ++it) {
     874      5683804 :         informFollower(blocked, dir, *it, remainingSeconds, plannedSpeed);
     875              :     }
     876      2178678 : }
     877              : 
     878              : 
     879              : void
     880     81886506 : MSLCM_SL2015::prepareStep() {
     881     81886506 :     MSAbstractLaneChangeModel::prepareStep();
     882              :     // keep information about strategic change direction
     883     81886506 :     myOwnState = (myOwnState & (LCA_STRATEGIC | LCA_COOPERATIVE)) ? (myOwnState & LCA_WANTS_LANECHANGE) : 0;
     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
     893     81886506 :     myLeadingBlockerLength = 0;
     894     81886506 :     myLeftSpace = 0;
     895              :     myLCAccelerationAdvices.clear();
     896     81886506 :     myDontBrake = false;
     897              :     myCFRelated.clear();
     898     81886506 :     myCFRelatedReady = false;
     899     81886506 :     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()
     901     81886506 :     mySafeLatDistRight = myVehicle.getLane()->getWidth() * 0.5 + myVehicle.getLateralPositionOnLane() - halfWidth;
     902     81886506 :     mySafeLatDistLeft = myVehicle.getLane()->getWidth() * 0.5 - myVehicle.getLateralPositionOnLane() - halfWidth;
     903     81886506 :     if (isOpposite()) {
     904              :         std::swap(mySafeLatDistLeft, mySafeLatDistRight);
     905              :     }
     906              :     // truncate to work around numerical instability between different builds
     907     81886506 :     mySpeedGainProbabilityRight = ceil(mySpeedGainProbabilityRight * 100000.0) * 0.00001;
     908     81886506 :     mySpeedGainProbabilityLeft = ceil(mySpeedGainProbabilityLeft * 100000.0) * 0.00001;
     909     81886506 :     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
     918    163773012 :     if (myExpectedSublaneSpeeds.size() != myVehicle.getLane()->getEdge().getSubLaneSides().size()) {
     919              :         // initialize
     920      1133643 :         const MSEdge* currEdge = &myVehicle.getLane()->getEdge();
     921              :         const std::vector<MSLane*>& lanes = currEdge->getLanes();
     922      3101480 :         for (std::vector<MSLane*>::const_iterator it_lane = lanes.begin(); it_lane != lanes.end(); ++it_lane) {
     923      1967837 :             const int subLanes = MAX2(1, int(ceil((*it_lane)->getWidth() / MSGlobals::gLateralResolution)));
     924     10471840 :             for (int i = 0; i < subLanes; ++i) {
     925      8504003 :                 newExpectedSpeeds.push_back((*it_lane)->getVehicleMaxSpeed(&myVehicle));
     926              :             }
     927              :         }
     928      1133643 :         if (currEdge->canChangeToOpposite()) {
     929        11123 :             MSLane* opposite = lanes.back()->getOpposite();
     930        11123 :             const int subLanes = MAX2(1, int(ceil(opposite->getWidth() / MSGlobals::gLateralResolution)));
     931        60966 :             for (int i = 0; i < subLanes; ++i) {
     932        49843 :                 newExpectedSpeeds.push_back(lanes.back()->getVehicleMaxSpeed(&myVehicle));
     933              :             }
     934              :         }
     935      1133643 :         if (myExpectedSublaneSpeeds.size() > 0) {
     936              :             // copy old values
     937              :             assert(myLastEdge != 0);
     938       629994 :             if (myLastEdge->getSubLaneSides().size() == myExpectedSublaneSpeeds.size()) {
     939       629994 :                 const int subLaneShift = computeSublaneShift(myLastEdge, currEdge);
     940       629994 :                 if (subLaneShift < std::numeric_limits<int>::max()) {
     941      2604642 :                     for (int i = 0; i < (int)myExpectedSublaneSpeeds.size(); ++i) {
     942      2253189 :                         const int newI = i + subLaneShift;
     943      2253189 :                         if (newI > 0 && newI < (int)newExpectedSpeeds.size()) {
     944      1545746 :                             newExpectedSpeeds[newI] = myExpectedSublaneSpeeds[i];
     945              :                         }
     946              :                     }
     947              :                 }
     948              :             }
     949              :         }
     950      1133643 :         myExpectedSublaneSpeeds = newExpectedSpeeds;
     951      1133643 :         myLastEdge = currEdge;
     952              :     }
     953              :     assert(myExpectedSublaneSpeeds.size() == myVehicle.getLane()->getEdge().getSubLaneSides().size());
     954     81886506 :     if (mySigma > 0) {
     955        53761 :         mySigmaState += getLateralDrift();
     956              :     }
     957     81886506 : }
     958              : 
     959              : double
     960        53761 : MSLCM_SL2015::getLateralDrift() {
     961              :     //OUProcess::step(double state, double dt, double timeScale, double noiseIntensity)
     962        53761 :     const double deltaState = OUProcess::step(mySigmaState,
     963        53761 :                               myVehicle.getActionStepLengthSecs(),
     964        53761 :                               MAX2(NUMERICAL_EPS, (1 - mySigma) * 100), mySigma) - mySigmaState;
     965        53761 :     const double scaledDelta = deltaState * myVehicle.getSpeed() / myVehicle.getLane()->getSpeedLimit();
     966        53761 :     return scaledDelta;
     967              : }
     968              : 
     969              : double
     970     94837064 : MSLCM_SL2015::getPosLat() {
     971     94837064 :     return myVehicle.getLateralPositionOnLane() + mySigmaState;
     972              : }
     973              : 
     974              : int
     975       629994 : MSLCM_SL2015::computeSublaneShift(const MSEdge* prevEdge, const MSEdge* curEdge) {
     976              :     // find the first lane that targets the new edge
     977              :     int prevShift = 0;
     978      1263036 :     for (const MSLane* const lane : prevEdge->getLanes()) {
     979      1839803 :         for (const MSLink* const link : lane->getLinkCont()) {
     980      1206761 :             if (&link->getLane()->getEdge() == curEdge) {
     981              :                 int curShift = 0;
     982              :                 const MSLane* target = link->getLane();
     983              :                 const std::vector<MSLane*>& lanes2 = curEdge->getLanes();
     984       490474 :                 for (std::vector<MSLane*>::const_iterator it_lane2 = lanes2.begin(); it_lane2 != lanes2.end(); ++it_lane2) {
     985       490474 :                     const MSLane* lane2 = *it_lane2;
     986       490474 :                     if (lane2 == target) {
     987       351453 :                         return prevShift + curShift;
     988              :                     }
     989       139021 :                     MSLeaderInfo ahead(lane2->getWidth());
     990       139021 :                     curShift += ahead.numSublanes();
     991       139021 :                 }
     992              :                 assert(false);
     993              :             }
     994              :         }
     995       633042 :         MSLeaderInfo ahead(lane->getWidth());
     996       633042 :         prevShift -= ahead.numSublanes();
     997       633042 :     }
     998              :     return std::numeric_limits<int>::max();
     999              : }
    1000              : 
    1001              : 
    1002              : void
    1003      1210986 : MSLCM_SL2015::changed() {
    1004      1210986 :     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       368834 :         if (getManeuverDist() < 0) {
    1008       166245 :             myKeepRightProbability = 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       368834 :         return;
    1016              :     }
    1017       842152 :     myOwnState = 0;
    1018              :     // XX do not reset values for unfinished maneuvers
    1019       842152 :     mySpeedGainProbabilityRight = 0;
    1020       842152 :     mySpeedGainProbabilityLeft = 0;
    1021       842152 :     myKeepRightProbability = 0;
    1022              : 
    1023       842152 :     if (myVehicle.getBestLaneOffset() == 0) {
    1024              :         // if we are not yet on our best lane there might still be unseen blockers
    1025              :         // (during patchSpeed)
    1026       821995 :         myLeadingBlockerLength = 0;
    1027       821995 :         myLeftSpace = 0;
    1028              :     }
    1029       842152 :     myLookAheadSpeed = LOOK_AHEAD_MIN_SPEED;
    1030              :     myLCAccelerationAdvices.clear();
    1031       842152 :     myDontBrake = false;
    1032       842152 :     myLeadingBlockerLength = 0;
    1033              : #if defined(DEBUG_MANEUVER) || defined(DEBUG_STATE)
    1034              :     if (DEBUG_COND) {
    1035              :         std::cout << SIMTIME << " veh=" << myVehicle.getID() << " changed()\n";
    1036              :     }
    1037              : #endif
    1038              : }
    1039              : 
    1040              : 
    1041              : void
    1042         3716 : MSLCM_SL2015::resetState() {
    1043         3716 :     myOwnState = 0;
    1044         3716 :     mySpeedGainProbabilityRight = 0;
    1045         3716 :     mySpeedGainProbabilityLeft = 0;
    1046         3716 :     myKeepRightProbability = 0;
    1047         3716 :     myLeadingBlockerLength = 0;
    1048         3716 :     myLeftSpace = 0;
    1049         3716 :     myLookAheadSpeed = LOOK_AHEAD_MIN_SPEED;
    1050              :     myLCAccelerationAdvices.clear();
    1051         3716 :     myDontBrake = false;
    1052         3716 : }
    1053              : 
    1054              : 
    1055              : int
    1056    117009275 : MSLCM_SL2015::_wantsChangeSublane(
    1057              :     int laneOffset,
    1058              :     LaneChangeAction alternatives,
    1059              :     const MSLeaderDistanceInfo& leaders,
    1060              :     const MSLeaderDistanceInfo& followers,
    1061              :     const MSLeaderDistanceInfo& blockers,
    1062              :     const MSLeaderDistanceInfo& neighLeaders,
    1063              :     const MSLeaderDistanceInfo& neighFollowers,
    1064              :     const MSLeaderDistanceInfo& neighBlockers,
    1065              :     const MSLane& neighLane,
    1066              :     const std::vector<MSVehicle::LaneQ>& preb,
    1067              :     MSVehicle** lastBlocked,
    1068              :     MSVehicle** firstBlocked,
    1069              :     double& latDist, double& maneuverDist, int& blocked) {
    1070              : 
    1071    117009275 :     if (laneOffset != 0) {
    1072              :         // update mySafeLatDist w.r.t. the direction being checkd
    1073     36147578 :         const double halfWidth = getWidth() * 0.5;
    1074     36147578 :         double center = getVehicleCenter();
    1075     36147578 :         if (laneOffset < 0) {
    1076     16690885 :             mySafeLatDistRight = center - halfWidth;
    1077              :         } else  {
    1078     19456693 :             mySafeLatDistLeft = getLeftBorder() - center - halfWidth;
    1079              :         }
    1080              :     }
    1081              : 
    1082    117009275 :     const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
    1083              :     // compute bestLaneOffset
    1084              :     MSVehicle::LaneQ curr, neigh, best;
    1085              :     int bestLaneOffset = 0;
    1086    117009275 :     double currentDist = 0;
    1087              :     double neighDist = 0;
    1088    117009275 :     const MSLane* prebLane = myVehicle.getLane();
    1089    117009275 :     if (prebLane->getEdge().isInternal()) {
    1090              :         // internal edges are not kept inside the bestLanes structure
    1091      2514327 :         if (isOpposite()) {
    1092          475 :             prebLane = prebLane->getNormalPredecessorLane();
    1093              :         } else {
    1094      2513852 :             prebLane = prebLane->getLinkCont()[0]->getLane();
    1095              :         }
    1096              :     }
    1097              :     // special case: vehicle considers changing to the opposite direction edge
    1098    117009275 :     const bool checkOpposite = &neighLane.getEdge() != &myVehicle.getLane()->getEdge();
    1099    117009275 :     const int prebOffset = (checkOpposite ? 0 : laneOffset);
    1100    161144352 :     for (int p = 0; p < (int) preb.size(); ++p) {
    1101    161144352 :         if (preb[p].lane == prebLane && p + laneOffset >= 0) {
    1102              :             assert(p + prebOffset < (int)preb.size());
    1103              :             curr = preb[p];
    1104    117009275 :             neigh = preb[p + prebOffset];
    1105    117009275 :             currentDist = curr.length;
    1106    117009275 :             neighDist = neigh.length;
    1107    117009275 :             bestLaneOffset = curr.bestLaneOffset;
    1108              :             // VARIANT_13 (equalBest)
    1109    117009275 :             if (bestLaneOffset == 0 && preb[p + prebOffset].bestLaneOffset == 0 && !checkOpposite) {
    1110              : #ifdef DEBUG_WANTSCHANGE
    1111              :                 if (gDebugFlag2) {
    1112              :                     std::cout << STEPS2TIME(currentTime)
    1113              :                               << " veh=" << myVehicle.getID()
    1114              :                               << " bestLaneOffsetOld=" << bestLaneOffset
    1115              :                               << " bestLaneOffsetNew=" << laneOffset
    1116              :                               << "\n";
    1117              :                 }
    1118              : #endif
    1119              :                 bestLaneOffset = prebOffset;
    1120              :             }
    1121    117009275 :             best = preb[p + bestLaneOffset];
    1122              :             break;
    1123              :         }
    1124              :     }
    1125              :     assert(curr.lane != nullptr);
    1126              :     assert(neigh.lane != nullptr);
    1127              :     assert(best.lane != nullptr);
    1128              :     double driveToNextStop = -std::numeric_limits<double>::max();
    1129              :     UNUSED_PARAMETER(driveToNextStop); // XXX use when computing usableDist
    1130    117009275 :     if (myVehicle.nextStopDist() < std::numeric_limits<double>::max()
    1131    117009275 :             && &myVehicle.getNextStop().lane->getEdge() == &myVehicle.getLane()->getEdge()) {
    1132              :         // vehicle can always drive up to stop distance
    1133              :         // @note this information is dynamic and thus not available in updateBestLanes()
    1134              :         // @note: nextStopDist was compute before the vehicle moved
    1135              :         driveToNextStop = myVehicle.nextStopDist();
    1136      1918762 :         const double stopPos = getForwardPos() + myVehicle.nextStopDist() - myVehicle.getLastStepDist();
    1137              : #ifdef DEBUG_WANTS_CHANGE
    1138              :         if (DEBUG_COND) {
    1139              :             std::cout << SIMTIME << std::setprecision(gPrecision) << " veh=" << myVehicle.getID()
    1140              :                       << " stopDist=" << myVehicle.nextStopDist()
    1141              :                       << " lastDist=" << myVehicle.getLastStepDist()
    1142              :                       << " stopPos=" << stopPos
    1143              :                       << " currentDist=" << currentDist
    1144              :                       << " neighDist=" << neighDist
    1145              :                       << "\n";
    1146              :         }
    1147              : #endif
    1148      2529384 :         currentDist = MAX2(currentDist, stopPos);
    1149              :         neighDist = MAX2(neighDist, stopPos);
    1150              :     }
    1151              :     // direction specific constants
    1152    117009275 :     const bool right = (laneOffset == -1);
    1153    117009275 :     const bool left = (laneOffset == 1);
    1154    117009275 :     const int myLca = (right ? LCA_MRIGHT : (left ? LCA_MLEFT : 0));
    1155    100318390 :     const int lcaCounter = (right ? LCA_LEFT : (left ? LCA_RIGHT : LCA_NONE));
    1156    117009275 :     const bool changeToBest = (right && bestLaneOffset < 0) || (left && bestLaneOffset > 0) || (laneOffset == 0 && bestLaneOffset == 0);
    1157              :     // keep information about being a leader/follower but remove information
    1158              :     // about previous lane change request or urgency
    1159    117009275 :     int ret = (myOwnState & 0xffff0000);
    1160              : 
    1161              :     // compute the distance when changing to the neighboring lane
    1162              :     // (ensure we do not lap into the line behind neighLane since there might be unseen blockers)
    1163              :     // minimum distance to move the vehicle fully onto the new lane
    1164    117009275 :     double latLaneDist = laneOffset == 0 ? 0. : myVehicle.lateralDistanceToLane(laneOffset);
    1165              : 
    1166              :     // VARIANT_5 (disableAMBACKBLOCKER1)
    1167              :     /*
    1168              :     if (leader.first != 0
    1169              :             && (myOwnState & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0
    1170              :             && (leader.first->getLaneChangeModel().getOwnState() & LCA_AMBLOCKINGFOLLOWER_DONTBRAKE) != 0) {
    1171              : 
    1172              :         myOwnState &= (0xffffffff - LCA_AMBLOCKINGFOLLOWER_DONTBRAKE);
    1173              :         if (myVehicle.getSpeed() > SUMO_const_haltingSpeed) {
    1174              :             myOwnState |= LCA_AMBACKBLOCKER;
    1175              :         } else {
    1176              :             ret |= LCA_AMBACKBLOCKER;
    1177              :             myDontBrake = true;
    1178              :         }
    1179              :     }
    1180              :     */
    1181              : 
    1182              : #ifdef DEBUG_WANTSCHANGE
    1183              :     if (gDebugFlag2) {
    1184              :         std::cout << STEPS2TIME(currentTime)
    1185              :                   << " veh=" << myVehicle.getID()
    1186              :                   << " myState=" << toString((LaneChangeAction)myOwnState)
    1187              :                   << " firstBlocked=" << Named::getIDSecure(*firstBlocked)
    1188              :                   << " lastBlocked=" << Named::getIDSecure(*lastBlocked)
    1189              :                   << "\n         leaders=" << leaders.toString()
    1190              :                   << "\n       followers=" << followers.toString()
    1191              :                   << "\n        blockers=" << blockers.toString()
    1192              :                   << "\n    neighLeaders=" << neighLeaders.toString()
    1193              :                   << "\n  neighFollowers=" << neighFollowers.toString()
    1194              :                   << "\n   neighBlockers=" << neighBlockers.toString()
    1195              :                   << "\n   changeToBest=" << changeToBest
    1196              :                   << " latLaneDist=" << latLaneDist
    1197              :                   << " alts=" << toString((LaneChangeAction)alternatives)
    1198              :                   << "\n   expectedSpeeds=" << toString(myExpectedSublaneSpeeds)
    1199              :                   << std::endl;
    1200              :     }
    1201              : #endif
    1202              : 
    1203    117009275 :     ret = slowDownForBlocked(lastBlocked, ret);
    1204              :     // VARIANT_14 (furtherBlock)
    1205    117009275 :     if (lastBlocked != firstBlocked) {
    1206    117009275 :         ret = slowDownForBlocked(firstBlocked, ret);
    1207              :     }
    1208              : 
    1209              : 
    1210              :     // we try to estimate the distance which is necessary to get on a lane
    1211              :     //  we have to get on in order to keep our route
    1212              :     // we assume we need something that depends on our velocity
    1213              :     // and compare this with the free space on our wished lane
    1214              :     //
    1215              :     // if the free space is somehow less than the space we need, we should
    1216              :     //  definitely try to get to the desired lane
    1217              :     //
    1218              :     // this rule forces our vehicle to change the lane if a lane changing is necessary soon
    1219              :     // lookAheadDistance:
    1220              :     // we do not want the lookahead distance to change all the time so we discrectize the speed a bit
    1221              : 
    1222              :     // VARIANT_18 (laHyst)
    1223    117009275 :     if (myVehicle.getSpeed() > myLookAheadSpeed) {
    1224     20299655 :         myLookAheadSpeed = myVehicle.getSpeed();
    1225              :     } else {
    1226              :         // FIXME: This strongly dependent on the value of TS, see LC2013 for the fix (l.1153, currently)
    1227     96709620 :         myLookAheadSpeed = MAX2(LOOK_AHEAD_MIN_SPEED,
    1228     96709620 :                                 (LOOK_AHEAD_SPEED_MEMORY * myLookAheadSpeed + (1 - LOOK_AHEAD_SPEED_MEMORY) * myVehicle.getSpeed()));
    1229              :     }
    1230              :     //myLookAheadSpeed = myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle);
    1231              : 
    1232              :     //double laDist = laSpeed > LOOK_FORWARD_SPEED_DIVIDER
    1233              :     //              ? laSpeed *  LOOK_FORWARD_FAR
    1234              :     //              : laSpeed *  LOOK_FORWARD_NEAR;
    1235    117009275 :     double laDist = myLookAheadSpeed * LOOK_FORWARD * myStrategicParam * (right ? 1 : myLookaheadLeft);
    1236    117009275 :     laDist += myVehicle.getVehicleType().getLengthWithGap() * 2.;
    1237              :     // aggressive drivers may elect to use reduced strategic lookahead to optimize speed
    1238              :     /*
    1239              :     if (mySpeedGainProbabilityRight > myChangeProbThresholdRight
    1240              :             || mySpeedGainProbabilityLeft > myChangeProbThresholdLeft) {
    1241              :         laDist *= MAX2(0.0, (1 - myPushy));
    1242              :         laDist *= MAX2(0,0, (1 - myAssertive));
    1243              :         laDist *= MAX2(0,0, (2 - mySpeedGainParam));
    1244              :     }
    1245              :     */
    1246              : 
    1247              :     // react to a stopped leader on the current lane
    1248    117009275 :     if (bestLaneOffset == 0 && leaders.hasStoppedVehicle()) {
    1249              :         // value is doubled for the check since we change back and forth
    1250              :         // laDist = 0.5 * (myVehicle.getVehicleType().getLengthWithGap() + leader.first->getVehicleType().getLengthWithGap());
    1251              :         // XXX determine length of longest stopped vehicle
    1252       229743 :         laDist = myVehicle.getVehicleType().getLengthWithGap();
    1253    116779532 :     } else if (checkOpposite && isOpposite() && neighLeaders.hasStoppedVehicle()) {
    1254              :         // compute exact distance to overtake stopped vehicle
    1255              :         laDist = 0;
    1256        45395 :         for (int i = 0; i < neighLeaders.numSublanes(); ++i) {
    1257        36316 :             CLeaderDist vehDist = neighLeaders[i];
    1258        36316 :             if (vehDist.first != nullptr && vehDist.first->isStopped()) {
    1259        29464 :                 laDist = MAX2(laDist, myVehicle.getVehicleType().getMinGap() + vehDist.second + vehDist.first->getVehicleType().getLengthWithGap());
    1260              :             }
    1261              :         }
    1262         9079 :         laDist += myVehicle.getVehicleType().getLength();
    1263              :     }
    1264    117009275 :     if (myStrategicParam < 0) {
    1265              :         laDist = -1e3; // never perform strategic change
    1266              :     }
    1267              : 
    1268              :     // free space that is available for changing
    1269              :     //const double neighSpeed = (neighLead.first != 0 ? neighLead.first->getSpeed() :
    1270              :     //        neighFollow.first != 0 ? neighFollow.first->getSpeed() :
    1271              :     //        best.lane->getSpeedLimit());
    1272              :     // @note: while this lets vehicles change earlier into the correct direction
    1273              :     // it also makes the vehicles more "selfish" and prevents changes which are necessary to help others
    1274              : 
    1275    117009275 :     const double roundaboutBonus = MSLCHelper::getRoundaboutDistBonus(myVehicle, myRoundaboutBonus, curr, neigh, best);
    1276    117009275 :     currentDist += roundaboutBonus;
    1277    117009275 :     neighDist += roundaboutBonus;
    1278              : 
    1279    117009275 :     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    117009275 :     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     11507455 :         const double center = myVehicle.getCenterOnEdge();
    1300     11507455 :         const double neighRight = getNeighRight(neighLane);
    1301     11507455 :         updateGaps(neighLeaders, neighRight, center, 1.0, mySafeLatDistRight, mySafeLatDistLeft);
    1302     11507455 :         updateGaps(neighFollowers, neighRight, center, 1.0, mySafeLatDistRight, mySafeLatDistLeft);
    1303              :         // remove TraCI flags because it should not be included in "state-without-traci"
    1304     11507455 :         ret = getCanceledState(laneOffset);
    1305     11507455 :         return ret;
    1306              :     }
    1307    105501820 :     if ((ret & LCA_URGENT) != 0) {
    1308              :         // prepare urgent lane change maneuver
    1309      2441227 :         if (changeToBest && abs(bestLaneOffset) > 1
    1310      2670662 :                 && 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        49324 :             const double reserve = MIN2(myLeftSpace - POSITION_EPS, getExtraReservation(bestLaneOffset, neighDist - currentDist));
    1314        50912 :             myLeadingBlockerLength = MAX2(reserve, myLeadingBlockerLength);
    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      2602436 :         MSVehicle* neighLeadLongest = const_cast<MSVehicle*>(getLongest(neighLeaders).first);
    1325      2602436 :         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      2602436 :         bool canReserve = MSLCHelper::updateBlockerLength(myVehicle, neighLeadLongest, lcaCounter, myLeftSpace - POSITION_EPS, canContinue, myLeadingBlockerLength);
    1332      2602436 :         if (*firstBlocked != neighLeadLongest && tieBrakeLeader(*firstBlocked)) {
    1333        81300 :             canReserve &= MSLCHelper::updateBlockerLength(myVehicle, *firstBlocked, lcaCounter, myLeftSpace - POSITION_EPS, canContinue, myLeadingBlockerLength);
    1334              :         }
    1335      2602436 :         if (!canReserve && !isOpposite()) {
    1336              :             // we have a low-priority relief connection
    1337              :             // std::cout << SIMTIME << " veh=" << myVehicle.getID() << " cannotReserve for blockers\n";
    1338         5727 :             myDontBrake = canContinue;
    1339              :         }
    1340              : 
    1341              :         std::vector<CLeaderDist> collectLeadBlockers;
    1342              :         std::vector<CLeaderDist> collectFollowBlockers;
    1343      2602436 :         int blockedFully = 0; // wether execution of the full maneuver is blocked
    1344      2602436 :         maneuverDist = latDist;
    1345      2602436 :         const double gapFactor = computeGapFactor(LCA_STRATEGIC);
    1346      2602436 :         blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
    1347              :                                 leaders, followers, blockers,
    1348              :                                 neighLeaders, neighFollowers, neighBlockers, &collectLeadBlockers, &collectFollowBlockers,
    1349              :                                 false, gapFactor, &blockedFully);
    1350              : 
    1351      2602436 :         const double absLaneOffset = fabs(bestLaneOffset != 0 ? bestLaneOffset : latDist / SUMO_const_laneWidth);
    1352      2602436 :         const double remainingSeconds = ((ret & LCA_TRACI) == 0 ?
    1353      3202184 :                                          MAX2(STEPS2TIME(TS), myLeftSpace / MAX2(myLookAheadSpeed, NUMERICAL_EPS) / absLaneOffset / URGENCY) :
    1354          184 :                                          myVehicle.getInfluencer().changeRequestRemainingSeconds(currentTime));
    1355      2602436 :         const double plannedSpeed = informLeaders(blocked, myLca, collectLeadBlockers, remainingSeconds);
    1356              :         // coordinate with direct obstructions
    1357      2602436 :         if (plannedSpeed >= 0) {
    1358              :             // maybe we need to deal with a blocking follower
    1359      2178678 :             informFollowers(blocked, myLca, collectFollowBlockers, remainingSeconds, plannedSpeed);
    1360              :         }
    1361      2178678 :         if (plannedSpeed > 0) {
    1362       963672 :             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      2602436 :         ret = getCanceledState(laneOffset);
    1380              :         return ret;
    1381      2602436 :     }
    1382              :     // VARIANT_15
    1383    102899384 :     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      5373637 :         if (left) {
    1397       537785 :             ret |= LCA_COOPERATIVE;
    1398       537785 :             if (!cancelRequest(ret | LCA_LEFT, laneOffset)) {
    1399       537785 :                 if ((ret & LCA_STAY) == 0) {
    1400       537785 :                     latDist = latLaneDist;
    1401       537785 :                     maneuverDist = latLaneDist;
    1402       537785 :                     blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
    1403              :                                             leaders, followers, blockers,
    1404              :                                             neighLeaders, neighFollowers, neighBlockers);
    1405              :                 }
    1406       537785 :                 return ret;
    1407              :             } else {
    1408              :                 ret &= ~LCA_COOPERATIVE;
    1409              :             }
    1410              :         } else {
    1411      4835852 :             myKeepRightProbability = 0;
    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
    1425    102361599 :                                   ? -mySpeedGainProbabilityRight / myChangeProbThresholdRight
    1426     91624435 :                                   : -mySpeedGainProbabilityLeft / myChangeProbThresholdLeft);
    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
    1444     21559377 :             && ((amBlockingFollowerPlusNB()
    1445              :                  // VARIANT_6 : counterNoHelp
    1446       258521 :                  && ((myOwnState & myLca) != 0))
    1447     21481154 :                 ||
    1448              :                 // continue previous cooperative change
    1449     21481154 :                 ((myPreviousState & LCA_COOPERATIVE) != 0
    1450        84694 :                  && !myCanChangeFully
    1451              :                  // change is in the right direction
    1452        46620 :                  && (laneOffset * getManeuverDist() > 0)))
    1453        80443 :             && (inconvenience < myCooperativeParam)
    1454    102439779 :             && (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        78180 :         ret |= LCA_COOPERATIVE | LCA_URGENT ;//| LCA_CHANGE_TO_HELP;
    1464        78180 :         if (!cancelRequest(ret | getLCA(ret, latLaneDist), laneOffset)) {
    1465        78180 :             latDist = amBlockingFollowerPlusNB() ? latLaneDist : getManeuverDist();
    1466        78180 :             maneuverDist = latDist;
    1467        78180 :             blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
    1468              :                                     leaders, followers, blockers,
    1469              :                                     neighLeaders, neighFollowers, neighBlockers);
    1470        78180 :             return ret;
    1471              :         } else {
    1472              :             ret &= ~(LCA_COOPERATIVE | LCA_URGENT);
    1473              :         }
    1474              :     }
    1475              : 
    1476              :     // --------
    1477              : 
    1478              : 
    1479              :     //// -------- security checks for krauss
    1480              :     ////  (vsafe fails when gap<0)
    1481              :     //if ((blocked & LCA_BLOCKED) != 0) {
    1482              :     //    return ret;
    1483              :     //}
    1484              :     //// --------
    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    102283419 :     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    102283419 :     const double vehWidth = getWidth();
    1496    102283419 :     const double rightVehSide = getVehicleCenter() - 0.5 * vehWidth;
    1497    102283419 :     const double leftVehSide = rightVehSide + vehWidth;
    1498              :     // figure out next speed when staying where we are
    1499              :     double defaultNextSpeed = std::numeric_limits<double>::max();
    1500              :     /// determine the leftmost and rightmost sublanes currently occupied
    1501    102283419 :     int leftmostOnEdge = (int)sublaneSides.size() - 1;
    1502    295699522 :     while (leftmostOnEdge > 0 && sublaneSides[leftmostOnEdge] > leftVehSide) {
    1503    193416103 :         leftmostOnEdge--;
    1504              :     }
    1505              :     int rightmostOnEdge = leftmostOnEdge;
    1506    380098364 :     while (rightmostOnEdge > 0 && sublaneSides[rightmostOnEdge] > rightVehSide + NUMERICAL_EPS) {
    1507    277814945 :         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    277814945 :         rightmostOnEdge--;
    1515              :     }
    1516    102283419 :     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    102283419 :     const int iMin = MIN2(myVehicle.getLane()->getRightmostSublane(), neighLane.getRightmostSublane());
    1529    102283419 :     double leftMax = MAX2(
    1530    102283419 :                          myVehicle.getLane()->getRightSideOnEdge() + myVehicle.getLane()->getWidth(),
    1531              :                          neighLane.getRightSideOnEdge() + neighLane.getWidth());
    1532    102283419 :     double rightMin = MIN2(myVehicle.getLane()->getRightSideOnEdge(), neighLane.getRightSideOnEdge());
    1533    102283419 :     if (checkOpposite || isOpposite()) {
    1534       137914 :         leftMax = getLeftBorder();
    1535              :     } else {
    1536              :         assert(leftMax <= edge.getWidth());
    1537              :     }
    1538    102283419 :     int sublaneCompact = MAX2(iMin, rightmostOnEdge - 1); // try to compactify to the right by default
    1539              : 
    1540    102283419 :     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    102283419 :     const double maxLatDist = leftMax - leftVehSide;
    1545    102283419 :     const double minLatDist = rightMin - rightVehSide;
    1546    102283419 :     const int iStart = laneOffset == 0 ? iMin : 0;
    1547     21481197 :     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    755919121 :     for (int i = iStart; i < (int)sublaneSides.size(); ++i) {
    1563    653635702 :         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    348542518 :             double vMin = myExpectedSublaneSpeeds[i];
    1567              :             //std::cout << "   i=" << i << "\n";
    1568              :             int j = i;
    1569   1357530586 :             while (vMin > 0 && j < (int)sublaneSides.size() && sublaneSides[j] < sublaneSides[i] + vehWidth) {
    1570   1008988068 :                 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   1008988068 :                 ++j;
    1577              :             }
    1578              :             // check whether the vehicle is between lanes
    1579    348542518 :             if (laneOffset != 0 && overlap(sublaneSides[i], sublaneSides[i] + vehWidth, laneBoundary, laneBoundary)) {
    1580     40523757 :                 vMin *= (1 - myLaneDiscipline);
    1581              :             }
    1582    494737065 :             double relativeGain = (vMin - defaultNextSpeed) / MAX2(vMin, RELGAIN_NORMALIZATION_MIN_SPEED);
    1583    348542518 :             double currentLatDist = sublaneSides[i] - rightVehSide;
    1584    348542518 :             if ((laneOffset == 0 && (currentLatDist > maxLatDist || currentLatDist < minLatDist))
    1585    348542518 :                     || (laneOffset < 0 && currentLatDist > maxLatDist)
    1586    341393323 :                     || (laneOffset > 0 && currentLatDist < minLatDist)) {
    1587              : #ifdef DEBUG_WANTSCHANGE
    1588              :                 if (gDebugFlag2) {
    1589              :                     std::cout << "      i=" << i << " currentLatDist=" << currentLatDist << " outOfBounds\n";
    1590              :                 }
    1591              : #endif
    1592     15940991 :                 continue;
    1593              :             }
    1594              :             currentLatDist = MIN2(MAX2(currentLatDist, minLatDist), maxLatDist);
    1595    332601527 :             if (currentLatDist > 0 && myVehicle.getLane()->getBidiLane() != nullptr) {
    1596              :                 // penalize overtaking on the left if the lane is used in both
    1597              :                 // directions
    1598       894286 :                 relativeGain *= 0.5;
    1599              :             }
    1600              :             // @note only consider change if it is compatible with the current direction (same sign or laneOffset == 0)
    1601    332601527 :             if (relativeGain > maxGain && currentLatDist * laneOffset >= 0) {
    1602              :                 maxGain = relativeGain;
    1603    126732271 :                 if (maxGain > GAIN_PERCEPTION_THRESHOLD) {
    1604              :                     sublaneCompact = i;
    1605      8028385 :                     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    205869256 :                 if (currentLatDist > 0
    1615              :                         //&& latDist < 0 // #7184 compensates for #7185
    1616    137579348 :                         && mySpeedGainProbabilityLeft > mySpeedGainProbabilityRight
    1617     35957954 :                         && relativeGain > GAIN_PERCEPTION_THRESHOLD
    1618      3481293 :                         && maxGain - relativeGain < NUMERICAL_EPS) {
    1619      2768141 :                     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    332601527 :             if (currentLatDist < -NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
    1628              :                 maxGainRight = MAX2(maxGainRight, relativeGain);
    1629    164831247 :             } else if (currentLatDist > NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
    1630              :                 maxGainLeft = MAX2(maxGainLeft, relativeGain);
    1631              :             }
    1632    332601527 :             const double subAlignDist = sublaneSides[i] - rightVehSide;
    1633    332601527 :             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    102283419 :     if (maxGainRight != -std::numeric_limits<double>::max()) {
    1650              : #ifdef DEBUG_WANTSCHANGE
    1651              :         if (gDebugFlag2) {
    1652              :             std::cout << "  speedGainR_old=" << mySpeedGainProbabilityRight;
    1653              :         }
    1654              : #endif
    1655     97560909 :         mySpeedGainProbabilityRight += myVehicle.getActionStepLengthSecs() * maxGainRight;
    1656              : #ifdef DEBUG_WANTSCHANGE
    1657              :         if (gDebugFlag2) {
    1658              :             std::cout << "  speedGainR_new=" << mySpeedGainProbabilityRight << "\n";
    1659              :         }
    1660              : #endif
    1661              :     }
    1662    102283419 :     if (maxGainLeft != -std::numeric_limits<double>::max()) {
    1663              : #ifdef DEBUG_WANTSCHANGE
    1664              :         if (gDebugFlag2) {
    1665              :             std::cout << "  speedGainL_old=" << mySpeedGainProbabilityLeft;
    1666              :         }
    1667              : #endif
    1668     94817385 :         mySpeedGainProbabilityLeft += myVehicle.getActionStepLengthSecs() * maxGainLeft;
    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    102283419 :     if ((fabs(maxGainRight) < NUMERICAL_EPS || maxGainRight == -std::numeric_limits<double>::max())
    1677     96604432 :             && (right || (alternatives & LCA_RIGHT) == 0)) {
    1678     79259929 :         mySpeedGainProbabilityRight *= pow(SPEEDGAIN_DECAY_FACTOR, myVehicle.getActionStepLengthSecs());
    1679              :     }
    1680    102283419 :     if ((fabs(maxGainLeft) < NUMERICAL_EPS || maxGainLeft == -std::numeric_limits<double>::max())
    1681     93358059 :             && (left || (alternatives & LCA_LEFT) == 0)) {
    1682     73617210 :         mySpeedGainProbabilityLeft *= pow(SPEEDGAIN_DECAY_FACTOR, myVehicle.getActionStepLengthSecs());
    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    102283419 :     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     91443120 :         if (right && myVehicle.getSpeed() > 0 && (maxGainRight >= 0
    1706       145804 :                 || ((myPreviousState & LCA_KEEPRIGHT) != 0 && maxGainRight >= -myKeepRightParam))) {
    1707              :             // honor the obligation to keep right (Rechtsfahrgebot)
    1708      8609506 :             const double vMax = myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle);
    1709      8609506 :             const double roadSpeedFactor = vMax / myVehicle.getLane()->getSpeedLimit(); // differse from speedFactor if vMax < speedLimit
    1710              :             double acceptanceTime;
    1711      8609506 :             if (myKeepRightAcceptanceTime == -1) {
    1712              :                 // legacy behavior: scale acceptance time with current speed and
    1713              :                 // use old hard-coded constant
    1714     17218478 :                 acceptanceTime = 7 * roadSpeedFactor * MAX2(1.0, myVehicle.getSpeed());
    1715              :             } else {
    1716          267 :                 acceptanceTime = myKeepRightAcceptanceTime * roadSpeedFactor;
    1717          267 :                 if (followers.hasVehicles()) {
    1718              :                     // reduce acceptanceTime if a follower vehicle is faster or wants to drive faster
    1719              :                     double minFactor = 1.0;
    1720          220 :                     for (int i = 0; i < followers.numSublanes(); ++i) {
    1721          176 :                         CLeaderDist follower = followers[i];
    1722          352 :                         if (follower.first != nullptr && follower.second < 2 * follower.first->getCarFollowModel().brakeGap(follower.first->getSpeed())) {
    1723          176 :                             if (follower.first->getSpeed() >= myVehicle.getSpeed()) {
    1724          352 :                                 double factor = MAX2(1.0, myVehicle.getSpeed()) / MAX2(1.0, follower.first->getSpeed());
    1725          176 :                                 const double fRSF = follower.first->getLane()->getVehicleMaxSpeed(follower.first) / follower.first->getLane()->getSpeedLimit();
    1726          176 :                                 if (fRSF > roadSpeedFactor) {
    1727          100 :                                     factor /= fRSF;
    1728              :                                 }
    1729          176 :                                 if (factor < minFactor) {
    1730              :                                     minFactor = factor;
    1731              :                                 }
    1732              :                             }
    1733              :                         }
    1734              :                     }
    1735           44 :                     acceptanceTime *= minFactor;
    1736              :                 }
    1737              :             }
    1738      8609506 :             double fullSpeedGap = MAX2(0., neighDist - myVehicle.getCarFollowModel().brakeGap(vMax));
    1739      8609506 :             double fullSpeedDrivingSeconds = MIN2(acceptanceTime, fullSpeedGap / vMax);
    1740      8609506 :             CLeaderDist neighLead = getSlowest(neighLeaders);
    1741      8609506 :             if (neighLead.first != 0 && neighLead.first->getSpeed() < vMax) {
    1742      7153748 :                 fullSpeedGap = MAX2(0., MIN2(fullSpeedGap,
    1743      7153748 :                                              neighLead.second - myVehicle.getCarFollowModel().getSecureGap(&myVehicle, neighLead.first,
    1744      7153748 :                                                      vMax, neighLead.first->getSpeed(), neighLead.first->getCarFollowModel().getMaxDecel())));
    1745      7153748 :                 fullSpeedDrivingSeconds = MIN2(fullSpeedDrivingSeconds, fullSpeedGap / (vMax - neighLead.first->getSpeed()));
    1746              :             }
    1747      8609506 :             const double deltaProb = (myChangeProbThresholdRight * (fullSpeedDrivingSeconds / acceptanceTime) / KEEP_RIGHT_TIME) * myVehicle.getActionStepLengthSecs();
    1748      8609506 :             const bool isSlide = preventSliding(latLaneDist);
    1749              :             // stay below threshold
    1750      9707946 :             if (!isSlide || !wantsKeepRight(myKeepRightProbability + deltaProb)) {
    1751      8571895 :                 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
    1774      8609506 :             if (wantsKeepRight(myKeepRightProbability)
    1775              :                     /*&& latLaneDist <= -NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()*/) {
    1776       499760 :                 ret |= LCA_KEEPRIGHT;
    1777              :                 assert(myVehicle.getLane()->getIndex() > neighLane.getIndex() || isOpposite());
    1778       499760 :                 if (!cancelRequest(ret | LCA_RIGHT, laneOffset)) {
    1779       498908 :                     latDist = latLaneDist;
    1780       498908 :                     maneuverDist = latLaneDist;
    1781       498908 :                     blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
    1782              :                                             leaders, followers, blockers,
    1783              :                                             neighLeaders, neighFollowers, neighBlockers);
    1784       498908 :                     return ret;
    1785              :                 } else {
    1786              :                     ret &= ~LCA_KEEPRIGHT;
    1787              :                 }
    1788              :             }
    1789              :         }
    1790              : 
    1791     90944212 :         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      2808773 :         if (latDist < 0 && mySpeedGainProbabilityRight >= myChangeProbThresholdRight * bidiRightFactor
    1808     93041348 :                 && neighDist / MAX2(.1, myVehicle.getSpeed()) > mySpeedGainRemainTime) {
    1809      1013881 :             ret |= LCA_SPEEDGAIN;
    1810      1013881 :             if (!cancelRequest(ret | getLCA(ret, latDist), laneOffset)) {
    1811      1001290 :                 int blockedFully = 0;
    1812      1001290 :                 maneuverDist = latDist;
    1813      1001290 :                 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        12591 :                 latDist = 0;
    1822              :                 ret &= ~LCA_SPEEDGAIN;
    1823              :             }
    1824              :         }
    1825              :     }
    1826    100783221 :     if (!right || isOpposite()) {
    1827              : 
    1828     91440784 :         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              : 
    1843     91440784 :         if (latDist > 0 && mySpeedGainProbabilityLeft > myChangeProbThresholdLeft &&
    1844              :                 // if we leave our lane, we should be able to stay in the new
    1845              :                 // lane for some time
    1846      3983844 :                 (stayInLane || neighDist / MAX2(.1, myVehicle.getSpeed()) > mySpeedGainRemainTime)) {
    1847      2757762 :             ret |= LCA_SPEEDGAIN;
    1848      2757762 :             if (!cancelRequest(ret + getLCA(ret, latDist), laneOffset)) {
    1849      2738726 :                 int blockedFully = 0;
    1850      2738726 :                 maneuverDist = latDist;
    1851      2738726 :                 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        19036 :                 latDist = 0;
    1859              :                 ret &= ~LCA_SPEEDGAIN;
    1860              :             }
    1861              :         }
    1862              :     }
    1863              : 
    1864              :     double latDistSublane = 0.;
    1865     98044495 :     const double halfLaneWidth = myVehicle.getLane()->getWidth() * 0.5;
    1866     98044495 :     const double halfVehWidth = getWidth() * 0.5;
    1867     98044495 :     if (myVehicle.getParameter().arrivalPosLatProcedure != ArrivalPosLatDefinition::DEFAULT
    1868         6099 :             && myVehicle.getRoute().getLastEdge() == &myVehicle.getLane()->getEdge()
    1869         4454 :             && bestLaneOffset == 0
    1870     98048949 :             && (myVehicle.getArrivalPos() - getForwardPos()) < ARRIVALPOS_LAT_THRESHOLD) {
    1871              :         // vehicle is on its final edge, on the correct lane and close to
    1872              :         // its arrival position. Change to the desired lateral position
    1873          868 :         switch (myVehicle.getParameter().arrivalPosLatProcedure) {
    1874           54 :             case ArrivalPosLatDefinition::GIVEN:
    1875           54 :                 latDistSublane = myVehicle.getParameter().arrivalPosLat - myVehicle.getLateralPositionOnLane();
    1876           54 :                 break;
    1877          814 :             case ArrivalPosLatDefinition::RIGHT:
    1878          814 :                 latDistSublane = -halfLaneWidth + halfVehWidth - myVehicle.getLateralPositionOnLane();
    1879          814 :                 break;
    1880            0 :             case ArrivalPosLatDefinition::CENTER:
    1881            0 :                 latDistSublane = -myVehicle.getLateralPositionOnLane();
    1882            0 :                 break;
    1883            0 :             case ArrivalPosLatDefinition::LEFT:
    1884            0 :                 latDistSublane = halfLaneWidth - halfVehWidth - myVehicle.getLateralPositionOnLane();
    1885            0 :                 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              : 
    1897     98043627 :         LatAlignmentDefinition align = getDesiredAlignment();
    1898     98043627 :         switch (align) {
    1899      4071856 :             case LatAlignmentDefinition::RIGHT:
    1900      4071856 :                 latDistSublane = -halfLaneWidth + halfVehWidth - getPosLat();
    1901      4071856 :                 break;
    1902       152369 :             case LatAlignmentDefinition::LEFT:
    1903       152369 :                 latDistSublane = halfLaneWidth - halfVehWidth - getPosLat();
    1904       152369 :                 break;
    1905     90584678 :             case LatAlignmentDefinition::CENTER:
    1906              :             case LatAlignmentDefinition::DEFAULT:
    1907     90584678 :                 latDistSublane = -getPosLat();
    1908     90584678 :                 break;
    1909              :             case LatAlignmentDefinition::NICE:
    1910              :                 latDistSublane = latDistNice;
    1911              :                 break;
    1912      2934399 :             case LatAlignmentDefinition::COMPACT:
    1913      2934399 :                 latDistSublane = sublaneSides[sublaneCompact] - rightVehSide;
    1914      2934399 :                 break;
    1915        27720 :             case LatAlignmentDefinition::ARBITRARY: {
    1916        27720 :                 latDistSublane = myVehicle.getLateralPositionOnLane() - getPosLat();
    1917        27720 :                 const double hLW = myVehicle.getLane()->getWidth() * 0.5;
    1918        27720 :                 const double posLat = myVehicle.getLateralPositionOnLane();
    1919        27720 :                 if (fabs(posLat) > hLW) {
    1920              :                     // vehicle is not within it's current lane
    1921           37 :                     if (posLat > 0) {
    1922           32 :                         latDistSublane -= (posLat - hLW);
    1923              :                     } else {
    1924            5 :                         latDistSublane += (-posLat - hLW);
    1925              :                     }
    1926              :                 } else {
    1927        27683 :                     const double edgeWidth = myVehicle.getCurrentEdge()->getWidth();
    1928        27683 :                     if (getWidth() < edgeWidth) {
    1929        27683 :                         if (rightVehSide < 0) {
    1930         2957 :                             latDistSublane -= rightVehSide;
    1931        24726 :                         } else if (leftVehSide > edgeWidth) {
    1932         1416 :                             latDistSublane -= leftVehSide - edgeWidth;
    1933              :                         }
    1934              :                     }
    1935              :                 }
    1936              :                 break;
    1937              :             }
    1938          441 :             case LatAlignmentDefinition::GIVEN: {
    1939              :                 // sublane alignment should not cause the vehicle to leave the lane
    1940          441 :                 const double hw = myVehicle.getLane()->getWidth() / 2 - NUMERICAL_EPS;
    1941          882 :                 const double offset = MAX2(-hw, MIN2(hw, myVehicle.getVehicleType().getPreferredLateralAlignmentOffset()));
    1942          441 :                 latDistSublane = -getPosLat() + offset;
    1943              :             }
    1944          441 :             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     98044495 :     if (fabs(latDist) <= NUMERICAL_EPS * myVehicle.getActionStepLengthSecs() ||
    1951      2925524 :             latDistSublane * latDist > 0) {
    1952              : 
    1953              : #if defined(DEBUG_WANTSCHANGE) || defined(DEBUG_STATE) || defined(DEBUG_MANEUVER)
    1954              :         if (gDebugFlag2) std::cout << SIMTIME
    1955              :                                        << " alignment=" << toString(myVehicle.getVehicleType().getPreferredLateralAlignment())
    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     10400719 :         if ((latDistSublane < 0 && mySpeedGainProbabilityRight < mySpeedLossProbThreshold)
    1970     95257323 :                 || (latDistSublane > 0 && mySpeedGainProbabilityLeft < mySpeedLossProbThreshold)
    1971    189279467 :                 || 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     96345167 :         if (!myCanChangeFully
    1981      7750715 :                 && (myPreviousState & (LCA_STRATEGIC | LCA_COOPERATIVE | LCA_KEEPRIGHT | LCA_SPEEDGAIN)) != 0
    1982    101980187 :                 && ((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     96345167 :         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     96345167 :         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      8220413 :             ret |= LCA_SUBLANE;
    2002              :             // include prior motivation when sublane-change is part of finishing an ongoing maneuver in the same direction
    2003      8220413 :             if (getPreviousManeuverDist() * latDist > 0) {
    2004       932637 :                 int priorReason = (myPreviousState & LCA_CHANGE_REASONS & ~LCA_SUBLANE);
    2005       932637 :                 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      8220413 :             if (!cancelRequest(ret + getLCA(ret, latDist), laneOffset)) {
    2012      8217293 :                 maneuverDist = latDist;
    2013      8217293 :                 blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset,
    2014              :                                         leaders, followers, blockers,
    2015              :                                         neighLeaders, neighFollowers, neighBlockers);
    2016      8217293 :                 return ret;
    2017              :             } else {
    2018         3120 :                 ret &= ~LCA_SUBLANE;
    2019              :             }
    2020              :         } else {
    2021     88124754 :             return ret | LCA_SUBLANE | LCA_STAY;
    2022              :         }
    2023              :     }
    2024      1702448 :     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      1702448 :     return ret;
    2055    102283419 : }
    2056              : 
    2057              : 
    2058              : int
    2059    234018550 : MSLCM_SL2015::slowDownForBlocked(MSVehicle** blocked, int state) {
    2060              :     //  if this vehicle is blocking someone in front, we maybe decelerate to let him in
    2061    234018550 :     if ((*blocked) != nullptr) {
    2062     16794672 :         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     16794672 :         if (gap > POSITION_EPS) {
    2073              :             //const bool blockedWantsUrgentRight = (((*blocked)->getLaneChangeModel().getOwnState() & LCA_RIGHT != 0)
    2074              :             //    && ((*blocked)->getLaneChangeModel().getOwnState() & LCA_URGENT != 0));
    2075              : 
    2076     13344084 :             if (myVehicle.getSpeed() < myVehicle.getCarFollowModel().getMaxDecel()
    2077              :                     //|| blockedWantsUrgentRight  // VARIANT_10 (helpblockedRight)
    2078              :                ) {
    2079     11412501 :                 if ((*blocked)->getSpeed() < SUMO_const_haltingSpeed) {
    2080      6942464 :                     state |= LCA_AMBACKBLOCKER_STANDING;
    2081              :                 } else {
    2082      4470037 :                     state |= LCA_AMBACKBLOCKER;
    2083              :                 }
    2084     34237503 :                 const double targetSpeed = getCarFollowModel().followSpeed(
    2085     11412501 :                                                &myVehicle, myVehicle.getSpeed(), (gap - POSITION_EPS),
    2086     11412501 :                                                (*blocked)->getSpeed(), (*blocked)->getCarFollowModel().getMaxDecel());
    2087              : #ifdef DEBUG_INFORM
    2088              :                 if (gDebugFlag2) {
    2089              :                     std::cout << "   slowing down for blocked " << Named::getIDSecure(*blocked) << " targetSpeed=" << targetSpeed << "\n";
    2090              :                 }
    2091              : #endif
    2092     11412501 :                 addLCSpeedAdvice(targetSpeed, false);
    2093              :                 //(*blocked) = 0; // VARIANT_14 (furtherBlock)
    2094              :             }
    2095              :         }
    2096              :     }
    2097    234018550 :     return state;
    2098              : }
    2099              : 
    2100              : 
    2101              : bool
    2102      2860954 : MSLCM_SL2015::isBidi(const MSLane* lane) const {
    2103      2860954 :     if (!MSNet::getInstance()->hasBidiEdges()) {
    2104              :         return false;
    2105              :     }
    2106      2860954 :     if (lane == myVehicle.getLane()->getBidiLane()) {
    2107              :         return true;
    2108              :     }
    2109      9710533 :     for (const MSLane* cand : myVehicle.getBestLanesContinuation()) {
    2110      7087694 :         if (cand != nullptr && cand->getBidiLane() == lane) {
    2111              :             return true;
    2112              :         }
    2113              :     }
    2114              :     return false;
    2115              : }
    2116              : 
    2117              : void
    2118    126025553 : MSLCM_SL2015::updateExpectedSublaneSpeeds(const MSLeaderDistanceInfo& ahead, int sublaneOffset, int laneIndex) {
    2119    126025553 :     const std::vector<MSLane*>& lanes = myVehicle.getLane()->getEdge().getLanes();
    2120    126025553 :     const std::vector<MSVehicle::LaneQ>& preb = myVehicle.getBestLanes();
    2121    126025553 :     const MSLane* lane = isOpposite() ? myVehicle.getLane()->getParallelOpposite() : lanes[laneIndex];
    2122    126025553 :     const MSLane* next = myVehicle.getBestLanesContinuation().size() > 1 ? myVehicle.getBestLanesContinuation()[1] : nullptr;
    2123    104247997 :     const MSLink* link = next != nullptr ? lane->getLinkTo(next) : nullptr;
    2124    126025553 :     const double shift = link != nullptr ? link->getLateralShift() + 0.5 * (lane->getWidth() - next->getWidth()) : 0;
    2125    126025553 :     const MSLane* bidi = myVehicle.getLane()->getBidiLane();
    2126    126025553 :     const double vMax = lane->getVehicleMaxSpeed(&myVehicle);
    2127              :     assert(preb.size() == lanes.size() || isOpposite());
    2128              : #ifdef DEBUG_EXPECTED_SLSPEED
    2129              :     if (DEBUG_COND) {
    2130              :         std::cout << SIMTIME << " veh=" << myVehicle.getID() << " updateExpectedSublaneSpeeds opposite=" << isOpposite()
    2131              :                   << " sublaneOffset=" << sublaneOffset << " laneIndex=" << laneIndex << " lane=" << lane->getID() << " ahead=" << ahead.toString() << "\n";
    2132              :     }
    2133              : #endif
    2134              : 
    2135    654252691 :     for (int sublane = 0; sublane < (int)ahead.numSublanes(); ++sublane) {
    2136    528227138 :         const int edgeSublane = sublane + sublaneOffset;
    2137    528227138 :         if (edgeSublane >= (int)myExpectedSublaneSpeeds.size()) {
    2138              :             // this may happen if a sibling lane is wider than the changer lane
    2139          170 :             continue;
    2140              :         }
    2141    528226968 :         if (link != nullptr && lane->getWidth() > next->getWidth() + NUMERICAL_EPS && MSGlobals::gLateralResolution > 0 && sublaneEnds(sublane, next, shift)) {
    2142              :             // sublane does not continue, discourage from use
    2143      2815299 :             myExpectedSublaneSpeeds[edgeSublane] = 0;
    2144              : #ifdef DEBUG_EXPECTED_SLSPEED
    2145              :             if (DEBUG_COND) {
    2146              :                 std::cout << "   updateExpectedSublaneSpeeds sublane=" << sublane << " doesNotContinue\n";
    2147              :             }
    2148              : #endif
    2149      2815299 :             continue;
    2150    525411669 :         } else if (lane->allowsVehicleClass(myVehicle.getVehicleType().getVehicleClass())) {
    2151              :             // lane allowed, find potential leaders and compute safe speeds
    2152              :             // XXX anticipate future braking if leader has a lower speed than myVehicle
    2153    519334032 :             const MSVehicle* leader = ahead[sublane].first;
    2154    519334032 :             const double gap = ahead[sublane].second;
    2155              :             double vSafe;
    2156    519334032 :             if (leader == nullptr) {
    2157     97618741 :                 if (hasBlueLight()) {
    2158              :                     // can continue from any lane if necessary
    2159              :                     vSafe = vMax;
    2160              :                 } else {
    2161     97388135 :                     const int prebIndex = isOpposite() ? (int)preb.size() - 1 : laneIndex;
    2162     97388135 :                     const double dist = preb[prebIndex].length - myVehicle.getPositionOnLane();
    2163     97388135 :                     vSafe = getCarFollowModel().followSpeed(&myVehicle, vMax, dist, 0, 0);
    2164              :                 }
    2165    421715291 :             } else if (bidi != nullptr && leader->getLane()->getBidiLane() != nullptr && isBidi(leader->getLane())) {
    2166              :                 // oncoming
    2167       238115 :                 if (gap < (1 + mySpeedGainLookahead * 2) * (vMax + leader->getSpeed())) {
    2168              :                     vSafe = 0;
    2169              :                 } else {
    2170              :                     vSafe = vMax;
    2171              :                 }
    2172              : #ifdef DEBUG_EXPECTED_SLSPEED
    2173              :                 if (DEBUG_COND) {
    2174              :                     std::cout << SIMTIME << " updateExpectedSublaneSpeeds sublane=" << sublane << " leader=" << leader->getID() << " bidi=" << bidi->getID() << " gap=" << gap << " vSafe=" << vSafe << "\n";
    2175              :                 }
    2176              : #endif
    2177              :             } else {
    2178    421477176 :                 if (leader->getAcceleration() > 0.5 * leader->getCarFollowModel().getMaxAccel()) {
    2179              :                     // assume that the leader will continue accelerating to its maximum speed
    2180     48124777 :                     vSafe = leader->getLane()->getVehicleMaxSpeed(leader);
    2181              :                 } else {
    2182    373352399 :                     vSafe = getCarFollowModel().followSpeed(
    2183    373352399 :                                 &myVehicle, vMax, gap, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel());
    2184              : #ifdef DEBUG_EXPECTED_SLSPEED
    2185              :                     if (DEBUG_COND) {
    2186              :                         std::cout << "   updateExpectedSublaneSpeeds edgeSublane=" << edgeSublane << " leader=" << leader->getID() << " gap=" << gap << " vSafe=" << vSafe << "\n";
    2187              :                     }
    2188              : #endif
    2189    373352399 :                     vSafe = forecastAverageSpeed(vSafe, vMax, gap, leader->getSpeed());
    2190              :                 }
    2191              :             }
    2192              :             // take pedestrians into account
    2193    519334032 :             if (lane->getEdge().getPersons().size() > 0 && lane->hasPedestrians()) {
    2194              :                 /// XXX this could be done faster by checking all sublanes at once (but would complicate the MSPModel API)
    2195              :                 double foeRight, foeLeft;
    2196       279827 :                 ahead.getSublaneBorders(sublane, 0, foeRight, foeLeft);
    2197              :                 // get all leaders ahead or overlapping
    2198       279827 :                 const PersonDist pedLeader = lane->nextBlocking(myVehicle.getPositionOnLane() - myVehicle.getVehicleType().getLength(), foeRight, foeLeft);
    2199       279827 :                 if (pedLeader.first != 0) {
    2200        77801 :                     const double pedGap = pedLeader.second - myVehicle.getVehicleType().getMinGap() - myVehicle.getVehicleType().getLength();
    2201              :                     // we do not know the walking direction here so we take the pedestrian speed as 0
    2202        77801 :                     vSafe = MIN2(getCarFollowModel().stopSpeed(&myVehicle, vMax, pedGap),
    2203              :                                  forecastAverageSpeed(vSafe, vMax, pedGap, 0));
    2204              : #ifdef DEBUG_EXPECTED_SLSPEED
    2205              :                     if (DEBUG_COND) {
    2206              :                         std::cout << "   updateExpectedSublaneSpeeds edgeSublane=" << edgeSublane << " pedLeader=" << pedLeader.first->getID() << " gap=" << pedGap << " vSafe=" << vSafe << "\n";
    2207              :                     }
    2208              : #endif
    2209              :                 }
    2210              :             }
    2211              :             // take bidi pedestrians into account
    2212    519334032 :             if (bidi != nullptr && bidi->getEdge().getPersons().size() > 0 && bidi->hasPedestrians()) {
    2213              :                 /// XXX this could be done faster by checking all sublanes at once (but would complicate the MSPModel API)
    2214              :                 double foeRight, foeLeft;
    2215         4277 :                 ahead.getSublaneBorders(sublane, 0, foeRight, foeLeft);
    2216         4277 :                 const double foeRightBidi = bidi->getWidth() - foeLeft;
    2217         4277 :                 const double foeLeftBidi = bidi->getWidth() - foeRight;
    2218              :                 // get all leaders ahead or overlapping
    2219         4277 :                 const double relativeBackPos = myVehicle.getLane()->getLength() - myVehicle.getPositionOnLane() + myVehicle.getLength();
    2220         4277 :                 const double stopTime = ceil(myVehicle.getSpeed() / myVehicle.getCarFollowModel().getMaxDecel());
    2221         4277 :                 PersonDist pedLeader = bidi->nextBlocking(relativeBackPos, foeRightBidi, foeLeftBidi, stopTime, true);
    2222         4277 :                 if (pedLeader.first != 0) {
    2223          719 :                     const double pedGap = pedLeader.second - myVehicle.getVehicleType().getMinGap() - myVehicle.getVehicleType().getLength();
    2224              :                     // we do not know the walking direction here so we take the pedestrian speed as 0
    2225          719 :                     vSafe = MIN2(getCarFollowModel().stopSpeed(&myVehicle, vMax, pedGap),
    2226              :                                  forecastAverageSpeed(vSafe, vMax, pedGap, 0));
    2227              : #ifdef DEBUG_EXPECTED_SLSPEED
    2228              :                     if (DEBUG_COND) {
    2229              :                         std::cout << "   updateExpectedSublaneSpeeds edgeSublane=" << edgeSublane << " pedLeader=" << pedLeader.first->getID() << " (bidi) gap=" << pedGap << " vSafe=" << vSafe << "\n";
    2230              :                     }
    2231              : #endif
    2232              :                 }
    2233              :             }
    2234              :             vSafe = MIN2(vMax, vSafe);
    2235              :             // forget old data when on the opposite side
    2236    519334032 :             const double memoryFactor = isOpposite() ? 0 : pow(SPEEDGAIN_MEMORY_FACTOR, myVehicle.getActionStepLengthSecs());
    2237    519334032 :             myExpectedSublaneSpeeds[edgeSublane] = memoryFactor * myExpectedSublaneSpeeds[edgeSublane] + (1 - memoryFactor) * vSafe;
    2238              :         } else {
    2239              :             // lane forbidden
    2240      6077637 :             myExpectedSublaneSpeeds[edgeSublane] = -1;
    2241              : #ifdef DEBUG_EXPECTED_SLSPEED
    2242              :             if (DEBUG_COND) {
    2243              :                 std::cout << "   updateExpectedSublaneSpeeds edgeSublane=" << edgeSublane << " lane " << lane->getID() << " forbidden\n";
    2244              :             }
    2245              : #endif
    2246              :         }
    2247              :     }
    2248              :     // XXX deal with leaders on subsequent lanes based on preb
    2249    126025553 : }
    2250              : 
    2251              : 
    2252              : bool
    2253      7041117 : MSLCM_SL2015::sublaneEnds(int i, const MSLane* next, double shift) {
    2254      7041117 :     const double side = i * MSGlobals::gLateralResolution + shift;
    2255              :     return ((side < -NUMERICAL_EPS
    2256           13 :              && (next->getParallelLane(-1) == nullptr || !next->getParallelLane(-1)->allowsVehicleClass(myVehicle.getVClass())))
    2257      7041130 :             || (side + MSGlobals::gLateralResolution > next->getWidth()
    2258      6245636 :                 && (next->getParallelLane(1) == nullptr || !next->getParallelLane(1)->allowsVehicleClass(myVehicle.getVClass()))));
    2259              : }
    2260              : 
    2261              : 
    2262              : double
    2263    373430919 : MSLCM_SL2015::forecastAverageSpeed(double vSafe, double vMax, double gap, double vLeader) const {
    2264    373430919 :     const double deltaV = vMax - vLeader;
    2265    373430919 :     if (deltaV > 0 && gap / deltaV < mySpeedGainLookahead && mySpeedGainLookahead > 0) {
    2266              :         // anticipate future braking by computing the average
    2267              :         // speed over the next few seconds
    2268    219679810 :         const double foreCastTime = mySpeedGainLookahead * 2;
    2269              :         const double gapClosingTime = MAX2(0.0, gap / deltaV);
    2270    219679810 :         const double vSafe2 = (gapClosingTime * vSafe + (foreCastTime - gapClosingTime) * vLeader) / foreCastTime;
    2271              : #ifdef DEBUG_EXPECTED_SLSPEED
    2272              :         if (DEBUG_COND && vSafe2 != vSafe) {
    2273              :             std::cout << "     foreCastTime=" << foreCastTime << " gapClosingTime=" << gapClosingTime << " extrapolated vSafe=" << vSafe2 << "\n";
    2274              :         }
    2275              : #endif
    2276              :         vSafe = vSafe2;
    2277              :     }
    2278    373430919 :     return vSafe;
    2279              : }
    2280              : 
    2281              : 
    2282              : double
    2283     92934300 : MSLCM_SL2015::computeSpeedGain(double latDistSublane, double defaultNextSpeed) const {
    2284              :     double result = std::numeric_limits<double>::max();
    2285     92934300 :     const std::vector<double>& sublaneSides = myVehicle.getLane()->getEdge().getSubLaneSides();
    2286     92934300 :     const double vehWidth = getWidth();
    2287     92934300 :     const double rightVehSide = myVehicle.getCenterOnEdge() - vehWidth * 0.5 + latDistSublane;
    2288     92934300 :     const double leftVehSide = rightVehSide + vehWidth;
    2289    743147376 :     for (int i = 0; i < (int)sublaneSides.size(); ++i) {
    2290    650213076 :         const double leftSide = i + 1 < (int)sublaneSides.size() ? sublaneSides[i + 1] : MAX2(myVehicle.getLane()->getEdge().getWidth(), sublaneSides[i] + POSITION_EPS);
    2291    650213076 :         if (overlap(rightVehSide, leftVehSide, sublaneSides[i], leftSide)) {
    2292    346617615 :             result = MIN2(result, myExpectedSublaneSpeeds[i]);
    2293              :         }
    2294              :         //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";
    2295              :     }
    2296     92934300 :     return result - defaultNextSpeed;
    2297     92934300 : }
    2298              : 
    2299              : 
    2300              : CLeaderDist
    2301      2602436 : MSLCM_SL2015::getLongest(const MSLeaderDistanceInfo& ldi) const {
    2302              :     int iMax = -1;
    2303              :     double maxLength = -1;
    2304     14269664 :     for (int i = 0; i < ldi.numSublanes(); ++i) {
    2305     11667228 :         const MSVehicle* veh = ldi[i].first;
    2306     11667228 :         if (veh) {
    2307      9041750 :             const double length = veh->getVehicleType().getLength();
    2308      9041750 :             if (length > maxLength && tieBrakeLeader(veh)) {
    2309              :                 maxLength = length;
    2310              :                 iMax = i;
    2311              :             }
    2312              :         }
    2313              :     }
    2314      2602436 :     return iMax >= 0 ? ldi[iMax] : std::make_pair(nullptr, -1);
    2315              : }
    2316              : 
    2317              : 
    2318              : bool
    2319      4341946 : MSLCM_SL2015::tieBrakeLeader(const MSVehicle* veh) const {
    2320              :     // tie braker if the leader is at the same lane position
    2321      4341946 :     return veh != nullptr && (veh->getPositionOnLane() != myVehicle.getPositionOnLane()
    2322        10390 :                               || veh->getSpeed() < myVehicle.getSpeed()
    2323        10282 :                               || &veh->getLane()->getEdge() != &myVehicle.getLane()->getEdge()
    2324        10282 :                               || veh->getLane()->getIndex() > myVehicle.getLane()->getIndex());
    2325              : }
    2326              : 
    2327              : 
    2328              : CLeaderDist
    2329     11470893 : MSLCM_SL2015::getSlowest(const MSLeaderDistanceInfo& ldi) {
    2330              :     int iMax = 0;
    2331              :     double minSpeed = std::numeric_limits<double>::max();
    2332     57893138 :     for (int i = 0; i < ldi.numSublanes(); ++i) {
    2333     46422245 :         if (ldi[i].first != 0) {
    2334     41290107 :             const double speed = ldi[i].first->getSpeed();
    2335     41290107 :             if (speed < minSpeed) {
    2336              :                 minSpeed = speed;
    2337              :                 iMax = i;
    2338              :             }
    2339              :         }
    2340              :     }
    2341     11470893 :     return ldi[iMax];
    2342              : }
    2343              : 
    2344              : 
    2345              : const MSVehicle*
    2346     69676190 : MSLCM_SL2015::getStopped(const MSLeaderDistanceInfo& ldi) {
    2347     69676190 :     if (ldi.hasVehicles()) {
    2348    300892104 :         for (const MSVehicle* const v : ldi.getVehicles()) {
    2349    243343862 :             if (v != nullptr && v->isStopped()) {
    2350              :                 return v;
    2351              :             }
    2352              :         }
    2353              :     }
    2354              :     return nullptr;
    2355              : }
    2356              : 
    2357              : 
    2358              : int
    2359     20025556 : MSLCM_SL2015::checkBlocking(const MSLane& neighLane, double& latDist, double maneuverDist, int laneOffset,
    2360              :                             const MSLeaderDistanceInfo& leaders,
    2361              :                             const MSLeaderDistanceInfo& followers,
    2362              :                             const MSLeaderDistanceInfo& /*blockers */,
    2363              :                             const MSLeaderDistanceInfo& neighLeaders,
    2364              :                             const MSLeaderDistanceInfo& neighFollowers,
    2365              :                             const MSLeaderDistanceInfo& /* neighBlockers */,
    2366              :                             std::vector<CLeaderDist>* collectLeadBlockers,
    2367              :                             std::vector<CLeaderDist>* collectFollowBlockers,
    2368              :                             bool keepLatGapManeuver,
    2369              :                             double gapFactor,
    2370              :                             int* retBlockedFully) {
    2371              :     // truncate latDist according to maxSpeedLat
    2372     20025556 :     const double maxDist = SPEED2DIST(getMaxSpeedLat2());
    2373     20025556 :     latDist = MAX2(MIN2(latDist, maxDist), -maxDist);
    2374     20025556 :     if (myVehicle.hasInfluencer() && myVehicle.getInfluencer().getLatDist() != 0 && myVehicle.getInfluencer().ignoreOverlap()) {
    2375              :         return 0;
    2376              :     }
    2377              : 
    2378     20023180 :     const double neighRight = getNeighRight(neighLane);
    2379     20023180 :     if (!myCFRelatedReady) {
    2380     11864617 :         updateCFRelated(followers, myVehicle.getLane()->getRightSideOnEdge(), false);
    2381     11864617 :         updateCFRelated(leaders, myVehicle.getLane()->getRightSideOnEdge(), true);
    2382     11864617 :         if (laneOffset != 0) {
    2383      8280254 :             updateCFRelated(neighFollowers, neighRight, false);
    2384      8280254 :             updateCFRelated(neighLeaders, neighRight, true);
    2385              :         }
    2386     11864617 :         myCFRelatedReady = true;
    2387              :     }
    2388              : 
    2389              :     // reduce latDist to avoid blockage with overlapping vehicles (no minGapLat constraints)
    2390     20023180 :     const double center = myVehicle.getCenterOnEdge();
    2391     20023180 :     updateGaps(leaders, myVehicle.getLane()->getRightSideOnEdge(), center, gapFactor, mySafeLatDistRight, mySafeLatDistLeft, false, 0, latDist, collectLeadBlockers);
    2392     20023180 :     updateGaps(followers, myVehicle.getLane()->getRightSideOnEdge(), center, gapFactor, mySafeLatDistRight, mySafeLatDistLeft, false, 0, latDist, collectFollowBlockers);
    2393     20023180 :     if (laneOffset != 0) {
    2394     10030517 :         updateGaps(neighLeaders, neighRight, center, gapFactor, mySafeLatDistRight, mySafeLatDistLeft, false, 0, latDist, collectLeadBlockers);
    2395     10030517 :         updateGaps(neighFollowers, neighRight, center, gapFactor, mySafeLatDistRight, mySafeLatDistLeft, false, 0, latDist, collectFollowBlockers);
    2396              :     }
    2397              : #ifdef DEBUG_BLOCKING
    2398              :     if (gDebugFlag2) {
    2399              :         std::cout << "    checkBlocking latDist=" << latDist << " mySafeLatDistRight=" << mySafeLatDistRight << " mySafeLatDistLeft=" << mySafeLatDistLeft << "\n";
    2400              :     }
    2401              : #endif
    2402              :     // if we can move at least a little bit in the desired direction, do so (rather than block)
    2403     20023180 :     const bool forcedTraCIChange = (myVehicle.hasInfluencer()
    2404        57959 :                                     && myVehicle.getInfluencer().getLatDist() != 0
    2405     20023472 :                                     && myVehicle.getInfluencer().ignoreOverlap());
    2406     20023180 :     if (latDist < 0) {
    2407     10226547 :         if (mySafeLatDistRight <= NUMERICAL_EPS) {
    2408              :             return LCA_BLOCKED_RIGHT | LCA_OVERLAPPING;
    2409      9219617 :         } else if (!forcedTraCIChange) {
    2410     12698838 :             latDist = MAX2(latDist, -mySafeLatDistRight);
    2411              :         }
    2412              :     } else {
    2413      9796633 :         if (mySafeLatDistLeft <= NUMERICAL_EPS) {
    2414              :             return LCA_BLOCKED_LEFT | LCA_OVERLAPPING;
    2415      7531417 :         } else if (!forcedTraCIChange) {
    2416      7531417 :             latDist = MIN2(latDist, mySafeLatDistLeft);
    2417              :         }
    2418              :     }
    2419              : 
    2420     16751034 :     myCanChangeFully = (maneuverDist == 0 || latDist == maneuverDist);
    2421              : #ifdef DEBUG_BLOCKING
    2422              :     if (gDebugFlag2) {
    2423              :         std::cout << "    checkBlocking latDist=" << latDist << " maneuverDist=" << maneuverDist << "\n";
    2424              :     }
    2425              : #endif
    2426              :     // destination sublanes must be safe
    2427              :     // intermediate sublanes must not be blocked by overlapping vehicles
    2428              : 
    2429              :     // XXX avoid checking the same leader multiple times
    2430              :     // XXX ensure that only changes within the same lane are undertaken if laneOffset = 0
    2431              : 
    2432              :     int blocked = 0;
    2433     16751034 :     blocked |= checkBlockingVehicles(&myVehicle, leaders, laneOffset, latDist, myVehicle.getLane()->getRightSideOnEdge(), true,
    2434              :                                      mySafeLatDistRight, mySafeLatDistLeft, collectLeadBlockers);
    2435     16751034 :     blocked |= checkBlockingVehicles(&myVehicle, followers, laneOffset, latDist, myVehicle.getLane()->getRightSideOnEdge(), false,
    2436              :                                      mySafeLatDistRight, mySafeLatDistLeft, collectFollowBlockers);
    2437     16751034 :     if (laneOffset != 0) {
    2438      8595466 :         blocked |= checkBlockingVehicles(&myVehicle, neighLeaders, laneOffset, latDist, neighRight, true,
    2439              :                                          mySafeLatDistRight, mySafeLatDistLeft, collectLeadBlockers);
    2440      8595466 :         blocked |= checkBlockingVehicles(&myVehicle, neighFollowers, laneOffset, latDist, neighRight, false,
    2441              :                                          mySafeLatDistRight, mySafeLatDistLeft, collectFollowBlockers);
    2442              :     }
    2443              : 
    2444              :     int blockedFully = 0;
    2445     16751034 :     blockedFully |= checkBlockingVehicles(&myVehicle, leaders, laneOffset, maneuverDist, myVehicle.getLane()->getRightSideOnEdge(), true,
    2446              :                                           mySafeLatDistRight, mySafeLatDistLeft, collectLeadBlockers);
    2447     16751034 :     blockedFully |= checkBlockingVehicles(&myVehicle, followers, laneOffset, maneuverDist, myVehicle.getLane()->getRightSideOnEdge(), false,
    2448              :                                           mySafeLatDistRight, mySafeLatDistLeft, collectFollowBlockers);
    2449     16751034 :     if (laneOffset != 0) {
    2450      8595466 :         blockedFully |= checkBlockingVehicles(&myVehicle, neighLeaders, laneOffset, maneuverDist, neighRight, true,
    2451              :                                               mySafeLatDistRight, mySafeLatDistLeft, collectLeadBlockers);
    2452      8595466 :         blockedFully |= checkBlockingVehicles(&myVehicle, neighFollowers, laneOffset, maneuverDist, neighRight, false,
    2453              :                                               mySafeLatDistRight, mySafeLatDistLeft, collectFollowBlockers);
    2454              :     }
    2455     16751034 :     if (retBlockedFully != nullptr) {
    2456      5640795 :         *retBlockedFully = blockedFully;
    2457              :     }
    2458              : #ifdef DEBUG_BLOCKING
    2459              :     if (gDebugFlag2) {
    2460              :         std::cout << "    blocked=" << blocked << " (" << toString((LaneChangeAction)blocked) << ") blockedFully=" << toString((LaneChangeAction)blockedFully)
    2461              :                   << " canChangeFully=" << myCanChangeFully << " keepLatGapManeuver=" << keepLatGapManeuver << "\n";
    2462              :     }
    2463              : #endif
    2464     16751034 :     if (blocked == 0 && !myCanChangeFully && myPushy == 0 && !keepLatGapManeuver) {
    2465              :         // aggressive drivers immediately start moving towards potential
    2466              :         // blockers and only check that the start of their maneuver (latDist) is safe. In
    2467              :         // contrast, cautious drivers need to check latDist and origLatDist to
    2468              :         // ensure that the maneuver can be finished without encroaching on other vehicles.
    2469      8511217 :         blocked |= blockedFully;
    2470              :     } else {
    2471              :         // XXX: in case of action step length > simulation step length, pushing may lead to collisions,
    2472              :         //      because maneuver is continued until maneuverDist is reached (perhaps set maneuverDist=latDist)
    2473              :     }
    2474              : #ifdef DEBUG_BLOCKING
    2475              :     if (gDebugFlag2) {
    2476              :         std::cout << "    blocked2=" << blocked << " (" << toString((LaneChangeAction)blocked) << ")\n";
    2477              :     }
    2478              : #endif
    2479     16751034 :     if (collectFollowBlockers != nullptr && collectLeadBlockers != nullptr) {
    2480              :         // prevent vehicles from being classified as leader and follower simultaneously
    2481      8871782 :         for (std::vector<CLeaderDist>::const_iterator it2 = collectLeadBlockers->begin(); it2 != collectLeadBlockers->end(); ++it2) {
    2482     32723901 :             for (std::vector<CLeaderDist>::iterator it = collectFollowBlockers->begin(); it != collectFollowBlockers->end();) {
    2483     25953697 :                 if ((*it2).first == (*it).first) {
    2484              : #ifdef DEBUG_BLOCKING
    2485              :                     if (gDebugFlag2) {
    2486              :                         std::cout << "    removed follower " << (*it).first->getID() << " because it is already a leader\n";
    2487              :                     }
    2488              : #endif
    2489              :                     it = collectFollowBlockers->erase(it);
    2490              :                 } else {
    2491              :                     ++it;
    2492              :                 }
    2493              :             }
    2494              :         }
    2495              :     }
    2496              :     return blocked;
    2497              : }
    2498              : 
    2499              : 
    2500              : int
    2501    101386000 : MSLCM_SL2015::checkBlockingVehicles(
    2502              :     const MSVehicle* ego, const MSLeaderDistanceInfo& vehicles,
    2503              :     int laneOffset, double latDist, double foeOffset, bool leaders,
    2504              :     double& safeLatGapRight, double& safeLatGapLeft,
    2505              :     std::vector<CLeaderDist>* collectBlockers) const {
    2506              :     // determine borders where safety/no-overlap conditions must hold
    2507              :     const LaneChangeAction blockType = (laneOffset == 0
    2508    101386000 :                                         ? (leaders ? LCA_BLOCKED_BY_LEADER : LCA_BLOCKED_BY_FOLLOWER)
    2509              :                                         : (laneOffset > 0
    2510     68763728 :                                            ? (leaders ? LCA_BLOCKED_BY_LEFT_LEADER : LCA_BLOCKED_BY_LEFT_FOLLOWER)
    2511     30332528 :                                            : (leaders ? LCA_BLOCKED_BY_RIGHT_LEADER : LCA_BLOCKED_BY_RIGHT_FOLLOWER)));
    2512    101386000 :     const double vehWidth = getWidth();
    2513    101386000 :     const double rightVehSide = ego->getRightSideOnEdge();
    2514    101386000 :     const double leftVehSide = rightVehSide + vehWidth;
    2515    101386000 :     const double rightVehSideDest = rightVehSide + latDist;
    2516    101386000 :     const double leftVehSideDest = leftVehSide + latDist;
    2517              :     const double rightNoOverlap = MIN2(rightVehSideDest, rightVehSide);
    2518              :     const double leftNoOverlap = MAX2(leftVehSideDest, leftVehSide);
    2519              : #ifdef DEBUG_BLOCKING
    2520              :     if (gDebugFlag2) {
    2521              :         std::cout << "  checkBlockingVehicles"
    2522              :                   << " laneOffset=" << laneOffset
    2523              :                   << " latDist=" << latDist
    2524              :                   << " foeOffset=" << foeOffset
    2525              :                   << " vehRight=" << rightVehSide
    2526              :                   << " vehLeft=" << leftVehSide
    2527              :                   << " rightNoOverlap=" << rightNoOverlap
    2528              :                   << " leftNoOverlap=" << leftNoOverlap
    2529              :                   << " destRight=" << rightVehSideDest
    2530              :                   << " destLeft=" << leftVehSideDest
    2531              :                   << " leaders=" << leaders
    2532              :                   << " blockType=" << toString((LaneChangeAction) blockType)
    2533              :                   << "\n";
    2534              :     }
    2535              : #endif
    2536              :     int result = 0;
    2537    519152373 :     for (int i = 0; i < vehicles.numSublanes(); ++i) {
    2538    429142364 :         CLeaderDist vehDist = vehicles[i];
    2539    429142364 :         if (vehDist.first != 0 && myCFRelated.count(vehDist.first) == 0) {
    2540              :             const MSVehicle* leader = vehDist.first;
    2541              :             const MSVehicle* follower = ego;
    2542    163275288 :             if (!leaders) {
    2543              :                 std::swap(leader, follower);
    2544              :             }
    2545              :             // only check the current stripe occupied by foe (transform into edge-coordinates)
    2546              :             double foeRight, foeLeft;
    2547    163275288 :             vehicles.getSublaneBorders(i, foeOffset, foeRight, foeLeft);
    2548    163275288 :             const bool overlapBefore = overlap(rightVehSide, leftVehSide, foeRight, foeLeft);
    2549    163275288 :             const bool overlapDest = overlap(rightVehSideDest, leftVehSideDest, foeRight, foeLeft);
    2550    163275288 :             const bool overlapAny = overlap(rightNoOverlap, leftNoOverlap, foeRight, foeLeft);
    2551              : #ifdef DEBUG_BLOCKING
    2552              :             if (gDebugFlag2) {
    2553              :                 std::cout << "   foe=" << vehDist.first->getID()
    2554              :                           << " gap=" << vehDist.second
    2555              :                           << " secGap=" << follower->getCarFollowModel().getSecureGap(follower, leader, follower->getSpeed(), leader->getSpeed(), leader->getCarFollowModel().getMaxDecel())
    2556              :                           << " foeRight=" << foeRight
    2557              :                           << " foeLeft=" << foeLeft
    2558              :                           << " overlapBefore=" << overlapBefore
    2559              :                           << " overlap=" << overlapAny
    2560              :                           << " overlapDest=" << overlapDest
    2561              :                           << "\n";
    2562              :             }
    2563              : #endif
    2564    163275288 :             if (overlapAny) {
    2565     36764010 :                 if (vehDist.second < 0) {
    2566     11813588 :                     if (overlapBefore && !overlapDest && !outsideEdge()) {
    2567              : #ifdef DEBUG_BLOCKING
    2568              :                         if (gDebugFlag2) {
    2569              :                             std::cout << "    ignoring current overlap to come clear\n";
    2570              :                         }
    2571              : #endif
    2572              :                     } else {
    2573              : #ifdef DEBUG_BLOCKING
    2574              :                         if (gDebugFlag2) {
    2575              :                             std::cout << "    overlap (" << toString((LaneChangeAction)blockType) << ")\n";
    2576              :                         }
    2577              : #endif
    2578     11740391 :                         result |= (blockType | LCA_OVERLAPPING);
    2579     11740391 :                         if (collectBlockers == nullptr) {
    2580     11375991 :                             return result;
    2581              :                         } else {
    2582      5109919 :                             collectBlockers->push_back(vehDist);
    2583              :                         }
    2584              :                     }
    2585     24950422 :                 } else if (overlapDest || !myCanChangeFully) {
    2586              :                     // Estimate state after actionstep (follower may be accelerating!)
    2587              :                     // A comparison between secure gap depending on the expected speeds and the extrapolated gap
    2588              :                     // determines whether the s is blocking the lane change.
    2589              :                     // (Note that the longitudinal state update has already taken effect before LC dynamics (thus "-TS" below), would be affected by #3665)
    2590              : 
    2591              :                     // Use conservative estimate for time until next action step
    2592              :                     // (XXX: how can the ego know the foe's action step length?)
    2593     24926338 :                     const double timeTillAction = MAX2(follower->getActionStepLengthSecs(), leader->getActionStepLengthSecs()) - TS;
    2594              :                     // Ignore decel for follower
    2595     24926338 :                     const double followerAccel = MAX2(0., follower->getAcceleration());
    2596     24926338 :                     const double leaderAccel = leader->getAcceleration();
    2597              :                     // Expected gap after next actionsteps
    2598     24926338 :                     const double expectedGap = MSCFModel::gapExtrapolation(timeTillAction, vehDist.second, leader->getSpeed(), follower->getSpeed(), leaderAccel, followerAccel, std::numeric_limits<double>::max(), std::numeric_limits<double>::max());
    2599              : 
    2600              :                     // Determine expected speeds and corresponding secure gap at the extrapolated timepoint
    2601     24926338 :                     const double followerExpectedSpeed = follower->getSpeed() + timeTillAction * followerAccel;
    2602     24926338 :                     const double leaderExpectedSpeed = MAX2(0., leader->getSpeed() + timeTillAction * leaderAccel);
    2603     24926338 :                     const double expectedSecureGap = follower->getCarFollowModel().getSecureGap(follower, leader, followerExpectedSpeed, leaderExpectedSpeed, leader->getCarFollowModel().getMaxDecel());
    2604              : 
    2605              : #if defined(DEBUG_ACTIONSTEPS) && defined(DEBUG_BLOCKING)
    2606              :                     if (gDebugFlag2) {
    2607              :                         std::cout << "    timeTillAction=" << timeTillAction
    2608              :                                   << " followerAccel=" << followerAccel
    2609              :                                   << " followerExpectedSpeed=" << followerExpectedSpeed
    2610              :                                   << " leaderAccel=" << leaderAccel
    2611              :                                   << " leaderExpectedSpeed=" << leaderExpectedSpeed
    2612              :                                   << "\n    gap=" << vehDist.second
    2613              :                                   << " gapChange=" << (expectedGap - vehDist.second)
    2614              :                                   << " expectedGap=" << expectedGap
    2615              :                                   << " expectedSecureGap=" << expectedSecureGap
    2616              :                                   << " safeLatGapLeft=" << safeLatGapLeft
    2617              :                                   << " safeLatGapRight=" << safeLatGapRight
    2618              :                                   << std::endl;
    2619              :                     }
    2620              : #endif
    2621              : 
    2622              :                     // @note for euler-update, a different value for secureGap2 may be obtained when applying safetyFactor to followerDecel rather than secureGap
    2623     24926338 :                     const double secureGap2 = expectedSecureGap * getSafetyFactor();
    2624     24926338 :                     if (expectedGap < secureGap2) {
    2625              :                         // Foe is a blocker. Update lateral safe gaps accordingly.
    2626      5531665 :                         if (foeRight > leftVehSide) {
    2627      4019134 :                             safeLatGapLeft = MIN2(safeLatGapLeft, foeRight - leftVehSide);
    2628      3144020 :                         } else if (foeLeft < rightVehSide) {
    2629      4821075 :                             safeLatGapRight = MIN2(safeLatGapRight, rightVehSide - foeLeft);
    2630              :                         }
    2631              : 
    2632              : #ifdef DEBUG_BLOCKING
    2633              :                         if (gDebugFlag2) {
    2634              :                             std::cout << "    blocked by " << vehDist.first->getID() << " gap=" << vehDist.second << " expectedGap=" << expectedGap
    2635              :                                       << " expectedSecureGap=" << expectedSecureGap << " secGap2=" << secureGap2 << " safetyFactor=" << getSafetyFactor()
    2636              :                                       << " safeLatGapLeft=" << safeLatGapLeft << " safeLatGapRight=" << safeLatGapRight
    2637              :                                       << "\n";
    2638              :                         }
    2639              : #endif
    2640      5531665 :                         result |= blockType;
    2641      5531665 :                         if (collectBlockers == nullptr) {
    2642      4745519 :                             return result;
    2643              :                         }
    2644              : #ifdef DEBUG_BLOCKING
    2645              :                     } else if (gDebugFlag2 && expectedGap < expectedSecureGap) {
    2646              :                         std::cout << "    ignore blocker " << vehDist.first->getID() << " gap=" << vehDist.second << " expectedGap=" << expectedGap
    2647              :                                   << " expectedSecureGap=" << expectedSecureGap << " secGap2=" << secureGap2 << " safetyFactor=" << getSafetyFactor() << "\n";
    2648              : #endif
    2649              :                     }
    2650     19394673 :                     if (collectBlockers != nullptr) {
    2651              :                         // collect non-blocking followers as well to make sure
    2652              :                         // they remain non-blocking
    2653      5162868 :                         collectBlockers->push_back(vehDist);
    2654              :                     }
    2655              :                 }
    2656              :             }
    2657              :         }
    2658              :     }
    2659              :     return result;
    2660              : 
    2661              : }
    2662              : 
    2663              : 
    2664              : void
    2665     40289742 : MSLCM_SL2015::updateCFRelated(const MSLeaderDistanceInfo& vehicles, double foeOffset, bool leaders) {
    2666              :     // to ensure that we do not ignore the wrong vehicles due to numerical
    2667              :     // instability we slightly reduce the width
    2668     40289742 :     const double vehWidth = myVehicle.getVehicleType().getWidth() - NUMERICAL_EPS;
    2669     40289742 :     const double rightVehSide = myVehicle.getCenterOnEdge() - 0.5 * vehWidth;
    2670     40289742 :     const double leftVehSide = rightVehSide + vehWidth;
    2671              : #ifdef DEBUG_BLOCKING
    2672              :     if (gDebugFlag2) {
    2673              :         std::cout << " updateCFRelated foeOffset=" << foeOffset << " vehicles=" << vehicles.toString() << "\n";
    2674              :     }
    2675              : #endif
    2676    221525128 :     for (int i = 0; i < vehicles.numSublanes(); ++i) {
    2677    181235386 :         CLeaderDist vehDist = vehicles[i];
    2678    222098829 :         if (vehDist.first != 0 && (myCFRelated.count(vehDist.first) == 0 || vehDist.second < 0)) {
    2679              :             double foeRight, foeLeft;
    2680    109875285 :             vehicles.getSublaneBorders(i, foeOffset, foeRight, foeLeft);
    2681              : #ifdef DEBUG_BLOCKING
    2682              :             if (gDebugFlag2) {
    2683              :                 std::cout << "    foe=" << vehDist.first->getID() << " gap=" << vehDist.second
    2684              :                           << " sublane=" << i
    2685              :                           << " foeOffset=" << foeOffset
    2686              :                           << " egoR=" << rightVehSide << " egoL=" << leftVehSide
    2687              :                           << " iR=" << foeRight << " iL=" << foeLeft
    2688              :                           << " egoV=" << myVehicle.getSpeed() << " foeV=" << vehDist.first->getSpeed()
    2689              :                           << " egoE=" << myVehicle.getLane()->getEdge().getID() << " foeE=" << vehDist.first->getLane()->getEdge().getID()
    2690              :                           << "\n";
    2691              :             }
    2692              : #endif
    2693    109875285 :             if (overlap(rightVehSide, leftVehSide, foeRight, foeLeft) && !outsideEdge() && (vehDist.second >= 0
    2694              :                     // avoid deadlock due to #3729
    2695      4980519 :                     || (!leaders
    2696      1969993 :                         && myVehicle.getPositionOnLane() >= myVehicle.getVehicleType().getLength()
    2697      1840767 :                         && myVehicle.getSpeed() < SUMO_const_haltingSpeed
    2698      1834432 :                         && vehDist.first->getSpeed() < SUMO_const_haltingSpeed
    2699      1824211 :                         && -vehDist.second < vehDist.first->getVehicleType().getMinGap()
    2700      1806994 :                         && &(myVehicle.getLane()->getEdge()) != &(vehDist.first->getLane()->getEdge()))
    2701              :                                                                                            )) {
    2702              : #ifdef DEBUG_BLOCKING
    2703              :                 if (gDebugFlag2) {
    2704              :                     std::cout << "       ignoring cfrelated foe=" << vehDist.first->getID()  << "\n";
    2705              :                 }
    2706              : #endif
    2707              :                 myCFRelated.insert(vehDist.first);
    2708              :             } else {
    2709              :                 const int erased = (int)myCFRelated.erase(vehDist.first);
    2710              : #ifdef DEBUG_BLOCKING
    2711              :                 if (gDebugFlag2 && erased > 0) {
    2712              :                     std::cout << "       restoring cfrelated foe=" << vehDist.first->getID()  << "\n";
    2713              :                 }
    2714              : #else
    2715              :                 UNUSED_PARAMETER(erased);
    2716              : #endif
    2717              :             }
    2718              :         }
    2719              :     }
    2720     40289742 : }
    2721              : 
    2722              : 
    2723              : bool
    2724   1419185107 : MSLCM_SL2015::overlap(double right, double left, double right2, double left2) {
    2725              :     assert(right <= left);
    2726              :     assert(right2 <= left2);
    2727   1419185107 :     return left2 >= right + NUMERICAL_EPS && left >= right2 + NUMERICAL_EPS;
    2728              : }
    2729              : 
    2730              : 
    2731              : int
    2732     69146174 : MSLCM_SL2015::lowest_bit(int changeReason) {
    2733     69146174 :     if ((changeReason & LCA_STRATEGIC) != 0) {
    2734              :         return LCA_STRATEGIC;
    2735              :     }
    2736     54413463 :     if ((changeReason & LCA_COOPERATIVE) != 0) {
    2737              :         return LCA_COOPERATIVE;
    2738              :     }
    2739     53711618 :     if ((changeReason & LCA_SPEEDGAIN) != 0) {
    2740              :         return LCA_SPEEDGAIN;
    2741              :     }
    2742     49951427 :     if ((changeReason & LCA_KEEPRIGHT) != 0) {
    2743              :         return LCA_KEEPRIGHT;
    2744              :     }
    2745     49277815 :     if ((changeReason & LCA_TRACI) != 0) {
    2746         4760 :         return LCA_TRACI;
    2747              :     }
    2748              :     return changeReason;
    2749              : }
    2750              : 
    2751              : 
    2752              : MSLCM_SL2015::StateAndDist
    2753    161723394 : MSLCM_SL2015::decideDirection(StateAndDist sd1, StateAndDist sd2) const {
    2754              :     // ignore dummy decisions (returned if mayChange() failes)
    2755    161723394 :     if (sd1.state == 0) {
    2756     65271670 :         return sd2;
    2757     96451724 :     } else if (sd2.state == 0) {
    2758     61878637 :         return sd1;
    2759              :     }
    2760              :     // LCA_SUBLANE is special because LCA_STAY|LCA_SUBLANE may override another LCA_SUBLANE command
    2761     34573087 :     const bool want1 = ((sd1.state & LCA_WANTS_LANECHANGE) != 0) || ((sd1.state & LCA_SUBLANE) != 0 && (sd1.state & LCA_STAY) != 0);
    2762     34573087 :     const bool want2 = ((sd2.state & LCA_WANTS_LANECHANGE) != 0) || ((sd2.state & LCA_SUBLANE) != 0 && (sd2.state & LCA_STAY) != 0);
    2763     34573087 :     const bool can1 = ((sd1.state & LCA_BLOCKED) == 0);
    2764     34573087 :     const bool can2 = ((sd2.state & LCA_BLOCKED) == 0);
    2765     34573087 :     int reason1 = lowest_bit(sd1.state & LCA_CHANGE_REASONS);
    2766     34573087 :     int reason2 = lowest_bit(sd2.state & LCA_CHANGE_REASONS);
    2767              : #ifdef DEBUG_DECISION
    2768              :     if (DEBUG_COND) std::cout << SIMTIME
    2769              :                                   << " veh=" << myVehicle.getID()
    2770              :                                   << " state1=" << toString((LaneChangeAction)sd1.state)
    2771              :                                   << " want1=" << (sd1.state & LCA_WANTS_LANECHANGE)
    2772              :                                   << " dist1=" << sd1.latDist
    2773              :                                   << " dir1=" << sd1.dir
    2774              :                                   << " state2=" << toString((LaneChangeAction)sd2.state)
    2775              :                                   << " want2=" << (sd2.state & LCA_WANTS_LANECHANGE)
    2776              :                                   << " dist2=" << sd2.latDist
    2777              :                                   << " dir2=" << sd2.dir
    2778              :                                   << " reason1=" << toString((LaneChangeAction)reason1)
    2779              :                                   << " reason2=" << toString((LaneChangeAction)reason2)
    2780              :                                   << "\n";
    2781              : #endif
    2782     34573087 :     if (want1) {
    2783     33735480 :         if (want2) {
    2784     23263957 :             if ((sd1.state & LCA_TRACI) != 0 && (sd2.state & LCA_TRACI) != 0) {
    2785              :                 // influencer may assign LCA_WANTS_LANECHANGE despite latDist = 0
    2786         1316 :                 if (sd1.latDist == 0 && sd2.latDist != 0) {
    2787           96 :                     return sd2;
    2788         1220 :                 } else if (sd2.latDist == 0 && sd1.latDist != 0) {
    2789           16 :                     return sd1;
    2790              :                 }
    2791              :             }
    2792              :             // decide whether right or left has higher priority (lower value in enum LaneChangeAction)
    2793     23263845 :             if (reason1 < reason2) {
    2794              :                 //if (DEBUG_COND) std::cout << "   " << (sd1.state & LCA_CHANGE_REASONS) << " < " << (sd2.state & LCA_CHANGE_REASONS) << "\n";
    2795       495346 :                 return (!can1 && can2 && sd1.sameDirection(sd2)) ? sd2 : sd1;
    2796              :                 //return sd1;
    2797     23013668 :             } else if (reason1 > reason2) {
    2798              :                 //if (DEBUG_COND) std::cout << "   " << (sd1.state & LCA_CHANGE_REASONS) << " > " << (sd2.state & LCA_CHANGE_REASONS) << "\n";
    2799     11892979 :                 return (!can2 && can1 && sd1.sameDirection(sd2)) ? sd1 : sd2;
    2800              :                 //return sd2;
    2801              :             } else {
    2802              :                 // same priority.
    2803     17045414 :                 if ((sd1.state & LCA_SUBLANE) != 0) {
    2804              :                     // special treatment: prefer action with dir != 0
    2805     16758649 :                     if (sd1.dir == 0) {
    2806     15966602 :                         return sd2;
    2807       792047 :                     } else if (sd2.dir == 0) {
    2808            0 :                         return sd1;
    2809              :                     } else {
    2810              :                         // prefer action that knows more about the desired direction
    2811              :                         // @note when deciding between right and left, right is always given as sd1
    2812              :                         assert(sd1.dir == -1);
    2813              :                         assert(sd2.dir == 1);
    2814       792047 :                         if (sd1.latDist <= 0) {
    2815       744152 :                             return sd1;
    2816        47895 :                         } else if (sd2.latDist >= 0) {
    2817        47147 :                             return sd2;
    2818              :                         }
    2819              :                         // when in doubt, prefer moving to the right
    2820          748 :                         return sd1.latDist <= sd2.latDist ? sd1 : sd2;
    2821              :                     }
    2822              :                 } else {
    2823       286765 :                     if (can1) {
    2824       162924 :                         if (can2) {
    2825              :                             // break strategic ties with tactial concerns
    2826       108102 :                             if (reason1 == LCA_STRATEGIC) {
    2827         2727 :                                 if (sd1.latDist <= sd2.latDist) {
    2828         4510 :                                     return mySpeedGainProbabilityRight > mySpeedGainProbabilityLeft ? sd1 : sd2;
    2829              :                                 } else {
    2830            3 :                                     return mySpeedGainProbabilityRight > mySpeedGainProbabilityLeft ? sd2 : sd1;
    2831              :                                 }
    2832              :                             } else {
    2833              :                                 // finish the shorter maneuver (i.e. continue the current maneuver)
    2834       185238 :                                 return fabs(sd1.maneuverDist) < fabs(sd2.maneuverDist) ? sd1 : sd2;
    2835              :                             }
    2836              :                         } else {
    2837        54822 :                             return sd1;
    2838              :                         }
    2839              :                     } else {
    2840       123841 :                         return sd2;
    2841              :                     }
    2842              :                 }
    2843              :             }
    2844              :         } else {
    2845     10471523 :             return sd1;
    2846              :         }
    2847              :     } else {
    2848       837607 :         return sd2;
    2849              :     }
    2850              : 
    2851              : }
    2852              : 
    2853              : 
    2854              : LaneChangeAction
    2855    129079511 : MSLCM_SL2015::getLCA(int state, double latDist) {
    2856     27050716 :     return ((latDist == 0 || (state & LCA_CHANGE_REASONS) == 0)
    2857    156129825 :             ? LCA_NONE : (latDist < 0 ? LCA_RIGHT : LCA_LEFT));
    2858              : }
    2859              : 
    2860              : 
    2861              : int
    2862    117009275 : MSLCM_SL2015::checkStrategicChange(int ret,
    2863              :                                    const MSLane& neighLane,
    2864              :                                    int laneOffset,
    2865              :                                    const MSLeaderDistanceInfo& leaders,
    2866              :                                    const MSLeaderDistanceInfo& neighLeaders,
    2867              :                                    const MSVehicle::LaneQ& curr,
    2868              :                                    const MSVehicle::LaneQ& neigh,
    2869              :                                    const MSVehicle::LaneQ& best,
    2870              :                                    int bestLaneOffset,
    2871              :                                    bool changeToBest,
    2872              :                                    double& currentDist,
    2873              :                                    double neighDist,
    2874              :                                    double laDist,
    2875              :                                    double roundaboutBonus,
    2876              :                                    double latLaneDist,
    2877              :                                    bool checkOpposite,
    2878              :                                    double& latDist
    2879              :                                   ) {
    2880    117009275 :     const bool right = (laneOffset == -1);
    2881              :     const bool left = (laneOffset == 1);
    2882              : 
    2883    117009275 :     const double forwardPos = getForwardPos();
    2884    117009275 :     if (laneOffset != 0) {
    2885     36147578 :         myLeftSpace = currentDist - forwardPos;
    2886              :     }
    2887    117009275 :     const double usableDist = (currentDist - forwardPos - best.occupation *  JAM_FACTOR);
    2888              :     //- (best.lane->getVehicleNumber() * neighSpeed)); // VARIANT 9 jfSpeed
    2889    117009275 :     const double maxJam = MAX2(neigh.occupation, curr.occupation);
    2890    117009275 :     const double neighLeftPlace = MAX2(0., neighDist - forwardPos - maxJam);
    2891    117009275 :     const double overlap = myVehicle.getLateralOverlap();
    2892              :     // save the left space
    2893              : 
    2894              : #ifdef DEBUG_STRATEGIC_CHANGE
    2895              :     if (gDebugFlag2) {
    2896              :         std::cout << SIMTIME
    2897              :                   << " veh=" << myVehicle.getID()
    2898              :                   << " forwardPos=" << forwardPos
    2899              :                   << " laSpeed=" << myLookAheadSpeed
    2900              :                   << " laDist=" << laDist
    2901              :                   << " currentDist=" << currentDist
    2902              :                   << " usableDist=" << usableDist
    2903              :                   << " bestLaneOffset=" << bestLaneOffset
    2904              :                   << " best.length=" << best.length
    2905              :                   << " maxJam=" << maxJam
    2906              :                   << " neighLeftPlace=" << neighLeftPlace
    2907              :                   << " myLeftSpace=" << myLeftSpace
    2908              :                   << " overlap=" << overlap
    2909              :                   << "\n";
    2910              :     }
    2911              : #endif
    2912              : 
    2913    117009275 :     if (laneOffset == 0) {
    2914     80861697 :         if (overlap > MAX2(POSITION_EPS, MSGlobals::gLateralResolution)
    2915       429182 :                 && (getShadowLane() == nullptr || !getShadowLane()->allowsVehicleClass(myVehicle.getVClass()))
    2916     80865082 :                 && getWidth() < myVehicle.getLane()->getWidth()) {
    2917              :             // @brief we urgently need to return to within lane bounds
    2918         3298 :             latDist = myVehicle.getLateralPositionOnLane() > 0 ? -overlap : overlap;
    2919         3298 :             ret |= LCA_STRATEGIC | LCA_URGENT;
    2920              : #ifdef DEBUG_STRATEGIC_CHANGE
    2921              :             if (gDebugFlag2) {
    2922              :                 std::cout << SIMTIME << " returnToLaneBounds\n";
    2923              :             }
    2924              : #endif
    2925              :             //std::cout << SIMTIME << " veh=" << myVehicle.getID() << " overlap=" << overlap << " returnToLaneBounds\n";
    2926     80858399 :         } else if (myVehicle.getBestLanesContinuation().size() > 1 && myVehicle.getLane()->getWidth() > myVehicle.getBestLanesContinuation()[1]->getWidth()) {
    2927       831226 :             const MSLane* cur = myVehicle.getLane();
    2928       831226 :             const MSLane* next = myVehicle.getBestLanesContinuation()[1];
    2929       831226 :             const MSLink* link = cur->getLinkTo(next);
    2930       831226 :             const double distOnLane = cur->getLength() - myVehicle.getPositionOnLane();
    2931       831226 :             if (link != nullptr && getWidth() < next->getWidth() && distOnLane < 100) {
    2932       826750 :                 double hwDiff = 0.5 * (cur->getWidth() - next->getWidth());
    2933       826750 :                 double rightVehSide = myVehicle.getRightSideOnLane() + link->getLateralShift() - hwDiff;
    2934       826750 :                 double leftVehSide = myVehicle.getLeftSideOnLane() + link->getLateralShift() - hwDiff;
    2935       826750 :                 const double res = MSGlobals::gLateralResolution > 0 ? MSGlobals::gLateralResolution : next->getWidth();
    2936       826750 :                 if (rightVehSide < -res && (next->getParallelLane(-1) == nullptr || !next->getParallelLane(-1)->allowsVehicleClass(myVehicle.getVClass()))) {
    2937        24136 :                     latDist = -rightVehSide;
    2938        24136 :                     myLeftSpace = distOnLane;
    2939        24136 :                     ret |= LCA_STRATEGIC | LCA_URGENT;
    2940              : #ifdef DEBUG_STRATEGIC_CHANGE
    2941              :                     if (gDebugFlag2) {
    2942              :                         std::cout << SIMTIME << " rightSublaneEnds rVSide=" << myVehicle.getRightSideOnLane()
    2943              :                                   << " shift=" << link->getLateralShift() << " rVSide2=" << rightVehSide << " myLeftSpace=" << myLeftSpace << " \n";
    2944              :                     }
    2945              : #endif
    2946       802614 :                 } else if (leftVehSide > next->getWidth() + res && (next->getParallelLane(1) == nullptr || !next->getParallelLane(1)->allowsVehicleClass(myVehicle.getVClass()))) {
    2947        30413 :                     latDist = -(leftVehSide - next->getWidth());
    2948        30413 :                     myLeftSpace = distOnLane;
    2949        30413 :                     ret |= LCA_STRATEGIC | LCA_URGENT;
    2950              : #ifdef DEBUG_STRATEGIC_CHANGE
    2951              :                     if (gDebugFlag2) {
    2952              :                         std::cout << SIMTIME << " leftSublaneEnds lVSide=" << myVehicle.getLeftSideOnLane()
    2953              :                                   << " shift=" << link->getLateralShift() << " lVSide2=" << leftVehSide << " myLeftSpace=" << myLeftSpace << "\n";
    2954              :                     }
    2955              : #endif
    2956              :                 }
    2957              :             }
    2958              :         }
    2959     22540544 :     } else if (laneOffset != 0 && changeToBest && bestLaneOffset == curr.bestLaneOffset
    2960     41747764 :                && currentDistDisallows(usableDist, bestLaneOffset, laDist)) {
    2961              :         /// @brief we urgently need to change lanes to follow our route
    2962      2372904 :         if (!mustOvertakeStopped(false, neighLane, neighLeaders, leaders, forwardPos, neighDist, right, latLaneDist, currentDist, latDist)) {
    2963      2358705 :             latDist = latLaneDist;
    2964      2358705 :             ret |= LCA_STRATEGIC | LCA_URGENT;
    2965              : #ifdef DEBUG_STRATEGIC_CHANGE
    2966              :             if (gDebugFlag2) {
    2967              :                 std::cout << SIMTIME << " mustChangeToBest\n";
    2968              :             }
    2969              : #endif
    2970              :         } else {
    2971              : #ifdef DEBUG_STRATEGIC_CHANGE
    2972              :             if (gDebugFlag2) {
    2973              :                 std::cout << " veh=" << myVehicle.getID() << " avoidStoppedNeigh\n";
    2974              :             }
    2975              : #endif
    2976              :         }
    2977              :     } else {
    2978              :         // VARIANT_20 (noOvertakeRight)
    2979     33774674 :         const MSVehicle* const stoppedLeader = getStopped(neighLeaders);
    2980     33774674 :         if (left && avoidOvertakeRight(stoppedLeader, true) && neighLeaders.hasVehicles()) {
    2981              :             // check for slower leader on the left. we should not overtake but
    2982              :             // rather move left ourselves (unless congested)
    2983              :             // XXX only adapt as much as possible to get a lateral gap
    2984      2861387 :             CLeaderDist cld = getSlowest(neighLeaders);
    2985      2861387 :             const MSVehicle* nv = cld.first;
    2986      2861387 :             double deltaV = 0.;
    2987      2861387 :             double vSafe = 0.;
    2988      2861387 :             if (canOvertakeRight(nv, cld.second, myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle) - neighLane.getVehicleMaxSpeed(nv), HELP_OVERTAKE, vSafe, deltaV)) {
    2989       560017 :                 addLCSpeedAdvice(vSafe);
    2990       560017 :                 if (vSafe < myVehicle.getSpeed()) {
    2991       179273 :                     mySpeedGainProbabilityRight += myVehicle.getActionStepLengthSecs() * myChangeProbThresholdLeft / 3;
    2992              :                 }
    2993              : #ifdef DEBUG_STRATEGIC_CHANGE
    2994              :                 if (gDebugFlag2) {
    2995              :                     std::cout << SIMTIME
    2996              :                               << " avoid overtaking on the right nv=" << nv->getID()
    2997              :                               << " nvSpeed=" << nv->getSpeed()
    2998              :                               << " mySpeedGainProbabilityR=" << mySpeedGainProbabilityRight
    2999              :                               << " plannedSpeed=" << myVehicle.getSpeed() + ACCEL2SPEED(myLCAccelerationAdvices.back().first)
    3000              :                               << "\n";
    3001              :                 }
    3002              : #endif
    3003              :             }
    3004              :         }
    3005              : 
    3006              :         // handling reaction to stopped for opposite direction driving NYI
    3007     33774674 :         const bool noOpposites = &myVehicle.getLane()->getEdge() == &neighLane.getEdge();
    3008     33774674 :         if (laneOffset != 0 && myStrategicParam >= 0 && noOpposites && mustOvertakeStopped(true, neighLane, leaders, neighLeaders, forwardPos, neighDist, right, latLaneDist, currentDist, latDist)) {
    3009              : #ifdef DEBUG_STRATEGIC_CHANGE
    3010              :             if (gDebugFlag2) {
    3011              :                 std::cout << " veh=" << myVehicle.getID() << " mustOvertakeStopped\n";
    3012              :             }
    3013              : #endif
    3014        24895 :             if (latDist == 0) {
    3015            0 :                 ret |= LCA_STAY | LCA_STRATEGIC;
    3016              :             } else {
    3017        24895 :                 ret |= LCA_STRATEGIC | LCA_URGENT;
    3018              :             }
    3019              : 
    3020     33749779 :         } else if (!changeToBest && (currentDistDisallows(neighLeftPlace, abs(bestLaneOffset) + 2, laDist))) {
    3021              :             // the opposite lane-changing direction should be done than the one examined herein
    3022              :             //  we'll check whether we assume we could change anyhow and get back in time...
    3023              :             //
    3024              :             // this rule prevents the vehicle from moving in opposite direction of the best lane
    3025              :             //  unless the way till the end where the vehicle has to be on the best lane
    3026              :             //  is long enough
    3027              : #ifdef DEBUG_STRATEGIC_CHANGE
    3028              :             if (gDebugFlag2) {
    3029              :                 std::cout << " veh=" << myVehicle.getID() << " could not change back and forth in time (1) neighLeftPlace=" << neighLeftPlace << "\n";
    3030              :             }
    3031              : #endif
    3032     11449345 :             ret |= LCA_STAY | LCA_STRATEGIC;
    3033              :         } else if (
    3034              :             laneOffset != 0
    3035     22300434 :             && bestLaneOffset == 0
    3036      2133632 :             && !leaders.hasStoppedVehicle()
    3037      2111669 :             && neigh.bestContinuations.back()->getLinkCont().size() != 0
    3038      1358159 :             && roundaboutBonus == 0
    3039      1358159 :             && !checkOpposite
    3040       632390 :             && neighDist < TURN_LANE_DIST
    3041     22492675 :             && myStrategicParam >= 0) {
    3042              :             // VARIANT_21 (stayOnBest)
    3043              :             // we do not want to leave the best lane for a lane which leads elsewhere
    3044              :             // unless our leader is stopped or we are approaching a roundabout
    3045              : #ifdef DEBUG_STRATEGIC_CHANGE
    3046              :             if (gDebugFlag2) {
    3047              :                 std::cout << " veh=" << myVehicle.getID() << " does not want to leave the bestLane (neighDist=" << neighDist << ")\n";
    3048              :             }
    3049              : #endif
    3050       188290 :             ret |= LCA_STAY | LCA_STRATEGIC;
    3051              :         } else if (right
    3052     22112144 :                    && bestLaneOffset == 0
    3053       575509 :                    && myVehicle.getLane()->getSpeedLimit() > 80. / 3.6
    3054     22134954 :                    && myLookAheadSpeed > SUMO_const_haltingSpeed
    3055              :                   ) {
    3056              :             // let's also regard the case where the vehicle is driving on a highway...
    3057              :             //  in this case, we do not want to get to the dead-end of an on-ramp
    3058              : #ifdef DEBUG_STRATEGIC_CHANGE
    3059              :             if (gDebugFlag2) {
    3060              :                 std::cout << " veh=" << myVehicle.getID() << " does not want to get stranded on the on-ramp of a highway\n";
    3061              :             }
    3062              : #endif
    3063        21831 :             ret |= LCA_STAY | LCA_STRATEGIC;
    3064              :         }
    3065              :     }
    3066    117009275 :     if (laneOffset != 0 && (ret & LCA_URGENT) == 0 && getShadowLane() != nullptr &&
    3067              :             // ignore overlap if it goes in the correct direction
    3068      1882060 :             bestLaneOffset * myVehicle.getLateralPositionOnLane() <= 0) {
    3069              :         // no decision or decision to stay
    3070              :         // make sure to stay within lane bounds in case the shadow lane ends
    3071              :         //const double requiredDist = MAX2(2 * myVehicle.getLateralOverlap(), getSublaneWidth()) / SUMO_const_laneWidth * laDist;
    3072       964025 :         const double requiredDist = 2 * overlap / SUMO_const_laneWidth * laDist;
    3073       964025 :         double currentShadowDist = -myVehicle.getPositionOnLane();
    3074              :         MSLane* shadowPrev = nullptr;
    3075      2049914 :         for (std::vector<MSLane*>::const_iterator it = curr.bestContinuations.begin(); it != curr.bestContinuations.end(); ++it) {
    3076      1790443 :             if (*it == nullptr) {
    3077        31960 :                 continue;
    3078              :             }
    3079      1758483 :             MSLane* shadow = getShadowLane(*it);
    3080      1758483 :             if (shadow == nullptr || currentShadowDist >= requiredDist) {
    3081              :                 break;
    3082              :             }
    3083      1053929 :             if (shadowPrev != nullptr) {
    3084       114733 :                 currentShadowDist += shadowPrev->getEdge().getInternalFollowingLengthTo(&shadow->getEdge(), myVehicle.getVClass());
    3085              :             }
    3086      1053929 :             currentShadowDist += shadow->getLength();
    3087              :             shadowPrev = shadow;
    3088              : #ifdef DEBUG_STRATEGIC_CHANGE
    3089              :             if (gDebugFlag2) {
    3090              :                 std::cout << "    shadow=" << shadow->getID() << " currentShadowDist=" << currentShadowDist << "\n";
    3091              :             }
    3092              : #endif
    3093              :         }
    3094              : #ifdef DEBUG_STRATEGIC_CHANGE
    3095              :         if (gDebugFlag2) {
    3096              :             std::cout << " veh=" << myVehicle.getID() << " currentShadowDist=" << currentShadowDist << " requiredDist=" << requiredDist << " overlap=" << overlap << "\n";
    3097              :         }
    3098              : #endif
    3099       964025 :         if (currentShadowDist < requiredDist && currentShadowDist < usableDist) {
    3100       160861 :             myLeftSpace = currentShadowDist;
    3101       160861 :             latDist = myVehicle.getLateralPositionOnLane() < 0 ? overlap : -overlap;
    3102              : #ifdef DEBUG_STRATEGIC_CHANGE
    3103              :             if (gDebugFlag2) {
    3104              :                 std::cout << "    must change for shadowLane end latDist=" << latDist << " myLeftSpace=" << myLeftSpace << "\n";
    3105              :             }
    3106              : #endif
    3107       160861 :             ret |= LCA_STRATEGIC | LCA_URGENT | LCA_STAY ;
    3108              :         }
    3109              :     }
    3110              : 
    3111              :     // check for overriding TraCI requests
    3112              : #if defined(DEBUG_STRATEGIC_CHANGE) || defined(DEBUG_TRACI)
    3113              :     if (gDebugFlag2) {
    3114              :         std::cout << SIMTIME << " veh=" << myVehicle.getID() << " ret=" << ret;
    3115              :     }
    3116              : #endif
    3117              :     // store state before canceling
    3118    117009275 :     getCanceledState(laneOffset) |= ret;
    3119    117009275 :     int retTraCI = myVehicle.influenceChangeDecision(ret);
    3120    117009275 :     if ((retTraCI & LCA_TRACI) != 0) {
    3121         3952 :         if ((retTraCI & LCA_STAY) != 0) {
    3122              :             ret = retTraCI;
    3123         3484 :             latDist = 0;
    3124          468 :         } else if (((retTraCI & LCA_RIGHT) != 0 && laneOffset < 0)
    3125          408 :                    || ((retTraCI & LCA_LEFT) != 0 && laneOffset > 0)) {
    3126              :             ret = retTraCI;
    3127          184 :             latDist = latLaneDist;
    3128              :         }
    3129              :     }
    3130              : #if defined(DEBUG_STRATEGIC_CHANGE) || defined(DEBUG_TRACI)
    3131              :     if (gDebugFlag2) {
    3132              :         std::cout << " reqAfterInfluence=" << toString((LaneChangeAction)retTraCI) << " ret=" << toString((LaneChangeAction)ret) << "\n";
    3133              :     }
    3134              : #endif
    3135    117009275 :     return ret;
    3136              : }
    3137              : 
    3138              : 
    3139              : bool
    3140     35901516 : MSLCM_SL2015::mustOvertakeStopped(bool checkCurrent, const MSLane& neighLane, const MSLeaderDistanceInfo& leaders, const MSLeaderDistanceInfo& neighLead,
    3141              :                                   double posOnLane, double neighDist, bool right, double latLaneDist, double& currentDist, double& latDist) {
    3142              :     bool mustOvertake = false;
    3143     35901516 :     const MSVehicle* const stoppedLeader = getStopped(leaders);
    3144     35901516 :     const bool checkOverTakeRight = avoidOvertakeRight(stoppedLeader, true);
    3145              :     int rightmost;
    3146              :     int leftmost;
    3147              :     const bool curHasStopped = stoppedLeader != nullptr;
    3148     35901516 :     const int dir = latLaneDist < 0 ? -1 : 1;
    3149     35901516 :     const MSLane* neighBeyond = neighLane.getParallelLane(dir);
    3150     35901516 :     const bool hasLaneBeyond = checkCurrent && neighBeyond != nullptr && neighBeyond->allowsVehicleClass(myVehicle.getVClass());
    3151              :     UNUSED_PARAMETER(hasLaneBeyond);
    3152     35901516 :     if (curHasStopped) {
    3153        96238 :         leaders.getSubLanes(&myVehicle, 0, rightmost, leftmost);
    3154       407448 :         for (int i = rightmost; i <= leftmost; i++) {
    3155       311210 :             const CLeaderDist& leader = leaders[i];
    3156       311210 :             if (leader.first != 0 && leader.first->isStopped() && leader.second < REACT_TO_STOPPED_DISTANCE) {
    3157       200020 :                 const double overtakeDist = leader.second + myVehicle.getVehicleType().getLength() + leader.first->getVehicleType().getLengthWithGap();
    3158       200020 :                 const double remaining = MIN2(neighDist, currentDist) - posOnLane;
    3159              : #ifdef DEBUG_STRATEGIC_CHANGE
    3160              :                 if (DEBUG_COND) {
    3161              :                     std::cout << "  overtakeDist=" << overtakeDist << " remaining=" << remaining
    3162              :                               << " minDistToStopped=" << neighLead.getMinDistToStopped()
    3163              :                               << " hasLaneBeyond=" << hasLaneBeyond
    3164              :                               << "\n";
    3165              :                 }
    3166              : #endif
    3167              :                 if (// current destination leaves enough space to overtake the leader
    3168              :                     remaining > overtakeDist
    3169              :                     // maybe do not overtake on the right at high speed
    3170        68450 :                     && (!checkCurrent || !checkOverTakeRight || !right)
    3171       267725 :                     && (!neighLead.hasStoppedVehicle() || neighLead.getMinDistToStopped() > overtakeDist /*|| (hasLaneBeyond && hasFreeLaneBeyond(neighBeyond, dir))*/)
    3172              :                     //&& (neighLead.first == 0 || !neighLead.first->isStopped()
    3173              :                     //    // neighboring stopped vehicle leaves enough space to overtake leader
    3174              :                     //    || neighLead.second > overtakeDist))
    3175              :                 ) {
    3176              :                     // avoid becoming stuck behind a stopped leader
    3177        39110 :                     currentDist = myVehicle.getPositionOnLane() + leader.second;
    3178        39110 :                     myLeftSpace = currentDist - posOnLane;
    3179        39110 :                     latDist = latLaneDist;
    3180              :                     mustOvertake = true;
    3181              : #ifdef DEBUG_STRATEGIC_CHANGE
    3182              :                     if (DEBUG_COND) {
    3183              :                         std::cout << " veh=" << myVehicle.getID() << " overtake stopped leader=" << leader.first->getID()
    3184              :                                   << " newCurrentDist=" << currentDist
    3185              :                                   << " overtakeDist=" << overtakeDist
    3186              :                                   << " remaining=" << remaining
    3187              :                                   << "\n";
    3188              :                     }
    3189              : #endif
    3190              :                 }
    3191              :             }
    3192              : 
    3193              :         }
    3194              :     }
    3195     35901516 :     if (!mustOvertake && !curHasStopped && neighLead.hasStoppedVehicle()) {
    3196              :         // #todo fix this if the neigh lane has a different width
    3197       145199 :         const double offset = (latLaneDist < 0 ? -1 : 1) * myVehicle.getLane()->getWidth();
    3198       124757 :         neighLead.getSubLanes(&myVehicle, offset, rightmost, leftmost);
    3199       619808 :         for (int i = 0; i < leaders.numSublanes(); i++) {
    3200       495051 :             const CLeaderDist& leader = leaders[i];
    3201       495051 :             if (leader.first != 0 && leader.first->isStopped() && leader.second < REACT_TO_STOPPED_DISTANCE) {
    3202              :                 mustOvertake = true;
    3203            0 :                 if (i >= rightmost && i <= leftmost) {
    3204            0 :                     latDist = myVehicle.getLateralOverlap() * (latLaneDist > 0 ? -1 : 1);
    3205            0 :                     break;
    3206              :                 }
    3207              :             }
    3208              :         }
    3209              :     }
    3210     35901516 :     return mustOvertake;
    3211              : }
    3212              : 
    3213              : 
    3214              : double
    3215    119608452 : MSLCM_SL2015::computeGapFactor(int state) const {
    3216    119608452 :     return (state & LCA_STRATEGIC) != 0 ? MAX2(0.0, (1.0 - myPushy * (1 + 0.5 * myImpatience))) : 1.0;
    3217              : }
    3218              : 
    3219              : 
    3220              : int
    3221    117006016 : MSLCM_SL2015::keepLatGap(int state,
    3222              :                          const MSLeaderDistanceInfo& leaders,
    3223              :                          const MSLeaderDistanceInfo& followers,
    3224              :                          const MSLeaderDistanceInfo& blockers,
    3225              :                          const MSLeaderDistanceInfo& neighLeaders,
    3226              :                          const MSLeaderDistanceInfo& neighFollowers,
    3227              :                          const MSLeaderDistanceInfo& neighBlockers,
    3228              :                          const MSLane& neighLane,
    3229              :                          int laneOffset,
    3230              :                          double& latDist,
    3231              :                          double& maneuverDist,
    3232              :                          int& blocked) {
    3233              : 
    3234              :     /* @notes
    3235              :      * vehicles may need to compromise between fulfilling lane change objectives
    3236              :      * (LCA_STRATEGIC, LCA_SPEED etc) and maintaining lateral gap. The minimum
    3237              :      * acceptable lateral gap depends on
    3238              :      * - the cultural context (China vs Europe)
    3239              :      * - the driver agressiveness (willingness to encroach on other vehicles to force them to move laterally as well)
    3240              :      *    - see @note in checkBlocking
    3241              :      * - the vehicle type (car vs motorcycle)
    3242              :      * - the current speed
    3243              :      * - the speed difference
    3244              :      * - the importance / urgency of the desired maneuver
    3245              :      *
    3246              :      * the object of this method is to evaluate the above circumstances and
    3247              :      * either:
    3248              :      * - allow the current maneuver (state, latDist)
    3249              :      * - to override the current maneuver with a distance-keeping maneuver
    3250              :      *
    3251              :      *
    3252              :      * laneChangeModel/driver parameters
    3253              :      * - bool pushy (willingness to encroach)
    3254              :      * - float minGap at 100km/h (to be interpolated for lower speeds (assume 0 at speed 0)
    3255              :      * - gapFactors (a factor for each of the change reasons
    3256              :      *
    3257              :      * further assumptions
    3258              :      * - the maximum of egoSpeed and deltaSpeed can be used when interpolating minGap
    3259              :      * - distance keeping to the edges of the road can be ignored (for now)
    3260              :      *
    3261              :      * currentMinGap = minGap * min(1.0, max(v, abs(v - vOther)) / 100) * gapFactor[lc_reason]
    3262              :      *
    3263              :      * */
    3264              : 
    3265              :     /// XXX to be made configurable
    3266    117006016 :     double gapFactor = computeGapFactor(state);
    3267    117006016 :     const double oldLatDist = latDist;
    3268    117006016 :     const double oldManeuverDist = maneuverDist;
    3269              :     /// passed state is without traci-influence but we need it here
    3270    117006016 :     const int traciState = myVehicle.influenceChangeDecision(state);
    3271              : 
    3272              :     // compute gaps after maneuver
    3273    117006016 :     const double halfWidth = getWidth() * 0.5;
    3274              :     // if the current maneuver is blocked we will stay where we are
    3275    117006016 :     const double oldCenter = myVehicle.getCenterOnEdge();
    3276              :     // surplus gaps. these are used to collect various constraints
    3277              :     // if they do not permit the desired maneuvre, should override it to better maintain distance
    3278              :     // stay within the current edge
    3279    117006016 :     double surplusGapRight = oldCenter - halfWidth;
    3280    117006016 :     double surplusGapLeft = getLeftBorder(laneOffset != 0) - oldCenter - halfWidth;
    3281              :     const bool stayInLane = (laneOffset == 0
    3282    117006016 :                              || ((traciState & LCA_STRATEGIC) != 0
    3283              :                                  && (traciState & LCA_STAY) != 0
    3284              :                                  // permit wide vehicles to stay on the road
    3285     11665375 :                                  && (surplusGapLeft >= 0 && surplusGapRight >= 0)));
    3286              : 
    3287    117006016 :     if (isOpposite()) {
    3288              :         std::swap(surplusGapLeft, surplusGapRight);
    3289              :     }
    3290              : #ifdef DEBUG_KEEP_LATGAP
    3291              :     if (gDebugFlag2) {
    3292              :         std::cout << "\n  " << SIMTIME << " keepLatGap() laneOffset=" << laneOffset
    3293              :                   << " latDist=" << latDist
    3294              :                   << " maneuverDist=" << maneuverDist
    3295              :                   << " state=" << toString((LaneChangeAction)state)
    3296              :                   << " traciState=" << toString((LaneChangeAction)traciState)
    3297              :                   << " blocked=" << toString((LaneChangeAction)blocked)
    3298              :                   << " gapFactor=" << gapFactor
    3299              :                   << " stayInLane=" << stayInLane << "\n"
    3300              :                   << "       stayInEdge: surplusGapRight=" << surplusGapRight << " surplusGapLeft=" << surplusGapLeft << "\n";
    3301              :     }
    3302              : #endif
    3303              :     // staying within the edge overrides all minGap considerations
    3304    117006016 :     if (surplusGapLeft < 0 || surplusGapRight < 0) {
    3305              :         gapFactor = 0;
    3306              :     }
    3307              : 
    3308              :     // maintain gaps to vehicles on the current lane
    3309              :     // ignore vehicles that are too far behind
    3310    117006016 :     const double netOverlap = -myVehicle.getVehicleType().getLength() * 0.5;
    3311    117006016 :     updateGaps(leaders, myVehicle.getLane()->getRightSideOnEdge(), oldCenter, gapFactor, surplusGapRight, surplusGapLeft, true);
    3312    117006016 :     updateGaps(followers, myVehicle.getLane()->getRightSideOnEdge(), oldCenter, gapFactor, surplusGapRight, surplusGapLeft, true, netOverlap);
    3313              : 
    3314    117006016 :     if (laneOffset != 0) {
    3315              :         // maintain gaps to vehicles on the target lane
    3316     36144319 :         const double neighRight = getNeighRight(neighLane);
    3317     36144319 :         updateGaps(neighLeaders, neighRight, oldCenter, gapFactor, surplusGapRight, surplusGapLeft, true);
    3318     36144319 :         updateGaps(neighFollowers, neighRight, oldCenter, gapFactor, surplusGapRight, surplusGapLeft, true, netOverlap);
    3319              :     }
    3320              : #ifdef DEBUG_KEEP_LATGAP
    3321              :     if (gDebugFlag2) {
    3322              :         std::cout << "       minGapLat: surplusGapRight=" << surplusGapRight << " surplusGapLeft=" << surplusGapLeft << "\n"
    3323              :                   << "       lastGaps: right=" << myLastLateralGapRight << " left=" << myLastLateralGapLeft << "\n";
    3324              :     }
    3325              : #endif
    3326              :     // we also need to track the physical gap, in addition to the psychological gap
    3327    117006016 :     double physicalGapLeft = myLastLateralGapLeft == NO_NEIGHBOR ? surplusGapLeft : myLastLateralGapLeft;
    3328    117006016 :     double physicalGapRight = myLastLateralGapRight == NO_NEIGHBOR ? surplusGapRight : myLastLateralGapRight;
    3329              : 
    3330    117006016 :     const double halfLaneWidth = myVehicle.getLane()->getWidth() * 0.5;
    3331    117006016 :     const double posLat = myVehicle.getLateralPositionOnLane() * (isOpposite() ? -1 : 1);
    3332    117006016 :     if (stayInLane || laneOffset == 1) {
    3333              :         // do not move past the right boundary of the current lane (traffic wasn't checked there)
    3334              :         // but assume it's ok to be where we are in case we are already beyond
    3335    303869005 :         surplusGapRight  = MIN2(surplusGapRight,  MAX2(0.0, halfLaneWidth + posLat - halfWidth));
    3336              :         physicalGapRight = MIN2(physicalGapRight, MAX2(0.0, halfLaneWidth + posLat - halfWidth));
    3337              :     }
    3338    117006016 :     if (stayInLane || laneOffset == -1) {
    3339              :         // do not move past the left boundary of the current lane (traffic wasn't checked there)
    3340              :         // but assume it's ok to be where we are in case we are already beyond
    3341    283994535 :         surplusGapLeft  = MIN2(surplusGapLeft,  MAX2(0.0, halfLaneWidth - posLat - halfWidth));
    3342              :         physicalGapLeft = MIN2(physicalGapLeft, MAX2(0.0, halfLaneWidth - posLat - halfWidth));
    3343              :     }
    3344              : #ifdef DEBUG_KEEP_LATGAP
    3345              :     if (gDebugFlag2) {
    3346              :         std::cout << "       stayInLane: surplusGapRight=" << surplusGapRight << " surplusGapLeft=" << surplusGapLeft << "\n";
    3347              :     }
    3348              : #endif
    3349              : 
    3350    117006016 :     if (surplusGapRight + surplusGapLeft < 0) {
    3351              :         // insufficient lateral space to fulfill all requirements. apportion space proportionally
    3352      3698887 :         if ((state & LCA_CHANGE_REASONS) == 0) {
    3353        29650 :             state |= LCA_SUBLANE;
    3354              :         }
    3355      3698887 :         const double equalDeficit = 0.5 * (surplusGapLeft + surplusGapRight);
    3356      3698887 :         if (surplusGapRight < surplusGapLeft) {
    3357              :             // shift further to the left but no further than there is physical space
    3358      1133261 :             const double delta = MIN2(equalDeficit - surplusGapRight, physicalGapLeft);
    3359      1133261 :             latDist = delta;
    3360      1133261 :             maneuverDist = delta;
    3361              : #ifdef DEBUG_KEEP_LATGAP
    3362              :             if (gDebugFlag2) {
    3363              :                 std::cout << "    insufficient latSpace, move left: delta=" << delta << "\n";
    3364              :             }
    3365              : #endif
    3366              :         } else {
    3367              :             // shift further to the right but no further than there is physical space
    3368      2565626 :             const double delta = MIN2(equalDeficit - surplusGapLeft, physicalGapRight);
    3369      2565626 :             latDist = -delta;
    3370      2565626 :             maneuverDist = -delta;
    3371              : #ifdef DEBUG_KEEP_LATGAP
    3372              :             if (gDebugFlag2) {
    3373              :                 std::cout << "    insufficient latSpace, move right: delta=" << delta << "\n";
    3374              :             }
    3375              : #endif
    3376              :         }
    3377              :     } else {
    3378              :         // sufficient space. move as far as the gaps permit
    3379    113307129 :         latDist = MAX2(MIN2(latDist, surplusGapLeft), -surplusGapRight);
    3380    113307129 :         maneuverDist = MAX2(MIN2(maneuverDist, surplusGapLeft), -surplusGapRight);
    3381    113307129 :         if ((state & LCA_KEEPRIGHT) != 0 && maneuverDist != oldManeuverDist) {
    3382              :             // don't start keepRight unless it can be completed
    3383       127413 :             latDist = oldLatDist;
    3384       127413 :             maneuverDist = oldManeuverDist;
    3385              :         }
    3386              : #ifdef DEBUG_KEEP_LATGAP
    3387              :         if (gDebugFlag2) {
    3388              :             std::cout << "     adapted latDist=" << latDist << " maneuverDist=" << maneuverDist << " (old=" << oldLatDist << ")\n";
    3389              :         }
    3390              : #endif
    3391              :     }
    3392              :     // take into account overriding traci sublane-request
    3393    117006016 :     if (myVehicle.hasInfluencer() && myVehicle.getInfluencer().getLatDist() != 0) {
    3394              :         // @note: the influence is reset in MSAbstractLaneChangeModel::setOwnState at the end of the lane-changing code for this vehicle
    3395         1888 :         latDist = myVehicle.getInfluencer().getLatDist();
    3396         1888 :         maneuverDist = myVehicle.getInfluencer().getLatDist();
    3397         1888 :         if (latDist < 0) {
    3398         1972 :             mySafeLatDistRight = MAX2(-latDist, mySafeLatDistRight);
    3399              :         } else {
    3400         1432 :             mySafeLatDistLeft = MAX2(latDist, mySafeLatDistLeft);
    3401              :         }
    3402         1888 :         state |= LCA_TRACI;
    3403              : #ifdef DEBUG_KEEP_LATGAP
    3404              :         if (gDebugFlag2) {
    3405              :             std::cout << "     traci influenced latDist=" << latDist << "\n";
    3406              :         }
    3407              : #endif
    3408              :     }
    3409              :     // if we cannot move in the desired direction, consider the maneuver blocked anyway
    3410    117006016 :     const bool nonSublaneChange = (state & (LCA_STRATEGIC | LCA_COOPERATIVE | LCA_SPEEDGAIN | LCA_KEEPRIGHT)) != 0;
    3411    117006016 :     const bool traciChange = ((state | traciState) & LCA_TRACI) != 0;
    3412    117006016 :     if (nonSublaneChange && !traciChange) {
    3413     19651604 :         if ((latDist < NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) && (oldLatDist > 0)) {
    3414              : #ifdef DEBUG_KEEP_LATGAP
    3415              :             if (gDebugFlag2) {
    3416              :                 std::cout << "     wanted changeToLeft oldLatDist=" << oldLatDist << ", blocked latGap changeToRight\n";
    3417              :             }
    3418              : #endif
    3419      1179642 :             latDist = oldLatDist; // restore old request for usage in decideDirection()
    3420      1179642 :             blocked = LCA_OVERLAPPING | LCA_BLOCKED_LEFT;
    3421     18471962 :         } else if ((latDist > -NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) && (oldLatDist < 0)) {
    3422              : #ifdef DEBUG_KEEP_LATGAP
    3423              :             if (gDebugFlag2) {
    3424              :                 std::cout << "     wanted changeToRight oldLatDist=" << oldLatDist << ", blocked latGap changeToLeft\n";
    3425              :             }
    3426              : #endif
    3427       726310 :             latDist = oldLatDist; // restore old request for usage in decideDirection()
    3428       726310 :             blocked = LCA_OVERLAPPING | LCA_BLOCKED_RIGHT;
    3429              :         }
    3430              :     }
    3431              :     // if we move, even though we wish to stay, update the change reason (except for TraCI)
    3432    117006016 :     if (fabs(latDist) > NUMERICAL_EPS * myVehicle.getActionStepLengthSecs() && oldLatDist == 0) {
    3433      1148618 :         state &= (~(LCA_CHANGE_REASONS | LCA_STAY) | LCA_TRACI);
    3434              :     }
    3435              :     // update blocked status
    3436    117006016 :     if (fabs(latDist - oldLatDist) > NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
    3437              : #ifdef DEBUG_KEEP_LATGAP
    3438              :         if (gDebugFlag2) {
    3439              :             std::cout << "     latDistUpdated=" << latDist << " oldLatDist=" << oldLatDist << "\n";
    3440              :         }
    3441              : #endif
    3442      4350938 :         blocked = checkBlocking(neighLane, latDist, maneuverDist, laneOffset, leaders, followers, blockers, neighLeaders, neighFollowers, neighBlockers, nullptr, nullptr, nonSublaneChange);
    3443              :     }
    3444    117006016 :     if (fabs(latDist) > NUMERICAL_EPS * myVehicle.getActionStepLengthSecs()) {
    3445     14979845 :         state = (state & ~LCA_STAY);
    3446     14979845 :         if ((state & LCA_CHANGE_REASONS) == 0) {
    3447      1147626 :             state |= LCA_SUBLANE;
    3448              :         }
    3449              :     } else {
    3450    102026171 :         if ((state & LCA_SUBLANE) != 0) {
    3451     89113982 :             state |= LCA_STAY;
    3452              :         }
    3453              :         // avoid setting blinker due to numerical issues
    3454    102026171 :         latDist = 0;
    3455              :     }
    3456              : #if defined(DEBUG_KEEP_LATGAP) || defined(DEBUG_STATE)
    3457              :     if (gDebugFlag2) {
    3458              :         std::cout << "       latDist2=" << latDist
    3459              :                   << " state2=" << toString((LaneChangeAction)state)
    3460              :                   << " lastGapLeft=" << myLastLateralGapLeft
    3461              :                   << " lastGapRight=" << myLastLateralGapRight
    3462              :                   << " blockedAfter=" << toString((LaneChangeAction)blocked)
    3463              :                   << "\n";
    3464              :     }
    3465              : #endif
    3466    117006016 :     return state;
    3467              : }
    3468              : 
    3469              : 
    3470              : void
    3471    389422974 : MSLCM_SL2015::updateGaps(const MSLeaderDistanceInfo& others, double foeOffset, double oldCenter, double gapFactor,
    3472              :                          double& surplusGapRight, double& surplusGapLeft,
    3473              :                          bool saveMinGap, double netOverlap,
    3474              :                          double latDist,
    3475              :                          std::vector<CLeaderDist>* collectBlockers) {
    3476    389422974 :     if (others.hasVehicles()) {
    3477    335003215 :         const double halfWidth = getWidth() * 0.5 + NUMERICAL_EPS;
    3478    335003215 :         const double baseMinGap = myMinGapLat;
    3479   1761808328 :         for (int i = 0; i < others.numSublanes(); ++i) {
    3480   2775194079 :             if (others[i].first != 0 && others[i].second <= 0
    3481   1459776682 :                     && myCFRelated.count(others[i].first) == 0
    3482   1639959315 :                     && (netOverlap == 0 || others[i].second + others[i].first->getVehicleType().getMinGap() < netOverlap)) {
    3483              :                 /// foe vehicle occupies full sublanes
    3484    180439400 :                 const MSVehicle* foe = others[i].first;
    3485    180439400 :                 const double res = MSGlobals::gLateralResolution > 0 ? MSGlobals::gLateralResolution : others[i].first->getLane()->getWidth();
    3486              :                 double foeRight, foeLeft;
    3487    180439400 :                 others.getSublaneBorders(i, foeOffset, foeRight, foeLeft);
    3488    180439400 :                 const double foeCenter = foeRight + 0.5 * res;
    3489    180439400 :                 const double gap = MIN2(fabs(foeRight - oldCenter), fabs(foeLeft - oldCenter)) - halfWidth;
    3490    180439400 :                 const double deltaV = MIN2(LATGAP_SPEED_THRESHOLD, MAX3(LATGAP_SPEED_THRESHOLD2, myVehicle.getSpeed(), fabs(myVehicle.getSpeed() - foe->getSpeed())));
    3491    180439400 :                 const double desiredMinGap = baseMinGap * deltaV / LATGAP_SPEED_THRESHOLD;
    3492    180439400 :                 const double currentMinGap = desiredMinGap * gapFactor; // pushy vehicles may accept a lower lateral gap temporarily
    3493              :                 /*
    3494              :                 if (netOverlap != 0) {
    3495              :                     // foe vehicle is follower with its front ahead of the ego midpoint
    3496              :                     // scale gap requirements so it gets lower for foe which are further behind ego
    3497              :                     //
    3498              :                     // relOverlap approaches 0 as the foe gets closer to the midpoint and it equals 1 if the foe is driving head-to-head
    3499              :                     const double relOverlap = 1 - (others[i].second + others[i].first->getVehicleType().getMinGap()) / netOverlap;
    3500              :                     currentMinGap *= currOverlap * relOverlap;
    3501              :                 }
    3502              :                 */
    3503              : #if defined(DEBUG_BLOCKING) || defined(DEBUG_KEEP_LATGAP)
    3504              :                 if (debugVehicle()) {
    3505              :                     std::cout << "  updateGaps"
    3506              :                               << " i=" << i
    3507              :                               << " foe=" << foe->getID()
    3508              :                               << " foeRight=" << foeRight
    3509              :                               << " foeLeft=" << foeLeft
    3510              :                               << " oldCenter=" << oldCenter
    3511              :                               << " gap=" << others[i].second
    3512              :                               << " latgap=" << gap
    3513              :                               << " currentMinGap=" << currentMinGap
    3514              :                               << " surplusGapRight=" << surplusGapRight
    3515              :                               << " surplusGapLeft=" << surplusGapLeft
    3516              :                               << "\n";
    3517              :                 }
    3518              : #endif
    3519              : 
    3520              :                 // If foe is maneuvering towards ego, reserve some additional distance.
    3521              :                 // But don't expect the foe to come closer than currentMinGap if it isn't already there.
    3522              :                 //   (XXX: How can the ego know the foe's maneuver dist?)
    3523    180439400 :                 if (foeCenter < oldCenter) { // && foe->getLaneChangeModel().getSpeedLat() > 0) {
    3524     90345961 :                     const double foeManeuverDist = MAX2(0., foe->getLaneChangeModel().getManeuverDist());
    3525    259629323 :                     surplusGapRight = MIN3(surplusGapRight, gap - currentMinGap, MAX2(currentMinGap, gap - foeManeuverDist));
    3526              :                 } else { //if (foeCenter > oldCenter && foe->getLaneChangeModel().getSpeedLat() < 0) {
    3527     90093439 :                     const double foeManeuverDist = -MIN2(0., foe->getLaneChangeModel().getManeuverDist());
    3528    259817647 :                     surplusGapLeft = MIN3(surplusGapLeft, gap - currentMinGap, MAX2(currentMinGap, gap - foeManeuverDist));
    3529              :                 }
    3530    180439400 :                 if (saveMinGap) {
    3531     86634285 :                     if (foeCenter < oldCenter) {
    3532              : #if defined(DEBUG_BLOCKING) || defined(DEBUG_KEEP_LATGAP)
    3533              :                         if (gDebugFlag2 && gap < myLastLateralGapRight) {
    3534              :                             std::cout << "    new minimum rightGap=" << gap << "\n";
    3535              :                         }
    3536              : #endif
    3537     79560597 :                         myLastLateralGapRight = MIN2(myLastLateralGapRight, gap);
    3538              :                     } else {
    3539              : #if defined(DEBUG_BLOCKING) || defined(DEBUG_KEEP_LATGAP)
    3540              :                         if (gDebugFlag2 && gap < myLastLateralGapLeft) {
    3541              :                             std::cout << "    new minimum leftGap=" << gap << "\n";
    3542              :                         }
    3543              : #endif
    3544     55090934 :                         myLastLateralGapLeft = MIN2(myLastLateralGapLeft, gap);
    3545              :                     }
    3546              :                 }
    3547    180439400 :                 if (collectBlockers != nullptr) {
    3548              :                     // check if the vehicle is blocking a desire lane change
    3549     10768854 :                     if ((foeCenter < oldCenter && latDist < 0 && gap < (desiredMinGap - latDist))
    3550      8296679 :                             || (foeCenter > oldCenter && latDist > 0 && gap < (desiredMinGap + latDist))) {
    3551      9365644 :                         collectBlockers->push_back(others[i]);
    3552              :                     }
    3553              :                 }
    3554              :             }
    3555              :         }
    3556              :     }
    3557    389422974 : }
    3558              : 
    3559              : 
    3560              : double
    3561   1083855415 : MSLCM_SL2015::getWidth() const {
    3562   1083855415 :     return myVehicle.getVehicleType().getWidth() + NUMERICAL_EPS;
    3563              : }
    3564              : 
    3565              : 
    3566              : double
    3567    117813611 : MSLCM_SL2015::computeSpeedLat(double latDist, double& maneuverDist, bool urgent) const {
    3568    117813611 :     int currentDirection = mySpeedLat >= 0 ? 1 : -1;
    3569    117813611 :     int directionWish = latDist >= 0 ? 1 : -1;
    3570    117813611 :     double maxSpeedLat = myVehicle.getVehicleType().getMaxSpeedLat();
    3571    117813611 :     double accelLat = myAccelLat;
    3572    117813611 :     if (!urgent && (myLeftSpace > POSITION_EPS || myMaxSpeedLatFactor < 0)) {
    3573     67868850 :         const double speedBound = myMaxSpeedLatStanding + myMaxSpeedLatFactor * myVehicle.getSpeed();
    3574     67868850 :         if (myMaxSpeedLatFactor >= 0) {
    3575              :             // speedbound increases with speed and needs an upper bound
    3576              :             maxSpeedLat = MIN2(maxSpeedLat, speedBound);
    3577              :         } else {
    3578              :             // speedbound decreases with speed and needs a lower bound
    3579              :             // (only useful if myMaxSpeedLatStanding > maxSpeedLat)
    3580              :             maxSpeedLat = MAX2(maxSpeedLat, speedBound);
    3581              :             // increase (never decrease) lateral acceleration in proportion
    3582          498 :             accelLat *= MAX2(1.0, speedBound / myVehicle.getVehicleType().getMaxSpeedLat());
    3583              :         }
    3584              :     }
    3585    117813611 :     if (getWidth() < myVehicle.getCurrentEdge()->getWidth() && !isOpposite()) {
    3586    117722492 :         const double rightVehSide = myVehicle.getRightSideOnEdge();
    3587    117722492 :         const double edgeOverlap = MAX2(-rightVehSide, rightVehSide + myVehicle.getVehicleType().getWidth() - myVehicle.getCurrentEdge()->getWidth());
    3588              :         // if vehicle is outside edge bounds. Permit stronger lateral maneuvering
    3589    117722492 :         accelLat = MAX2(accelLat, 2 * edgeOverlap);
    3590              :         maxSpeedLat = MAX2(maxSpeedLat, edgeOverlap);
    3591              :     }
    3592              : 
    3593              : #ifdef DEBUG_MANEUVER
    3594              :     if (debugVehicle()) {
    3595              :         std::cout << SIMTIME
    3596              :                   << " veh=" << myVehicle.getID()
    3597              :                   << " computeSpeedLat()"
    3598              :                   << " latDist=" << latDist
    3599              :                   << " maneuverDist=" << maneuverDist
    3600              :                   << " urgent=" << urgent
    3601              :                   << " speedLat=" << mySpeedLat
    3602              :                   << " currentDirection=" << currentDirection
    3603              :                   << " directionWish=" << directionWish
    3604              :                   << " myLeftSpace=" << myLeftSpace
    3605              :                   << " maxSpeedLat=" << maxSpeedLat
    3606              :                   << std::endl;
    3607              :     }
    3608              : #endif
    3609              :     // reduced lateral speed (in the desired direction). Don't change direction against desired.
    3610              :     double speedDecel;
    3611    117813611 :     if (directionWish == 1) {
    3612    110195705 :         speedDecel = MAX2(mySpeedLat - ACCEL2SPEED(accelLat), 0.);
    3613              :     } else {
    3614      7617906 :         speedDecel = MIN2(mySpeedLat + ACCEL2SPEED(accelLat), 0.);
    3615              :     }
    3616              :     // increased lateral speed (in the desired direction)
    3617    117813611 :     double speedAccel = MAX2(MIN2(mySpeedLat + directionWish * ACCEL2SPEED(accelLat), maxSpeedLat), -maxSpeedLat);
    3618              : 
    3619              :     // can we reach the target distance in a single step? (XXX: assumes "Euler" update)
    3620    117813611 :     double speedBound = DIST2SPEED(latDist);
    3621              :     // for lat-gap keeping maneuvres myOrigLatDist may be 0
    3622    228713284 :     const double fullLatDist = latDist > 0 ? MIN2(mySafeLatDistLeft, MAX2(maneuverDist, latDist)) : MAX2(-mySafeLatDistRight, MIN2(maneuverDist, latDist));
    3623              : 
    3624              :     // update maneuverDist, if safety constraints apply in its direction
    3625    117813611 :     if (maneuverDist * latDist > 0) {
    3626     14877405 :         maneuverDist = fullLatDist;
    3627              :     }
    3628              : 
    3629              : #ifdef DEBUG_MANEUVER
    3630              :     if (debugVehicle()) {
    3631              :         std::cout << "     mySafeLatDistRight=" << mySafeLatDistRight
    3632              :                   << " mySafeLatDistLeft=" << mySafeLatDistLeft
    3633              :                   << " fullLatDist=" << fullLatDist
    3634              :                   << " speedAccel=" << speedAccel
    3635              :                   << " speedDecel=" << speedDecel
    3636              :                   << " speedBound=" << speedBound
    3637              :                   << std::endl;
    3638              :     }
    3639              : #endif
    3640    117813611 :     if (speedDecel * speedAccel <= 0 && (
    3641              :                 // speedAccel and speedDecel bracket speed 0. This means we can end the maneuver
    3642    109417134 :                 (latDist >= 0 && speedAccel >= speedBound && speedBound >= speedDecel)
    3643      9043115 :                 || (latDist <= 0 && speedAccel <= speedBound && speedBound <= speedDecel))) {
    3644              :         // we can reach the desired value in this step
    3645              : #ifdef DEBUG_MANEUVER
    3646              :         if (debugVehicle()) {
    3647              :             std::cout << "   computeSpeedLat a)\n";
    3648              :         }
    3649              : #endif
    3650              :         return speedBound;
    3651              :     }
    3652              :     // are we currently moving in the wrong direction?
    3653      4082610 :     if (latDist * mySpeedLat < 0) {
    3654              : #ifdef DEBUG_MANEUVER
    3655              :         if (debugVehicle()) {
    3656              :             std::cout << "   computeSpeedLat b)\n";
    3657              :         }
    3658              : #endif
    3659       444597 :         return emergencySpeedLat(speedAccel);
    3660              :     }
    3661              :     // check if the remaining distance allows to accelerate laterally
    3662      3638013 :     double minDistAccel = SPEED2DIST(speedAccel) + currentDirection * MSCFModel::brakeGapEuler(fabs(speedAccel), accelLat, 0); // most we can move in the target direction
    3663      3638013 :     if ((fabs(minDistAccel) < fabs(fullLatDist)) || (fabs(minDistAccel - fullLatDist) < NUMERICAL_EPS)) {
    3664              : #ifdef DEBUG_MANEUVER
    3665              :         if (debugVehicle()) {
    3666              :             std::cout << "   computeSpeedLat c)\n";
    3667              :         }
    3668              : #endif
    3669              :         return speedAccel;
    3670              :     } else {
    3671              : #ifdef DEBUG_MANEUVER
    3672              :         if (debugVehicle()) {
    3673              :             std::cout << "      minDistAccel=" << minDistAccel << "\n";
    3674              :         }
    3675              : #endif
    3676              :         // check if the remaining distance allows to maintain current lateral speed
    3677       626522 :         double minDistCurrent = SPEED2DIST(mySpeedLat) + currentDirection * MSCFModel::brakeGapEuler(fabs(mySpeedLat), accelLat, 0);
    3678       626522 :         if ((fabs(minDistCurrent) < fabs(fullLatDist)) || (fabs(minDistCurrent - fullLatDist) < NUMERICAL_EPS)) {
    3679              : #ifdef DEBUG_MANEUVER
    3680              :             if (debugVehicle()) {
    3681              :                 std::cout << "   computeSpeedLat d)\n";
    3682              :             }
    3683              : #endif
    3684       151144 :             return mySpeedLat;
    3685              :         }
    3686              :     }
    3687              :     // reduce lateral speed
    3688              : #ifdef DEBUG_MANEUVER
    3689              :     if (debugVehicle()) {
    3690              :         std::cout << "   computeSpeedLat e)\n";
    3691              :     }
    3692              : #endif
    3693       475378 :     return emergencySpeedLat(speedDecel);
    3694              : }
    3695              : 
    3696              : 
    3697              : double
    3698       919975 : MSLCM_SL2015::emergencySpeedLat(double speedLat) const {
    3699              :     // reduce lateral speed for safety purposes
    3700       919975 :     if (speedLat < 0 && SPEED2DIST(-speedLat) > mySafeLatDistRight) {
    3701        14861 :         speedLat = -DIST2SPEED(mySafeLatDistRight);
    3702              : #ifdef DEBUG_MANEUVER
    3703              :         if (debugVehicle()) {
    3704              :             std::cout << "   rightDanger speedLat=" << speedLat << "\n";
    3705              :         }
    3706              : #endif
    3707       905114 :     } else if (speedLat > 0 && SPEED2DIST(speedLat) > mySafeLatDistLeft) {
    3708        23283 :         speedLat = DIST2SPEED(mySafeLatDistLeft);
    3709              : #ifdef DEBUG_MANEUVER
    3710              :         if (debugVehicle()) {
    3711              :             std::cout << "   leftDanger speedLat=" << speedLat << "\n";
    3712              :         }
    3713              : #endif
    3714              :     }
    3715       919975 :     return speedLat;
    3716              : }
    3717              : 
    3718              : 
    3719              : LatAlignmentDefinition
    3720     98043627 : MSLCM_SL2015::getDesiredAlignment() const {
    3721              :     LatAlignmentDefinition align = MSAbstractLaneChangeModel::getDesiredAlignment();
    3722              :     // Check whether the vehicle should adapt its alignment to an upcoming turn
    3723     98043627 :     if (myTurnAlignmentDist > 0) {
    3724       374040 :         const std::pair<double, const MSLink*>& turnInfo = myVehicle.getNextTurn();
    3725       374040 :         const LinkDirection turnDir = turnInfo.second == nullptr ? LinkDirection::NODIR : turnInfo.second->getDirection();
    3726       374040 :         const bool indirect = turnInfo.second == nullptr ? false : turnInfo.second->isIndirect();
    3727       374040 :         if (turnInfo.first < myTurnAlignmentDist) {
    3728              :             // Vehicle is close enough to the link to change its default alignment
    3729       158949 :             switch (turnDir) {
    3730        13757 :                 case LinkDirection::TURN:
    3731              :                 case LinkDirection::LEFT:
    3732              :                 case LinkDirection::PARTLEFT:
    3733        13757 :                     if (myVehicle.getLane()->getBidiLane() == nullptr) {
    3734              :                         // no left alignment on bidi lane to avoid blocking oncoming traffic
    3735        13689 :                         align = MSGlobals::gLefthand != indirect ? LatAlignmentDefinition::RIGHT : LatAlignmentDefinition::LEFT;
    3736              :                     }
    3737              :                     break;
    3738        12791 :                 case LinkDirection::TURN_LEFTHAND:
    3739              :                 case LinkDirection::RIGHT:
    3740              :                 case LinkDirection::PARTRIGHT:
    3741        12791 :                     align = MSGlobals::gLefthand != indirect ? LatAlignmentDefinition::LEFT : LatAlignmentDefinition::RIGHT;
    3742              :                     break;
    3743              :                 case LinkDirection::STRAIGHT:
    3744              :                 case LinkDirection::NODIR:
    3745              :                 default:
    3746              :                     break;
    3747              :             }
    3748              :         }
    3749              :     }
    3750     98043627 :     return align;
    3751              : }
    3752              : 
    3753              : 
    3754              : void
    3755       963672 : MSLCM_SL2015::commitManoeuvre(int blocked, int blockedFully,
    3756              :                               const MSLeaderDistanceInfo& leaders,
    3757              :                               const MSLeaderDistanceInfo& neighLeaders,
    3758              :                               const MSLane& neighLane,
    3759              :                               double maneuverDist) {
    3760       963672 :     if (!blocked && !blockedFully && !myCanChangeFully) {
    3761              :         // round to full action steps
    3762              :         double secondsToLeaveLane;
    3763       502417 :         if (MSGlobals::gSemiImplicitEulerUpdate) {
    3764       430723 :             secondsToLeaveLane = ceil(fabs(maneuverDist) / myVehicle.getVehicleType().getMaxSpeedLat() / myVehicle.getActionStepLengthSecs()) * myVehicle.getActionStepLengthSecs();
    3765              :             // XXX myAccelLat must be taken into account (refs #3601, see ballistic case for solution)
    3766              : 
    3767              :             // XXX This also causes probs: if the difference between the current speed and the committed is higher than the maximal decel,
    3768              :             //     the vehicle may pass myLeftSpace before completing the maneuver.
    3769       861446 :             myCommittedSpeed = MIN3(myLeftSpace / secondsToLeaveLane,
    3770       430723 :                                     myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle),
    3771       430723 :                                     myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle));
    3772              : #if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
    3773              :             if (debugVehicle()) {
    3774              :                 std::cout << SIMTIME << " veh=" << myVehicle.getID() << " myCommittedSpeed=" << myCommittedSpeed << " leftSpace=" << myLeftSpace << " secondsToLeave=" << secondsToLeaveLane << "\n";
    3775              :             }
    3776              : #endif
    3777              :         } else {
    3778              : 
    3779              :             // Calculate seconds needed for leaving lane assuming start from lateral speed zero, and lat.accel == -lat.decel
    3780        71694 :             secondsToLeaveLane = MSCFModel::estimateArrivalTime(fabs(maneuverDist), 0., 0., myVehicle.getVehicleType().getMaxSpeedLat(), myAccelLat, myAccelLat);
    3781              :             // round to full action steps
    3782        71694 :             secondsToLeaveLane = ceil(secondsToLeaveLane / myVehicle.getActionStepLengthSecs()) * myVehicle.getActionStepLengthSecs();
    3783              : 
    3784              :             // committed speed will eventually be pushed into a drive item during the next planMove() step. This item
    3785              :             // will not be read before the next action step at current time + actionStepLength-TS, so we need to schedule the corresponding speed.
    3786        71694 :             const double timeTillActionStep = myVehicle.getActionStepLengthSecs() - TS;
    3787        71694 :             const double nextActionStepSpeed = MAX2(0., myVehicle.getSpeed() + timeTillActionStep * myVehicle.getAcceleration());
    3788              :             double nextLeftSpace;
    3789        71298 :             if (nextActionStepSpeed > 0.) {
    3790        71295 :                 nextLeftSpace = myLeftSpace - timeTillActionStep * (myVehicle.getSpeed() + nextActionStepSpeed) * 0.5;
    3791          399 :             } else if (myVehicle.getAcceleration() == 0) {
    3792            0 :                 nextLeftSpace = myLeftSpace;
    3793              :             } else {
    3794              :                 assert(myVehicle.getAcceleration() < 0.);
    3795          399 :                 nextLeftSpace = myLeftSpace + (myVehicle.getSpeed() * myVehicle.getSpeed() / myVehicle.getAcceleration()) * 0.5;
    3796              :             }
    3797        71694 :             const double avoidArrivalSpeed = nextActionStepSpeed + ACCEL2SPEED(MSCFModel::avoidArrivalAccel(
    3798              :                                                  nextLeftSpace, secondsToLeaveLane - timeTillActionStep, nextActionStepSpeed, myVehicle.getCarFollowModel().getEmergencyDecel()));
    3799              : 
    3800       143388 :             myCommittedSpeed = MIN3(avoidArrivalSpeed,
    3801        71694 :                                     myVehicle.getSpeed() + myVehicle.getCarFollowModel().getMaxAccel() * myVehicle.getActionStepLengthSecs(),
    3802        71694 :                                     myVehicle.getLane()->getVehicleMaxSpeed(&myVehicle));
    3803              : 
    3804              : #if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
    3805              :             if (gDebugFlag2) {
    3806              :                 std::cout << SIMTIME
    3807              :                           << " veh=" << myVehicle.getID()
    3808              :                           << " avoidArrivalSpeed=" << avoidArrivalSpeed
    3809              :                           << " currentSpeed=" << myVehicle.getSpeed()
    3810              :                           << " myLeftSpace=" << myLeftSpace
    3811              :                           << "\n             nextLeftSpace=" << nextLeftSpace
    3812              :                           << " nextActionStepSpeed=" << nextActionStepSpeed
    3813              :                           << " nextActionStepRemainingSeconds=" << secondsToLeaveLane - timeTillActionStep
    3814              :                           << "\n";
    3815              :             }
    3816              : #endif
    3817              :         }
    3818       502417 :         myCommittedSpeed = commitFollowSpeed(myCommittedSpeed, maneuverDist, secondsToLeaveLane, leaders, myVehicle.getLane()->getRightSideOnEdge());
    3819       502417 :         myCommittedSpeed = commitFollowSpeed(myCommittedSpeed, maneuverDist, secondsToLeaveLane, neighLeaders, neighLane.getRightSideOnEdge());
    3820       502417 :         if (myCommittedSpeed < myVehicle.getCarFollowModel().minNextSpeed(myVehicle.getSpeed(), &myVehicle)) {
    3821        71462 :             myCommittedSpeed = 0;
    3822              :         }
    3823              : #if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
    3824              :         if (gDebugFlag2) {
    3825              :             std::cout << SIMTIME
    3826              :                       << " veh=" << myVehicle.getID()
    3827              :                       << " secondsToLeave=" << secondsToLeaveLane
    3828              :                       << " maxNext=" << myVehicle.getCarFollowModel().maxNextSpeed(myVehicle.getSpeed(), &myVehicle)
    3829              :                       << " committed=" << myCommittedSpeed
    3830              :                       << "\n";
    3831              :         }
    3832              : #endif
    3833              :     }
    3834       963672 : }
    3835              : 
    3836              : double
    3837      1004834 : MSLCM_SL2015::commitFollowSpeed(double speed, double latDist, double secondsToLeaveLane, const MSLeaderDistanceInfo& leaders, double foeOffset) const {
    3838      1004834 :     if (leaders.hasVehicles()) {
    3839              :         // we distinguish 3 cases
    3840              :         // - vehicles with lateral overlap at the end of the maneuver: try to follow safely
    3841              :         // - vehicles with overlap at the start of the maneuver: avoid collision within secondsToLeaveLane
    3842              :         // - vehicles without overlap: ignore
    3843              : 
    3844       487981 :         const double maxDecel = myVehicle.getCarFollowModel().getMaxDecel();
    3845              :         // temporarily use another decel value
    3846              :         MSCFModel& cfmodel = const_cast<MSCFModel&>(myVehicle.getCarFollowModel());
    3847       487981 :         cfmodel.setMaxDecel(maxDecel / getSafetyFactor());
    3848              : 
    3849       487981 :         const double vehWidth = getWidth();
    3850       487981 :         const double rightVehSide = myVehicle.getCenterOnEdge() - 0.5 * vehWidth;
    3851       487981 :         const double leftVehSide = rightVehSide + vehWidth;
    3852       487981 :         const double rightVehSideDest = rightVehSide + latDist;
    3853       487981 :         const double leftVehSideDest = leftVehSide + latDist;
    3854              : #if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
    3855              :         if (gDebugFlag2) {
    3856              :             std::cout << "  commitFollowSpeed"
    3857              :                       << " latDist=" << latDist
    3858              :                       << " foeOffset=" << foeOffset
    3859              :                       << " vehRight=" << rightVehSide
    3860              :                       << " vehLeft=" << leftVehSide
    3861              :                       << " destRight=" << rightVehSideDest
    3862              :                       << " destLeft=" << leftVehSideDest
    3863              :                       << "\n";
    3864              :         }
    3865              : #endif
    3866      2734063 :         for (int i = 0; i < leaders.numSublanes(); ++i) {
    3867      2246082 :             CLeaderDist vehDist = leaders[i];
    3868      2246082 :             if (vehDist.first != 0) {
    3869              :                 const MSVehicle* leader = vehDist.first;
    3870              :                 // only check the current stripe occuped by foe (transform into edge-coordinates)
    3871              :                 double foeRight, foeLeft;
    3872      1779353 :                 leaders.getSublaneBorders(i, foeOffset, foeRight, foeLeft);
    3873              : #if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
    3874              :                 if (gDebugFlag2) {
    3875              :                     std::cout << "   foe=" << vehDist.first->getID()
    3876              :                               << " gap=" << vehDist.second
    3877              :                               << " secGap=" << myVehicle.getCarFollowModel().getSecureGap(&myVehicle, leader, myVehicle.getSpeed(), leader->getSpeed(), leader->getCarFollowModel().getMaxDecel())
    3878              :                               << " foeRight=" << foeRight
    3879              :                               << " foeLeft=" << foeLeft
    3880              :                               << " overlapBefore=" << overlap(rightVehSide, leftVehSide, foeRight, foeLeft)
    3881              :                               << " overlapDest=" << overlap(rightVehSideDest, leftVehSideDest, foeRight, foeLeft)
    3882              :                               << "\n";
    3883              :                 }
    3884              : #endif
    3885      1779353 :                 if (overlap(rightVehSideDest, leftVehSideDest, foeRight, foeLeft)) {
    3886              :                     // case 1
    3887       811853 :                     const double vSafe = myVehicle.getCarFollowModel().followSpeed(
    3888       811853 :                                              &myVehicle, speed, vehDist.second, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel());
    3889              :                     speed = MIN2(speed, vSafe);
    3890              : #if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
    3891              :                     if (gDebugFlag2) {
    3892              :                         std::cout << "     case1 vsafe=" << vSafe << " speed=" << speed << "\n";
    3893              :                     }
    3894              : #endif
    3895       967500 :                 } else if (overlap(rightVehSide, leftVehSide, foeRight, foeLeft)) {
    3896              :                     // case 2
    3897       484070 :                     const double vSafe = myVehicle.getCarFollowModel().followSpeedTransient(
    3898              :                                              secondsToLeaveLane,
    3899       484070 :                                              &myVehicle, speed, vehDist.second, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel());
    3900              :                     speed = MIN2(speed, vSafe);
    3901              : #if defined(DEBUG_MANEUVER) || defined(DEBUG_COMMITTED_SPEED)
    3902              :                     if (gDebugFlag2) {
    3903              :                         std::cout << "     case2 vsafe=" << vSafe << " speed=" << speed << "\n";
    3904              :                     }
    3905              : #endif
    3906              :                 }
    3907              :             }
    3908              :         }
    3909              :         // restore original deceleration
    3910       487981 :         cfmodel.setMaxDecel(maxDecel);
    3911              : 
    3912              :     }
    3913      1004834 :     return speed;
    3914              : }
    3915              : 
    3916              : double
    3917    756704408 : MSLCM_SL2015::getSafetyFactor() const {
    3918    756704408 :     return 1 / ((1 + 0.5 * myImpatience) * myAssertive);
    3919              : }
    3920              : 
    3921              : double
    3922      1830751 : MSLCM_SL2015::getOppositeSafetyFactor() const {
    3923      1830751 :     return myOppositeParam <= 0 ? std::numeric_limits<double>::max() : 1 / myOppositeParam;
    3924              : }
    3925              : 
    3926              : 
    3927              : std::string
    3928          152 : MSLCM_SL2015::getParameter(const std::string& key) const {
    3929          152 :     if (key == toString(SUMO_ATTR_LCA_STRATEGIC_PARAM)) {
    3930           36 :         return toString(myStrategicParam);
    3931          116 :     } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_PARAM)) {
    3932           36 :         return toString(myCooperativeParam);
    3933           80 :     } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_PARAM)) {
    3934           36 :         return toString(mySpeedGainParam);
    3935           44 :     } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_PARAM)) {
    3936            0 :         return toString(myKeepRightParam);
    3937           44 :     } else if (key == toString(SUMO_ATTR_LCA_OPPOSITE_PARAM)) {
    3938            0 :         return toString(myOppositeParam);
    3939           44 :     } else if (key == toString(SUMO_ATTR_LCA_SUBLANE_PARAM)) {
    3940            0 :         return toString(mySublaneParam);
    3941           44 :     } else if (key == toString(SUMO_ATTR_MINGAP_LAT)) {
    3942           44 :         return toString(myMinGapLat);
    3943            0 :     } else if (key == toString(SUMO_ATTR_LCA_PUSHY)) {
    3944            0 :         return toString(myPushy);
    3945            0 :     } else if (key == toString(SUMO_ATTR_LCA_PUSHYGAP)) {
    3946            0 :         return toString((myPushy - 1) * myMinGapLat);
    3947            0 :     } else if (key == toString(SUMO_ATTR_LCA_ASSERTIVE)) {
    3948            0 :         return toString(myAssertive);
    3949            0 :     } else if (key == toString(SUMO_ATTR_LCA_IMPATIENCE)) {
    3950            0 :         return toString(myImpatience);
    3951            0 :     } else if (key == toString(SUMO_ATTR_LCA_TIME_TO_IMPATIENCE)) {
    3952            0 :         return toString(myTimeToImpatience);
    3953            0 :     } else if (key == toString(SUMO_ATTR_LCA_ACCEL_LAT)) {
    3954            0 :         return toString(myAccelLat);
    3955            0 :     } else if (key == toString(SUMO_ATTR_LCA_LOOKAHEADLEFT)) {
    3956            0 :         return toString(myLookaheadLeft);
    3957            0 :     } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAINRIGHT)) {
    3958            0 :         return toString(mySpeedGainRight);
    3959            0 :     } else if (key == toString(SUMO_ATTR_LCA_LANE_DISCIPLINE)) {
    3960            0 :         return toString(myLaneDiscipline);
    3961            0 :     } else if (key == toString(SUMO_ATTR_LCA_SIGMA)) {
    3962            0 :         return toString(mySigma);
    3963            0 :     } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_ACCEPTANCE_TIME)) {
    3964            0 :         return toString(myKeepRightAcceptanceTime);
    3965            0 :     } else if (key == toString(SUMO_ATTR_LCA_OVERTAKE_DELTASPEED_FACTOR)) {
    3966            0 :         return toString(myOvertakeDeltaSpeedFactor);
    3967            0 :     } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD)) {
    3968            0 :         return toString(mySpeedGainLookahead);
    3969            0 :     } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME)) {
    3970            0 :         return toString(mySpeedGainRemainTime);
    3971            0 :     } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_ROUNDABOUT)) {
    3972            0 :         return toString(myRoundaboutBonus);
    3973            0 :     } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_SPEED)) {
    3974            0 :         return toString(myCooperativeSpeed);
    3975            0 :     } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATSTANDING)) {
    3976            0 :         return toString(myMaxSpeedLatStanding);
    3977            0 :     } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATFACTOR)) {
    3978            0 :         return toString(myMaxSpeedLatFactor);
    3979            0 :     } else if (key == toString(SUMO_ATTR_LCA_MAXDISTLATSTANDING)) {
    3980            0 :         return toString(myMaxDistLatStanding);
    3981              :         // access to internal state for debugging in sumo-gui (not documented since it may change at any time)
    3982            0 :     } else if (key == "speedGainProbabilityRight") {
    3983            0 :         return toString(mySpeedGainProbabilityRight);
    3984            0 :     } else if (key == "speedGainProbabilityLeft") {
    3985            0 :         return toString(mySpeedGainProbabilityLeft);
    3986            0 :     } else if (key == "keepRightProbability") {
    3987            0 :         return toString(myKeepRightProbability);
    3988            0 :     } else if (key == "lookAheadSpeed") {
    3989            0 :         return toString(myLookAheadSpeed);
    3990            0 :     } else if (key == "sigmaState") {
    3991            0 :         return toString(mySigmaState);
    3992              :         // motivation relative to threshold
    3993            0 :     } else if (key == "speedGainRP") {
    3994            0 :         return toString(mySpeedGainProbabilityRight / myChangeProbThresholdRight);
    3995            0 :     } else if (key == "speedGainLP") {
    3996            0 :         return toString(mySpeedGainProbabilityLeft / myChangeProbThresholdLeft);
    3997            0 :     } else if (key == "keepRightP") {
    3998            0 :         return toString(myKeepRightProbability * myKeepRightParam / myChangeProbThresholdRight);
    3999              :     }
    4000            0 :     throw InvalidArgument("Parameter '" + key + "' is not supported for laneChangeModel of type '" + toString(myModel) + "'");
    4001              : }
    4002              : 
    4003              : void
    4004       277690 : MSLCM_SL2015::setParameter(const std::string& key, const std::string& value) {
    4005              :     double doubleValue;
    4006              :     try {
    4007       277690 :         doubleValue = StringUtils::toDouble(value);
    4008            0 :     } catch (NumberFormatException&) {
    4009            0 :         throw InvalidArgument("Setting parameter '" + key + "' requires a number for laneChangeModel of type '" + toString(myModel) + "'");
    4010            0 :     }
    4011       277690 :     if (key == toString(SUMO_ATTR_LCA_STRATEGIC_PARAM)) {
    4012        71075 :         myStrategicParam = doubleValue;
    4013       206615 :     } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_PARAM)) {
    4014            0 :         myCooperativeParam = doubleValue;
    4015       206615 :     } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_PARAM)) {
    4016            0 :         mySpeedGainParam = doubleValue;
    4017       206615 :     } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_PARAM)) {
    4018            0 :         myKeepRightParam = doubleValue;
    4019       206615 :     } else if (key == toString(SUMO_ATTR_LCA_OPPOSITE_PARAM)) {
    4020            0 :         myOppositeParam = doubleValue;
    4021       206615 :     } else if (key == toString(SUMO_ATTR_LCA_SUBLANE_PARAM)) {
    4022            0 :         mySublaneParam = doubleValue;
    4023       206615 :     } else if (key == toString(SUMO_ATTR_MINGAP_LAT)) {
    4024        68877 :         myMinGapLat = doubleValue;
    4025       137738 :     } else if (key == toString(SUMO_ATTR_LCA_PUSHY)) {
    4026            0 :         myPushy = doubleValue;
    4027       137738 :     } else if (key == toString(SUMO_ATTR_LCA_PUSHYGAP)) {
    4028            0 :         myPushy = 1 - doubleValue / myMinGapLat;
    4029       137738 :     } else if (key == toString(SUMO_ATTR_LCA_ASSERTIVE)) {
    4030            0 :         myAssertive = doubleValue;
    4031       137738 :     } else if (key == toString(SUMO_ATTR_LCA_IMPATIENCE)) {
    4032            0 :         myImpatience = doubleValue;
    4033            0 :         myMinImpatience = doubleValue;
    4034       137738 :     } else if (key == toString(SUMO_ATTR_LCA_TIME_TO_IMPATIENCE)) {
    4035            0 :         myTimeToImpatience = doubleValue;
    4036       137738 :     } else if (key == toString(SUMO_ATTR_LCA_ACCEL_LAT)) {
    4037            0 :         myAccelLat = doubleValue;
    4038       137738 :     } else if (key == toString(SUMO_ATTR_LCA_TURN_ALIGNMENT_DISTANCE)) {
    4039            0 :         myTurnAlignmentDist = doubleValue;
    4040       137738 :     } else if (key == toString(SUMO_ATTR_LCA_LOOKAHEADLEFT)) {
    4041            0 :         myLookaheadLeft = doubleValue;
    4042       137738 :     } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAINRIGHT)) {
    4043            0 :         mySpeedGainRight = doubleValue;
    4044       137738 :     } else if (key == toString(SUMO_ATTR_LCA_LANE_DISCIPLINE)) {
    4045            0 :         myLaneDiscipline = doubleValue;
    4046       137738 :     } else if (key == toString(SUMO_ATTR_LCA_SIGMA)) {
    4047            0 :         mySigma = doubleValue;
    4048       137738 :     } else if (key == toString(SUMO_ATTR_LCA_KEEPRIGHT_ACCEPTANCE_TIME)) {
    4049            0 :         myKeepRightAcceptanceTime = doubleValue;
    4050       137738 :     } else if (key == toString(SUMO_ATTR_LCA_OVERTAKE_DELTASPEED_FACTOR)) {
    4051            0 :         myOvertakeDeltaSpeedFactor = doubleValue;
    4052       137738 :     } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_LOOKAHEAD)) {
    4053        68869 :         mySpeedGainLookahead = doubleValue;
    4054        68869 :     } else if (key == toString(SUMO_ATTR_LCA_SPEEDGAIN_REMAIN_TIME)) {
    4055        68869 :         mySpeedGainRemainTime = doubleValue;
    4056            0 :     } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_ROUNDABOUT)) {
    4057            0 :         myRoundaboutBonus = doubleValue;
    4058            0 :     } else if (key == toString(SUMO_ATTR_LCA_COOPERATIVE_SPEED)) {
    4059            0 :         myCooperativeSpeed = doubleValue;
    4060            0 :     } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATSTANDING)) {
    4061            0 :         myMaxSpeedLatStanding = doubleValue;
    4062            0 :     } else if (key == toString(SUMO_ATTR_LCA_MAXSPEEDLATFACTOR)) {
    4063            0 :         myMaxSpeedLatFactor = doubleValue;
    4064            0 :     } else if (key == toString(SUMO_ATTR_LCA_MAXDISTLATSTANDING)) {
    4065            0 :         myMaxDistLatStanding = doubleValue;
    4066              :         // access to internal state
    4067            0 :     } else if (key == "speedGainProbabilityRight") {
    4068            0 :         mySpeedGainProbabilityRight = doubleValue;
    4069            0 :     } else if (key == "speedGainProbabilityLeft") {
    4070            0 :         mySpeedGainProbabilityLeft = doubleValue;
    4071            0 :     } else if (key == "keepRightProbability") {
    4072            0 :         myKeepRightProbability = doubleValue;
    4073            0 :     } else if (key == "lookAheadSpeed") {
    4074            0 :         myLookAheadSpeed = doubleValue;
    4075            0 :     } else if (key == "sigmaState") {
    4076            0 :         mySigmaState = doubleValue;
    4077              :     } else {
    4078            0 :         throw InvalidArgument("Setting parameter '" + key + "' is not supported for laneChangeModel of type '" + toString(myModel) + "'");
    4079              :     }
    4080       277690 :     initDerivedParameters();
    4081       277690 : }
    4082              : 
    4083              : 
    4084              : int
    4085         3259 : MSLCM_SL2015::wantsChange(
    4086              :     int laneOffset,
    4087              :     MSAbstractLaneChangeModel::MSLCMessager& /* msgPass */,
    4088              :     int blocked,
    4089              :     const std::pair<MSVehicle*, double>& leader,
    4090              :     const std::pair<MSVehicle*, double>& follower,
    4091              :     const std::pair<MSVehicle*, double>& neighLead,
    4092              :     const std::pair<MSVehicle*, double>& neighFollow,
    4093              :     const MSLane& neighLane,
    4094              :     const std::vector<MSVehicle::LaneQ>& preb,
    4095              :     MSVehicle** lastBlocked,
    4096              :     MSVehicle** firstBlocked) {
    4097              : 
    4098              :     const LaneChangeAction alternatives = LCA_NONE; // @todo pas this data
    4099              : 
    4100              : #ifdef DEBUG_WANTSCHANGE
    4101              :     if (DEBUG_COND) {
    4102              :         std::cout << "\nWANTS_CHANGE\n" << SIMTIME
    4103              :                   //<< std::setprecision(10)
    4104              :                   << " veh=" << myVehicle.getID()
    4105              :                   << " lane=" << myVehicle.getLane()->getID()
    4106              :                   << " neigh=" << neighLane.getID()
    4107              :                   << " pos=" << myVehicle.getPositionOnLane()
    4108              :                   << " posLat=" << myVehicle.getLateralPositionOnLane()
    4109              :                   << " speed=" << myVehicle.getSpeed()
    4110              :                   << " considerChangeTo=" << (laneOffset == -1  ? "right" : "left")
    4111              :                   << "\n";
    4112              :     }
    4113              : #endif
    4114              : 
    4115         3259 :     double latDist = 0;
    4116         3259 :     const double laneWidth = myVehicle.getLane()->getWidth();
    4117         3259 :     MSLeaderDistanceInfo leaders(leader, laneWidth);
    4118         3259 :     MSLeaderDistanceInfo followers(follower, laneWidth);
    4119         3259 :     MSLeaderDistanceInfo blockers(std::make_pair((MSVehicle*)nullptr, -1), laneWidth);
    4120         3259 :     MSLeaderDistanceInfo neighLeaders(neighLead, laneWidth);
    4121         3259 :     MSLeaderDistanceInfo neighFollowers(neighFollow, laneWidth);
    4122         3259 :     MSLeaderDistanceInfo neighBlockers(std::make_pair((MSVehicle*)nullptr, -1), laneWidth);
    4123              : 
    4124              :     double maneuverDist;
    4125         3259 :     int result = _wantsChangeSublane(laneOffset,
    4126              :                                      alternatives,
    4127              :                                      leaders, followers, blockers,
    4128              :                                      neighLeaders, neighFollowers, neighBlockers,
    4129              :                                      neighLane, preb,
    4130              :                                      lastBlocked, firstBlocked, latDist, maneuverDist, blocked);
    4131              : 
    4132         3259 :     myCanChangeFully = true;
    4133              :     // ignore sublane motivation
    4134         3259 :     result &= ~LCA_SUBLANE;
    4135         3259 :     result |= getLCA(result, latDist);
    4136              : 
    4137              : #if defined(DEBUG_WANTSCHANGE) || defined(DEBUG_STATE)
    4138              :     if (DEBUG_COND) {
    4139              :         if (result & LCA_WANTS_LANECHANGE) {
    4140              :             std::cout << SIMTIME
    4141              :                       << " veh=" << myVehicle.getID()
    4142              :                       << " wantsChangeTo=" << (laneOffset == -1  ? "right" : "left")
    4143              :                       << ((result & LCA_URGENT) ? " (urgent)" : "")
    4144              :                       << ((result & LCA_CHANGE_TO_HELP) ? " (toHelp)" : "")
    4145              :                       << ((result & LCA_STRATEGIC) ? " (strat)" : "")
    4146              :                       << ((result & LCA_COOPERATIVE) ? " (coop)" : "")
    4147              :                       << ((result & LCA_SPEEDGAIN) ? " (speed)" : "")
    4148              :                       << ((result & LCA_KEEPRIGHT) ? " (keepright)" : "")
    4149              :                       << ((result & LCA_TRACI) ? " (traci)" : "")
    4150              :                       << ((blocked & LCA_BLOCKED) ? " (blocked)" : "")
    4151              :                       << ((blocked & LCA_OVERLAPPING) ? " (overlap)" : "")
    4152              :                       << "\n\n\n";
    4153              :         }
    4154              :     }
    4155              : #endif
    4156              : 
    4157         3259 :     return result;
    4158         3259 : }
    4159              : 
    4160              : 
    4161              : double
    4162    136738376 : MSLCM_SL2015::getLeftBorder(bool checkOpposite) const {
    4163    136738376 :     return (myVehicle.getLane()->getEdge().getWidth()
    4164    136738376 :             + ((myVehicle.getLane()->getParallelOpposite() != nullptr && checkOpposite) ? myVehicle.getLane()->getParallelOpposite()->getEdge().getWidth() : 0));
    4165              : }
    4166              : 
    4167              : double
    4168    138430997 : MSLCM_SL2015::getVehicleCenter() const {
    4169    138430997 :     if (isOpposite()) {
    4170       155906 :         return myVehicle.getEdge()->getWidth() + myVehicle.getLane()->getWidth() * 0.5 - myVehicle.getLateralPositionOnLane();
    4171              :     } else {
    4172    138275091 :         return myVehicle.getCenterOnEdge();
    4173              :     }
    4174              : }
    4175              : 
    4176              : double
    4177     67674954 : MSLCM_SL2015::getNeighRight(const MSLane& neighLane) const {
    4178     67674954 :     if (isOpposite()) {
    4179       132784 :         return myVehicle.getLane()->getRightSideOnEdge() - neighLane.getWidth() + 2 * myVehicle.getLateralPositionOnLane();
    4180     67542170 :     } else if ((&myVehicle.getLane()->getEdge() != &neighLane.getEdge())) {
    4181       117745 :         return myVehicle.getLane()->getRightSideOnEdge() + myVehicle.getLane()->getWidth();
    4182              :     } else {
    4183              :         // the normal case
    4184     67424425 :         return neighLane.getRightSideOnEdge();
    4185              :     }
    4186              : }
    4187              : 
    4188              : 
    4189              : bool
    4190      8609506 : MSLCM_SL2015::preventSliding(double maneuverDist) const {
    4191              :     // prevent wide maneuvers with unsufficient forward space
    4192      8609506 :     if (fabs(maneuverDist) > myMaxDistLatStanding) {
    4193              :         // emergency vehicles should not be restricted (TODO solve this with LCA_URGENT)
    4194      8251123 :         if (myVehicle.getVehicleType().getVehicleClass() == SVC_EMERGENCY) {
    4195              :             return false;
    4196              :         }
    4197      8223227 :         const double brakeGap = myVehicle.getCarFollowModel().brakeGap(myVehicle.getSpeed());
    4198      8223227 :         const bool isSlide = fabs(maneuverDist) > myMaxDistLatStanding + brakeGap * fabs(myMaxSpeedLatFactor);
    4199              : #ifdef DEBUG_SLIDING
    4200              :         if (gDebugFlag2) {
    4201              :             std::cout << SIMTIME << " veh=" << myVehicle.getID() << " bgap=" << brakeGap << " maneuverDist=" << maneuverDist
    4202              :                       << " mds=" << myMaxDistLatStanding << " isSlide=" << isSlide << "\n";
    4203              :         }
    4204              : #endif
    4205      8223227 :         return isSlide;
    4206              :     }
    4207              :     return false;
    4208              : }
    4209              : 
    4210              : bool
    4211              : MSLCM_SL2015::wantsKeepRight(double keepRightProb) const {
    4212      9707946 :     return keepRightProb * myKeepRightParam > MAX2(myChangeProbThresholdRight, mySpeedGainProbabilityLeft);
    4213              : }
    4214              : 
    4215              : 
    4216              : bool
    4217        23319 : MSLCM_SL2015::saveBlockerLength(double length, double foeLeftSpace) {
    4218        23319 :     const bool canReserve = MSLCHelper::canSaveBlockerLength(myVehicle, length, myLeftSpace);
    4219        23319 :     if (!isOpposite() && (canReserve || myLeftSpace > foeLeftSpace)) {
    4220        17426 :         myLeadingBlockerLength = MAX2(length, myLeadingBlockerLength);
    4221        17426 :         if (myLeftSpace == 0 && foeLeftSpace < 0) {
    4222              :             // called from opposite overtaking, myLeftSpace must be initialized
    4223         1268 :             myLeftSpace = myVehicle.getBestLanes()[myVehicle.getLane()->getIndex()].length - myVehicle.getPositionOnLane();
    4224              :         }
    4225        17426 :         return true;
    4226              :     } else {
    4227              :         return false;
    4228              :     }
    4229              : }
    4230              : 
    4231              : 
    4232              : bool
    4233     27722642 : MSLCM_SL2015::outsideEdge() const {
    4234     27722642 :     return myVehicle.getLeftSideOnEdge() < 0 || myVehicle.getRightSideOnEdge() > myVehicle.getLane()->getEdge().getWidth();
    4235              : }
    4236              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1