SUMO - Simulation of Urban MObility
MSCFModel_SmartSK.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A smarter SK
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <map>
34 #include <microsim/MSVehicle.h>
35 #include <microsim/MSLane.h>
36 #include "MSCFModel_SmartSK.h"
39 
40 //#define SmartSK_DEBUG
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46  double decel, double emergencyDecel, double apparentDecel,
47  double dawdle, double headwayTime,
48  double tmp1, double tmp2, double tmp3, double tmp4, double tmp5) :
49 // check whether setting these variables here with default values is ''good'' SUMO design
50 // double tmp1=0.0, double tmp2=5.0, double tmp3=0.0, double tmp4, double tmp5)
51  MSCFModel(vtype, accel, decel, emergencyDecel, apparentDecel, headwayTime),
52  myDawdle(dawdle), myTauDecel(decel * headwayTime),
53  myTmp1(tmp1), myTmp2(tmp2), myTmp3(tmp3), myTmp4(tmp4), myTmp5(tmp5) {
54  // the variable tmp1 is the acceleration delay time, e.g. two seconds (or something like this).
55  // for use in the upate process, a rule like if (v<myTmp1) vsafe = 0; is needed.
56  // To have this, we have to transform myTmp1 (which is a time) into an equivalent speed. This is done by the
57  // using the vsafe formula and computing:
58  // v(t=myTmp1) = -myTauDecel + sqrt(myTauDecel*myTauDecel + accel*(accel + decel)*t*t + accel*decel*t*TS);
59  double t = myTmp1;
60  myS2Sspeed = -myTauDecel + sqrt(myTauDecel * myTauDecel + accel * (accel + decel) * t * t + accel * decel * t * TS);
61 #ifdef SmartSK_DEBUG
62  std::cout << "# s2s-speed: " << myS2Sspeed << std::endl;
63 #endif
64  if (myS2Sspeed > 5.0) {
65  myS2Sspeed = 5.0;
66  }
67 // double maxDeltaGap = -0.5*ACCEL2DIST(myDecel + myAccel);
68  maxDeltaGap = -0.5 * (myDecel + myAccel) * TS * TS;
69 #ifdef SmartSK_DEBUG
70  std::cout << "# maxDeltaGap = " << maxDeltaGap << std::endl;
71 #endif
72  myTmp2 = TS / myTmp2;
73  myTmp3 = sqrt(TS) * myTmp3;
74 }
75 
76 
78 
79 
80 double
81 MSCFModel_SmartSK::moveHelper(MSVehicle* const veh, double vPos) const {
82  const double oldV = veh->getSpeed(); // save old v for optional acceleration computation
83  const double vSafe = MIN2(vPos, veh->processNextStop(vPos)); // process stops
84  // we need the acceleration for emission computation;
85  // in this case, we neglect dawdling, nonetheless, using
86  // vSafe does not incorporate speed reduction due to interaction
87  // on lane changing
88  const double vMin = getSpeedAfterMaxDecel(oldV);
89  const double vMax = MIN3(veh->getLane()->getVehicleMaxSpeed(veh), maxNextSpeed(oldV, veh), vSafe);
90 #ifdef SmartSK_DEBUG
91  if (vMin > vMax) {
92  WRITE_WARNING("Maximum speed of vehicle '" + veh->getID() + "' is lower than the minimum speed (min: " + toString(vMin) + ", max: " + toString(vMax) + ").");
93  }
94 #endif
95  updateMyHeadway(veh);
97 #ifdef SmartSK_DEBUG
98  if (vars->ggOld.size() > 1) {
99  std::cout << "# more than one entry in ggOld list. Speed is " << vPos << ", corresponding dist is " << vars->ggOld[(int) vPos] << "\n";
100  for (std::map<int, double>::iterator I = vars->ggOld.begin(); I != vars->ggOld.end(); I++) {
101  std::cout << "# " << (*I).first << ' ' << (*I).second << std::endl;
102  }
103  }
104 #endif
105 
106  vars->gOld = vars->ggOld[(int) vPos];
107  vars->ggOld.clear();
108  return veh->getLaneChangeModel().patchSpeed(vMin, MAX2(vMin, dawdle(vMax)), vMax, *this);
109 }
110 
111 double
112 MSCFModel_SmartSK::followSpeed(const MSVehicle* const veh, double speed, double gap, double predSpeed, double /*predMaxDecel*/) const {
114 
115 // if (((gap - vars->gOld) < maxDeltaGap) && (speed>=5.0) && gap>=5.0) {
116  if ((gap - vars->gOld) < maxDeltaGap) {
117  double tTauTest = gap / speed;
118 // allow headway only to decrease only, never to increase. Increase is handled automatically by the headway dynamics in moveHelper()!!!
119  if ((tTauTest < vars->myHeadway) && (tTauTest > TS)) {
120  vars->myHeadway = tTauTest;
121  }
122  }
123 
124  double vsafe = _vsafe(veh, gap, predSpeed);
125  if ((speed <= 0.0) && (vsafe < myS2Sspeed)) {
126  vsafe = 0;
127  }
128 
129  double vNew = MAX2(getSpeedAfterMaxDecel(speed), MIN2(vsafe, maxNextSpeed(speed, veh)));
130  // there must be a better place to do the following assignment!!!
131  vars->gOld = gap;
132  vars->ggOld[(int)vNew] = gap;
133  return vNew;
134 }
135 
136 double
137 MSCFModel_SmartSK::stopSpeed(const MSVehicle* const veh, const double speed, double gap) const {
139 
140 // if (((gap - vars->gOld) < maxDeltaGap) && (speed>=5.0) && gap>=5.0) {
141  if ((gap - vars->gOld) < maxDeltaGap) {
142  double tTauTest = gap / speed;
143 // allow headway only to decrease only, never to increase. Increase is handled automatically by the headway dynamics in moveHelper()!!!
144  if ((tTauTest < vars->myHeadway) && (tTauTest > TS)) {
145  vars->myHeadway = tTauTest;
146  }
147  }
148 
149  return MAX2(getSpeedAfterMaxDecel(speed), MIN2(_vsafe(veh, gap, 0), maxNextSpeed(speed, veh)));
150 }
151 
152 
153 double
154 MSCFModel_SmartSK::dawdle(double speed) const {
155  return MAX2(0., speed - ACCEL2SPEED(myDawdle * myAccel * RandHelper::rand()));
156 }
157 
158 
160 double MSCFModel_SmartSK::_vsafe(const MSVehicle* const veh, double gap, double predSpeed) const {
161  if (predSpeed == 0 && gap < 0.01) {
162  return 0;
163  }
165  // this is the most obvious change to the normal SK: the model uses the variable vars->myHeadway instead of the constant
166  // myHeadwayTime as the "desired headway" tau
167  double bTau = myDecel * (vars->myHeadway);
168  double vsafe = (double)(-1. * bTau
169  + sqrt(
170  bTau * bTau
171  + (predSpeed * predSpeed)
172  + (2. * myDecel * gap)
173  ));
174  assert(vsafe >= 0);
175  return vsafe;
176 }
177 
178 
179 MSCFModel*
183 }
virtual double stopSpeed(const MSVehicle *const veh, const double speed, double gap2pred) const
Computes the vehicle&#39;s safe speed for approaching a non-moving obstacle (no dawdling) ...
double getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the lane&#39;s maximum speed, given a vehicle&#39;s speed limit adaptation.
Definition: MSLane.h:475
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle&#39;s car following model variables.
Definition: MSVehicle.h:891
double myApparentDecel
The vehicle&#39;s deceleration as expected by surrounding traffic [m/s^2].
Definition: MSCFModel.h:541
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:60
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:564
The car-following model abstraction.
Definition: MSCFModel.h:59
virtual MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:64
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
Definition: MSCFModel.cpp:204
double myAccel
The vehicle&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:534
double moveHelper(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences.
T MAX2(T a, T b)
Definition: StdDefs.h:73
~MSCFModel_SmartSK()
Destructor.
virtual double _vsafe(const MSVehicle *const veh, double gap, double predSpeed) const
Returns the "safe" velocity.
#define TS
Definition: SUMOTime.h:51
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
The car-following model and parameter.
Definition: MSVehicleType.h:72
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:3674
virtual double patchSpeed(const double min, const double wanted, const double max, const MSCFModel &cfModel)=0
Called to adapt the speed in order to allow a lane change. It uses information on LC-related desired ...
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
T MIN2(T a, T b)
Definition: StdDefs.h:67
virtual double getSpeedAfterMaxDecel(double v) const
Returns the velocity after maximum deceleration.
Definition: MSCFModel.h:318
double myDecel
The vehicle&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:537
virtual double dawdle(double speed) const
Applies driver imperfection (dawdling / sigma)
double processNextStop(double currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1376
MSCFModel_SmartSK(const MSVehicleType *vtype, double accel, double decel, double emergencyDecel, double apparentDecel, double dawdle, double headwayTime, double tmp1, double tmp2, double tmp3, double tmp4, double tmp5)
Constructor.
double myEmergencyDecel
The vehicle&#39;s maximum emergency deceleration [m/s^2].
Definition: MSCFModel.h:539
double myDawdle
The vehicle&#39;s dawdle-parameter. 0 for no dawdling, 1 for max.
double myTauDecel
The precomputed value for myDecel*myTau.
virtual void updateMyHeadway(const MSVehicle *const veh) const
T MIN3(T a, T b, T c)
Definition: StdDefs.h:80
double myHeadwayTime
The driver&#39;s desired time headway (aka reaction time tau) [s].
Definition: MSCFModel.h:544
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:482
virtual double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel) const
Computes the vehicle&#39;s safe speed (no dawdling)
double myS2Sspeed
new variables needed in this model; myS2Sspeed is the speed below which the vehicle does not move whe...
const std::string & getID() const
Returns the name of the vehicle.
double myTmp1
temporary (testing) parameter