A Discrete-Event Network Simulator
API
steady-state-random-waypoint-mobility-model-test.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  */
19 #include "ns3/boolean.h"
20 #include "ns3/config.h"
21 #include "ns3/double.h"
22 #include "ns3/rng-seed-manager.h"
23 #include "ns3/simulator.h"
24 #include "ns3/steady-state-random-waypoint-mobility-model.h"
25 #include "ns3/test.h"
26 
27 #include <cmath>
28 
29 using namespace ns3;
30 
37 {
38  public:
40  : TestCase("Check steady-state rwp mobility model velocity and position distributions")
41  {
42  }
43 
45  {
46  }
47 
48  private:
49  std::vector<Ptr<MobilityModel>> mobilityStack;
50  double count;
51  private:
52  void DoRun() override;
53  void DoTeardown() override;
55  void DistribCompare();
56 };
57 
58 void
60 {
61  mobilityStack.clear();
62 }
63 
64 void
66 {
68 
69  // Total simulation time, seconds
70  double totalTime = 1000;
71 
72  ObjectFactory mobilityFactory;
73  mobilityFactory.SetTypeId("ns3::SteadyStateRandomWaypointMobilityModel");
74  mobilityFactory.Set("MinSpeed", DoubleValue(0.01));
75  mobilityFactory.Set("MaxSpeed", DoubleValue(20.0));
76  mobilityFactory.Set("MinPause", DoubleValue(0.0));
77  mobilityFactory.Set("MaxPause", DoubleValue(0.0));
78  mobilityFactory.Set("MinX", DoubleValue(0));
79  mobilityFactory.Set("MaxX", DoubleValue(1000));
80  mobilityFactory.Set("MinY", DoubleValue(0));
81  mobilityFactory.Set("MaxY", DoubleValue(600));
82 
83  // Populate the vector of mobility models.
84  count = 10000;
85  for (uint32_t i = 0; i < count; i++)
86  {
87  // Create a new mobility model.
88  Ptr<MobilityModel> model = mobilityFactory.Create()->GetObject<MobilityModel>();
89  model->AssignStreams(100 * (i + 1));
90  // Add this mobility model to the stack.
91  mobilityStack.push_back(model);
93  }
94 
97  Simulator::Stop(Seconds(totalTime));
100 }
101 
102 void
104 {
105  double velocity;
106  double sum_x = 0;
107  double sum_y = 0;
108  double sum_v = 0;
109  std::vector<Ptr<MobilityModel>>::iterator i;
110  Ptr<MobilityModel> model;
111  for (i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
112  {
113  model = (*i);
114  velocity =
115  std::sqrt(std::pow(model->GetVelocity().x, 2) + std::pow(model->GetVelocity().y, 2));
116  sum_x += model->GetPosition().x;
117  sum_y += model->GetPosition().y;
118  sum_v += velocity;
119  }
120  double mean_x = sum_x / count;
121  double mean_y = sum_y / count;
122  double mean_v = sum_v / count;
123 
124  NS_TEST_EXPECT_MSG_EQ_TOL(mean_x, 500, 25.0, "Got unexpected x-position mean value");
125  NS_TEST_EXPECT_MSG_EQ_TOL(mean_y, 300, 15.0, "Got unexpected y-position mean value");
126  NS_TEST_EXPECT_MSG_EQ_TOL(mean_v, 2.6, 0.13, "Got unexpected velocity mean value");
127 
128  sum_x = 0;
129  sum_y = 0;
130  sum_v = 0;
131  double tmp;
132  for (i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
133  {
134  model = (*i);
135  velocity =
136  std::sqrt(std::pow(model->GetVelocity().x, 2) + std::pow(model->GetVelocity().y, 2));
137  tmp = model->GetPosition().x - mean_x;
138  sum_x += tmp * tmp;
139  tmp = model->GetPosition().y - mean_y;
140  sum_y += tmp * tmp;
141  tmp = velocity - mean_v;
142  sum_v += tmp * tmp;
143  }
144  double dev_x = std::sqrt(sum_x / (count - 1));
145  double dev_y = std::sqrt(sum_y / (count - 1));
146  double dev_v = std::sqrt(sum_v / (count - 1));
147 
148  NS_TEST_EXPECT_MSG_EQ_TOL(dev_x, 230, 10.0, "Got unexpected x-position standard deviation");
149  NS_TEST_EXPECT_MSG_EQ_TOL(dev_y, 140, 7.0, "Got unexpected y-position standard deviation");
150  NS_TEST_EXPECT_MSG_EQ_TOL(dev_v, 4.4, 0.22, "Got unexpected velocity standard deviation");
151 }
152 
159 {
161  : TestSuite("steady-state-rwp-mobility-model", UNIT)
162  {
164  }
std::vector< Ptr< MobilityModel > > mobilityStack
modility model
void DoRun() override
Implementation to actually run this TestCase.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Keep track of the current position and velocity of an object.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Vector GetVelocity() const
Vector GetPosition() const
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:186
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
encapsulates test code
Definition: test.h:1060
@ QUICK
Fast test.
Definition: test.h:1065
A suite of tests to run.
Definition: test.h:1256
SteadyStateRandomWaypointTestSuite g_steadyStateRandomWaypointTestSuite
the test suite
#define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report if ...
Definition: test.h:510
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.