RORouteDef* RORouteDef::copyOrigDest(const std::string& id) const { RORouteDef* result = new RORouteDef(id, 0, true, true); RORoute* route = myAlternatives[0]; RGBColor* col = route->getColor() != 0 ? new RGBColor(*route->getColor()) : 0; ConstROEdgeVector edges; edges.push_back(route->getFirst()); edges.push_back(route->getLast()); result->addLoadedAlternative(new RORoute(id, 0, 1, edges, col, route->getStops())); return result; }
void ROGawronCalculator::calculateProbabilities(std::vector<RORoute*> alternatives, const ROVehicle* const /* veh */, const SUMOTime /* time */) { for (std::vector<RORoute*>::iterator i = alternatives.begin(); i != alternatives.end() - 1; i++) { RORoute* pR = *i; for (std::vector<RORoute*>::iterator j = i + 1; j != alternatives.end(); j++) { RORoute* pS = *j; // see [Gawron, 1998] (4.2) const SUMOReal delta = (pS->getCosts() - pR->getCosts()) / (pS->getCosts() + pR->getCosts()); // see [Gawron, 1998] (4.3a, 4.3b) SUMOReal newPR = gawronF(pR->getProbability(), pS->getProbability(), delta); SUMOReal newPS = pR->getProbability() + pS->getProbability() - newPR; if (ISNAN(newPR) || ISNAN(newPS)) { newPR = pS->getCosts() > pR->getCosts() ? (SUMOReal) 1. : 0; newPS = pS->getCosts() > pR->getCosts() ? 0 : (SUMOReal) 1.; } newPR = MIN2((SUMOReal) MAX2(newPR, (SUMOReal) 0), (SUMOReal) 1); newPS = MIN2((SUMOReal) MAX2(newPS, (SUMOReal) 0), (SUMOReal) 1); pR->setProbability(newPR); pS->setProbability(newPS); } } }
void RORouteDef::addAlternative(SUMOAbstractRouter<ROEdge, ROVehicle>& router, const ROVehicle* const veh, RORoute* current, SUMOTime begin) { if (myTryRepair) { if (myNewRoute) { delete myAlternatives[0]; myAlternatives.pop_back(); myAlternatives.push_back(current); } const SUMOReal costs = router.recomputeCosts(current->getEdgeVector(), veh, begin); if (costs < 0) { throw ProcessError("Route '" + getID() + "' (vehicle '" + veh->getID() + "') is not valid."); } current->setCosts(costs); return; } // add the route when it's new if (myNewRoute) { myAlternatives.push_back(current); } // recompute the costs and (when a new route was added) scale the probabilities const SUMOReal scale = SUMOReal(myAlternatives.size() - 1) / SUMOReal(myAlternatives.size()); for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) { RORoute* alt = *i; // recompute the costs for all routes const SUMOReal newCosts = router.recomputeCosts(alt->getEdgeVector(), veh, begin); if (newCosts < 0.) { throw ProcessError("Route '" + current->getID() + "' (vehicle '" + veh->getID() + "') is not valid."); } assert(myAlternatives.size() != 0); if (myNewRoute) { if (*i == current) { // set initial probability and costs alt->setProbability((SUMOReal)(1.0 / (SUMOReal) myAlternatives.size())); alt->setCosts(newCosts); } else { // rescale probs for all others alt->setProbability(alt->getProbability() * scale); } } ROCostCalculator::getCalculator().setCosts(alt, newCosts, *i == myAlternatives[myLastUsed]); } assert(myAlternatives.size() != 0); ROCostCalculator::getCalculator().calculateProbabilities(veh, myAlternatives); if (!ROCostCalculator::getCalculator().keepRoutes()) { // remove with probability of 0 (not mentioned in Gawron) for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end();) { if ((*i)->getProbability() == 0) { delete *i; i = myAlternatives.erase(i); } else { i++; } } } if (myAlternatives.size() > ROCostCalculator::getCalculator().getMaxRouteNumber()) { // only keep the routes with highest probability sort(myAlternatives.begin(), myAlternatives.end(), ComparatorProbability()); for (std::vector<RORoute*>::iterator i = myAlternatives.begin() + ROCostCalculator::getCalculator().getMaxRouteNumber(); i != myAlternatives.end(); i++) { delete *i; } myAlternatives.erase(myAlternatives.begin() + ROCostCalculator::getCalculator().getMaxRouteNumber(), myAlternatives.end()); // rescale probabilities SUMOReal newSum = 0; for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) { newSum += (*i)->getProbability(); } assert(newSum > 0); // @note newSum may be larger than 1 for numerical reasons for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) { (*i)->setProbability((*i)->getProbability() / newSum); } } // find the route to use SUMOReal chosen = RandHelper::rand(); int pos = 0; for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end() - 1; i++, pos++) { chosen -= (*i)->getProbability(); if (chosen <= 0) { myLastUsed = pos; return; } } myLastUsed = pos; }