random walk position model (untested)
This commit is contained in:
@@ -129,6 +129,7 @@ simu.add_sources([
|
||||
'scheduler-map.cc',
|
||||
'event-impl.cc',
|
||||
'simulator.cc',
|
||||
'time-default-value.cc',
|
||||
])
|
||||
simu.add_headers([
|
||||
'scheduler-heap.h',
|
||||
@@ -144,6 +145,7 @@ simu.add_inst_headers([
|
||||
'scheduler.h',
|
||||
'scheduler-factory.h',
|
||||
'simulation-singleton.h',
|
||||
'time-default-value.h',
|
||||
])
|
||||
high_precision_as_double = ARGUMENTS.get('high-precision-as-double', 'n')
|
||||
if high_precision_as_double == 'y':
|
||||
@@ -247,6 +249,7 @@ node.add_sources ([
|
||||
'notify-static-position.cc',
|
||||
'static-speed-position.cc',
|
||||
'grid-topology.cc',
|
||||
'random-walk-position.cc',
|
||||
])
|
||||
node.add_inst_headers ([
|
||||
'node.h',
|
||||
@@ -269,6 +272,7 @@ node.add_inst_headers ([
|
||||
'notify-static-position.h',
|
||||
'static-speed-position.h',
|
||||
'grid-topology.h',
|
||||
'random-walk-position.h',
|
||||
])
|
||||
|
||||
applications = build.Ns3Module ('applications', 'src/applications')
|
||||
|
||||
153
src/node/random-walk-position.cc
Normal file
153
src/node/random-walk-position.cc
Normal file
@@ -0,0 +1,153 @@
|
||||
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2006,2007 INRIA
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation;
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
||||
*/
|
||||
#include "random-walk-position.h"
|
||||
#include "ns3/default-value.h"
|
||||
#include "ns3/time-default-value.h"
|
||||
#include "ns3/simulator.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
static IntegerDefaultValue<double> g_minSpeed ("RandomWalkMinSpeed",
|
||||
"Minimum speed used during a random walk",
|
||||
0);
|
||||
static IntegerDefaultValue<double> g_maxSpeed ("RandomWalkMaxSpeed",
|
||||
"Maximum speed used during a random walk",
|
||||
0);
|
||||
static EnumDefaultValue<RandomWalkPositionParameters::Mode> g_mode
|
||||
("RandomWalkMode",
|
||||
"The mode indicates the condition used to "
|
||||
"change the current speed and direction",
|
||||
RandomWalkPositionParameters::MODE_DISTANCE, "Distance",
|
||||
RandomWalkPositionParameters::MODE_TIME, "Time",
|
||||
0, 0);
|
||||
static IntegerDefaultValue<double> g_modeDistance ("RandomWalkModeDistance",
|
||||
"Distance to walk before changing direction and speed.",
|
||||
10);
|
||||
static TimeDefaultValue g_modeTime ("RandomWalkModeTime",
|
||||
"Time to walk before changing direction and speed.",
|
||||
Seconds (1));
|
||||
|
||||
RandomWalkPositionParameters::RandomWalkPositionParameters ()
|
||||
: m_minSpeed (g_minSpeed.GetValue ()),
|
||||
m_maxSpeed (g_maxSpeed.GetValue ()),
|
||||
m_mode (g_mode.GetValue ()),
|
||||
m_modeDistance (g_modeDistance.GetValue ()),
|
||||
m_modeTime (g_modeDistance.GetValue ())
|
||||
{}
|
||||
bool
|
||||
RandomWalkPositionParameters::IsDefault (void) const
|
||||
{
|
||||
if (m_minSpeed != g_minSpeed.GetValue () ||
|
||||
m_maxSpeed != g_maxSpeed.GetValue () ||
|
||||
m_mode != g_mode.GetValue () ||
|
||||
m_modeDistance != g_modeDistance.GetValue () ||
|
||||
m_modeTime != g_modeTime.GetValue ())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
RandomWalkPositionParameters::SetSpeedBounds (double minSpeed, double maxSpeed)
|
||||
{
|
||||
m_minSpeed = minSpeed;
|
||||
m_maxSpeed = maxSpeed;
|
||||
}
|
||||
|
||||
|
||||
UniformVariable RandomWalkPosition::m_randomDirection (0.0, 2*3.141592);
|
||||
|
||||
Ptr<RandomWalkPositionParameters>
|
||||
RandomWalkPosition::GetDefaultParameters (void)
|
||||
{
|
||||
static Ptr<RandomWalkPositionParameters> parameters = Create<RandomWalkPositionParameters> ();
|
||||
if (!parameters->IsDefault ())
|
||||
{
|
||||
parameters = Create<RandomWalkPositionParameters> ();
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
RandomWalkPosition::RandomWalkPosition ()
|
||||
: m_x (0.0),
|
||||
m_y (0.0),
|
||||
m_dx (0.0),
|
||||
m_dy (0.0),
|
||||
m_prevTime (Simulator::Now ()),
|
||||
m_parameters (RandomWalkPosition::GetDefaultParameters ())
|
||||
{
|
||||
Reset ();
|
||||
}
|
||||
|
||||
void
|
||||
RandomWalkPosition::Reset (void)
|
||||
{
|
||||
Update ();
|
||||
double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed,
|
||||
m_parameters->m_maxSpeed);
|
||||
double direction = m_randomDirection.GetValue ();
|
||||
double dx = std::cos (direction);
|
||||
double dy = std::sin (direction);
|
||||
m_dx = dx * speed;
|
||||
m_dy = dy * speed;
|
||||
Time delay;
|
||||
if (m_parameters->m_mode == RandomWalkPositionParameters::MODE_TIME)
|
||||
{
|
||||
delay = m_parameters->m_modeTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
double distance = g_modeDistance.GetValue ();
|
||||
delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy));
|
||||
}
|
||||
Simulator::Schedule (delay, &RandomWalkPosition::Reset, this);
|
||||
}
|
||||
|
||||
void
|
||||
RandomWalkPosition::Update (void) const
|
||||
{
|
||||
Time deltaTime = Simulator::Now () - m_prevTime;
|
||||
m_prevTime = Simulator::Now ();
|
||||
double deltaS = deltaTime.GetSeconds ();
|
||||
m_x += m_dx * deltaS;
|
||||
m_y += m_dy * deltaS;
|
||||
}
|
||||
|
||||
void
|
||||
RandomWalkPosition::DoGet (double &x, double &y, double &z) const
|
||||
{
|
||||
Update ();
|
||||
x = m_x;
|
||||
y = m_y;
|
||||
z = 0;
|
||||
}
|
||||
void
|
||||
RandomWalkPosition::DoSet (double x, double y, double z)
|
||||
{
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_prevTime = Simulator::Now ();
|
||||
}
|
||||
|
||||
|
||||
} // namespace ns3
|
||||
94
src/node/random-walk-position.h
Normal file
94
src/node/random-walk-position.h
Normal file
@@ -0,0 +1,94 @@
|
||||
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2006,2007 INRIA
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation;
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
||||
*/
|
||||
#ifndef RANDOM_WALK_POSITION_H
|
||||
#define RANDOM_WALK_POSITION_H
|
||||
|
||||
#include "ns3/object.h"
|
||||
#include "ns3/position.h"
|
||||
#include "ns3/nstime.h"
|
||||
#include "ns3/random-variable.h"
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
class RandomWalkPositionParameters : public Object
|
||||
{
|
||||
public:
|
||||
enum Mode {
|
||||
MODE_DISTANCE,
|
||||
MODE_TIME
|
||||
};
|
||||
RandomWalkPositionParameters ();
|
||||
bool IsDefault (void) const;
|
||||
void SetSpeedBounds (double minSpeed, double maxSpeed);
|
||||
private:
|
||||
friend class RandomWalkPosition;
|
||||
double m_xMin;
|
||||
double m_xMax;
|
||||
double m_yMin;
|
||||
double m_yMax;
|
||||
double m_minSpeed;
|
||||
double m_maxSpeed;
|
||||
enum Mode m_mode;
|
||||
double m_modeDistance;
|
||||
Time m_modeTime;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief a random walk position model
|
||||
*
|
||||
* Each instance moves with a speed and direction choosen at random
|
||||
* in the intervals [minspeed,maxspeed] and [0,2pi] until
|
||||
* either a fixed distance has been walked or until a fixed period
|
||||
* of time.
|
||||
*/
|
||||
class RandomWalkPosition : public Position
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create a new position object located at a random
|
||||
* position within the default random walk area.
|
||||
*/
|
||||
RandomWalkPosition ();
|
||||
RandomWalkPosition (double x, double y);
|
||||
RandomWalkPosition (Ptr<RandomWalkPositionParameters> parameters);
|
||||
RandomWalkPosition (Ptr<RandomWalkPositionParameters> parameters,
|
||||
double x, double y);
|
||||
private:
|
||||
virtual void DoGet (double &x, double &y, double &z) const;
|
||||
virtual void DoSet (double x, double y, double z);
|
||||
|
||||
void Reset (void);
|
||||
void Update (void) const;
|
||||
static Ptr<RandomWalkPositionParameters> GetDefaultParameters (void);
|
||||
static UniformVariable m_randomDirection;
|
||||
|
||||
mutable double m_x;
|
||||
mutable double m_y;
|
||||
double m_dx;
|
||||
double m_dy;
|
||||
mutable Time m_prevTime;
|
||||
Ptr<RandomWalkPositionParameters> m_parameters;
|
||||
};
|
||||
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
#endif /* RANDOM_WALK_POSITION_H */
|
||||
Reference in New Issue
Block a user