46 #define DEBUG_COND (veh->isSelected()) 60 SUMOVTypeParameter::getDefaultEmergencyDecel(vtype->getParameter().vehicleClass, myDecel))),
62 myHeadwayTime(vtype->getParameter().getCFParam(
SUMO_ATTR_TAU, 1.0)) {
81 return speed * (headwayTime + 0.5 * speed / decel);
92 const int steps = int(speed / speedReduction);
93 return SPEED2DIST(steps * speed - speedReduction * steps * (steps + 1) / 2) + speed * headwayTime;
98 MSCFModel::freeSpeed(
const double currentSpeed,
const double decel,
const double dist,
const double targetSpeed,
const bool onInsertion,
const double actionStepLength) {
114 const double y =
MAX2(0.0, ((sqrt((b + 2.0 * v) * (b + 2.0 * v) + 8.0 * b * dist) - b) * 0.5 - v) / b);
115 const double yFull = floor(y);
116 const double exactGap = (yFull * yFull + yFull) * 0.5 * b + yFull * v + (y > yFull ? v : 0.0);
117 const double fullSpeedGain = (yFull + (onInsertion ? 1. : 0.)) *
ACCEL2SPEED(decel);
118 return DIST2SPEED(
MAX2(0.0, dist - exactGap) / (yFull + 1)) + fullSpeedGain + targetSpeed;
133 assert(currentSpeed >= 0);
134 assert(targetSpeed >= 0);
136 const double dt = onInsertion ? 0 : actionStepLength;
137 const double v0 = currentSpeed;
138 const double vT = targetSpeed;
139 const double b = decel;
150 if (0.5 * (v0 + vT)*dt >= d) {
152 return v0 +
TS * (vT - v0) / actionStepLength;
154 const double q = ((dt * v0 - 2 * d) * b - vT * vT);
155 const double p = 0.5 * b * dt;
156 const double vN = -p + sqrt(p * p - q);
157 return v0 +
TS * (vN - v0) / actionStepLength;
165 const double oldV = veh->
getSpeed();
185 #ifdef DEBUG_FINALIZE_SPEED 187 std::cout <<
"\n" <<
SIMTIME <<
" FINALIZE_SPEED\n";
191 vMax =
MAX2(vMin, vMax);
194 assert(vNext >= vMin);
195 assert(vNext <= vMax);
198 assert(vNext >= vMin);
199 assert(vNext <= vMax);
201 #ifdef DEBUG_FINALIZE_SPEED 204 <<
"veh '" << veh->
getID() <<
"' oldV=" << oldV
209 <<
" vNext=" << vNext
223 const double gap = (vNext - vL) *
296 double leaderMinDist = gap2pred +
distAfterTime(duration, predSpeed, -predMaxDecel);
301 int a = (int)ceil(duration /
TS -
TS);
304 if (bg <= leaderMinDist) {
308 if (
gDebugFlag2) std::cout <<
" followSpeedTransient" 309 <<
" duration=" << duration
310 <<
" gap=" << gap2pred
311 <<
" leaderMinDist=" << leaderMinDist
316 <<
" x=" << (b + leaderMinDist) / duration
318 return (b + leaderMinDist) / duration;
323 while (bg < leaderMinDist) {
332 const double fullBrakingSeconds = sqrt(leaderMinDist * 2 /
myDecel);
333 if (fullBrakingSeconds >= duration) {
337 return leaderMinDist / duration + duration *
getMaxDecel() / 2;
339 return fullBrakingSeconds *
myDecel;
347 return (speed + 0.5 * accel * t) * t;
349 const double decel = -accel;
350 if (speed <= decel * t) {
364 const double speed2 = speed - t * decel;
365 return 0.5 * (speed + speed2) * t;
372 const double accelTime = (arrivalSpeed - currentSpeed) / accel;
373 const double accelWay = accelTime * (arrivalSpeed + currentSpeed) * 0.5;
374 const double nonAccelWay =
MAX2(0., dist - accelWay);
378 return TIME2STEPS(accelTime + nonAccelWay / nonAccelSpeed);
391 if ((accel < 0. && -0.5 * speed * speed / accel < dist) || (accel <= 0. && speed == 0.)) {
400 double p = speed / accel;
404 return (-p - sqrt(p * p + 2 * dist / accel));
409 double t1 = (maxSpeed - speed) / accel;
411 double d1 = speed * t1 + 0.5 * accel * t1 * t1;
414 return (-p + sqrt(p * p + 2 * dist / accel));
416 return (-p + sqrt(p * p + 2 * d1 / accel)) + (dist - d1) / maxSpeed;
430 assert(accel == decel);
432 assert(initialSpeed == 0);
433 assert(arrivalSpeed == 0);
434 assert(maxSpeed > 0);
437 double accelTime = (maxSpeed - initialSpeed) / accel;
439 double accelDist = accelTime * (initialSpeed + 0.5 * (maxSpeed - initialSpeed));
441 if (accelDist >= dist * 0.5) {
443 arrivalTime = 4 * sqrt(accelDist) / accel;
446 const double constSpeedTime = (dist - accelDist * 2) / maxSpeed;
447 arrivalTime = accelTime + constSpeedTime;
455 assert(time > 0 || dist == 0);
457 return -std::numeric_limits<double>::max();
458 }
else if (time * speed > 2 * dist) {
461 return - 0.5 * speed * speed / dist;
465 return 2 * (dist / time - speed) / time;
479 double arrivalSpeedBraking;
482 if (dist < currentSpeed) {
490 return arrivalSpeedBraking;
497 MSCFModel::gapExtrapolation(
const double duration,
const double currentGap,
double v1,
double v2,
double a1,
double a2,
const double maxV1,
const double maxV2) {
499 double newGap = currentGap;
502 for (
unsigned int steps = 1; steps *
TS <= duration; ++steps) {
503 v1 =
MIN2(
MAX2(v1 + a1, 0.), maxV1);
504 v2 =
MIN2(
MAX2(v2 + a2, 0.), maxV2);
505 newGap += TS * (v1 - v2);
510 double t1 = 0, t2 = 0, t3 = 0, t4 = 0;
513 if (a1 < 0 && v1 > 0) {
514 const double leaderStopTime = - v1 / a1;
515 t1 =
MIN2(leaderStopTime, duration);
516 }
else if (a1 >= 0) {
520 if (a2 < 0 && v2 > 0) {
521 const double followerStopTime = -v2 / a2;
522 t2 =
MIN2(followerStopTime, duration);
523 }
else if (a2 >= 0) {
527 if (a1 > 0 && v1 < maxV1) {
528 const double leaderMaxSpeedTime = (maxV1 - v1) / a1;
529 t3 =
MIN2(leaderMaxSpeedTime, duration);
530 }
else if (a1 <= 0) {
534 if (a2 > 0 && v2 < maxV2) {
535 const double followerMaxSpeedTime = (maxV2 - v2) / a2;
536 t4 =
MIN2(followerMaxSpeedTime, duration);
537 }
else if (a2 <= 0) {
549 std::list<double>::const_iterator i;
551 for (i = l.begin(); i != l.end(); ++i) {
553 double dt =
MIN2(*i, duration) - tLast;
556 newGap += dv * dt + da * dt * dt / 2.;
560 if (*i == t1 || *i == t3) {
565 if (*i == t2 || *i == t4) {
570 tLast =
MIN2(*i, duration);
571 if (tLast == duration) {
576 if (duration != tLast) {
578 assert(a1 == 0. && a2 == 0.);
579 double dt = duration - tLast;
591 MSCFModel::passingTime(
const double lastPos,
const double passedPos,
const double currentPos,
const double lastSpeed,
const double currentSpeed) {
593 assert(passedPos <= currentPos);
594 assert(passedPos >= lastPos);
595 assert(currentPos > lastPos);
596 assert(currentSpeed >= 0);
598 if (passedPos > currentPos || passedPos < lastPos) {
599 std::stringstream ss;
603 ss <<
"passingTime(): given argument passedPos = " << passedPos <<
" doesn't lie within [lastPos, currentPos] = [" << lastPos <<
", " << currentPos <<
"]\nExtrapolating...";
604 std::cout << ss.str() <<
"\n";
607 const double lastCoveredDist = currentPos - lastPos;
608 const double extrapolated = passedPos > currentPos ?
TS * (passedPos - lastPos) / lastCoveredDist :
TS * (currentPos - passedPos) / lastCoveredDist;
610 }
else if (currentSpeed < 0) {
611 WRITE_ERROR(
"passingTime(): given argument 'currentSpeed' is negative. This case is not handled yet.");
615 const double distanceOldToPassed = passedPos - lastPos;
619 const double t = distanceOldToPassed / currentSpeed;
627 if (currentSpeed > 0) {
632 assert(currentSpeed == 0 && lastSpeed != 0);
636 a = lastSpeed * lastSpeed / (2 * (lastPos - currentPos));
645 const double t = 2 * distanceOldToPassed / (lastSpeed + currentSpeed);
649 const double va = lastSpeed / a;
650 const double t = -va + sqrt(va * va + 2 * distanceOldToPassed / a);
651 assert(t < 1 && t >= 0);
655 const double va = lastSpeed / a;
656 const double t = -va - sqrt(va * va + 2 * distanceOldToPassed / a);
657 assert(t < 1 && t >= 0);
667 assert(t >= 0 && t <=
TS);
675 if (dist <
TS * v0 / 2) {
678 const double accel = - v0 * v0 / (2 * dist);
680 return v0 + accel * t;
683 const double accel = 2 * (dist /
TS - v0) /
TS;
685 return v0 + accel * t;
697 (double)sqrt(2 * dist * accel + v * v)));
762 const double g = gap;
772 const double n = floor(.5 - ((t + (sqrt(((s * s) + (4.0 * ((s * (2.0 * g / b - t)) + (t * t))))) * -0.5)) / s));
773 const double h = 0.5 * n * (n - 1) * b * s + n * b * t;
777 const double r = (g - h) / (n * s + t);
778 const double x = n * b + r;
803 const double btau =
myDecel * headway;
804 const double v0 = -btau + sqrt(btau * btau + 2 *
myDecel * g);
813 const double tau = headway;
814 const double v0 =
MAX2(0., v);
816 if (v0 * tau >= 2 * g) {
820 return -std::numeric_limits<double>::max();
828 const double a = -v0 * v0 / (2 * g);
843 const double btau2 =
myDecel * tau / 2;
844 const double v1 = -btau2 + sqrt(btau2 * btau2 +
myDecel * (2 * g - tau * v0));
845 const double a = (v1 - v0) / tau;
890 #ifdef DEBUG_EMERGENCYDECEL 892 std::cout <<
SIMTIME <<
" initial vsafe=" << x
893 <<
" egoSpeed=" << egoSpeed <<
" (origSafeDecel=" << origSafeDecel <<
")" 894 <<
" predSpeed=" << predSpeed <<
" (predDecel=" << predMaxDecel <<
")" 906 x =
MIN2(origSafeDecel, x);
908 #ifdef DEBUG_EMERGENCYDECEL 910 std::cout <<
" -> corrected emergency deceleration: " << safeDecel << std::endl;
929 const double predBrakeDist = 0.5 * predSpeed * predSpeed / predMaxDecel;
931 const double b1 = 0.5 * egoSpeed * egoSpeed / (gap + predBrakeDist);
933 #ifdef DEBUG_EMERGENCYDECEL 935 std::cout <<
SIMTIME <<
" calculateEmergencyDeceleration()" 936 <<
" gap=" << gap <<
" egoSpeed=" << egoSpeed <<
" predSpeed=" << predSpeed
937 <<
" predBrakeDist=" << predBrakeDist
943 if (b1 <= predMaxDecel) {
945 #ifdef DEBUG_EMERGENCYDECEL 947 std::cout <<
" case 1 ..." << std::endl;
952 #ifdef DEBUG_EMERGENCYDECEL 954 std::cout <<
" case 2 ...";
959 assert(gap < 0 || predSpeed < egoSpeed);
961 return - std::numeric_limits<double>::max();
964 const double b2 = 0.5 * (egoSpeed * egoSpeed - predSpeed * predSpeed) / gap;
966 #ifdef DEBUG_EMERGENCYDECEL 968 std::cout <<
" b2=" << b2 << std::endl;
983 const double perceivedGap = veh->
getDriverState()->getPerceivedHeadway(gap, pred);
984 const double perceivedSpeedDifference = veh->
getDriverState()->getPerceivedSpeedDifference(predSpeed - speed, gap, pred);
986 #ifdef DEBUG_DRIVER_ERRORS 990 std::cout <<
SIMTIME <<
" veh '" << veh->
getID() <<
"' -> MSCFModel_Krauss::applyHeadwayAndSpeedDifferencePerceptionErrors()\n" 991 <<
" speed=" << speed <<
" gap=" << gap <<
" leaderSpeed=" << predSpeed
992 <<
"\n perceivedGap=" << perceivedGap <<
" perceivedLeaderSpeed=" << speed + perceivedSpeedDifference
993 <<
" perceivedSpeedDifference=" << perceivedSpeedDifference
995 const double exactFollowSpeed =
followSpeed(veh, speed, gap, predSpeed, predMaxDecel);
996 const double errorFollowSpeed =
followSpeed(veh, speed, perceivedGap, speed + perceivedSpeedDifference, predMaxDecel);
997 const double accelError =
SPEED2ACCEL(errorFollowSpeed - exactFollowSpeed);
998 std::cout <<
" gapError=" << perceivedGap - gap <<
" dvError=" << perceivedSpeedDifference - (predSpeed - speed)
999 <<
"\n resulting accelError: " << accelError << std::endl;
1006 predSpeed = speed + perceivedSpeedDifference;
1016 const double perceivedGap = veh->
getDriverState()->getPerceivedHeadway(gap);
1018 #ifdef DEBUG_DRIVER_ERRORS 1022 std::cout <<
SIMTIME <<
" veh '" << veh->
getID() <<
"' -> MSCFModel_Krauss::applyHeadwayPerceptionError()\n" 1023 <<
" speed=" << speed <<
" gap=" << gap <<
"\n perceivedGap=" << perceivedGap << std::endl;
1024 const double exactStopSpeed =
stopSpeed(veh, speed, gap);
1025 const double errorStopSpeed =
stopSpeed(veh, speed, perceivedGap);
1026 const double accelError =
SPEED2ACCEL(errorStopSpeed - exactStopSpeed);
1027 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) ...