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