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
ConstantVelocityHelper m_helper
constant velocity helper
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
double zMin
The z coordinate of the down bound of the box.
Definition: box.h:118
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Time m_timeStep
duraiton after which direction and speed should change
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Hold variables of type string.
Definition: string.h:41
Ptr< const AttributeChecker > MakeBoxChecker(void)
Definition: box.cc:207
Vector GetCurrentPosition(void) const
Get current position vector.
EventId m_event
event id of scheduled start
Ptr< RandomVariableStream > m_rndMeanDirection
rv used to assign avg direction
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
void DoWalk(Time timeLeft)
Perform a walk operation.
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
double xMax
The x coordinate of the right bound of the box.
Definition: box.h:112
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
a 3d box
Definition: box.h:34
double yMax
The y coordinate of the top bound of the box.
Definition: box.h:116
Keep track of the current position and velocity of an object.
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
Ptr< RandomVariableStream > m_rndMeanVelocity
rv used to assign avg velocity
virtual void DoSetPosition(const Vector &position)
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
void Update(void) const
Update position, if not paused, from last position and time of last update.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:588
AttributeValue implementation for Time.
Definition: nstime.h:1353
double m_alpha
tunable constant in the model
AttributeValue implementation for Box.
Definition: box.h:126
double yMin
The y coordinate of the bottom bound of the box.
Definition: box.h:114
virtual void DoDispose(void)
Destructor implementation.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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
Ptr< RandomVariableStream > m_rndMeanPitch
rv used to assign avg.
Ptr< NormalRandomVariable > m_normalDirection
Gaussian rv for next direction value.
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
double m_meanVelocity
current mean velocity
virtual Vector DoGetPosition(void) const
void SetVelocity(const Vector &vel)
Set new velocity vector.
double zMax
The z coordinate of the up bound of the box.
Definition: box.h:120
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
void Start(void)
Initialize the model and calculate new velocity, direction, and pitch.
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
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
virtual Vector DoGetVelocity(void) const
Gauss-Markov mobility model.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Vector GetVelocity(void) const
Get velocity; if paused, will return a zero vector.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
void Unpause(void)
Resume mobility from current position at current velocity.
Ptr< NormalRandomVariable > m_normalVelocity
Gaussian rv used to for next velocity.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
Ptr< NormalRandomVariable > m_normalPitch
Gaussian rv for next pitch.
void SetPosition(const Vector &position)
Set position vector.
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:58
bool IsInside(const Vector &position) const
Definition: box.cc:54
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
double m_meanDirection
current mean direction
double xMin
The x coordinate of the left bound of the box.
Definition: box.h:110