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-2018 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 /****************************************************************************/
18 // A smarter SK
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <map>
28 #include <microsim/MSVehicle.h>
29 #include <microsim/MSLane.h>
30 #include "MSCFModel_SmartSK.h"
33 
34 //#define SmartSK_DEBUG
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
40 // check whether setting these variables here with default values is ''good'' SUMO design
41 // double tmp1=0.0, double tmp2=5.0, double tmp3=0.0, double tmp4, double tmp5)
42  MSCFModel(vtype),
43  myDawdle(vtype->getParameter().getCFParam(SUMO_ATTR_SIGMA, SUMOVTypeParameter::getDefaultImperfection(vtype->getParameter().vehicleClass))),
44  myTauDecel(myDecel * myHeadwayTime),
45  myTmp1(vtype->getParameter().getCFParam(SUMO_ATTR_TMP1, 1.0)),
46  myTmp2(vtype->getParameter().getCFParam(SUMO_ATTR_TMP2, 1.0)),
47  myTmp3(vtype->getParameter().getCFParam(SUMO_ATTR_TMP3, 1.0)),
48  myTmp4(vtype->getParameter().getCFParam(SUMO_ATTR_TMP4, 1.0)),
49  myTmp5(vtype->getParameter().getCFParam(SUMO_ATTR_TMP5, 1.0)) {
50  // the variable tmp1 is the acceleration delay time, e.g. two seconds (or something like this).
51  // for use in the upate process, a rule like if (v<myTmp1) vsafe = 0; is needed.
52  // To have this, we have to transform myTmp1 (which is a time) into an equivalent speed. This is done by the
53  // using the vsafe formula and computing:
54  // v(t=myTmp1) = -myTauDecel + sqrt(myTauDecel*myTauDecel + accel*(accel + decel)*t*t + accel*decel*t*TS);
55  double t = myTmp1;
56  myS2Sspeed = -myTauDecel + sqrt(myTauDecel * myTauDecel + myAccel * (myAccel + myDecel) * t * t + myAccel * myDecel * t * TS);
57 #ifdef SmartSK_DEBUG
58  std::cout << "# s2s-speed: " << myS2Sspeed << std::endl;
59 #endif
60  if (myS2Sspeed > 5.0) {
61  myS2Sspeed = 5.0;
62  }
63 // double maxDeltaGap = -0.5*ACCEL2DIST(myDecel + myAccel);
64  maxDeltaGap = -0.5 * (myDecel + myAccel) * TS * TS;
65 #ifdef SmartSK_DEBUG
66  std::cout << "# maxDeltaGap = " << maxDeltaGap << std::endl;
67 #endif
68  myTmp2 = TS / myTmp2;
69  myTmp3 = sqrt(TS) * myTmp3;
70 }
71 
72 
74 
75 
76 double
77 MSCFModel_SmartSK::finalizeSpeed(MSVehicle* const veh, double vPos) const {
78  const double vNext = MSCFModel::finalizeSpeed(veh, vPos);
79  updateMyHeadway(veh);
81 #ifdef SmartSK_DEBUG
82  if (vars->ggOld.size() > 1) {
83  std::cout << "# more than one entry in ggOld list. Speed is " << vPos << ", corresponding dist is " << vars->ggOld[(int) vPos] << "\n";
84  for (std::map<int, double>::iterator I = vars->ggOld.begin(); I != vars->ggOld.end(); I++) {
85  std::cout << "# " << (*I).first << ' ' << (*I).second << std::endl;
86  }
87  }
88 #endif
89  vars->gOld = vars->ggOld[(int) vPos];
90  vars->ggOld.clear();
91  return vNext;
92 }
93 
94 double
95 MSCFModel_SmartSK::followSpeed(const MSVehicle* const veh, double speed, double gap, double predSpeed, double /*predMaxDecel*/, const MSVehicle* const /*pred*/) const {
97 
98 // if (((gap - vars->gOld) < maxDeltaGap) && (speed>=5.0) && gap>=5.0) {
99  if ((gap - vars->gOld) < maxDeltaGap) {
100  double tTauTest = gap / speed;
101 // allow headway only to decrease only, never to increase. Increase is handled automatically by the headway dynamics in finalizeSpeed()!!!
102  if ((tTauTest < vars->myHeadway) && (tTauTest > TS)) {
103  vars->myHeadway = tTauTest;
104  }
105  }
106 
107  double vsafe = _vsafe(veh, gap, predSpeed);
108  if ((speed <= 0.0) && (vsafe < myS2Sspeed)) {
109  vsafe = 0;
110  }
111 
112  double vNew = MAX2(getSpeedAfterMaxDecel(speed), MIN2(vsafe, maxNextSpeed(speed, veh)));
113  // there must be a better place to do the following assignment!!!
114  vars->gOld = gap;
115  vars->ggOld[(int)vNew] = gap;
116  return vNew;
117 }
118 
119 double
120 MSCFModel_SmartSK::stopSpeed(const MSVehicle* const veh, const double speed, double gap) const {
122 
123 // if (((gap - vars->gOld) < maxDeltaGap) && (speed>=5.0) && gap>=5.0) {
124  if ((gap - vars->gOld) < maxDeltaGap) {
125  double tTauTest = gap / speed;
126 // allow headway only to decrease only, never to increase. Increase is handled automatically by the headway dynamics in finalizeSpeed()!!!
127  if ((tTauTest < vars->myHeadway) && (tTauTest > TS)) {
128  vars->myHeadway = tTauTest;
129  }
130  }
131 
132  return MAX2(getSpeedAfterMaxDecel(speed), MIN2(_vsafe(veh, gap, 0), maxNextSpeed(speed, veh)));
133 }
134 
135 
136 double
137 MSCFModel_SmartSK::dawdle(double speed) const {
138  return MAX2(0., speed - ACCEL2SPEED(myDawdle * myAccel * RandHelper::rand()));
139 }
140 
141 
143 double MSCFModel_SmartSK::_vsafe(const MSVehicle* const veh, double gap, double predSpeed) const {
144  if (predSpeed == 0 && gap < 0.01) {
145  return 0;
146  }
148  // this is the most obvious change to the normal SK: the model uses the variable vars->myHeadway instead of the constant
149  // myHeadwayTime as the "desired headway" tau
150  double bTau = myDecel * (vars->myHeadway);
151  double vsafe = (double)(-1. * bTau
152  + sqrt(
153  bTau * bTau
154  + (predSpeed * predSpeed)
155  + (2. * myDecel * gap)
156  ));
157  assert(vsafe >= 0);
158  return vsafe;
159 }
160 
161 
162 MSCFModel*
164  return new MSCFModel_SmartSK(vtype);
165 }
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) ...
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle&#39;s car following model variables.
Definition: MSVehicle.h:909
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:54
Structure representing possible vehicle parameter.
The car-following model abstraction.
Definition: MSCFModel.h:57
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:61
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
Definition: MSCFModel.cpp:239
double myAccel
The vehicle&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:590
T MAX2(T a, T b)
Definition: StdDefs.h:76
~MSCFModel_SmartSK()
Destructor.
virtual double _vsafe(const MSVehicle *const veh, double gap, double predSpeed) const
Returns the "safe" velocity.
#define TS
Definition: SUMOTime.h:45
virtual double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences. Called at most once per simulation...
Definition: MSCFModel.cpp:165
The car-following model and parameter.
Definition: MSVehicleType.h:66
MSCFModel_SmartSK(const MSVehicleType *vtype)
Constructor.
virtual double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0) const
Computes the vehicle&#39;s safe speed (no dawdling)
T MIN2(T a, T b)
Definition: StdDefs.h:70
virtual double getSpeedAfterMaxDecel(double v) const
Returns the velocity after maximum deceleration.
Definition: MSCFModel.h:341
double myDecel
The vehicle&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:593
virtual double dawdle(double speed) const
Applies driver imperfection (dawdling / sigma)
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
double myS2Sspeed
new variables needed in this model; myS2Sspeed is the speed below which the vehicle does not move whe...
double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences.
double myTmp1
temporary (testing) parameter