Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/duarouter/duarouter_main.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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/vehicle/SUMOVehicleParserHelper.h>
55
#include <utils/xml/XMLSubSys.h>
56
#include <router/ROFrame.h>
57
#include <router/ROLoader.h>
58
#include <router/RONet.h>
59
#include <router/ROEdge.h>
60
#include "RODUAEdgeBuilder.h"
61
#include "RODUAFrame.h"
62
63
64
// ===========================================================================
65
// functions
66
// ===========================================================================
67
/* -------------------------------------------------------------------------
68
* data processing methods
69
* ----------------------------------------------------------------------- */
70
/**
71
* loads the net
72
* The net is in this meaning made up by the net itself and the dynamic
73
* weights which may be supplied in a separate file
74
*/
75
void
76
initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
77
// load the net
78
RODUAEdgeBuilder builder;
79
ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
80
loader.loadNet(net, builder);
81
// load the weights when wished/available
82
if (oc.isSet("weight-files")) {
83
loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
84
}
85
if (oc.isSet("lane-weight-files")) {
86
loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
87
}
88
}
89
90
91
/**
92
* Computes the routes saving them
93
*/
94
void
95
computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
96
// initialise the loader
97
loader.openRoutes(net);
98
// build the router
99
auto ttFunction = gWeightsRandomFactor > 1 ? &ROEdge::getTravelTimeStaticRandomized : &ROEdge::getTravelTimeStatic;
100
SUMOAbstractRouter<ROEdge, ROVehicle>* router;
101
const std::string measure = oc.getString("weight-attribute");
102
const std::string routingAlgorithm = oc.getString("routing-algorithm");
103
const double priorityFactor = oc.getFloat("weights.priority-factor");
104
const SUMOTime begin = string2time(oc.getString("begin"));
105
const SUMOTime end = oc.isDefault("end") ? SUMOTime_MAX : string2time(oc.getString("end"));
106
DijkstraRouter<ROEdge, ROVehicle>::Operation op = &ROEdge::getTravelTimeStatic;
107
108
if (oc.isSet("restriction-params") &&
109
(routingAlgorithm == "CH" || routingAlgorithm == "CHWrapper")) {
110
throw ProcessError(TLF("Routing algorithm '%' does not support restriction-params", routingAlgorithm));
111
}
112
113
if (measure == "traveltime" && priorityFactor == 0) {
114
if (routingAlgorithm == "dijkstra") {
115
router = new DijkstraRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, nullptr, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
116
} else if (routingAlgorithm == "astar") {
117
typedef AStarRouter<ROEdge, ROVehicle, ROMapMatcher> AStar;
118
std::shared_ptr<const AStar::LookupTable> lookup;
119
if (oc.isSet("astar.all-distances")) {
120
lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
121
} else if (oc.isSet("astar.landmark-distances")) {
122
/* CHRouterWrapper<ROEdge, ROVehicle> chrouter(
123
ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic,
124
begin, end, SUMOTime_MAX, 1); */
125
DijkstraRouter<ROEdge, ROVehicle> forward(ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic);
126
std::vector<ReversedEdge<ROEdge, ROVehicle>*> reversed;
127
for (ROEdge* edge : ROEdge::getAllEdges()) {
128
reversed.push_back(edge->getReversedRoutingEdge());
129
}
130
for (ReversedEdge<ROEdge, ROVehicle>* redge : reversed) {
131
redge->init();
132
}
133
DijkstraRouter<ReversedEdge<ROEdge, ROVehicle>, ROVehicle> backward(reversed, true, &ReversedEdge<ROEdge, ROVehicle>::getTravelTimeStatic);
134
ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
135
ROMapMatcher* mapMatcher = dynamic_cast<ROMapMatcher*>(loader.getRouteHandler());
136
lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &forward, &backward, &defaultVehicle,
137
oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"), mapMatcher);
138
}
139
router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, lookup, net.hasPermissions(), oc.isSet("restriction-params"));
140
} else if (routingAlgorithm == "CH" && !net.hasPermissions()) {
141
const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
142
string2time(oc.getString("weight-period")) :
143
SUMOTime_MAX);
144
router = new CHRouter<ROEdge, ROVehicle>(
145
ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, SVC_IGNORING, weightPeriod, net.hasPermissions(), oc.isSet("restriction-params"));
146
} else if (routingAlgorithm == "CHWrapper" || routingAlgorithm == "CH") {
147
// use CHWrapper instead of CH if the net has permissions
148
const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
149
string2time(oc.getString("weight-period")) :
150
SUMOTime_MAX);
151
router = new CHRouterWrapper<ROEdge, ROVehicle>(
152
ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction,
153
begin, end, weightPeriod, net.hasPermissions(), oc.getInt("routing-threads"));
154
} else if (routingAlgorithm == "arcflag") {
155
/// @brief The number of levels in the k-d tree partition
156
constexpr auto NUMBER_OF_LEVELS = 5; //or 4 or 8
157
ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
158
KDTreePartition<ROEdge, RONode, ROVehicle>* partition = new KDTreePartition<ROEdge, RONode, ROVehicle>(NUMBER_OF_LEVELS, ROEdge::getAllEdges(), net.hasPermissions(), oc.isSet("restriction-params"));
159
partition->init(&defaultVehicle);
160
auto reversedTtFunction = gWeightsRandomFactor > 1 ? &FlippedEdge<ROEdge, RONode, ROVehicle>::getTravelTimeStaticRandomized : &FlippedEdge<ROEdge, RONode, ROVehicle>::getTravelTimeStatic;
161
router = new AFRouter<ROEdge, RONode, ROVehicle, ROMapMatcher>(ROEdge::getAllEdges(),
162
partition, oc.getBool("ignore-errors"), ttFunction, reversedTtFunction, (oc.isSet("weight-files") ? string2time(oc.getString("weight-period")) : SUMOTime_MAX),
163
nullptr, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
164
} else {
165
throw ProcessError(TLF("Unknown routing Algorithm '%'!", routingAlgorithm));
166
}
167
} else {
168
if (measure == "traveltime") {
169
if (ROEdge::initPriorityFactor(priorityFactor)) {
170
op = &ROEdge::getTravelTimeStaticPriorityFactor;
171
}
172
} else if (measure == "CO") {
173
op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
174
} else if (measure == "CO2") {
175
op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
176
} else if (measure == "PMx") {
177
op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
178
} else if (measure == "HC") {
179
op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
180
} else if (measure == "NOx") {
181
op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
182
} else if (measure == "fuel") {
183
op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
184
} else if (measure == "electricity") {
185
op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
186
} else if (measure == "noise") {
187
op = &ROEdge::getNoiseEffort;
188
} else {
189
op = &ROEdge::getStoredEffort;
190
}
191
if (measure != "traveltime" && !net.hasLoadedEffort()) {
192
WRITE_WARNINGF(TL("No weight data was loaded for attribute '%'."), measure);
193
}
194
router = new DijkstraRouter<ROEdge, ROVehicle>(
195
ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
196
}
197
const int carWalk = SUMOVehicleParserHelper::parseCarWalkTransfer(oc);
198
double taxiWait = STEPS2TIME(string2time(OptionsCont::getOptions().getString("persontrip.taxi.waiting-time")));
199
200
RailwayRouter<ROEdge, ROVehicle>* railRouter = nullptr;
201
if (net.hasBidiEdges()) {
202
railRouter = new RailwayRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), true, op, ttFunction, false, net.hasPermissions(),
203
oc.isSet("restriction-params"),
204
oc.getFloat("railway.max-train-length"));
205
}
206
RORouterProvider provider(router, new PedestrianRouter<ROEdge, ROLane, RONode, ROVehicle>(),
207
new ROIntermodalRouter(RONet::adaptIntermodalRouter, carWalk, taxiWait, routingAlgorithm),
208
railRouter);
209
// process route definitions
210
try {
211
net.openOutput(oc);
212
loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
213
net.writeIntermodal(oc, provider.getIntermodalRouter());
214
// end the processing
215
net.cleanup();
216
} catch (ProcessError&) {
217
net.cleanup();
218
throw;
219
}
220
}
221
222
223
/* -------------------------------------------------------------------------
224
* main
225
* ----------------------------------------------------------------------- */
226
int
227
main(int argc, char** argv) {
228
OptionsCont& oc = OptionsCont::getOptions();
229
oc.setApplicationDescription(TL("Shortest path router and DUE computer for the microscopic, multi-modal traffic simulation SUMO."));
230
oc.setApplicationName("duarouter", "Eclipse SUMO duarouter " VERSION_STRING);
231
int ret = 0;
232
RONet* net = nullptr;
233
try {
234
XMLSubSys::init();
235
RODUAFrame::fillOptions();
236
OptionsIO::setArgs(argc, argv);
237
OptionsIO::getOptions();
238
if (oc.processMetaOptions(argc < 2)) {
239
SystemFrame::close();
240
return 0;
241
}
242
SystemFrame::checkOptions(oc);
243
XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
244
#ifdef HAVE_FOX
245
if (oc.getInt("routing-threads") > 1) {
246
// make the output aware of threading
247
MsgHandler::setFactory(&MsgHandlerSynchronized::create);
248
}
249
#endif
250
MsgHandler::initOutputOptions();
251
if (!RODUAFrame::checkOptions()) {
252
throw ProcessError();
253
}
254
RandHelper::initRandGlobal();
255
// load data
256
ROLoader loader(oc, false, !oc.getBool("no-step-log"));
257
net = new RONet();
258
initNet(*net, loader, oc);
259
// build routes
260
try {
261
computeRoutes(*net, loader, oc);
262
} catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
263
WRITE_ERROR(toString(e.getLineNumber()));
264
ret = 1;
265
} catch (XERCES_CPP_NAMESPACE::SAXException& e) {
266
WRITE_ERROR(StringUtils::transcode(e.getMessage()));
267
ret = 1;
268
}
269
if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
270
throw ProcessError();
271
}
272
} catch (const ProcessError& e) {
273
if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
274
WRITE_ERROR(e.what());
275
}
276
MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
277
ret = 1;
278
#ifndef _DEBUG
279
} catch (const std::exception& e) {
280
if (std::string(e.what()) != std::string("")) {
281
WRITE_ERROR(e.what());
282
}
283
MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
284
ret = 1;
285
} catch (...) {
286
MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
287
ret = 1;
288
#endif
289
}
290
delete net;
291
SystemFrame::close();
292
if (ret == 0) {
293
std::cout << "Success." << std::endl;
294
}
295
return ret;
296
}
297
298
299
/****************************************************************************/
300
301