SUMO - Simulation of Urban MObility
MSCFModel_SmartSK.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A smarter SK
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2012-2017 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <map>
35 #include <microsim/MSVehicle.h>
36 #include <microsim/MSLane.h>
37 #include "MSCFModel_SmartSK.h"
40 
41 //#define SmartSK_DEBUG
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
47  double decel, double emergencyDecel, double apparentDecel,
48  double dawdle, double headwayTime,
49  double tmp1, double tmp2, double tmp3, double tmp4, double tmp5) :
50 // check whether setting these variables here with default values is ''good'' SUMO design
51 // double tmp1=0.0, double tmp2=5.0, double tmp3=0.0, double tmp4, double tmp5)
52  MSCFModel(vtype, accel, decel, emergencyDecel, apparentDecel, headwayTime),
53  myDawdle(dawdle), myTauDecel(decel * headwayTime),
54  myTmp1(tmp1), myTmp2(tmp2), myTmp3(tmp3), myTmp4(tmp4), myTmp5(tmp5) {
55  // the variable tmp1 is the acceleration delay time, e.g. two seconds (or something like this).
56  // for use in the upate process, a rule like if (v<myTmp1) vsafe = 0; is needed.
57  // To have this, we have to transform myTmp1 (which is a time) into an equivalent speed. This is done by the
58  // using the vsafe formula and computing:
59  // v(t=myTmp1) = -myTauDecel + sqrt(myTauDecel*myTauDecel + accel*(accel + decel)*t*t + accel*decel*t*TS);
60  double t = myTmp1;
61  myS2Sspeed = -myTauDecel + sqrt(myTauDecel * myTauDecel + accel * (accel + decel) * t * t + accel * decel * t * TS);
62 #ifdef SmartSK_DEBUG
63  std::cout << "# s2s-speed: " << myS2Sspeed << std::endl;
64 #endif
65  if (myS2Sspeed > 5.0) {
66  myS2Sspeed = 5.0;
67  }
68 // double maxDeltaGap = -0.5*ACCEL2DIST(myDecel + myAccel);
69  maxDeltaGap = -0.5 * (myDecel + myAccel) * TS * TS;
70 #ifdef SmartSK_DEBUG
71  std::cout << "# maxDeltaGap = " << maxDeltaGap << std::endl;
72 #endif
73  myTmp2 = TS / myTmp2;
74  myTmp3 = sqrt(TS) * myTmp3;
75 }
76 
77 
79 
80 
81 double
82 MSCFModel_SmartSK::moveHelper(MSVehicle* const veh, double vPos) const {
83  const double oldV = veh->getSpeed(); // save old v for optional acceleration computation
84  const double vSafe = MIN2(vPos, veh->processNextStop(vPos)); // process stops
85  // we need the acceleration for emission computation;
86  // in this case, we neglect dawdling, nonetheless, using
87  // vSafe does not incorporate speed reduction due to interaction
88  // on lane changing
89  const double vMin = getSpeedAfterMaxDecel(oldV);
90  const double vMax = MIN3(veh->getLane()->getVehicleMaxSpeed(veh), maxNextSpeed(oldV, veh), vSafe);
91 #ifdef SmartSK_DEBUG
92  if (vMin > vMax) {
93  WRITE_WARNING("Maximum speed of vehicle '" + veh->getID() + "' is lower than the minimum speed (min: " + toString(vMin) + ", max: " + toString(vMax) + ").");
94  }
95 #endif
96  updateMyHeadway(veh);
98 #ifdef SmartSK_DEBUG
99  if (vars->ggOld.size() > 1) {
100  std::cout << "# more than one entry in ggOld list. Speed is " << vPos << ", corresponding dist is " << vars->ggOld[(int) vPos] << "\n";
101  for (std::map<int, double>::iterator I = vars->ggOld.begin(); I != vars->ggOld.end(); I++) {
102  std::cout << "# " << (*I).first << ' ' << (*I).second << std::endl;
103  }
104  }
105 #endif
106 
107  vars->gOld = vars->ggOld[(int) vPos];
108  vars->ggOld.clear();
109  return veh->getLaneChangeModel().patchSpeed(vMin, MAX2(vMin, dawdle(vMax)), vMax, *this);
110 }
111 
112 double
113 MSCFModel_SmartSK::followSpeed(const MSVehicle* const veh, double speed, double gap, double predSpeed, double /*predMaxDecel*/) const {
115 
116 // if (((gap - vars->gOld) < maxDeltaGap) && (speed>=5.0) && gap>=5.0) {
117  if ((gap - vars->gOld) < maxDeltaGap) {
118  double tTauTest = gap / speed;
119 // allow headway only to decrease only, never to increase. Increase is handled automatically by the headway dynamics in moveHelper()!!!
120  if ((tTauTest < vars->myHeadway) && (tTauTest > TS)) {
121  vars->myHeadway = tTauTest;
122  }
123  }
124 
125  double vsafe = _vsafe(veh, gap, predSpeed);
126  if ((speed <= 0.0) && (vsafe < myS2Sspeed)) {
127  vsafe = 0;
128  }
129 
130  double vNew = MAX2(getSpeedAfterMaxDecel(speed), MIN2(vsafe, maxNextSpeed(speed, veh)));
131  // there must be a better place to do the following assignment!!!
132  vars->gOld = gap;
133  vars->ggOld[(int)vNew] = gap;
134  return vNew;
135 }
136 
137 double
138 MSCFModel_SmartSK::stopSpeed(const MSVehicle* const veh, const double speed, double gap) const {
140 
141 // if (((gap - vars->gOld) < maxDeltaGap) && (speed>=5.0) && gap>=5.0) {
142  if ((gap - vars->gOld) < maxDeltaGap) {
143  double tTauTest = gap / speed;
144 // allow headway only to decrease only, never to increase. Increase is handled automatically by the headway dynamics in moveHelper()!!!
145  if ((tTauTest < vars->myHeadway) && (tTauTest > TS)) {
146  vars->myHeadway = tTauTest;
147  }
148  }
149 
150  return MAX2(getSpeedAfterMaxDecel(speed), MIN2(_vsafe(veh, gap, 0), maxNextSpeed(speed, veh)));
151 }
152 
153 
154 double
155 MSCFModel_SmartSK::dawdle(double speed) const {
156  return MAX2(0., speed - ACCEL2SPEED(myDawdle * myAccel * RandHelper::rand()));
157 }
158 
159 
161 double MSCFModel_SmartSK::_vsafe(const MSVehicle* const veh, double gap, double predSpeed) const {
162  if (predSpeed == 0 && gap < 0.01) {
163  return 0;
164  }
166  // this is the most obvious change to the normal SK: the model uses the variable vars->myHeadway instead of the constant
167  // myHeadwayTime as the "desired headway" tau
168  double bTau = myDecel * (vars->myHeadway);
169  double vsafe = (double)(-1. * bTau
170  + sqrt(
171  bTau * bTau
172  + (predSpeed * predSpeed)
173  + (2. * myDecel * gap)
174  ));
175  assert(vsafe >= 0);
176  return vsafe;
177 }
178 
179 
180 MSCFModel*
184 }
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
virtual double getSpeedAfterMaxDecel(double v) const
Returns the velocity after maximum deceleration.
Definition: MSCFModel.h:331
double myApparentDecel
The vehicle&#39;s deceleration as expected by surrounding traffic [m/s^2].
Definition: MSCFModel.h:531
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:61
The car-following model abstraction.
Definition: MSCFModel.h:60
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:65
double myAccel
The vehicle&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:524
T MAX2(T a, T b)
Definition: StdDefs.h:74
~MSCFModel_SmartSK()
Destructor.
#define TS
Definition: SUMOTime.h:52
virtual void updateMyHeadway(const MSVehicle *const veh) const
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The car-following model and parameter.
Definition: MSVehicleType.h:73
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:3144
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.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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)
T MIN2(T a, T b)
Definition: StdDefs.h:68
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 myDecel
The vehicle&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:527
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle&#39;s car following model variables.
Definition: MSVehicle.h:805
double moveHelper(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences.
virtual double _vsafe(const MSVehicle *const veh, double gap, double predSpeed) const
Returns the "safe" velocity.
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:1284
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
Definition: MSCFModel.cpp:200
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:529
double myDawdle
The vehicle&#39;s dawdle-parameter. 0 for no dawdling, 1 for max.
double myTauDecel
The precomputed value for myDecel*myTau.
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:462
T MIN3(T a, T b, T c)
Definition: StdDefs.h:81
double myHeadwayTime
The driver&#39;s desired time headway (aka reaction time tau) [s].
Definition: MSCFModel.h:534
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:499
double myS2Sspeed
new variables needed in this model; myS2Sspeed is the speed below which the vehicle does not move whe...
double myTmp1
temporary (testing) parameter
const std::string & getID() const
Returns the name of the vehicle.
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:443
virtual MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.