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
29using namespace ns3;
30
38{
39 public:
41 : TestCase("Check steady-state rwp mobility model velocity and position distributions")
42 {
43 }
44
46 {
47 }
48
49 private:
50 std::vector<Ptr<MobilityModel>> mobilityStack;
51 double count;
52 private:
53 void DoRun() override;
54 void DoTeardown() override;
56 void DistribCompare();
57};
58
59void
61{
62 mobilityStack.clear();
63}
64
65void
67{
68 SeedManager::SetSeed(123);
69
70 // Total simulation time, seconds
71 double totalTime = 1000;
72
73 ObjectFactory mobilityFactory;
74 mobilityFactory.SetTypeId("ns3::SteadyStateRandomWaypointMobilityModel");
75 mobilityFactory.Set("MinSpeed", DoubleValue(0.01));
76 mobilityFactory.Set("MaxSpeed", DoubleValue(20.0));
77 mobilityFactory.Set("MinPause", DoubleValue(0.0));
78 mobilityFactory.Set("MaxPause", DoubleValue(0.0));
79 mobilityFactory.Set("MinX", DoubleValue(0));
80 mobilityFactory.Set("MaxX", DoubleValue(1000));
81 mobilityFactory.Set("MinY", DoubleValue(0));
82 mobilityFactory.Set("MaxY", DoubleValue(600));
83
84 // Populate the vector of mobility models.
85 count = 10000;
86 for (uint32_t i = 0; i < count; i++)
87 {
88 // Create a new mobility model.
89 Ptr<MobilityModel> model = mobilityFactory.Create()->GetObject<MobilityModel>();
90 model->AssignStreams(100 * (i + 1));
91 // Add this mobility model to the stack.
92 mobilityStack.push_back(model);
93 Simulator::Schedule(Seconds(0.0), &Object::Initialize, model);
94 }
95
96 Simulator::Schedule(Seconds(0.001), &SteadyStateRandomWaypointTest::DistribCompare, this);
97 Simulator::Schedule(Seconds(totalTime), &SteadyStateRandomWaypointTest::DistribCompare, this);
98 Simulator::Stop(Seconds(totalTime));
99 Simulator::Run();
100 Simulator::Destroy();
101}
102
103void
105{
106 double velocity;
107 double sum_x = 0;
108 double sum_y = 0;
109 double sum_v = 0;
110 std::vector<Ptr<MobilityModel>>::iterator i;
111 Ptr<MobilityModel> model;
112 for (i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
113 {
114 model = (*i);
115 velocity =
116 std::sqrt(std::pow(model->GetVelocity().x, 2) + std::pow(model->GetVelocity().y, 2));
117 sum_x += model->GetPosition().x;
118 sum_y += model->GetPosition().y;
119 sum_v += velocity;
120 }
121 double mean_x = sum_x / count;
122 double mean_y = sum_y / count;
123 double mean_v = sum_v / count;
124
125 NS_TEST_EXPECT_MSG_EQ_TOL(mean_x, 500, 25.0, "Got unexpected x-position mean value");
126 NS_TEST_EXPECT_MSG_EQ_TOL(mean_y, 300, 15.0, "Got unexpected y-position mean value");
127 NS_TEST_EXPECT_MSG_EQ_TOL(mean_v, 2.6, 0.13, "Got unexpected velocity mean value");
128
129 sum_x = 0;
130 sum_y = 0;
131 sum_v = 0;
132 double tmp;
133 for (i = mobilityStack.begin(); i != mobilityStack.end(); ++i)
134 {
135 model = (*i);
136 velocity =
137 std::sqrt(std::pow(model->GetVelocity().x, 2) + std::pow(model->GetVelocity().y, 2));
138 tmp = model->GetPosition().x - mean_x;
139 sum_x += tmp * tmp;
140 tmp = model->GetPosition().y - mean_y;
141 sum_y += tmp * tmp;
142 tmp = velocity - mean_v;
143 sum_v += tmp * tmp;
144 }
145 double dev_x = std::sqrt(sum_x / (count - 1));
146 double dev_y = std::sqrt(sum_y / (count - 1));
147 double dev_v = std::sqrt(sum_v / (count - 1));
148
149 NS_TEST_EXPECT_MSG_EQ_TOL(dev_x, 230, 10.0, "Got unexpected x-position standard deviation");
150 NS_TEST_EXPECT_MSG_EQ_TOL(dev_y, 140, 7.0, "Got unexpected y-position standard deviation");
151 NS_TEST_EXPECT_MSG_EQ_TOL(dev_v, 4.4, 0.22, "Got unexpected velocity standard deviation");
152}
153
161{
163 : TestSuite("steady-state-rwp-mobility-model", UNIT)
164 {
165 AddTestCase(new SteadyStateRandomWaypointTest, TestCase::QUICK);
166 }
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
#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
SteadyStateRandomWaypointTestSuite g_steadyStateRandomWaypointTestSuite
the test suite
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.