SUMO - Simulation of Urban MObility
MSCFModel.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // The car-following model abstraction
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <math.h>
36 #include <microsim/MSVehicleType.h>
37 #include <microsim/MSVehicle.h>
38 #include <microsim/MSLane.h>
40 #include "MSCFModel.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 MSCFModel::MSCFModel(const MSVehicleType* vtype, const SUMOReal accel,
47  const SUMOReal decel, const SUMOReal headwayTime)
48  : myType(vtype), myAccel(accel), myDecel(decel), myHeadwayTime(headwayTime) {
49 }
50 
51 
53 
54 
56 
57 
59 MSCFModel::moveHelper(MSVehicle* const veh, SUMOReal vPos) const {
60  const SUMOReal oldV = veh->getSpeed(); // save old v for optional acceleration computation
61  const SUMOReal vSafe = MIN2(vPos, veh->processNextStop(vPos)); // process stops
62  // we need the acceleration for emission computation;
63  // in this case, we neglect dawdling, nonetheless, using
64  // vSafe does not incorporate speed reduction due to interaction
65  // on lane changing
66  const SUMOReal vMax = MIN3(veh->getLane()->getVehicleMaxSpeed(veh), maxNextSpeed(oldV, veh), vSafe);
67  // we cannot rely on never braking harder than maxDecel because TraCI or strange cf models may decide to do so
68  const SUMOReal vMin = MIN2(getSpeedAfterMaxDecel(oldV), vMax);
69  return veh->getLaneChangeModel().patchSpeed(vMin, vMax, vMax, *this);
70 }
71 
72 
74 MSCFModel::interactionGap(const MSVehicle* const veh, SUMOReal vL) const {
75  // Resolve the vsafe equation to gap. Assume predecessor has
76  // speed != 0 and that vsafe will be the current speed plus acceleration,
77  // i.e that with this gap there will be no interaction.
78  const SUMOReal vNext = MIN2(maxNextSpeed(veh->getSpeed(), veh), veh->getLane()->getVehicleMaxSpeed(veh));
79  const SUMOReal gap = (vNext - vL) *
80  ((veh->getSpeed() + vL) / (2.*myDecel) + myHeadwayTime) +
81  vL * myHeadwayTime;
82 
83  // Don't allow timeHeadWay < deltaT situations.
84  return MAX2(gap, SPEED2DIST(vNext));
85 }
86 
87 
89 MSCFModel::maxNextSpeed(SUMOReal speed, const MSVehicle* const /*veh*/) const {
90  return MIN2(speed + (SUMOReal) ACCEL2SPEED(getMaxAccel()), myType->getMaxSpeed());
91 }
92 
93 
95 MSCFModel::freeSpeed(const MSVehicle* const /* veh */, SUMOReal /* speed */, SUMOReal seen, SUMOReal maxSpeed, const bool onInsertion) const {
96  return freeSpeed(myDecel, seen, maxSpeed, onInsertion);
97 }
98 
99 
100 SUMOReal
101 MSCFModel::insertionFollowSpeed(const MSVehicle* const, SUMOReal, SUMOReal gap2pred, SUMOReal predSpeed, SUMOReal predMaxDecel) const {
102  return maximumSafeFollowSpeed(gap2pred, predSpeed, predMaxDecel);
103 }
104 
105 
106 
107 SUMOReal
109  gap -= NUMERICAL_EPS; // lots of code relies on some slack
110  if (gap <= 0) {
111  return 0;
112  } else if (gap <= ACCEL2SPEED(myDecel)) {
113  // workaround for #2310
114  return MIN2(ACCEL2SPEED(myDecel), DIST2SPEED(gap));
115  }
116  const SUMOReal g = gap;
117  const SUMOReal b = ACCEL2SPEED(myDecel);
118  const SUMOReal t = myHeadwayTime;
119  const SUMOReal s = TS;
120  // h = the distance that would be covered if it were possible to stop
121  // exactly after gap and decelerate with b every simulation step
122  // h = 0.5 * n * (n-1) * b * s + n * b * t (solve for n)
123  //n = ((1.0/2.0) - ((t + (pow(((s*s) + (4.0*((s*((2.0*h/b) - t)) + (t*t)))), (1.0/2.0))*sign/2.0))/s));
124  const SUMOReal n = floor(.5 - ((t + (sqrt(((s * s) + (4.0 * ((s * (2.0 * g / b - t)) + (t * t))))) * -0.5)) / s));
125  const SUMOReal h = 0.5 * n * (n - 1) * b * s + n * b * t;
126  assert(h <= g + NUMERICAL_EPS);
127  // compute the additional speed that must be used during deceleration to fix
128  // the discrepancy between g and h
129  const SUMOReal r = (g - h) / (n * s + t);
130  const SUMOReal x = n * b + r;
131  assert(x >= 0);
132  return x;
133 }
134 
135 
137 SUMOReal
138 MSCFModel::maximumSafeFollowSpeed(SUMOReal gap, SUMOReal predSpeed, SUMOReal predMaxDecel) const {
139  // the speed is safe if allows the ego vehicle to come to a stop behind the leader even if
140  // the leaders starts braking hard until stopped
141  // unfortunately it is not sufficent to compare stopping distances if the follower can brake harder than the leader
142  // (the trajectories might intersect before both vehicles are stopped even if the follower has a shorter stopping distance than the leader)
143  // To make things safe, we ensure that the leaders brake distance is computed with an deceleration that is at least as high as the follower's.
144  // @todo: this is a conservative estimate for safe speed which could be increased
145  const SUMOReal x = maximumSafeStopSpeed(gap + brakeGap(predSpeed, MAX2(myDecel, predMaxDecel), 0));
146  assert(x >= 0);
147  assert(!ISNAN(x));
148  return x;
149 }
150 
151 
152 /****************************************************************************/
#define DIST2SPEED(x)
Definition: SUMOTime.h:57
SUMOReal getSpeedAfterMaxDecel(SUMOReal v) const
Returns the velocity after maximum deceleration.
Definition: MSCFModel.h:285
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:81
SUMOReal getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
const MSVehicleType * myType
The type to which this model definition belongs to.
Definition: MSCFModel.h:344
#define SPEED2DIST(x)
Definition: SUMOTime.h:55
#define ACCEL2SPEED(x)
Definition: SUMOTime.h:61
virtual SUMOReal insertionFollowSpeed(const MSVehicle *const veh, SUMOReal speed, SUMOReal gap2pred, SUMOReal predSpeed, SUMOReal predMaxDecel) const
Computes the vehicle&#39;s safe speed (no dawdling) This method is used during the insertion stage...
Definition: MSCFModel.cpp:101
virtual SUMOReal maxNextSpeed(SUMOReal speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
Definition: MSCFModel.cpp:89
T MAX2(T a, T b)
Definition: StdDefs.h:75
MSCFModel(const MSVehicleType *vtype, SUMOReal accel, SUMOReal decel, SUMOReal headwayTime)
Constructor.
Definition: MSCFModel.cpp:46
SUMOReal myHeadwayTime
The driver&#39;s desired time headway (aka reaction time tau) [s].
Definition: MSCFModel.h:353
#define TS
Definition: SUMOTime.h:52
SUMOReal processNextStop(SUMOReal currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1012
The car-following model and parameter.
Definition: MSVehicleType.h:74
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:2462
SUMOReal maximumSafeStopSpeed(SUMOReal gap) const
Returns the maximum velocity for stopping within gap This depends stronlgy on the position update mod...
Definition: MSCFModel.cpp:108
SUMOReal brakeGap(const SUMOReal speed) const
Returns the distance the vehicle needs to halt including driver&#39;s reaction time.
Definition: MSCFModel.h:234
virtual SUMOReal interactionGap(const MSVehicle *const veh, SUMOReal vL) const
Returns the maximum gap at which an interaction between both vehicles occurs.
Definition: MSCFModel.cpp:74
T MIN2(T a, T b)
Definition: StdDefs.h:69
virtual SUMOReal freeSpeed(const MSVehicle *const veh, SUMOReal speed, SUMOReal seen, SUMOReal maxSpeed, const bool onInsertion=false) const
Computes the vehicle&#39;s safe speed without a leader.
Definition: MSCFModel.cpp:95
T ISNAN(T a)
Definition: StdDefs.h:110
virtual SUMOReal moveHelper(MSVehicle *const veh, SUMOReal vPos) const
Applies interaction with stops and lane changing model influences.
Definition: MSCFModel.cpp:59
SUMOReal maximumSafeFollowSpeed(SUMOReal gap, SUMOReal predSpeed, SUMOReal predMaxDecel) const
Returns the maximum safe velocity for following the given leader.
Definition: MSCFModel.cpp:138
SUMOReal getMaxAccel() const
Get the vehicle type&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:178
virtual ~MSCFModel()
Destructor.
Definition: MSCFModel.cpp:52
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&#39;s current speed.
Definition: MSVehicle.h:410
#define SUMOReal
Definition: config.h:213
T MIN3(T a, T b, T c)
Definition: StdDefs.h:82
#define NUMERICAL_EPS
Definition: config.h:160
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the lane&#39;s maximum speed, given a vehicle&#39;s speed limit adaptation.
Definition: MSLane.h:453
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:447
SUMOReal myDecel
The vehicle&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:350