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
GetTypeId(void)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     .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 
SteadyStateRandomWaypointMobilityModel()86 SteadyStateRandomWaypointMobilityModel::SteadyStateRandomWaypointMobilityModel () :
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   m_position = CreateObject<RandomBoxPositionAllocator> ();
99 }
100 
101 void
DoInitialize(void)102 SteadyStateRandomWaypointMobilityModel::DoInitialize (void)
103 {
104   DoInitializePrivate ();
105   MobilityModel::DoInitialize ();
106 }
107 
108 void
DoInitializePrivate(void)109 SteadyStateRandomWaypointMobilityModel::DoInitializePrivate (void)
110 {
111   alreadyStarted = true;
112   // Configure random variables based on attributes
113   NS_ASSERT (m_minSpeed >= 1e-6);
114   NS_ASSERT (m_minSpeed <= m_maxSpeed);
115   m_speed->SetAttribute ("Min", DoubleValue (m_minSpeed));
116   m_speed->SetAttribute ("Max", DoubleValue (m_maxSpeed));
117   NS_ASSERT (m_minX < m_maxX);
118   NS_ASSERT (m_minY < m_maxY);
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);
125   Ptr<ConstantRandomVariable> z = CreateObject<ConstantRandomVariable> ();
126   z->SetAttribute ("Constant", DoubleValue (m_z));
127   m_position->SetZ (z);
128 
129   NS_ASSERT (m_minPause <= m_maxPause);
130   m_pause->SetAttribute ("Min", DoubleValue (m_minPause));
131   m_pause->SetAttribute ("Max", DoubleValue (m_maxPause));
132 
133   m_helper.Update ();
134   m_helper.Pause ();
135 
136   // calculate the steady-state probability that a node is initially paused
137   double expectedPauseTime = (m_minPause + m_maxPause)/2;
138   double a = m_maxX - m_minX;
139   double b = m_maxY - m_minY;
140   double v0 = m_minSpeed;
141   double v1 = m_maxSpeed;
142   double log1 = b*b / a*std::log (std::sqrt ((a*a)/(b*b) + 1) + a/b);
143   double log2 = a*a / b*std::log (std::sqrt ((b*b)/(a*a) + 1) + b/a);
144   double expectedTravelTime = 1.0/6.0*(log1 + log2);
145   expectedTravelTime += 1.0/15.0*((a*a*a)/(b*b) + (b*b*b)/(a*a)) -
146     1.0/15.0*std::sqrt (a*a + b*b)*((a*a)/(b*b) + (b*b)/(a*a) - 3);
147   if (v0 == v1)
148     {
149       expectedTravelTime /= v0;
150     }
151   else
152     {
153       expectedTravelTime *= std::log (v1/v0)/(v1 - v0);
154     }
155   double probabilityPaused = expectedPauseTime/(expectedPauseTime + expectedTravelTime);
156   NS_ASSERT (probabilityPaused >= 0 && probabilityPaused <= 1);
157 
158   double u = m_u_r->GetValue (0, 1);
159   if (u < probabilityPaused) // node initially paused
160     {
161       m_helper.SetPosition (m_position->GetNext ());
162       u = m_u_r->GetValue (0, 1);
163       Time pause;
164       if (m_minPause != m_maxPause)
165         {
166           if (u < (2*m_minPause/(m_minPause + m_maxPause)))
167             {
168               pause = Seconds (u*(m_minPause + m_maxPause)/2);
169             }
170           else
171             {
172               // there is an error in equation 20 in the Tech. Report MCS-03-04
173               // this error is corrected in the TMC 2004 paper and below
174               pause = Seconds (m_maxPause - std::sqrt ((1 - u)*(m_maxPause*m_maxPause - m_minPause*m_minPause)));
175             }
176         }
177       else // if pause is constant
178         {
179           pause = Seconds (u*expectedPauseTime);
180         }
181       NS_ASSERT (!m_event.IsRunning ());
182       m_event = Simulator::Schedule (pause, &SteadyStateRandomWaypointMobilityModel::BeginWalk, this);
183     }
184   else // node initially moving
185     {
186       double x1, x2, y1, y2;
187       double r = 0;
188       double u1 = 1;
189       while (u1 >= r)
190         {
191           x1 = m_x1_r->GetValue (0, a);
192           y1 = m_y1_r->GetValue (0, b);
193           x2 = m_x2_r->GetValue (0, a);
194           y2 = m_y2_r->GetValue (0, b);
195           u1 = m_u_r->GetValue (0, 1);
196           r = std::sqrt (((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))/(a*a + b*b));
197           NS_ASSERT (r <= 1);
198         }
199       double u2 = m_u_r->GetValue (0, 1);
200       m_helper.SetPosition (Vector (m_minX + u2*x1 + (1 - u2)*x2, m_minY + u2*y1 + (1 - u2)*y2, m_z));
201       NS_ASSERT (!m_event.IsRunning ());
202       m_event = Simulator::ScheduleNow (&SteadyStateRandomWaypointMobilityModel::SteadyStateBeginWalk, this,
203                                         Vector (m_minX + x2, m_minY + y2, m_z));
204     }
205   NotifyCourseChange ();
206 }
207 
208 void
SteadyStateBeginWalk(const Vector & destination)209 SteadyStateRandomWaypointMobilityModel::SteadyStateBeginWalk (const Vector &destination)
210 {
211   m_helper.Update ();
212   Vector m_current = m_helper.GetCurrentPosition ();
213   NS_ASSERT (m_minX <= m_current.x && m_current.x <= m_maxX);
214   NS_ASSERT (m_minY <= m_current.y && m_current.y <= m_maxY);
215   NS_ASSERT (m_minX <= destination.x && destination.x <= m_maxX);
216   NS_ASSERT (m_minY <= destination.y && destination.y <= m_maxY);
217   double u = m_u_r->GetValue (0, 1);
218   double speed = std::pow (m_maxSpeed, u)/std::pow (m_minSpeed, u - 1);
219   double dx = (destination.x - m_current.x);
220   double dy = (destination.y - m_current.y);
221   double dz = (destination.z - m_current.z);
222   double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
223 
224   m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
225   m_helper.Unpause ();
226   Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
227   m_event = Simulator::Schedule (travelDelay,
228                                  &SteadyStateRandomWaypointMobilityModel::Start, this);
229   NotifyCourseChange ();
230 }
231 
232 void
BeginWalk(void)233 SteadyStateRandomWaypointMobilityModel::BeginWalk (void)
234 {
235   m_helper.Update ();
236   Vector m_current = m_helper.GetCurrentPosition ();
237   NS_ASSERT (m_minX <= m_current.x && m_current.x <= m_maxX);
238   NS_ASSERT (m_minY <= m_current.y && m_current.y <= m_maxY);
239   Vector destination = m_position->GetNext ();
240   double speed = m_speed->GetValue ();
241   double dx = (destination.x - m_current.x);
242   double dy = (destination.y - m_current.y);
243   double dz = (destination.z - m_current.z);
244   double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
245 
246   m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
247   m_helper.Unpause ();
248   Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
249   m_event = Simulator::Schedule (travelDelay,
250                                  &SteadyStateRandomWaypointMobilityModel::Start, this);
251   NotifyCourseChange ();
252 }
253 
254 void
Start(void)255 SteadyStateRandomWaypointMobilityModel::Start (void)
256 {
257   m_helper.Update ();
258   m_helper.Pause ();
259   Time pause = Seconds (m_pause->GetValue ());
260   m_event = Simulator::Schedule (pause, &SteadyStateRandomWaypointMobilityModel::BeginWalk, this);
261   NotifyCourseChange ();
262 }
263 
264 Vector
DoGetPosition(void) const265 SteadyStateRandomWaypointMobilityModel::DoGetPosition (void) const
266 {
267   m_helper.Update ();
268   return m_helper.GetCurrentPosition ();
269 }
270 void
DoSetPosition(const Vector & position)271 SteadyStateRandomWaypointMobilityModel::DoSetPosition (const Vector &position)
272 {
273   if (alreadyStarted)
274     {
275       m_helper.SetPosition (position);
276       m_event.Cancel ();
277       m_event = Simulator::ScheduleNow (&SteadyStateRandomWaypointMobilityModel::Start, this);
278     }
279 }
280 Vector
DoGetVelocity(void) const281 SteadyStateRandomWaypointMobilityModel::DoGetVelocity (void) const
282 {
283   return m_helper.GetVelocity ();
284 }
285 int64_t
DoAssignStreams(int64_t stream)286 SteadyStateRandomWaypointMobilityModel::DoAssignStreams (int64_t stream)
287 {
288   int64_t positionStreamsAllocated = 0;
289   m_speed->SetStream (stream);
290   m_pause->SetStream (stream + 1);
291   m_x1_r->SetStream (stream + 2);
292   m_y1_r->SetStream (stream + 3);
293   m_x2_r->SetStream (stream + 4);
294   m_y2_r->SetStream (stream + 5);
295   m_u_r->SetStream (stream + 6);
296   m_x->SetStream (stream + 7);
297   m_y->SetStream (stream + 8);
298   positionStreamsAllocated = m_position->AssignStreams (stream + 9);
299   return (9 + positionStreamsAllocated);
300 }
301 
302 } // namespace ns3
303