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