Network::RouteInfo Network::updateRoute(Route& newRoute) throw(NetworkException) { std::pair<const Node&, const Node&> np(newRoute.front(), newRoute.back()); auto ori = routes.find(np); assert(ori != routes.end()); Route& oldRoute = ori->second.first; const double traffic = ori->second.second; // Remove the traffic from the old route for (auto i = oldRoute.begin(); i < oldRoute.end() - 1; i++) { Link l = getLink(make_pair(*i, *(i + 1))); l.removeTraffic(traffic); i->removeTraffic(*(i + 1), traffic); } // Add it to the new route double cost = 0.0; for (auto i = newRoute.begin(); i < newRoute.end() - 1; i++) { Link l = getLink(make_pair(*i, *(i + 1))); l.addTraffic(traffic); i->addTraffic(*(i + 1), traffic); cost += l.getCurrentCost(traffic); } ori->second = RouteInfo(newRoute, traffic); return RouteInfo(newRoute, cost); }
Network::RouteInfo Network::addTraffic(Node orig, const Node& dst, double traffic, RouteMode mode) throw (TrancasException) { /* Algorithm: * a) Calculate route (link collection) * b) Update info at orig node * c) Update link traffic * d) Update Nodes neighbour traffic */ Dijkstra spf(orig, dst, *this, traffic, mode==SPF ? Dijkstra::Constant : Dijkstra::Real); Route r = spf.getRoute(); double cost = 0.0; for (auto i = r.begin(); i < r.end() - 1; i++) { Link l = getLink(make_pair(*i, *(i + 1))); cost += l.getCost(traffic); l.addTraffic(traffic); i->addTraffic(*(i + 1), traffic); } addNewRoute(r, traffic); return RouteInfo(move(r), cost); }
Network::RouteInfo Network::changeTrafficInRoute(const Node& orig, const Node& dst, double traffic) throw (NetworkException) { auto ci = routes.find(make_pair(orig, dst)); if (ci == routes.end()) return addTraffic(orig, dst, traffic); Route& route = ci->second.first; double& curTraffic = ci->second.second; if (curTraffic + traffic < 0.0) { throw NetworkException("Routes must have positive values from total traffic"); } for (auto i = route.begin(); i < route.end() - 1; i++) { Link l = getLink(make_pair(*i, *(i + 1))); l.addTraffic(traffic); i->addTraffic(*(i + 1), traffic); } curTraffic += traffic; return ci->second; }