LCOV - code coverage report
Current view: top level - src/microsim/cfmodels - MSCFModel_IDM.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 90.9 % 77 70
Test Date: 2024-12-21 15:45:41 Functions: 85.7 % 14 12

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    MSCFModel_IDM.cpp
      15              : /// @author  Tobias Mayer
      16              : /// @author  Daniel Krajzewicz
      17              : /// @author  Michael Behrisch
      18              : /// @date    Thu, 03 Sep 2009
      19              : ///
      20              : // The Intelligent Driver Model (IDM) car-following model
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #include "MSCFModel_IDM.h"
      25              : #include <microsim/MSVehicle.h>
      26              : 
      27              : //#define DEBUG_V
      28              : //#define DEBUG_INSERTION_SPEED
      29              : 
      30              : #define DEBUG_COND (veh->isSelected())
      31              : //#define DEBUG_COND true
      32              : 
      33              : 
      34              : // ===========================================================================
      35              : // method definitions
      36              : // ===========================================================================
      37         7886 : MSCFModel_IDM::MSCFModel_IDM(const MSVehicleType* vtype, bool idmm) :
      38              :     MSCFModel(vtype),
      39         7886 :     myIDMM(idmm),
      40         7626 :     myDelta(idmm ? 4.0 : vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDM_DELTA, 4.)),
      41         7886 :     myAdaptationFactor(idmm ? vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDMM_ADAPT_FACTOR, 1.8) : 1.0),
      42         7886 :     myAdaptationTime(idmm ? vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDMM_ADAPT_TIME, 600.0) : 0.0),
      43         7886 :     myIterations(MAX2(1, int(TS / vtype->getParameter().getCFParam(SUMO_ATTR_CF_IDM_STEPPING, .25) + .5))),
      44         7886 :     myTwoSqrtAccelDecel(double(2 * sqrt(myAccel * myDecel))) {
      45              :     // IDM does not drive very precise and may violate minGap on occasion
      46         7886 :     myCollisionMinGapFactor = vtype->getParameter().getCFParam(SUMO_ATTR_COLLISION_MINGAP_FACTOR, 0.1);
      47         7886 :     if (TS / myIterations > 0.25) {
      48           24 :         WRITE_WARNINGF("Stepping duration of % for % model in vType % is unsafe", (TS / myIterations), myIDMM ? "IDMM" : "IDM", vtype->getID());
      49              :     }
      50         7886 : }
      51              : 
      52        15612 : MSCFModel_IDM::~MSCFModel_IDM() {}
      53              : 
      54              : 
      55              : double
      56     66849753 : MSCFModel_IDM::minNextSpeed(double speed, const MSVehicle* const /*veh*/) const {
      57              :     // permit exceeding myDecel when approaching stops
      58    133693822 :     const double decel = MAX2(myDecel, MIN2(myEmergencyDecel, 1.5));
      59     66849753 :     if (MSGlobals::gSemiImplicitEulerUpdate) {
      60     64400039 :         return MAX2(speed - ACCEL2SPEED(decel), 0.);
      61              :     } else {
      62              :         // NOTE: ballistic update allows for negative speeds to indicate a stop within the next timestep
      63      2449714 :         return speed - ACCEL2SPEED(decel);
      64              :     }
      65              : }
      66              : 
      67              : 
      68              : 
      69              : double
      70     33232266 : MSCFModel_IDM::finalizeSpeed(MSVehicle* const veh, double vPos) const {
      71     33232266 :     const double vNext = MSCFModel::finalizeSpeed(veh, vPos);
      72     33232266 :     if (myAdaptationFactor != 1.) {
      73              :         VehicleVariables* vars = (VehicleVariables*)veh->getCarFollowVariables();
      74      3653034 :         vars->levelOfService += (vNext / veh->getLane()->getVehicleMaxSpeed(veh) - vars->levelOfService) / myAdaptationTime * TS;
      75              :     }
      76     33232266 :     return vNext;
      77              : }
      78              : 
      79              : 
      80              : double
      81     58230815 : MSCFModel_IDM::freeSpeed(const MSVehicle* const veh, double speed, double seen, double maxSpeed, const bool /*onInsertion*/, const CalcReason /*usage*/) const {
      82     58230815 :     if (maxSpeed < 0.) {
      83              :         // can occur for ballistic update (in context of driving at red light)
      84              :         return maxSpeed;
      85              :     }
      86     58230815 :     const double secGap = getSecureGap(veh, nullptr, maxSpeed, 0, myDecel);
      87              :     double vSafe;
      88     58230815 :     if (speed <= maxSpeed) {
      89              :         // accelerate
      90     57396190 :         vSafe = _v(veh, 1e6, speed, maxSpeed, veh->getLane()->getVehicleMaxSpeed(veh), false);
      91              :     } else {
      92              :         // decelerate
      93              :         // @note relax gap to avoid emergency braking
      94              :         // @note since the transition point does not move we set the leader speed to 0
      95      1669250 :         vSafe = _v(veh, MAX2(seen, secGap), speed, 0, veh->getLane()->getVehicleMaxSpeed(veh), false);
      96              :     }
      97     58230815 :     if (seen < secGap) {
      98              :         // avoid overshoot when close to change in speed limit
      99              :         vSafe = MIN2(vSafe, maxSpeed);
     100              :     }
     101              :     //std::cout << SIMTIME << " speed=" << speed << " maxSpeed=" << maxSpeed << " seen=" << seen << " secGap=" << secGap << " vSafe=" << vSafe << "\n";
     102              :     return vSafe;
     103              : }
     104              : 
     105              : 
     106              : double
     107    214256464 : MSCFModel_IDM::followSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle* const pred, const CalcReason /*usage*/) const {
     108    214256464 :     applyHeadwayAndSpeedDifferencePerceptionErrors(veh, speed, gap2pred, predSpeed, predMaxDecel, pred);
     109    214256464 :     return _v(veh, gap2pred, speed, predSpeed, veh->getLane()->getVehicleMaxSpeed(veh));
     110              : }
     111              : 
     112              : 
     113              : double
     114      1869744 : MSCFModel_IDM::insertionFollowSpeed(const MSVehicle* const v, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle* const pred) const {
     115              :     // see definition of s in _v()
     116      1869744 :     double s = MAX2(0., speed * myHeadwayTime + speed * (speed - predSpeed) / myTwoSqrtAccelDecel);
     117      1869744 :     if (gap2pred >= s) {
     118              :         // followSpeed always stays below speed because s*s / (gap2pred * gap2pred) > 0. This would prevent insertion with maximum speed at all distances
     119              :         return speed;
     120              :     } else {
     121              :         // we cannot call follow speed directly because it assumes that 'speed'
     122              :         // is the current speed rather than the desired insertion speed.
     123              :         // If the safe speed is much lower than the desired speed, the
     124              :         // followSpeed function would still return a new speed that involves
     125              :         // reasonable braking rather than the actual safe speed (and cause
     126              :         // emergency braking in a subsequent step)
     127      1400027 :         const double speed2 = followSpeed(v, speed, gap2pred, predSpeed, predMaxDecel, pred, CalcReason::FUTURE);
     128      1400027 :         const double speed3 = followSpeed(v, speed2, gap2pred, predSpeed, predMaxDecel, pred, CalcReason::FUTURE);
     129      1400027 :         if (speed2 - speed3 < ACCEL2SPEED(1)) {
     130              :             return speed2;
     131              :         } else {
     132              : #ifdef DEBUG_INSERTION_SPEED
     133              :             std::cout << SIMTIME << " veh=" << v->getID() << " speed=" << speed << " gap2pred=" << gap2pred << " predSpeed=" << predSpeed << " predMaxDecel=" << predMaxDecel << " pred=" << Named::getIDSecure(pred) << " s=" << s << " speed2=" << speed2 << " speed3=" << speed3 << "\n";
     134              : #endif
     135      1233687 :             return insertionFollowSpeed(v, speed2, gap2pred, predSpeed, predMaxDecel, pred);
     136              :         }
     137              :     }
     138              : }
     139              : 
     140              : 
     141              : double
     142         4665 : MSCFModel_IDM::insertionStopSpeed(const MSVehicle* const veh, double speed, double gap) const {
     143              :     // we want to insert the vehicle in an equilibrium state
     144         4665 :     double result = MSCFModel::insertionStopSpeed(veh, speed, gap);
     145              :     int i = 0;
     146        13986 :     while (result - speed < -ACCEL2SPEED(myDecel) && ++i < 10) {
     147              :         speed = result;
     148         9321 :         result = MSCFModel::insertionStopSpeed(veh, speed, gap);
     149              :     }
     150         4665 :     return result;
     151              : }
     152              : 
     153              : 
     154              : double
     155     73407944 : MSCFModel_IDM::stopSpeed(const MSVehicle* const veh, const double speed, double gap, double decel, const CalcReason /*usage*/) const {
     156     73407944 :     applyHeadwayPerceptionError(veh, speed, gap);
     157     73407944 :     if (gap < 0.01) {
     158              :         return 0;
     159              :     }
     160     72876568 :     double result = _v(veh, gap, speed, 0, veh->getLane()->getVehicleMaxSpeed(veh), false);
     161              :     //std::cout << SIMTIME << " stopSpeed speed=" << speed << " gap=" << gap << " decel=" << decel << " result=" << result << "\n";
     162     72876568 :     if (gap > 0 && speed < NUMERICAL_EPS && result < NUMERICAL_EPS) {
     163              :         // ensure that stops can be reached:
     164              :         //std::cout << " switching to krauss: " << veh->getID() << " gap=" << gap << " speed=" << speed << " res1=" << result << " res2=" << maximumSafeStopSpeed(gap, speed, false, veh->getActionStepLengthSecs())<< "\n";
     165        18225 :         result = maximumSafeStopSpeed(gap, decel, speed, false, veh->getActionStepLengthSecs());
     166              :     }
     167              :     // avoid overshooting the stop location
     168     72876568 :     if (gap >= 0) {
     169     72876568 :         result = MIN2(result, DIST2SPEED(gap));
     170              :         //if (result * TS > gap) {
     171              :         //    std::cout << "Maximum stop speed exceeded for gap=" << gap << " result=" << result << " veh=" << veh->getID() << " speed=" << speed << " t=" << SIMTIME << "\n";
     172              :         //}
     173              :     }
     174              : 
     175              :     return result;
     176              : }
     177              : 
     178              : 
     179              : /// @todo update interactionGap logic to IDM
     180              : double
     181            0 : MSCFModel_IDM::interactionGap(const MSVehicle* const veh, double vL) const {
     182              :     // Resolve the IDM equation to gap. Assume predecessor has
     183              :     // speed != 0 and that vsafe will be the current speed plus acceleration,
     184              :     // i.e that with this gap there will be no interaction.
     185            0 :     const double acc = myAccel * (1. - pow(veh->getSpeed() / veh->getLane()->getVehicleMaxSpeed(veh), myDelta));
     186            0 :     const double vNext = veh->getSpeed() + acc;
     187            0 :     const double gap = (vNext - vL) * (veh->getSpeed() + vL) / (2 * myDecel) + vL;
     188              : 
     189              :     // Don't allow timeHeadWay < deltaT situations.
     190            0 :     return MAX2(gap, SPEED2DIST(vNext));
     191              : }
     192              : 
     193              : double
     194    368675672 : MSCFModel_IDM::getSecureGap(const MSVehicle* const /*veh*/, const MSVehicle* const /*pred*/, const double speed, const double leaderSpeed, const double /*leaderMaxDecel*/) const {
     195    368675672 :     const double delta_v = speed - leaderSpeed;
     196    368675672 :     return MAX2(0.0, speed * myHeadwayTime + speed * delta_v / myTwoSqrtAccelDecel);
     197              : }
     198              : 
     199              : 
     200              : double
     201    345363847 : MSCFModel_IDM::_v(const MSVehicle* const veh, const double gap2pred, const double egoSpeed,
     202              :                   const double predSpeed, const double desSpeed, const bool respectMinGap) const {
     203              : // this is more or less based on http://www.vwi.tu-dresden.de/~treiber/MicroApplet/IDM.html
     204              : // and http://arxiv.org/abs/cond-mat/0304337
     205              : // we assume however constant speed for the leader
     206    345363847 :     double headwayTime = myHeadwayTime;
     207    345363847 :     if (myAdaptationFactor != 1.) {
     208              :         const VehicleVariables* vars = (VehicleVariables*)veh->getCarFollowVariables();
     209     42655249 :         headwayTime *= myAdaptationFactor + vars->levelOfService * (1. - myAdaptationFactor);
     210              :     }
     211              :     double newSpeed = egoSpeed;
     212              :     double gap = gap2pred;
     213    345363847 :     if (respectMinGap) {
     214              :         // gap2pred comes with minGap already subtracted so we need to add it here again
     215    214256464 :         gap += myType->getMinGap();
     216              :     }
     217              : #ifdef DEBUG_V
     218              :     if (DEBUG_COND) {
     219              :         std::cout << SIMTIME << " veh=" << veh->getID() << " gap2pred=" << gap2pred << " egoSpeed=" << egoSpeed << " predSpeed=" << predSpeed << " desSpeed=" << desSpeed << " rMG=" << respectMinGap << " hw=" << headwayTime << "\n";
     220              :     }
     221              : #endif
     222   1601436647 :     for (int i = 0; i < myIterations; i++) {
     223   1256072800 :         const double delta_v = newSpeed - predSpeed;
     224   1256072800 :         double s = MAX2(0., newSpeed * headwayTime + newSpeed * delta_v / myTwoSqrtAccelDecel);
     225   1256072800 :         if (respectMinGap) {
     226    776844329 :             s += myType->getMinGap();
     227              :         }
     228              :         gap = MAX2(NUMERICAL_EPS, gap); // avoid singularity
     229   1256072800 :         const double acc = myAccel * (1. - pow(newSpeed / MAX2(NUMERICAL_EPS, desSpeed), myDelta) - (s * s) / (gap * gap));
     230              : #ifdef DEBUG_V
     231              :         if (DEBUG_COND) {
     232              :             std::cout << "   i=" << i << " gap=" << gap << " t=" << myHeadwayTime << " t2=" << headwayTime << " s=" << s << " pow=" << pow(newSpeed / desSpeed, myDelta) << " gapDecel=" << (s * s) / (gap * gap) << " a=" << acc;
     233              :         }
     234              : #endif
     235   1256072800 :         newSpeed = MAX2(0.0, newSpeed + ACCEL2SPEED(acc) / myIterations);
     236              : #ifdef DEBUG_V
     237              :         if (DEBUG_COND) {
     238              :             std::cout << " v2=" << newSpeed << " gLC=" << MSGlobals::gComputeLC << "\n";
     239              :         }
     240              : #endif
     241              :         //TODO use more realistic position update which takes accelerated motion into account
     242   2018142556 :         gap -= MAX2(0., SPEED2DIST(newSpeed - predSpeed) / myIterations);
     243              :     }
     244    345363847 :     return MAX2(0., newSpeed);
     245              : }
     246              : 
     247              : 
     248              : MSCFModel*
     249            0 : MSCFModel_IDM::duplicate(const MSVehicleType* vtype) const {
     250            0 :     return new MSCFModel_IDM(vtype, myIDMM);
     251              : }
        

Generated by: LCOV version 2.0-1