A Discrete-Event Network Simulator
API
steady-state-random-waypoint-mobility-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 IITP RAS
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Denis Fakhriev <fakhriev@iitp.ru>
18  */
20 
21 #include "ns3/double.h"
22 #include "ns3/simulator.h"
23 #include "ns3/test.h"
24 
25 #include <cmath>
26 
27 namespace ns3
28 {
29 
30 NS_OBJECT_ENSURE_REGISTERED(SteadyStateRandomWaypointMobilityModel);
31 
32 TypeId
34 {
35  static TypeId tid =
36  TypeId("ns3::SteadyStateRandomWaypointMobilityModel")
38  .SetGroupName("Mobility")
40  .AddAttribute("MinSpeed",
41  "Minimum speed value, [m/s]",
42  DoubleValue(0.3),
44  MakeDoubleChecker<double>())
45  .AddAttribute("MaxSpeed",
46  "Maximum speed value, [m/s]",
47  DoubleValue(0.7),
49  MakeDoubleChecker<double>())
50  .AddAttribute("MinPause",
51  "Minimum pause value, [s]",
52  DoubleValue(0.0),
54  MakeDoubleChecker<double>())
55  .AddAttribute("MaxPause",
56  "Maximum pause value, [s]",
57  DoubleValue(0.0),
59  MakeDoubleChecker<double>())
60  .AddAttribute("MinX",
61  "Minimum X value of traveling region, [m]",
62  DoubleValue(1),
64  MakeDoubleChecker<double>())
65  .AddAttribute("MaxX",
66  "Maximum X value of traveling region, [m]",
67  DoubleValue(1),
69  MakeDoubleChecker<double>())
70  .AddAttribute("MinY",
71  "Minimum Y value of traveling region, [m]",
72  DoubleValue(1),
74  MakeDoubleChecker<double>())
75  .AddAttribute("MaxY",
76  "Maximum Y value of traveling region, [m]",
77  DoubleValue(1),
79  MakeDoubleChecker<double>())
80  .AddAttribute("Z",
81  "Z value of traveling region (fixed), [m]",
82  DoubleValue(0.0),
84  MakeDoubleChecker<double>());
85 
86  return tid;
87 }
88 
90  : alreadyStarted(false)
91 {
92  m_speed = CreateObject<UniformRandomVariable>();
93  m_pause = CreateObject<UniformRandomVariable>();
94  m_x1_r = CreateObject<UniformRandomVariable>();
95  m_y1_r = CreateObject<UniformRandomVariable>();
96  m_x2_r = CreateObject<UniformRandomVariable>();
97  m_y2_r = CreateObject<UniformRandomVariable>();
98  m_u_r = CreateObject<UniformRandomVariable>();
99  m_x = CreateObject<UniformRandomVariable>();
100  m_y = CreateObject<UniformRandomVariable>();
101  m_position = CreateObject<RandomBoxPositionAllocator>();
102 }
103 
104 void
106 {
109 }
110 
111 void
113 {
114  alreadyStarted = true;
115  // Configure random variables based on attributes
116  NS_ASSERT(m_minSpeed >= 1e-6);
126  m_position->SetX(m_x);
127  m_position->SetY(m_y);
128  Ptr<ConstantRandomVariable> z = CreateObject<ConstantRandomVariable>();
129  z->SetAttribute("Constant", DoubleValue(m_z));
130  m_position->SetZ(z);
131 
135 
136  m_helper.Update();
137  m_helper.Pause();
138 
139  // calculate the steady-state probability that a node is initially paused
140  double expectedPauseTime = (m_minPause + m_maxPause) / 2;
141  double a = m_maxX - m_minX;
142  double b = m_maxY - m_minY;
143  double v0 = m_minSpeed;
144  double v1 = m_maxSpeed;
145  double log1 = b * b / a * std::log(std::sqrt((a * a) / (b * b) + 1) + a / b);
146  double log2 = a * a / b * std::log(std::sqrt((b * b) / (a * a) + 1) + b / a);
147  double expectedTravelTime = 1.0 / 6.0 * (log1 + log2);
148  expectedTravelTime +=
149  1.0 / 15.0 * ((a * a * a) / (b * b) + (b * b * b) / (a * a)) -
150  1.0 / 15.0 * std::sqrt(a * a + b * b) * ((a * a) / (b * b) + (b * b) / (a * a) - 3);
151  if (v0 == v1)
152  {
153  expectedTravelTime /= v0;
154  }
155  else
156  {
157  expectedTravelTime *= std::log(v1 / v0) / (v1 - v0);
158  }
159  double probabilityPaused = expectedPauseTime / (expectedPauseTime + expectedTravelTime);
160  NS_ASSERT(probabilityPaused >= 0 && probabilityPaused <= 1);
161 
162  double u = m_u_r->GetValue(0, 1);
163  if (u < probabilityPaused) // node initially paused
164  {
165  m_helper.SetPosition(m_position->GetNext());
166  u = m_u_r->GetValue(0, 1);
167  Time pause;
168  if (m_minPause != m_maxPause)
169  {
170  if (u < (2 * m_minPause / (m_minPause + m_maxPause)))
171  {
172  pause = Seconds(u * (m_minPause + m_maxPause) / 2);
173  }
174  else
175  {
176  // there is an error in equation 20 in the Tech. Report MCS-03-04
177  // this error is corrected in the TMC 2004 paper and below
178  pause = Seconds(m_maxPause - std::sqrt((1 - u) * (m_maxPause * m_maxPause -
179  m_minPause * m_minPause)));
180  }
181  }
182  else // if pause is constant
183  {
184  pause = Seconds(u * expectedPauseTime);
185  }
187  m_event =
189  }
190  else // node initially moving
191  {
192  double x1;
193  double x2;
194  double y1;
195  double y2;
196  x1 = x2 = y1 = y2 = 0;
197  double r = 0;
198  double u1 = 1;
199  while (u1 >= r)
200  {
201  x1 = m_x1_r->GetValue(0, a);
202  y1 = m_y1_r->GetValue(0, b);
203  x2 = m_x2_r->GetValue(0, a);
204  y2 = m_y2_r->GetValue(0, b);
205  u1 = m_u_r->GetValue(0, 1);
206  r = std::sqrt(((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) / (a * a + b * b));
207  NS_ASSERT(r <= 1);
208  }
209  double u2 = m_u_r->GetValue(0, 1);
211  Vector(m_minX + u2 * x1 + (1 - u2) * x2, m_minY + u2 * y1 + (1 - u2) * y2, m_z));
213  m_event =
215  this,
216  Vector(m_minX + x2, m_minY + y2, m_z));
217  }
219 }
220 
221 void
223 {
224  m_helper.Update();
225  Vector m_current = m_helper.GetCurrentPosition();
226  NS_ASSERT(m_minX <= m_current.x && m_current.x <= m_maxX);
227  NS_ASSERT(m_minY <= m_current.y && m_current.y <= m_maxY);
228  NS_ASSERT(m_minX <= destination.x && destination.x <= m_maxX);
229  NS_ASSERT(m_minY <= destination.y && destination.y <= m_maxY);
230  double u = m_u_r->GetValue(0, 1);
231  double speed = std::pow(m_maxSpeed, u) / std::pow(m_minSpeed, u - 1);
232  double dx = (destination.x - m_current.x);
233  double dy = (destination.y - m_current.y);
234  double dz = (destination.z - m_current.z);
235  double k = speed / std::sqrt(dx * dx + dy * dy + dz * dz);
236 
237  m_helper.SetVelocity(Vector(k * dx, k * dy, k * dz));
238  m_helper.Unpause();
239  Time travelDelay = Seconds(CalculateDistance(destination, m_current) / speed);
240  m_event =
243 }
244 
245 void
247 {
248  m_helper.Update();
249  Vector m_current = m_helper.GetCurrentPosition();
250  NS_ASSERT(m_minX <= m_current.x && m_current.x <= m_maxX);
251  NS_ASSERT(m_minY <= m_current.y && m_current.y <= m_maxY);
252  Vector destination = m_position->GetNext();
253  double speed = m_speed->GetValue();
254  double dx = (destination.x - m_current.x);
255  double dy = (destination.y - m_current.y);
256  double dz = (destination.z - m_current.z);
257  double k = speed / std::sqrt(dx * dx + dy * dy + dz * dz);
258 
259  m_helper.SetVelocity(Vector(k * dx, k * dy, k * dz));
260  m_helper.Unpause();
261  Time travelDelay = Seconds(CalculateDistance(destination, m_current) / speed);
262  m_event =
265 }
266 
267 void
269 {
270  m_helper.Update();
271  m_helper.Pause();
272  Time pause = Seconds(m_pause->GetValue());
275 }
276 
277 Vector
279 {
280  m_helper.Update();
281  return m_helper.GetCurrentPosition();
282 }
283 
284 void
286 {
287  if (alreadyStarted)
288  {
289  m_helper.SetPosition(position);
290  m_event.Cancel();
292  }
293 }
294 
295 Vector
297 {
298  return m_helper.GetVelocity();
299 }
300 
301 int64_t
303 {
304  int64_t positionStreamsAllocated = 0;
305  m_speed->SetStream(stream);
306  m_pause->SetStream(stream + 1);
307  m_x1_r->SetStream(stream + 2);
308  m_y1_r->SetStream(stream + 3);
309  m_x2_r->SetStream(stream + 4);
310  m_y2_r->SetStream(stream + 5);
311  m_u_r->SetStream(stream + 6);
312  m_x->SetStream(stream + 7);
313  m_y->SetStream(stream + 8);
314  positionStreamsAllocated = m_position->AssignStreams(stream + 9);
315  return (9 + positionStreamsAllocated);
316 }
317 
318 } // namespace ns3
Vector GetCurrentPosition() const
Get current position vector.
Vector GetVelocity() const
Get velocity; if paused, will return a zero vector.
void Update() const
Update position, if not paused, from last position and time of last update.
void Unpause()
Resume mobility from current position at current velocity.
void SetPosition(const Vector &position)
Set position vector.
void SetVelocity(const Vector &vel)
Set new velocity vector.
void Pause()
Pause mobility at current position.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
Keep track of the current position and velocity of an object.
void NotifyCourseChange() const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:204
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:359
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
Ptr< UniformRandomVariable > m_u_r
rv used in step 5 of algorithm
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
Ptr< UniformRandomVariable > m_y2_r
rv used in rejection sampling phase
Ptr< UniformRandomVariable > m_pause
random variable for pause values
Ptr< UniformRandomVariable > m_x2_r
rv used in rejection sampling phase
void SteadyStateBeginWalk(const Vector &destination)
Use provided destination to calculate travel delay, and schedule a Start() event at that time.
Ptr< UniformRandomVariable > m_y
rv used for position allocator
Ptr< UniformRandomVariable > m_speed
random variable for speed values
static TypeId GetTypeId()
Register this type with the TypeId system.
Ptr< UniformRandomVariable > m_x1_r
rv used in rejection sampling phase
void DoInitializePrivate()
Configure random variables based on attributes; calculate the steady state probability that node is i...
void BeginWalk()
Start a motion period and schedule the ending of the motion.
void Start()
Start a pause period and schedule the ending of the pause.
Ptr< UniformRandomVariable > m_x
rv used for position allocator
ConstantVelocityHelper m_helper
helper for velocity computations
Ptr< UniformRandomVariable > m_y1_r
rv used in rejection sampling phase
Ptr< RandomBoxPositionAllocator > m_position
position allocator
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:109