SUMO - Simulation of Urban MObility

MSVehicleType.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00011 // The car-following model and parameter
00012 /****************************************************************************/
00013 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00014 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00015 /****************************************************************************/
00016 //
00017 //   This file is part of SUMO.
00018 //   SUMO is free software: you can redistribute it and/or modify
00019 //   it under the terms of the GNU General Public License as published by
00020 //   the Free Software Foundation, either version 3 of the License, or
00021 //   (at your option) any later version.
00022 //
00023 /****************************************************************************/
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #include <cassert>
00036 #include <utils/iodevices/BinaryInputDevice.h>
00037 #include <utils/common/FileHelpers.h>
00038 #include <utils/common/RandHelper.h>
00039 #include <utils/common/SUMOVTypeParameter.h>
00040 #include "MSNet.h"
00041 #include "cfmodels/MSCFModel_IDM.h"
00042 #include "cfmodels/MSCFModel_Kerner.h"
00043 #include "cfmodels/MSCFModel_Krauss.h"
00044 #include "cfmodels/MSCFModel_KraussOrig1.h"
00045 #include "cfmodels/MSCFModel_PWag2009.h"
00046 #include "cfmodels/MSCFModel_Wiedemann.h"
00047 #include "MSVehicleType.h"
00048 
00049 #ifdef CHECK_MEMORY_LEAKS
00050 #include <foreign/nvwa/debug_new.h>
00051 #endif // CHECK_MEMORY_LEAKS
00052 
00053 
00054 // ===========================================================================
00055 // method definitions
00056 // ===========================================================================
00057 MSVehicleType::MSVehicleType(const std::string& id, SUMOReal length,
00058                              SUMOReal minGap, SUMOReal maxSpeed, SUMOReal prob,
00059                              SUMOReal speedFactor, SUMOReal speedDev,
00060                              SUMOVehicleClass vclass,
00061                              SUMOEmissionClass emissionClass,
00062                              SUMOReal guiWidth, SUMOVehicleShape shape,
00063                              const std::string& lcModel,
00064                              const RGBColor& c)
00065     : myID(id), myLength(length),
00066       myMinGap(minGap), myMaxSpeed(maxSpeed),
00067       myDefaultProbability(prob), mySpeedFactor(speedFactor),
00068       mySpeedDev(speedDev), myLaneChangeModel(lcModel),
00069       myEmissionClass(emissionClass), myColor(c),
00070       myVehicleClass(vclass), myWidth(guiWidth), myShape(shape),
00071       myOriginalType(0) {
00072     assert(myLength > 0);
00073     assert(getMaxSpeed() > 0);
00074 }
00075 
00076 
00077 MSVehicleType::~MSVehicleType() {
00078     delete myCarFollowModel;
00079 }
00080 
00081 
00082 void
00083 MSVehicleType::saveState(std::ostream& os) {
00084     FileHelpers::writeString(os, myID);
00085     FileHelpers::writeFloat(os, myLength);
00086     FileHelpers::writeFloat(os, myMinGap);
00087     FileHelpers::writeFloat(os, getMaxSpeed());
00088     FileHelpers::writeInt(os, (int) myVehicleClass);
00089     FileHelpers::writeInt(os, (int) myEmissionClass);
00090     FileHelpers::writeInt(os, (int) myShape);
00091     FileHelpers::writeFloat(os, myWidth);
00092     FileHelpers::writeFloat(os, myDefaultProbability);
00093     FileHelpers::writeFloat(os, mySpeedFactor);
00094     FileHelpers::writeFloat(os, mySpeedDev);
00095     FileHelpers::writeFloat(os, myColor.red());
00096     FileHelpers::writeFloat(os, myColor.green());
00097     FileHelpers::writeFloat(os, myColor.blue());
00098     FileHelpers::writeInt(os, myCarFollowModel->getModelID());
00099     FileHelpers::writeString(os, myLaneChangeModel);
00100     //myCarFollowModel->saveState(os);
00101 }
00102 
00103 
00104 // ------------ Setter methods
00105 void
00106 MSVehicleType::setLength(const SUMOReal& length) {
00107     assert(myOriginalType != 0);
00108     if (length < 0) {
00109         myLength = myOriginalType->myLength;
00110     } else {
00111         myLength = length;
00112     }
00113 }
00114 
00115 
00116 void
00117 MSVehicleType::setMinGap(const SUMOReal& minGap) {
00118     assert(myOriginalType != 0);
00119     if (minGap < 0) {
00120         myMinGap = myOriginalType->myMinGap;
00121     } else {
00122         myMinGap = minGap;
00123     }
00124 }
00125 
00126 
00127 void
00128 MSVehicleType::setMaxSpeed(const SUMOReal& maxSpeed) {
00129     assert(myOriginalType != 0);
00130     if (maxSpeed < 0) {
00131         myMaxSpeed = myOriginalType->myMaxSpeed;
00132     } else {
00133         myMaxSpeed = maxSpeed;
00134     }
00135 }
00136 
00137 
00138 void
00139 MSVehicleType::setVClass(SUMOVehicleClass vclass) {
00140     myVehicleClass = vclass;
00141 }
00142 
00143 
00144 void
00145 MSVehicleType::setDefaultProbability(const SUMOReal& prob) {
00146     assert(myOriginalType != 0);
00147     if (prob < 0) {
00148         myDefaultProbability = myOriginalType->myDefaultProbability;
00149     } else {
00150         myDefaultProbability = prob;
00151     }
00152 }
00153 
00154 
00155 void
00156 MSVehicleType::setSpeedFactor(const SUMOReal& factor) {
00157     assert(myOriginalType != 0);
00158     if (factor < 0) {
00159         mySpeedFactor = myOriginalType->mySpeedFactor;
00160     } else {
00161         mySpeedFactor = factor;
00162     }
00163 }
00164 
00165 
00166 void
00167 MSVehicleType::setSpeedDeviation(const SUMOReal& dev) {
00168     assert(myOriginalType != 0);
00169     if (dev < 0) {
00170         mySpeedDev = myOriginalType->mySpeedDev;
00171     } else {
00172         mySpeedDev = dev;
00173     }
00174 }
00175 
00176 
00177 void
00178 MSVehicleType::setEmissionClass(SUMOEmissionClass eclass) {
00179     myEmissionClass = eclass;
00180 }
00181 
00182 
00183 void
00184 MSVehicleType::setColor(const RGBColor& color) {
00185     myColor = color;
00186 }
00187 
00188 
00189 void
00190 MSVehicleType::setWidth(const SUMOReal& width) {
00191     assert(myOriginalType != 0);
00192     if (width < 0) {
00193         myWidth = myOriginalType->myWidth;
00194     } else {
00195         myWidth = width;
00196     }
00197 }
00198 
00199 
00200 void
00201 MSVehicleType::setShape(SUMOVehicleShape shape) {
00202     myShape = shape;
00203 }
00204 
00205 
00206 
00207 // ------------ Static methods for building vehicle types
00208 SUMOReal
00209 MSVehicleType::get(const SUMOVTypeParameter::CFParams& from, SumoXMLAttr attr, SUMOReal defaultValue) {
00210     if (from.count(attr)) {
00211         return from.find(attr)->second;
00212     } else {
00213         return defaultValue;
00214     }
00215 }
00216 
00217 
00218 MSVehicleType*
00219 MSVehicleType::build(SUMOVTypeParameter& from) {
00220     MSVehicleType* vtype = new MSVehicleType(
00221         from.id, from.length, from.minGap, from.maxSpeed,
00222         from.defaultProbability, from.speedFactor, from.speedDev, from.vehicleClass, from.emissionClass,
00223         from.width, from.shape, from.lcModel, from.color);
00224     MSCFModel* model = 0;
00225     switch (from.cfModel) {
00226         case SUMO_TAG_CF_IDM:
00227             model = new MSCFModel_IDM(vtype,
00228                                       get(from.cfParameter, SUMO_ATTR_ACCEL, DEFAULT_VEH_ACCEL),
00229                                       get(from.cfParameter, SUMO_ATTR_DECEL, DEFAULT_VEH_DECEL),
00230                                       get(from.cfParameter, SUMO_ATTR_TAU, DEFAULT_VEH_TAU),
00231                                       get(from.cfParameter, SUMO_ATTR_CF_IDM_DELTA, 4.),
00232                                       get(from.cfParameter, SUMO_ATTR_CF_IDM_STEPPING, .25));
00233             break;
00234         case SUMO_TAG_CF_IDMM:
00235             model = new MSCFModel_IDM(vtype,
00236                                       get(from.cfParameter, SUMO_ATTR_ACCEL, DEFAULT_VEH_ACCEL),
00237                                       get(from.cfParameter, SUMO_ATTR_DECEL, DEFAULT_VEH_DECEL),
00238                                       get(from.cfParameter, SUMO_ATTR_TAU, DEFAULT_VEH_TAU),
00239                                       get(from.cfParameter, SUMO_ATTR_CF_IDMM_ADAPT_FACTOR, 1.8),
00240                                       get(from.cfParameter, SUMO_ATTR_CF_IDMM_ADAPT_TIME, 600.),
00241                                       get(from.cfParameter, SUMO_ATTR_CF_IDM_STEPPING, .25));
00242             break;
00243         case SUMO_TAG_CF_BKERNER:
00244             model = new MSCFModel_Kerner(vtype,
00245                                          get(from.cfParameter, SUMO_ATTR_ACCEL, DEFAULT_VEH_ACCEL),
00246                                          get(from.cfParameter, SUMO_ATTR_DECEL, DEFAULT_VEH_DECEL),
00247                                          get(from.cfParameter, SUMO_ATTR_TAU, DEFAULT_VEH_TAU),
00248                                          get(from.cfParameter, SUMO_ATTR_K, .5),
00249                                          get(from.cfParameter, SUMO_ATTR_CF_KERNER_PHI, 5.));
00250             break;
00251         case SUMO_TAG_CF_KRAUSS_ORIG1:
00252             model = new MSCFModel_KraussOrig1(vtype,
00253                                               get(from.cfParameter, SUMO_ATTR_ACCEL, DEFAULT_VEH_ACCEL),
00254                                               get(from.cfParameter, SUMO_ATTR_DECEL, DEFAULT_VEH_DECEL),
00255                                               get(from.cfParameter, SUMO_ATTR_SIGMA, DEFAULT_VEH_SIGMA),
00256                                               get(from.cfParameter, SUMO_ATTR_TAU, DEFAULT_VEH_TAU));
00257             break;
00258         case SUMO_TAG_CF_PWAGNER2009:
00259             model = new MSCFModel_PWag2009(vtype,
00260                                            get(from.cfParameter, SUMO_ATTR_ACCEL, DEFAULT_VEH_ACCEL),
00261                                            get(from.cfParameter, SUMO_ATTR_DECEL, DEFAULT_VEH_DECEL),
00262                                            get(from.cfParameter, SUMO_ATTR_SIGMA, DEFAULT_VEH_SIGMA),
00263                                            get(from.cfParameter, SUMO_ATTR_TAU, DEFAULT_VEH_TAU),
00264                                            get(from.cfParameter, SUMO_ATTR_CF_PWAGNER2009_TAULAST, 0.3),
00265                                            get(from.cfParameter, SUMO_ATTR_CF_PWAGNER2009_APPROB, 0.5));
00266             break;
00267         case SUMO_TAG_CF_WIEDEMANN:
00268             model = new MSCFModel_Wiedemann(vtype,
00269                                             get(from.cfParameter, SUMO_ATTR_ACCEL, DEFAULT_VEH_ACCEL),
00270                                             get(from.cfParameter, SUMO_ATTR_DECEL, DEFAULT_VEH_DECEL),
00271                                             get(from.cfParameter, SUMO_ATTR_CF_WIEDEMANN_SECURITY, 0.5),
00272                                             get(from.cfParameter, SUMO_ATTR_CF_WIEDEMANN_ESTIMATION, 0.5));
00273             break;
00274         case SUMO_TAG_CF_KRAUSS:
00275         default:
00276             model = new MSCFModel_Krauss(vtype,
00277                                          get(from.cfParameter, SUMO_ATTR_ACCEL, DEFAULT_VEH_ACCEL),
00278                                          get(from.cfParameter, SUMO_ATTR_DECEL, DEFAULT_VEH_DECEL),
00279                                          get(from.cfParameter, SUMO_ATTR_SIGMA, DEFAULT_VEH_SIGMA),
00280                                          get(from.cfParameter, SUMO_ATTR_TAU, DEFAULT_VEH_TAU));
00281             break;
00282     }
00283     vtype->myCarFollowModel = model;
00284     return vtype;
00285 }
00286 
00287 
00288 MSVehicleType*
00289 MSVehicleType::build(const std::string& id, const MSVehicleType* from) {
00290     MSVehicleType* vtype = new MSVehicleType(
00291         id, from->myLength, from->myMinGap, from->myMaxSpeed,
00292         from->myDefaultProbability, from->mySpeedFactor, from->mySpeedDev, from->myVehicleClass, from->myEmissionClass,
00293         from->myWidth, from->myShape, from->myLaneChangeModel, from->myColor);
00294     vtype->myCarFollowModel = from->myCarFollowModel->duplicate(vtype);
00295     vtype->myOriginalType = from->myOriginalType != 0 ? from->myOriginalType : from;
00296     return vtype;
00297 }
00298 
00299 
00300 /****************************************************************************/
00301 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines