SUMO - Simulation of Urban MObility

jtrrouter_main.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Main for JTRROUTER
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #ifdef HAVE_VERSION_H
00031 #include <version.h>
00032 #endif
00033 
00034 #include <xercesc/sax/SAXException.hpp>
00035 #include <xercesc/sax/SAXParseException.hpp>
00036 #include <utils/common/TplConvert.h>
00037 #include <iostream>
00038 #include <string>
00039 #include <limits.h>
00040 #include <ctime>
00041 #include <set>
00042 #include <router/ROFrame.h>
00043 #include <router/ROLoader.h>
00044 #include <router/RONet.h>
00045 #include <utils/common/MsgHandler.h>
00046 #include <utils/options/Option.h>
00047 #include <utils/options/OptionsCont.h>
00048 #include <utils/options/OptionsIO.h>
00049 #include <utils/common/UtilExceptions.h>
00050 #include <utils/common/SystemFrame.h>
00051 #include <utils/common/ToString.h>
00052 #include <utils/common/RandHelper.h>
00053 #include <utils/common/StringTokenizer.h>
00054 #include <utils/xml/XMLSubSys.h>
00055 #include "ROJTREdgeBuilder.h"
00056 #include "ROJTRRouter.h"
00057 #include "ROJTREdge.h"
00058 #include "ROJTRTurnDefLoader.h"
00059 #include "ROJTRFrame.h"
00060 #include <utils/iodevices/OutputDevice.h>
00061 
00062 #ifdef CHECK_MEMORY_LEAKS
00063 #include <foreign/nvwa/debug_new.h>
00064 #endif // CHECK_MEMORY_LEAKS
00065 
00066 
00067 // ===========================================================================
00068 // functions
00069 // ===========================================================================
00070 /* -------------------------------------------------------------------------
00071  * data processing methods
00072  * ----------------------------------------------------------------------- */
00078 void
00079 initNet(RONet& net, ROLoader& loader, OptionsCont& oc,
00080         const std::vector<SUMOReal> &turnDefs) {
00081     // load the net
00082     ROJTREdgeBuilder builder;
00083     loader.loadNet(net, builder);
00084     // set the turn defaults
00085     const std::map<std::string, ROEdge*> &edges = net.getEdgeMap();
00086     for (std::map<std::string, ROEdge*>::const_iterator i=edges.begin(); i!=edges.end(); ++i) {
00087         static_cast<ROJTREdge*>((*i).second)->setTurnDefaults(turnDefs);
00088     }
00089 }
00090 
00091 std::vector<SUMOReal>
00092 getTurningDefaults(OptionsCont& oc) {
00093     std::vector<SUMOReal> ret;
00094     std::vector<std::string> defs = oc.getStringVector("turn-defaults");
00095     if (defs.size()<2) {
00096         throw ProcessError("The defaults for turnings must be a tuple of at least two numbers divided by ','.");
00097     }
00098     for (std::vector<std::string>::const_iterator i=defs.begin(); i!=defs.end(); ++i) {
00099         try {
00100             SUMOReal val = TplConvert<char>::_2SUMOReal((*i).c_str());
00101             ret.push_back(val);
00102         } catch (NumberFormatException&) {
00103             throw ProcessError("A turn default is not numeric.");
00104         }
00105     }
00106     return ret;
00107 }
00108 
00109 
00110 void
00111 loadJTRDefinitions(RONet& net, OptionsCont& oc) {
00112     // load the turning definitions (and possible sink definition)
00113     if (oc.isSet("turn-ratio-files")) {
00114         ROJTRTurnDefLoader loader(net);
00115         std::vector<std::string> ratio_files = oc.getStringVector("turn-ratio-files");
00116         for (std::vector<std::string>::const_iterator i=ratio_files.begin(); i!=ratio_files.end(); ++i) {
00117             if (!XMLSubSys::runParser(loader, *i)) {
00118                 throw ProcessError();
00119             }
00120         }
00121     }
00122     if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
00123         MsgHandler::getErrorInstance()->clear();
00124     }
00125     // parse sink edges specified at the input/within the configuration
00126     if (oc.isSet("sink-edges")) {
00127         std::vector<std::string> edges = oc.getStringVector("sink-edges");
00128         for (std::vector<std::string>::const_iterator i=edges.begin(); i!=edges.end(); ++i) {
00129             ROJTREdge* edge = static_cast<ROJTREdge*>(net.getEdge(*i));
00130             if (edge==0) {
00131                 throw ProcessError("The edge '" + *i + "' declared as a sink is not known.");
00132             }
00133             edge->setType(ROEdge::ET_SINK);
00134         }
00135     }
00136 }
00137 
00138 
00142 void
00143 computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
00144     // initialise the loader
00145     loader.openRoutes(net);
00146     // prepare the output
00147     net.openOutput(oc.getString("output-file"), false);
00148     // build the router
00149     ROJTRRouter router(net, oc.getBool("ignore-errors"), oc.getBool("accept-all-destinations"),
00150                        (int)(((SUMOReal) net.getEdgeNo()) * OptionsCont::getOptions().getFloat("max-edges-factor")),
00151                        oc.getBool("ignore-vclasses"), oc.getBool("allow-loops"));
00152     if (!oc.getBool("unsorted-input")) {
00153         // the routes are sorted - process stepwise
00154         loader.processRoutesStepWise(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, router);
00155     } else {
00156         // the routes are not sorted: load all and process
00157         loader.processAllRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, router);
00158     }
00159     // end the processing
00160     net.closeOutput();
00161 }
00162 
00163 
00164 /* -------------------------------------------------------------------------
00165  * main
00166  * ----------------------------------------------------------------------- */
00167 int
00168 main(int argc, char** argv) {
00169     OptionsCont& oc = OptionsCont::getOptions();
00170     // give some application descriptions
00171     oc.setApplicationDescription("Router for the microscopic road traffic simulation SUMO based on junction turning ratios.");
00172     oc.setApplicationName("jtrrouter", "SUMO jtrrouter Version " + (std::string)VERSION_STRING);
00173     int ret = 0;
00174     RONet* net = 0;
00175     try {
00176         // initialise the application system (messaging, xml, options)
00177         XMLSubSys::init(false);
00178         ROJTRFrame::fillOptions();
00179         OptionsIO::getOptions(true, argc, argv);
00180         if (oc.processMetaOptions(argc < 2)) {
00181             SystemFrame::close();
00182             return 0;
00183         }
00184         MsgHandler::initOutputOptions();
00185         if (!ROJTRFrame::checkOptions()) {
00186             throw ProcessError();
00187         }
00188         RandHelper::initRandGlobal();
00189         std::vector<SUMOReal> defs = getTurningDefaults(oc);
00190         // load data
00191         ROLoader loader(oc, true);
00192         net = new RONet();
00193         initNet(*net, loader, oc, defs);
00194         try {
00195             // parse and set the turn defaults first
00196             loadJTRDefinitions(*net, oc);
00197             // build routes
00198             computeRoutes(*net, loader, oc);
00199         } catch (SAXParseException& e) {
00200             WRITE_ERROR(toString(e.getLineNumber()));
00201             ret = 1;
00202         } catch (SAXException& e) {
00203             WRITE_ERROR(TplConvert<XMLCh>::_2str(e.getMessage()));
00204             ret = 1;
00205         }
00206         if (MsgHandler::getErrorInstance()->wasInformed()) {
00207             throw ProcessError();
00208         }
00209     } catch (ProcessError& e) {
00210         if (std::string(e.what())!=std::string("Process Error") && std::string(e.what())!=std::string("")) {
00211             WRITE_ERROR(e.what());
00212         }
00213         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00214         ret = 1;
00215 #ifndef _DEBUG
00216     } catch (...) {
00217         MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
00218         ret = 1;
00219 #endif
00220     }
00221     delete net;
00222     OutputDevice::closeAll();
00223     SystemFrame::close();
00224     if (ret==0) {
00225         std::cout << "Success." << std::endl;
00226     }
00227     return ret;
00228 }
00229 
00230 
00231 
00232 /****************************************************************************/
00233 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines