SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // Main for DUAROUTER 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 00023 00024 // =========================================================================== 00025 // included modules 00026 // =========================================================================== 00027 #ifdef _MSC_VER 00028 #include <windows_config.h> 00029 #else 00030 #include <config.h> 00031 #endif 00032 00033 #ifdef HAVE_VERSION_H 00034 #include <version.h> 00035 #endif 00036 00037 #include <xercesc/sax/SAXException.hpp> 00038 #include <xercesc/sax/SAXParseException.hpp> 00039 #include <utils/common/TplConvert.h> 00040 #include <iostream> 00041 #include <string> 00042 #include <limits.h> 00043 #include <ctime> 00044 #include <router/ROLoader.h> 00045 #include <router/RONet.h> 00046 #include <router/ROEdge.h> 00047 #include <router/ROCostCalculator.h> 00048 #include <utils/common/DijkstraRouterTT.h> 00049 #include <utils/common/DijkstraRouterEffort.h> 00050 #include <utils/common/AStarRouter.h> 00051 #include "RODUAEdgeBuilder.h" 00052 #include <router/ROFrame.h> 00053 #include <utils/common/MsgHandler.h> 00054 #include <utils/options/Option.h> 00055 #include <utils/options/OptionsCont.h> 00056 #include <utils/options/OptionsIO.h> 00057 #include <utils/common/UtilExceptions.h> 00058 #include <utils/common/SystemFrame.h> 00059 #include <utils/common/RandHelper.h> 00060 #include <utils/common/ToString.h> 00061 #include <utils/xml/XMLSubSys.h> 00062 #include "RODUAFrame.h" 00063 #include <utils/iodevices/OutputDevice.h> 00064 00065 #ifdef HAVE_MESOSIM // catchall for internal stuff 00066 #include <internal/BulkStarRouter.h> 00067 #include <internal/CHRouter.h> 00068 #include <internal/CHRouterWrapper.h> 00069 #endif // have HAVE_MESOSIM 00070 00071 #ifdef CHECK_MEMORY_LEAKS 00072 #include <foreign/nvwa/debug_new.h> 00073 #endif // CHECK_MEMORY_LEAKS 00074 00075 00076 // =========================================================================== 00077 // functions 00078 // =========================================================================== 00079 /* ------------------------------------------------------------------------- 00080 * data processing methods 00081 * ----------------------------------------------------------------------- */ 00087 void 00088 initNet(RONet& net, ROLoader& loader, OptionsCont& oc) { 00089 // load the net 00090 RODUAEdgeBuilder builder(oc.getBool("weights.expand"), oc.getBool("weights.interpolate")); 00091 loader.loadNet(net, builder); 00092 // load the weights when wished/available 00093 if (oc.isSet("weight-files")) { 00094 loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false); 00095 } 00096 if (oc.isSet("lane-weight-files")) { 00097 loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true); 00098 } 00099 } 00100 00101 00102 00106 void 00107 computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) { 00108 // initialise the loader 00109 loader.openRoutes(net); 00110 // prepare the output 00111 net.openOutput(oc.getString("output-file"), true, oc.getString("vtype-output")); 00112 // build the router 00113 SUMOAbstractRouter<ROEdge, ROVehicle> *router; 00114 const std::string measure = oc.getString("weight-attribute"); 00115 const std::string routingAlgorithm = oc.getString("routing-algorithm"); 00116 if (measure == "traveltime") { 00117 if (routingAlgorithm == "dijkstra") { 00118 if (net.hasRestrictions()) { 00119 router = new DijkstraRouterTT_Direct<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >( 00120 net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime); 00121 } else { 00122 router = new DijkstraRouterTT_Direct<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >( 00123 net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime); 00124 } 00125 } else if (routingAlgorithm == "astar") { 00126 if (net.hasRestrictions()) { 00127 router = new AStarRouterTT_Direct<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >( 00128 net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime); 00129 } else { 00130 router = new AStarRouterTT_Direct<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >( 00131 net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime); 00132 } 00133 #ifdef HAVE_MESOSIM // catchall for internal stuff 00134 } else if (routingAlgorithm == "bulkstar") { 00135 if (net.hasRestrictions()) { 00136 router = new BulkStarRouterTT<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >( 00137 net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, &ROEdge::getMinimumTravelTime); 00138 } else { 00139 router = new BulkStarRouterTT<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >( 00140 net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, &ROEdge::getMinimumTravelTime); 00141 } 00142 00143 } else if (routingAlgorithm == "CH") { 00144 // defaultVehicle is only in constructor and may be safely deleted 00145 // it is mainly needed for its maximum speed. @todo XXX make this configurable 00146 ROVehicle defaultVehicle(SUMOVehicleParameter(), 0, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID)); 00147 const SUMOTime begin = string2time(oc.getString("begin")); 00148 const SUMOTime weightPeriod = (oc.isSet("weight-files") ? 00149 string2time(oc.getString("weight-period")) : 00150 std::numeric_limits<int>::max()); 00151 if (net.hasRestrictions()) { 00152 router = new CHRouter<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >( 00153 net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, &defaultVehicle, begin, weightPeriod, true); 00154 } else { 00155 router = new CHRouter<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >( 00156 net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, &defaultVehicle, begin, weightPeriod, false); 00157 } 00158 00159 } else if (routingAlgorithm == "CHWrapper") { 00160 const SUMOTime begin = string2time(oc.getString("begin")); 00161 const SUMOTime weightPeriod = (oc.isSet("weight-files") ? 00162 string2time(oc.getString("weight-period")) : 00163 std::numeric_limits<int>::max()); 00164 00165 if (!net.hasRestrictions()) { 00166 WRITE_WARNING("CHWrapper is only needed for a restricted network"); 00167 } 00168 router = new CHRouterWrapper<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >( 00169 oc.getBool("ignore-errors"), &ROEdge::getTravelTime, begin, weightPeriod); 00170 00171 #endif // have HAVE_MESOSIM 00172 } else { 00173 throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!"); 00174 } 00175 00176 } else { 00177 DijkstraRouterEffort_Direct<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >::Operation op; 00178 if (measure == "CO") { 00179 op = &ROEdge::getCOEffort; 00180 } else if (measure == "CO2") { 00181 op = &ROEdge::getCO2Effort; 00182 } else if (measure == "PMx") { 00183 op = &ROEdge::getPMxEffort; 00184 } else if (measure == "HC") { 00185 op = &ROEdge::getHCEffort; 00186 } else if (measure == "NOx") { 00187 op = &ROEdge::getNOxEffort; 00188 } else if (measure == "fuel") { 00189 op = &ROEdge::getFuelEffort; 00190 } else if (measure == "noise") { 00191 op = &ROEdge::getNoiseEffort; 00192 } else { 00193 net.closeOutput(); 00194 throw ProcessError("Unknown measure (weight attribute '" + measure + "')!"); 00195 } 00196 if (net.hasRestrictions()) { 00197 router = new DijkstraRouterEffort_Direct<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >( 00198 net.getEdgeNo(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTime); 00199 } else { 00200 router = new DijkstraRouterEffort_Direct<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >( 00201 net.getEdgeNo(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTime); 00202 } 00203 } 00204 // process route definitions 00205 try { 00206 if (routingAlgorithm == "bulkstar") { 00207 // need to load all routes for spatial aggregation 00208 loader.processAllRoutesWithBulkRouter(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, *router); 00209 } else if (!oc.getBool("unsorted-input")) { 00210 // the routes are sorted - process stepwise 00211 loader.processRoutesStepWise(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, *router); 00212 } else { 00213 // the routes are not sorted: load all and process 00214 loader.processAllRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, *router); 00215 } 00216 // end the processing 00217 net.closeOutput(); 00218 delete router; 00219 ROCostCalculator::cleanup(); 00220 } catch (ProcessError&) { 00221 net.closeOutput(); 00222 delete router; 00223 ROCostCalculator::cleanup(); 00224 throw; 00225 } 00226 } 00227 00228 00229 /* ------------------------------------------------------------------------- 00230 * main 00231 * ----------------------------------------------------------------------- */ 00232 int 00233 main(int argc, char** argv) { 00234 OptionsCont& oc = OptionsCont::getOptions(); 00235 // give some application descriptions 00236 oc.setApplicationDescription("Shortest path router and DUE computer for the microscopic road traffic simulation SUMO."); 00237 oc.setApplicationName("duarouter", "SUMO duarouter Version " + (std::string)VERSION_STRING); 00238 int ret = 0; 00239 RONet* net = 0; 00240 try { 00241 XMLSubSys::init(false); 00242 RODUAFrame::fillOptions(); 00243 OptionsIO::getOptions(true, argc, argv); 00244 if (oc.processMetaOptions(argc < 2)) { 00245 OutputDevice::closeAll(); 00246 SystemFrame::close(); 00247 return 0; 00248 } 00249 MsgHandler::initOutputOptions(); 00250 if (!RODUAFrame::checkOptions()) { 00251 throw ProcessError(); 00252 } 00253 RandHelper::initRandGlobal(); 00254 // load data 00255 ROLoader loader(oc, false); 00256 net = new RONet(); 00257 initNet(*net, loader, oc); 00258 // build routes 00259 try { 00260 computeRoutes(*net, loader, oc); 00261 } catch (SAXParseException& e) { 00262 WRITE_ERROR(toString(e.getLineNumber())); 00263 ret = 1; 00264 } catch (SAXException& e) { 00265 WRITE_ERROR(TplConvert<XMLCh>::_2str(e.getMessage())); 00266 ret = 1; 00267 } 00268 if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) { 00269 throw ProcessError(); 00270 } 00271 } catch (ProcessError& e) { 00272 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) { 00273 WRITE_ERROR(e.what()); 00274 } 00275 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false); 00276 ret = 1; 00277 #ifndef _DEBUG 00278 } catch (...) { 00279 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false); 00280 ret = 1; 00281 #endif 00282 } 00283 delete net; 00284 OutputDevice::closeAll(); 00285 SystemFrame::close(); 00286 if (ret == 0) { 00287 std::cout << "Success." << std::endl; 00288 } 00289 return ret; 00290 } 00291 00292 00293 00294 /****************************************************************************/ 00295