SUMO - Simulation of Urban MObility
netgen_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Main for NETGENERATE
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #ifdef HAVE_VERSION_H
28 #include <version.h>
29 #endif
30 
31 #include <iostream>
32 #include <fstream>
33 #include <string>
34 #include <ctime>
35 #include <netgen/NGNet.h>
37 #include <netgen/NGFrame.h>
38 #include <netbuild/NBNetBuilder.h>
39 #include <netbuild/NBFrame.h>
40 #include <netwrite/NWFrame.h>
43 #include <utils/options/Option.h>
48 #include <utils/common/ToString.h>
51 #include <utils/xml/XMLSubSys.h>
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
58 void
61  oc.addCallExample("-c <CONFIGURATION>", "create net from given configuration");
62  oc.addCallExample("--grid [grid-network options] -o <OUTPUTFILE>", "create grid net");
63  oc.addCallExample("--spider [spider-network options] -o <OUTPUTFILE>", "create spider net");
64  oc.addCallExample("--rand [random-network options] -o <OUTPUTFILE>", "create random net");
65 
66  oc.setAdditionalHelpMessage(" Either \"--grid\", \"--spider\" or \"--rand\" must be supplied.\n In dependance to these switches other options are used.");
67 
68  // insert options sub-topics
69  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
70  oc.addOptionSubTopic("Grid Network");
71  oc.addOptionSubTopic("Spider Network");
72  oc.addOptionSubTopic("Random Network");
73  oc.addOptionSubTopic("Output");
74  oc.addOptionSubTopic("Processing");
75  oc.addOptionSubTopic("Building Defaults");
76  oc.addOptionSubTopic("TLS Building");
77  oc.addOptionSubTopic("Edge Removal");
78  oc.addOptionSubTopic("Unregulated Nodes");
79  oc.addOptionSubTopic("Junctions");
80  oc.addOptionSubTopic("Pedestrian");
81  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
82 
86  oc.doRegister("default-junction-type", 'j', new Option_String());
87  oc.addSynonyme("default-junction-type", "junctions");
88  oc.addDescription("default-junction-type", "Building Defaults", "[traffic_light|priority|right_before_left|traffic_light_right_on_red|priority_stop|allway_stop|...] Determines junction type (see wiki/Networks/PlainXML#Node_types)");
90 }
91 
92 
93 bool
95  bool ok = NGFrame::checkOptions();
96  ok &= NBFrame::checkOptions();
97  ok &= NWFrame::checkOptions();
99  return ok;
100 }
101 
102 
103 NGNet*
106 
107  // spider-net
108  if (oc.getBool("spider")) {
109  // check values
110  bool hadError = false;
111  if (oc.getInt("spider.arm-number") < 3) {
112  WRITE_ERROR("Spider networks need at least 3 arms.");
113  hadError = true;
114  }
115  if (oc.getInt("spider.circle-number") < 1) {
116  WRITE_ERROR("Spider networks need at least one circle.");
117  hadError = true;
118  }
119  if (oc.getFloat("spider.space-radius") < 10) {
120  WRITE_ERROR("The radius of spider networks must be at least 10m.");
121  hadError = true;
122  }
123  if (hadError) {
124  throw ProcessError();
125  }
126  // build if everything's ok
127  NGNet* net = new NGNet(nb);
128  net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
129  oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"));
130  return net;
131  }
132  // grid-net
133  if (oc.getBool("grid")) {
134  // get options
135  int xNo = oc.getInt("grid.x-number");
136  int yNo = oc.getInt("grid.y-number");
137  double xLength = oc.getFloat("grid.x-length");
138  double yLength = oc.getFloat("grid.y-length");
139  double attachLength = oc.getFloat("grid.attach-length");
140  if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
141  xNo = oc.getInt("grid.number");
142  }
143  if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
144  yNo = oc.getInt("grid.number");
145  }
146  if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
147  xLength = oc.getFloat("grid.length");
148  }
149  if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
150  yLength = oc.getFloat("grid.length");
151  }
152  // check values
153  bool hadError = false;
154  if (attachLength == 0 && (xNo < 2 || yNo < 2)) {
155  WRITE_ERROR("The number of nodes must be at least 2 in both directions.");
156  hadError = true;
157  }
158  if (xLength < 10. || yLength < 10.) {
159  WRITE_ERROR("The distance between nodes must be at least 10m in both directions.");
160  hadError = true;
161  }
162  if (attachLength != 0.0 && attachLength < 10.) {
163  WRITE_ERROR("The length of attached streets must be at least 10m.");
164  hadError = true;
165  }
166  if (hadError) {
167  throw ProcessError();
168  }
169  // build if everything's ok
170  NGNet* net = new NGNet(nb);
171  net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength);
172  return net;
173  }
174  // random net
175  RandomDistributor<int> neighborDist;
176  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
177  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
178  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
179  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
180  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
181  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
182  NGNet* net = new NGNet(nb);
183  NGRandomNetBuilder randomNet(*net,
184  oc.getFloat("rand.min-angle"),
185  oc.getFloat("rand.min-distance"),
186  oc.getFloat("rand.max-distance"),
187  oc.getFloat("rand.connectivity"),
188  oc.getInt("rand.num-tries"),
189  neighborDist);
190  randomNet.createNet(oc.getInt("rand.iterations"), oc.getBool("rand.grid"));
191  return net;
192 }
193 
194 
195 
196 int
197 main(int argc, char** argv) {
199  // give some application descriptions
200  oc.setApplicationDescription("Synthetic network generator for the microscopic, multi-modal traffic simulation SUMO.");
201  oc.setApplicationName("netgenerate", "Eclipse SUMO netgenerate Version " VERSION_STRING);
202  int ret = 0;
203  try {
204  // initialise the application system (messaging, xml, options)
205  XMLSubSys::init();
206  fillOptions();
207  OptionsIO::setArgs(argc, argv);
209  if (oc.processMetaOptions(argc < 2)) {
211  return 0;
212  }
213  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
215  if (!checkOptions()) {
216  throw ProcessError();
217  }
219  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
220  Boundary(), Boundary());
222  NBNetBuilder nb;
223  nb.applyOptions(oc);
224  // build the netgen-network description
225  NGNet* net = buildNetwork(nb);
226  // ... and we have to do this...
227  oc.resetWritable();
228  // transfer to the netbuilding structures
229  net->toNB();
230  delete net;
231  // report generated structures
232  WRITE_MESSAGE(" Generation done;");
233  WRITE_MESSAGE(" " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
234  WRITE_MESSAGE(" " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
235  nb.compute(oc);
236  NWFrame::writeNetwork(oc, nb);
237  } catch (const ProcessError& e) {
238  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
239  WRITE_ERROR(e.what());
240  }
241  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
242  ret = 1;
243 #ifndef _DEBUG
244  } catch (const std::exception& e) {
245  if (std::string(e.what()) != std::string("")) {
246  WRITE_ERROR(e.what());
247  }
248  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
249  ret = 1;
250  } catch (...) {
251  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
252  ret = 1;
253 #endif
254  }
256  if (ret == 0) {
257  std::cout << "Success." << std::endl;
258  }
259  return ret;
260 }
261 
262 
263 
264 /****************************************************************************/
265 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:48
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:43
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NWFrame.cpp:125
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NBFrame.cpp:536
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void resetWritable()
Resets all options to be writeable.
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:65
bool checkOptions()
Definition: netgen_main.cpp:94
int main(int argc, char **argv)
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:231
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
NGNet * buildNetwork(NBNetBuilder &nb)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
void fillOptions()
Definition: netgen_main.cpp:59
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:40
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:96
int size() const
Returns the number of nodes stored in this container.
Definition: NBNodeCont.h:257
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
void setAdditionalHelpMessage(const std::string &add)
Sets an additional message to be printed at the begin of the help screen.
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:150
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NGFrame.cpp:206
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static bool checkOptions()
checks shared options and sets StdDefs
The class storing the generated network.
Definition: NGNet.h:50
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
#define VERSION_STRING
Definition: config.h:207
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network stored in the given net builder.
Definition: NWFrame.cpp:174
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:247
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:155
Instance responsible for building networks.
Definition: NBNetBuilder.h:109
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:160
int size() const
Returns the number of edges.
Definition: NBEdgeCont.h:306
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:113
A storage for options typed value containers)
Definition: OptionsCont.h:92
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:72
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength)
Creates a grid network.
Definition: NGNet.cpp:95
static void fillOptions()
Inserts options used by the network generator.
Definition: NGFrame.cpp:40
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:49
A class that builds random network using an algorithm by Markus Hartinger.
void compute(OptionsCont &oc, const std::set< std::string > &explicitTurnarounds=std::set< std::string >(), bool mayAddOrRemove=true)
Performs the network building steps.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void fillOptions(bool forNetgen)
Inserts options used by the network writer.
Definition: NWFrame.cpp:52
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:242
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:239
void createNet(int numNodes, bool gridMode)
Builds a NGNet using the set values.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.