A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
gauss-markov-mobility-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 Dan Broyles
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Dan Broyles <dbroyl01@ku.edu>
7 */
9
10#include "position-allocator.h"
11
12#include "ns3/double.h"
13#include "ns3/pointer.h"
14#include "ns3/simulator.h"
15#include "ns3/string.h"
16
17#include <cmath>
18
19namespace ns3
20{
21
23
26{
27 static TypeId tid =
28 TypeId("ns3::GaussMarkovMobilityModel")
30 .SetGroupName("Mobility")
31 .AddConstructor<GaussMarkovMobilityModel>()
32 .AddAttribute("Bounds",
33 "Bounds of the area to cruise.",
34 BoxValue(Box(-100.0, 100.0, -100.0, 100.0, 0.0, 100.0)),
37 .AddAttribute("TimeStep",
38 "Change current direction and speed after moving for this time.",
42 .AddAttribute(
43 "Alpha",
44 "A constant representing the tunable parameter in the Gauss-Markov model.",
45 DoubleValue(1.0),
48 .AddAttribute("MeanVelocity",
49 "A random variable used to assign the average velocity.",
50 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
53 .AddAttribute("MeanDirection",
54 "A random variable used to assign the average direction.",
55 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=6.283185307]"),
58 .AddAttribute("MeanPitch",
59 "A random variable used to assign the average pitch.",
60 StringValue("ns3::ConstantRandomVariable[Constant=0.0]"),
63 .AddAttribute("NormalVelocity",
64 "A gaussian random variable used to calculate the next velocity value.",
65 StringValue("ns3::NormalRandomVariable[Mean=0.0|Variance=1.0|Bound=10."
66 "0]"), // Defaults to zero mean, and std dev = 1, and bound to
67 // +-10 of the mean
70 .AddAttribute(
71 "NormalDirection",
72 "A gaussian random variable used to calculate the next direction value.",
73 StringValue("ns3::NormalRandomVariable[Mean=0.0|Variance=1.0|Bound=10.0]"),
76 .AddAttribute(
77 "NormalPitch",
78 "A gaussian random variable used to calculate the next pitch value.",
79 StringValue("ns3::NormalRandomVariable[Mean=0.0|Variance=1.0|Bound=10.0]"),
82
83 return tid;
84}
85
94
99
100void
102{
103 if (m_meanVelocity == 0.0)
104 {
105 // Initialize the mean velocity, direction, and pitch variables
106 m_meanVelocity = m_rndMeanVelocity->GetValue();
108 m_meanPitch = m_rndMeanPitch->GetValue();
109 double cosD = std::cos(m_meanDirection);
110 double cosP = std::cos(m_meanPitch);
111 double sinD = std::sin(m_meanDirection);
112 double sinP = std::sin(m_meanPitch);
113 // Initialize the starting velocity, direction, and pitch to be identical to the mean ones
117 // Set the velocity vector to give to the constant velocity helper
118 m_helper.SetVelocity(
119 Vector(m_Velocity * cosD * cosP, m_Velocity * sinD * cosP, m_Velocity * sinP));
120 }
121 m_helper.Update();
122
123 // Get the next values from the gaussian distributions for velocity, direction, and pitch
124 double rv = m_normalVelocity->GetValue();
125 double rd = m_normalDirection->GetValue();
126 double rp = m_normalPitch->GetValue();
127
128 // Calculate the NEW velocity, direction, and pitch values using the Gauss-Markov formula:
129 // newVal = alpha*oldVal + (1-alpha)*meanVal + sqrt(1-alpha^2)*rv
130 // where rv is a random number from a normal (gaussian) distribution
131 double one_minus_alpha = 1 - m_alpha;
132 double sqrt_alpha = std::sqrt(1 - m_alpha * m_alpha);
133 m_Velocity = m_alpha * m_Velocity + one_minus_alpha * m_meanVelocity + sqrt_alpha * rv;
134 m_Direction = m_alpha * m_Direction + one_minus_alpha * m_meanDirection + sqrt_alpha * rd;
135 m_Pitch = m_alpha * m_Pitch + one_minus_alpha * m_meanPitch + sqrt_alpha * rp;
136
137 // Calculate the linear velocity vector to give to the constant velocity helper
138 double cosDir = std::cos(m_Direction);
139 double cosPit = std::cos(m_Pitch);
140 double sinDir = std::sin(m_Direction);
141 double sinPit = std::sin(m_Pitch);
142 double vx = m_Velocity * cosDir * cosPit;
143 double vy = m_Velocity * sinDir * cosPit;
144 double vz = m_Velocity * sinPit;
145 m_helper.SetVelocity(Vector(vx, vy, vz));
146
147 m_helper.Unpause();
148
150}
151
152void
154{
155 m_helper.UpdateWithBounds(m_bounds);
156 Vector position = m_helper.GetCurrentPosition();
157 Vector speed = m_helper.GetVelocity();
158 Vector nextPosition = position;
159 nextPosition.x += speed.x * delayLeft.GetSeconds();
160 nextPosition.y += speed.y * delayLeft.GetSeconds();
161 nextPosition.z += speed.z * delayLeft.GetSeconds();
162 if (delayLeft.GetSeconds() < 0.0)
163 {
164 delayLeft = Seconds(1);
165 }
166
167 // Make sure that the position by the next time step is still within the boundary.
168 // If out of bounds, then alter the velocity vector and average direction to keep the position
169 // in bounds
170 if (m_bounds.IsInside(nextPosition))
171 {
173 }
174 else
175 {
176 if (nextPosition.x > m_bounds.xMax || nextPosition.x < m_bounds.xMin)
177 {
178 speed.x = -speed.x;
180 }
181
182 if (nextPosition.y > m_bounds.yMax || nextPosition.y < m_bounds.yMin)
183 {
184 speed.y = -speed.y;
186 }
187
188 if (nextPosition.z > m_bounds.zMax || nextPosition.z < m_bounds.zMin)
189 {
190 speed.z = -speed.z;
192 }
193
196 m_helper.SetVelocity(speed);
197 m_helper.Unpause();
199 }
201}
202
203void
209
210Vector
212{
213 m_helper.Update();
214 return m_helper.GetCurrentPosition();
215}
216
217void
219{
220 m_helper.SetPosition(position);
221 m_event.Cancel();
223}
224
225Vector
227{
228 return m_helper.GetVelocity();
229}
230
231int64_t
233{
234 m_rndMeanVelocity->SetStream(stream);
235 m_normalVelocity->SetStream(stream + 1);
236 m_rndMeanDirection->SetStream(stream + 2);
237 m_normalDirection->SetStream(stream + 3);
238 m_rndMeanPitch->SetStream(stream + 4);
239 m_normalPitch->SetStream(stream + 5);
240 return 6;
241}
242
243} // namespace ns3
a 3d box
Definition box.h:24
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Ptr< NormalRandomVariable > m_normalDirection
Gaussian rv for next direction value.
void DoDispose() override
Destructor implementation.
Ptr< NormalRandomVariable > m_normalPitch
Gaussian rv for next pitch.
void Start()
Initialize the model and calculate new velocity, direction, and pitch.
void DoWalk(Time timeLeft)
Perform a walk operation.
double m_meanVelocity
current mean velocity
double m_meanDirection
current mean direction
static TypeId GetTypeId()
Register this type with the TypeId system.
Ptr< NormalRandomVariable > m_normalVelocity
Gaussian rv used to for next velocity.
EventId m_event
event id of scheduled start
ConstantVelocityHelper m_helper
constant velocity helper
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
Ptr< RandomVariableStream > m_rndMeanPitch
rv used to assign avg.
Ptr< RandomVariableStream > m_rndMeanVelocity
rv used to assign avg velocity
void DoSetPosition(const Vector &position) override
Ptr< RandomVariableStream > m_rndMeanDirection
rv used to assign avg direction
double m_alpha
tunable constant in the model
Time m_timeStep
duraiton after which direction and speed should change
void NotifyCourseChange() const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:595
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1345
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1433
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeBoxAccessor(T1 a1)
Definition box.h:115
Ptr< const AttributeChecker > MakeBoxChecker()
Definition box.cc:191
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1453