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 duarouter_main.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @date Thu, 06 Jun 2002
19 : ///
20 : // Main for DUAROUTER
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #ifdef HAVE_VERSION_H
25 : #include <version.h>
26 : #endif
27 :
28 : #include <iostream>
29 : #include <string>
30 : #include <limits.h>
31 : #include <ctime>
32 : #include <memory>
33 : #include <xercesc/sax/SAXException.hpp>
34 : #include <xercesc/sax/SAXParseException.hpp>
35 : #include <utils/common/StringUtils.h>
36 : #include <utils/common/MsgHandler.h>
37 : #include <utils/common/UtilExceptions.h>
38 : #include <utils/common/SystemFrame.h>
39 : #include <utils/common/RandHelper.h>
40 : #include <utils/common/ToString.h>
41 : #include <utils/vehicle/SUMORouteHandler.h>
42 : #ifdef HAVE_FOX
43 : #include <utils/foxtools/MsgHandlerSynchronized.h>
44 : #endif
45 : #include <utils/iodevices/OutputDevice.h>
46 : #include <utils/options/Option.h>
47 : #include <utils/options/OptionsCont.h>
48 : #include <utils/options/OptionsIO.h>
49 : #include <utils/router/DijkstraRouter.h>
50 : #include <utils/router/AFRouter.h>
51 : #include <utils/router/AStarRouter.h>
52 : #include <utils/router/CHRouter.h>
53 : #include <utils/router/CHRouterWrapper.h>
54 : #include <utils/router/CCHRouter.h>
55 : #include <utils/vehicle/SUMOVehicleParserHelper.h>
56 : #include <utils/xml/XMLSubSys.h>
57 : #include <router/ROFrame.h>
58 : #include <router/ROLoader.h>
59 : #include <router/RONet.h>
60 : #include <router/ROEdge.h>
61 : #include "RODUAEdgeBuilder.h"
62 : #include <router/ROLane.h>
63 : #include <router/RONode.h>
64 : #include <router/ROCCHMetrics.h>
65 : #include "RODUAFrame.h"
66 :
67 :
68 : // ===========================================================================
69 : // functions
70 : // ===========================================================================
71 :
72 : /* -------------------------------------------------------------------------
73 : * data processing methods
74 : * ----------------------------------------------------------------------- */
75 : /**
76 : * loads the net
77 : * The net is in this meaning made up by the net itself and the dynamic
78 : * weights which may be supplied in a separate file
79 : */
80 : void
81 4132 : initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
82 : // load the net
83 4132 : RODUAEdgeBuilder builder;
84 4132 : ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
85 4132 : loader.loadNet(net, builder);
86 : // load the weights when wished/available
87 7766 : if (oc.isSet("weight-files")) {
88 1338 : loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
89 : }
90 7766 : if (oc.isSet("lane-weight-files")) {
91 9 : loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
92 : }
93 7766 : if (oc.getBool("skip-new-routes")) {
94 : RORouteDef::setSkipNew();
95 : }
96 4132 : }
97 :
98 :
99 : /**
100 : * Computes the routes saving them
101 : */
102 : void
103 3883 : computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
104 : // initialise the loader
105 3883 : loader.openRoutes(net);
106 : // build the router
107 3327 : auto ttFunction = gWeightsRandomFactor > 1 ? &ROEdge::getTravelTimeStaticRandomized : &ROEdge::getTravelTimeStatic;
108 : SUMOAbstractRouter<ROEdge, ROVehicle>* router;
109 3327 : const std::string measure = oc.getString("weight-attribute");
110 3390 : const std::string routingAlgorithm = oc.getString("routing-algorithm");
111 3327 : const double priorityFactor = oc.getFloat("weights.priority-factor");
112 3327 : const SUMOTime begin = string2time(oc.getString("begin"));
113 3401 : const SUMOTime end = oc.isDefault("end") ? SUMOTime_MAX : string2time(oc.getString("end"));
114 : DijkstraRouter<ROEdge, ROVehicle>::Operation op = ttFunction;
115 3327 : if (measure == "traveltime") {
116 3316 : if (priorityFactor != 0) {
117 6 : if (ROEdge::initPriorityFactor(priorityFactor)) {
118 4 : if (gWeightsRandomFactor > 0) {
119 : op = &ROEdge::getTravelTimeStaticPriorityFactorRandomized;
120 : } else {
121 : op = &ROEdge::getTravelTimeStaticPriorityFactor;
122 : }
123 : }
124 : }
125 : }
126 :
127 6654 : if (oc.isSet("restriction-params") &&
128 30 : (routingAlgorithm == "CH" || routingAlgorithm == "CHWrapper")) {
129 36 : throw ProcessError(TLF("Routing algorithm '%' does not support restriction-params", routingAlgorithm));
130 : }
131 :
132 3315 : if (measure == "traveltime") {
133 3304 : if (routingAlgorithm == "dijkstra") {
134 2438 : router = new DijkstraRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, nullptr, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
135 2085 : } else if (routingAlgorithm == "astar") {
136 : typedef AStarRouter<ROEdge, ROVehicle, ROMapMatcher> AStar;
137 523 : std::shared_ptr<const AStar::LookupTable> lookup;
138 1046 : if (oc.isSet("astar.all-distances")) {
139 0 : lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
140 1052 : } else if (oc.isSet("astar.landmark-distances")) {
141 : /* CHRouterWrapper<ROEdge, ROVehicle> chrouter(
142 : ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic,
143 : begin, end, SUMOTime_MAX, 1); */
144 20 : DijkstraRouter<ROEdge, ROVehicle> forward(ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic);
145 20 : std::vector<ReversedEdge<ROEdge, ROVehicle>*> reversed;
146 33564 : for (ROEdge* edge : ROEdge::getAllEdges()) {
147 33544 : reversed.push_back(edge->getReversedRoutingEdge());
148 : }
149 33564 : for (ReversedEdge<ROEdge, ROVehicle>* redge : reversed) {
150 33544 : redge->init();
151 : }
152 20 : DijkstraRouter<ReversedEdge<ROEdge, ROVehicle>, ROVehicle> backward(reversed, true, &ReversedEdge<ROEdge, ROVehicle>::getTravelTimeStatic);
153 26 : ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
154 20 : ROMapMatcher* mapMatcher = dynamic_cast<ROMapMatcher*>(loader.getRouteHandler());
155 40 : lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &forward, &backward, &defaultVehicle,
156 60 : oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"), mapMatcher);
157 40 : }
158 1048 : router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, lookup, net.hasPermissions(), oc.isSet("restriction-params"));
159 1562 : } else if (routingAlgorithm == "CH" && !net.hasPermissions() && !gRoutingPreferences && !net.hasSpeedRestrictions()) {
160 79 : const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
161 97 : string2time(oc.getString("weight-period")) :
162 : SUMOTime_MAX);
163 : router = new CHRouter<ROEdge, ROVehicle>(
164 158 : ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, SVC_IGNORING, weightPeriod, net.hasPermissions(), oc.isSet("restriction-params"));
165 1483 : } else if (routingAlgorithm == "CHWrapper" || routingAlgorithm == "CH") {
166 : // use CHWrapper instead of CH if the net has permissions, preferences or speed restriction
167 933 : const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
168 959 : string2time(oc.getString("weight-period")) :
169 : SUMOTime_MAX);
170 : router = new CHRouterWrapper<ROEdge, ROVehicle>(
171 1866 : ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op,
172 2799 : begin, end, weightPeriod, net.hasPermissions(), oc.getInt("routing-threads"));
173 550 : } else if (routingAlgorithm == "CCH") {
174 : // One weight-independent hierarchy over the union graph; one
175 : // customized metric per (vehicle type, weight period), built
176 : // lazily on first query (see ROCCHMetrics.h). With
177 : // time-dependent weight files this follows CHRouterWrapper's
178 : // per-period semantics, but repeats only the metric
179 : // customization, never the topology build.
180 : // Because the metrics are keyed by vehicle type, the type and
181 : // class specific routing preferences of the effort function are
182 : // captured exactly. weights.random-factor freezes one random
183 : // realization into each metric, the same approximation
184 : // CHRouterWrapper makes when building its hierarchies.
185 1084 : const bool hasWeights = oc.isSet("weight-files") || oc.isSet("lane-weight-files");
186 567 : const SUMOTime weightPeriod = hasWeights ? string2time(oc.getString("weight-period")) : SUMOTime_MAX;
187 : // Process-lifetime singletons: the topology and metric store are
188 : // shared by every router clone until duarouter exits.
189 550 : ROCCHGraph* cchGraph = new ROCCHGraph(ROEdge::getAllEdges());
190 550 : ROCCHMetrics::init(cchGraph, op, begin, weightPeriod);
191 : SUMOAbstractRouter<ROEdge, ROVehicle>* fallback = new AStarRouter<ROEdge, ROVehicle, ROMapMatcher>(
192 1100 : ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op,
193 1650 : nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
194 : // restriction-params are handled natively: each distinct
195 : // restriction profile gets its own masked metric (see
196 : // ROCCHMetrics::get); the period-end hook lets queries whose
197 : // trip crosses a weight-period boundary re-query on the next
198 : // period's metric.
199 : router = new CCHRouter<ROEdge, ROVehicle, ROCCHGraph>(
200 : cchGraph, &ROCCHMetrics::get, op,
201 1100 : oc.getBool("ignore-errors"), fallback,
202 1100 : hasWeights ? &ROCCHMetrics::periodEnd : nullptr);
203 0 : } else if (routingAlgorithm == "arcflag") {
204 : /// @brief The number of levels in the k-d tree partition
205 : constexpr auto NUMBER_OF_LEVELS = 5; //or 4 or 8
206 0 : ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
207 0 : KDTreePartition<ROEdge, RONode, ROVehicle>* partition = new KDTreePartition<ROEdge, RONode, ROVehicle>(NUMBER_OF_LEVELS, ROEdge::getAllEdges(), net.hasPermissions(), oc.isSet("restriction-params"));
208 0 : partition->init(&defaultVehicle);
209 0 : auto reversedTtFunction = gWeightsRandomFactor > 1 ? &FlippedEdge<ROEdge, RONode, ROVehicle>::getTravelTimeStaticRandomized : &FlippedEdge<ROEdge, RONode, ROVehicle>::getTravelTimeStatic;
210 0 : router = new AFRouter<ROEdge, RONode, ROVehicle, ROMapMatcher>(ROEdge::getAllEdges(),
211 0 : partition, oc.getBool("ignore-errors"), ttFunction, reversedTtFunction, (oc.isSet("weight-files") ? string2time(oc.getString("weight-period")) : SUMOTime_MAX),
212 0 : nullptr, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
213 0 : } else {
214 0 : throw ProcessError(TLF("Unknown routing algorithm '%'!", routingAlgorithm));
215 : }
216 : } else {
217 11 : if (measure == "CO") {
218 : op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
219 8 : } else if (measure == "CO2") {
220 : op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
221 7 : } else if (measure == "PMx") {
222 : op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
223 6 : } else if (measure == "HC") {
224 : op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
225 4 : } else if (measure == "NOx") {
226 : op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
227 3 : } else if (measure == "fuel") {
228 : op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
229 2 : } else if (measure == "electricity") {
230 : op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
231 2 : } else if (measure == "noise") {
232 : op = &ROEdge::getNoiseEffort;
233 : } else {
234 : op = &ROEdge::getStoredEffort;
235 : }
236 11 : if (measure != "traveltime" && !net.hasLoadedEffort()) {
237 6 : WRITE_WARNINGF(TL("No weight data was loaded for attribute '%'."), measure);
238 : }
239 : router = new DijkstraRouter<ROEdge, ROVehicle>(
240 22 : ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
241 : }
242 3309 : const int carWalk = SUMOVehicleParserHelper::parseCarWalkTransfer(oc, true);
243 6681 : double taxiWait = STEPS2TIME(string2time(OptionsCont::getOptions().getString("persontrip.taxi.waiting-time")));
244 :
245 : RailwayRouter<ROEdge, ROVehicle>* railRouter = nullptr;
246 3309 : if (net.hasBidiEdges()) {
247 176 : railRouter = new RailwayRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), true, op, ttFunction, false, net.hasPermissions(),
248 176 : oc.isSet("restriction-params"),
249 88 : oc.getFloat("railway.max-train-length"),
250 352 : oc.getFloat("weights.reversal-penalty"));
251 : }
252 3309 : RORouterProvider provider(router, new PedestrianRouter<ROEdge, ROLane, RONode, ROVehicle>(),
253 3309 : new ROIntermodalRouter(RONet::adaptIntermodalRouter, carWalk, taxiWait, routingAlgorithm),
254 : railRouter);
255 : // process route definitions
256 : try {
257 3309 : net.openOutput(oc);
258 6568 : loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
259 3264 : net.writeIntermodal(oc, provider.getIntermodalRouter());
260 : // end the processing
261 3264 : net.cleanup();
262 45 : } catch (ProcessError&) {
263 45 : net.cleanup();
264 45 : throw;
265 45 : }
266 6573 : }
267 :
268 :
269 : /* -------------------------------------------------------------------------
270 : * main
271 : * ----------------------------------------------------------------------- */
272 : int
273 4450 : main(int argc, char** argv) {
274 4450 : OptionsCont& oc = OptionsCont::getOptions();
275 4450 : oc.setApplicationDescription(TL("Shortest path router and DUE computer for the microscopic, multi-modal traffic simulation SUMO."));
276 8900 : oc.setApplicationName("duarouter", "Eclipse SUMO duarouter " VERSION_STRING);
277 : int ret = 0;
278 : RONet* net = nullptr;
279 : try {
280 4450 : XMLSubSys::init();
281 4450 : RODUAFrame::fillOptions();
282 4450 : OptionsIO::setArgs(argc, argv);
283 4450 : OptionsIO::getOptions();
284 4445 : if (oc.processMetaOptions(argc < 2)) {
285 258 : SystemFrame::close();
286 258 : return 0;
287 : }
288 4187 : SystemFrame::checkOptions(oc);
289 12561 : XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
290 : #ifdef HAVE_FOX
291 8374 : if (oc.getInt("routing-threads") > 1) {
292 : // make the output aware of threading
293 : MsgHandler::setFactory(&MsgHandlerSynchronized::create);
294 : }
295 : #endif
296 4187 : MsgHandler::initOutputOptions();
297 4187 : if (!RODUAFrame::checkOptions()) {
298 55 : throw ProcessError();
299 : }
300 4132 : RandHelper::initRandGlobal();
301 : // load data
302 5293 : ROLoader loader(oc, false, !oc.getBool("no-step-log"));
303 4132 : net = new RONet();
304 4132 : initNet(*net, loader, oc);
305 : // build routes
306 : try {
307 3883 : computeRoutes(*net, loader, oc);
308 619 : } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
309 0 : WRITE_ERROR(toString(e.getLineNumber()));
310 : ret = 1;
311 0 : } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
312 0 : WRITE_ERROR(XMLSubSys::transcode(e.getMessage()));
313 : ret = 1;
314 0 : }
315 3264 : if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
316 233 : throw ProcessError();
317 : }
318 5293 : } catch (const ProcessError& e) {
319 2314 : if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
320 1078 : WRITE_ERROR(e.what());
321 : }
322 1161 : MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
323 : ret = 1;
324 : #ifndef _DEBUG
325 1161 : } catch (const std::exception& e) {
326 0 : if (std::string(e.what()) != std::string("")) {
327 0 : WRITE_ERROR(e.what());
328 : }
329 0 : MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
330 : ret = 1;
331 0 : } catch (...) {
332 0 : MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
333 : ret = 1;
334 : #endif
335 0 : }
336 4192 : delete net;
337 4192 : SystemFrame::close();
338 4192 : if (ret == 0) {
339 : std::cout << "Success." << std::endl;
340 : }
341 : return ret;
342 : }
343 :
344 :
345 : /****************************************************************************/
|