/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file MSDriverState.cpp14/// @author Melanie Weber15/// @author Andreas Kendziorra16/// @author Michael Behrisch17/// @date Thu, 12 Jun 201418///19// The common superclass for modelling transportable objects like persons and containers20/****************************************************************************/21#include <config.h>2223#include <math.h>24#include <cmath>25#include <utils/common/RandHelper.h>26#include <utils/common/SUMOTime.h>27//#include <microsim/MSVehicle.h>28#include <microsim/transportables/MSPerson.h>29//#include <microsim/MSLane.h>30#include <microsim/MSEdge.h>31//#include <microsim/MSGlobals.h>32//#include <microsim/MSNet.h>33#include <microsim/traffic_lights/MSTrafficLightLogic.h>34#include <microsim/lcmodels/MSAbstractLaneChangeModel.h>35#include "MSDriverState.h"3637// ===========================================================================38// DEBUG constants39// ===========================================================================40//#define DEBUG_OUPROCESS41//#define DEBUG_TRAFFIC_ITEMS42//#define DEBUG_AWARENESS43//#define DEBUG_PERCEPTION_ERRORS44//#define DEBUG_DRIVERSTATE45#define DEBUG_COND (true)46//#define DEBUG_COND (myVehicle->isSelected())474849/* -------------------------------------------------------------------------50* static member definitions51* ----------------------------------------------------------------------- */52// hash function53//std::hash<std::string> MSDriverState::MSTrafficItem::hash = std::hash<std::string>();54SumoRNG OUProcess::myRNG("driverstate");5556// ===========================================================================57// Default value definitions58// ===========================================================================59//double TCIDefaults::myMinTaskCapability = 0.1;60//double TCIDefaults::myMaxTaskCapability = 10.0;61//double TCIDefaults::myMaxTaskDemand = 20.0;62//double TCIDefaults::myMaxDifficulty = 10.0;63//double TCIDefaults::mySubCriticalDifficultyCoefficient = 0.1;64//double TCIDefaults::mySuperCriticalDifficultyCoefficient = 1.0;65//double TCIDefaults::myOppositeDirectionDrivingFactor = 1.3;66//double TCIDefaults::myHomeostasisDifficulty = 1.5;67//double TCIDefaults::myCapabilityTimeScale = 0.5;68//double TCIDefaults::myAccelerationErrorTimeScaleCoefficient = 1.0;69//double TCIDefaults::myAccelerationErrorNoiseIntensityCoefficient = 1.0;70//double TCIDefaults::myActionStepLengthCoefficient = 1.0;71//double TCIDefaults::myMinActionStepLength = 0.0;72//double TCIDefaults::myMaxActionStepLength = 3.0;73//double TCIDefaults::mySpeedPerceptionErrorTimeScaleCoefficient = 1.0;74//double TCIDefaults::mySpeedPerceptionErrorNoiseIntensityCoefficient = 1.0;75//double TCIDefaults::myHeadwayPerceptionErrorTimeScaleCoefficient = 1.0;76//double TCIDefaults::myHeadwayPerceptionErrorNoiseIntensityCoefficient = 1.0;7778double DriverStateDefaults::minAwareness = 0.1;79double DriverStateDefaults::initialAwareness = 1.0;80double DriverStateDefaults::errorTimeScaleCoefficient = 100.0;81double DriverStateDefaults::errorNoiseIntensityCoefficient = 0.2;82double DriverStateDefaults::speedDifferenceErrorCoefficient = 0.15;83double DriverStateDefaults::headwayErrorCoefficient = 0.75;84double DriverStateDefaults::freeSpeedErrorCoefficient = 0.0;85double DriverStateDefaults::speedDifferenceChangePerceptionThreshold = 0.1;86double DriverStateDefaults::headwayChangePerceptionThreshold = 0.1;87double DriverStateDefaults::maximalReactionTimeFactor = 1.0;888990// ===========================================================================91// method definitions92// ===========================================================================9394OUProcess::OUProcess(double initialState, double timeScale, double noiseIntensity)95: myState(initialState),96myTimeScale(timeScale),97myNoiseIntensity(noiseIntensity) {}9899100OUProcess::~OUProcess() {}101102103void104OUProcess::step(double dt) {105#ifdef DEBUG_OUPROCESS106const double oldstate = myState;107#endif108myState = exp(-dt / myTimeScale) * myState + myNoiseIntensity * sqrt(2 * dt / myTimeScale) * RandHelper::randNorm(0, 1, &myRNG);109#ifdef DEBUG_OUPROCESS110std::cout << " OU-step (" << dt << " s.): " << oldstate << "->" << myState << std::endl;111#endif112}113114double115OUProcess::step(double state, double dt, double timeScale, double noiseIntensity) {116/// see above117return exp(-dt / timeScale) * state + noiseIntensity * sqrt(2 * dt / timeScale) * RandHelper::randNorm(0, 1, &myRNG);118}119120double121OUProcess::getState() const {122return myState;123}124125126MSSimpleDriverState::MSSimpleDriverState(MSVehicle* veh) :127myVehicle(veh),128myAwareness(1.),129myMinAwareness(DriverStateDefaults::minAwareness),130myError(0., 1., 1.),131myErrorTimeScaleCoefficient(DriverStateDefaults::errorTimeScaleCoefficient),132myErrorNoiseIntensityCoefficient(DriverStateDefaults::errorNoiseIntensityCoefficient),133mySpeedDifferenceErrorCoefficient(DriverStateDefaults::speedDifferenceErrorCoefficient),134myHeadwayErrorCoefficient(DriverStateDefaults::headwayErrorCoefficient),135myFreeSpeedErrorCoefficient(DriverStateDefaults::freeSpeedErrorCoefficient),136myHeadwayChangePerceptionThreshold(DriverStateDefaults::headwayChangePerceptionThreshold),137mySpeedDifferenceChangePerceptionThreshold(DriverStateDefaults::speedDifferenceChangePerceptionThreshold),138myOriginalReactionTime(veh->getActionStepLengthSecs()),139myMaximalReactionTime(DriverStateDefaults::maximalReactionTimeFactor * myOriginalReactionTime),140// myActionStepLength(TS),141myStepDuration(TS),142myLastUpdateTime(SIMTIME - TS),143myDebugLock(false) {144#ifdef DEBUG_DRIVERSTATE145std::cout << "Constructing driver state for veh '" << veh->getID() << "'." << std::endl;146#endif147updateError();148updateReactionTime();149}150151152void153MSSimpleDriverState::update() {154#ifdef DEBUG_AWARENESS155if (DEBUG_COND) {156std::cout << SIMTIME << " veh=" << myVehicle->getID() << ", DriverState::update()" << std::endl;157}158#endif159// Adapt step duration160updateStepDuration();161// Update error162updateError();163// Update actionStepLength, aka reaction time164updateReactionTime();165// Update assumed gaps166updateAssumedGaps();167#ifdef DEBUG_AWARENESS168if (DEBUG_COND) {169std::cout << SIMTIME << " stepDuration=" << myStepDuration << ", error=" << myError.getState() << std::endl;170}171#endif172}173174void175MSSimpleDriverState::updateStepDuration() {176myStepDuration = SIMTIME - myLastUpdateTime;177myLastUpdateTime = SIMTIME;178}179180void181MSSimpleDriverState::updateError() {182if (myAwareness == 1.0 || myAwareness == 0.0) {183myError.setState(0.);184} else {185myError.setTimeScale(myErrorTimeScaleCoefficient * myAwareness);186myError.setNoiseIntensity(myErrorNoiseIntensityCoefficient * (1. - myAwareness));187myError.step(myStepDuration);188}189}190191void192MSSimpleDriverState::updateReactionTime() {193if (myAwareness == 1.0 || myAwareness == 0.0) {194myActionStepLength = myOriginalReactionTime;195} else {196const double theta = (myAwareness - myMinAwareness) / (1.0 - myMinAwareness);197myActionStepLength = myOriginalReactionTime + theta * (myMaximalReactionTime - myOriginalReactionTime);198// Round to multiple of simstep length199int quotient;200remquo(myActionStepLength, TS, "ient);201myActionStepLength = TS * MAX2(quotient, 1);202}203}204205void206MSSimpleDriverState::setAwareness(const double value) {207assert(value >= 0.);208assert(value <= 1.);209#ifdef DEBUG_AWARENESS210if (DEBUG_COND) {211std::cout << SIMTIME << " veh=" << myVehicle->getID() << ", setAwareness(" << MAX2(value, myMinAwareness) << ")" << std::endl;212}213#endif214myAwareness = MAX2(value, myMinAwareness);215if (myAwareness == 1.) {216myError.setState(0.);217}218updateReactionTime();219}220221222double223MSSimpleDriverState::getPerceivedOwnSpeed(double speed) {224return speed + myFreeSpeedErrorCoefficient * myError.getState() * sqrt(speed);225}226227228double229MSSimpleDriverState::getPerceivedHeadway(const double trueGap, const void* objID) {230#ifdef DEBUG_PERCEPTION_ERRORS231if (DEBUG_COND) {232if (!debugLocked()) {233std::cout << SIMTIME << " getPerceivedHeadway() for veh '" << myVehicle->getID() << "'\n"234<< " trueGap=" << trueGap << " objID=" << objID << std::endl;235}236}237#endif238239const double perceivedGap = trueGap + myHeadwayErrorCoefficient * myError.getState() * trueGap;240const auto assumedGap = myAssumedGap.find(objID);241if (assumedGap == myAssumedGap.end()242|| fabs(perceivedGap - assumedGap->second) > myHeadwayChangePerceptionThreshold * trueGap * (1.0 - myAwareness)) {243244#ifdef DEBUG_PERCEPTION_ERRORS245if (!debugLocked()) {246std::cout << " new perceived gap (=" << perceivedGap << ") differs significantly from the assumed (="247<< (assumedGap == myAssumedGap.end() ? "NA" : toString(assumedGap->second)) << ")" << std::endl;248}249#endif250251// new perceived gap differs significantly from the previous252myAssumedGap[objID] = perceivedGap;253return perceivedGap;254} else {255256#ifdef DEBUG_PERCEPTION_ERRORS257if (DEBUG_COND) {258if (!debugLocked()) {259std::cout << " new perceived gap (=" << perceivedGap << ") does *not* differ significantly from the assumed (="260<< (assumedGap->second) << ")" << std::endl;261}262}263#endif264// new perceived gap doesn't differ significantly from the previous265return myAssumedGap[objID];266}267}268269void270MSSimpleDriverState::updateAssumedGaps() {271for (auto& p : myAssumedGap) {272const void* objID = p.first;273const auto speedDiff = myLastPerceivedSpeedDifference.find(objID);274double assumedSpeedDiff;275if (speedDiff != myLastPerceivedSpeedDifference.end()) {276// update the assumed gap with the last perceived speed difference277assumedSpeedDiff = speedDiff->second;278} else {279// Assume the object is not moving, if no perceived speed difference is known.280assumedSpeedDiff = -myVehicle->getSpeed();281}282p.second += SPEED2DIST(assumedSpeedDiff);283}284}285286double287MSSimpleDriverState::getPerceivedSpeedDifference(const double trueSpeedDifference, const double trueGap, const void* objID) {288#ifdef DEBUG_PERCEPTION_ERRORS289if (DEBUG_COND) {290if (!debugLocked()) {291std::cout << SIMTIME << " getPerceivedSpeedDifference() for veh '" << myVehicle->getID() << "'\n"292<< " trueGap=" << trueGap << " trueSpeedDifference=" << trueSpeedDifference << " objID=" << objID << std::endl;293}294}295#endif296const double perceivedSpeedDifference = trueSpeedDifference + mySpeedDifferenceErrorCoefficient * myError.getState() * trueGap;297const auto lastPerceivedSpeedDifference = myLastPerceivedSpeedDifference.find(objID);298if (lastPerceivedSpeedDifference == myLastPerceivedSpeedDifference.end()299|| fabs(perceivedSpeedDifference - lastPerceivedSpeedDifference->second) > mySpeedDifferenceChangePerceptionThreshold * trueGap * (1.0 - myAwareness)) {300301#ifdef DEBUG_PERCEPTION_ERRORS302if (DEBUG_COND) {303if (!debugLocked()) {304std::cout << " new perceived speed difference (=" << perceivedSpeedDifference << ") differs significantly from the last perceived (="305<< (lastPerceivedSpeedDifference == myLastPerceivedSpeedDifference.end() ? "NA" : toString(lastPerceivedSpeedDifference->second)) << ")"306<< std::endl;307}308}309#endif310311// new perceived speed difference differs significantly from the previous312myLastPerceivedSpeedDifference[objID] = perceivedSpeedDifference;313return perceivedSpeedDifference;314} else {315#ifdef DEBUG_PERCEPTION_ERRORS316if (!debugLocked()) {317std::cout << " new perceived speed difference (=" << perceivedSpeedDifference << ") does *not* differ significantly from the last perceived (="318<< (lastPerceivedSpeedDifference->second) << ")" << std::endl;319}320#endif321// new perceived speed difference doesn't differ significantly from the previous322return lastPerceivedSpeedDifference->second;323}324}325326327//MSDriverState::MSTrafficItem::MSTrafficItem(MSTrafficItemType type, const std::string& id, std::shared_ptr<MSTrafficItemCharacteristics> data) :328// type(type),329// id_hash(hash(id)),330// data(data),331// remainingIntegrationTime(0.),332// integrationDemand(0.),333// latentDemand(0.)334//{}335//336//MSDriverState::MSDriverState(MSVehicle* veh) :337// myVehicle(veh),338// myMinTaskCapability(TCIDefaults::myMinTaskCapability),339// myMaxTaskCapability(TCIDefaults::myMaxTaskCapability),340// myMaxTaskDemand(TCIDefaults::myMaxTaskDemand),341// myMaxDifficulty(TCIDefaults::myMaxDifficulty),342// mySubCriticalDifficultyCoefficient(TCIDefaults::mySubCriticalDifficultyCoefficient),343// mySuperCriticalDifficultyCoefficient(TCIDefaults::mySuperCriticalDifficultyCoefficient),344// myOppositeDirectionDrivingDemandFactor(TCIDefaults::myOppositeDirectionDrivingFactor),345// myHomeostasisDifficulty(TCIDefaults::myHomeostasisDifficulty),346// myCapabilityTimeScale(TCIDefaults::myCapabilityTimeScale),347// myAccelerationErrorTimeScaleCoefficient(TCIDefaults::myAccelerationErrorTimeScaleCoefficient),348// myAccelerationErrorNoiseIntensityCoefficient(TCIDefaults::myAccelerationErrorNoiseIntensityCoefficient),349// myActionStepLengthCoefficient(TCIDefaults::myActionStepLengthCoefficient),350// myMinActionStepLength(TCIDefaults::myMinActionStepLength),351// myMaxActionStepLength(TCIDefaults::myMaxActionStepLength),352// mySpeedPerceptionErrorTimeScaleCoefficient(TCIDefaults::mySpeedPerceptionErrorTimeScaleCoefficient),353// mySpeedPerceptionErrorNoiseIntensityCoefficient(TCIDefaults::mySpeedPerceptionErrorNoiseIntensityCoefficient),354// myHeadwayPerceptionErrorTimeScaleCoefficient(TCIDefaults::myHeadwayPerceptionErrorTimeScaleCoefficient),355// myHeadwayPerceptionErrorNoiseIntensityCoefficient(TCIDefaults::myHeadwayPerceptionErrorNoiseIntensityCoefficient),356// myAmOpposite(false),357// myAccelerationError(0., 1.,1.),358// myHeadwayPerceptionError(0., 1.,1.),359// mySpeedPerceptionError(0., 1.,1.),360// myTaskDemand(0.),361// myTaskCapability(myMaxTaskCapability),362// myCurrentDrivingDifficulty(myTaskDemand/myTaskCapability),363// myActionStepLength(TS),364// myStepDuration(TS),365// myLastUpdateTime(SIMTIME-TS),366// myCurrentSpeed(0.),367// myCurrentAcceleration(0.)368//{}369//370//371//void372//MSDriverState::updateStepDuration() {373// myStepDuration = SIMTIME - myLastUpdateTime;374// myLastUpdateTime = SIMTIME;375//}376//377//378//void379//MSDriverState::calculateDrivingDifficulty() {380// if (myAmOpposite) {381// myCurrentDrivingDifficulty = difficultyFunction(myOppositeDirectionDrivingDemandFactor*myTaskDemand/myTaskCapability);382// } else {383// myCurrentDrivingDifficulty = difficultyFunction(myTaskDemand/myTaskCapability);384// }385//}386//387//388//double389//MSDriverState::difficultyFunction(double demandCapabilityQuotient) const {390// double difficulty;391// if (demandCapabilityQuotient <= 1) {392// // demand does not exceed capability -> we are in the region for a slight ascend of difficulty393// difficulty = mySubCriticalDifficultyCoefficient*demandCapabilityQuotient;394// } else {395// // demand exceeds capability -> we are in the region for a steeper ascend of the effect of difficulty396// difficulty = mySubCriticalDifficultyCoefficient + (demandCapabilityQuotient - 1)*mySuperCriticalDifficultyCoefficient;397// }398// return MIN2(myMaxDifficulty, difficulty);399//}400//401//402//void403//MSDriverState::adaptTaskCapability() {404// myTaskCapability = myTaskCapability + myCapabilityTimeScale*myStepDuration*(myTaskDemand - myHomeostasisDifficulty*myTaskCapability);405//}406//407//408//void409//MSDriverState::updateAccelerationError() {410//#ifdef DEBUG_OUPROCESS411// if (DEBUG_COND) {412// std::cout << SIMTIME << " Updating acceleration error (for " << myStepDuration << " s.):\n "413// << myAccelerationError.getState() << " -> ";414// }415//#endif416//417// updateErrorProcess(myAccelerationError, myAccelerationErrorTimeScaleCoefficient, myAccelerationErrorNoiseIntensityCoefficient);418//419//#ifdef DEBUG_OUPROCESS420// if (DEBUG_COND) {421// std::cout << myAccelerationError.getState() << std::endl;422// }423//#endif424//}425//426//void427//MSDriverState::updateSpeedPerceptionError() {428//#ifdef DEBUG_OUPROCESS429// if (DEBUG_COND) {430// std::cout << SIMTIME << " Updating speed perception error (for " << myStepDuration << " s.):\n "431// << mySpeedPerceptionError.getState() << " -> ";432// }433//#endif434//435// updateErrorProcess(mySpeedPerceptionError, mySpeedPerceptionErrorTimeScaleCoefficient, mySpeedPerceptionErrorNoiseIntensityCoefficient);436//437//#ifdef DEBUG_OUPROCESS438// if (DEBUG_COND) {439// std::cout << mySpeedPerceptionError.getState() << std::endl;440// }441//#endif442//}443//444//void445//MSDriverState::updateHeadwayPerceptionError() {446//#ifdef DEBUG_OUPROCESS447// if (DEBUG_COND) {448// std::cout << SIMTIME << " Updating headway perception error (for " << myStepDuration << " s.):\n "449// << myHeadwayPerceptionError.getState() << " -> ";450// }451//#endif452//453// updateErrorProcess(myHeadwayPerceptionError, myHeadwayPerceptionErrorTimeScaleCoefficient, myHeadwayPerceptionErrorNoiseIntensityCoefficient);454//455//#ifdef DEBUG_OUPROCESS456// if (DEBUG_COND) {457// std::cout << myHeadwayPerceptionError.getState() << std::endl;458// }459//#endif460//}461//462//void463//MSDriverState::updateActionStepLength() {464//#ifdef DEBUG_OUPROCESS465// if (DEBUG_COND) {466// std::cout << SIMTIME << " Updating action step length (for " << myStepDuration << " s.): \n" << myActionStepLength;467// }468//#endif469// if (myActionStepLengthCoefficient*myCurrentDrivingDifficulty <= myMinActionStepLength) {470// myActionStepLength = myMinActionStepLength;471// } else {472// myActionStepLength = MIN2(myActionStepLengthCoefficient*myCurrentDrivingDifficulty - myMinActionStepLength, myMaxActionStepLength);473// }474//#ifdef DEBUG_OUPROCESS475// if (DEBUG_COND) {476// std::cout << " -> " << myActionStepLength << std::endl;477// }478//#endif479//}480//481//482//void483//MSDriverState::updateErrorProcess(OUProcess& errorProcess, double timeScaleCoefficient, double noiseIntensityCoefficient) const {484// if (myCurrentDrivingDifficulty == 0) {485// errorProcess.setState(0.);486// } else {487// errorProcess.setTimeScale(timeScaleCoefficient/myCurrentDrivingDifficulty);488// errorProcess.setNoiseIntensity(myCurrentDrivingDifficulty*noiseIntensityCoefficient);489// errorProcess.step(myStepDuration);490// }491//}492//493//void494//MSDriverState::registerLeader(const MSVehicle* leader, double gap, double relativeSpeed, double latGap) {495// std::shared_ptr<MSTrafficItemCharacteristics> tic = std::dynamic_pointer_cast<MSTrafficItemCharacteristics>(std::make_shared<VehicleCharacteristics>(leader, gap, latGap, relativeSpeed));496// std::shared_ptr<MSTrafficItem> ti = std::make_shared<MSTrafficItem>(TRAFFIC_ITEM_VEHICLE, leader->getID(), tic);497// registerTrafficItem(ti);498//}499//500//void501//MSDriverState::registerPedestrian(const MSPerson* pedestrian, double gap) {502// std::shared_ptr<MSTrafficItemCharacteristics> tic = std::dynamic_pointer_cast<MSTrafficItemCharacteristics>(std::make_shared<PedestrianCharacteristics>(pedestrian, gap));503// std::shared_ptr<MSTrafficItem> ti = std::make_shared<MSTrafficItem>(TRAFFIC_ITEM_PEDESTRIAN, pedestrian->getID(), tic);504// registerTrafficItem(ti);505//}506//507//void508//MSDriverState::registerSpeedLimit(const MSLane* lane, double speedLimit, double dist) {509// std::shared_ptr<MSTrafficItemCharacteristics> tic = std::dynamic_pointer_cast<MSTrafficItemCharacteristics>(std::make_shared<SpeedLimitCharacteristics>(lane, dist, speedLimit));510// std::shared_ptr<MSTrafficItem> ti = std::make_shared<MSTrafficItem>(TRAFFIC_ITEM_SPEED_LIMIT, lane->getID(), tic);511// registerTrafficItem(ti);512//}513//514//void515//MSDriverState::registerJunction(MSLink* link, double dist) {516// const MSJunction* junction = link->getJunction();517// std::shared_ptr<MSTrafficItemCharacteristics> tic = std::dynamic_pointer_cast<MSTrafficItemCharacteristics>(std::make_shared<JunctionCharacteristics>(junction, link, dist));518// std::shared_ptr<MSTrafficItem> ti = std::make_shared<MSTrafficItem>(TRAFFIC_ITEM_JUNCTION, junction->getID(), tic);519// registerTrafficItem(ti);520//}521//522//void523//MSDriverState::registerEgoVehicleState() {524// myAmOpposite = myVehicle->getLaneChangeModel().isOpposite();525// myCurrentSpeed = myVehicle->getSpeed();526// myCurrentAcceleration = myVehicle->getAcceleration();527//}528//529//void530//MSDriverState::update() {531// // Adapt step duration532// updateStepDuration();533//534// // Replace traffic items from previous step with the newly encountered.535// myTrafficItems = myNewTrafficItems;536//537// // Iterate through present traffic items and take into account the corresponding538// // task demands. Further update the item's integration progress.539// for (auto& hashItemPair : myTrafficItems) {540// // Traffic item541// auto ti = hashItemPair.second;542// // Take into account the task demand associated with the item543// integrateDemand(ti);544// // Update integration progress545// if (ti->remainingIntegrationTime>0) {546// updateItemIntegration(ti);547// }548// }549//550// // Update capability (~attention) according to the changed demand551// // NOTE: Doing this before recalculating the errors seems more adequate552// // than after adjusting the errors, since a very fast time scale553// // for the capability could not be captured otherwise. A slow timescale554// // could still be tuned to have a desired effect.555// adaptTaskCapability();556//557// // Update driving difficulty558// calculateDrivingDifficulty();559//560// // Update errors561// updateAccelerationError();562// updateSpeedPerceptionError();563// updateHeadwayPerceptionError();564// updateActionStepLength();565//}566//567//568//void569//MSDriverState::integrateDemand(std::shared_ptr<MSTrafficItem> ti) {570// myMaxTaskDemand += ti->integrationDemand;571// myMaxTaskDemand += ti->latentDemand;572//}573//574//575//void576//MSDriverState::registerTrafficItem(std::shared_ptr<MSTrafficItem> ti) {577// if (myNewTrafficItems.find(ti->id_hash) == myNewTrafficItems.end()) {578//579// // Update demand associated with the item580// auto knownTiIt = myTrafficItems.find(ti->id_hash);581// if (knownTiIt == myTrafficItems.end()) {582// // new item --> init integration demand and latent task demand583// calculateIntegrationDemandAndTime(ti);584// } else {585// // known item --> only update latent task demand associated with the item586// ti = knownTiIt->second;587// }588// calculateLatentDemand(ti);589//590// // Track item591// myNewTrafficItems[ti->id_hash] = ti;592// }593//}594//595//596//void597//MSDriverState::updateItemIntegration(std::shared_ptr<MSTrafficItem> ti) const {598// // Eventually decrease integration time and take into account integration cost.599// ti->remainingIntegrationTime -= myStepDuration;600// if (ti->remainingIntegrationTime <= 0.) {601// ti->remainingIntegrationTime = 0.;602// ti->integrationDemand = 0.;603// }604//}605//606//607//void608//MSDriverState::calculateIntegrationDemandAndTime(std::shared_ptr<MSTrafficItem> ti) const {609// // @todo Idea is that the integration demand is the quantitatively the same for a specific610// // item type with definite characteristics but it can be stretched over time,611// // if the integration is less urgent (item farther away), thus resulting in612// // smaller effort for a longer time.613// switch (ti->type) {614// case TRAFFIC_ITEM_JUNCTION: {615// std::shared_ptr<JunctionCharacteristics> ch = std::dynamic_pointer_cast<JunctionCharacteristics>(ti->data);616// const double totalIntegrationDemand = calculateJunctionIntegrationDemand(ch);617// const double integrationTime = calculateIntegrationTime(ch->dist, myVehicle->getSpeed());618// ti->integrationDemand = totalIntegrationDemand/integrationTime;619// ti->remainingIntegrationTime = integrationTime;620// }621// break;622// case TRAFFIC_ITEM_PEDESTRIAN: {623// std::shared_ptr<PedestrianCharacteristics> ch = std::dynamic_pointer_cast<PedestrianCharacteristics>(ti->data);624// const double totalIntegrationDemand = calculatePedestrianIntegrationDemand(ch);625// const double integrationTime = calculateIntegrationTime(ch->dist, myVehicle->getSpeed());626// ti->integrationDemand = totalIntegrationDemand/integrationTime;627// ti->remainingIntegrationTime = integrationTime;628// }629// break;630// case TRAFFIC_ITEM_SPEED_LIMIT: {631// std::shared_ptr<SpeedLimitCharacteristics> ch = std::dynamic_pointer_cast<SpeedLimitCharacteristics>(ti->data);632// const double totalIntegrationDemand = calculateSpeedLimitIntegrationDemand(ch);633// const double integrationTime = calculateIntegrationTime(ch->dist, myVehicle->getSpeed());634// ti->integrationDemand = totalIntegrationDemand/integrationTime;635// ti->remainingIntegrationTime = integrationTime;636// }637// break;638// case TRAFFIC_ITEM_VEHICLE: {639// std::shared_ptr<VehicleCharacteristics> ch = std::dynamic_pointer_cast<VehicleCharacteristics>(ti->data);640// ti->latentDemand = calculateLatentVehicleDemand(ch);641// const double totalIntegrationDemand = calculateVehicleIntegrationDemand(ch);642// const double integrationTime = calculateIntegrationTime(ch->longitudinalDist, ch->relativeSpeed);643// ti->integrationDemand = totalIntegrationDemand/integrationTime;644// ti->remainingIntegrationTime = integrationTime;645// }646// break;647// default:648// WRITE_WARNING(TL("Unknown traffic item type!"))649// break;650// }651//}652//653//654//double655//MSDriverState::calculatePedestrianIntegrationDemand(std::shared_ptr<PedestrianCharacteristics> ch) const {656// // Integration demand for a pedestrian657// const double INTEGRATION_DEMAND_PEDESTRIAN = 0.5;658// return INTEGRATION_DEMAND_PEDESTRIAN;659//}660//661//662//double663//MSDriverState::calculateSpeedLimitIntegrationDemand(std::shared_ptr<SpeedLimitCharacteristics> ch) const {664// // Integration demand for speed limit665// const double INTEGRATION_DEMAND_SPEEDLIMIT = 0.1;666// return INTEGRATION_DEMAND_SPEEDLIMIT;667//}668//669//670//double671//MSDriverState::calculateJunctionIntegrationDemand(std::shared_ptr<JunctionCharacteristics> ch) const {672// // Latent demand for junction is proportional to number of conflicting lanes673// // for the vehicle's path plus a factor for the total number of incoming lanes674// // at the junction. Further, the distance to the junction is inversely proportional675// // to the induced demand [~1/(c*dist + 1)].676// // Traffic lights induce an additional demand677// const MSJunction* j = ch->junction;678//679// // Basic junction integration demand680// const double INTEGRATION_DEMAND_JUNCTION_BASE = 0.3;681//682// // Surplus integration demands683// const double INTEGRATION_DEMAND_JUNCTION_TLS = 0.2;684// const double INTEGRATION_DEMAND_JUNCTION_FOE_LANE = 0.3; // per foe lane685// const double INTEGRATION_DEMAND_JUNCTION_LANE = 0.1; // per lane686// const double INTEGRATION_DEMAND_JUNCTION_RAIL = 0.2;687// const double INTEGRATION_DEMAND_JUNCTION_ZIPPER = 0.3;688//689// double result = INTEGRATION_DEMAND_JUNCTION_BASE;690//// LinkState linkState = ch->approachingLink->getState();691// switch (ch->junction->getType()) {692// case SumoXMLNodeType::NOJUNCTION:693// case SumoXMLNodeType::UNKNOWN:694// case SumoXMLNodeType::DISTRICT:695// case SumoXMLNodeType::DEAD_END:696// case SumoXMLNodeType::DEAD_END_DEPRECATED:697// case SumoXMLNodeType::RAIL_SIGNAL: {698// result = 0.;699// }700// break;701// case SumoXMLNodeType::RAIL_CROSSING: {702// result += INTEGRATION_DEMAND_JUNCTION_RAIL;703// }704// break;705// case SumoXMLNodeType::TRAFFIC_LIGHT:706// case SumoXMLNodeType::TRAFFIC_LIGHT_NOJUNCTION:707// case SumoXMLNodeType::TRAFFIC_LIGHT_RIGHT_ON_RED: {708// // TODO: Take into account traffic light state?709//// switch (linkState) {710//// case LINKSTATE_TL_GREEN_MAJOR:711//// case LINKSTATE_TL_GREEN_MINOR:712//// case LINKSTATE_TL_RED:713//// case LINKSTATE_TL_REDYELLOW:714//// case LINKSTATE_TL_YELLOW_MAJOR:715//// case LINKSTATE_TL_YELLOW_MINOR:716//// case LINKSTATE_TL_OFF_BLINKING:717//// case LINKSTATE_TL_OFF_NOSIGNAL:718//// default:719//// }720// result += INTEGRATION_DEMAND_JUNCTION_TLS;721// }722// // no break. TLS has extra integration demand.723// case SumoXMLNodeType::PRIORITY:724// case SumoXMLNodeType::PRIORITY_STOP:725// case SumoXMLNodeType::RIGHT_BEFORE_LEFT:726// case SumoXMLNodeType::ALLWAY_STOP:727// case SumoXMLNodeType::INTERNAL: {728// // TODO: Consider link type (major or minor...)729// double junctionComplexity = (INTEGRATION_DEMAND_JUNCTION_LANE*j->getNrOfIncomingLanes()730// + INTEGRATION_DEMAND_JUNCTION_FOE_LANE*j->getFoeLinks(ch->approachingLink).size());731// result += junctionComplexity;732// }733// break;734// case SumoXMLNodeType::ZIPPER: {735// result += INTEGRATION_DEMAND_JUNCTION_ZIPPER;736// }737// break;738// default:739// assert(false);740// result = 0.;741// }742// return result;743//744//}745//746//747//double748//MSDriverState::calculateVehicleIntegrationDemand(std::shared_ptr<VehicleCharacteristics> ch) const {749// // TODO750// return 0.;751//}752//753//754//double755//MSDriverState::calculateIntegrationTime(double dist, double speed) const {756// // Fraction of encounter time, which is accounted for the corresponding traffic item's integration757// const double INTEGRATION_TIME_COEFF = 0.5;758// // Maximal time to be accounted for integration759// const double MAX_INTEGRATION_TIME = 5.;760// if (speed <= 0.) {761// return MAX_INTEGRATION_TIME;762// } else {763// return MIN2(MAX_INTEGRATION_TIME, INTEGRATION_TIME_COEFF*dist/speed);764// }765//}766//767//768//void769//MSDriverState::calculateLatentDemand(std::shared_ptr<MSTrafficItem> ti) const {770// switch (ti->type) {771// case TRAFFIC_ITEM_JUNCTION: {772// std::shared_ptr<JunctionCharacteristics> ch = std::dynamic_pointer_cast<JunctionCharacteristics>(ti->data);773// ti->latentDemand = calculateLatentJunctionDemand(ch);774// }775// break;776// case TRAFFIC_ITEM_PEDESTRIAN: {777// std::shared_ptr<PedestrianCharacteristics> ch = std::dynamic_pointer_cast<PedestrianCharacteristics>(ti->data);778// ti->latentDemand = calculateLatentPedestrianDemand(ch);779// }780// break;781// case TRAFFIC_ITEM_SPEED_LIMIT: {782// std::shared_ptr<SpeedLimitCharacteristics> ch = std::dynamic_pointer_cast<SpeedLimitCharacteristics>(ti->data);783// ti->latentDemand = calculateLatentSpeedLimitDemand(ch);784// }785// break;786// case TRAFFIC_ITEM_VEHICLE: {787// std::shared_ptr<VehicleCharacteristics> ch = std::dynamic_pointer_cast<VehicleCharacteristics>(ti->data);788// ti->latentDemand = calculateLatentVehicleDemand(ch);789// }790// break;791// default:792// WRITE_WARNING(TL("Unknown traffic item type!"))793// break;794// }795//}796//797//798//double799//MSDriverState::calculateLatentPedestrianDemand(std::shared_ptr<PedestrianCharacteristics> ch) const {800// // Latent demand for pedestrian is proportional to the euclidean distance to the801// // pedestrian (i.e. its potential to 'jump in front of the car) [~1/(c*dist + 1)]802// const double LATENT_DEMAND_COEFF_PEDESTRIAN_DIST = 0.1;803// const double LATENT_DEMAND_COEFF_PEDESTRIAN = 0.5;804// double result = LATENT_DEMAND_COEFF_PEDESTRIAN/(1. + LATENT_DEMAND_COEFF_PEDESTRIAN_DIST*ch->dist);805// return result;806//}807//808//809//double810//MSDriverState::calculateLatentSpeedLimitDemand(std::shared_ptr<SpeedLimitCharacteristics> ch) const {811// // Latent demand for speed limit is proportional to speed difference to current vehicle speed812// // during approach [~c*(1+deltaV) if dist<threshold].813// const double LATENT_DEMAND_COEFF_SPEEDLIMIT_TIME_THRESH = 5;814// const double LATENT_DEMAND_COEFF_SPEEDLIMIT = 0.1;815// double dist_thresh = LATENT_DEMAND_COEFF_SPEEDLIMIT_TIME_THRESH*myVehicle->getSpeed();816// double result = 0.;817// if (ch->dist <= dist_thresh && myVehicle->getSpeed() > ch->limit*myVehicle->getChosenSpeedFactor()) {818// // Upcoming speed limit does require a slowdown and is close enough.819// double dv = myVehicle->getSpeed() - ch->limit*myVehicle->getChosenSpeedFactor();820// result = LATENT_DEMAND_COEFF_SPEEDLIMIT*(1 + dv);821// }822// return result;823//}824//825//826//double827//MSDriverState::calculateLatentVehicleDemand(std::shared_ptr<VehicleCharacteristics> ch) const {828//829//830// // TODO831//832//833// // Latent demand for neighboring vehicle is determined from the relative and absolute speed,834// // and from the lateral and longitudinal distance.835// double result = 0.;836// const MSVehicle* foe = ch->foe;837// if (foe->getEdge() == myVehicle->getEdge()) {838// // on same edge839// } else if (foe->getEdge() == myVehicle->getEdge()->getOppositeEdge()) {840// // on opposite edges841// }842// return result;843//}844//845//846//847//double848//MSDriverState::calculateLatentJunctionDemand(std::shared_ptr<JunctionCharacteristics> ch) const {849// // Latent demand for junction is proportional to number of conflicting lanes850// // for the vehicle's path plus a factor for the total number of incoming lanes851// // at the junction. Further, the distance to the junction is inversely proportional852// // to the induced demand [~1/(c*dist + 1)].853// // Traffic lights induce an additional demand854// const MSJunction* j = ch->junction;855// const double LATENT_DEMAND_COEFF_JUNCTION_TIME_DIST_THRESH = 5; // seconds till arrival, below which junction is relevant856// const double LATENT_DEMAND_COEFF_JUNCTION_INCOMING = 0.1;857// const double LATENT_DEMAND_COEFF_JUNCTION_FOES = 0.5;858// const double LATENT_DEMAND_COEFF_JUNCTION_DIST = 0.1;859//860// double v = myVehicle->getSpeed();861// double dist_thresh = LATENT_DEMAND_COEFF_JUNCTION_TIME_DIST_THRESH*v;862//863// if (ch->dist > dist_thresh) {864// return 0.;865// }866// double result = 0.;867// LinkState linkState = ch->approachingLink->getState();868// switch (ch->junction->getType()) {869// case SumoXMLNodeType::NOJUNCTION:870// case SumoXMLNodeType::UNKNOWN:871// case SumoXMLNodeType::DISTRICT:872// case SumoXMLNodeType::DEAD_END:873// case SumoXMLNodeType::DEAD_END_DEPRECATED:874// case SumoXMLNodeType::RAIL_SIGNAL: {875// result = 0.;876// }877// break;878// case SumoXMLNodeType::RAIL_CROSSING: {879// result = 0.5;880// }881// break;882// case SumoXMLNodeType::TRAFFIC_LIGHT:883// case SumoXMLNodeType::TRAFFIC_LIGHT_NOJUNCTION:884// case SumoXMLNodeType::TRAFFIC_LIGHT_RIGHT_ON_RED: {885// // Take into account traffic light state886// switch (linkState) {887// case LINKSTATE_TL_GREEN_MAJOR:888// result = 0;889// break;890// case LINKSTATE_TL_GREEN_MINOR:891// result = 0.2*(1. + 0.1*v);892// break;893// case LINKSTATE_TL_RED:894// result = 0.1*(1. + 0.1*v);895// break;896// case LINKSTATE_TL_REDYELLOW:897// result = 0.2*(1. + 0.1*v);898// break;899// case LINKSTATE_TL_YELLOW_MAJOR:900// result = 0.1*(1. + 0.1*v);901// break;902// case LINKSTATE_TL_YELLOW_MINOR:903// result = 0.2*(1. + 0.1*v);904// break;905// case LINKSTATE_TL_OFF_BLINKING:906// result = 0.3*(1. + 0.1*v);907// break;908// case LINKSTATE_TL_OFF_NOSIGNAL:909// result = 0.2*(1. + 0.1*v);910// }911// }912// // no break, TLS is accounted extra913// case SumoXMLNodeType::PRIORITY:914// case SumoXMLNodeType::PRIORITY_STOP:915// case SumoXMLNodeType::RIGHT_BEFORE_LEFT:916// case SumoXMLNodeType::ALLWAY_STOP:917// case SumoXMLNodeType::INTERNAL: {918// // TODO: Consider link type (major or minor...)919// double junctionComplexity = (LATENT_DEMAND_COEFF_JUNCTION_INCOMING*j->getNrOfIncomingLanes()920// + LATENT_DEMAND_COEFF_JUNCTION_FOES*j->getFoeLinks(ch->approachingLink).size())921// /(1 + ch->dist*LATENT_DEMAND_COEFF_JUNCTION_DIST);922// result += junctionComplexity;923// }924// break;925// case SumoXMLNodeType::ZIPPER: {926// result = 0.5*(1. + 0.1*v);927// }928// break;929// default:930// assert(false);931// result = 0.;932// }933// return result;934//}935//936937938/****************************************************************************/939940941