46 #define DEBUG_COND (veh->isSelected()) 63 myHeadwayTime(vtype->getParameter().getCFParam(
SUMO_ATTR_TAU, 1.0)) {
82 return speed * (headwayTime + 0.5 * speed / decel);
93 const int steps = int(speed / speedReduction);
94 return SPEED2DIST(steps * speed - speedReduction * steps * (steps + 1) / 2) + speed * headwayTime;
99 MSCFModel::freeSpeed(
const double currentSpeed,
const double decel,
const double dist,
const double targetSpeed,
const bool onInsertion,
const double actionStepLength) {
115 const double y =
MAX2(0.0, ((sqrt((b + 2.0 * v) * (b + 2.0 * v) + 8.0 * b * dist) - b) * 0.5 - v) / b);
116 const double yFull = floor(y);
117 const double exactGap = (yFull * yFull + yFull) * 0.5 * b + yFull * v + (y > yFull ? v : 0.0);
118 const double fullSpeedGain = (yFull + (onInsertion ? 1. : 0.)) *
ACCEL2SPEED(decel);
119 return DIST2SPEED(
MAX2(0.0, dist - exactGap) / (yFull + 1)) + fullSpeedGain + targetSpeed;
134 assert(currentSpeed >= 0);
135 assert(targetSpeed >= 0);
137 const double dt = onInsertion ? 0 : actionStepLength;
138 const double v0 = currentSpeed;
139 const double vT = targetSpeed;
140 const double b = decel;
151 if (0.5 * (v0 + vT)*dt >= d) {
153 return v0 +
TS * (vT - v0) / actionStepLength;
155 const double q = ((dt * v0 - 2 * d) * b - vT * vT);
156 const double p = 0.5 * b * dt;
157 const double vN = -p + sqrt(p * p - q);
158 return v0 +
TS * (vN - v0) / actionStepLength;
166 const double oldV = veh->
getSpeed();
186 #ifdef DEBUG_FINALIZE_SPEED 188 std::cout <<
"\n" <<
SIMTIME <<
" FINALIZE_SPEED\n";
192 vMax =
MAX2(vMin, vMax);
195 assert(vNext >= vMin);
196 assert(vNext <= vMax);
199 assert(vNext >= vMin);
200 assert(vNext <= vMax);
202 #ifdef DEBUG_FINALIZE_SPEED 205 <<
"veh '" << veh->
getID() <<
"' oldV=" << oldV
210 <<
" vNext=" << vNext
224 const double gap = (vNext - vL) *
297 double leaderMinDist = gap2pred +
distAfterTime(duration, predSpeed, -predMaxDecel);
302 int a = (int)ceil(duration /
TS -
TS);
305 if (bg <= leaderMinDist) {
309 if (
gDebugFlag2) std::cout <<
" followSpeedTransient" 310 <<
" duration=" << duration
311 <<
" gap=" << gap2pred
312 <<
" leaderMinDist=" << leaderMinDist
317 <<
" x=" << (b + leaderMinDist) / duration
319 return (b + leaderMinDist) / duration;
324 while (bg < leaderMinDist) {
333 const double fullBrakingSeconds = sqrt(leaderMinDist * 2 /
myDecel);
334 if (fullBrakingSeconds >= duration) {
338 return leaderMinDist / duration + duration *
getMaxDecel() / 2;
340 return fullBrakingSeconds *
myDecel;
348 return (speed + 0.5 * accel * t) * t;
350 const double decel = -accel;
351 if (speed <= decel * t) {
365 const double speed2 = speed - t * decel;
366 return 0.5 * (speed + speed2) * t;
373 const double accelTime = (arrivalSpeed - currentSpeed) / accel;
374 const double accelWay = accelTime * (arrivalSpeed + currentSpeed) * 0.5;
375 const double nonAccelWay =
MAX2(0., dist - accelWay);
379 return TIME2STEPS(accelTime + nonAccelWay / nonAccelSpeed);
392 if ((accel < 0. && -0.5 * speed * speed / accel < dist) || (accel <= 0. && speed == 0.)) {
401 double p = speed / accel;
405 return (-p - sqrt(p * p + 2 * dist / accel));
410 double t1 = (maxSpeed - speed) / accel;
412 double d1 = speed * t1 + 0.5 * accel * t1 * t1;
415 return (-p + sqrt(p * p + 2 * dist / accel));
417 return (-p + sqrt(p * p + 2 * d1 / accel)) + (dist - d1) / maxSpeed;
431 assert(accel == decel);
433 assert(initialSpeed == 0);
434 assert(arrivalSpeed == 0);
435 assert(maxSpeed > 0);
438 double accelTime = (maxSpeed - initialSpeed) / accel;
440 double accelDist = accelTime * (initialSpeed + 0.5 * (maxSpeed - initialSpeed));
442 if (accelDist >= dist * 0.5) {
444 arrivalTime = 4 * sqrt(accelDist) / accel;
447 const double constSpeedTime = (dist - accelDist * 2) / maxSpeed;
448 arrivalTime = accelTime + constSpeedTime;
456 assert(time > 0 || dist == 0);
458 return -std::numeric_limits<double>::max();
459 }
else if (time * speed > 2 * dist) {
462 return - 0.5 * speed * speed / dist;
466 return 2 * (dist / time - speed) / time;
480 double arrivalSpeedBraking;
483 if (dist < currentSpeed) {
491 return arrivalSpeedBraking;
498 MSCFModel::gapExtrapolation(
const double duration,
const double currentGap,
double v1,
double v2,
double a1,
double a2,
const double maxV1,
const double maxV2) {
500 double newGap = currentGap;
503 for (
unsigned int steps = 1; steps *
TS <= duration; ++steps) {
504 v1 =
MIN2(
MAX2(v1 + a1, 0.), maxV1);
505 v2 =
MIN2(
MAX2(v2 + a2, 0.), maxV2);
506 newGap += TS * (v1 - v2);
511 double t1 = 0, t2 = 0, t3 = 0, t4 = 0;
514 if (a1 < 0 && v1 > 0) {
515 const double leaderStopTime = - v1 / a1;
516 t1 =
MIN2(leaderStopTime, duration);
517 }
else if (a1 >= 0) {
521 if (a2 < 0 && v2 > 0) {
522 const double followerStopTime = -v2 / a2;
523 t2 =
MIN2(followerStopTime, duration);
524 }
else if (a2 >= 0) {
528 if (a1 > 0 && v1 < maxV1) {
529 const double leaderMaxSpeedTime = (maxV1 - v1) / a1;
530 t3 =
MIN2(leaderMaxSpeedTime, duration);
531 }
else if (a1 <= 0) {
535 if (a2 > 0 && v2 < maxV2) {
536 const double followerMaxSpeedTime = (maxV2 - v2) / a2;
537 t4 =
MIN2(followerMaxSpeedTime, duration);
538 }
else if (a2 <= 0) {
550 std::list<double>::const_iterator i;
552 for (i = l.begin(); i != l.end(); ++i) {
554 double dt =
MIN2(*i, duration) - tLast;
557 newGap += dv * dt + da * dt * dt / 2.;
561 if (*i == t1 || *i == t3) {
566 if (*i == t2 || *i == t4) {
571 tLast =
MIN2(*i, duration);
572 if (tLast == duration) {
577 if (duration != tLast) {
579 assert(a1 == 0. && a2 == 0.);
580 double dt = duration - tLast;
592 MSCFModel::passingTime(
const double lastPos,
const double passedPos,
const double currentPos,
const double lastSpeed,
const double currentSpeed) {
594 assert(passedPos <= currentPos);
595 assert(passedPos >= lastPos);
596 assert(currentPos > lastPos);
597 assert(currentSpeed >= 0);
599 if (passedPos > currentPos || passedPos < lastPos) {
600 std::stringstream ss;
604 ss <<
"passingTime(): given argument passedPos = " << passedPos <<
" doesn't lie within [lastPos, currentPos] = [" << lastPos <<
", " << currentPos <<
"]\nExtrapolating...";
605 std::cout << ss.str() <<
"\n";
608 const double lastCoveredDist = currentPos - lastPos;
609 const double extrapolated = passedPos > currentPos ?
TS * (passedPos - lastPos) / lastCoveredDist :
TS * (currentPos - passedPos) / lastCoveredDist;
611 }
else if (currentSpeed < 0) {
612 WRITE_ERROR(
"passingTime(): given argument 'currentSpeed' is negative. This case is not handled yet.");
616 const double distanceOldToPassed = passedPos - lastPos;
620 const double t = distanceOldToPassed / currentSpeed;
628 if (currentSpeed > 0) {
633 assert(currentSpeed == 0 && lastSpeed != 0);
637 a = lastSpeed * lastSpeed / (2 * (lastPos - currentPos));
646 const double t = 2 * distanceOldToPassed / (lastSpeed + currentSpeed);
650 const double va = lastSpeed / a;
651 const double t = -va + sqrt(va * va + 2 * distanceOldToPassed / a);
652 assert(t < 1 && t >= 0);
656 const double va = lastSpeed / a;
657 const double t = -va - sqrt(va * va + 2 * distanceOldToPassed / a);
658 assert(t < 1 && t >= 0);
668 assert(t >= 0 && t <=
TS);
676 if (dist <
TS * v0 / 2) {
679 const double accel = - v0 * v0 / (2 * dist);
681 return v0 + accel * t;
684 const double accel = 2 * (dist /
TS - v0) /
TS;
686 return v0 + accel * t;
698 (double)sqrt(2 * dist * accel + v * v)));
763 const double g = gap;
773 const double n = floor(.5 - ((t + (sqrt(((s * s) + (4.0 * ((s * (2.0 * g / b - t)) + (t * t))))) * -0.5)) / s));
774 const double h = 0.5 * n * (n - 1) * b * s + n * b * t;
778 const double r = (g - h) / (n * s + t);
779 const double x = n * b + r;
804 const double btau =
myDecel * headway;
805 const double v0 = -btau + sqrt(btau * btau + 2 *
myDecel * g);
814 const double tau = headway;
815 const double v0 =
MAX2(0., v);
817 if (v0 * tau >= 2 * g) {
821 return -std::numeric_limits<double>::max();
829 const double a = -v0 * v0 / (2 * g);
844 const double btau2 =
myDecel * tau / 2;
845 const double v1 = -btau2 + sqrt(btau2 * btau2 +
myDecel * (2 * g - tau * v0));
846 const double a = (v1 - v0) / tau;
891 #ifdef DEBUG_EMERGENCYDECEL 893 std::cout <<
SIMTIME <<
" initial vsafe=" << x
894 <<
" egoSpeed=" << egoSpeed <<
" (origSafeDecel=" << origSafeDecel <<
")" 895 <<
" predSpeed=" << predSpeed <<
" (predDecel=" << predMaxDecel <<
")" 907 x =
MIN2(origSafeDecel, x);
909 #ifdef DEBUG_EMERGENCYDECEL 911 std::cout <<
" -> corrected emergency deceleration: " << safeDecel << std::endl;
930 const double predBrakeDist = 0.5 * predSpeed * predSpeed / predMaxDecel;
932 const double b1 = 0.5 * egoSpeed * egoSpeed / (gap + predBrakeDist);
934 #ifdef DEBUG_EMERGENCYDECEL 936 std::cout <<
SIMTIME <<
" calculateEmergencyDeceleration()" 937 <<
" gap=" << gap <<
" egoSpeed=" << egoSpeed <<
" predSpeed=" << predSpeed
938 <<
" predBrakeDist=" << predBrakeDist
944 if (b1 <= predMaxDecel) {
946 #ifdef DEBUG_EMERGENCYDECEL 948 std::cout <<
" case 1 ..." << std::endl;
953 #ifdef DEBUG_EMERGENCYDECEL 955 std::cout <<
" case 2 ...";
960 assert(gap < 0 || predSpeed < egoSpeed);
962 return - std::numeric_limits<double>::max();
965 const double b2 = 0.5 * (egoSpeed * egoSpeed - predSpeed * predSpeed) / gap;
967 #ifdef DEBUG_EMERGENCYDECEL 969 std::cout <<
" b2=" << b2 << std::endl;
984 const double perceivedGap = veh->
getDriverState()->getPerceivedHeadway(gap, pred);
985 const double perceivedSpeedDifference = veh->
getDriverState()->getPerceivedSpeedDifference(predSpeed - speed, gap, pred);
987 #ifdef DEBUG_DRIVER_ERRORS 991 std::cout <<
SIMTIME <<
" veh '" << veh->
getID() <<
"' -> MSCFModel_Krauss::applyHeadwayAndSpeedDifferencePerceptionErrors()\n" 992 <<
" speed=" << speed <<
" gap=" << gap <<
" leaderSpeed=" << predSpeed
993 <<
"\n perceivedGap=" << perceivedGap <<
" perceivedLeaderSpeed=" << speed + perceivedSpeedDifference
994 <<
" perceivedSpeedDifference=" << perceivedSpeedDifference
996 const double exactFollowSpeed =
followSpeed(veh, speed, gap, predSpeed, predMaxDecel);
997 const double errorFollowSpeed =
followSpeed(veh, speed, perceivedGap, speed + perceivedSpeedDifference, predMaxDecel);
998 const double accelError =
SPEED2ACCEL(errorFollowSpeed - exactFollowSpeed);
999 std::cout <<
" gapError=" << perceivedGap - gap <<
" dvError=" << perceivedSpeedDifference - (predSpeed - speed)
1000 <<
"\n resulting accelError: " << accelError << std::endl;
1007 predSpeed = speed + perceivedSpeedDifference;
1017 const double perceivedGap = veh->
getDriverState()->getPerceivedHeadway(gap);
1019 #ifdef DEBUG_DRIVER_ERRORS 1023 std::cout <<
SIMTIME <<
" veh '" << veh->
getID() <<
"' -> MSCFModel_Krauss::applyHeadwayPerceptionError()\n" 1024 <<
" speed=" << speed <<
" gap=" << gap <<
"\n perceivedGap=" << perceivedGap << std::endl;
1025 const double exactStopSpeed =
stopSpeed(veh, speed, gap);
1026 const double errorStopSpeed =
stopSpeed(veh, speed, perceivedGap);
1027 const double accelError =
SPEED2ACCEL(errorStopSpeed - exactStopSpeed);
1028 std::cout <<
" gapError=" << perceivedGap - gap <<
"\n resulting accelError: " << accelError << std::endl;
static double speedAfterTime(const double t, const double oldSpeed, const double dist)
Calculates the speed after a time t [0,TS] given the initial speed and the distance traveled in an i...
double getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
double maximumSafeFollowSpeed(double gap, double egoSpeed, double predSpeed, double predMaxDecel, bool onInsertion=false) const
Returns the maximum safe velocity for following the given leader.
double brakeGap(const double speed) const
Returns the distance the vehicle needs to halt including driver's reaction time tau (i...
virtual double freeSpeed(const MSVehicle *const veh, double speed, double seen, double maxSpeed, const bool onInsertion=false) const
Computes the vehicle's safe speed without a leader.
Representation of a vehicle in the micro simulation.
const MSVehicleType * myType
The type to which this model definition belongs to.
virtual ~VehicleVariables()
#define EMERGENCY_DECEL_AMPLIFIER
MSLane * getLane() const
Returns the lane the vehicle is on.
virtual double minNextSpeed(double speed, const MSVehicle *const veh=0) const
Returns the minimum speed given the current speed (depends on the numerical update scheme and its ste...
Structure representing possible vehicle parameter.
int gPrecision
the precision for floating point outputs
static bool gComputeLC
whether the simulationLoop is in the lane changing phase
MSCFModel(const MSVehicleType *vtype)
Constructor.
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
virtual double insertionStopSpeed(const MSVehicle *const veh, double speed, double gap) const
Computes the vehicle's safe speed for approaching an obstacle at insertion without constraints due to...
static double avoidArrivalAccel(double dist, double time, double speed)
Computes the acceleration needed to arrive not before the given time.
#define UNUSED_PARAMETER(x)
virtual double insertionFollowSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel) const
Computes the vehicle's safe speed (no dawdling) This method is used during the insertion stage...
virtual double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences. Called at most once per simulation...
The car-following model and parameter.
MSAbstractLaneChangeModel & getLaneChangeModel()
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. It uses information on LC-related desired ...
double getMaxAccel() const
Get the vehicle type's maximum acceleration [m/s^2].
bool hasDriverState() const
Whether this vehicle is equipped with a MSDriverState.
static double distAfterTime(double t, double speed, double accel)
calculates the distance travelled after accelerating for time t
double getActionStepLengthSecs() const
Returns the vehicle's action step length in secs, i.e. the interval between two action points...
virtual double minNextSpeedEmergency(double speed, const MSVehicle *const veh=0) const
Returns the minimum speed after emergency braking, given the current speed (depends on the numerical ...
static double brakeGapEuler(const double speed, const double decel, const double headwayTime)
double getMaxSpeed() const
Get vehicle's maximum speed [m/s].
virtual double interactionGap(const MSVehicle *const veh, double vL) const
Returns the maximum gap at which an interaction between both vehicles occurs.
double getMaxDecel() const
Get the vehicle type's maximal comfortable deceleration [m/s^2].
double calculateEmergencyDeceleration(double gap, double egoSpeed, double predSpeed, double predMaxDecel) const
Returns the minimal deceleration for following the given leader safely.
double maximumSafeStopSpeed(double gap, double currentSpeed, bool onInsertion=false, double headway=-1) const
Returns the maximum next velocity for stopping within gap.
double getMinimalArrivalSpeedEuler(double dist, double currentSpeed) const
Computes the minimal possible arrival speed after covering a given distance for Euler update...
double myDecel
The vehicle's maximum deceleration [m/s^2].
void applyHeadwayAndSpeedDifferencePerceptionErrors(const MSVehicle *const veh, double speed, double &gap, double &predSpeed, double predMaxDecel, const MSVehicle *const pred) const
Overwrites gap2pred and predSpeed by the perceived values obtained from the vehicle's driver state...
static double gapExtrapolation(const double duration, const double currentGap, double v1, double v2, double a1=0, double a2=0, const double maxV1=std::numeric_limits< double >::max(), const double maxV2=std::numeric_limits< double >::max())
return the resulting gap if, starting with gap currentGap, two vehicles continue with constant accele...
virtual ~MSCFModel()
Destructor.
static double passingTime(const double lastPos, const double passedPos, const double currentPos, const double lastSpeed, const double currentSpeed)
Calculates the time at which the position passedPosition has been passed In case of a ballistic updat...
double processNextStop(double currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
double maximumSafeStopSpeedBallistic(double gap, double currentSpeed, bool onInsertion=false, double headway=-1) const
Returns the maximum next velocity for stopping within gap when using the ballistic positional update...
virtual double getHeadwayTime() const
Get the driver's desired headway [s].
static double estimateArrivalTime(double dist, double speed, double maxSpeed, double accel)
Computes the time needed to travel a distance dist given an initial speed and constant acceleration...
double myEmergencyDecel
The vehicle's maximum emergency deceleration [m/s^2].
double getMinimalArrivalSpeed(double dist, double currentSpeed) const
Computes the minimal possible arrival speed after covering a given distance.
SUMOTime getMinimalArrivalTime(double dist, double currentSpeed, double arrivalSpeed) const
Computes the minimal time needed to cover a distance given the desired speed at arrival.
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
virtual double followSpeedTransient(double duration, const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel) const
Computes the vehicle's follow speed that avoids a collision for the given amount of time...
double estimateSpeedAfterDistance(const double dist, const double v, const double accel) const
virtual double patchSpeedBeforeLC(const MSVehicle *veh, double vMin, double vMax) const
apply custom speed adaptations within the given speed bounds
static bool gSemiImplicitEulerUpdate
std::shared_ptr< MSSimpleDriverState > getDriverState() const
Returns the vehicle driver's state.
const double INVALID_DOUBLE
double myHeadwayTime
The driver's desired time headway (aka reaction time tau) [s].
double getSpeed() const
Returns the vehicle's current speed.
double maximumSafeStopSpeedEuler(double gap, double headway=-1) const
Returns the maximum next velocity for stopping within gap when using the semi-implicit Euler update...
virtual double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0) const =0
Computes the vehicle's follow speed (no dawdling)
const std::string & getID() const
Returns the name of the vehicle.
void applyHeadwayPerceptionError(const MSVehicle *const veh, double speed, double &gap) const
Overwrites gap by the perceived value obtained from the vehicle's driver state.
virtual double stopSpeed(const MSVehicle *const veh, const double speed, double gap) const =0
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling) ...