Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file ROMAAssignments.cpp
15 : /// @author Yun-Pang Floetteroed
16 : /// @author Laura Bieker
17 : /// @author Michael Behrisch
18 : /// @date Feb 2013
19 : ///
20 : // Assignment methods
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <vector>
25 : #include <algorithm>
26 : #include <utils/common/SUMOTime.h>
27 : #include <utils/distribution/Distribution_Points.h>
28 : #include <utils/router/RouteCostCalculator.h>
29 : #include <utils/router/SUMOAbstractRouter.h>
30 : #include <router/ROEdge.h>
31 : #include <router/RONet.h>
32 : #include <router/RORoute.h>
33 : #include <od/ODMatrix.h>
34 : #include "ROMAEdge.h"
35 : #include "ROMAAssignments.h"
36 :
37 :
38 : // ===========================================================================
39 : // static member variables
40 : // ===========================================================================
41 : std::map<const ROEdge* const, double> ROMAAssignments::myPenalties;
42 :
43 :
44 : // ===========================================================================
45 : // method definitions
46 : // ===========================================================================
47 :
48 77 : ROMAAssignments::ROMAAssignments(const SUMOTime begin, const SUMOTime end, const bool additiveTraffic,
49 : const double adaptionFactor, const int maxAlternatives, const bool defaultCapacities,
50 : RONet& net, ODMatrix& matrix, SUMOAbstractRouter<ROEdge, ROVehicle>& router,
51 77 : OutputDevice* netloadOutput)
52 77 : : myBegin(begin), myEnd(end), myAdditiveTraffic(additiveTraffic), myAdaptionFactor(adaptionFactor),
53 77 : myMaxAlternatives(maxAlternatives), myUseDefaultCapacities(defaultCapacities),
54 77 : myNet(net), myMatrix(matrix), myRouter(router), myNetloadOutput(netloadOutput) {
55 77 : myDefaultVehicle = new ROVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
56 77 : }
57 :
58 :
59 77 : ROMAAssignments::~ROMAAssignments() {
60 77 : delete myDefaultVehicle;
61 77 : }
62 :
63 : // based on the definitions in PTV-Validate and in the VISUM-Cologne network
64 : double
65 163824 : ROMAAssignments::getCapacity(const ROEdge* edge) const {
66 163824 : if (edge->isTazConnector()) {
67 : return 0;
68 : }
69 163824 : const int roadClass = myUseDefaultCapacities ? -1 : -edge->getPriority();
70 : // TODO: differ road class 1 from the unknown road class 1!!!
71 163824 : if (edge->getNumLanes() == 0) {
72 : // TAZ have no cost
73 : return 0;
74 163824 : } else if (roadClass == 0 || roadClass == 1) {
75 160636 : return edge->getNumLanes() * 2000.; //CR13 in table.py
76 3188 : } else if (roadClass == 2 && edge->getSpeedLimit() <= 11.) {
77 0 : return edge->getNumLanes() * 1333.33; //CR5 in table.py
78 3188 : } else if (roadClass == 2 && edge->getSpeedLimit() > 11. && edge->getSpeedLimit() <= 16.) {
79 0 : return edge->getNumLanes() * 1500.; //CR3 in table.py
80 3188 : } else if (roadClass == 2 && edge->getSpeedLimit() > 16.) {
81 0 : return edge->getNumLanes() * 2000.; //CR13 in table.py
82 3188 : } else if (roadClass == 3 && edge->getSpeedLimit() <= 11.) {
83 0 : return edge->getNumLanes() * 800.; //CR5 in table.py
84 3188 : } else if (roadClass == 3 && edge->getSpeedLimit() > 11. && edge->getSpeedLimit() <= 13.) {
85 0 : return edge->getNumLanes() * 875.; //CR5 in table.py
86 3188 : } else if (roadClass == 3 && edge->getSpeedLimit() > 13. && edge->getSpeedLimit() <= 16.) {
87 0 : return edge->getNumLanes() * 1500.; //CR4 in table.py
88 3188 : } else if (roadClass == 3 && edge->getSpeedLimit() > 16.) {
89 0 : return edge->getNumLanes() * 1800.; //CR13 in table.py
90 3188 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() <= 5.) {
91 0 : return edge->getNumLanes() * 200.; //CR7 in table.py
92 3188 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 5. && edge->getSpeedLimit() <= 7.) {
93 0 : return edge->getNumLanes() * 412.5; //CR7 in table.py
94 3188 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 7. && edge->getSpeedLimit() <= 9.) {
95 0 : return edge->getNumLanes() * 600.; //CR6 in table.py
96 3188 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 9. && edge->getSpeedLimit() <= 11.) {
97 0 : return edge->getNumLanes() * 800.; //CR5 in table.py
98 3188 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 11. && edge->getSpeedLimit() <= 13.) {
99 0 : return edge->getNumLanes() * 1125.; //CR5 in table.py
100 3188 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 13. && edge->getSpeedLimit() <= 16.) {
101 3134 : return edge->getNumLanes() * 1583.; //CR4 in table.py
102 54 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 16. && edge->getSpeedLimit() <= 18.) {
103 0 : return edge->getNumLanes() * 1100.; //CR3 in table.py
104 54 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 18. && edge->getSpeedLimit() <= 22.) {
105 0 : return edge->getNumLanes() * 1200.; //CR3 in table.py
106 54 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 22. && edge->getSpeedLimit() <= 26.) {
107 0 : return edge->getNumLanes() * 1300.; //CR3 in table.py
108 54 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 26.) {
109 0 : return edge->getNumLanes() * 1400.; //CR3 in table.py
110 : }
111 54 : return edge->getNumLanes() * 800.; //CR5 in table.py
112 : }
113 :
114 :
115 : // based on the definitions in PTV-Validate and in the VISUM-Cologne network
116 : double
117 244087 : ROMAAssignments::capacityConstraintFunction(const ROEdge* edge, const double flow) const {
118 244087 : if (edge->isTazConnector()) {
119 : return 0;
120 : }
121 162605 : const int roadClass = myUseDefaultCapacities ? -1 : -edge->getPriority();
122 162605 : const double capacity = getCapacity(edge);
123 : // TODO: differ road class 1 from the unknown road class 1!!!
124 162605 : if (edge->getNumLanes() == 0) {
125 : // TAZ have no cost
126 : return 0;
127 162605 : } else if (roadClass == 0 || roadClass == 1) {
128 159435 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 1.3)) * 2.); //CR13 in table.py
129 3170 : } else if (roadClass == 2 && edge->getSpeedLimit() <= 11.) {
130 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.9)) * 3.); //CR5 in table.py
131 3170 : } else if (roadClass == 2 && edge->getSpeedLimit() > 11. && edge->getSpeedLimit() <= 16.) {
132 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 1.)) * 2.); //CR3 in table.py
133 3170 : } else if (roadClass == 2 && edge->getSpeedLimit() > 16.) {
134 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 1.3)) * 2.); //CR13 in table.py
135 3170 : } else if (roadClass == 3 && edge->getSpeedLimit() <= 11.) {
136 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.9)) * 3.); //CR5 in table.py
137 3170 : } else if (roadClass == 3 && edge->getSpeedLimit() > 11. && edge->getSpeedLimit() <= 13.) {
138 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.9)) * 3.); //CR5 in table.py
139 3170 : } else if (roadClass == 3 && edge->getSpeedLimit() > 13. && edge->getSpeedLimit() <= 16.) {
140 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.7 * (flow / (capacity * 1.)) * 2.); //CR4 in table.py
141 3170 : } else if (roadClass == 3 && edge->getSpeedLimit() > 16.) {
142 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 1.3)) * 2.); //CR13 in table.py
143 3170 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() <= 5.) {
144 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.5)) * 3.); //CR7 in table.py
145 3170 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 5. && edge->getSpeedLimit() <= 7.) {
146 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.5)) * 3.); //CR7 in table.py
147 3170 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 7. && edge->getSpeedLimit() <= 9.) {
148 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.8)) * 3.); //CR6 in table.py
149 3170 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 9. && edge->getSpeedLimit() <= 11.) {
150 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.9)) * 3.); //CR5 in table.py
151 3170 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 11. && edge->getSpeedLimit() <= 13.) {
152 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.9)) * 3.); //CR5 in table.py
153 3170 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 13. && edge->getSpeedLimit() <= 16.) {
154 3119 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.7 * (flow / (capacity * 1.)) * 2.); //CR4 in table.py
155 51 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 16. && edge->getSpeedLimit() <= 18.) {
156 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 1.)) * 2.); //CR3 in table.py
157 51 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 18. && edge->getSpeedLimit() <= 22.) {
158 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 1.)) * 2.); //CR3 in table.py
159 51 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 22. && edge->getSpeedLimit() <= 26.) {
160 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 1.)) * 2.); //CR3 in table.py
161 51 : } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeedLimit() > 26.) {
162 0 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 1.)) * 2.); //CR3 in table.py
163 : }
164 51 : return edge->getLength() / edge->getSpeedLimit() * (1. + 1.*(flow / (capacity * 0.9)) * 3.); //CR5 in table.py
165 : }
166 :
167 :
168 : bool
169 40926 : ROMAAssignments::addRoute(const ConstROEdgeVector& edges, std::vector<std::shared_ptr<RORoute> >& paths, std::string routeId, double prob) {
170 : std::vector<std::shared_ptr<RORoute> >::iterator p;
171 48447 : for (p = paths.begin(); p != paths.end(); p++) {
172 45787 : if (edges == (*p)->getEdgeVector()) {
173 : break;
174 : }
175 : }
176 40926 : if (p == paths.end()) {
177 5320 : paths.push_back(std::make_shared<RORoute>(routeId, 0., prob, edges, nullptr, StopParVector()));
178 2660 : return true;
179 : }
180 38266 : (*p)->addProbability(prob);
181 : std::iter_swap(paths.end() - 1, p);
182 38266 : return false;
183 : }
184 :
185 :
186 : const ConstROEdgeVector
187 40964 : ROMAAssignments::computePath(ODCell* cell, const SUMOTime time, const double probability, SUMOAbstractRouter<ROEdge, ROVehicle>* router, bool setBulkMode) {
188 81668 : const ROEdge* const from = myNet.getEdge(cell->origin + (cell->originIsEdge ? "" : "-source"));
189 40964 : if (from == nullptr) {
190 0 : throw ProcessError(TLF("Unknown origin '%'.", cell->origin));
191 : }
192 81668 : const ROEdge* const to = myNet.getEdge(cell->destination + (cell->destinationIsEdge ? "" : "-sink"));
193 40964 : if (to == nullptr) {
194 0 : throw ProcessError(TLF("Unknown destination '%'.", cell->destination));
195 : }
196 40964 : ConstROEdgeVector edges;
197 40964 : if (router == nullptr) {
198 24 : router = &myRouter;
199 : }
200 40964 : if (myMaxAlternatives > 0 && (int)cell->pathsVector.size() < myMaxAlternatives) {
201 40926 : router->compute(from, to, myDefaultVehicle, time, edges);
202 40926 : if (setBulkMode) {
203 40762 : router->setBulkMode(true);
204 : }
205 122778 : if (addRoute(edges, cell->pathsVector, cell->origin + cell->destination + toString(cell->pathsVector.size()), probability)) {
206 : return edges;
207 : }
208 : } else {
209 : double minCost = std::numeric_limits<double>::max();
210 : std::shared_ptr<RORoute> minRoute = nullptr;
211 76 : for (const std::shared_ptr<RORoute>& p : cell->pathsVector) {
212 38 : const double cost = router->recomputeCosts(edges, myDefaultVehicle, time);
213 38 : if (cost < minCost) {
214 : minCost = cost;
215 : minRoute = p;
216 : }
217 : }
218 38 : minRoute->addProbability(probability);
219 : }
220 38304 : return ConstROEdgeVector();
221 40964 : }
222 :
223 :
224 : void
225 6 : ROMAAssignments::getKPaths(const int kPaths, const double penalty) {
226 12 : for (ODCell* const c : myMatrix.getCells()) {
227 : myPenalties.clear();
228 20 : for (int k = 0; k < kPaths; k++) {
229 88 : for (const ROEdge* const e : computePath(c)) {
230 74 : myPenalties[e] += penalty;
231 14 : }
232 : }
233 : }
234 : myPenalties.clear();
235 6 : }
236 :
237 :
238 : void
239 77 : ROMAAssignments::resetFlows() {
240 77 : const double begin = STEPS2TIME(MIN2(myBegin, myMatrix.getCells().front()->begin));
241 2363 : for (ROEdge* e : ROEdge::getAllEdges()) {
242 : ROMAEdge* const edge = static_cast<ROMAEdge*>(e);
243 2286 : edge->setFlow(begin, STEPS2TIME(myEnd), 0.);
244 2286 : edge->setHelpFlow(begin, STEPS2TIME(myEnd), 0.);
245 : }
246 77 : }
247 :
248 :
249 : void
250 218 : ROMAAssignments::writeInterval(const SUMOTime begin, const SUMOTime end) {
251 218 : if (myNetloadOutput != nullptr) {
252 32 : myNetloadOutput->openTag(SUMO_TAG_INTERVAL).writeAttr(SUMO_ATTR_BEGIN, time2string(begin)).writeAttr(SUMO_ATTR_END, time2string(end));
253 6491 : for (ROEdge* e : ROEdge::getAllEdges()) {
254 : const ROMAEdge* const edge = static_cast<ROMAEdge*>(e);
255 6459 : if (edge->getFunction() == SumoXMLEdgeFunc::NORMAL) {
256 1219 : myNetloadOutput->openTag(SUMO_TAG_EDGE).writeAttr(SUMO_ATTR_ID, edge->getID());
257 1219 : const double traveltime = edge->getTravelTime(getDefaultVehicle(), STEPS2TIME(begin));
258 1219 : const double speed = edge->getLength() / traveltime;
259 1219 : const double flow = edge->getFlow(STEPS2TIME(begin));
260 1219 : const double timeGap = STEPS2TIME(end - begin) / flow;
261 1219 : const double spaceGap = timeGap * speed;
262 1219 : const double density = 1000.0 / spaceGap;
263 1219 : const double laneDensity = density / edge->getNumLanes();
264 1219 : myNetloadOutput->writeAttr(SUMO_ATTR_TRAVELTIME, traveltime);
265 1219 : myNetloadOutput->writeAttr(SUMO_ATTR_SPEED, speed);
266 1219 : myNetloadOutput->writeAttr(SUMO_ATTR_SPEEDREL, speed / edge->getSpeedLimit());
267 1219 : myNetloadOutput->writeAttr(SUMO_ATTR_ENTERED, flow);
268 1219 : myNetloadOutput->writeAttr(SUMO_ATTR_DENSITY, density);
269 1219 : myNetloadOutput->writeAttr(SUMO_ATTR_LANEDENSITY, laneDensity);
270 1219 : myNetloadOutput->writeAttr("flowCapacityRatio", 100. * flow / getCapacity(edge));
271 2438 : myNetloadOutput->closeTag();
272 : }
273 : }
274 64 : myNetloadOutput->closeTag();
275 : }
276 218 : }
277 :
278 :
279 : void
280 71 : ROMAAssignments::incremental(const int numIter, const bool verbose) {
281 : SUMOTime lastBegin = -1;
282 71 : std::vector<int> intervals;
283 71 : int count = 0;
284 2115 : for (const ODCell* const c : myMatrix.getCells()) {
285 2044 : if (c->begin != lastBegin) {
286 212 : intervals.push_back(count);
287 212 : lastBegin = c->begin;
288 : }
289 2044 : count++;
290 : }
291 : lastBegin = -1;
292 283 : for (std::vector<int>::const_iterator offset = intervals.begin(); offset != intervals.end(); offset++) {
293 212 : std::vector<ODCell*>::const_iterator firstCell = myMatrix.getCells().begin() + (*offset);
294 : std::vector<ODCell*>::const_iterator lastCell = myMatrix.getCells().end();
295 212 : if (offset != intervals.end() - 1) {
296 141 : lastCell = myMatrix.getCells().begin() + (*(offset + 1));
297 : }
298 212 : const SUMOTime intervalStart = (*firstCell)->begin;
299 212 : if (verbose) {
300 0 : WRITE_MESSAGE(" starting interval " + time2string(intervalStart));
301 : }
302 : std::map<const ROMAEdge*, double> loadedTravelTimes;
303 27115 : for (ROEdge* e : ROEdge::getAllEdges()) {
304 26903 : const ROMAEdge* const edge = static_cast<ROMAEdge*>(e);
305 26903 : if (edge->hasLoadedTravelTime(STEPS2TIME(intervalStart))) {
306 2024 : loadedTravelTimes[edge] = edge->getTravelTime(myDefaultVehicle, STEPS2TIME(intervalStart));
307 : }
308 : }
309 212 : if (loadedTravelTimes.empty()) {
310 : // we don't have loaded travel times for this interval but we may have set ourselves some for the interval before but no need to warn
311 : ROEdge::disableTimelineWarning();
312 : }
313 4482 : for (int t = 0; t < numIter; t++) {
314 4270 : if (verbose) {
315 0 : WRITE_MESSAGE(" starting iteration " + toString(t));
316 : }
317 4270 : std::string lastOrigin = "";
318 : int workerIndex = 0;
319 45210 : for (std::vector<ODCell*>::const_iterator i = firstCell; i != lastCell; i++) {
320 40940 : ODCell* const c = *i;
321 40940 : const double linkFlow = c->vehicleNumber / numIter;
322 40940 : const SUMOTime begin = myAdditiveTraffic ? myBegin : c->begin;
323 : #ifdef HAVE_FOX
324 40940 : if (myNet.getThreadPool().size() > 0) {
325 140 : if (lastOrigin != c->origin) {
326 140 : workerIndex++;
327 140 : if (workerIndex == myNet.getThreadPool().size()) {
328 : workerIndex = 0;
329 : }
330 140 : myNet.getThreadPool().add(new RONet::BulkmodeTask(false), workerIndex);
331 : lastOrigin = c->origin;
332 140 : myNet.getThreadPool().add(new RoutingTask(*this, c, begin, linkFlow), workerIndex);
333 : } else {
334 0 : myNet.getThreadPool().add(new RoutingTask(*this, c, begin, linkFlow), workerIndex);
335 : }
336 140 : continue;
337 : }
338 : #else
339 : UNUSED_PARAMETER(workerIndex);
340 : #endif
341 40800 : if (lastOrigin != c->origin) {
342 11960 : myRouter.setBulkMode(false);
343 : lastOrigin = c->origin;
344 : }
345 40800 : computePath(c, begin, linkFlow, &myRouter, true);
346 : }
347 : #ifdef HAVE_FOX
348 4270 : if (myNet.getThreadPool().size() > 0) {
349 100 : myNet.getThreadPool().waitAll();
350 : }
351 : #endif
352 45210 : for (std::vector<ODCell*>::const_iterator i = firstCell; i != lastCell; i++) {
353 40940 : ODCell* const c = *i;
354 40940 : const double linkFlow = c->vehicleNumber / numIter;
355 40940 : const SUMOTime begin = myAdditiveTraffic ? myBegin : c->begin;
356 40940 : const SUMOTime end = myAdditiveTraffic ? myEnd : c->end;
357 40940 : const double intervalLengthInHours = STEPS2TIME(end - begin) / 3600.;
358 : const ConstROEdgeVector& edges = c->pathsVector.back()->getEdgeVector();
359 284028 : for (ConstROEdgeVector::const_iterator e = edges.begin(); e != edges.end(); e++) {
360 243088 : ROMAEdge* edge = static_cast<ROMAEdge*>(myNet.getEdge((*e)->getID()));
361 243088 : const double newFlow = edge->getFlow(STEPS2TIME(begin)) + linkFlow;
362 243088 : edge->setFlow(STEPS2TIME(begin), STEPS2TIME(end), newFlow);
363 243088 : double travelTime = capacityConstraintFunction(edge, newFlow / intervalLengthInHours);
364 243088 : if (lastBegin >= 0 && myAdaptionFactor > 0.) {
365 : if (loadedTravelTimes.count(edge) != 0) {
366 0 : travelTime = loadedTravelTimes[edge] * myAdaptionFactor + (1. - myAdaptionFactor) * travelTime;
367 : } else {
368 55200 : travelTime = edge->getTravelTime(myDefaultVehicle, STEPS2TIME(lastBegin)) * myAdaptionFactor + (1. - myAdaptionFactor) * travelTime;
369 : }
370 : }
371 243088 : edge->addTravelTime(travelTime, STEPS2TIME(begin), STEPS2TIME(end));
372 : }
373 : }
374 4270 : myRouter.reset(myDefaultVehicle);
375 : }
376 212 : writeInterval(intervalStart, (*(lastCell - 1))->end);
377 : lastBegin = intervalStart;
378 : }
379 71 : }
380 :
381 :
382 : void
383 6 : ROMAAssignments::sue(const int maxOuterIteration, const int maxInnerIteration, const int kPaths, const double penalty, const double tolerance, const std::string /* routeChoiceMethod */) {
384 6 : getKPaths(kPaths, penalty);
385 : std::map<const SUMOTime, SUMOTime> intervals;
386 6 : if (myAdditiveTraffic) {
387 0 : intervals[myBegin] = myEnd;
388 : } else {
389 12 : for (const ODCell* const c : myMatrix.getCells()) {
390 6 : intervals[c->begin] = c->end;
391 : }
392 : }
393 10 : for (int outer = 0; outer < maxOuterIteration; outer++) {
394 32 : for (int inner = 0; inner < maxInnerIteration; inner++) {
395 64 : for (const ODCell* const c : myMatrix.getCells()) {
396 32 : const SUMOTime begin = myAdditiveTraffic ? myBegin : c->begin;
397 32 : const SUMOTime end = myAdditiveTraffic ? myEnd : c->end;
398 : // update path cost
399 88 : for (const std::shared_ptr<RORoute>& r : c->pathsVector) {
400 56 : r->setCosts(myRouter.recomputeCosts(r->getEdgeVector(), myDefaultVehicle, 0));
401 : // std::cout << std::setprecision(20) << r->getID() << ":" << r->getCosts() << std::endl;
402 : }
403 : // calculate route utilities and probabilities
404 32 : RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().calculateProbabilities(c->pathsVector, myDefaultVehicle, 0);
405 : // calculate route flows
406 88 : for (const std::shared_ptr<RORoute>& r : c->pathsVector) {
407 56 : const double pathFlow = r->getProbability() * c->vehicleNumber;
408 : // assign edge flow deltas
409 532 : for (ConstROEdgeVector::const_iterator e = r->getEdgeVector().begin(); e != r->getEdgeVector().end(); e++) {
410 476 : ROMAEdge* edge = static_cast<ROMAEdge*>(myNet.getEdge((*e)->getID()));
411 476 : edge->setHelpFlow(STEPS2TIME(begin), STEPS2TIME(end), edge->getHelpFlow(STEPS2TIME(begin)) + pathFlow);
412 : }
413 : }
414 : }
415 : // calculate new edge flows and check for stability
416 : int unstableEdges = 0;
417 64 : for (const auto& it : intervals) {
418 32 : const double intervalLengthInHours = STEPS2TIME(it.second - it.first) / 3600.;
419 32 : const double intBegin = STEPS2TIME(it.first);
420 32 : const double intEnd = STEPS2TIME(it.second);
421 1031 : for (ROEdge* e : ROEdge::getAllEdges()) {
422 : ROMAEdge* const edge = static_cast<ROMAEdge*>(e);
423 : const double oldFlow = edge->getFlow(intBegin);
424 : double newFlow = oldFlow;
425 999 : if (inner == 0 && outer == 0) {
426 210 : newFlow += edge->getHelpFlow(intBegin);
427 : } else {
428 789 : newFlow += (edge->getHelpFlow(intBegin) - oldFlow) / (inner + 1);
429 : }
430 : // if not lohse:
431 999 : if (newFlow > 0.) {
432 338 : if (fabs(newFlow - oldFlow) / newFlow > tolerance) {
433 135 : unstableEdges++;
434 : }
435 661 : } else if (newFlow == 0.) {
436 661 : if (oldFlow != 0. && (fabs(newFlow - oldFlow) / oldFlow > tolerance)) {
437 0 : unstableEdges++;
438 : }
439 : } else { // newFlow < 0.
440 0 : unstableEdges++;
441 : newFlow = 0.;
442 : }
443 : edge->setFlow(intBegin, intEnd, newFlow);
444 999 : const double travelTime = capacityConstraintFunction(edge, newFlow / intervalLengthInHours);
445 999 : edge->addTravelTime(travelTime, intBegin, intEnd);
446 : edge->setHelpFlow(intBegin, intEnd, 0.);
447 : }
448 : }
449 : // if stable break
450 32 : if (unstableEdges == 0) {
451 : break;
452 : }
453 : // additional stability check from python script: if notstable < math.ceil(net.geteffEdgeCounts()*0.005) or notstable < 3: stable = True
454 : }
455 : // travel times changed since the metric was last built (or since the
456 : // previous outer iteration): flag it stale so the *first* subsequent
457 : // router query re-customizes (CCH) / rebuilds the hierarchy (CH)
458 : // from current edge weights. recomputeCosts() above never consults
459 : // the cached metric (it reads the live effort function directly), so
460 : // a single reset() here -- right before the only call that does,
461 : // computePath()'s router->compute() below -- is exactly as correct
462 : // as resetting after every inner iteration, but for an
463 : // expensive-to-rebuild algorithm like CH this cuts the number of
464 : // rebuilds from O(maxInnerIteration) to one per outer iteration.
465 10 : myRouter.reset(myDefaultVehicle);
466 : // check for a new route, if none available, break
467 : // several modifications about when a route is new and when to break are in the original script
468 : bool newRoute = false;
469 20 : for (ODCell* const c : myMatrix.getCells()) {
470 10 : newRoute |= !computePath(c).empty();
471 : }
472 10 : if (!newRoute) {
473 : break;
474 : }
475 : }
476 : // final round of assignment
477 12 : for (const ODCell* const c : myMatrix.getCells()) {
478 : // update path cost
479 18 : for (const std::shared_ptr<RORoute>& r : c->pathsVector) {
480 12 : r->setCosts(myRouter.recomputeCosts(r->getEdgeVector(), myDefaultVehicle, 0));
481 : }
482 : // calculate route utilities and probabilities
483 6 : RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().calculateProbabilities(c->pathsVector, myDefaultVehicle, 0);
484 : // calculate route flows
485 18 : for (const std::shared_ptr<RORoute>& r : c->pathsVector) {
486 12 : r->setProbability(r->getProbability() * c->vehicleNumber);
487 : }
488 : }
489 12 : for (const auto& it : intervals) {
490 6 : writeInterval(it.first, it.second);
491 : }
492 6 : }
493 :
494 :
495 : double
496 0 : ROMAAssignments::getPenalizedEffort(const ROEdge* const e, const ROVehicle* const v, double t) {
497 : const std::map<const ROEdge* const, double>::const_iterator i = myPenalties.find(e);
498 0 : return i == myPenalties.end() ? e->getEffort(v, t) : e->getEffort(v, t) + i->second;
499 : }
500 :
501 :
502 : double
503 236 : ROMAAssignments::getPenalizedTT(const ROEdge* const e, const ROVehicle* const v, double t) {
504 : const std::map<const ROEdge* const, double>::const_iterator i = myPenalties.find(e);
505 236 : return i == myPenalties.end() ? e->getTravelTime(v, t) : e->getTravelTime(v, t) + i->second;
506 : }
507 :
508 :
509 : double
510 0 : ROMAAssignments::getTravelTime(const ROEdge* const e, const ROVehicle* const v, double t) {
511 0 : return e->getTravelTime(v, t);
512 : }
513 :
514 :
515 : #ifdef HAVE_FOX
516 : // ---------------------------------------------------------------------------
517 : // ROMAAssignments::RoutingTask-methods
518 : // ---------------------------------------------------------------------------
519 : void
520 140 : ROMAAssignments::RoutingTask::run(MFXWorkerThread* context) {
521 280 : myAssign.computePath(myCell, myBegin, myLinkFlow, &static_cast<RONet::WorkerThread*>(context)->getVehicleRouter(SVC_IGNORING), mySetBulkMode);
522 140 : }
523 : #endif
|