Eclipse SUMO - Simulation of Urban MObility
MSCFModel_W99.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2011-2020 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>
31 
32 
33 // only used by DK2008
34 #define INTERACTION_GAP 150
35 
36 // ===========================================================================
37 // DEBUG constants
38 // ===========================================================================
39 #define DEBUG_FOLLOW_SPEED
40 //#define DEBUG_COND (veh->getID() == "flow.0")
41 #define DEBUG_COND (veh->isSelected())
42 
43 // ===========================================================================
44 // static members
45 // ===========================================================================
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
52  MSCFModel(vtype),
53  myCC1(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC1, 1.30)),
54  myCC2(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC2, 8.00)),
55  myCC3(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC3, -12.00)),
56  myCC4(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC4, -0.25)),
57  myCC5(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC5, 0.35)),
58  myCC6(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC6, 6.00)),
59  myCC7(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC7, 0.25)),
60  myCC8(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC8, 2.00)),
61  myCC9(vtype->getParameter().getCFParam(SUMO_ATTR_CF_W99_CC9, 1.50)) {
62  // translate some values to make them show up correctly in the gui
64  myAccel = myCC8;
65  // W99 does not drive very precise and may violate minGap on occasion
67 }
68 
69 
71 
72 
73 void
74 MSCFModel_W99::computeThresholds(double speed, double predSpeed, double leaderAccel, double rndVal,
75  double& sdxc, double& sdxo, double& sdxv) const {
76 
77  const double dv = predSpeed - speed;
78  sdxc = myType->getMinGap(); // cc0
79  if (predSpeed > 0) {
80  const double v_slower = (dv >= 0 || leaderAccel < 1) ? speed : predSpeed + dv * rndVal;
81  sdxc += myCC1 * MAX2(0.0, v_slower);
82  }
83  sdxo = sdxc + myCC2; //maximum following distance (upper limit of car-following process)
84  sdxv = sdxo + myCC3 * (dv - myCC4);
85 }
86 
87 
88 double
89 MSCFModel_W99::followSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double /*predMaxDecel*/, const MSVehicle* const pred) const {
90  const double dx = gap2pred + myType->getMinGap();
91  const double dv = predSpeed - speed;
92 
93  const double cc0 = myType->getMinGap();
94 
95  const double leaderAccel = pred != nullptr ? pred->getAcceleration() : 0;
96  const double oldAccel = veh->getAcceleration();
97 
98  const double rndVal = speed > 0 ? RandHelper::rand(veh->getRNG()) - 0.5 : 0.5;
99  double sdxc, sdxo, sdxv;
100  computeThresholds(speed, predSpeed, leaderAccel, rndVal, sdxc, sdxo, sdxv);
101 
102  const double sdv = myCC6 * dx * dx / 10000;
103  const double sdvc = speed > 0 ? myCC4 - sdv : 0; //minimum closing dv
104  const double sdvo = predSpeed > myCC5 ? sdv + myCC5 : sdv; //minimum opening dv
105 
106  double accel = 0; // resulting acceleration
107  int status = 0;
108 
109  if (dv < sdvo && dx <= sdxc) {
110  // 'Decelerate - Increase Distance';
111  accel = 0;
112  if (predSpeed > 0) {
113  if (dv < 0) {
114  if (dx > cc0) {
115  accel = MIN2(leaderAccel + dv * dv / (cc0 - dx), 0.0);
116  status = 0;
117  } else {
118  accel = MIN2(leaderAccel + 0.5 * (dv - sdvo), 0.0);
119  status = 1;
120  }
121  }
122  if (accel > -myCC7) {
123  accel = -myCC7;
124  status = 2;
125  } else {
126  accel = MAX2(accel, -10 + 0.5 * sqrt(speed));
127  status = 3;
128  }
129  }
130 
131  } else if (dv < sdvc && dx < sdxv) {
132  // 'Decelerate - Decrease Distance';
133  accel = 0.5 * dv * dv / (sdxc - dx - 0.1);
134  status = 4;
135  // deceleration is capped by myEmergencyDecel in MSCFModel::finalizeSpeed
136  } else if (dv < sdvo && dx < sdxo) {
137  // 'Accelerate/Decelerate - Keep Distance';
138  if (oldAccel <= 0) {
139  accel = MIN2(oldAccel, -myCC7);
140  status = 5;
141  } else {
142  accel = MAX2(oldAccel, myCC7);
143  status = 6;
144  // acceleration is capped at desired speed in MSCFModel::finalizeSpeed
145  }
146  } else {
147  // 'Accelerate/Relax - Increase/Keep Speed';
148  if (dx > sdxc) {
149  //if (follower.status === 'w') {
150  // accel = cc7;
151  //} else
152  {
153  const double accelMax = myCC8 + myCC9 * MIN2(speed, 80 / 3.6) + RandHelper::rand(veh->getRNG());
154  if (dx < sdxo) {
155  accel = MIN2(dv * dv / (sdxo - dx), accelMax);
156  status = 7;
157  } else {
158  accel = accelMax;
159  status = 8;
160  }
161  }
162  // acceleration is capped at desired speed in MSCFModel::finalizeSpeed
163  }
164  }
165  double vNew = speed + ACCEL2SPEED(accel);
166 #ifdef DEBUG_FOLLOW_SPEED
167  if (DEBUG_COND) {
168  std::cout << SIMTIME << " W99::fS veh=" << veh->getID() << " pred=" << Named::getIDSecure(pred)
169  << " v=" << speed << " pV=" << predSpeed << " g=" << gap2pred
170  << " dv=" << dv << " dx=" << dx
171  << " sdxc=" << sdxc << " sdxo=" << sdxo << " sdxv=" << sdxv << " sdv=" << sdv << " sdvo=" << sdvo << " sdvc=" << sdvc
172  << " st=" << status
173  << " a=" << accel << " V=" << vNew << "\n";
174  }
175 #else
176  UNUSED_PARAMETER(status);
177 #endif
179  vNew = MAX2(0.0, vNew);
180  }
181  return vNew;
182 }
183 
184 
185 
186 double
187 MSCFModel_W99::stopSpeed(const MSVehicle* const veh, const double speed, double gap) const {
188  // see reasoning in MSCFModel_Wiedemann::stopSpeed
189  return MIN2(maximumSafeStopSpeed(gap, speed, false, veh->getActionStepLengthSecs()), maxNextSpeed(speed, veh));
190 }
191 
192 
193 double
194 MSCFModel_W99::interactionGap(const MSVehicle* const, double vL) const {
195  UNUSED_PARAMETER(vL);
196  return INTERACTION_GAP;
197 }
198 
199 
200 MSCFModel*
202  return new MSCFModel_W99(vtype);
203 }
204 
#define INTERACTION_GAP
#define DEBUG_COND
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:49
#define SIMTIME
Definition: SUMOTime.h:60
@ 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
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
T MIN2(T a, T b)
Definition: StdDefs.h:73
T MAX2(T a, T b)
Definition: StdDefs.h:79
std::mt19937 * getRNG() const
const double myCC8
const double myCC7
const double myCC6
double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0) const
Computes the vehicle's safe speed (no dawdling)
double stopSpeed(const MSVehicle *const veh, const double speed, double gap) const
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling)
MSCFModel_W99(const MSVehicleType *vtype)
Constructor.
const double myCC9
const double myCC2
double interactionGap(const MSVehicle *const, double vL) const
Returns the maximum gap at which an interaction between both vehicles occurs.
const double myCC1
const double myCC5
const double myCC3
void computeThresholds(double speed, double predSpeed, double leaderAccel, double rndVal, double &sdxc, double &sdxo, double &sdxv) const
~MSCFModel_W99()
Destructor.
const double myCC4
MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
The car-following model abstraction.
Definition: MSCFModel.h:55
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
Definition: MSCFModel.cpp:237
double maximumSafeStopSpeed(double gap, double currentSpeed, bool onInsertion=false, double headway=-1) const
Returns the maximum next velocity for stopping within gap.
Definition: MSCFModel.cpp:710
double myCollisionMinGapFactor
The factor of minGap that must be maintained to avoid a collision event.
Definition: MSCFModel.h:625
double myAccel
The vehicle's maximum acceleration [m/s^2].
Definition: MSCFModel.h:616
const MSVehicleType * myType
The type to which this model definition belongs to.
Definition: MSCFModel.h:613
double myHeadwayTime
The driver's desired time headway (aka reaction time tau) [s].
Definition: MSCFModel.h:628
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:502
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:483
The car-following model and parameter.
Definition: MSVehicleType.h:62
double getMinGap() const
Get the free space in front of vehicles of this class.
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(std::mt19937 *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.h:51
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.