Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSCFModel_Kerner.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// car-following model by B. Kerner
21// Implementation based on "B. S. Kerner, S. L. Klenov, A. Brakemeier, Testbed for Wireless Vehicle Communication: a
22// Simulation Approach based on Three-Phase Traffic Theory"
23// https://arxiv.org/abs/0712.2711
24/****************************************************************************/
25#include <config.h>
26
27#include <microsim/MSVehicle.h>
28#include <microsim/MSLane.h>
29#include "MSCFModel_Kerner.h"
32
33
34// ===========================================================================
35// method definitions
36// ===========================================================================
38 MSCFModel(vtype),
39 myK(vtype->getParameter().getCFParam(SUMO_ATTR_K, 0.5)),
40 myPhi(vtype->getParameter().getCFParam(SUMO_ATTR_CF_KERNER_PHI, 5.0)),
42 // Kerner does not drive very precise and may violate minGap on occasion
44}
45
46
48
49
50void
53 out.writeAttr(SUMO_ATTR_ID, "BKerner");
54 std::ostringstream internals;
55 internals << rand;
56 out.writeAttr(SUMO_ATTR_STATE, internals.str());
57 out.closeTag();
58}
59
60
61void
63 bool ok = true;
64 const std::string cfmID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
65 if (cfmID != "BKerner") {
66 throw ProcessError(TLF("incompatible carFollowModel '%' when loading state for BKerner", cfmID));
67 }
68 std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
69 bis >> rand;
70}
71
72double
73MSCFModel_Kerner::finalizeSpeed(MSVehicle* const veh, double vPos) const {
74 const double vNext = MSCFModel::finalizeSpeed(veh, vPos);
76 vars->rand = RandHelper::rand(veh->getRNG());
77 return vNext;
78}
79
80
81double
82MSCFModel_Kerner::followSpeed(const MSVehicle* const veh, double speed, double gap, double predSpeed, double /*predMaxDecel*/, const MSVehicle* const /*pred*/, const CalcReason /*usage*/) const {
83 return MIN2(_v(veh, speed, maxNextSpeed(speed, veh), gap, predSpeed), maxNextSpeed(speed, veh));
84}
85
86
87double
88MSCFModel_Kerner::stopSpeed(const MSVehicle* const veh, const double speed, double gap, double /*decel*/, const CalcReason /*usage*/) const {
89 return MIN2(_v(veh, speed, maxNextSpeed(speed, veh), gap, 0), maxNextSpeed(speed, veh));
90}
91
92
97 ret->rand = RandHelper::rand();
98 return ret;
99}
100
101
102double
103MSCFModel_Kerner::_v(const MSVehicle* const veh, double speed, double vfree, double gap, double predSpeed) const {
104 if (predSpeed == 0 && gap < 0.01) {
105 return 0;
106 }
107 // !!! in the following, the prior step is not considered!!!
108 double G = MAX2((double) 0, (double)(SPEED2DIST(myK * speed) + myPhi / myAccel * speed * (speed - predSpeed)));
109 double vcond = gap > G ? speed + ACCEL2SPEED(myAccel) : speed + MAX2(ACCEL2SPEED(-myDecel), MIN2(ACCEL2SPEED(myAccel), predSpeed - speed));
110 double vsafe = (double)(-1. * myTauDecel + sqrt(myTauDecel * myTauDecel + (predSpeed * predSpeed) + (2. * myDecel * gap)));
112 double va = MAX2((double) 0, MIN3(vfree, vsafe, vcond)) + vars->rand;
113 //std::cout << SIMTIME << " veh=" << veh->getID() << " speed=" << speed << " gap=" << gap << " G=" << G << " predSpeed=" << predSpeed << " vfree=" << vfree << " vsafe=" << vsafe << " vcond=" << vcond << " rand=" << vars->rand << "\n";
114 double v = MAX2((double) 0, MIN4(vfree, va, speed + ACCEL2SPEED(myAccel), vsafe));
115 return v;
116}
117
118
121 return new MSCFModel_Kerner(vtype);
122}
#define TLF(string,...)
Definition MsgHandler.h:306
#define SPEED2DIST(x)
Definition SUMOTime.h:48
#define ACCEL2SPEED(x)
Definition SUMOTime.h:54
@ SUMO_TAG_CFM_VARIABLES
@ SUMO_ATTR_CF_KERNER_PHI
@ SUMO_ATTR_COLLISION_MINGAP_FACTOR
@ SUMO_ATTR_ID
@ SUMO_ATTR_K
@ SUMO_ATTR_STATE
The state of a link.
T MIN4(T a, T b, T c, T d)
Definition StdDefs.h:107
T MIN3(T a, T b, T c)
Definition StdDefs.h:93
T MIN2(T a, T b)
Definition StdDefs.h:80
T MAX2(T a, T b)
Definition StdDefs.h:86
SumoRNG * getRNG() const
void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the vehicle variables from the given description.
void saveState(OutputDevice &out, const MSCFModel &cfm) const
Saves the vehicle variables.
MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
~MSCFModel_Kerner()
Destructor.
double myTauDecel
The precomputed value for myDecel*myTau.
double myPhi
Kerner's phi.
double _v(const MSVehicle *const veh, double speed, double vfree, double gap, double predSpeed) const
Returns the "safe" velocity.
double stopSpeed(const MSVehicle *const veh, const double speed, double gap2pred, double decel, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling).
double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed (no dawdling).
MSCFModel::VehicleVariables * createVehicleVariables() const
Returns model specific values which are stored inside a vehicle and must be used with casting.
double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences.
MSCFModel_Kerner(const MSVehicleType *vtype)
Constructor.
The car-following model abstraction.
Definition MSCFModel.h:59
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
virtual std::string getParameter(const MSVehicle *veh, const std::string &key) const
try to get the given parameter for this carFollowingModel
Definition MSCFModel.h:707
virtual double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences. Called at most once per simulation...
CalcReason
What the return value of stop/follow/free-Speed is used for.
Definition MSCFModel.h:95
double myCollisionMinGapFactor
The factor of minGap that must be maintained to avoid a collision event.
Definition MSCFModel.h:768
MSCFModel(const MSVehicleType *vtype)
Constructor.
Definition MSCFModel.cpp:55
double myDecel
The vehicle's maximum deceleration [m/s^2].
Definition MSCFModel.h:762
double myAccel
The vehicle's maximum acceleration [m/s^2].
Definition MSCFModel.h:759
double myHeadwayTime
The driver's desired time headway (aka reaction time tau) [s].
Definition MSCFModel.h:771
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle's car following model variables.
Definition MSVehicle.h:994
The car-following model and parameter.
const SUMOVTypeParameter & getParameter() const
Static storage of an output device and its base (abstract) implementation.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false)
writes a named attribute
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1).
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.