LCOV - code coverage report
Current view: top level - src/duarouter - duarouter_main.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 90.2 % 122 110
Test Date: 2024-11-20 15:55:46 Functions: 100.0 % 3 3

            Line data    Source code
       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              : /****************************************************************************/
      14              : /// @file    duarouter_main.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @date    Thu, 06 Jun 2002
      19              : ///
      20              : // Main for DUAROUTER
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #ifdef HAVE_VERSION_H
      25              : #include <version.h>
      26              : #endif
      27              : 
      28              : #include <iostream>
      29              : #include <string>
      30              : #include <limits.h>
      31              : #include <ctime>
      32              : #include <memory>
      33              : #include <xercesc/sax/SAXException.hpp>
      34              : #include <xercesc/sax/SAXParseException.hpp>
      35              : #include <utils/common/StringUtils.h>
      36              : #include <utils/common/MsgHandler.h>
      37              : #include <utils/common/UtilExceptions.h>
      38              : #include <utils/common/SystemFrame.h>
      39              : #include <utils/common/RandHelper.h>
      40              : #include <utils/common/ToString.h>
      41              : #ifdef HAVE_FOX
      42              : #include <utils/foxtools/MsgHandlerSynchronized.h>
      43              : #endif
      44              : #include <utils/iodevices/OutputDevice.h>
      45              : #include <utils/options/Option.h>
      46              : #include <utils/options/OptionsCont.h>
      47              : #include <utils/options/OptionsIO.h>
      48              : #include <utils/router/DijkstraRouter.h>
      49              : #include <utils/router/AStarRouter.h>
      50              : #include <utils/router/CHRouter.h>
      51              : #include <utils/router/CHRouterWrapper.h>
      52              : #include <utils/vehicle/SUMOVehicleParserHelper.h>
      53              : #include <utils/xml/XMLSubSys.h>
      54              : #include <router/ROFrame.h>
      55              : #include <router/ROLoader.h>
      56              : #include <router/RONet.h>
      57              : #include <router/ROEdge.h>
      58              : #include "RODUAEdgeBuilder.h"
      59              : #include "RODUAFrame.h"
      60              : 
      61              : 
      62              : // ===========================================================================
      63              : // functions
      64              : // ===========================================================================
      65              : /* -------------------------------------------------------------------------
      66              :  * data processing methods
      67              :  * ----------------------------------------------------------------------- */
      68              : /**
      69              :  * loads the net
      70              :  * The net is in this meaning made up by the net itself and the dynamic
      71              :  * weights which may be supplied in a separate file
      72              :  */
      73              : void
      74         9073 : initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
      75              :     // load the net
      76         9073 :     RODUAEdgeBuilder builder;
      77         9073 :     ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
      78         9073 :     loader.loadNet(net, builder);
      79              :     // load the weights when wished/available
      80        17654 :     if (oc.isSet("weight-files")) {
      81         1179 :         loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
      82              :     }
      83        17654 :     if (oc.isSet("lane-weight-files")) {
      84            6 :         loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
      85              :     }
      86         9073 : }
      87              : 
      88              : 
      89              : /**
      90              :  * Computes the routes saving them
      91              :  */
      92              : void
      93         8827 : computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
      94              :     // initialise the loader
      95         8827 :     loader.openRoutes(net);
      96              :     // build the router
      97         5982 :     auto ttFunction = gWeightsRandomFactor > 1 ? &ROEdge::getTravelTimeStaticRandomized : &ROEdge::getTravelTimeStatic;
      98              :     SUMOAbstractRouter<ROEdge, ROVehicle>* router;
      99         5982 :     const std::string measure = oc.getString("weight-attribute");
     100         6026 :     const std::string routingAlgorithm = oc.getString("routing-algorithm");
     101         5982 :     const double priorityFactor = oc.getFloat("weights.priority-factor");
     102         5982 :     const SUMOTime begin = string2time(oc.getString("begin"));
     103         6110 :     const SUMOTime end = oc.isDefault("end") ? SUMOTime_MAX : string2time(oc.getString("end"));
     104              :     DijkstraRouter<ROEdge, ROVehicle>::Operation op = &ROEdge::getTravelTimeStatic;
     105              : 
     106        11964 :     if (oc.isSet("restriction-params") &&
     107           12 :             (routingAlgorithm == "CH" || routingAlgorithm == "CHWrapper")) {
     108           18 :         throw ProcessError(TLF("Routing algorithm '%' does not support restriction-params", routingAlgorithm));
     109              :     }
     110              : 
     111         5976 :     if (measure == "traveltime" && priorityFactor == 0) {
     112         5962 :         if (routingAlgorithm == "dijkstra") {
     113         9282 :             router = new DijkstraRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, nullptr, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
     114         1321 :         } else if (routingAlgorithm == "astar") {
     115              :             typedef AStarRouter<ROEdge, ROVehicle> AStar;
     116          452 :             std::shared_ptr<const AStar::LookupTable> lookup;
     117          904 :             if (oc.isSet("astar.all-distances")) {
     118            0 :                 lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
     119          906 :             } else if (oc.isSet("astar.landmark-distances")) {
     120              :                 /* CHRouterWrapper<ROEdge, ROVehicle> chrouter(
     121              :                     ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic,
     122              :                     begin, end, SUMOTime_MAX, 1); */
     123           10 :                 DijkstraRouter<ROEdge, ROVehicle> forward(ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic);
     124              :                 std::vector<ReversedEdge<ROEdge, ROVehicle>*> reversed;
     125        20498 :                 for (ROEdge* edge : ROEdge::getAllEdges()) {
     126        20488 :                     reversed.push_back(edge->getReversedRoutingEdge());
     127              :                 }
     128        20498 :                 for (ReversedEdge<ROEdge, ROVehicle>* redge : reversed) {
     129        20488 :                     redge->init();
     130              :                 }
     131           10 :                 DijkstraRouter<ReversedEdge<ROEdge, ROVehicle>, ROVehicle> backward(reversed, true, &ReversedEdge<ROEdge, ROVehicle>::getTravelTimeStatic);
     132           12 :                 ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
     133           20 :                 lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &forward, &backward, &defaultVehicle,
     134           28 :                          oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"));
     135           20 :             }
     136          908 :             router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, lookup, net.hasPermissions(), oc.isSet("restriction-params"));
     137          869 :         } else if (routingAlgorithm == "CH" && !net.hasPermissions()) {
     138           65 :             const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
     139           83 :                                            string2time(oc.getString("weight-period")) :
     140              :                                            SUMOTime_MAX);
     141              :             router = new CHRouter<ROEdge, ROVehicle>(
     142          130 :                 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, SVC_IGNORING, weightPeriod, net.hasPermissions(), oc.isSet("restriction-params"));
     143          804 :         } else if (routingAlgorithm == "CHWrapper" || routingAlgorithm == "CH") {
     144              :             // use CHWrapper instead of CH if the net has permissions
     145          804 :             const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
     146          826 :                                            string2time(oc.getString("weight-period")) :
     147              :                                            SUMOTime_MAX);
     148              :             router = new CHRouterWrapper<ROEdge, ROVehicle>(
     149         1608 :                 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction,
     150         2412 :                 begin, end, weightPeriod, net.hasPermissions(), oc.getInt("routing-threads"));
     151              :         } else {
     152            0 :             throw ProcessError(TLF("Unknown routing Algorithm '%'!", routingAlgorithm));
     153              :         }
     154              :     } else {
     155           14 :         if (measure == "traveltime") {
     156            3 :             if (ROEdge::initPriorityFactor(priorityFactor)) {
     157              :                 op = &ROEdge::getTravelTimeStaticPriorityFactor;
     158              :             }
     159           11 :         } else if (measure == "CO") {
     160              :             op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
     161            8 :         } else if (measure == "CO2") {
     162              :             op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
     163            7 :         } else if (measure == "PMx") {
     164              :             op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
     165            6 :         } else if (measure == "HC") {
     166              :             op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
     167            4 :         } else if (measure == "NOx") {
     168              :             op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
     169            3 :         } else if (measure == "fuel") {
     170              :             op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
     171            2 :         } else if (measure == "electricity") {
     172              :             op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
     173            2 :         } else if (measure == "noise") {
     174              :             op = &ROEdge::getNoiseEffort;
     175              :         } else {
     176              :             op = &ROEdge::getStoredEffort;
     177              :         }
     178           14 :         if (measure != "traveltime" && !net.hasLoadedEffort()) {
     179            6 :             WRITE_WARNINGF(TL("No weight data was loaded for attribute '%'."), measure);
     180              :         }
     181              :         router = new DijkstraRouter<ROEdge, ROVehicle>(
     182           28 :             ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
     183              :     }
     184         5974 :     const int carWalk = SUMOVehicleParserHelper::parseCarWalkTransfer(oc);
     185        11992 :     double taxiWait = STEPS2TIME(string2time(OptionsCont::getOptions().getString("persontrip.taxi.waiting-time")));
     186              : 
     187              :     RailwayRouter<ROEdge, ROVehicle>* railRouter = nullptr;
     188         5974 :     if (net.hasBidiEdges()) {
     189           80 :         railRouter = new RailwayRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), true, op, ttFunction, false, net.hasPermissions(),
     190           80 :                 oc.isSet("restriction-params"),
     191          120 :                 oc.getFloat("railway.max-train-length"));
     192              :     }
     193         5974 :     RORouterProvider provider(router, new PedestrianRouter<ROEdge, ROLane, RONode, ROVehicle>(),
     194         5974 :                               new ROIntermodalRouter(RONet::adaptIntermodalRouter, carWalk, taxiWait, routingAlgorithm),
     195              :                               railRouter);
     196              :     // process route definitions
     197              :     try {
     198         5974 :         net.openOutput(oc);
     199        11908 :         loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
     200         5938 :         net.writeIntermodal(oc, provider.getIntermodalRouter());
     201              :         // end the processing
     202         5938 :         net.cleanup();
     203           36 :     } catch (ProcessError&) {
     204           36 :         net.cleanup();
     205           36 :         throw;
     206           36 :     }
     207        11912 : }
     208              : 
     209              : 
     210              : /* -------------------------------------------------------------------------
     211              :  * main
     212              :  * ----------------------------------------------------------------------- */
     213              : int
     214         9358 : main(int argc, char** argv) {
     215         9358 :     OptionsCont& oc = OptionsCont::getOptions();
     216         9358 :     oc.setApplicationDescription(TL("Shortest path router and DUE computer for the microscopic, multi-modal traffic simulation SUMO."));
     217        18716 :     oc.setApplicationName("duarouter", "Eclipse SUMO duarouter Version " VERSION_STRING);
     218              :     int ret = 0;
     219              :     RONet* net = nullptr;
     220              :     try {
     221         9358 :         XMLSubSys::init();
     222         9358 :         RODUAFrame::fillOptions();
     223         9358 :         OptionsIO::setArgs(argc, argv);
     224         9358 :         OptionsIO::getOptions();
     225         9354 :         if (oc.processMetaOptions(argc < 2)) {
     226          240 :             SystemFrame::close();
     227          240 :             return 0;
     228              :         }
     229         9114 :         SystemFrame::checkOptions(oc);
     230        27342 :         XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
     231              : #ifdef HAVE_FOX
     232        18228 :         if (oc.getInt("routing-threads") > 1) {
     233              :             // make the output aware of threading
     234              :             MsgHandler::setFactory(&MsgHandlerSynchronized::create);
     235              :         }
     236              : #endif
     237         9114 :         MsgHandler::initOutputOptions();
     238         9114 :         if (!RODUAFrame::checkOptions()) {
     239           41 :             throw ProcessError();
     240              :         }
     241         9073 :         RandHelper::initRandGlobal();
     242              :         // load data
     243        12424 :         ROLoader loader(oc, false, !oc.getBool("no-step-log"));
     244         9073 :         net = new RONet();
     245         9073 :         initNet(*net, loader, oc);
     246              :         // build routes
     247              :         try {
     248         8827 :             computeRoutes(*net, loader, oc);
     249         2889 :         } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
     250            0 :             WRITE_ERROR(toString(e.getLineNumber()));
     251              :             ret = 1;
     252            0 :         } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
     253            0 :             WRITE_ERROR(StringUtils::transcode(e.getMessage()));
     254              :             ret = 1;
     255            0 :         }
     256         5938 :         if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
     257          171 :             throw ProcessError();
     258              :         }
     259        12424 :     } catch (const ProcessError& e) {
     260         7325 :         if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
     261         3914 :             WRITE_ERROR(e.what());
     262              :         }
     263         3351 :         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
     264              :         ret = 1;
     265              : #ifndef _DEBUG
     266         3351 :     } catch (const std::exception& e) {
     267            0 :         if (std::string(e.what()) != std::string("")) {
     268            0 :             WRITE_ERROR(e.what());
     269              :         }
     270            0 :         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
     271              :         ret = 1;
     272            0 :     } catch (...) {
     273            0 :         MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
     274              :         ret = 1;
     275              : #endif
     276            0 :     }
     277         9118 :     delete net;
     278         9118 :     SystemFrame::close();
     279         9118 :     if (ret == 0) {
     280              :         std::cout << "Success." << std::endl;
     281              :     }
     282              :     return ret;
     283              : }
     284              : 
     285              : 
     286              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1