A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
steady-state-random-waypoint-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 IITP RAS
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: Denis Fakhriev <fakhriev@iitp.ru>
19  */
20 #include <cmath>
21 #include "ns3/simulator.h"
22 #include "ns3/double.h"
24 #include "ns3/test.h"
25 
26 namespace ns3 {
27 
28 NS_OBJECT_ENSURE_REGISTERED (SteadyStateRandomWaypointMobilityModel);
29 
30 TypeId
32 {
33  static TypeId tid = TypeId ("ns3::SteadyStateRandomWaypointMobilityModel")
35  .SetGroupName ("Mobility")
36  .AddConstructor<SteadyStateRandomWaypointMobilityModel> ()
37  .AddAttribute ("MinSpeed",
38  "Minimum speed value, [m/s]",
39  DoubleValue (0.3),
41  MakeDoubleChecker<double> ())
42  .AddAttribute ("MaxSpeed",
43  "Maximum speed value, [m/s]",
44  DoubleValue (0.7),
46  MakeDoubleChecker<double> ())
47  .AddAttribute ("MinPause",
48  "Minimum pause value, [s]",
49  DoubleValue (0.0),
51  MakeDoubleChecker<double> ())
52  .AddAttribute ("MaxPause",
53  "Maximum pause value, [s]",
54  DoubleValue (0.0),
56  MakeDoubleChecker<double> ())
57  .AddAttribute ("MinX",
58  "Minimum X value of traveling region, [m]",
59  DoubleValue (1),
61  MakeDoubleChecker<double> ())
62  .AddAttribute ("MaxX",
63  "Maximum X value of traveling region, [m]",
64  DoubleValue (1),
66  MakeDoubleChecker<double> ())
67  .AddAttribute ("MinY",
68  "Minimum Y value of traveling region, [m]",
69  DoubleValue (1),
71  MakeDoubleChecker<double> ())
72  .AddAttribute ("MaxY",
73  "Maximum Y value of traveling region, [m]",
74  DoubleValue (1),
76  MakeDoubleChecker<double> ())
77  .AddAttribute ("Z",
78  "Z value of traveling region (fixed), [m]",
79  DoubleValue (0.0),
80  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_z),
81  MakeDoubleChecker<double> ());
82 
83  return tid;
84 }
85 
87  alreadyStarted (false)
88 {
89  m_speed = CreateObject<UniformRandomVariable> ();
90  m_pause = CreateObject<UniformRandomVariable> ();
91  m_x1_r = CreateObject<UniformRandomVariable> ();
92  m_y1_r = CreateObject<UniformRandomVariable> ();
93  m_x2_r = CreateObject<UniformRandomVariable> ();
94  m_y2_r = CreateObject<UniformRandomVariable> ();
95  m_u_r = CreateObject<UniformRandomVariable> ();
96  m_x = CreateObject<UniformRandomVariable> ();
97  m_y = CreateObject<UniformRandomVariable> ();
98 }
99 
100 void
102 {
105 }
106 
107 void
109 {
110  alreadyStarted = true;
111  // Configure random variables based on attributes
112  NS_ASSERT (m_minSpeed >= 1e-6);
116  NS_ASSERT (m_minX < m_maxX);
117  NS_ASSERT (m_minY < m_maxY);
118  m_position = CreateObject<RandomRectanglePositionAllocator> ();
119  m_x->SetAttribute ("Min", DoubleValue (m_minX));
120  m_x->SetAttribute ("Max", DoubleValue (m_maxX));
121  m_y->SetAttribute ("Min", DoubleValue (m_minY));
122  m_y->SetAttribute ("Max", DoubleValue (m_maxY));
123  m_position->SetX (m_x);
124  m_position->SetY (m_y);
128 
129  m_helper.Update ();
130  m_helper.Pause ();
131 
132  // calculate the steady-state probability that a node is initially paused
133  double expectedPauseTime = (m_minPause + m_maxPause)/2;
134  double a = m_maxX - m_minX;
135  double b = m_maxY - m_minY;
136  double v0 = m_minSpeed;
137  double v1 = m_maxSpeed;
138  double log1 = b*b / a*std::log (std::sqrt ((a*a)/(b*b) + 1) + a/b);
139  double log2 = a*a / b*std::log (std::sqrt ((b*b)/(a*a) + 1) + b/a);
140  double expectedTravelTime = 1.0/6.0*(log1 + log2);
141  expectedTravelTime += 1.0/15.0*((a*a*a)/(b*b) + (b*b*b)/(a*a)) -
142  1.0/15.0*std::sqrt (a*a + b*b)*((a*a)/(b*b) + (b*b)/(a*a) - 3);
143  if (v0 == v1)
144  {
145  expectedTravelTime /= v0;
146  }
147  else
148  {
149  expectedTravelTime *= std::log (v1/v0)/(v1 - v0);
150  }
151  double probabilityPaused = expectedPauseTime/(expectedPauseTime + expectedTravelTime);
152  NS_ASSERT (probabilityPaused >= 0 && probabilityPaused <= 1);
153 
154  double u = m_u_r->GetValue (0, 1);
155  if (u < probabilityPaused) // node initially paused
156  {
157  m_helper.SetPosition (m_position->GetNext ());
158  u = m_u_r->GetValue (0, 1);
159  Time pause;
160  if (m_minPause != m_maxPause)
161  {
162  if (u < (2*m_minPause/(m_minPause + m_maxPause)))
163  {
164  pause = Seconds (u*(m_minPause + m_maxPause)/2);
165  }
166  else
167  {
168  // there is an error in equation 20 in the Tech. Report MCS-03-04
169  // this error is corrected in the TMC 2004 paper and below
170  pause = Seconds (m_maxPause - std::sqrt ((1 - u)*(m_maxPause*m_maxPause - m_minPause*m_minPause)));
171  }
172  }
173  else // if pause is constant
174  {
175  pause = Seconds (u*expectedPauseTime);
176  }
179  }
180  else // node initially moving
181  {
182  double x1, x2, y1, y2;
183  double r = 0;
184  double u1 = 1;
185  while (u1 >= r)
186  {
187  x1 = m_x1_r->GetValue (0, a);
188  y1 = m_y1_r->GetValue (0, b);
189  x2 = m_x2_r->GetValue (0, a);
190  y2 = m_y2_r->GetValue (0, b);
191  u1 = m_u_r->GetValue (0, 1);
192  r = std::sqrt (((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))/(a*a + b*b));
193  NS_ASSERT (r <= 1);
194  }
195  double u2 = m_u_r->GetValue (0, 1);
196  m_helper.SetPosition (Vector (m_minX + u2*x1 + (1 - u2)*x2, m_minY + u2*y1 + (1 - u2)*y2, m_z));
199  Vector (m_minX + x2, m_minY + y2, m_z));
200  }
202 }
203 
204 void
206 {
207  m_helper.Update ();
208  Vector m_current = m_helper.GetCurrentPosition ();
209  NS_ASSERT (m_minX <= m_current.x && m_current.x <= m_maxX);
210  NS_ASSERT (m_minY <= m_current.y && m_current.y <= m_maxY);
211  NS_ASSERT (m_minX <= destination.x && destination.x <= m_maxX);
212  NS_ASSERT (m_minY <= destination.y && destination.y <= m_maxY);
213  double u = m_u_r->GetValue (0, 1);
214  double speed = std::pow (m_maxSpeed, u)/std::pow (m_minSpeed, u - 1);
215  double dx = (destination.x - m_current.x);
216  double dy = (destination.y - m_current.y);
217  double dz = (destination.z - m_current.z);
218  double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
219 
220  m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
221  m_helper.Unpause ();
222  Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
223  m_event = Simulator::Schedule (travelDelay,
226 }
227 
228 void
230 {
231  m_helper.Update ();
232  Vector m_current = m_helper.GetCurrentPosition ();
233  NS_ASSERT (m_minX <= m_current.x && m_current.x <= m_maxX);
234  NS_ASSERT (m_minY <= m_current.y && m_current.y <= m_maxY);
235  Vector destination = m_position->GetNext ();
236  double speed = m_speed->GetValue ();
237  double dx = (destination.x - m_current.x);
238  double dy = (destination.y - m_current.y);
239  double dz = (destination.z - m_current.z);
240  double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
241 
242  m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
243  m_helper.Unpause ();
244  Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
245  m_event = Simulator::Schedule (travelDelay,
248 }
249 
250 void
252 {
253  m_helper.Update ();
254  m_helper.Pause ();
255  Time pause = Seconds (m_pause->GetValue ());
258 }
259 
260 Vector
262 {
263  m_helper.Update ();
264  return m_helper.GetCurrentPosition ();
265 }
266 void
268 {
269  if (alreadyStarted)
270  {
271  m_helper.SetPosition (position);
274  }
275 }
276 Vector
278 {
279  return m_helper.GetVelocity ();
280 }
281 int64_t
283 {
284  int64_t positionStreamsAllocated = 0;
285  m_speed->SetStream (stream);
286  m_pause->SetStream (stream + 1);
287  m_x1_r->SetStream (stream + 2);
288  m_y1_r->SetStream (stream + 3);
289  m_x2_r->SetStream (stream + 4);
290  m_y2_r->SetStream (stream + 5);
291  m_u_r->SetStream (stream + 6);
292  m_x->SetStream (stream + 7);
293  m_y->SetStream (stream + 8);
294  positionStreamsAllocated = m_position->AssignStreams (stream + 9);
295  return (9 + positionStreamsAllocated);
296 }
297 
298 } // namespace ns3
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
double x
x coordinate of vector
Definition: vector.h:49
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
bool IsRunning(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:59
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:825
a 3d vector
Definition: vector.h:31
Keep track of the current position and velocity of an object.
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:71
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
void SetVelocity(const Vector &vel)
double y
y coordinate of vector
Definition: vector.h:53
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:986
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
void SetPosition(const Vector &position)
Hold a floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:176
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:342
double z
z coordinate of vector
Definition: vector.h:57