A Discrete-Event Network Simulator
API
gauss-markov-mobility-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 Dan Broyles
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Dan Broyles <dbroyl01@ku.edu>
19  */
20 #include <cmath>
21 #include "ns3/simulator.h"
22 #include "ns3/double.h"
23 #include "ns3/pointer.h"
24 #include "ns3/string.h"
26 #include "position-allocator.h"
27 
28 namespace ns3 {
29 
30 NS_OBJECT_ENSURE_REGISTERED (GaussMarkovMobilityModel);
31 
32 TypeId
34 {
35  static TypeId tid = TypeId ("ns3::GaussMarkovMobilityModel")
37  .SetGroupName ("Mobility")
38  .AddConstructor<GaussMarkovMobilityModel> ()
39  .AddAttribute ("Bounds",
40  "Bounds of the area to cruise.",
41  BoxValue (Box (-100.0, 100.0, -100.0, 100.0, 0.0, 100.0)),
43  MakeBoxChecker ())
44  .AddAttribute ("TimeStep",
45  "Change current direction and speed after moving for this time.",
46  TimeValue (Seconds (1.0)),
48  MakeTimeChecker ())
49  .AddAttribute ("Alpha",
50  "A constant representing the tunable parameter in the Gauss-Markov model.",
51  DoubleValue (1.0),
53  MakeDoubleChecker<double> ())
54  .AddAttribute ("MeanVelocity",
55  "A random variable used to assign the average velocity.",
56  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
58  MakePointerChecker<RandomVariableStream> ())
59  .AddAttribute ("MeanDirection",
60  "A random variable used to assign the average direction.",
61  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.283185307]"),
63  MakePointerChecker<RandomVariableStream> ())
64  .AddAttribute ("MeanPitch",
65  "A random variable used to assign the average pitch.",
66  StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"),
68  MakePointerChecker<RandomVariableStream> ())
69  .AddAttribute ("NormalVelocity",
70  "A gaussian random variable used to calculate the next velocity value.",
71  StringValue ("ns3::NormalRandomVariable[Mean=0.0|Variance=1.0|Bound=10.0]"), // Defaults to zero mean, and std dev = 1, and bound to +-10 of the mean
73  MakePointerChecker<NormalRandomVariable> ())
74  .AddAttribute ("NormalDirection",
75  "A gaussian random variable used to calculate the next direction value.",
76  StringValue ("ns3::NormalRandomVariable[Mean=0.0|Variance=1.0|Bound=10.0]"),
78  MakePointerChecker<NormalRandomVariable> ())
79  .AddAttribute ("NormalPitch",
80  "A gaussian random variable used to calculate the next pitch value.",
81  StringValue ("ns3::NormalRandomVariable[Mean=0.0|Variance=1.0|Bound=10.0]"),
83  MakePointerChecker<NormalRandomVariable> ());
84 
85  return tid;
86 }
87 
89 {
90  m_meanVelocity = 0.0;
91  m_meanDirection = 0.0;
92  m_meanPitch = 0.0;
94  m_helper.Unpause ();
95 }
96 
97 void
99 {
100  if (m_meanVelocity == 0.0)
101  {
102  //Initialize the mean velocity, direction, and pitch variables
106  double cosD = std::cos (m_meanDirection);
107  double cosP = std::cos (m_meanPitch);
108  double sinD = std::sin (m_meanDirection);
109  double sinP = std::sin (m_meanPitch);
110  //Initialize the starting velocity, direction, and pitch to be identical to the mean ones
114  //Set the velocity vector to give to the constant velocity helper
115  m_helper.SetVelocity (Vector (m_Velocity*cosD*cosP, m_Velocity*sinD*cosP, m_Velocity*sinP));
116  }
117  m_helper.Update ();
118 
119  //Get the next values from the gaussian distributions for velocity, direction, and pitch
120  double rv = m_normalVelocity->GetValue ();
121  double rd = m_normalDirection->GetValue ();
122  double rp = m_normalPitch->GetValue ();
123 
124  //Calculate the NEW velocity, direction, and pitch values using the Gauss-Markov formula:
125  //newVal = alpha*oldVal + (1-alpha)*meanVal + sqrt(1-alpha^2)*rv
126  //where rv is a random number from a normal (gaussian) distribution
127  double one_minus_alpha = 1 - m_alpha;
128  double sqrt_alpha = std::sqrt (1 - m_alpha*m_alpha);
129  m_Velocity = m_alpha * m_Velocity + one_minus_alpha * m_meanVelocity + sqrt_alpha * rv;
130  m_Direction = m_alpha * m_Direction + one_minus_alpha * m_meanDirection + sqrt_alpha * rd;
131  m_Pitch = m_alpha * m_Pitch + one_minus_alpha * m_meanPitch + sqrt_alpha * rp;
132 
133  //Calculate the linear velocity vector to give to the constant velocity helper
134  double cosDir = std::cos (m_Direction);
135  double cosPit = std::cos (m_Pitch);
136  double sinDir = std::sin (m_Direction);
137  double sinPit = std::sin (m_Pitch);
138  double vx = m_Velocity * cosDir * cosPit;
139  double vy = m_Velocity * sinDir * cosPit;
140  double vz = m_Velocity * sinPit;
141  m_helper.SetVelocity (Vector (vx, vy, vz));
142 
143  m_helper.Unpause ();
144 
145  DoWalk (m_timeStep);
146 }
147 
148 void
150 {
152  Vector position = m_helper.GetCurrentPosition ();
153  Vector speed = m_helper.GetVelocity ();
154  Vector nextPosition = position;
155  nextPosition.x += speed.x * delayLeft.GetSeconds ();
156  nextPosition.y += speed.y * delayLeft.GetSeconds ();
157  nextPosition.z += speed.z * delayLeft.GetSeconds ();
158  if (delayLeft.GetSeconds () < 0.0) delayLeft = Seconds (1.0);
159 
160  // Make sure that the position by the next time step is still within the boundary.
161  // If out of bounds, then alter the velocity vector and average direction to keep the position in bounds
162  if (m_bounds.IsInside (nextPosition))
163  {
165  }
166  else
167  {
168  if (nextPosition.x > m_bounds.xMax || nextPosition.x < m_bounds.xMin)
169  {
170  speed.x = -speed.x;
172  }
173 
174  if (nextPosition.y > m_bounds.yMax || nextPosition.y < m_bounds.yMin)
175  {
176  speed.y = -speed.y;
178  }
179 
180  if (nextPosition.z > m_bounds.zMax || nextPosition.z < m_bounds.zMin)
181  {
182  speed.z = -speed.z;
184  }
185 
188  m_helper.SetVelocity (speed);
189  m_helper.Unpause ();
191  }
193 }
194 
195 void
197 {
198  // chain up
200 }
201 
202 Vector
204 {
205  m_helper.Update ();
206  return m_helper.GetCurrentPosition ();
207 }
208 void
210 {
211  m_helper.SetPosition (position);
212  m_event.Cancel ();
214 }
215 Vector
217 {
218  return m_helper.GetVelocity ();
219 }
220 
221 int64_t
223 {
224  m_rndMeanVelocity->SetStream (stream);
225  m_normalVelocity->SetStream (stream + 1);
226  m_rndMeanDirection->SetStream (stream + 2);
227  m_normalDirection->SetStream (stream + 3);
228  m_rndMeanPitch->SetStream (stream + 4);
229  m_normalPitch->SetStream (stream + 5);
230  return 6;
231 }
232 
233 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3::ConstantVelocityHelper::SetVelocity
void SetVelocity(const Vector &vel)
Set new velocity vector.
Definition: constant-velocity-helper.cc:72
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
ns3::GaussMarkovMobilityModel::DoWalk
void DoWalk(Time timeLeft)
Perform a walk operation.
Definition: gauss-markov-mobility-model.cc:149
ns3::GaussMarkovMobilityModel::GaussMarkovMobilityModel
GaussMarkovMobilityModel()
Definition: gauss-markov-mobility-model.cc:88
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Box::zMin
double zMin
The z coordinate of the down bound of the box.
Definition: box.h:118
ns3::GaussMarkovMobilityModel::m_rndMeanDirection
Ptr< RandomVariableStream > m_rndMeanDirection
rv used to assign avg direction
Definition: gauss-markov-mobility-model.h:115
position-allocator.h
ns3::Box::xMin
double xMin
The x coordinate of the left bound of the box.
Definition: box.h:110
ns3::MobilityModel::NotifyCourseChange
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
Definition: mobility-model.cc:108
ns3::GaussMarkovMobilityModel::DoAssignStreams
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
Definition: gauss-markov-mobility-model.cc:222
ns3::GaussMarkovMobilityModel::m_timeStep
Time m_timeStep
duraiton after which direction and speed should change
Definition: gauss-markov-mobility-model.h:105
ns3::GaussMarkovMobilityModel::m_meanDirection
double m_meanDirection
current mean direction
Definition: gauss-markov-mobility-model.h:108
ns3::MakeBoxChecker
Ptr< const AttributeChecker > MakeBoxChecker(void)
Definition: box.cc:207
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::GaussMarkovMobilityModel::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: gauss-markov-mobility-model.cc:196
ns3::Box::yMin
double yMin
The y coordinate of the bottom bound of the box.
Definition: box.h:114
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::GaussMarkovMobilityModel::m_meanPitch
double m_meanPitch
current mean pitch
Definition: gauss-markov-mobility-model.h:109
ns3::GaussMarkovMobilityModel::m_rndMeanVelocity
Ptr< RandomVariableStream > m_rndMeanVelocity
rv used to assign avg velocity
Definition: gauss-markov-mobility-model.h:113
ns3::GaussMarkovMobilityModel::m_rndMeanPitch
Ptr< RandomVariableStream > m_rndMeanPitch
rv used to assign avg.
Definition: gauss-markov-mobility-model.h:117
ns3::GaussMarkovMobilityModel::DoGetPosition
virtual Vector DoGetPosition(void) const
Definition: gauss-markov-mobility-model.cc:203
ns3::ConstantVelocityHelper::GetCurrentPosition
Vector GetCurrentPosition(void) const
Get current position vector.
Definition: constant-velocity-helper.cc:59
ns3::GaussMarkovMobilityModel
Gauss-Markov mobility model.
Definition: gauss-markov-mobility-model.h:81
ns3::GaussMarkovMobilityModel::DoSetPosition
virtual void DoSetPosition(const Vector &position)
Definition: gauss-markov-mobility-model.cc:209
gauss-markov-mobility-model.h
ns3::EventId::Cancel
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::GaussMarkovMobilityModel::DoGetVelocity
virtual Vector DoGetVelocity(void) const
Definition: gauss-markov-mobility-model.cc:216
ns3::GaussMarkovMobilityModel::m_normalDirection
Ptr< NormalRandomVariable > m_normalDirection
Gaussian rv for next direction value.
Definition: gauss-markov-mobility-model.h:116
ns3::ConstantVelocityHelper::Unpause
void Unpause(void)
Resume mobility from current position at current velocity.
Definition: constant-velocity-helper.cc:129
ns3::GaussMarkovMobilityModel::m_meanVelocity
double m_meanVelocity
current mean velocity
Definition: gauss-markov-mobility-model.h:107
ns3::Box::zMax
double zMax
The z coordinate of the up bound of the box.
Definition: box.h:120
ns3::GaussMarkovMobilityModel::GetTypeId
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Definition: gauss-markov-mobility-model.cc:33
ns3::MakePointerAccessor
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: pointer.h:227
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::GaussMarkovMobilityModel::m_bounds
Box m_bounds
bounding box
Definition: gauss-markov-mobility-model.h:120
ns3::MakeDoubleAccessor
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: double.h:42
ns3::GaussMarkovMobilityModel::m_alpha
double m_alpha
tunable constant in the model
Definition: gauss-markov-mobility-model.h:106
ns3::GaussMarkovMobilityModel::m_normalVelocity
Ptr< NormalRandomVariable > m_normalVelocity
Gaussian rv used to for next velocity.
Definition: gauss-markov-mobility-model.h:114
ns3::RandomVariableStream::GetValue
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
ns3::ConstantVelocityHelper::UpdateWithBounds
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
Definition: constant-velocity-helper.cc:98
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::ConstantVelocityHelper::Update
void Update(void) const
Update position, if not paused, from last position and time of last update.
Definition: constant-velocity-helper.cc:80
ns3::GaussMarkovMobilityModel::m_Velocity
double m_Velocity
current velocity
Definition: gauss-markov-mobility-model.h:110
ns3::Box
a 3d box
Definition: box.h:35
ns3::BoxValue
AttributeValue implementation for Box.
Definition: box.h:126
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::ConstantVelocityHelper::SetPosition
void SetPosition(const Vector &position)
Set position vector.
Definition: constant-velocity-helper.cc:50
ns3::MobilityModel
Keep track of the current position and velocity of an object.
Definition: mobility-model.h:40
ns3::RandomVariableStream::SetStream
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Definition: random-variable-stream.cc:100
ns3::GaussMarkovMobilityModel::m_Pitch
double m_Pitch
current pitch
Definition: gauss-markov-mobility-model.h:112
ns3::GaussMarkovMobilityModel::m_normalPitch
Ptr< NormalRandomVariable > m_normalPitch
Gaussian rv for next pitch.
Definition: gauss-markov-mobility-model.h:118
ns3::Box::IsInside
bool IsInside(const Vector &position) const
Definition: box.cc:54
ns3::GaussMarkovMobilityModel::m_event
EventId m_event
event id of scheduled start
Definition: gauss-markov-mobility-model.h:119
ns3::GaussMarkovMobilityModel::m_Direction
double m_Direction
current direction
Definition: gauss-markov-mobility-model.h:111
ns3::Box::xMax
double xMax
The x coordinate of the right bound of the box.
Definition: box.h:112
ns3::GaussMarkovMobilityModel::Start
void Start(void)
Initialize the model and calculate new velocity, direction, and pitch.
Definition: gauss-markov-mobility-model.cc:98
ns3::Object::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
ns3::Simulator::ScheduleNow
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
ns3::MakeTimeAccessor
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1354
ns3::ConstantVelocityHelper::GetVelocity
Vector GetVelocity(void) const
Get velocity; if paused, will return a zero vector.
Definition: constant-velocity-helper.cc:66
ns3::MakeBoxAccessor
Ptr< const AttributeAccessor > MakeBoxAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: box.h:126
ns3::GaussMarkovMobilityModel::m_helper
ConstantVelocityHelper m_helper
constant velocity helper
Definition: gauss-markov-mobility-model.h:104
ns3::Box::yMax
double yMax
The y coordinate of the top bound of the box.
Definition: box.h:116