SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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  SUMOReal dawdle, SUMOReal headwayTime,
48  SUMOReal tmp1, SUMOReal tmp2, SUMOReal tmp3, SUMOReal tmp4, SUMOReal tmp5)
49 // check whether setting these variables here with default values is ''good'' SUMO design
50 // SUMOReal tmp1=0.0, SUMOReal tmp2=5.0, SUMOReal tmp3=0.0, SUMOReal tmp4, SUMOReal tmp5)
51  : MSCFModel(vtype, accel, decel, headwayTime), myDawdle(dawdle), myTauDecel(decel * headwayTime),
52  myTmp1(tmp1), myTmp2(tmp2), myTmp3(tmp3), myTmp4(tmp4), myTmp5(tmp5) {
53  // the variable tmp1 is the acceleration delay time, e.g. two seconds (or something like this).
54  // for use in the upate process, a rule like if (v<myTmp1) vsafe = 0; is needed.
55  // To have this, we have to transform myTmp1 (which is a time) into an equivalent speed. This is done by the
56  // using the vsafe formula and computing:
57  // v(t=myTmp1) = -myTauDecel + sqrt(myTauDecel*myTauDecel + accel*(accel + decel)*t*t + accel*decel*t*TS);
58  SUMOReal t = myTmp1;
59  myS2Sspeed = -myTauDecel + sqrt(myTauDecel * myTauDecel + accel * (accel + decel) * t * t + accel * decel * t * TS);
60 #ifdef SmartSK_DEBUG
61  std::cout << "# s2s-speed: " << myS2Sspeed << std::endl;
62 #endif
63  if (myS2Sspeed > 5.0) {
64  myS2Sspeed = 5.0;
65  }
66 // SUMOReal maxDeltaGap = -0.5*ACCEL2DIST(myDecel + myAccel);
67  maxDeltaGap = -0.5 * (myDecel + myAccel) * TS * TS;
68 #ifdef SmartSK_DEBUG
69  std::cout << "# maxDeltaGap = " << maxDeltaGap << std::endl;
70 #endif
71  myTmp2 = TS / myTmp2;
72  myTmp3 = sqrt(TS) * myTmp3;
73 }
74 
75 
77 
78 
81  const SUMOReal oldV = veh->getSpeed(); // save old v for optional acceleration computation
82  const SUMOReal vSafe = MIN2(vPos, veh->processNextStop(vPos)); // process stops
83  // we need the acceleration for emission computation;
84  // in this case, we neglect dawdling, nonetheless, using
85  // vSafe does not incorporate speed reduction due to interaction
86  // on lane changing
87  const SUMOReal vMin = getSpeedAfterMaxDecel(oldV);
88  const SUMOReal vMax = MIN3(veh->getLane()->getVehicleMaxSpeed(veh), maxNextSpeed(oldV, veh), vSafe);
89 #ifdef SmartSK_DEBUG
90  if (vMin > vMax) {
91  WRITE_WARNING("Maximum speed of vehicle '" + veh->getID() + "' is lower than the minimum speed (min: " + toString(vMin) + ", max: " + toString(vMax) + ").");
92  }
93 #endif
94  updateMyHeadway(veh);
96 #ifdef SmartSK_DEBUG
97  if (vars->ggOld.size() > 1) {
98  std::cout << "# more than one entry in ggOld list. Speed is " << vPos << ", corresponding dist is " << vars->ggOld[(int) vPos] << "\n";
99  for (std::map<int, SUMOReal>::iterator I = vars->ggOld.begin(); I != vars->ggOld.end(); I++) {
100  std::cout << "# " << (*I).first << ' ' << (*I).second << std::endl;
101  }
102  }
103 #endif
104 
105  vars->gOld = vars->ggOld[(int) vPos];
106  vars->ggOld.clear();
107  return veh->getLaneChangeModel().patchSpeed(vMin, MAX2(vMin, dawdle(vMax)), vMax, *this);
108 }
109 
110 SUMOReal
111 MSCFModel_SmartSK::followSpeed(const MSVehicle* const veh, SUMOReal speed, SUMOReal gap, SUMOReal predSpeed, SUMOReal /*predMaxDecel*/) const {
113 
114 // if (((gap - vars->gOld) < maxDeltaGap) && (speed>=5.0) && gap>=5.0) {
115  if ((gap - vars->gOld) < maxDeltaGap) {
116  SUMOReal tTauTest = gap / speed;
117 // allow headway only to decrease only, never to increase. Increase is handled automatically by the headway dynamics in moveHelper()!!!
118  if ((tTauTest < vars->myHeadway) && (tTauTest > TS)) {
119  vars->myHeadway = tTauTest;
120  }
121  }
122 
123  SUMOReal vsafe = _vsafe(veh, gap, predSpeed);
124  if ((speed <= 0.0) && (vsafe < myS2Sspeed)) {
125  vsafe = 0;
126  }
127 
128  SUMOReal vNew = MAX2(getSpeedAfterMaxDecel(speed), MIN2(vsafe, maxNextSpeed(speed, veh)));
129  // there must be a better place to do the following assignment!!!
130  vars->gOld = gap;
131  vars->ggOld[(int)vNew] = gap;
132  return vNew;
133 }
134 
135 SUMOReal
136 MSCFModel_SmartSK::stopSpeed(const MSVehicle* const veh, const SUMOReal speed, SUMOReal gap) const {
138 
139 // if (((gap - vars->gOld) < maxDeltaGap) && (speed>=5.0) && gap>=5.0) {
140  if ((gap - vars->gOld) < maxDeltaGap) {
141  SUMOReal tTauTest = gap / speed;
142 // allow headway only to decrease only, never to increase. Increase is handled automatically by the headway dynamics in moveHelper()!!!
143  if ((tTauTest < vars->myHeadway) && (tTauTest > TS)) {
144  vars->myHeadway = tTauTest;
145  }
146  }
147 
148  return MAX2(getSpeedAfterMaxDecel(speed), MIN2(_vsafe(veh, gap, 0), maxNextSpeed(speed, veh)));
149 }
150 
151 
152 SUMOReal
154  return MAX2(SUMOReal(0), speed - ACCEL2SPEED(myDawdle * myAccel * RandHelper::rand()));
155 }
156 
157 
159 SUMOReal MSCFModel_SmartSK::_vsafe(const MSVehicle* const veh, SUMOReal gap, SUMOReal predSpeed) const {
160  if (predSpeed == 0 && gap < 0.01) {
161  return 0;
162  }
164  // this is the most obvious change to the normal SK: the model uses the variable vars->myHeadway instead of the constant
165  // myHeadwayTime as the "reaction time" tau
166  SUMOReal bTau = myDecel * (vars->myHeadway);
167  SUMOReal vsafe = (SUMOReal)(-1. * bTau
168  + sqrt(
169  bTau * bTau
170  + (predSpeed * predSpeed)
171  + (2. * myDecel * gap)
172  ));
173  assert(vsafe >= 0);
174  return vsafe;
175 }
176 
177 
178 MSCFModel*
182 }
SUMOReal getSpeedAfterMaxDecel(SUMOReal v) const
Returns the velocity after maximum deceleration.
Definition: MSCFModel.h:302
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:82
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:61
virtual SUMOReal maxNextSpeed(SUMOReal speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
Definition: MSCFModel.cpp:190
The car-following model abstraction.
Definition: MSCFModel.h:60
virtual SUMOReal followSpeed(const MSVehicle *const veh, SUMOReal speed, SUMOReal gap2pred, SUMOReal predSpeed, SUMOReal predMaxDecel) const
Computes the vehicle's safe speed (no dawdling)
SUMOReal myAccel
The vehicle's maximum acceleration [m/s^2].
Definition: MSCFModel.h:466
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
virtual SUMOReal stopSpeed(const MSVehicle *const veh, const SUMOReal speed, SUMOReal gap2pred) const
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling) ...
T MAX2(T a, T b)
Definition: StdDefs.h:75
~MSCFModel_SmartSK()
Destructor.
SUMOReal myHeadwayTime
The driver's desired time headway (aka reaction time tau) [s].
Definition: MSCFModel.h:472
#define TS
Definition: SUMOTime.h:52
SUMOReal processNextStop(SUMOReal currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1027
virtual void updateMyHeadway(const MSVehicle *const veh) const
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
virtual SUMOReal dawdle(SUMOReal speed) const
Applies driver imperfection (dawdling / sigma)
The car-following model and parameter.
Definition: MSVehicleType.h:74
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:2702
MSCFModel_SmartSK(const MSVehicleType *vtype, SUMOReal accel, SUMOReal decel, SUMOReal dawdle, SUMOReal headwayTime, SUMOReal tmp1, SUMOReal tmp2, SUMOReal tmp3, SUMOReal tmp4, SUMOReal tmp5)
Constructor.
SUMOReal myTmp1
temporary (testing) parameter
T MIN2(T a, T b)
Definition: StdDefs.h:69
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
SUMOReal myS2Sspeed
new variables needed in this model; myS2Sspeed is the speed below which the vehicle does not move whe...
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle's car following model variables.
Definition: MSVehicle.h:767
SUMOReal moveHelper(MSVehicle *const veh, SUMOReal vPos) const
Applies interaction with stops and lane changing model influences.
virtual SUMOReal patchSpeed(const SUMOReal min, const SUMOReal wanted, const SUMOReal max, const MSCFModel &cfModel)=0
Called to adapt the speed in order to allow a lane change.
SUMOReal getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:441
#define SUMOReal
Definition: config.h:214
T MIN3(T a, T b, T c)
Definition: StdDefs.h:82
SUMOReal myTauDecel
The precomputed value for myDecel*myTau.
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
Definition: MSLane.h:458
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:487
SUMOReal myDecel
The vehicle's maximum deceleration [m/s^2].
Definition: MSCFModel.h:469
virtual SUMOReal _vsafe(const MSVehicle *const veh, SUMOReal gap, SUMOReal predSpeed) const
Returns the "safe" velocity.
SUMOReal myDawdle
The vehicle's dawdle-parameter. 0 for no dawdling, 1 for max.
const std::string & getID() const
Returns the name of the vehicle.
virtual MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.