SUMO - Simulation of Urban MObility

MSVehicleControl.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // The class responsible for building and deletion of vehicles
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 "MSVehicleControl.h"
00031 #include "MSVehicle.h"
00032 #include "MSLane.h"
00033 #include <microsim/devices/MSDevice.h>
00034 #include <utils/common/FileHelpers.h>
00035 #include <utils/common/RGBColor.h>
00036 #include <utils/common/SUMOVTypeParameter.h>
00037 #include <utils/iodevices/BinaryInputDevice.h>
00038 #include <utils/iodevices/OutputDevice.h>
00039 #include <utils/options/OptionsCont.h>
00040 
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044 
00045 
00046 // ===========================================================================
00047 // member method definitions
00048 // ===========================================================================
00049 MSVehicleControl::MSVehicleControl() throw() :
00050     myLoadedVehNo(0),
00051     myRunningVehNo(0),
00052     myEndedVehNo(0),
00053     myTotalDepartureDelay(0),
00054     myTotalTravelTime(0),
00055     myDefaultVTypeMayBeDeleted(true),
00056     myWaitingForPerson(0) {
00057     SUMOVTypeParameter defType;
00058     myVTypeDict[DEFAULT_VTYPE_ID] = MSVehicleType::build(defType);
00059 }
00060 
00061 
00062 MSVehicleControl::~MSVehicleControl() throw() {
00063     // delete vehicles
00064     for (VehicleDictType::iterator i=myVehicleDict.begin(); i!=myVehicleDict.end(); ++i) {
00065         delete(*i).second;
00066     }
00067     myVehicleDict.clear();
00068     // delete vehicle type distributions
00069     for (VTypeDistDictType::iterator i=myVTypeDistDict.begin(); i!=myVTypeDistDict.end(); ++i) {
00070         delete(*i).second;
00071     }
00072     myVTypeDistDict.clear();
00073     // delete vehicle types
00074     for (VTypeDictType::iterator i=myVTypeDict.begin(); i!=myVTypeDict.end(); ++i) {
00075         delete(*i).second;
00076     }
00077     myVTypeDict.clear();
00078 }
00079 
00080 
00081 SUMOVehicle*
00082 MSVehicleControl::buildVehicle(SUMOVehicleParameter* defs,
00083                                const MSRoute* route,
00084                                const MSVehicleType* type) throw(ProcessError) {
00085     myLoadedVehNo++;
00086     MSVehicle* built = new MSVehicle(defs, route, type, myLoadedVehNo-1);
00087     MSNet::getInstance()->informVehicleStateListener(built, MSNet::VEHICLE_STATE_BUILT);
00088     return built;
00089 }
00090 
00091 
00092 void
00093 MSVehicleControl::scheduleVehicleRemoval(SUMOVehicle* veh) throw() {
00094     assert(myRunningVehNo>0);
00095     for (std::vector<MSDevice*>::const_iterator i=veh->getDevices().begin(); i!=veh->getDevices().end(); ++i) {
00096         (*i)->generateOutput();
00097     }
00098     if (OptionsCont::getOptions().isSet("tripinfo-output")) {
00099         OutputDevice::getDeviceByOption("tripinfo-output").closeTag(veh->getDevices().size()==1);
00100     }
00101     myTotalTravelTime += STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - veh->getDeparture());
00102     myRunningVehNo--;
00103     MSNet::getInstance()->informVehicleStateListener(veh, MSNet::VEHICLE_STATE_ARRIVED);
00104     deleteVehicle(veh);
00105 }
00106 
00107 
00108 void
00109 MSVehicleControl::printMeanWaitingTime(OutputDevice& od) const throw() {
00110     if (getDepartedVehicleNo()==0) {
00111         od << -1.;
00112     } else {
00113         od << (myTotalDepartureDelay / (SUMOReal) getDepartedVehicleNo());
00114     }
00115 }
00116 
00117 
00118 void
00119 MSVehicleControl::printMeanTravelTime(OutputDevice& od) const throw() {
00120     if (myEndedVehNo==0) {
00121         od << -1.;
00122     } else {
00123         od << (myTotalTravelTime / (SUMOReal) myEndedVehNo);
00124     }
00125 }
00126 
00127 
00128 void
00129 MSVehicleControl::vehicleDeparted(const SUMOVehicle& v) throw() {
00130     ++myRunningVehNo;
00131     myTotalDepartureDelay += STEPS2TIME(v.getDeparture() - STEPFLOOR(v.getParameter().depart));
00132     MSNet::getInstance()->informVehicleStateListener(&v, MSNet::VEHICLE_STATE_DEPARTED);
00133 }
00134 
00135 
00136 void
00137 MSVehicleControl::saveState(std::ostream& /*os*/) throw() {
00138 }
00139 
00140 
00141 void
00142 MSVehicleControl::loadState(BinaryInputDevice& /*bis*/, const SUMOTime /*offset*/) throw() {
00143 }
00144 
00145 
00146 bool
00147 MSVehicleControl::addVehicle(const std::string& id, SUMOVehicle* v) throw() {
00148     VehicleDictType::iterator it = myVehicleDict.find(id);
00149     if (it == myVehicleDict.end()) {
00150         // id not in myVehicleDict.
00151         myVehicleDict[id] = v;
00152         return true;
00153     }
00154     return false;
00155 }
00156 
00157 
00158 SUMOVehicle*
00159 MSVehicleControl::getVehicle(const std::string& id) const throw() {
00160     VehicleDictType::const_iterator it = myVehicleDict.find(id);
00161     if (it == myVehicleDict.end()) {
00162         return 0;
00163     }
00164     return it->second;
00165 }
00166 
00167 
00168 void
00169 MSVehicleControl::deleteVehicle(SUMOVehicle* veh) throw() {
00170     myEndedVehNo++;
00171     myVehicleDict.erase(veh->getID());
00172     delete veh;
00173 }
00174 
00175 
00176 MSVehicleControl::constVehIt
00177 MSVehicleControl::loadedVehBegin() const throw() {
00178     return myVehicleDict.begin();
00179 }
00180 
00181 
00182 MSVehicleControl::constVehIt
00183 MSVehicleControl::loadedVehEnd() const throw() {
00184     return myVehicleDict.end();
00185 }
00186 
00187 
00188 bool
00189 MSVehicleControl::checkVType(const std::string& id) throw() {
00190     if (id == DEFAULT_VTYPE_ID) {
00191         if (myDefaultVTypeMayBeDeleted) {
00192             delete myVTypeDict[id];
00193             myVTypeDict.erase(myVTypeDict.find(id));
00194             myDefaultVTypeMayBeDeleted = false;
00195         } else {
00196             return false;
00197         }
00198     } else {
00199         if (myVTypeDict.find(id) != myVTypeDict.end() || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
00200             return false;
00201         }
00202     }
00203     return true;
00204 }
00205 
00206 bool
00207 MSVehicleControl::addVType(MSVehicleType* vehType) throw() {
00208     if (checkVType(vehType->getID())) {
00209         myVTypeDict[vehType->getID()] = vehType;
00210         return true;
00211     }
00212     return false;
00213 }
00214 
00215 
00216 bool
00217 MSVehicleControl::addVTypeDistribution(const std::string& id, RandomDistributor<MSVehicleType*> *vehTypeDistribution) throw() {
00218     if (checkVType(id)) {
00219         myVTypeDistDict[id] = vehTypeDistribution;
00220         return true;
00221     }
00222     return false;
00223 }
00224 
00225 
00226 bool
00227 MSVehicleControl::hasVTypeDistribution(const std::string& id) const throw() {
00228     return myVTypeDistDict.find(id) != myVTypeDistDict.end();
00229 }
00230 
00231 
00232 MSVehicleType*
00233 MSVehicleControl::getVType(const std::string& id) throw() {
00234     VTypeDictType::iterator it = myVTypeDict.find(id);
00235     if (it == myVTypeDict.end()) {
00236         VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
00237         if (it2 == myVTypeDistDict.end()) {
00238             return 0;
00239         }
00240         return it2->second->get();
00241     }
00242     if (id == DEFAULT_VTYPE_ID) {
00243         myDefaultVTypeMayBeDeleted = false;
00244     }
00245     return it->second;
00246 }
00247 
00248 
00249 void
00250 MSVehicleControl::insertVTypeIDs(std::vector<std::string> &into) const throw() {
00251     into.reserve(into.size()+myVTypeDict.size()+myVTypeDistDict.size());
00252     for (VTypeDictType::const_iterator i=myVTypeDict.begin(); i!=myVTypeDict.end(); ++i) {
00253         into.push_back((*i).first);
00254     }
00255     for (VTypeDistDictType::const_iterator i=myVTypeDistDict.begin(); i!=myVTypeDistDict.end(); ++i) {
00256         into.push_back((*i).first);
00257     }
00258 }
00259 
00260 
00261 void
00262 MSVehicleControl::addWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) throw() {
00263     if (myWaiting.find(edge) == myWaiting.end()) {
00264         myWaiting[edge] = std::vector<SUMOVehicle*>();
00265     }
00266     myWaiting[edge].push_back(vehicle);
00267 }
00268 
00269 
00270 void
00271 MSVehicleControl::removeWaiting(const MSEdge* const edge, SUMOVehicle* vehicle) throw() {
00272     if (myWaiting.find(edge) != myWaiting.end()) {
00273         std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting[edge].begin(), myWaiting[edge].end(), vehicle);
00274         if (it != myWaiting[edge].end()) {
00275             myWaiting[edge].erase(it);
00276         }
00277     }
00278 }
00279 
00280 
00281 SUMOVehicle*
00282 MSVehicleControl::getWaitingVehicle(const MSEdge* const edge, const std::set<std::string> &lines) throw() {
00283     if (myWaiting.find(edge) != myWaiting.end()) {
00284         for (std::vector<SUMOVehicle*>::const_iterator it = myWaiting[edge].begin(); it != myWaiting[edge].end(); ++it) {
00285             const std::string& line = (*it)->getParameter().line == "" ? (*it)->getParameter().id : (*it)->getParameter().line;
00286             if (lines.count(line)) {
00287                 return (*it);
00288             }
00289         }
00290     }
00291     return 0;
00292 }
00293 
00294 
00295 void
00296 MSVehicleControl::abortWaiting() throw() {
00297     for (VehicleDictType::iterator i=myVehicleDict.begin(); i!=myVehicleDict.end(); ++i) {
00298         WRITE_WARNING("Vehicle " + i->first + " aborted waiting for a person that will never come.");
00299     }
00300 }
00301 
00302 /****************************************************************************/
00303 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines