Skip to content

Commit

Permalink
refactoring refs #12, preparing #14823
Browse files Browse the repository at this point in the history
  • Loading branch information
namdre committed May 5, 2024
1 parent 62a432e commit a7ccca7
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/microsim/transportables/MSPerson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ MSPerson::MSPersonStage_Access::tripInfoOutput(OutputDevice& os, const MSTranspo
os.writeAttr("stop", getDestinationStop()->getID());
os.writeAttr("depart", time2string(myDeparted));
os.writeAttr("arrival", myArrived >= 0 ? time2string(myArrived) : "-1");
os.writeAttr("duration", myArrived > 0 ? time2string(myArrived - myDeparted) : "-1");
os.writeAttr("duration", myArrived > 0 ? time2string(getDuration()) : "-1");
os.writeAttr("routeLength", myDist);
os.closeTag();
}
Expand Down
23 changes: 23 additions & 0 deletions src/microsim/transportables/MSStage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ MSStage::getArrived() const {
return myArrived;
}


SUMOTime
MSStage::getDuration() const {
return myArrived > 0 ? myArrived - myDeparted : SUMOTime_MAX;
}


SUMOTime
MSStage::getTravelTime() const {
return getDuration();
}

SUMOTime
MSStage::getWaitingTime() const {
return 0;
}

SUMOTime
MSStage::getTimeLoss(const MSTransportable* /*transportable*/) const {
return 0;
}


const std::string
MSStage::setArrived(MSNet* /* net */, MSTransportable* /* transportable */, SUMOTime now, const bool /* vehicleArrived */) {
myArrived = now;
Expand Down
5 changes: 5 additions & 0 deletions src/microsim/transportables/MSStage.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ class MSStage : public Parameterised {
/// get arrival time of stage
SUMOTime getArrived() const;

virtual SUMOTime getTimeLoss(const MSTransportable* transportable) const;
virtual SUMOTime getDuration() const;
virtual SUMOTime getTravelTime() const;
virtual SUMOTime getWaitingTime() const;

/// logs end of the step
void setDeparted(SUMOTime now);

Expand Down
18 changes: 15 additions & 3 deletions src/microsim/transportables/MSStageDriving.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,23 +335,35 @@ MSStageDriving::registerWaiting(MSTransportable* transportable, SUMOTime now) {
}


SUMOTime
MSStageDriving::getWaitingTime() const {
const SUMOTime departed = myDeparted >= 0 ? myDeparted : SIMSTEP;
return myWaitingSince >= 0 ? departed - myWaitingSince : SUMOTime_MAX;
}


SUMOTime
MSStageDriving::getTimeLoss(const MSTransportable* /*transportable*/) const {
return myArrived >= 0 ? myTimeLoss : SUMOTime_MAX;
}

void
MSStageDriving::tripInfoOutput(OutputDevice& os, const MSTransportable* const transportable) const {
const SUMOTime now = MSNet::getInstance()->getCurrentTimeStep();
const SUMOTime departed = myDeparted >= 0 ? myDeparted : now;
const SUMOTime waitingTime = myWaitingSince >= 0 ? departed - myWaitingSince : -1;
const SUMOTime waitingTime = getWaitingTime();
const SUMOTime duration = myArrived - myDeparted;
MSDevice_Tripinfo::addRideTransportData(transportable->isPerson(), myVehicleDistance, duration, myVehicleVClass, myVehicleLine, waitingTime);
os.openTag(transportable->isPerson() ? "ride" : "transport");
os.writeAttr("waitingTime", waitingTime >= 0 ? time2string(waitingTime) : "-1");
os.writeAttr("waitingTime", waitingTime != SUMOTime_MAX ? time2string(waitingTime) : "-1");
os.writeAttr("vehicle", myVehicleID);
os.writeAttr("depart", myDeparted >= 0 ? time2string(myDeparted) : "-1");
os.writeAttr("arrival", myArrived >= 0 ? time2string(myArrived) : "-1");
os.writeAttr("arrivalPos", myArrived >= 0 ? toString(getArrivalPos()) : "-1");
os.writeAttr("duration", myArrived >= 0 ? time2string(duration) :
(myDeparted >= 0 ? time2string(now - myDeparted) : "-1"));
os.writeAttr("routeLength", myArrived >= 0 || myVehicle != nullptr ? toString(getDistance()) : "-1");
os.writeAttr("timeLoss", myArrived >= 0 ? time2string(myTimeLoss) : "-1");
os.writeAttr("timeLoss", myArrived >= 0 ? time2string(getTimeLoss(transportable)) : "-1");
os.closeTag();
}

Expand Down
7 changes: 5 additions & 2 deletions src/microsim/transportables/MSStageDriving.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class MSStageDriving : public MSStage {
/// @brief checks whether the person may exit at the current vehicle position
bool canLeaveVehicle(const MSTransportable* t, const SUMOVehicle& veh, const MSStop& stop);

SUMOTime getTimeLoss(const MSTransportable* transportable) const;
SUMOTime getWaitingTime() const;

/** @brief Saves the current state into the given stream
*/
void saveState(std::ostringstream& out);
Expand Down Expand Up @@ -213,7 +216,7 @@ class MSStageDriving : public MSStage {

std::string myIntendedVehicleID;
SUMOTime myIntendedDepart;


private:
/// brief register waiting person (on proceed or loadState)
Expand All @@ -232,7 +235,7 @@ class MSStageDriving : public MSStage {
BookReservation(MSTransportable* transportable, SUMOTime earliestPickupTime, MSStageDriving* stage) :
myTransportable(transportable), myEarliestPickupTime(earliestPickupTime), myStage(stage), myWaitingPos(stage->myWaitingPos) {}
SUMOTime execute(SUMOTime currentTime);

public:
MSTransportable* myTransportable;
SUMOTime myEarliestPickupTime;
Expand Down
2 changes: 1 addition & 1 deletion src/microsim/transportables/MSStageTranship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ MSStageTranship::tripInfoOutput(OutputDevice& os, const MSTransportable* const)
os.writeAttr("departPos", myDepartPos);
os.writeAttr("arrival", time2string(myArrived));
os.writeAttr("arrivalPos", myArrivalPos);
os.writeAttr("duration", myArrived >= 0 ? time2string(myArrived - myDeparted) : "-1");
os.writeAttr("duration", myArrived >= 0 ? time2string(getDuration()) : "-1");
os.writeAttr("routeLength", getDistance());
os.writeAttr("maxSpeed", mySpeed);
os.closeTag();
Expand Down
6 changes: 6 additions & 0 deletions src/microsim/transportables/MSStageTrip.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ class MSStageTrip : public MSStage {
UNUSED_PARAMETER(transportable);
}

/// @brief trip doesn't participate in plan summary
SUMOTime getTimeLoss() const { return 0; }
SUMOTime getDuration() const { return 0; }
SUMOTime getTravelTime() const { return 0; }
SUMOTime getWaitingTime() const { return 0; }

/** @brief Called on writing vehroute output
*
* @param[in] os The stream to write the information into
Expand Down
1 change: 0 additions & 1 deletion src/microsim/transportables/MSStageWaiting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ MSStageWaiting::getPlannedDuration() const {
return myWaitingDuration;
}


Position
MSStageWaiting::getPosition(SUMOTime /* now */) const {
if (myStopWaitPos != Position::INVALID) {
Expand Down
4 changes: 4 additions & 0 deletions src/microsim/transportables/MSStageWaiting.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class MSStageWaiting : public MSStage {

SUMOTime getPlannedDuration() const;

SUMOTime getTravelTime() const {
return 0;
}

SUMOTime getStopEnd() const {
return myStopEndTime;
}
Expand Down
17 changes: 12 additions & 5 deletions src/microsim/transportables/MSStageWalking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ MSStageWalking::walkDistance(bool partial) const {
}


SUMOTime
MSStageWalking::getTimeLoss(const MSTransportable* transportable) const {
SUMOTime timeLoss = myArrived == -1 ? 0 : getDuration() - TIME2STEPS(walkDistance(true) / getMaxSpeed(transportable));
if (timeLoss < 0 && timeLoss > TIME2STEPS(-0.1)) {
// avoid negative timeLoss due to rounding errors
timeLoss = 0;
}
return timeLoss;
}


void
MSStageWalking::tripInfoOutput(OutputDevice& os, const MSTransportable* const person) const {
if (!myWarnedInvalidTripinfo && MSNet::getInstance()->getPersonControl().getMovementModel()->usingShortcuts()) {
Expand All @@ -286,11 +297,7 @@ MSStageWalking::tripInfoOutput(OutputDevice& os, const MSTransportable* const pe
const double distance = walkDistance(true);
const double maxSpeed = getMaxSpeed(person);
const SUMOTime duration = myArrived - myDeparted;
SUMOTime timeLoss = myArrived == -1 ? 0 : duration - TIME2STEPS(distance / maxSpeed);
if (timeLoss < 0 && timeLoss > TIME2STEPS(-0.1)) {
// avoid negative timeLoss due to rounding errors
timeLoss = 0;
}
const SUMOTime timeLoss = getTimeLoss(person);
MSDevice_Tripinfo::addPedestrianData(distance, duration, timeLoss);
os.openTag("walk");
os.writeAttr("depart", myDeparted >= 0 ? time2string(myDeparted) : "-1");
Expand Down
2 changes: 2 additions & 0 deletions src/microsim/transportables/MSStageWalking.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class MSStageWalking : public MSStageMoving {
return true;
}

SUMOTime getTimeLoss(const MSTransportable* transportable) const;

private:
/// @brief compute total walking distance
double walkDistance(bool partial = false) const;
Expand Down
46 changes: 42 additions & 4 deletions src/microsim/transportables/MSTransportable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,50 @@ MSTransportable::getSpeed() const {
void
MSTransportable::tripInfoOutput(OutputDevice& os) const {
os.openTag(isPerson() ? "personinfo" : "containerinfo");
os.writeAttr("id", getID());
os.writeAttr("depart", time2string(getDesiredDepart()));
os.writeAttr("type", getVehicleType().getID());
os.writeAttr(SUMO_ATTR_ID, getID());
os.writeAttr(SUMO_ATTR_DEPART, time2string(getDesiredDepart()));
os.writeAttr(SUMO_ATTR_TYPE, getVehicleType().getID());
if (isPerson()) {
os.writeAttr("speedFactor", getChosenSpeedFactor());
os.writeAttr(SUMO_ATTR_SPEEDFACTOR, getChosenSpeedFactor());
}
SUMOTime duration = 0;
SUMOTime waitingTime = 0;
SUMOTime timeLoss = 0;
SUMOTime travelTime = 0;
bool durationOK = true;
bool waitingTimeOK = true;
bool timeLossOK = true;
bool travelTimeOK = true;
for (MSStage* const i : *myPlan) {
SUMOTime t = i->getDuration();
if (t != SUMOTime_MAX) {
duration += t;
} else {
durationOK = false;
}
t = i->getWaitingTime();
if (t != SUMOTime_MAX) {
waitingTime += t;
} else {
waitingTimeOK = false;
}
t = i->getTimeLoss(this);
if (t != SUMOTime_MAX) {
timeLoss += t;
} else {
timeLossOK = false;
}
t = i->getTravelTime();
if (t != SUMOTime_MAX) {
travelTime += t;
} else {
travelTimeOK = false;
}
}
//os.writeAttr(SUMO_ATTR_DURATION, durationOK ? time2string(duration) : "-1");
//os.writeAttr(SUMO_ATTR_WAITINGTIME, waitingTimeOK ? time2string(waitingTime) : "-1");
//os.writeAttr(SUMO_ATTR_TIMELOSS, timeLossOK ? time2string(timeLoss) : "-1");
//os.writeAttr(SUMO_ATTR_TRAVELTIME, travelTimeOK ? time2string(travelTime) : "-1");
for (MSStage* const i : *myPlan) {
i->tripInfoOutput(os, this);
}
Expand Down

0 comments on commit a7ccca7

Please sign in to comment.