A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
steady-state-random-waypoint-mobility-model.cc
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"
23 #include "steady-state-random-waypoint-mobility-model.h"
24 #include "ns3/test.h"
25 
26 namespace ns3 {
27 
28 NS_OBJECT_ENSURE_REGISTERED (SteadyStateRandomWaypointMobilityModel);
29 
30 TypeId
31 SteadyStateRandomWaypointMobilityModel::GetTypeId (void)
32 {
33  static TypeId tid = TypeId ("ns3::SteadyStateRandomWaypointMobilityModel")
34  .SetParent<MobilityModel> ()
35  .SetGroupName ("Mobility")
36  .AddConstructor<SteadyStateRandomWaypointMobilityModel> ()
37  .AddAttribute ("MinSpeed",
38  "Minimum speed value, [m/s]",
39  DoubleValue (0.3),
40  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_minSpeed),
41  MakeDoubleChecker<double> ())
42  .AddAttribute ("MaxSpeed",
43  "Maximum speed value, [m/s]",
44  DoubleValue (0.7),
45  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_maxSpeed),
46  MakeDoubleChecker<double> ())
47  .AddAttribute ("MinPause",
48  "Minimum pause value, [s]",
49  DoubleValue (0.0),
50  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_minPause),
51  MakeDoubleChecker<double> ())
52  .AddAttribute ("MaxPause",
53  "Maximum pause value, [s]",
54  DoubleValue (0.0),
55  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_maxPause),
56  MakeDoubleChecker<double> ())
57  .AddAttribute ("MinX",
58  "Minimum X value of traveling region, [m]",
59  DoubleValue (1),
60  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_minX),
61  MakeDoubleChecker<double> ())
62  .AddAttribute ("MaxX",
63  "Maximum X value of traveling region, [m]",
64  DoubleValue (1),
65  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_maxX),
66  MakeDoubleChecker<double> ())
67  .AddAttribute ("MinY",
68  "Minimum Y value of traveling region, [m]",
69  DoubleValue (1),
70  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_minY),
71  MakeDoubleChecker<double> ())
72  .AddAttribute ("MaxY",
73  "Maximum Y value of traveling region, [m]",
74  DoubleValue (1),
75  MakeDoubleAccessor (&SteadyStateRandomWaypointMobilityModel::m_maxY),
76  MakeDoubleChecker<double> ());
77 
78  return tid;
79 }
80 
81 SteadyStateRandomWaypointMobilityModel::SteadyStateRandomWaypointMobilityModel () :
82  alreadyStarted (false)
83 {
84  m_speed = CreateObject<UniformRandomVariable> ();
85  m_pause = CreateObject<UniformRandomVariable> ();
86  m_x1_r = CreateObject<UniformRandomVariable> ();
87  m_y1_r = CreateObject<UniformRandomVariable> ();
88  m_x2_r = CreateObject<UniformRandomVariable> ();
89  m_y2_r = CreateObject<UniformRandomVariable> ();
90  m_u_r = CreateObject<UniformRandomVariable> ();
91  m_x = CreateObject<UniformRandomVariable> ();
92  m_y = CreateObject<UniformRandomVariable> ();
93 }
94 
95 void
97 {
98  DoInitializePrivate ();
100 }
101 
102 void
103 SteadyStateRandomWaypointMobilityModel::DoInitializePrivate (void)
104 {
105  alreadyStarted = true;
106  // Configure random variables based on attributes
107  NS_ASSERT (m_minSpeed >= 1e-6);
108  NS_ASSERT (m_minSpeed <= m_maxSpeed);
109  m_speed->SetAttribute ("Min", DoubleValue (m_minSpeed));
110  m_speed->SetAttribute ("Max", DoubleValue (m_maxSpeed));
111  NS_ASSERT (m_minX < m_maxX);
112  NS_ASSERT (m_minY < m_maxY);
113  m_position = CreateObject<RandomRectanglePositionAllocator> ();
114  m_x->SetAttribute ("Min", DoubleValue (m_minX));
115  m_x->SetAttribute ("Max", DoubleValue (m_maxX));
116  m_y->SetAttribute ("Min", DoubleValue (m_minY));
117  m_y->SetAttribute ("Max", DoubleValue (m_maxY));
118  m_position->SetX (m_x);
119  m_position->SetY (m_y);
120  NS_ASSERT (m_minPause <= m_maxPause);
121  m_pause->SetAttribute ("Min", DoubleValue (m_minPause));
122  m_pause->SetAttribute ("Max", DoubleValue (m_maxPause));
123 
124  m_helper.Update ();
125  m_helper.Pause ();
126 
127  // calculate the steady-state probability that a node is initially paused
128  double expectedPauseTime = (m_minPause + m_maxPause)/2;
129  double a = m_maxX - m_minX;
130  double b = m_maxY - m_minY;
131  double v0 = m_minSpeed;
132  double v1 = m_maxSpeed;
133  double log1 = b*b / a*std::log (std::sqrt ((a*a)/(b*b) + 1) + a/b);
134  double log2 = a*a / b*std::log (std::sqrt ((b*b)/(a*a) + 1) + b/a);
135  double expectedTravelTime = 1.0/6.0*(log1 + log2);
136  expectedTravelTime += 1.0/15.0*((a*a*a)/(b*b) + (b*b*b)/(a*a)) -
137  1.0/15.0*std::sqrt (a*a + b*b)*((a*a)/(b*b) + (b*b)/(a*a) - 3);
138  if (v0 == v1)
139  {
140  expectedTravelTime /= v0;
141  }
142  else
143  {
144  expectedTravelTime *= std::log (v1/v0)/(v1 - v0);
145  }
146  double probabilityPaused = expectedPauseTime/(expectedPauseTime + expectedTravelTime);
147  NS_ASSERT (probabilityPaused >= 0 && probabilityPaused <= 1);
148 
149  double u = m_u_r->GetValue (0, 1);
150  if (u < probabilityPaused) // node initially paused
151  {
152  m_helper.SetPosition (m_position->GetNext ());
153  u = m_u_r->GetValue (0, 1);
154  Time pause;
155  if (m_minPause != m_maxPause)
156  {
157  if (u < (2*m_minPause/(m_minPause + m_maxPause)))
158  {
159  pause = Seconds (u*(m_minPause + m_maxPause)/2);
160  }
161  else
162  {
163  // there is an error in equation 20 in the Tech. Report MCS-03-04
164  // this error is corrected in the TMC 2004 paper and below
165  pause = Seconds (m_maxPause - std::sqrt ((1 - u)*(m_maxPause*m_maxPause - m_minPause*m_minPause)));
166  }
167  }
168  else // if pause is constant
169  {
170  pause = Seconds (u*expectedPauseTime);
171  }
172  NS_ASSERT (!m_event.IsRunning ());
173  m_event = Simulator::Schedule (pause, &SteadyStateRandomWaypointMobilityModel::BeginWalk, this);
174  }
175  else // node initially moving
176  {
177  double x1, x2, y1, y2;
178  double r = 0;
179  double u1 = 1;
180  while (u1 >= r)
181  {
182  x1 = m_x1_r->GetValue (0, a);
183  y1 = m_y1_r->GetValue (0, b);
184  x2 = m_x2_r->GetValue (0, a);
185  y2 = m_y2_r->GetValue (0, b);
186  u1 = m_u_r->GetValue (0, 1);
187  r = std::sqrt (((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))/(a*a + b*b));
188  NS_ASSERT (r <= 1);
189  }
190  double u2 = m_u_r->GetValue (0, 1);
191  m_helper.SetPosition (Vector (m_minX + u2*x1 + (1 - u2)*x2, m_minY + u2*y1 + (1 - u2)*y2, 0));
192  NS_ASSERT (!m_event.IsRunning ());
193  m_event = Simulator::ScheduleNow (&SteadyStateRandomWaypointMobilityModel::SteadyStateBeginWalk, this,
194  Vector (m_minX + x2, m_minY + y2, 0));
195  }
197 }
198 
199 void
200 SteadyStateRandomWaypointMobilityModel::SteadyStateBeginWalk (const Vector &destination)
201 {
202  m_helper.Update ();
203  Vector m_current = m_helper.GetCurrentPosition ();
204  NS_ASSERT (m_minX <= m_current.x && m_current.x <= m_maxX);
205  NS_ASSERT (m_minY <= m_current.y && m_current.y <= m_maxY);
206  NS_ASSERT (m_minX <= destination.x && destination.x <= m_maxX);
207  NS_ASSERT (m_minY <= destination.y && destination.y <= m_maxY);
208  double u = m_u_r->GetValue (0, 1);
209  double speed = std::pow (m_maxSpeed, u)/std::pow (m_minSpeed, u - 1);
210  double dx = (destination.x - m_current.x);
211  double dy = (destination.y - m_current.y);
212  double dz = (destination.z - m_current.z);
213  double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
214 
215  m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
216  m_helper.Unpause ();
217  Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
218  m_event = Simulator::Schedule (travelDelay,
219  &SteadyStateRandomWaypointMobilityModel::Start, this);
221 }
222 
223 void
224 SteadyStateRandomWaypointMobilityModel::BeginWalk (void)
225 {
226  m_helper.Update ();
227  Vector m_current = m_helper.GetCurrentPosition ();
228  NS_ASSERT (m_minX <= m_current.x && m_current.x <= m_maxX);
229  NS_ASSERT (m_minY <= m_current.y && m_current.y <= m_maxY);
230  Vector destination = m_position->GetNext ();
231  double speed = m_speed->GetValue ();
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 = Simulator::Schedule (travelDelay,
241  &SteadyStateRandomWaypointMobilityModel::Start, this);
243 }
244 
245 void
246 SteadyStateRandomWaypointMobilityModel::Start (void)
247 {
248  m_helper.Update ();
249  m_helper.Pause ();
250  Time pause = Seconds (m_pause->GetValue ());
251  m_event = Simulator::Schedule (pause, &SteadyStateRandomWaypointMobilityModel::BeginWalk, this);
253 }
254 
255 Vector
257 {
258  m_helper.Update ();
259  return m_helper.GetCurrentPosition ();
260 }
261 void
263 {
264  if (alreadyStarted)
265  {
266  m_helper.SetPosition (position);
267  Simulator::Remove (m_event);
268  m_event = Simulator::ScheduleNow (&SteadyStateRandomWaypointMobilityModel::Start, this);
269  }
270 }
271 Vector
273 {
274  return m_helper.GetVelocity ();
275 }
276 int64_t
278 {
279  int64_t positionStreamsAllocated = 0;
280  m_speed->SetStream (stream);
281  m_pause->SetStream (stream + 1);
282  m_x1_r->SetStream (stream + 2);
283  m_y1_r->SetStream (stream + 3);
284  m_x2_r->SetStream (stream + 4);
285  m_y2_r->SetStream (stream + 5);
286  m_u_r->SetStream (stream + 6);
287  m_x->SetStream (stream + 7);
288  m_y->SetStream (stream + 8);
289  positionStreamsAllocated = m_position->AssignStreams (stream + 9);
290  return (9 + positionStreamsAllocated);
291 }
292 
293 } // namespace ns3
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
#define NS_ASSERT(condition)
Definition: assert.h:64
bool IsRunning(void) const
Definition: event-id.cc:59
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
a 3d vector
Definition: vector.h:31
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:71
void NotifyCourseChange(void) const
static void Remove(const EventId &id)
Definition: simulator.cc:257
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Definition: simulator.h:981
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Hold an floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:160
virtual void DoInitialize(void)
Definition: object.cc:342