Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSCFModel_Krauss.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/****************************************************************************/
22// Krauss car-following model, with acceleration decrease and faster start
23/****************************************************************************/
24#include <config.h>
25
26#include <microsim/MSVehicle.h>
27#include <microsim/MSLane.h>
28#include <microsim/MSGlobals.h>
29#include "MSCFModel_Krauss.h"
33
34
35
36// ===========================================================================
37// DEBUG constants
38// ===========================================================================
39//#define DEBUG_COND (true)
40#define DEBUG_COND (veh->isSelected())
41#define DEBUG_DRIVER_ERRORS
42
44 : accelDawdle(1e6), updateOffset(SIMSTEP % dawdleStep + DELTA_T) { }
45
46// ===========================================================================
47// method definitions
48// ===========================================================================
52 if (myDawdleStep % DELTA_T != 0) {
54 if (rem < DELTA_T / 2) {
55 myDawdleStep += -rem;
56 } else {
57 myDawdleStep += DELTA_T - rem;
58 }
59 WRITE_WARNINGF(TL("Rounding 'sigmaStep' to % for vType '%'"), STEPS2TIME(myDawdleStep), vtype->getID());
60
61 }
62}
63
64
66
67
68void
71 out.writeAttr(SUMO_ATTR_ID, "Krauss");
72 std::ostringstream internals;
73 internals << accelDawdle << " ";
74 internals << updateOffset;
75 out.writeAttr(SUMO_ATTR_STATE, internals.str());
76 out.closeTag();
77}
78
79
80void
82 bool ok = true;
83 const std::string cfmID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
84 if (cfmID != "Krauss") {
85 throw ProcessError(TLF("incompatible carFollowModel '%' when loading state for Krauss", cfmID));
86 }
87 std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
88 bis >> accelDawdle;
89 bis >> updateOffset;
90}
91
92
93double
94MSCFModel_Krauss::patchSpeedBeforeLC(const MSVehicle* veh, double vMin, double vMax) const {
95 const double sigma = (veh->passingMinor()
97 : myDawdle);
98 double vDawdle;
99 if (myDawdleStep > DELTA_T) {
101 if (SIMSTEP % myDawdleStep == vars->updateOffset) {
102 const double vD = MAX2(vMin, dawdle2(vMax, sigma, veh->getRNG()));
103 const double a1 = SPEED2ACCEL(vMax - veh->getSpeed());
104 const double a2 = SPEED2ACCEL(vD - vMax);
105 const double accelMax = (veh->getLane()->getVehicleMaxSpeed(veh) - veh->getSpeed()) / STEPS2TIME(myDawdleStep);
106 // avoid exceeding maxSpeed before the next sigmaStep
107 vars->accelDawdle = MIN2(a1, accelMax) + a2;
108 vDawdle = veh->getSpeed() + ACCEL2SPEED(vars->accelDawdle);
109 //std::cout << SIMTIME << " v=" << veh->getSpeed() << " updated vD=" << vD<< " a1=" << a1 << " a2=" << a2 << " aM=" << accelMax << " accelDawdle=" << vars->accelDawdle << " vDawdle=" << vDawdle << "\n";
110 } else {
111 const double safeAccel = SPEED2ACCEL(vMax - veh->getSpeed());
112 const double accel = MIN2(safeAccel, vars->accelDawdle);
113 vDawdle = MAX2(vMin, MIN2(vMax, veh->getSpeed() + ACCEL2SPEED(accel)));
114 //std::cout << SIMTIME << " v=" << veh->getSpeed() << " safeAccel=" << safeAccel << " accel=" << accel << " vDawdle=" << vDawdle << "\n";
115 }
116 } else {
117 vDawdle = MAX2(vMin, dawdle2(vMax, sigma, veh->getRNG()));
118 //const double accel1 = SPEED2ACCEL(vMax - veh->getSpeed());
119 //const double accel2 = SPEED2ACCEL(vDawdle - vMax);
120 //std::cout << SIMTIME << " v=" << veh->getSpeed() << " updated vDawdle=" << vDawdle << " a1=" << accel1 << " a2=" << accel2 << " accelDawdle=" << SPEED2ACCEL(vDawdle - veh->getSpeed()) << "\n";
121 }
122 return vDawdle;
123}
124
125
126double
127MSCFModel_Krauss::stopSpeed(const MSVehicle* const veh, const double speed, double gap, double decel, const CalcReason usage) const {
128 // NOTE: This allows return of smaller values than minNextSpeed().
129 // Only relevant for the ballistic update: We give the argument headway=veh->getActionStepLengthSecs(), to assure that
130 // the stopping position is approached with a uniform deceleration also for tau!=veh->getActionStepLengthSecs().
131 applyHeadwayPerceptionError(veh, speed, gap);
132 const bool relaxEmergency = usage != FUTURE; // do not relax insertionStopSpeed
133 return MIN2(maximumSafeStopSpeed(gap, decel, speed, false, veh->getActionStepLengthSecs(), relaxEmergency), maxNextSpeed(speed, veh));
134}
135
136
137double
138MSCFModel_Krauss::followSpeed(const MSVehicle* const veh, double speed, double gap, double predSpeed, double predMaxDecel, const MSVehicle* const pred, const CalcReason /*usage*/) const {
139 //gDebugFlag1 = DEBUG_COND;
140 applyHeadwayAndSpeedDifferencePerceptionErrors(veh, speed, gap, predSpeed, predMaxDecel, pred);
141 //gDebugFlag1 = DEBUG_COND; // enable for DEBUG_EMERGENCYDECEL
142 const double vsafe = maximumSafeFollowSpeed(gap, speed, predSpeed, predMaxDecel);
143 //gDebugFlag1 = false;
144 const double vmax = maxNextSpeed(speed, veh);
146 return MIN2(vsafe, vmax);
147 } else {
148 // ballistic
149 // XXX: the euler variant can break as strong as it wishes immediately! The ballistic cannot, refs. #2575.
150 return MAX2(MIN2(vsafe, vmax), minNextSpeedEmergency(speed));
151 }
152}
153
154double
155MSCFModel_Krauss::dawdle2(double speed, double sigma, SumoRNG* rng) const {
157 // in case of the ballistic update, negative speeds indicate
158 // a desired stop before the completion of the next timestep.
159 // We do not allow dawdling to overwrite this indication
160 if (speed < 0) {
161 return speed;
162 }
163 }
164 // generate random number out of [0,1)
165 const double random = RandHelper::rand(rng);
166 // Dawdle.
167 if (speed < myAccel) {
168 // we should not prevent vehicles from driving just due to dawdling
169 // if someone is starting, he should definitely start
170 // (but what about slow-to-start?)!!!
171 speed -= ACCEL2SPEED(sigma * speed * random);
172 } else {
173 speed -= ACCEL2SPEED(sigma * myAccel * random);
174 }
175 return MAX2(0., speed);
176}
177
178
181 return new MSCFModel_Krauss(vtype);
182}
183
184
185/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
SUMOTime DELTA_T
Definition SUMOTime.cpp:38
#define STEPS2TIME(x)
Definition SUMOTime.h:58
#define SIMSTEP
Definition SUMOTime.h:64
#define ACCEL2SPEED(x)
Definition SUMOTime.h:54
#define TS
Definition SUMOTime.h:45
#define TIME2STEPS(x)
Definition SUMOTime.h:60
#define SPEED2ACCEL(x)
Definition SUMOTime.h:56
@ SUMO_TAG_CFM_VARIABLES
@ SUMO_ATTR_SIGMA_STEP
@ SUMO_ATTR_JM_SIGMA_MINOR
@ SUMO_ATTR_ID
@ SUMO_ATTR_STATE
The state of a link.
T MIN2(T a, T b)
Definition StdDefs.h:80
T MAX2(T a, T b)
Definition StdDefs.h:86
SumoRNG * getRNG() const
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the vehicle variables from the given description.
double accelDawdle
the accleration due to dawdling
void saveState(OutputDevice &out, const MSCFModel &cfm) const
Saves the vehicle variables.
Krauss car-following model, with acceleration decrease and faster start.
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) this uses the maximumSafeFollowSpeed.
MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
double patchSpeedBeforeLC(const MSVehicle *veh, double vMin, double vMax) const
apply custom speed adaptations within the given speed bounds
double dawdle2(double speed, double sigma, SumoRNG *rng) const
Applies driver imperfection (dawdling / sigma)
~MSCFModel_Krauss()
Destructor.
MSCFModel_Krauss(const MSVehicleType *vtype)
Constructor.
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) this uses the m...
SUMOTime myDawdleStep
The vehicle's update period for dawdling.
The original Krauss (1998) car-following model and parameter.
virtual double vsafe(double gap, double predSpeed, double predMaxDecel) const
Returns the "safe" velocity.
double myDawdle
The vehicle's dawdle-parameter. 0 for no dawdling, 1 for max.
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 double minNextSpeedEmergency(double speed, const MSVehicle *const veh=0) const
Returns the minimum speed after emergency braking, given the current speed (depends on the numerical ...
virtual std::string getParameter(const MSVehicle *veh, const std::string &key) const
try to get the given parameter for this carFollowingModel
Definition MSCFModel.h:707
void applyHeadwayPerceptionError(const MSVehicle *const veh, double speed, double &gap) const
Overwrites gap by the perceived value obtained from the vehicle's driver state.
void applyHeadwayAndSpeedDifferencePerceptionErrors(const MSVehicle *const veh, double speed, double &gap, double &predSpeed, double predMaxDecel, const MSVehicle *const pred) const
Overwrites gap2pred and predSpeed by the perceived values obtained from the vehicle's driver state,...
double maximumSafeFollowSpeed(double gap, double egoSpeed, double predSpeed, double predMaxDecel, bool onInsertion=false) const
Returns the maximum safe velocity for following the given leader.
CalcReason
What the return value of stop/follow/free-Speed is used for.
Definition MSCFModel.h:95
@ FUTURE
the return value is used for calculating future speeds
Definition MSCFModel.h:99
double maximumSafeStopSpeed(double gap, double decel, double currentSpeed, bool onInsertion=false, double headway=-1, bool relaxEmergency=true) const
Returns the maximum next velocity for stopping within gap.
double myAccel
The vehicle's maximum acceleration [m/s^2].
Definition MSCFModel.h:759
static bool gSemiImplicitEulerUpdate
Definition MSGlobals.h:53
double getVehicleMaxSpeed(const SUMOTrafficObject *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
Definition MSLane.h:575
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
double getActionStepLengthSecs() const
Returns the vehicle's action step length in secs, i.e. the interval between two action points.
Definition MSVehicle.h:533
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition MSVehicle.h:581
bool passingMinor() const
decide whether the vehicle is passing a minor link or has comitted to do so
double getSpeed() const
Returns the vehicle's current speed.
Definition MSVehicle.h:490
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle's car following model variables.
Definition MSVehicle.h:994
The car-following model and parameter.
const std::string & getID() const
Returns the name of the vehicle type.
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.
double getJMParam(const SumoXMLAttr attr, const double defaultValue) const
Returns the named value from the map, or the default if it is not contained there.