Developer/How To/Car-Following Model

This short document describes how a new car-following model can be added to SUMO. We'll do this by implementing a test model named "smartSK". When implementing your own model, you should also grep (look for) occurences of the name we use, here.

The car-following model's class

The best thing is to start with an already existing model. Models are located in <SUMO_HOME>/src/microsim/cfmodels and new added models should reside here, too. Copy both MSCFModel_KraussOrig1.h and MSCFModel_KraussOrig1.cpp and rename them. The name should be "MSCFModel_<YOUR_MODELS_NAME>", in our case "MSCFModel_SmartSK"

Convention: Car-following model implementations are located in <SUMO_HOME>/src/microsim/cfmodels.
Convention: A car-following class' name should start with "MSCFModel_".

Now, open both files and rename all occurences of MSCFModel_KraussOrig1 into your class' name.

Add the files to the makefile which is in this folder and to the MSVC10 project located in <SUMO_HOME>/build/msvc10/y_libmicrosim.

Loading into simulation

We now add the XML-elements which allow us to define and parse the model's parameter. Extend the list of known elements "SUMOXMLDefinitions::tags" located in <SUMO_HOME>/src/utils/xml/SUMOXMLDefinitions. In SUMOXMLDefinitions.h:

SUMO_TAG_CF_SMART_SK,

In SUMOXMLDefinitions.cpp:

{ "carFollowing-SmartSK",     SUMO_TAG_CF_SMART_SK },


Car-following models are instantiated in MSVehicleType::build(...) located in <SUMO_HOME>/src/microsim/MSVehicleType.cpp. You'll find a switch, here where you have to put the call to your model's constructor into.

case SUMO_TAG_CF_SMART_SK:
  model = new MSCFModel_SmartSK(vtype,
                                from.get(SUMO_ATTR_ACCEL, DEFAULT_VEH_ACCEL),
                                from.get(SUMO_ATTR_DECEL, DEFAULT_VEH_DECEL),
                                from.get(SUMO_ATTR_SIGMA, DEFAULT_VEH_SIGMA),
                                from.get(SUMO_ATTR_TAU, DEFAULT_VEH_TAU),
                                from.get(SUMO_ATTR_TMP1, DEFAULT_VEH_TMP1),
                                from.get(SUMO_ATTR_TMP1, DEFAULT_VEH_TMP2),
                                from.get(SUMO_ATTR_TMP1, DEFAULT_VEH_TMP3),
                                from.get(SUMO_ATTR_TMP1, DEFAULT_VEH_TMP4),
                                from.get(SUMO_ATTR_TMP1, DEFAULT_VEH_TMP5));


You may note that the constructor is read with values from "from". The first parameter, starting with SUMO_ATTR_ denotes the XML-attribute which names the parameter. These attributes, if new, have to be added to <SUMO_HOME>/src/utils/xml/SUMOXMLDefinitions. In SUMOXMLDefinitions.h:

SUMO_ATTR_TMP1,
SUMO_ATTR_TMP2,
SUMO_ATTR_TMP3,
SUMO_ATTR_TMP4,
SUMO_ATTR_TMP5,

In SUMOXMLDefinitions.cpp:

{ "tmp1",           SUMO_ATTR_TMP1 },
{ "tmp2",           SUMO_ATTR_TMP2 },
{ "tmp3",           SUMO_ATTR_TMP3 },
{ "tmp4",           SUMO_ATTR_TMP4 },
{ "tmp5",           SUMO_ATTR_TMP5 },

You also have to define which parameter must be read in <SUMO_HOME>/src/utils/xml/SUMOVehicleParserHelper, method getAllowedCFModelAttrs():

std::set<SumoXMLAttr> smartSKParams;
smartSKParams.insert(SUMO_ATTR_ACCEL);
smartSKParams.insert(SUMO_ATTR_DECEL);
smartSKParams.insert(SUMO_ATTR_SIGMA);
smartSKParams.insert(SUMO_ATTR_TAU);
smartSKParams.insert(SUMO_ATTR_TMP1);
smartSKParams.insert(SUMO_ATTR_TMP2);
smartSKParams.insert(SUMO_ATTR_TMP3);
smartSKParams.insert(SUMO_ATTR_TMP4);
smartSKParams.insert(SUMO_ATTR_TMP5);
allowedCFModelAttrs[SUMO_TAG_CF_SMART_SK] = smartSKParams;


The second parameter, starting with DEFAULT_VEH_ is a constant default value. It has to be defined in <SUMO_HOME>/src/utils/common/SUMOVehicleClass, if new. In SUMOVehicleClass.h:

extern const SUMOReal DEFAULT_VEH_TMP1;
extern const SUMOReal DEFAULT_VEH_TMP2;
extern const SUMOReal DEFAULT_VEH_TMP3;
extern const SUMOReal DEFAULT_VEH_TMP4;
extern const SUMOReal DEFAULT_VEH_TMP5;

and in SUMOVehicleClass.cpp:

const SUMOReal DEFAULT_VEH_TMP1(1.);
const SUMOReal DEFAULT_VEH_TMP2(1.);
const SUMOReal DEFAULT_VEH_TMP3(1.);
const SUMOReal DEFAULT_VEH_TMP4(1.);
const SUMOReal DEFAULT_VEH_TMP5(1.);


Note, that we have to adapt the constructor to retrieve the additional parameter (tmp1-tmp5). We have also to adapt the copy constructor located in our class in MSCFModel_SmartSK::duplicate(...).


For further interaction, you also have to adapt the "id" of the model in the model's .h class:

virtual int getModelID() const {
 return SUMO_TAG_CF_SMART_SK;
}

This page was last modified on 6 June 2012, at 11:57.