void MSBaseVehicle::reroute(SUMOTime t, SUMOAbstractRouter<MSEdge, SUMOVehicle>& router, const bool onInit, const bool withTaz) { // check whether to reroute const MSEdge* source = withTaz && onInit ? MSEdge::dictionary(myParameter->fromTaz + "-source") : getRerouteOrigin(); if (source == 0) { source = getRerouteOrigin(); } const MSEdge* sink = withTaz ? MSEdge::dictionary(myParameter->toTaz + "-sink") : myRoute->getLastEdge(); if (sink == 0) { sink = myRoute->getLastEdge(); } ConstMSEdgeVector edges; const ConstMSEdgeVector stops = getStopEdges(); for (MSRouteIterator s = stops.begin(); s != stops.end(); ++s) { if (*s != source) { // !!! need to adapt t here router.compute(source, *s, this, t, edges); source = *s; edges.pop_back(); } } router.compute(source, sink, this, t, edges); if (!edges.empty() && edges.front()->getPurpose() == MSEdge::EDGEFUNCTION_DISTRICT) { edges.erase(edges.begin()); } if (!edges.empty() && edges.back()->getPurpose() == MSEdge::EDGEFUNCTION_DISTRICT) { edges.pop_back(); } replaceRouteEdges(edges, onInit); }
void Person::rerouteTraveltime(const std::string& personID) { MSPerson* p = getPerson(personID); if (p->getNumRemainingStages() == 0) { throw TraCIException("Person '" + personID + "' has no remaining stages."); } const MSEdge* from = p->getEdge(); double departPos = p->getEdgePos(); // reroute to the start of the next-non-walking stage int firstIndex; if (p->getCurrentStageType() == MSTransportable::MOVING_WITHOUT_VEHICLE) { firstIndex = 0; } else if (p->getCurrentStageType() == MSTransportable::WAITING) { if (p->getNumRemainingStages() < 2 || p->getStageType(1) != MSTransportable::MOVING_WITHOUT_VEHICLE) { throw TraCIException("Person '" + personID + "' cannot reroute after the current stop."); } firstIndex = 1; } else { throw TraCIException("Person '" + personID + "' cannot reroute in stage type '" + toString(p->getCurrentStageType()) + "'."); } int nextIndex = firstIndex + 1; for (; nextIndex < p->getNumRemainingStages(); nextIndex++) { if (p->getStageType(nextIndex) != MSTransportable::MOVING_WITHOUT_VEHICLE) { break; } } MSTransportable::Stage* destStage = p->getNextStage(nextIndex - 1); const MSEdge* to = destStage->getEdges().back(); double arrivalPos = destStage->getArrivalPos(); double speed = p->getVehicleType().getMaxSpeed(); ConstMSEdgeVector newEdges; MSNet::getInstance()->getPedestrianRouter().compute(from, to, departPos, arrivalPos, speed, 0, nullptr, newEdges); if (newEdges.empty()) { throw TraCIException("Could not find new route for person '" + personID + "'."); } ConstMSEdgeVector oldEdges = p->getEdges(firstIndex); assert(!oldEdges.empty()); if (oldEdges.front()->getFunction() != EDGEFUNC_NORMAL) { oldEdges.erase(oldEdges.begin()); } //std::cout << " remainingStages=" << p->getNumRemainingStages() << " oldEdges=" << toString(oldEdges) << " newEdges=" << toString(newEdges) << " firstIndex=" << firstIndex << " nextIndex=" << nextIndex << "\n"; if (newEdges == oldEdges && (firstIndex + 1 == nextIndex)) { return; } if (newEdges.front() != from) { // @note: maybe this should be done automatically by the router newEdges.insert(newEdges.begin(), from); } p->reroute(newEdges, departPos, firstIndex, nextIndex); }
bool MSBaseVehicle::replaceRouteEdges(ConstMSEdgeVector& edges, bool onInit) { if (edges.empty()) { WRITE_WARNING("No route for vehicle '" + getID() + "' found."); return false; } // build a new id, first std::string id = getID(); if (id[0] != '!') { id = "!" + id; } if (myRoute->getID().find("!var#") != std::string::npos) { id = myRoute->getID().substr(0, myRoute->getID().rfind("!var#") + 5) + toString(getNumberReroutes() + 1); } else { id = id + "!var#1"; } int oldSize = (int)edges.size(); if (!onInit) { const MSEdge* const origin = getRerouteOrigin(); if (origin != *myCurrEdge && edges.front() == origin) { edges.insert(edges.begin(), *myCurrEdge); oldSize = (int)edges.size(); } edges.insert(edges.begin(), myRoute->begin(), myCurrEdge); } if (edges == myRoute->getEdges()) { if (onInit) { // if edges = 'from to' we still need to calculate the arrivalPos once calculateArrivalParams(); } return true; } const RGBColor& c = myRoute->getColor(); MSRoute* newRoute = new MSRoute(id, edges, false, &c == &RGBColor::DEFAULT_COLOR ? 0 : new RGBColor(c), myRoute->getStops()); if (!MSRoute::dictionary(id, newRoute)) { delete newRoute; return false; } if (!replaceRoute(newRoute, onInit, (int)edges.size() - oldSize)) { newRoute->addReference(); newRoute->release(); return false; } calculateArrivalParams(); return true; }
void GUIVehicle::rerouteDRTStop(MSStoppingPlace* busStop) { SUMOTime intermediateDuration = TIME2STEPS(20); SUMOTime finalDuration = SUMOTime_MAX; if (myParameter->stops.size() >= 2) { // copy durations from the original stops intermediateDuration = myParameter->stops.front().duration; finalDuration = myParameter->stops.back().duration; } // if the stop is already in the list of stops, cancel all stops that come // after it and set the stop duration std::string line = ""; int destinations = 0; bool add = true; for (auto it = myStops.begin(); it != myStops.end(); it++) { if (!it->reached && destinations < 2 && it->busstop != nullptr) { line += it->busstop->getID(); destinations++; } if (it->busstop == busStop) { it->duration = finalDuration; myStops.erase(++it, myStops.end()); add = false; break; } else { it->duration = MIN2(it->duration, intermediateDuration); } } if (destinations < 2) { line += busStop->getID(); } if (add) { // create new stop SUMOVehicleParameter::Stop stopPar; stopPar.busstop = busStop->getID(); stopPar.lane = busStop->getLane().getID(); stopPar.startPos = busStop->getBeginLanePosition(); stopPar.endPos = busStop->getEndLanePosition(); stopPar.duration = finalDuration; stopPar.until = -1; stopPar.triggered = false; stopPar.containerTriggered = false; stopPar.parking = false; stopPar.index = STOP_INDEX_FIT; stopPar.parametersSet = STOP_START_SET | STOP_END_SET; // clean up prior route to improve visualisation, ensure that the stop can be added immediately ConstMSEdgeVector edges = myRoute->getEdges(); edges.erase(edges.begin(), edges.begin() + getRoutePosition()); edges.push_back(&busStop->getLane().getEdge()); replaceRouteEdges(edges, -1, 0, "DRT.tmp", false, false, false); std::string errorMsg; // add stop addStop(stopPar, errorMsg); } const bool hasReroutingDevice = getDevice(typeid(MSDevice_Routing)) != nullptr; SUMOAbstractRouter<MSEdge, SUMOVehicle>& router = hasReroutingDevice ? MSRoutingEngine::getRouterTT() : MSNet::getInstance()->getRouterTT(); // reroute to ensure the new stop is reached reroute(MSNet::getInstance()->getCurrentTimeStep(), "DRT", router); myParameter->line = line; assert(haveValidStopEdges()); }