HowToAddRWPMobility

Adding the Random Waypoint Mobility Model to MF

Existing versions of the MF only have the 'ConstSpeedMobility' mobility model, which works similar to the actual Random Waypoint Mobility Model, but not quite. Following are steps that you can use in order to incorporate the code for the Random Waypoint Mobility Model alreday existing in the INET Framework into your own MF code. If you don't have the INET framework installed, just download a copy and extract the necessary files as instructed.

It shouldn't take you more than a few minutes to get it done. If you are hesitant to make changes to your working MF installation, you might create a twin MF directory and use it as a sandbox to make sure it works first (in which case, don't forget to make the necessary changes to your environmental variables).


First, copy the RandomWPMobility.* files from the /../INET/Mobility directory to /../protocols/mobility of your MF directory.

Because there are several changes that need to be made to these newly copied RandomWPMobility.* files, you might as well just copy the following code and paste it in order to replace the existing code. Alternatively, you may modify the file directly. In any case, RandomWPMobility.h in /../protocols/mobility of your MF need to look like this:

 #ifndef RANDOMWP_MOBILITY_H
 #define RANDOMWP_MOBILITY_H

 #include <omnetpp.h>

 #include "BasicMobility.h"

 class RandomWPMobility : public BasicMobility
 {
  protected:
     bool nextMoveIsWait;
     bool stationary;
     double targetTime;
     Coord targetPos;
     double updateInterval; ///< time interval to update the host's position
     Coord step;         ///< step size (added to pos every updateInterval)

     /** @brief Called upon arrival of a self messages*/
     virtual void handleSelfMsg(cMessage *msg);

     /** @brief Overridden from LineSegmentsMobilityBase.*/
     virtual void setTargetPosition();

     /** @brief Overridden from LineSegmentsMobilityBase.*/
     virtual void fixIfHostGetsOutside();

     /** @brief Begin new line segment after previous one finished */
     virtual void beginNextMove(cMessage *msg);

     /** @brief Move the host*/


  public:
     Module_Class_Members(RandomWPMobility, BasicMobility, 0);

     /** @brief Initializes mobility model parameters.*/
     virtual void initialize(int);

 };

 #endif

Then, change RandomWPMobility.cc so that it looks like:

 #include "RandomWPMobility.h"
 #include "FWMath.h"

 Define_Module(RandomWPMobility);

 void RandomWPMobility::initialize(int stage)
 {
     BasicMobility::initialize(stage);

     if (stage == 0)
     {
         stationary = (par("speed").type()=='L' || par("speed").type()=='D') && (double)par("speed") == 0;
         nextMoveIsWait = false;
     }

     if (stage == 1)
     {
         updateInterval = par("updateInterval");
         stationary = false;
         targetPos = pos;
         targetTime = simTime();

         // host moves the first time after some random delay to avoid synchronized movements
         scheduleAt(simTime() + uniform(0, updateInterval), new cMessage("move"));
     }
 }

 void RandomWPMobility::handleSelfMsg(cMessage *msg)
 {
     if (stationary)
     {
         delete msg;
         return;
     }
     else if (simTime()+updateInterval >= targetTime)
     {
         beginNextMove(msg);
     }
     else
     {
         scheduleAt(simTime() + updateInterval, msg);
     }

     // update position
     pos += step;

     // do something if we reach the wall
     fixIfHostGetsOutside();

     //EV << " xpos=" << pos.x << " ypos=" << pos.y << endl;

     updatePosition();
 }

 void RandomWPMobility::beginNextMove(cMessage *msg)
 {
     // go to exact position where previous statement was supposed to finish
     pos = targetPos;
     simtime_t now = targetTime;

     // choose new targetTime and targetPos
     setTargetPosition();

     if (targetTime<now)
         error("LineSegmentsMobilityBase: targetTime<now was set in %s's beginNextMove()", className());

     if (stationary)
     {
         // end of movement
         step.x = step.y = 0;
         delete msg;
     }
     else if (targetPos==pos)
     {
         // no movement, just wait
         step.x = step.y = 0;
         scheduleAt(Max(targetTime,simTime()), msg);
     }
     else
     {
         // keep moving
         double numIntervals = (targetTime-now) / updateInterval;
         // int numSteps = floor(numIntervals); -- currently unused,
         // although we could use step counting instead of comparing
         // simTime() to targetTime each step.

         // Note: step = speed*updateInterval = distance/time*updateInterval =
         //        = (targetPos-pos) / (targetTime-now) * updateInterval =
         //        = (targetPos-pos) / numIntervals
         step = (targetPos - pos) / numIntervals;
         scheduleAt(simTime() + updateInterval, msg);
     }
 }

 void RandomWPMobility::setTargetPosition()
 {
     if (nextMoveIsWait)
     {
         double waitTime = par("waitTime");
         targetTime += waitTime;
     }
     else
     {
         targetPos = getRandomPosition();
         double speed = par("speed");
         double distance = pos.distance(targetPos);
         double travelTime = distance / speed;
         targetTime += travelTime;
     }

     nextMoveIsWait = !nextMoveIsWait;
 }

 void RandomWPMobility::fixIfHostGetsOutside()
 {
     //raiseErrorIfOutside();

 }

Add the following function prototypes to Coord.h in the /../core/utils directory of your MF directory

 /** Multiplies a coordinate vector by a real number.*/
 friend Coord operator*(Coord a, double f) {
       return Coord(a.x*f, a.y*f);
 }

 /** Divides a coordinate vector by a real number.*/
 friend Coord operator/(Coord a, double f) {
       return Coord(a.x/f, a.y/f);
 }

Then, go to the root of your MF directory and recompile everything. In theory, recompiling only those directories whose files had updates should work, but I recompiled everything just to make sure there were no conflicts. So, first run:

 make clean

then:

 mkmk

and finally:

 make install && make networks

That's it! And it worked for me just fine. You may now use RandomWPMobility instead of ConstSpeedMobility by making the appropriate changes in your *.ned files. Also, remember to update your omnetpp.ini file accordingly in order to initialize variables in the RandomWPMobility module. Look into the omnetpp.ini files of the existing mobility samples in the INET directory that make use of this mobility model to see how it's done.

Finally, as far as was able to discern from the model's code, this version of the RWP mobility model does not take into account those well-known issues pertaining to this mobility model [1,2].

Good Luck

Sergio

[1] J. Yoon, M. Liu, and B. Noble, "Random Waypoint Considered Harmful." In proceedings of IEEE INFOCOM, April, 2003.

[2] G. Lin, G. Noubir, and R. Rajaraman, "Mobility Models for Ad-hoc Network Simulation." In proceedings of IEEE INFOCOM, April, 2004.

Edit - History - Print - Recent Changes - Search
Page last modified on December 02, 2005, at 06:56 AM