Skip to content

Commit

Permalink
fix #9891
Browse files Browse the repository at this point in the history
  • Loading branch information
namdre committed May 17, 2024
1 parent a1d96d8 commit d8467e3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/guisim/GUIVehicleControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ GUIVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {


void
GUIVehicleControl::deleteVehicle(SUMOVehicle* veh, bool discard) {
GUIVehicleControl::deleteVehicle(SUMOVehicle* veh, bool discard, bool wasKept) {
FXMutexLock locker(myLock);
MSVehicleControl::deleteVehicle(veh, discard);
MSVehicleControl::deleteVehicle(veh, discard, wasKept);
}


Expand Down
2 changes: 1 addition & 1 deletion src/guisim/GUIVehicleControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class GUIVehicleControl : public MSVehicleControl {
* @param[in] v The vehicle to delete
* @param[discard] Whether the vehicle is discard during loading (scale < 1)
*/
void deleteVehicle(SUMOVehicle* v, bool discard = false) override;
void deleteVehicle(SUMOVehicle* v, bool discard = false, bool wasKept = false) override;


/** @brief Returns the list of all known vehicles by gl-id
Expand Down
4 changes: 2 additions & 2 deletions src/mesogui/GUIMEVehicleControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ GUIMEVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) {


void
GUIMEVehicleControl::deleteVehicle(SUMOVehicle* veh, bool discard) {
GUIMEVehicleControl::deleteVehicle(SUMOVehicle* veh, bool discard, bool wasKept) {
FXMutexLock locker(myLock);
MEVehicleControl::deleteVehicle(veh, discard);
MEVehicleControl::deleteVehicle(veh, discard, wasKept);
}


Expand Down
2 changes: 1 addition & 1 deletion src/mesogui/GUIMEVehicleControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class GUIMEVehicleControl : public MEVehicleControl {
* @param[in] v The vehicle to delete
* @param[discard] Whether the vehicle is discard during loading (scale < 1)
*/
void deleteVehicle(SUMOVehicle* v, bool discard = false) override;
void deleteVehicle(SUMOVehicle* v, bool discard = false, bool wasKept = false) override;

/** @brief Returns the list of all known vehicles by gl-id
* @param[fill] into The list to fill with vehicle ids
Expand Down
3 changes: 3 additions & 0 deletions src/microsim/MSFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ MSFrame::fillOptions() {
oc.doRegister("collision.mingap-factor", new Option_Float(-1));
oc.addDescription("collision.mingap-factor", "Processing", TL("Sets the fraction of minGap that must be maintained to avoid collision detection. If a negative value is given, the carFollowModel parameter is used"));

oc.doRegister("keep-after-arrival", new Option_String("0", "TIME"));
oc.addDescription("keep-after-arrival", "Processing", TL("After a vehicle arrives, keep it in memory for the given TIME (for TraCI access)"));

oc.doRegister("max-num-vehicles", new Option_Integer(-1));
oc.addDescription("max-num-vehicles", "Processing", TL("Delay vehicle insertion to stay within the given maximum number"));

Expand Down
28 changes: 23 additions & 5 deletions src/microsim/MSVehicleControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "MSEdge.h"
#include "MSNet.h"
#include "MSRouteHandler.h"
#include "MSEventControl.h"
#include "MSStop.h"
#include <microsim/devices/MSVehicleDevice.h>
#include <microsim/devices/MSDevice_Tripinfo.h>
Expand Down Expand Up @@ -65,6 +66,7 @@ MSVehicleControl::MSVehicleControl() :

initDefaultTypes();
myScale = OptionsCont::getOptions().getFloat("scale");
myKeepTime = string2time(OptionsCont::getOptions().getString("keep-after-arrival"));
}


Expand Down Expand Up @@ -171,7 +173,12 @@ MSVehicleControl::removePending() {
// close tag after tripinfo (possibly including emissions from another device) have been written
tripinfoOut->closeTag();
}
deleteVehicle(veh);
if (myKeepTime == 0) {
deleteVehicle(veh);
} else {
myEndedVehNo++;
MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(new DeleteKeptVehicle(veh), SIMSTEP + myKeepTime);
}
}
vehs.clear();
if (tripinfoOut != nullptr) {
Expand Down Expand Up @@ -332,10 +339,12 @@ MSVehicleControl::getVehicle(const std::string& id) const {


void
MSVehicleControl::deleteVehicle(SUMOVehicle* veh, bool discard) {
myEndedVehNo++;
if (discard) {
myDiscarded++;
MSVehicleControl::deleteVehicle(SUMOVehicle* veh, bool discard, bool wasKept) {
if (!wasKept) {
myEndedVehNo++;
if (discard) {
myDiscarded++;
}
}
if (veh != nullptr) {
myVehicleDict.erase(veh->getID());
Expand Down Expand Up @@ -573,5 +582,14 @@ MSVehicleControl::adaptIntermodalRouter(MSTransportableRouter& router) const {
}
}

// ===========================================================================
// MSVehicleControl::DeleteKeptVehicle method definitions
// ===========================================================================

SUMOTime
MSVehicleControl::DeleteKeptVehicle::execute(SUMOTime /*currentTime*/) {
MSNet::getInstance()->getVehicleControl().deleteVehicle(myVehicle, false, true);
return 0;
}

/****************************************************************************/
17 changes: 16 additions & 1 deletion src/microsim/MSVehicleControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <utils/distribution/RandomDistributor.h>
#include <utils/common/SUMOTime.h>
#include <utils/common/SUMOVehicleClass.h>
#include <utils/common/Command.h>
#include <microsim/MSRouterDefs.h>


Expand Down Expand Up @@ -139,7 +140,7 @@ class MSVehicleControl {
* @param[discard] Whether the vehicle is discard during loading (scale < 1)
* @todo Isn't this quite insecure?
*/
virtual void deleteVehicle(SUMOVehicle* v, bool discard = false);
virtual void deleteVehicle(SUMOVehicle* v, bool discard = false, bool wasKept = false);

void fixVehicleCounts() {
myLoadedVehNo++;
Expand Down Expand Up @@ -636,6 +637,18 @@ class MSVehicleControl {


private:
class DeleteKeptVehicle : public Command {
public:
DeleteKeptVehicle(SUMOVehicle* veh) : myVehicle(veh) {};
~DeleteKeptVehicle() {};
SUMOTime execute(SUMOTime currentTime);
private:
SUMOVehicle* myVehicle;
private:
/// @brief Invalidated assignment operator.
DeleteKeptVehicle& operator=(const DeleteKeptVehicle&) = delete;
};

/// @name Vehicle type container
/// @{

Expand All @@ -661,6 +674,8 @@ class MSVehicleControl {
/// @brief The scaling factor (especially for inc-dua)
double myScale;

SUMOTime myKeepTime;

/// @brief The maximum speed factor for all vehicles in the network
double myMaxSpeedFactor;

Expand Down
10 changes: 10 additions & 0 deletions src/microsim/devices/MSDevice_Tripinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,16 @@ MSDevice_Tripinfo::getParameter(const std::string& key) const {
return toString(myWaitingCount);
} else if (key == toString(SUMO_ATTR_STOPTIME)) {
return toString(STEPS2TIME(myStoppingTime));
} else if (key == toString(SUMO_ATTR_ARRIVALTIME)) {
return toString(STEPS2TIME(myArrivalTime));
} else if (key == toString(SUMO_ATTR_ARRIVALLANE)) {
return toString(myArrivalLane);
} else if (key == toString(SUMO_ATTR_ARRIVALPOS)) {
return toString(myArrivalPos);
} else if (key == toString(SUMO_ATTR_ARRIVALPOS_LAT)) {
return toString(myArrivalPosLat);
} else if (key == toString(SUMO_ATTR_ARRIVALSPEED)) {
return toString(myArrivalSpeed);
}
throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
}
Expand Down

0 comments on commit d8467e3

Please sign in to comment.