-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFiveGTransmissionManager.cpp
More file actions
executable file
·202 lines (175 loc) · 6.13 KB
/
FiveGTransmissionManager.cpp
File metadata and controls
executable file
·202 lines (175 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// FiveGTransmissionManager.cpp
// AggregationNS3
//
// Created by Alper Sinan Akyurek on 8/21/16.
// Copyright © 2016 Alper Sinan Akyurek. All rights reserved.
//
#include "FiveGTransmissionManager.hpp"
#include "ApplicationConfigureHelper.hpp"
NS_LOG_COMPONENT_DEFINE("FiveGTransmissionManager");
NS_OBJECT_ENSURE_REGISTERED(FiveGTransmissionManager);
ns3::TypeId
FiveGTransmissionManager::GetTypeId( void )
{
static ns3::TypeId tid = ns3::TypeId( "FiveGTransmissionManager" )
.SetParent<ns3::Object> ()
.AddConstructor<FiveGTransmissionManager> ()
;
return ( tid );
}
FiveGTransmissionManager::FiveGTransmissionManager( void ) : m_searchCount( 20 ),
m_searchStart( ns3::Seconds( 0.1 ) ),
m_searchStop( ns3::Seconds( 20 ) ),
m_usedCValue( -1.0 )
{
}
FiveGTransmissionManager::TGain
FiveGTransmissionManager::GetTotalGain( const GainFunction::TTransmissionTime & x ) const
{
TGain totalGain = 0;
for ( auto i = this->m_applications.begin(); i != this->m_applications.end(); ++i )
{
totalGain += ( *i )->GetMeasurementGain( x );
}
return ( totalGain );
}
FiveGTransmissionManager::TGain
FiveGTransmissionManager::GetTotalGainDerivative( const GainFunction::TTransmissionTime & x ) const
{
TGain totalGainDerivative = 0;
for ( auto i = this->m_applications.begin(); i != this->m_applications.end(); ++i )
{
totalGainDerivative += ( *i )->GetMeasurementGainDerivative( x );
}
return ( totalGainDerivative * this->m_messageGainFunction->GetGain( x ) / this->m_messageGainFunction->GetGainDerivative( x ) );
}
GainFunction::TTransmissionTime
FiveGTransmissionManager::GetOptimalTime( void )
{
if ( this->m_usedCValue != *( this->m_cValue ) )
{
this->m_usedCValue = *( this->m_cValue );
this->m_currentOptimalTime = this->SolveForCValue( *this->m_cValue );
}
else
{
GainFunction::TTransmissionTime updatedTime = this->SolveForCValue( *this->m_cValue );
if ( std::abs( updatedTime.GetSeconds() - this->m_currentOptimalTime.GetSeconds() ) > this->m_updateThreshold.GetSeconds() )
{
BaseStation::TResponseVector myResponses, deltaResponses;
auto numberOfCValues = BaseStation::GetBaseStation().cVector.size();
for ( auto cIndex = 0; cIndex < numberOfCValues; ++cIndex )
{
myResponses.push_back( this->ProcessCValue( BaseStation::GetBaseStation().cVector[cIndex] ) );
deltaResponses.push_back( myResponses[cIndex] - this->m_myResponses[cIndex] );
}
BaseStation::GetBaseStation().UpdateResponses( deltaResponses );
this->m_myResponses = myResponses;
}
}
return ( this->m_currentOptimalTime );
}
void
FiveGTransmissionManager::Start( void )
{
this->ConnectToBaseStation();
GainFunction::TTransmissionTime x = this->GetOptimalTime() + ns3::Seconds( 0.1 * ((double)rand() / (double)RAND_MAX ) );
ns3::Simulator::Schedule( x, &FiveGTransmissionManager::TransmitAllMeasurements, this );
}
void
FiveGTransmissionManager::TransmitAllMeasurements( void )
{
this->SendAllMeasurements();
GainFunction::TTransmissionTime x = this->GetOptimalTime();
if ( x == 0 )
{
class ZeroTransmissionTime{};
throw ZeroTransmissionTime();
}
ns3::Simulator::Schedule( x, &FiveGTransmissionManager::TransmitAllMeasurements, this );
}
void
FiveGTransmissionManager::ReadConfiguration( void )
{
std::ifstream conf( "5GTM.txt" );
if ( !conf )
{
class FileOpenFailed{};
throw FileOpenFailed();
}
char comma;
if ( !( conf >> this->m_searchCount >> comma >> this->m_searchStart >> comma >> this->m_searchStop >> comma >> this->m_updateThreshold >> comma ) )
{
class ConfigurationFailed{};
throw ConfigurationFailed();
}
this->m_messageGainFunction = GainConfigureHelper::Configure( conf );
ApplicationConfigureHelper::Configure( this );
}
FiveGTransmissionManager::TGain
FiveGTransmissionManager::ProcessCValue( const TGain cValue ) const
{
return ( this->GetTotalGain( this->SolveForCValue( cValue ) ) );
}
GainFunction::TTransmissionTime
FiveGTransmissionManager::SolveForCValue( const TGain cValue ) const
{
GainFunction::TTransmissionTime xMin = this->m_searchStart;
GainFunction::TTransmissionTime xMax = this->m_searchStop;
GainFunction::TTransmissionTime xTrial = ( xMin + xMax ) / 2;
double yMin = this->GetTotalGainDerivative( xMin ) + cValue;
double yMax = this->GetTotalGainDerivative( xMax ) + cValue;
double yTrial = this->GetTotalGainDerivative( xTrial ) + cValue;
if ( yMin == 0 )
{
return ( xMin );
}
else if ( yMax == 0 )
{
return ( xMax );
}
else if ( yMin * yMax > 0 )
{
if ( yMin <= yMax )
{
return ( xMin );
}
else
{
return ( xMax );
}
}
for ( TCount count = 0; count < this->m_searchCount; ++count )
{
if ( yTrial == 0 )
{
break;
}
if ( yTrial * yMin < 0 )
{
xMax = xTrial;
}
else
{
xMin = xTrial;
yMin = this->GetTotalGainDerivative( xMin ) + cValue;
}
xTrial = ( xMin + xMax ) / 2;
yTrial = this->GetTotalGainDerivative( xTrial ) + cValue;
}
return ( xTrial );
}
void
FiveGTransmissionManager::ConnectToBaseStation( void )
{
BaseStation::TResponseVector myResponses;
auto numberOfCValues = BaseStation::GetBaseStation().cVector.size();
for ( auto cIndex = 0; cIndex < numberOfCValues; ++cIndex )
{
myResponses.push_back( this->ProcessCValue( BaseStation::GetBaseStation().cVector[cIndex] ) );
}
this->m_myResponses = myResponses;
this->m_cValue = &( BaseStation::GetBaseStation().RegisterMe( myResponses ) );
//std::cout << "Optimal C Value: " << *this->m_cValue << std::endl;
}