SUMO - Simulation of Urban MObility
od2trips_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Main for OD2TRIPS
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2002-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #ifdef HAVE_VERSION_H
36 #include <version.h>
37 #endif
38 
39 #include <iostream>
40 #include <algorithm>
41 #include <cmath>
42 #include <cstdlib>
43 #include <string>
44 #include <xercesc/parsers/SAXParser.hpp>
45 #include <xercesc/sax2/SAX2XMLReader.hpp>
46 #include <utils/options/Option.h>
53 #include <utils/common/ToString.h>
54 #include <utils/xml/XMLSubSys.h>
56 #include <od/ODDistrictCont.h>
57 #include <od/ODDistrictHandler.h>
58 #include <od/ODMatrix.h>
60 #include <utils/common/SUMOTime.h>
66 
67 
68 // ===========================================================================
69 // functions
70 // ===========================================================================
71 void
74  oc.addCallExample("-c <CONFIGURATION>", "run with configuration file");
75 
76  // insert options sub-topics
77  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
78  oc.addOptionSubTopic("Input");
79  oc.addOptionSubTopic("Output");
80  oc.addOptionSubTopic("Time");
81  oc.addOptionSubTopic("Processing");
82  oc.addOptionSubTopic("Defaults");
83  SystemFrame::addReportOptions(oc); // fill this subtopic, too
84 
85 
86  // register the file input options
87  oc.doRegister("taz-files", 'n', new Option_FileName());
88  oc.addSynonyme("taz-files", "taz");
89  oc.addSynonyme("taz-files", "net-file");
90  oc.addSynonyme("taz-files", "net");
91  oc.addDescription("taz-files", "Input", "Loads TAZ (districts; also from networks) from FILE(s)");
92 
93  oc.doRegister("od-matrix-files", 'd', new Option_FileName());
94  oc.addSynonyme("od-matrix-files", "od-files");
95  oc.addSynonyme("od-matrix-files", "od");
96  oc.addDescription("od-matrix-files", "Input", "Loads O/D-files from FILE(s)");
97 
98  oc.doRegister("od-amitran-files", new Option_FileName());
99  oc.addSynonyme("od-amitran-files", "amitran-files");
100  oc.addSynonyme("od-amitran-files", "amitran");
101  oc.addDescription("od-amitran-files", "Input", "Loads O/D-matrix in Amitran format from FILE(s)");
102 
103 
104  // register the file output options
105  oc.doRegister("output-file", 'o', new Option_FileName());
106  oc.addSynonyme("output-file", "output", true);
107  oc.addDescription("output-file", "Output", "Writes trip definitions into FILE");
108 
109  oc.doRegister("flow-output", new Option_FileName());
110  oc.addDescription("flow-output", "Output", "Writes flow definitions into FILE");
111 
112  oc.doRegister("flow-output.probability", new Option_Bool(false));
113  oc.addDescription("flow-output.probability", "Output", "Writes probabilistic flow instead of evenly spaced flow");
114 
115  oc.doRegister("pedestrians", new Option_Bool(false));
116  oc.addDescription("pedestrians", "Output", "Writes pedestrians instead of vehicles");
117 
118  oc.doRegister("persontrips", new Option_Bool(false));
119  oc.addDescription("persontrips", "Output", "Writes persontrips instead of vehicles");
120 
121  oc.doRegister("ignore-vehicle-type", new Option_Bool(false));
122  oc.addSynonyme("ignore-vehicle-type", "no-vtype", true);
123  oc.addDescription("ignore-vehicle-type", "Output", "Does not save vtype information");
124 
125 
126  // register the time settings
127  oc.doRegister("begin", 'b', new Option_String("0", "TIME"));
128  oc.addDescription("begin", "Time", "Defines the begin time; Previous trips will be discarded");
129 
130  oc.doRegister("end", 'e', new Option_String(SUMOTIME_MAXSTRING, "TIME"));
131  oc.addDescription("end", "Time", "Defines the end time; Later trips will be discarded; Defaults to the maximum time that SUMO can represent");
132 
133 
134  // register the data processing options
135  oc.doRegister("scale", 's', new Option_Float(1));
136  oc.addDescription("scale", "Processing", "Scales the loaded flows by FLOAT");
137 
138  oc.doRegister("spread.uniform", new Option_Bool(false));
139  oc.addDescription("spread.uniform", "Processing", "Spreads trips uniformly over each time period");
140 
141  oc.doRegister("different-source-sink", new Option_Bool(false));
142  oc.addDescription("different-source-sink", "Processing", "Always choose source and sink edge which are not identical");
143 
144  oc.doRegister("vtype", new Option_String(""));
145  oc.addDescription("vtype", "Processing", "Defines the name of the vehicle type to use");
146 
147  oc.doRegister("prefix", new Option_String(""));
148  oc.addDescription("prefix", "Processing", "Defines the prefix for vehicle names");
149 
150  oc.doRegister("timeline", new Option_String());
151  oc.addDescription("timeline", "Processing", "Uses STR as a timeline definition");
152 
153  oc.doRegister("timeline.day-in-hours", new Option_Bool(false));
154  oc.addDescription("timeline.day-in-hours", "Processing", "Uses STR as a 24h-timeline definition");
155 
156  oc.doRegister("ignore-errors", new Option_Bool(false)); // !!! describe, document
157  oc.addSynonyme("ignore-errors", "dismiss-loading-errors", true);
158  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
159 
160  oc.doRegister("no-step-log", new Option_Bool(false));
161  oc.addDescription("no-step-log", "Processing", "Disable console output of current time step");
162 
163 
164  // register defaults options
165  oc.doRegister("departlane", new Option_String("free"));
166  oc.addDescription("departlane", "Defaults", "Assigns a default depart lane");
167 
168  oc.doRegister("departpos", new Option_String());
169  oc.addDescription("departpos", "Defaults", "Assigns a default depart position");
170 
171  oc.doRegister("departspeed", new Option_String("max"));
172  oc.addDescription("departspeed", "Defaults", "Assigns a default depart speed");
173 
174  oc.doRegister("arrivallane", new Option_String());
175  oc.addDescription("arrivallane", "Defaults", "Assigns a default arrival lane");
176 
177  oc.doRegister("arrivalpos", new Option_String());
178  oc.addDescription("arrivalpos", "Defaults", "Assigns a default arrival position");
179 
180  oc.doRegister("arrivalspeed", new Option_String());
181  oc.addDescription("arrivalspeed", "Defaults", "Assigns a default arrival speed");
182 
183  // add rand options
185 }
186 
187 bool
190  bool ok = true;
191  if (!oc.isSet("taz-files")) {
192  WRITE_ERROR("No TAZ input file (-n) specified.");
193  ok = false;
194  }
195  if (!oc.isSet("od-matrix-files") && !oc.isSet("od-amitran-files")) {
196  WRITE_ERROR("No input specified.");
197  ok = false;
198  }
199  if (!oc.isSet("output-file")) {
200  WRITE_ERROR("No trip table output file (-o) specified.");
201  ok = false;
202  }
203  if (oc.getBool("pedestrians") && oc.getBool("persontrips")) {
204  WRITE_ERROR("Only of the the options 'pedestrians' and 'persontrips' may be set.");
205  ok = false;
206  }
207  //
209  std::string error;
210  if (oc.isSet("departlane") && !SUMOVehicleParameter::parseDepartLane(oc.getString("departlane"), "option", "departlane", p.departLane, p.departLaneProcedure, error)) {
211  WRITE_ERROR(error);
212  ok = false;
213  }
214  if (oc.isSet("departpos") && !SUMOVehicleParameter::parseDepartPos(oc.getString("departpos"), "option", "departpos", p.departPos, p.departPosProcedure, error)) {
215  WRITE_ERROR(error);
216  ok = false;
217  }
218  if (oc.isSet("departspeed") && !SUMOVehicleParameter::parseDepartSpeed(oc.getString("departspeed"), "option", "departspeed", p.departSpeed, p.departSpeedProcedure, error)) {
219  WRITE_ERROR(error);
220  ok = false;
221  }
222  if (oc.isSet("arrivallane") && !SUMOVehicleParameter::parseArrivalLane(oc.getString("arrivallane"), "option", "arrivallane", p.arrivalLane, p.arrivalLaneProcedure, error)) {
223  WRITE_ERROR(error);
224  ok = false;
225  }
226  if (oc.isSet("arrivalpos") && !SUMOVehicleParameter::parseArrivalPos(oc.getString("arrivalpos"), "option", "arrivalpos", p.arrivalPos, p.arrivalPosProcedure, error)) {
227  WRITE_ERROR(error);
228  ok = false;
229  }
230  if (oc.isSet("arrivalspeed") && !SUMOVehicleParameter::parseArrivalSpeed(oc.getString("arrivalspeed"), "option", "arrivalspeed", p.arrivalSpeed, p.arrivalSpeedProcedure, error)) {
231  WRITE_ERROR(error);
232  ok = false;
233  }
234  return ok;
235 }
236 
237 
238 
239 
240 /* -------------------------------------------------------------------------
241  * main
242  * ----------------------------------------------------------------------- */
243 int
244 main(int argc, char** argv) {
246  // give some application descriptions
247  oc.setApplicationDescription("Importer of O/D-matrices for the road traffic simulation SUMO.");
248  oc.setApplicationName("od2trips", "SUMO od2trips Version " VERSION_STRING);
249  int ret = 0;
250  try {
251  // initialise subsystems
252  XMLSubSys::init();
253  fillOptions();
254  OptionsIO::setArgs(argc, argv);
256  if (oc.processMetaOptions(argc < 2)) {
258  return 0;
259  }
260  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
262  if (!checkOptions()) {
263  throw ProcessError();
264  }
266  // load the districts
267  // check whether the user gave a net filename
268  if (!oc.isSet("taz-files")) {
269  throw ProcessError("You must supply a TAZ, network or districts file ('-n').");
270  }
271  // get the file name and set it
272  ODDistrictCont districts;
273  districts.loadDistricts(oc.getStringVector("taz-files"));
274  if (districts.size() == 0) {
275  throw ProcessError("No districts loaded.");
276  }
277  // load the matrix
278  ODMatrix matrix(districts);
279  matrix.loadMatrix(oc);
280  if (matrix.getNumLoaded() == 0) {
281  throw ProcessError("No vehicles loaded.");
282  }
283  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
284  throw ProcessError("Loading failed.");
285  }
286  WRITE_MESSAGE(toString(matrix.getNumLoaded()) + " vehicles loaded.");
287  // apply a curve if wished
288  if (oc.isSet("timeline")) {
289  matrix.applyCurve(matrix.parseTimeLine(oc.getStringVector("timeline"), oc.getBool("timeline.day-in-hours")));
290  }
291  // write
292  bool haveOutput = false;
293  if (OutputDevice::createDeviceByOption("output-file", "routes", "routes_file.xsd")) {
294  matrix.write(string2time(oc.getString("begin")), string2time(oc.getString("end")),
295  OutputDevice::getDeviceByOption("output-file"),
296  oc.getBool("spread.uniform"), oc.getBool("different-source-sink"),
297  oc.getBool("ignore-vehicle-type"),
298  oc.getString("prefix"), !oc.getBool("no-step-log"),
299  oc.getBool("pedestrians"),
300  oc.getBool("persontrips"));
301  haveOutput = true;
302  }
303  if (OutputDevice::createDeviceByOption("flow-output", "routes", "routes_file.xsd")) {
304  matrix.writeFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
305  OutputDevice::getDeviceByOption("flow-output"),
306  oc.getBool("ignore-vehicle-type"), oc.getString("prefix"),
307  oc.getBool("flow-output.probability"));
308  haveOutput = true;
309  }
310  if (!haveOutput) {
311  throw ProcessError("No output file given.");
312  }
313  WRITE_MESSAGE(toString(matrix.getNumDiscarded()) + " vehicles discarded.");
314  WRITE_MESSAGE(toString(matrix.getNumWritten()) + " vehicles written.");
315  } catch (const ProcessError& e) {
316  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
317  WRITE_ERROR(e.what());
318  }
319  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
320  ret = 1;
321 #ifndef _DEBUG
322  } catch (const std::exception& e) {
323  if (std::string(e.what()) != std::string("")) {
324  WRITE_ERROR(e.what());
325  }
326  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
327  ret = 1;
328  } catch (...) {
329  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
330  ret = 1;
331 #endif
332  }
334  if (ret == 0) {
335  std::cout << "Success." << std::endl;
336  }
337  return ret;
338 }
339 
340 
341 
342 /****************************************************************************/
343 
void write(SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool uniform, const bool differSourceSink, const bool noVtype, const std::string &prefix, const bool stepLog, bool pedestrians, bool persontrips)
Writes the vehicles stored in the matrix assigning the sources and sinks.
Definition: ODMatrix.cpp:217
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:82
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
bool checkOptions()
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:48
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:82
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:72
double getNumWritten() const
Returns the number of written vehicles.
Definition: ODMatrix.cpp:520
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
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)
double getNumDiscarded() const
Returns the number of discarded vehicles.
Definition: ODMatrix.cpp:526
ArrivalSpeedDefinition arrivalSpeedProcedure
Information how the vehicle&#39;s end speed shall be chosen.
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:47
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:62
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
void loadMatrix(OptionsCont &oc)
read a matrix in one of several formats
Definition: ODMatrix.cpp:561
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
double departSpeed
(optional) The initial speed of the vehicle
DepartSpeedDefinition departSpeedProcedure
Information how the vehicle&#39;s initial speed shall be chosen.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
An O/D (origin/destination) matrix.
Definition: ODMatrix.h:76
void fillOptions()
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
int arrivalLane
(optional) The lane the vehicle shall arrive on (not used yet)
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:47
A container for districts.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static bool parseArrivalLane(const std::string &val, const std::string &element, const std::string &id, int &lane, ArrivalLaneDefinition &ald, std::string &error)
Validates a given arrivalLane value.
#define VERSION_STRING
Definition: version.h:1
#define SUMOTIME_MAXSTRING
Definition: SUMOTime.h:46
int departLane
(optional) The lane the vehicle shall depart from (index in edge)
void loadDistricts(std::vector< std::string > files)
load districts from files
double arrivalPos
(optional) The position the vehicle shall arrive on
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
double departPos
(optional) The position the vehicle shall depart from
int main(int argc, char **argv)
Structure representing possible vehicle parameter.
static bool parseDepartPos(const std::string &val, const std::string &element, const std::string &id, double &pos, DepartPosDefinition &dpd, std::string &error)
Validates a given departPos value.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
static bool parseArrivalSpeed(const std::string &val, const std::string &element, const std::string &id, double &speed, ArrivalSpeedDefinition &asd, std::string &error)
Validates a given arrivalSpeed value.
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
double getNumLoaded() const
Returns the number of loaded vehicles.
Definition: ODMatrix.cpp:514
static bool parseArrivalPos(const std::string &val, const std::string &element, const std::string &id, double &pos, ArrivalPosDefinition &apd, std::string &error)
Validates a given arrivalPos value.
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:77
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
Definition: ODMatrix.cpp:548
static bool parseDepartSpeed(const std::string &val, const std::string &element, const std::string &id, double &speed, DepartSpeedDefinition &dsd, std::string &error)
Validates a given departSpeed value.
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="")
Creates the device using the output definition stored in the named option.
double arrivalSpeed
(optional) The final speed of the vehicle (not used yet)
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
static void initOutputOptions()
Definition: MsgHandler.cpp:193
void writeFlows(const SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool noVtype, const std::string &prefix, bool asProbability=false)
Writes the flows stored in the matrix.
Definition: ODMatrix.cpp:306
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
Definition: ODMatrix.cpp:624
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
int size() const
Returns the number of stored items within the container.
static bool parseDepartLane(const std::string &val, const std::string &element, const std::string &id, int &lane, DepartLaneDefinition &dld, std::string &error)
Validates a given departLane value.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.