SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // A complete route definition (with all passed edges being known) 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 #include <string> 00034 #include <deque> 00035 #include <iterator> 00036 #include "ROEdge.h" 00037 #include "RORouteDef.h" 00038 #include "RORoute.h" 00039 #include <utils/common/SUMOAbstractRouter.h> 00040 #include <utils/common/MsgHandler.h> 00041 #include <utils/common/UtilExceptions.h> 00042 #include "RORouteDef_Complete.h" 00043 #include "ROHelper.h" 00044 #include <utils/iodevices/OutputDevice.h> 00045 #include <utils/options/OptionsCont.h> 00046 00047 #ifdef CHECK_MEMORY_LEAKS 00048 #include <foreign/nvwa/debug_new.h> 00049 #endif // CHECK_MEMORY_LEAKS 00050 00051 00052 // =========================================================================== 00053 // method definitions 00054 // =========================================================================== 00055 RORouteDef_Complete::RORouteDef_Complete(const std::string& id, 00056 const RGBColor* const color, 00057 const std::vector<const ROEdge*> &edges, 00058 bool tryRepair) 00059 : RORouteDef(id, color), myEdges(edges), myTryRepair(tryRepair) { 00060 } 00061 00062 00063 RORouteDef_Complete::~RORouteDef_Complete() {} 00064 00065 00066 void 00067 RORouteDef_Complete::preComputeCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle> &router, 00068 SUMOTime begin, const ROVehicle& veh) const { 00069 if (myTryRepair) { 00070 const std::vector<const ROEdge*> &oldEdges = myEdges; 00071 if (oldEdges.size() == 0) { 00072 MsgHandler* m = OptionsCont::getOptions().getBool("ignore-errors") ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance(); 00073 m->inform("Could not repair empty route of vehicle '" + veh.getID() + "'."); 00074 myPrecomputed = new RORoute(myID, 0, 1, std::vector<const ROEdge*>(), copyColorIfGiven()); 00075 return; 00076 } 00077 std::vector<const ROEdge*> newEdges; 00078 newEdges.push_back(*(oldEdges.begin())); 00079 for (std::vector<const ROEdge*>::const_iterator i = oldEdges.begin() + 1; i != oldEdges.end(); ++i) { 00080 if ((*(i - 1))->isConnectedTo(*i)) { 00081 newEdges.push_back(*i); 00082 } else { 00083 std::vector<const ROEdge*> edges; 00084 router.compute(*(i - 1), *i, &veh, begin, edges); 00085 if (edges.size() == 0) { 00086 return; 00087 } 00088 std::copy(edges.begin() + 1, edges.end(), back_inserter(newEdges)); 00089 } 00090 } 00091 if (myEdges != newEdges) { 00092 WRITE_WARNING("Repaired route of vehicle '" + veh.getID() + "'."); 00093 } 00094 myEdges = newEdges; 00095 } 00096 SUMOReal costs = router.recomputeCosts(myEdges, &veh, begin); 00097 if (costs < 0) { 00098 throw ProcessError("Route '" + getID() + "' (vehicle '" + veh.getID() + "') is not valid."); 00099 } 00100 myPrecomputed = new RORoute(myID, 0, 1, myEdges, copyColorIfGiven()); 00101 } 00102 00103 00104 void 00105 RORouteDef_Complete::addAlternative(SUMOAbstractRouter<ROEdge, ROVehicle> &, 00106 const ROVehicle* const, RORoute* current, SUMOTime begin) { 00107 myStartTime = begin; 00108 myEdges = current->getEdgeVector(); 00109 delete current; 00110 } 00111 00112 00113 RORouteDef* 00114 RORouteDef_Complete::copy(const std::string& id) const { 00115 return new RORouteDef_Complete(id, copyColorIfGiven(), myEdges, myTryRepair); 00116 } 00117 00118 00119 OutputDevice& 00120 RORouteDef_Complete::writeXMLDefinition(SUMOAbstractRouter<ROEdge, ROVehicle> &router, 00121 OutputDevice& dev, const ROVehicle* const veh, 00122 bool asAlternatives, bool withExitTimes) const { 00123 // (optional) alternatives header 00124 if (asAlternatives) { 00125 dev.openTag(SUMO_TAG_ROUTE_DISTRIBUTION).writeAttr(SUMO_ATTR_LAST, 0).closeOpener(); 00126 } 00127 // the route 00128 dev.openTag(SUMO_TAG_ROUTE); 00129 if (asAlternatives) { 00130 dev.writeAttr(SUMO_ATTR_COST, router.recomputeCosts(myEdges, veh, veh->getDepartureTime())); 00131 dev.writeAttr(SUMO_ATTR_PROB, 1.); 00132 } 00133 if (myColor != 0) { 00134 dev.writeAttr(SUMO_ATTR_COLOR, *myColor); 00135 } 00136 dev.writeAttr(SUMO_ATTR_EDGES, myEdges); 00137 if (withExitTimes) { 00138 std::string exitTimes; 00139 SUMOReal time = STEPS2TIME(veh->getDepartureTime()); 00140 for (std::vector<const ROEdge*>::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) { 00141 if (i != myEdges.begin()) { 00142 exitTimes += " "; 00143 } 00144 time += (*i)->getTravelTime(veh, time); 00145 exitTimes += toString(time); 00146 } 00147 dev.writeAttr("exitTimes", exitTimes); 00148 } 00149 dev.closeTag(true); 00150 // (optional) alternatives end 00151 if (asAlternatives) { 00152 dev.closeTag(); 00153 } 00154 return dev; 00155 } 00156 00157 00158 const ROEdge* 00159 RORouteDef_Complete::getDestination() const { 00160 return myEdges.back(); 00161 } 00162 00163 00164 /****************************************************************************/ 00165