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 
31 TypeId
33 {
34  static TypeId tid = TypeId ("ns3::SteadyStateRandomWaypointMobilityModel")
36  .SetGroupName ("Mobility")
37  .AddConstructor<SteadyStateRandomWaypointMobilityModel> ()
38  .AddAttribute ("MinSpeed",
39  "Minimum speed value, [m/s]",
40  DoubleValue (0.3),
42  MakeDoubleChecker<double> ())
43  .AddAttribute ("MaxSpeed",
44  "Maximum speed value, [m/s]",
45  DoubleValue (0.7),
47  MakeDoubleChecker<double> ())
48  .AddAttribute ("MinPause",
49  "Minimum pause value, [s]",
50  DoubleValue (0.0),
52  MakeDoubleChecker<double> ())
53  .AddAttribute ("MaxPause",
54  "Maximum pause value, [s]",
55  DoubleValue (0.0),
57  MakeDoubleChecker<double> ())
58  .AddAttribute ("MinX",
59  "Minimum X value of traveling region, [m]",
60  DoubleValue (1),
62  MakeDoubleChecker<double> ())
63  .AddAttribute ("MaxX",
64  "Maximum X value of traveling region, [m]",
65  DoubleValue (1),
67  MakeDoubleChecker<double> ())
68  .AddAttribute ("MinY",
69  "Minimum Y value of traveling region, [m]",
70  DoubleValue (1),
72  MakeDoubleChecker<double> ())
73  .AddAttribute ("MaxY",
74  "Maximum Y value of traveling region, [m]",
75  DoubleValue (1),
77  MakeDoubleChecker<double> ())
78  .AddAttribute ("Z",
79  "Z value of traveling region (fixed), [m]",
80  DoubleValue (0.0),
81  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_z),
82  MakeDoubleChecker<double> ());
83 
84  return tid;
85 }
86 
88  alreadyStarted (false)
89 {
90  m_speed = CreateObject<UniformRandomVariable> ();
91  m_pause = CreateObject<UniformRandomVariable> ();
92  m_x1_r = CreateObject<UniformRandomVariable> ();
93  m_y1_r = CreateObject<UniformRandomVariable> ();
94  m_x2_r = CreateObject<UniformRandomVariable> ();
95  m_y2_r = CreateObject<UniformRandomVariable> ();
96  m_u_r = CreateObject<UniformRandomVariable> ();
97  m_x = CreateObject<UniformRandomVariable> ();
98  m_y = CreateObject<UniformRandomVariable> ();
99 }
100 
101 void
103 {
106 }
107 
108 void
110 {
111  alreadyStarted = true;
112  // Configure random variables based on attributes
113  NS_ASSERT (m_minSpeed >= 1e-6);
117  NS_ASSERT (m_minX < m_maxX);
118  NS_ASSERT (m_minY < m_maxY);
119  m_position = CreateObject<RandomRectanglePositionAllocator> ();
120  m_x->SetAttribute ("Min", DoubleValue (m_minX));
121  m_x->SetAttribute ("Max", DoubleValue (m_maxX));
122  m_y->SetAttribute ("Min", DoubleValue (m_minY));
123  m_y->SetAttribute ("Max", DoubleValue (m_maxY));
124  m_position->SetX (m_x);
125  m_position->SetY (m_y);
129 
130  m_helper.Update ();
131  m_helper.Pause ();
132 
133  // calculate the steady-state probability that a node is initially paused
134  double expectedPauseTime = (m_minPause + m_maxPause)/2;
135  double a = m_maxX - m_minX;
136  double b = m_maxY - m_minY;
137  double v0 = m_minSpeed;
138  double v1 = m_maxSpeed;
139  double log1 = b*b / a*std::log (std::sqrt ((a*a)/(b*b) + 1) + a/b);
140  double log2 = a*a / b*std::log (std::sqrt ((b*b)/(a*a) + 1) + b/a);
141  double expectedTravelTime = 1.0/6.0*(log1 + log2);
142  expectedTravelTime += 1.0/15.0*((a*a*a)/(b*b) + (b*b*b)/(a*a)) -
143  1.0/15.0*std::sqrt (a*a + b*b)*((a*a)/(b*b) + (b*b)/(a*a) - 3);
144  if (v0 == v1)
145  {
146  expectedTravelTime /= v0;
147  }
148  else
149  {
150  expectedTravelTime *= std::log (v1/v0)/(v1 - v0);
151  }
152  double probabilityPaused = expectedPauseTime/(expectedPauseTime + expectedTravelTime);
153  NS_ASSERT (probabilityPaused >= 0 && probabilityPaused <= 1);
154 
155  double u = m_u_r->GetValue (0, 1);
156  if (u < probabilityPaused) // node initially paused
157  {
158  m_helper.SetPosition (m_position->GetNext ());
159  u = m_u_r->GetValue (0, 1);
160  Time pause;
161  if (m_minPause != m_maxPause)
162  {
163  if (u < (2*m_minPause/(m_minPause + m_maxPause)))
164  {
165  pause = Seconds (u*(m_minPause + m_maxPause)/2);
166  }
167  else
168  {
169  // there is an error in equation 20 in the Tech. Report MCS-03-04
170  // this error is corrected in the TMC 2004 paper and below
171  pause = Seconds (m_maxPause - std::sqrt ((1 - u)*(m_maxPause*m_maxPause - m_minPause*m_minPause)));
172  }
173  }
174  else // if pause is constant
175  {
176  pause = Seconds (u*expectedPauseTime);
177  }
180  }
181  else // node initially moving
182  {
183  double x1, x2, y1, y2;
184  double r = 0;
185  double u1 = 1;
186  while (u1 >= r)
187  {
188  x1 = m_x1_r->GetValue (0, a);
189  y1 = m_y1_r->GetValue (0, b);
190  x2 = m_x2_r->GetValue (0, a);
191  y2 = m_y2_r->GetValue (0, b);
192  u1 = m_u_r->GetValue (0, 1);
193  r = std::sqrt (((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))/(a*a + b*b));
194  NS_ASSERT (r <= 1);
195  }
196  double u2 = m_u_r->GetValue (0, 1);
197  m_helper.SetPosition (Vector (m_minX + u2*x1 + (1 - u2)*x2, m_minY + u2*y1 + (1 - u2)*y2, m_z));
200  Vector (m_minX + x2, m_minY + y2, m_z));
201  }
203 }
204 
205 void
207 {
208  m_helper.Update ();
209  Vector m_current = m_helper.GetCurrentPosition ();
210  NS_ASSERT (m_minX <= m_current.x && m_current.x <= m_maxX);
211  NS_ASSERT (m_minY <= m_current.y && m_current.y <= m_maxY);
212  NS_ASSERT (m_minX <= destination.x && destination.x <= m_maxX);
213  NS_ASSERT (m_minY <= destination.y && destination.y <= m_maxY);
214  double u = m_u_r->GetValue (0, 1);
215  double speed = std::pow (m_maxSpeed, u)/std::pow (m_minSpeed, u - 1);
216  double dx = (destination.x - m_current.x);
217  double dy = (destination.y - m_current.y);
218  double dz = (destination.z - m_current.z);
219  double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
220 
221  m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
222  m_helper.Unpause ();
223  Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
224  m_event = Simulator::Schedule (travelDelay,
227 }
228 
229 void
231 {
232  m_helper.Update ();
233  Vector m_current = m_helper.GetCurrentPosition ();
234  NS_ASSERT (m_minX <= m_current.x && m_current.x <= m_maxX);
235  NS_ASSERT (m_minY <= m_current.y && m_current.y <= m_maxY);
236  Vector destination = m_position->GetNext ();
237  double speed = m_speed->GetValue ();
238  double dx = (destination.x - m_current.x);
239  double dy = (destination.y - m_current.y);
240  double dz = (destination.z - m_current.z);
241  double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
242 
243  m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
244  m_helper.Unpause ();
245  Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
246  m_event = Simulator::Schedule (travelDelay,
249 }
250 
251 void
253 {
254  m_helper.Update ();
255  m_helper.Pause ();
256  Time pause = Seconds (m_pause->GetValue ());
259 }
260 
261 Vector
263 {
264  m_helper.Update ();
265  return m_helper.GetCurrentPosition ();
266 }
267 void
269 {
270  if (alreadyStarted)
271  {
272  m_helper.SetPosition (position);
275  }
276 }
277 Vector
279 {
280  return m_helper.GetVelocity ();
281 }
282 int64_t
284 {
285  int64_t positionStreamsAllocated = 0;
286  m_speed->SetStream (stream);
287  m_pause->SetStream (stream + 1);
288  m_x1_r->SetStream (stream + 2);
289  m_y1_r->SetStream (stream + 3);
290  m_x2_r->SetStream (stream + 4);
291  m_y2_r->SetStream (stream + 5);
292  m_u_r->SetStream (stream + 6);
293  m_x->SetStream (stream + 7);
294  m_y->SetStream (stream + 8);
295  positionStreamsAllocated = m_position->AssignStreams (stream + 9);
296  return (9 + positionStreamsAllocated);
297 }
298 
299 } // 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
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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:824
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:985
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:161
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:343
double z
z coordinate of vector
Definition: vector.h:57