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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // The psycho-physical model of Wiedemann (10-Parameter version from 1999)
16 // references:
17 // code adapted from https://github.com/glgh/w99-demo
18 // (MIT License, Copyright (c) 2016 glgh)
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <cmath>
28 #include "MSCFModel_W99.h"
29 #include <microsim/MSVehicle.h>
30 #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 
75 void
76 MSCFModel_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 (speed > 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 
90 double
91 MSCFModel_W99::followSpeed(const MSVehicle* const veh, double speed, double gap2pred, double predSpeed, double /*predMaxDecel*/, const MSVehicle* const pred) 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  if (predSpeed > 0) {
115  if (dv < 0) {
116  if (dx > cc0) {
117  accel = MIN2(leaderAccel + dv * dv / (cc0 - dx), 0.0);
118  status = 0;
119  } else {
120  accel = MIN2(leaderAccel + 0.5 * (dv - sdvo), 0.0);
121  status = 1;
122  }
123  }
124  if (accel > -myCC7) {
125  accel = -myCC7;
126  status = 2;
127  } else {
128  accel = MAX2(accel, -10 + 0.5 * sqrt(speed));
129  status = 3;
130  }
131  }
132 
133  } else if (dv < sdvc && dx < sdxv) {
134  // 'Decelerate - Decrease Distance';
135  accel = 0.5 * dv * dv / (sdxc - dx - 0.1);
136  status = 4;
137  // deceleration is capped by myEmergencyDecel in MSCFModel::finalizeSpeed
138  } else if (dv < sdvo && dx < sdxo) {
139  // 'Accelerate/Decelerate - Keep Distance';
140  if (oldAccel <= 0) {
141  accel = MIN2(oldAccel, -myCC7);
142  status = 5;
143  } else {
144  accel = MAX2(oldAccel, myCC7);
145  status = 6;
146  // acceleration is capped at desired speed in MSCFModel::finalizeSpeed
147  }
148  } else {
149  // 'Accelerate/Relax - Increase/Keep Speed';
150  if (dx > sdxc) {
151  //if (follower.status === 'w') {
152  // accel = cc7;
153  //} else
154  {
155  const double accelMax = myCC8 + myCC9 * MIN2(speed, 80 / 3.6) + RandHelper::rand(veh->getRNG());
156  if (dx < sdxo) {
157  accel = MIN2(dv * dv / (sdxo - dx), accelMax);
158  status = 7;
159  } else {
160  accel = accelMax;
161  status = 8;
162  }
163  }
164  // acceleration is capped at desired speed in MSCFModel::finalizeSpeed
165  }
166  }
167  double vNew = speed + ACCEL2SPEED(accel);
168 #ifdef DEBUG_FOLLOW_SPEED
169  if (DEBUG_COND) {
170  std::cout << SIMTIME << " W99::fS veh=" << veh->getID() << " pred=" << Named::getIDSecure(pred)
171  << " v=" << speed << " pV=" << predSpeed << " g=" << gap2pred
172  << " dv=" << dv << " dx=" << dx
173  << " sdxc=" << sdxc << " sdxo=" << sdxo << " sdxv=" << sdxv << " sdv=" << sdv << " sdvo=" << sdvo << " sdvc=" << sdvc
174  << " st=" << status
175  << " a=" << accel << " V=" << vNew << "\n";
176  }
177 #else
178  UNUSED_PARAMETER(status);
179 #endif
181  vNew = MAX2(0.0, vNew);
182  }
183  return vNew;
184 }
185 
186 
187 
188 double
189 MSCFModel_W99::stopSpeed(const MSVehicle* const veh, const double speed, double gap) const {
190  // W99 doesn't stop on point so we add some slack
191  const double vFollow = followSpeed(veh, speed, gap + sqrt(gap) + 2, 0, 4.5, 0);
192  return MIN3(vFollow, maximumSafeStopSpeed(gap, speed, false, veh->getActionStepLengthSecs()), maxNextSpeed(speed, veh));
193 }
194 
195 
196 double
197 MSCFModel_W99::interactionGap(const MSVehicle* const, double vL) const {
198  UNUSED_PARAMETER(vL);
199  return INTERACTION_GAP;
200 }
201 
202 
203 MSCFModel*
205  return new MSCFModel_W99(vtype);
206 }
207 
208 
209 double
210 MSCFModel_W99::getSecureGap(const double speed, const double leaderSpeed, const double leaderMaxDecel) const {
211  double sdxc, sdxo, sdxv;
212  computeThresholds(speed, leaderSpeed, 0, 0.5, sdxc, sdxo, sdxv);
213  return MAX2(sdxv, MSCFModel::getSecureGap(speed, leaderSpeed, leaderMaxDecel));
214 }
215 
216 
MSVehicleType
The car-following model and parameter.
Definition: MSVehicleType.h:66
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:32
SUMO_ATTR_CF_W99_CC8
Definition: SUMOXMLDefinitions.h:847
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:74
SUMO_ATTR_CF_W99_CC2
Definition: SUMOXMLDefinitions.h:841
MSCFModel::maxNextSpeed
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
Definition: MSCFModel.cpp:239
ACCEL2SPEED
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:53
MSCFModel_W99::computeThresholds
void computeThresholds(double speed, double predSpeed, double leaderAccel, double rndVal, double &sdxc, double &sdxo, double &sdxv) const
Definition: MSCFModel_W99.cpp:76
MSCFModel_W99.h
MSCFModel_W99::myCC7
const double myCC7
Definition: MSCFModel_W99.h:145
SUMO_ATTR_CF_W99_CC1
Definition: SUMOXMLDefinitions.h:840
MSCFModel_W99::myCC9
const double myCC9
Definition: MSCFModel_W99.h:147
SUMO_ATTR_CF_W99_CC9
Definition: SUMOXMLDefinitions.h:848
MSVehicle.h
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:80
RandHelper::rand
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:60
SUMO_ATTR_CF_W99_CC7
Definition: SUMOXMLDefinitions.h:846
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:64
SUMO_ATTR_COLLISION_MINGAP_FACTOR
Definition: SUMOXMLDefinitions.h:461
MSCFModel_W99::myCC2
const double myCC2
Definition: MSCFModel_W99.h:140
MSVehicle::getActionStepLengthSecs
double getActionStepLengthSecs() const
Returns the vehicle's action step length in secs, i.e. the interval between two action points.
Definition: MSVehicle.h:513
SUMO_ATTR_CF_W99_CC5
Definition: SUMOXMLDefinitions.h:844
MSCFModel_W99::myCC3
const double myCC3
Definition: MSCFModel_W99.h:141
MSCFModel::maximumSafeStopSpeed
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:712
MSCFModel::myHeadwayTime
double myHeadwayTime
The driver's desired time headway (aka reaction time tau) [s].
Definition: MSCFModel.h:628
MSCFModel_W99::myCC1
const double myCC1
Definition: MSCFModel_W99.h:139
MSVehicleType::getMinGap
double getMinGap() const
Get the free space in front of vehicles of this class.
Definition: MSVehicleType.h:126
SUMOVTypeParameter::getCFParam
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.
Definition: SUMOVTypeParameter.cpp:406
INTERACTION_GAP
#define INTERACTION_GAP
Definition: MSCFModel_W99.cpp:36
MSCFModel_W99::duplicate
MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
Definition: MSCFModel_W99.cpp:204
MSCFModel_W99::MSCFModel_W99
MSCFModel_W99(const MSVehicleType *vtype)
Constructor.
Definition: MSCFModel_W99.cpp:53
MSCFModel_W99::myCC6
const double myCC6
Definition: MSCFModel_W99.h:144
MSCFModel::myCollisionMinGapFactor
double myCollisionMinGapFactor
The factor of minGap that must be maintained to avoid a collision event.
Definition: MSCFModel.h:625
MSCFModel::getSecureGap
virtual double getSecureGap(const double speed, const double leaderSpeed, const double leaderMaxDecel) const
Returns the minimum gap to reserve if the leader is braking at maximum (>=0)
Definition: MSCFModel.h:328
MSCFModel_W99::interactionGap
double interactionGap(const MSVehicle *const, double vL) const
Returns the maximum gap at which an interaction between both vehicles occurs.
Definition: MSCFModel_W99.cpp:197
MSVehicleType::getParameter
const SUMOVTypeParameter & getParameter() const
Definition: MSVehicleType.h:556
MSBaseVehicle::getID
const std::string & getID() const
Returns the name of the vehicle.
Definition: MSBaseVehicle.cpp:137
DEBUG_COND
#define DEBUG_COND
Definition: MSCFModel_W99.cpp:43
MSCFModel::myType
const MSVehicleType * myType
The type to which this model definition belongs to.
Definition: MSCFModel.h:613
MSVehicle::getAcceleration
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:494
MSCFModel
The car-following model abstraction.
Definition: MSCFModel.h:57
SUMO_ATTR_CF_W99_CC6
Definition: SUMOXMLDefinitions.h:845
config.h
Named::getIDSecure
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:70
RandHelper.h
MSCFModel_W99::getSecureGap
double getSecureGap(const double speed, const double leaderSpeed, const double leaderMaxDecel) const
Returns the minimum gap to reserve if the leader is braking at maximum (>=0)
Definition: MSCFModel_W99.cpp:210
MSCFModel_W99::followSpeed
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)
Definition: MSCFModel_W99.cpp:91
SUMO_ATTR_CF_W99_CC3
Definition: SUMOXMLDefinitions.h:842
MSCFModel::myAccel
double myAccel
The vehicle's maximum acceleration [m/s^2].
Definition: MSCFModel.h:616
MSLane.h
MSCFModel_W99::myCC5
const double myCC5
Definition: MSCFModel_W99.h:143
MSGlobals::gSemiImplicitEulerUpdate
static bool gSemiImplicitEulerUpdate
Definition: MSGlobals.h:56
MIN3
T MIN3(T a, T b, T c)
Definition: StdDefs.h:87
MSCFModel_W99::stopSpeed
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)
Definition: MSCFModel_W99.cpp:189
MSCFModel_W99::myCC8
const double myCC8
Definition: MSCFModel_W99.h:146
MSCFModel_W99::myCC4
const double myCC4
Definition: MSCFModel_W99.h:142
MSCFModel_W99::~MSCFModel_W99
~MSCFModel_W99()
Destructor.
Definition: MSCFModel_W99.cpp:72
SUMOXMLDefinitions.h
MSBaseVehicle::getRNG
std::mt19937 * getRNG() const
Definition: MSBaseVehicle.cpp:712
SUMO_ATTR_CF_W99_CC4
Definition: SUMOXMLDefinitions.h:843
MSVehicle
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80