|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // Writes route distributions at a certain edge 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 #include <string> 00031 #include <microsim/MSEdge.h> 00032 #include <microsim/MSLane.h> 00033 #include <microsim/MSGlobals.h> 00034 #include <microsim/MSRoute.h> 00035 #include <microsim/MSVehicle.h> 00036 #include <utils/common/ToString.h> 00037 #include <utils/iodevices/OutputDevice.h> 00038 #ifdef HAVE_MESOSIM 00039 #include <mesosim/MELoop.h> 00040 #include <mesosim/MESegment.h> 00041 #endif 00042 #include "MSRouteProbe.h" 00043 00044 #ifdef CHECK_MEMORY_LEAKS 00045 #include <foreign/nvwa/debug_new.h> 00046 #endif // CHECK_MEMORY_LEAKS 00047 00048 00049 // =========================================================================== 00050 // method definitions 00051 // =========================================================================== 00052 MSRouteProbe::MSRouteProbe(const std::string& id, const MSEdge* edge, SUMOTime begin) throw() 00053 : MSDetectorFileOutput(id), myCurrentRouteDistribution(0) { 00054 const std::string distID = id + "_" + toString(begin); 00055 myCurrentRouteDistribution = MSRoute::distDictionary(distID); 00056 if (myCurrentRouteDistribution == 0) { 00057 myCurrentRouteDistribution = new RandomDistributor<const MSRoute*>(); 00058 MSRoute::dictionary(distID, myCurrentRouteDistribution); 00059 } 00060 #ifdef HAVE_MESOSIM 00061 if (MSGlobals::gUseMesoSim) { 00062 MESegment* seg = MSGlobals::gMesoNet->getSegmentForEdge(*edge); 00063 while (seg!=0) { 00064 seg->addDetector(this); 00065 seg = seg->getNextSegment(); 00066 } 00067 return; 00068 } 00069 #endif 00070 for (std::vector<MSLane*>::const_iterator it = edge->getLanes().begin(); it!=edge->getLanes().end(); ++it) { 00071 (*it)->addMoveReminder(this); 00072 } 00073 } 00074 00075 00076 MSRouteProbe::~MSRouteProbe() throw() { 00077 } 00078 00079 00080 bool 00081 MSRouteProbe::notifyEnter(SUMOVehicle& veh, MSMoveReminder::Notification reason) throw() { 00082 if (myCurrentRouteDistribution != 0 && reason != MSMoveReminder::NOTIFICATION_SEGMENT && reason != MSMoveReminder::NOTIFICATION_LANE_CHANGE) { 00083 veh.getRoute().addReference(); 00084 myCurrentRouteDistribution->add(1., &veh.getRoute()); 00085 } 00086 return false; 00087 } 00088 00089 00090 void 00091 MSRouteProbe::writeXMLOutput(OutputDevice& dev, 00092 SUMOTime startTime, SUMOTime stopTime) throw(IOError) { 00093 if (myCurrentRouteDistribution->getOverallProb() > 0) { 00094 dev.openTag("routeDistribution") << " id=\"" << getID() + "_" + time2string(startTime) << "\">\n"; 00095 const std::vector<const MSRoute*> &routes = myCurrentRouteDistribution->getVals(); 00096 const std::vector<SUMOReal> &probs = myCurrentRouteDistribution->getProbs(); 00097 for (unsigned int j=0; j<routes.size(); ++j) { 00098 const MSRoute* r = routes[j]; 00099 dev.openTag("route") << " id=\"" << r->getID() + "_" + time2string(startTime) << "\" edges=\""; 00100 for (MSRouteIterator i = r->begin(); i!=r->end(); ++i) { 00101 if (i != r->begin()) { 00102 dev << " "; 00103 } 00104 dev << (*i)->getID(); 00105 } 00106 dev << "\" probability=\"" << probs[j] << "\""; 00107 dev.closeTag(true); 00108 } 00109 dev.closeTag(); 00110 myCurrentRouteDistribution = new RandomDistributor<const MSRoute*>(); 00111 MSRoute::dictionary(getID() + "_" + toString(stopTime), myCurrentRouteDistribution); 00112 } 00113 } 00114 00115 00116 void 00117 MSRouteProbe::writeXMLDetectorProlog(OutputDevice& dev) const throw(IOError) { 00118 dev.writeXMLHeader("route-probes"); 00119 }