Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
dfrouter_main.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 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/****************************************************************************/
23// Main for the DFROUTER
24/****************************************************************************/
25#include <config.h>
26
27#ifdef HAVE_VERSION_H
28#include <version.h>
29#endif
30
31#include <iostream>
32#include <string>
33#include <limits.h>
34#include <ctime>
35#include <xercesc/sax/SAXException.hpp>
36#include <xercesc/sax/SAXParseException.hpp>
47#include <utils/xml/XMLSubSys.h>
48#include <router/ROLoader.h>
49#include <router/RONet.h>
50#include "RODFEdgeBuilder.h"
51#include "RODFFrame.h"
52#include "RODFNet.h"
53#include "RODFEdge.h"
54#include "RODFDetector.h"
55#include "RODFDetectorHandler.h"
56#include "RODFRouteCont.h"
57#include "RODFDetectorFlow.h"
58#include "RODFDetFlowLoader.h"
59
60
61// ===========================================================================
62// functions
63// ===========================================================================
64/* -------------------------------------------------------------------------
65 * data processing methods
66 * ----------------------------------------------------------------------- */
67void
69 if (!oc.isSet("detector-files")) {
70 throw ProcessError(TL("No detector file given (use --detector-files <FILE>)."));
71 }
72 // read definitions stored in XML-format
73 std::vector<std::string> files = oc.getStringVector("detector-files");
74 for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
75 if (!FileHelpers::isReadable(*fileIt)) {
76 throw ProcessError(TLF("Could not open detector file '%'", *fileIt));
77 }
78 PROGRESS_BEGIN_MESSAGE("Loading detector definitions from '" + *fileIt + "'");
79 RODFDetectorHandler handler(optNet, oc.getBool("ignore-invalid-detectors"), detectors, *fileIt);
80 if (XMLSubSys::runParser(handler, *fileIt)) {
82 } else {
84 throw ProcessError();
85 }
86 }
87 if (detectors.getDetectors().empty()) {
88 throw ProcessError(TL("No detectors found."));
89 }
90}
91
92
93void
95 if (!oc.isSet("measure-files")) {
96 // ok, not given, return an empty container
97 return;
98 }
99 // check whether the file exists
100 std::vector<std::string> files = oc.getStringVector("measure-files");
101 for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
102 if (!FileHelpers::isReadable(*fileIt)) {
103 throw ProcessError(TLF("The measure-file '%' can not be opened.", *fileIt));
104 }
105 // parse
106 PROGRESS_BEGIN_MESSAGE("Loading flows from '" + *fileIt + "'");
107 RODFDetFlowLoader dfl(dc, flows, string2time(oc.getString("begin")), string2time(oc.getString("end")),
108 string2time(oc.getString("time-offset")), string2time(oc.getString("time-factor")));
109 dfl.read(*fileIt);
111 }
112}
113
114
115void
117 if (oc.getBool("print-absolute-flows")) {
118 flows.printAbsolute();
119 }
120
121 if (oc.getBool("remove-empty-detectors")) {
122 PROGRESS_BEGIN_MESSAGE(TL("Removing empty detectors"));
123 optNet->removeEmptyDetectors(detectors, flows);
125 } else if (oc.getBool("report-empty-detectors")) {
126 PROGRESS_BEGIN_MESSAGE(TL("Scanning for empty detectors"));
127 optNet->reportEmptyDetectors(detectors, flows);
129 }
130 // compute the detector types (optionally)
131 if (!detectors.detectorsHaveCompleteTypes() || oc.getBool("revalidate-detectors")) {
132 optNet->computeTypes(detectors, oc.getBool("strict-sources"));
133 }
134 std::vector<RODFDetector*>::const_iterator i = detectors.getDetectors().begin();
135 for (; i != detectors.getDetectors().end(); ++i) {
136 if ((*i)->getType() == SOURCE_DETECTOR) {
137 break;
138 }
139 }
140 if (i == detectors.getDetectors().end() && !oc.getBool("routes-for-all")) {
141 throw ProcessError(TL("No source detectors found."));
142 }
143 // compute routes between the detectors (optionally)
144 if (!detectors.detectorsHaveRoutes() || oc.getBool("revalidate-routes") || oc.getBool("guess-empty-flows")) {
145 PROGRESS_BEGIN_MESSAGE(TL("Computing routes"));
146 optNet->buildRoutes(detectors,
147 oc.getBool("keep-unfinished-routes"), oc.getBool("routes-for-all"),
148 !oc.getBool("keep-longer-routes"), oc.getInt("max-search-depth"));
150 }
151
152 // check
153 // whether the detectors are valid
154 if (!detectors.detectorsHaveCompleteTypes()) {
155 throw ProcessError(TL("The detector types are not defined; use in combination with a network"));
156 }
157 // whether the detectors have routes
158 if (!detectors.detectorsHaveRoutes()) {
159 throw ProcessError(TL("The emitters have no routes; use in combination with a network"));
160 }
161
162 // save the detectors if wished
163 if (oc.isSet("detector-output")) {
164 detectors.save(oc.getString("detector-output"));
165 }
166 // save their positions as POIs if wished
167 if (oc.isSet("detectors-poi-output")) {
168 detectors.saveAsPOIs(oc.getString("detectors-poi-output"));
169 }
170
171 // save the routes file if it was changed or it's wished
172 if (detectors.detectorsHaveRoutes() && oc.isSet("routes-output")) {
173 detectors.saveRoutes(oc.getString("routes-output"));
174 }
175
176 // guess flows if wished
177 if (oc.getBool("guess-empty-flows")) {
178 optNet->buildDetectorDependencies(detectors);
179 detectors.guessEmptyFlows(flows);
180 }
181
182 const SUMOTime begin = string2time(oc.getString("begin"));
183 const SUMOTime end = string2time(oc.getString("end"));
184 const SUMOTime step = string2time(oc.getString("time-step"));
185
186 // save emitters if wished
187 if (oc.isSet("emitters-output") || oc.isSet("emitters-poi-output")) {
188 optNet->buildEdgeFlowMap(flows, detectors, begin, end, step); // !!!
189 if (oc.getBool("revalidate-flows")) {
190 PROGRESS_BEGIN_MESSAGE(TL("Rechecking loaded flows"));
191 optNet->revalidateFlows(detectors, flows, begin, end, step);
193 }
194 if (oc.isSet("emitters-output")) {
195 PROGRESS_BEGIN_MESSAGE(TL("Writing emitters"));
196 detectors.writeEmitters(oc.getString("emitters-output"), flows,
197 begin, end, step,
198 *optNet,
199 oc.getBool("calibrator-output"),
200 oc.getBool("include-unused-routes"),
201 oc.getFloat("scale"),
202// oc.getInt("max-search-depth"),
203 oc.getBool("emissions-only"));
205 }
206 if (oc.isSet("emitters-poi-output")) {
207 PROGRESS_BEGIN_MESSAGE(TL("Writing emitter pois"));
208 detectors.writeEmitterPOIs(oc.getString("emitters-poi-output"), flows);
210 }
211 }
212 // save end speed trigger if wished
213 if (oc.isSet("variable-speed-sign-output")) {
214 PROGRESS_BEGIN_MESSAGE(TL("Writing speed triggers"));
215 detectors.writeSpeedTrigger(optNet, oc.getString("variable-speed-sign-output"), flows,
216 begin, end, step);
218 }
219 // save checking detectors if wished
220 if (oc.isSet("validation-output")) {
221 PROGRESS_BEGIN_MESSAGE(TL("Writing validation detectors"));
222 detectors.writeValidationDetectors(oc.getString("validation-output"),
223 oc.getBool("validation-output.add-sources"), true, true); // !!!
225 }
226 // build global rerouter on end if wished
227 if (oc.isSet("end-reroute-output")) {
228 PROGRESS_BEGIN_MESSAGE(TL("Writing highway end rerouter"));
229 detectors.writeEndRerouterDetectors(oc.getString("end-reroute-output")); // !!!
231 }
232 /*
233 // save the insertion definitions
234 if(oc.isSet("flow-definitions")) {
235 buildVehicleEmissions(oc.getString("flow-definitions"));
236 }
237 */
238 //
239}
240
241
242/* -------------------------------------------------------------------------
243 * main
244 * ----------------------------------------------------------------------- */
245int
246main(int argc, char** argv) {
248 oc.setApplicationDescription(TL("Builds vehicle routes for SUMO using detector values."));
249 oc.setApplicationName("dfrouter", "Eclipse SUMO dfrouter Version " VERSION_STRING);
250 int ret = 0;
251 RODFNet* net = nullptr;
252 RODFDetectorCon* detectors = nullptr;
253 RODFDetectorFlows* flows = nullptr;
254 try {
255 // initialise the application system (messaging, xml, options)
258 OptionsIO::setArgs(argc, argv);
260 if (oc.processMetaOptions(argc < 2)) {
262 return 0;
263 }
265 XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), "never");
268 throw ProcessError();
269 }
271 // load data
272 ROLoader loader(oc, false, !oc.getBool("no-step-log"));
273 net = new RODFNet(oc.getBool("highway-mode"));
274 RODFEdgeBuilder builder;
275 loader.loadNet(*net, builder);
276 net->buildApproachList();
277 // load detectors
278 detectors = new RODFDetectorCon();
279 readDetectors(*detectors, oc, net);
280 // load detector values
281 flows = new RODFDetectorFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
282 string2time(oc.getString("time-step")));
283 readDetectorFlows(*flows, oc, *detectors);
284 // build routes
285 startComputation(net, *flows, *detectors, oc);
286 } catch (const ProcessError& e) {
287 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
288 WRITE_ERROR(e.what());
289 }
290 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
291 ret = 1;
292#ifndef _DEBUG
293 } catch (const std::exception& e) {
294 if (std::string(e.what()) != std::string("")) {
295 WRITE_ERROR(e.what());
296 }
297 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
298 ret = 1;
299 } catch (...) {
300 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
301 ret = 1;
302#endif
303 }
304 delete net;
305 delete flows;
306 delete detectors;
308 if (ret == 0) {
309 std::cout << "Success." << std::endl;
310 }
311 return ret;
312}
313
314
315/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_ERROR(msg)
Definition MsgHandler.h:304
#define TL(string)
Definition MsgHandler.h:315
#define PROGRESS_DONE_MESSAGE()
Definition MsgHandler.h:300
#define TLF(string,...)
Definition MsgHandler.h:317
#define PROGRESS_FAILED_MESSAGE()
Definition MsgHandler.h:303
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition MsgHandler.h:299
@ SOURCE_DETECTOR
A source detector.
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:46
static bool isReadable(std::string path)
Checks whether the given file is readable.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
static void initOutputOptions()
init output options
A storage for options typed value containers)
Definition OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition OptionsIO.cpp:74
A loader for detector flows.
void read(const std::string &file)
Reads the given file assuming it contains detector values.
A container for RODFDetectors.
bool detectorsHaveRoutes() const
void save(const std::string &file) const
void writeEmitterPOIs(const std::string &file, const RODFDetectorFlows &flows)
void writeValidationDetectors(const std::string &file, bool includeSources, bool singleFile, bool friendly)
void guessEmptyFlows(RODFDetectorFlows &flows)
bool detectorsHaveCompleteTypes() const
void saveRoutes(const std::string &file) const
void writeSpeedTrigger(const RODFNet *const net, const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
void writeEmitters(const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset, const RODFNet &net, bool writeCalibrators, bool includeUnusedRoutes, double scale, bool insertionsOnly)
void saveAsPOIs(const std::string &file) const
const std::vector< RODFDetector * > & getDetectors() const
void writeEndRerouterDetectors(const std::string &file)
A container for flows.
SAX2-Handler for loading DFROUTER-detector definitions.
Interface for building instances of dfrouter-edges.
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within dfrouter.
static void fillOptions()
Inserts options used by dfrouter into the OptionsCont-singleton.
Definition RODFFrame.cpp:45
A DFROUTER-network.
Definition RODFNet.h:42
void buildEdgeFlowMap(const RODFDetectorFlows &flows, const RODFDetectorCon &detectors, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
Definition RODFNet.cpp:910
void computeTypes(RODFDetectorCon &dets, bool sourcesStrict) const
Definition RODFNet.cpp:111
void revalidateFlows(const RODFDetectorCon &detectors, RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
Definition RODFNet.cpp:567
void buildRoutes(RODFDetectorCon &det, bool keepUnfoundEnds, bool includeInBetween, bool keepShortestOnly, int maxFollowingLength) const
Definition RODFNet.cpp:343
void buildDetectorDependencies(RODFDetectorCon &detectors)
Definition RODFNet.cpp:1009
void removeEmptyDetectors(RODFDetectorCon &detectors, RODFDetectorFlows &flows)
Definition RODFNet.cpp:581
void buildApproachList()
Definition RODFNet.cpp:73
void reportEmptyDetectors(RODFDetectorCon &detectors, RODFDetectorFlows &flows)
Definition RODFNet.cpp:604
The data loader.
Definition ROLoader.h:53
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition ROLoader.cpp:108
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
static void close()
Closes all of an applications subsystems.
static bool checkOptions(OptionsCont &oc)
checks shared options and sets StdDefs
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition XMLSubSys.cpp:83
static void init()
Initialises the xml-subsystem.
Definition XMLSubSys.cpp:56
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.
int main(int argc, char **argv)
void readDetectors(RODFDetectorCon &detectors, OptionsCont &oc, RODFNet *optNet)
void startComputation(RODFNet *optNet, RODFDetectorFlows &flows, RODFDetectorCon &detectors, OptionsCont &oc)
void readDetectorFlows(RODFDetectorFlows &flows, OptionsCont &oc, RODFDetectorCon &dc)