A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::GaussMarkovMobilityModel")
38  .SetGroupName ("Mobility")
39  .AddConstructor<GaussMarkovMobilityModel> ()
40  .AddAttribute ("Bounds",
41  "Bounds of the area to cruise.",
42  BoxValue (Box (-100.0, 100.0, -100.0, 100.0, 0.0, 100.0)),
43  MakeBoxAccessor (&GaussMarkovMobilityModel::m_bounds),
44  MakeBoxChecker ())
45  .AddAttribute ("TimeStep",
46  "Change current direction and speed after moving for this time.",
47  TimeValue (Seconds (1.0)),
48  MakeTimeAccessor (&GaussMarkovMobilityModel::m_timeStep),
49  MakeTimeChecker ())
50  .AddAttribute ("Alpha",
51  "A constant representing the tunable parameter in the Gauss-Markov model.",
52  DoubleValue (1.0),
53  MakeDoubleAccessor (&GaussMarkovMobilityModel::m_alpha),
54  MakeDoubleChecker<double> ())
55  .AddAttribute ("MeanVelocity",
56  "A random variable used to assign the average velocity.",
57  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
58  MakePointerAccessor (&GaussMarkovMobilityModel::m_rndMeanVelocity),
59  MakePointerChecker<RandomVariableStream> ())
60  .AddAttribute ("MeanDirection",
61  "A random variable used to assign the average direction.",
62  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.283185307]"),
63  MakePointerAccessor (&GaussMarkovMobilityModel::m_rndMeanDirection),
64  MakePointerChecker<RandomVariableStream> ())
65  .AddAttribute ("MeanPitch",
66  "A random variable used to assign the average pitch.",
67  StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"),
68  MakePointerAccessor (&GaussMarkovMobilityModel::m_rndMeanPitch),
69  MakePointerChecker<RandomVariableStream> ())
70  .AddAttribute ("NormalVelocity",
71  "A gaussian random variable used to calculate the next velocity value.",
72  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  MakePointerAccessor (&GaussMarkovMobilityModel::m_normalVelocity),
74  MakePointerChecker<NormalRandomVariable> ())
75  .AddAttribute ("NormalDirection",
76  "A gaussian random variable used to calculate the next direction value.",
77  StringValue ("ns3::NormalRandomVariable[Mean=0.0|Variance=1.0|Bound=10.0]"),
78  MakePointerAccessor (&GaussMarkovMobilityModel::m_normalDirection),
79  MakePointerChecker<NormalRandomVariable> ())
80  .AddAttribute ("NormalPitch",
81  "A gaussian random variable used to calculate the next pitch value.",
82  StringValue ("ns3::NormalRandomVariable[Mean=0.0|Variance=1.0|Bound=10.0]"),
83  MakePointerAccessor (&GaussMarkovMobilityModel::m_normalPitch),
84  MakePointerChecker<NormalRandomVariable> ());
85 
86  return tid;
87 }
88 
90 {
91  m_meanVelocity = 0.0;
92  m_meanDirection = 0.0;
93  m_meanPitch = 0.0;
95  m_helper.Unpause ();
96 }
97 
98 void
100 {
101  if (m_meanVelocity == 0.0)
102  {
103  //Initialize the mean velocity, direction, and pitch variables
107  double cosD = std::cos (m_meanDirection);
108  double cosP = std::cos (m_meanPitch);
109  double sinD = std::sin (m_meanDirection);
110  double sinP = std::sin (m_meanPitch);
111  //Initialize the starting velocity, direction, and pitch to be identical to the mean ones
115  //Set the velocity vector to give to the constant velocity helper
116  m_helper.SetVelocity (Vector (m_Velocity*cosD*cosP, m_Velocity*sinD*cosP, m_Velocity*sinP));
117  }
118  m_helper.Update ();
119 
120  //Get the next values from the gaussian distributions for velocity, direction, and pitch
121  double rv = m_normalVelocity->GetValue ();
122  double rd = m_normalDirection->GetValue ();
123  double rp = m_normalPitch->GetValue ();
124 
125  //Calculate the NEW velocity, direction, and pitch values using the Gauss-Markov formula:
126  //newVal = alpha*oldVal + (1-alpha)*meanVal + sqrt(1-alpha^2)*rv
127  //where rv is a random number from a normal (gaussian) distribution
128  double one_minus_alpha = 1 - m_alpha;
129  double sqrt_alpha = std::sqrt (1 - m_alpha*m_alpha);
130  m_Velocity = m_alpha * m_Velocity + one_minus_alpha * m_meanVelocity + sqrt_alpha * rv;
131  m_Direction = m_alpha * m_Direction + one_minus_alpha * m_meanDirection + sqrt_alpha * rd;
132  m_Pitch = m_alpha * m_Pitch + one_minus_alpha * m_meanPitch + sqrt_alpha * rp;
133 
134  //Calculate the linear velocity vector to give to the constant velocity helper
135  double cosDir = std::cos (m_Direction);
136  double cosPit = std::cos (m_Pitch);
137  double sinDir = std::sin (m_Direction);
138  double sinPit = std::sin (m_Pitch);
139  double vx = m_Velocity * cosDir * cosPit;
140  double vy = m_Velocity * sinDir * cosPit;
141  double vz = m_Velocity * sinPit;
142  m_helper.SetVelocity (Vector (vx, vy, vz));
143 
144  m_helper.Unpause ();
145 
146  DoWalk (m_timeStep);
147 }
148 
149 void
151 {
153  Vector position = m_helper.GetCurrentPosition ();
154  Vector speed = m_helper.GetVelocity ();
155  Vector nextPosition = position;
156  nextPosition.x += speed.x * delayLeft.GetSeconds ();
157  nextPosition.y += speed.y * delayLeft.GetSeconds ();
158  nextPosition.z += speed.z * delayLeft.GetSeconds ();
159  if (delayLeft.GetSeconds () < 0.0) delayLeft = Seconds (1.0);
160 
161  // Make sure that the position by the next time step is still within the boundary.
162  // If out of bounds, then alter the velocity vector and average direction to keep the position in bounds
163  if (m_bounds.IsInside (nextPosition))
164  {
166  }
167  else
168  {
169  if (nextPosition.x > m_bounds.xMax || nextPosition.x < m_bounds.xMin)
170  {
171  speed.x = -speed.x;
172  m_meanDirection = 3.14159265 - m_meanDirection;
173  }
174 
175  if (nextPosition.y > m_bounds.yMax || nextPosition.y < m_bounds.yMin)
176  {
177  speed.y = -speed.y;
179  }
180 
181  if (nextPosition.z > m_bounds.zMax || nextPosition.z < m_bounds.zMin)
182  {
183  speed.z = -speed.z;
185  }
186 
189  m_helper.SetVelocity (speed);
190  m_helper.Unpause ();
192  }
194 }
195 
196 void
198 {
199  // chain up
201 }
202 
203 Vector
205 {
206  m_helper.Update ();
207  return m_helper.GetCurrentPosition ();
208 }
209 void
211 {
212  m_helper.SetPosition (position);
215 }
216 Vector
218 {
219  return m_helper.GetVelocity ();
220 }
221 
222 int64_t
224 {
225  m_rndMeanVelocity->SetStream (stream);
226  m_normalVelocity->SetStream (stream + 1);
227  m_rndMeanDirection->SetStream (stream + 2);
228  m_normalDirection->SetStream (stream + 3);
229  m_rndMeanPitch->SetStream (stream + 4);
230  m_normalPitch->SetStream (stream + 5);
231  return 6;
232 }
233 
234 } // namespace ns3
double x
x coordinate of vector
Definition: vector.h:49
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
double zMin
Definition: box.h:97
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
hold variables of type string
Definition: string.h:19
Ptr< RandomVariableStream > m_rndMeanDirection
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
double xMax
Definition: box.h:91
a 3d vector
Definition: vector.h:31
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
a 3d box
Definition: box.h:33
double yMax
Definition: box.h:95
Keep track of the current position and velocity of an object.
double GetSeconds(void) const
Definition: nstime.h:274
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
Ptr< RandomVariableStream > m_rndMeanVelocity
virtual void DoSetPosition(const Vector &position)
hold objects of type ns3::Time
Definition: nstime.h:961
virtual Vector DoGetVelocity(void) const
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
hold objects of type ns3::Box
double yMin
Definition: box.h:93
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
Ptr< RandomVariableStream > m_rndMeanPitch
virtual Vector DoGetPosition(void) const
Ptr< NormalRandomVariable > m_normalDirection
void SetVelocity(const Vector &vel)
double y
y coordinate of vector
Definition: vector.h:53
double zMax
Definition: box.h:99
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
void UpdateWithBounds(const Rectangle &rectangle) const
Gauss-Markov mobility model.
Ptr< NormalRandomVariable > m_normalVelocity
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
Ptr< NormalRandomVariable > m_normalPitch
bool IsInside(const Vector &position) const
Definition: box.cc:54
void SetPosition(const Vector &position)
Hold a floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
double xMin
Definition: box.h:89
double z
z coordinate of vector
Definition: vector.h:57