-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMultihopTransmissionManager.cpp
executable file
·150 lines (125 loc) · 4.15 KB
/
MultihopTransmissionManager.cpp
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
//
// MultihopTransmissionManager.cpp
// AggregationNS3
//
// Created by Alper Sinan Akyurek on 8/27/16.
// Copyright © 2016 Alper Sinan Akyurek. All rights reserved.
//
#include "MultihopTransmissionManager.hpp"
#include "ApplicationConfigureHelper.hpp"
NS_LOG_COMPONENT_DEFINE("MultihopTransmissionManager");
NS_OBJECT_ENSURE_REGISTERED(MultihopTransmissionManager);
ns3::TypeId
MultihopTransmissionManager::GetTypeId( void )
{
static ns3::TypeId tid = ns3::TypeId( "MultihopTransmissionManager" )
.SetParent<ns3::Object> ()
.AddConstructor<MultihopTransmissionManager> ()
;
return ( tid );
}
MultihopTransmissionManager::MultihopTransmissionManager( void ) : m_searchCount( 20 ),
m_searchStart( ns3::Seconds( 0.1 ) ),
m_searchStop( ns3::Seconds( 20 ) )
{
}
MultihopTransmissionManager::TGain
MultihopTransmissionManager::GetTotalGain( const GainFunction::TTransmissionTime & x ) const
{
TGain totalGain = 0;
TGain totalGainDerivative = 0;
for ( auto i = this->m_applications.begin(); i != this->m_applications.end(); ++i )
{
totalGain += ( *i )->GetMeasurementGain( x );
totalGainDerivative += ( *i )->GetMeasurementGainDerivative( x );
//std::cout << "Gains: " << ( *i )->GetMeasurementGain( x ) << ", " << ( *i )->GetMeasurementGainDerivative( x ) << std::endl;
}
// std::cout << "Total Gains: " << totalGain << ", " << totalGainDerivative << std::endl;
TGain totalDerivative = totalGain * this->m_messageGainFunction->GetGainDerivative( x ) +
totalGainDerivative * this->m_messageGainFunction->GetGain( x );
// std::cout << "Resulting expression: " << totalDerivative << std::endl;
return ( totalDerivative );
}
GainFunction::TTransmissionTime
MultihopTransmissionManager::GetOptimalTime( void ) const
{
GainFunction::TTransmissionTime xMin = this->m_searchStart;
GainFunction::TTransmissionTime xMax = this->m_searchStop;
GainFunction::TTransmissionTime xTrial = ( xMin + xMax ) / 2;
double yMin = this->GetTotalGain( xMin );
double yMax = this->GetTotalGain( xMax );
double yTrial = this->GetTotalGain( xTrial );
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->GetTotalGain( xMin );
}
xTrial = ( xMin + xMax ) / 2;
yTrial = this->GetTotalGain( xTrial );
}
// std::cout << xTrial << std::endl;
return ( xTrial );
}
void
MultihopTransmissionManager::Start( void )
{
GainFunction::TTransmissionTime x = this->GetOptimalTime() - this->m_hopFactor + ns3::Seconds( 0.1 * ((double)rand() / (double)RAND_MAX ) );
ns3::Simulator::Schedule( x, &MultihopTransmissionManager::TransmitAllMeasurements, this );
}
void
MultihopTransmissionManager::TransmitAllMeasurements( void )
{
this->SendAllMeasurements();
GainFunction::TTransmissionTime x = this->GetOptimalTime();
if ( x== 0 )
{
throw "No gain function";
}
ns3::Simulator::Schedule( x, &MultihopTransmissionManager::TransmitAllMeasurements, this );
}
void
MultihopTransmissionManager::ReadConfiguration( void )
{
std::ifstream conf( "multihopTM.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_hopFactor ) )
{
class ConfigurationFailed{};
throw ConfigurationFailed();
}
ApplicationConfigureHelper::Configure( this );
}