Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSCFModel_W99.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2011-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/****************************************************************************/
18// The psycho-physical model of Wiedemann (10-Parameter version from 1999)
19// references:
20// code adapted from https://github.com/glgh/w99-demo
21// (MIT License, Copyright (c) 2016 glgh)
22/****************************************************************************/
23#include <config.h>
24
25#include <cmath>
26#include "MSCFModel_W99.h"
27#include <microsim/MSVehicle.h>
28#include <microsim/MSLane.h>
33
34
35// only used by DK2008
36#define INTERACTION_GAP 150
37
38// ===========================================================================
39// DEBUG constants
40// ===========================================================================
41#define DEBUG_FOLLOW_SPEED
42//#define DEBUG_COND (veh->getID() == "flow.0")
43#define DEBUG_COND (veh->isSelected())
44
45// ===========================================================================
46// static members
47// ===========================================================================
48
49
50// ===========================================================================
51// method definitions
52// ===========================================================================
54 MSCFModel(vtype),
55 myCC1(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC1, 1.30)),
56 myCC2(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC2, 8.00)),
57 myCC3(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC3, -12.00)),
58 myCC4(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC4, -0.25)),
59 myCC5(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC5, 0.35)),
60 myCC6(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC6, 6.00)),
61 myCC7(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC7, 0.25)),
62 myCC8(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC8, 2.00)),
63 myCC9(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC9, 1.50)) {
64 // translate some values to make them show up correctly in the gui
66 myAccel = myCC8;
67 // W99 does not drive very precise and may violate minGap on occasion
69}
70
71
73
74
75void
76MSCFModel_W99::computeThresholds(double speed, double predSpeed, double leaderAccel, double rndVal,
77 double& sdxc, double& sdxo, double& sdxv) const {
78
79 const double dv = predSpeed - speed;
80 sdxc = myType->getMinGap(); // cc0
81 if (predSpeed > 0) {
82 const double v_slower = (dv >= 0 || leaderAccel < 1) ? speed : predSpeed + dv * rndVal;
83 sdxc += myCC1 * MAX2(0.0, v_slower);
84 }
85 sdxo = sdxc + myCC2; //maximum following distance (upper limit of car-following process)
86 sdxv = sdxo + myCC3 * (dv - myCC4);
87}
88
89
90double
91MSCFModel_W99::followSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double /*predMaxDecel*/, const MSVehicle* const pred, const CalcReason /*usage*/) const {
92 const double dx = gap2pred + myType->getMinGap();
93 const double dv = predSpeed - speed;
94
95 const double cc0 = myType->getMinGap();
96
97 const double leaderAccel = pred != nullptr ? pred->getAcceleration() : 0;
98 const double oldAccel = veh->getAcceleration();
99
100 const double rndVal = speed > 0 ? RandHelper::rand(veh->getRNG()) - 0.5 : 0.5;
101 double sdxc, sdxo, sdxv;
102 computeThresholds(speed, predSpeed, leaderAccel, rndVal, sdxc, sdxo, sdxv);
103
104 const double sdv = myCC6 * dx * dx / 10000;
105 const double sdvc = speed > 0 ? myCC4 - sdv : 0; //minimum closing dv
106 const double sdvo = predSpeed > myCC5 ? sdv + myCC5 : sdv; //minimum opening dv
107
108 double accel = 0; // resulting acceleration
109 int status = 0;
110
111 if (dv < sdvo && dx <= sdxc) {
112 // 'Decelerate - Increase Distance';
113 accel = 0;
114 // code in addtion to w99-demo to fix collision (#10472)
115 if (dx - SPEED2DIST(speed) < myType->getMinGap() * myCollisionMinGapFactor) {
116 // prevent crashin in the next step
117 accel = -SPEED2ACCEL(speed);
118 status = 9;
119 }
120 if (predSpeed > 0) {
121 if (dv < 0) {
122 if (dx > cc0) {
123 accel = MIN2(leaderAccel + dv * dv / (cc0 - dx), 0.0);
124 status = 0;
125 } else {
126 accel = MIN2(leaderAccel + 0.5 * (dv - sdvo), 0.0);
127 status = 1;
128 }
129 }
130 if (accel > -myCC7) {
131 accel = -myCC7;
132 status = 2;
133 } else {
134 accel = MAX2(accel, -10 + 0.5 * sqrt(speed));
135 status = 3;
136 }
137 }
138
139 } else if (dv < sdvc && dx < sdxv) {
140 // 'Decelerate - Decrease Distance';
141 accel = 0.5 * dv * dv / (sdxc - dx - 0.1);
142 status = 4;
143 // deceleration is capped by myEmergencyDecel in MSCFModel::finalizeSpeed
144 } else if (dv < sdvo && dx < sdxo) {
145 // 'Accelerate/Decelerate - Keep Distance';
146 if (oldAccel <= 0) {
147 accel = MIN2(oldAccel, -myCC7);
148 status = 5;
149 } else {
150 accel = MAX2(oldAccel, myCC7);
151 status = 6;
152 // acceleration is capped at desired speed in MSCFModel::finalizeSpeed
153 }
154 } else {
155 // 'Accelerate/Relax - Increase/Keep Speed';
156 if (dx > sdxc) {
157 //if (follower.status === 'w') {
158 // accel = cc7;
159 //} else
160 {
161 const double accelMax = myCC8 + myCC9 * MIN2(speed, 80 / 3.6) + RandHelper::rand(veh->getRNG());
162 if (dx < sdxo) {
163 accel = MIN2(dv * dv / (sdxo - dx), accelMax);
164 status = 7;
165 } else {
166 accel = accelMax;
167 status = 8;
168 }
169 }
170 // acceleration is capped at desired speed in MSCFModel::finalizeSpeed
171 }
172 }
173 double vNew = speed + ACCEL2SPEED(accel);
174#ifdef DEBUG_FOLLOW_SPEED
175 if (DEBUG_COND) {
176 std::cout << SIMTIME << " W99::fS veh=" << veh->getID() << " pred=" << Named::getIDSecure(pred)
177 << " v=" << speed << " pV=" << predSpeed << " g=" << gap2pred
178 << " dv=" << dv << " dx=" << dx
179 << " sdxc=" << sdxc << " sdxo=" << sdxo << " sdxv=" << sdxv << " sdv=" << sdv << " sdvo=" << sdvo << " sdvc=" << sdvc
180 << " st=" << status
181 << " a=" << accel << " V=" << vNew << "\n";
182 }
183#else
184 UNUSED_PARAMETER(status);
185#endif
187 vNew = MAX2(0.0, vNew);
188 }
189 return vNew;
190}
191
192
193
194double
195MSCFModel_W99::stopSpeed(const MSVehicle* const veh, const double speed, double gap, double decel, const CalcReason /*usage*/) const {
196 // see reasoning in MSCFModel_Wiedemann::stopSpeed
197 return MIN2(maximumSafeStopSpeed(gap, decel, speed, false, veh->getActionStepLengthSecs()), maxNextSpeed(speed, veh));
198}
199
200
201double
202MSCFModel_W99::interactionGap(const MSVehicle* const, double vL) const {
204 return INTERACTION_GAP;
205}
206
207
210 return new MSCFModel_W99(vtype);
211}
212
213
214std::string
215MSCFModel_W99::getParameter(const MSVehicle* veh, const std::string& key) const {
216 if (key == "cc0") {
217 return toString(veh->getVehicleType().getMinGap());
218 } else if (key == "cc1") {
219 return toString(myCC1);
220 } else if (key == "cc2") {
221 return toString(myCC2);
222 } else if (key == "cc3") {
223 return toString(myCC3);
224 } else if (key == "cc4") {
225 return toString(myCC4);
226 } else if (key == "cc5") {
227 return toString(myCC5);
228 } else if (key == "cc6") {
229 return toString(myCC6);
230 } else if (key == "cc7") {
231 return toString(myCC7);
232 } else if (key == "cc8") {
233 return toString(myCC8);
234 } else if (key == "cc9") {
235 return toString(myCC9);
236 } else {
237 throw InvalidArgument("Parameter '" + key + "' is not supported by carFollowModel");
238 }
239}
240
241
242void
243MSCFModel_W99::setParameter(MSVehicle* veh, const std::string& key, const std::string& value) const {
244 double val;
245 try {
246 val = StringUtils::toDouble(value);
247 } catch (NumberFormatException&) {
248 throw InvalidArgument("Invalid value '" + value + "' for parameter '" + key + "' for vehicle '" + veh->getID() + "'");
249 }
250 if (key != "cc0" && key != "cc1" && key != "cc2" && key != "cc3" && key != "cc4"
251 && key != "cc5" && key != "cc6" && key != "cc7" && key != "cc8" && key != "cc9") {
252 throw InvalidArgument("Setting parameter '" + key + "' is not supported by carFollowModel");
253 }
254 MSVehicleType& singularType = veh->getSingularType();
255 MSCFModel_W99& mod = static_cast<MSCFModel_W99&>(singularType.getCarFollowModel());
256 SUMOVTypeParameter::SubParams& cfParams = const_cast<SUMOVTypeParameter&>(singularType.getParameter()).cfParameter;
257 if (key == "cc0") {
258 singularType.setMinGap(val);
259 } else if (key == "cc1") {
260 mod.myCC1 = val;
261 mod.myHeadwayTime = val;
262 cfParams[SUMO_ATTR_CF_W99_CC1] = value;
263 } else if (key == "cc2") {
264 mod.myCC2 = val;
265 cfParams[SUMO_ATTR_CF_W99_CC2] = value;
266 } else if (key == "cc3") {
267 mod.myCC3 = val;
268 cfParams[SUMO_ATTR_CF_W99_CC3] = value;
269 } else if (key == "cc4") {
270 mod.myCC4 = val;
271 cfParams[SUMO_ATTR_CF_W99_CC4] = value;
272 } else if (key == "cc5") {
273 mod.myCC5 = val;
274 cfParams[SUMO_ATTR_CF_W99_CC5] = value;
275 } else if (key == "cc6") {
276 mod.myCC6 = val;
277 cfParams[SUMO_ATTR_CF_W99_CC6] = value;
278 } else if (key == "cc7") {
279 mod.myCC7 = val;
280 cfParams[SUMO_ATTR_CF_W99_CC7] = value;
281 } else if (key == "cc8") {
282 mod.myCC8 = val;
283 mod.myAccel = val;
284 cfParams[SUMO_ATTR_CF_W99_CC8] = value;
285 } else if (key == "cc9") {
286 mod.myCC9 = val;
287 cfParams[SUMO_ATTR_CF_W99_CC9] = value;
288 }
289}
#define INTERACTION_GAP
#define SPEED2DIST(x)
Definition SUMOTime.h:48
#define ACCEL2SPEED(x)
Definition SUMOTime.h:54
#define SIMTIME
Definition SUMOTime.h:70
#define SPEED2ACCEL(x)
Definition SUMOTime.h:56
@ SUMO_ATTR_CF_W99_CC9
@ SUMO_ATTR_CF_W99_CC5
@ SUMO_ATTR_CF_W99_CC8
@ SUMO_ATTR_CF_W99_CC3
@ SUMO_ATTR_COLLISION_MINGAP_FACTOR
@ SUMO_ATTR_CF_W99_CC2
@ SUMO_ATTR_CF_W99_CC4
@ SUMO_ATTR_CF_W99_CC6
@ SUMO_ATTR_CF_W99_CC7
@ SUMO_ATTR_CF_W99_CC1
T MIN2(T a, T b)
Definition StdDefs.h:80
T MAX2(T a, T b)
Definition StdDefs.h:86
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
MSVehicleType & getSingularType()
Replaces the current vehicle type with a new one used by this vehicle only.
SumoRNG * getRNG() const
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
The W99 Model car-following model.
MSCFModel_W99(const MSVehicleType *vtype)
Constructor.
std::string getParameter(const MSVehicle *veh, const std::string &key) const
try to get the given parameter for this carFollowingModel
double interactionGap(const MSVehicle *const, double vL) const
Returns the maximum gap at which an interaction between both vehicles occurs.
void setParameter(MSVehicle *veh, const std::string &key, const std::string &value) const
try to set the given parameter for this carFollowingModel
double stopSpeed(const MSVehicle *const veh, const double speed, double gap, double decel, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling)
void computeThresholds(double speed, double predSpeed, double leaderAccel, double rndVal, double &sdxc, double &sdxo, double &sdxv) const
~MSCFModel_W99()
Destructor.
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 * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
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.
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
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
const MSVehicleType * myType
The type to which this model definition belongs to.
Definition MSCFModel.h:756
double myHeadwayTime
The driver's desired time headway (aka reaction time tau) [s].
Definition MSCFModel.h:771
static bool gSemiImplicitEulerUpdate
Definition MSGlobals.h:53
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
double getAcceleration() const
Returns the vehicle's acceleration in m/s (this is computed as the last step's mean acceleration in c...
Definition MSVehicle.h:514
The car-following model and parameter.
double getMinGap() const
Get the free space in front of vehicles of this class.
const MSCFModel & getCarFollowModel() const
Returns the vehicle type's car following model definition (const version)
void setMinGap(const double &minGap)
Set a new value for this type's minimum gap.
const SUMOVTypeParameter & getParameter() const
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition Named.h:66
const std::string & getID() const
Returns the id.
Definition Named.h:73
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Structure representing possible vehicle parameter.
std::map< SumoXMLAttr, std::string > SubParams
sub-model parameters
double getCFParam(const SumoXMLAttr attr, const double defaultValue) const
Returns the named value from the map, or the default if it is not contained there.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
#define UNUSED_PARAMETER(x)
#define DEBUG_COND