Skip to content

Commit

Permalink
refactoring code into device
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Mar 22, 2024
1 parent e2711d6 commit 2da7c50
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
8 changes: 3 additions & 5 deletions src/microsim/MSVehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ MSVehicle::Influencer::gapControlSpeed(SUMOTime currentTime, const SUMOVehicle*
if DEBUG_COND2(veh) {
std::cout << " --- no refVeh; myGapControlState->addGapCurrent: " << myGapControlState->addGapCurrent << ", brakeGap: " << brakeGap << " in simstep: " << SIMSTEP << std::endl;
}
#endif
#endif
} else {
// Control gap wrt reference vehicle
const MSVehicle* leader = myGapControlState->referenceVeh;
Expand Down Expand Up @@ -991,8 +991,7 @@ MSVehicle::MSVehicle(SUMOVehicleParameter* pars, ConstMSRoutePtr route,
myJunctionEntryTimeNeverYield(SUMOTime_MAX),
myJunctionConflictEntryTime(SUMOTime_MAX),
myTimeSinceStartup(TIME2STEPS(3600 * 24)),
myInfluencer(nullptr),
myUnboardingPositions(new std::vector<Position>()) {
myInfluencer(nullptr) {
myCFVariables = type->getCarFollowModel().createVehicleVariables();
myNextDriveItem = myLFLinkLanes.begin();
}
Expand All @@ -1006,7 +1005,6 @@ MSVehicle::~MSVehicle() {
}
delete myInfluencer;
delete myCFVariables;
delete myUnboardingPositions;
}


Expand Down Expand Up @@ -1920,7 +1918,7 @@ MSVehicle::boardTransportables(MSStop& stop) {
stop.containerTriggered = false;
if (myAmRegisteredAsWaiting) {
unregister = true;
myAmRegisteredAsWaiting = false;
myAmRegisteredAsWaiting = false;
}
}
if (boarded) {
Expand Down
9 changes: 1 addition & 8 deletions src/microsim/MSVehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1725,10 +1725,6 @@ class MSVehicle : public MSBaseVehicle {
double dist, double leaveSpeed);
//@}

std::vector<Position>& getUnboardingPositions() const {
return *myUnboardingPositions;
}

protected:

/// @name Interaction with move reminders
Expand Down Expand Up @@ -1942,9 +1938,6 @@ class MSVehicle : public MSBaseVehicle {
/// @brief duration of driving (speed > SUMO_const_haltingSpeed) after the last halting episode
SUMOTime myTimeSinceStartup;

/// @brief unboarding positions of passengers if vehicle is a train
std::vector<Position>* const myUnboardingPositions;

protected:

/// @brief Drive process items represent bounds on the safe velocity
Expand Down Expand Up @@ -2063,7 +2056,7 @@ class MSVehicle : public MSBaseVehicle {
inline double accelThresholdForWaiting() const {
return 0.5 * getCarFollowModel().getMaxAccel();
}

/* @brief return the previous lane in this vehicles route including internal lanes
* @param[in] current The lane of which the predecessor should be returned
* @param[in,out] routeIndex The index of the current or previous non-internal edge in the route
Expand Down
9 changes: 7 additions & 2 deletions src/microsim/devices/MSDevice_Transportable.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class MSDevice_Transportable : public MSVehicleDevice {
static MSDevice_Transportable* buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into, const bool isContainer);



public:
/// @brief Destructor.
~MSDevice_Transportable();
Expand Down Expand Up @@ -149,6 +148,10 @@ class MSDevice_Transportable : public MSVehicleDevice {
return myTransportables;
}

std::vector<Position>& getUnboardingPositions() {
return myUnboardingPositions;
}

/// @brief check if boardingDuration should be applied
static bool willTransferAtJoin(const MSTransportable* t, const MSBaseVehicle* joinVeh);

Expand All @@ -174,7 +177,6 @@ class MSDevice_Transportable : public MSVehicleDevice {
MSDevice_Transportable(SUMOVehicle& holder, const std::string& id, const bool isContainer);



private:
/// @brief Whether it is a container device
const bool myAmContainer;
Expand All @@ -187,6 +189,9 @@ class MSDevice_Transportable : public MSVehicleDevice {
*/
bool myStopped;

/// @brief unboarding positions of passengers if vehicle is a train
std::vector<Position> myUnboardingPositions;


private:
/// @brief Invalidated copy constructor.
Expand Down
28 changes: 13 additions & 15 deletions src/microsim/transportables/MSStageDriving.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <microsim/MSVehicleType.h>
#include <microsim/devices/MSTransportableDevice.h>
#include <microsim/devices/MSDevice_Taxi.h>
#include <microsim/devices/MSDevice_Transportable.h>
#include <microsim/devices/MSDevice_Tripinfo.h>
#include <microsim/devices/MSDispatch.h>
#include <microsim/transportables/MSTransportableControl.h>
Expand All @@ -50,7 +51,7 @@
// method definitions
// ===========================================================================
MSStageDriving::MSStageDriving(const MSEdge* origin, const MSEdge* destination,
MSStoppingPlace* toStop, const double arrivalPos, const double arrivalPosLat,
MSStoppingPlace* toStop, const double arrivalPos, const double arrivalPosLat,
const std::vector<std::string>& lines, const std::string& group,
const std::string& intendedVeh, SUMOTime intendedDepart) :
MSStage(MSStageType::DRIVING, destination, toStop, arrivalPos, arrivalPosLat, group),
Expand Down Expand Up @@ -103,7 +104,7 @@ MSStageDriving::init(MSTransportable* transportable) {
myReservationCommand = new BookReservation(transportable, earliestPickupTime, this);
MSNet::getInstance()->getBeginOfTimestepEvents()->addEvent(myReservationCommand, reservationTime);
}


}

Expand Down Expand Up @@ -393,7 +394,7 @@ MSStageDriving::isWaitingFor(const SUMOVehicle* vehicle) const {
assert(myLines.size() > 0);
return (myLines.count(vehicle->getID()) > 0
|| ((myLines.count(vehicle->getParameter().line) > 0
|| myLines.count("ANY") > 0) &&
|| myLines.count("ANY") > 0) &&
// even if the line matches we still have to check for stops (#14526)
(myDestinationStop == nullptr
? vehicle->stopsAtEdge(myDestination)
Expand Down Expand Up @@ -466,10 +467,8 @@ MSStageDriving::setArrived(MSNet* net, MSTransportable* transportable, SUMOTime
MSVehicle* train = dynamic_cast<MSVehicle*>(myVehicle);
if (train != nullptr) {
MSTrainHelper trainHelper = MSTrainHelper(train);
const MSLane* myLane = myVehicle->getLane();
const OptionsCont& oc = OptionsCont::getOptions();
const std::string model = oc.getString("pedestrian.model");
if (model != "jupedsim") {
const MSLane* const lane = myVehicle->getLane();
if (OptionsCont::getOptions().getString("pedestrian.model") != "jupedsim") {
trainHelper.computeDoorPositions();
const std::vector<MSTrainHelper::Carriage*>& carriages = trainHelper.getCarriages();
const int randomCarriageIx = RandHelper::rand(trainHelper.getNumCarriages() - trainHelper.getFirstPassengerCarriage()) + trainHelper.getFirstPassengerCarriage();
Expand All @@ -481,21 +480,20 @@ MSStageDriving::setArrived(MSNet* net, MSTransportable* transportable, SUMOTime
direction.norm2D();
randomDoor.add(direction * RandHelper::rand(-0.5 * MSTrainHelper::CARRIAGE_DOOR_WIDTH, 0.5 * MSTrainHelper::CARRIAGE_DOOR_WIDTH));
// Project onto the lane.
myArrivalPos = myLane->getShape().nearest_offset_to_point2D(randomDoor);
myArrivalPos = myLane->interpolateGeometryPosToLanePos(myArrivalPos);
myArrivalPos = lane->getShape().nearest_offset_to_point2D(randomDoor);
myArrivalPos = lane->interpolateGeometryPosToLanePos(myArrivalPos);
myArrivalPos = MIN2(MAX2(0., myArrivalPos), myVehicle->getEdge()->getLength());
}
else {
std::vector<Position>& unboardingPositions = train->getUnboardingPositions();
} else {
std::vector<Position>& unboardingPositions = static_cast<MSDevice_Transportable*>(train->getDevice(typeid(MSDevice_Transportable)))->getUnboardingPositions();
if (unboardingPositions.empty()) {
const MSVehicleType* defaultPedestrianType = MSNet::getInstance()->getVehicleControl().getVType(DEFAULT_PEDTYPE_ID, nullptr, true);
const MSVehicleType* defaultPedestrianType = MSNet::getInstance()->getVehicleControl().getVType(DEFAULT_PEDTYPE_ID, nullptr, true);
const double defaultPassengerRadius = MAX2(defaultPedestrianType->getLength(), defaultPedestrianType->getWidth()) / 2.;
trainHelper.computeUnboardingPositions(defaultPassengerRadius, unboardingPositions);
}
// Random shuffling of the positions has already been done in the train helper.
const Position availableUnboardingPosition = unboardingPositions.back();
unboardingPositions.pop_back();
const Position arrivalPos = myLane->getShape().transformToVectorCoordinates(availableUnboardingPosition);
const Position arrivalPos = lane->getShape().transformToVectorCoordinates(availableUnboardingPosition);
myArrivalPos = arrivalPos.x();
myArrivalPosLat = arrivalPos.y();
}
Expand Down Expand Up @@ -642,7 +640,7 @@ MSStageDriving::loadState(MSTransportable* transportable, std::istringstream& st
SUMOTime
MSStageDriving::BookReservation::execute(SUMOTime currentTime) {
MSDevice_Taxi::addReservation(myTransportable, myStage->getLines(), currentTime, currentTime, myEarliestPickupTime,
myStage->myOrigin, myStage->myWaitingPos, myStage->getDestination(), myStage->getArrivalPos(), myStage->myGroup);
myStage->myOrigin, myStage->myWaitingPos, myStage->getDestination(), myStage->getArrivalPos(), myStage->myGroup);
// do not repeat if execution fails
return 0;
}
Expand Down
6 changes: 4 additions & 2 deletions src/utils/common/RandHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
/****************************************************************************/
#pragma once
#include <config.h>

#include <cassert>
#include <vector>
#include <map>
#include <random>
#include <sstream>
#include <iostream>
#include <algorithm>


// ===========================================================================
Expand Down Expand Up @@ -235,8 +237,8 @@ class RandHelper {
}

template<class T>
static void shuffle(std::vector<T>& v) {
std::shuffle(v.begin(), v.end(), myRandomNumberGenerator);
static void shuffle(std::vector<T>& v, SumoRNG* rng = nullptr) {
std::shuffle(v.begin(), v.end(), rng == nullptr ? myRandomNumberGenerator : *rng);
}

protected:
Expand Down

0 comments on commit 2da7c50

Please sign in to comment.