-
Notifications
You must be signed in to change notification settings - Fork 0
/
adp.cpp
288 lines (207 loc) · 5.81 KB
/
adp.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// adp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <conio.h>
#include <stdlib.h>
#include <chrono>
using namespace std;
class adp {
public:
void Initialization();
void GamaInitialization();
double GamaCalculation(int);
double UtilizationCalculation(int);
void ThreadingNodes();
//void adpStart(int);
};
/*variables*/
int numberOfNodes;
double noise;
double** Gain;
double* gama;
double maxOfPower;
double* currentPower; //current power of transmiters
double* price;
double* utilization;
const double bandWidth = 128;
thread* threads;
double* powerDifference;
const double stopThreshold = 0.00001;
int main()
{
adp a;
a.Initialization();
a.GamaInitialization();
a.ThreadingNodes();
getch();
return 0;
}
void adp::Initialization()
{
cout << "enter the number of nodesn : ";
cin >> numberOfNodes;
cout << "enter the amount of noise : ";
cin >> noise;
maxOfPower = noise* 10000; //
double* Xtransmiter = new double[numberOfNodes];
double* Ytransmiter = new double[numberOfNodes];
double* Xreciver = new double[numberOfNodes];
double* Yreciver = new double[numberOfNodes];
currentPower = new double[numberOfNodes];
gama = new double[numberOfNodes];
utilization = new double[numberOfNodes];
powerDifference = new double[numberOfNodes];
price = new double[numberOfNodes];
for (int i = 0; i < numberOfNodes; i++) {
/*((double)rand() / (RAND_MAX)) is a number between 0,1*/
/*random positioning*/
/*random positioning in (0,10) for transmiters*/
Xtransmiter[i] = ((double)rand() / (RAND_MAX)) * 10;
Ytransmiter[i] = ((double)rand() / (RAND_MAX)) * 10;
/*random positioning for recivers
((rand(0,1) - 0.5) * 6) => rand(-3,3) */
Xreciver[i] = Xtransmiter[i] + (((double)rand() / (RAND_MAX)- 0.5) * 6);
Yreciver[i] = Ytransmiter[i] + (((double)rand() / (RAND_MAX)- 0.5) * 6);
cout <<"transmiter ["<<i<<"] position is : ("<< Xtransmiter[i] <<" , " << Ytransmiter[i]
<< ")\n reciver ["<<i<<"] position is : (" << Xreciver[i] << " , " << Yreciver[i] << ") \n";
currentPower[i] = ((double)rand() / (RAND_MAX)) * maxOfPower;
price[i] = (1/(noise*bandWidth))*((double)rand() / (RAND_MAX));
powerDifference[i] = 1;
}
Gain = new double*[numberOfNodes];
for (int i = 0; i < numberOfNodes; i++) {
Gain[i] = new double[numberOfNodes];
}
for (int i = 0; i < numberOfNodes; i++)
{
for (int j = 0; j < numberOfNodes; j++)
{
/*calcute destanse between trasmiter (i) and reciver (j)*/
double distance = pow((pow((Xtransmiter[i]- Xreciver[j]),2) + pow((Ytransmiter[i] - Yreciver[j]),2 )),0.5);
/*gain of trasmiter i and reciver j*/
Gain[i][j] = pow(distance, -4);
}
}
}
void adp::GamaInitialization()
{
adp a;
for (int i = 0; i < numberOfNodes; i++)
{
gama[i] = a.GamaCalculation(i);
}
}
double adp::UtilizationCalculation(int i)
{
return( log(gama[i]));
}
double adp::GamaCalculation(int i) {
double sigI = 0;
for (int j = 0; j < numberOfNodes; j++)
{
sigI += currentPower[j] * Gain[j][i];
}
return((currentPower[i] * Gain[i][i]) / (noise + (1 / bandWidth)*sigI));
}
void ADPNode(int i)
{
while (true)
{
double maxDiffr = 0;
for (int i = 0; i < numberOfNodes; i++)
{
if (maxDiffr < powerDifference[i])
maxDiffr = powerDifference[i];
}
/*max deferences between powers is lower than threshold??*/
if (maxDiffr < stopThreshold)
{
break;
}
/*each thread (transmiter) wait for a random miliseconds
(for simulate the asynchronous defination)*/
std::this_thread::sleep_for(std::chrono::milliseconds((rand()) % 1000));
double someOfPrice = 0;
double someOfInterference = 0;
for (int j = 0; j < numberOfNodes; j++)
{
if (i != j)
{
someOfPrice += price[i] * Gain[j][i];
someOfInterference += currentPower[j] * Gain[i][j];
}
}
double newPower = 1/someOfPrice;
if (newPower <= maxOfPower && newPower >= 0)
{
powerDifference[i] = (abs(newPower - currentPower[i])) / currentPower[i];
currentPower[i] = newPower;
}
else if (newPower > maxOfPower)
{
powerDifference[i] = (abs(maxOfPower - currentPower[i])) / currentPower[i];
currentPower[i] = maxOfPower;
}
else
{
powerDifference[i] = 1;
currentPower[i] = 0;
}
price[i] = 1/ (bandWidth*noise +someOfInterference);
adp a;
gama[i] = a.GamaCalculation(i);
utilization[i] = a.UtilizationCalculation(i);
}
}
void outputTask()
{
double finalUtilization;
while (true)
{
double correntUtilization = 0;
double maxDeffr = 0;
for (int i = 0; i < numberOfNodes; i++)
{
correntUtilization += utilization[i];
if (maxDeffr < powerDifference[i])
maxDeffr = powerDifference[i];
}
if (maxDeffr < stopThreshold)
{
finalUtilization = correntUtilization;
break;
}
cout << "\n utilization = " << correntUtilization;// << " & max defference in is : " << maxcon;
/*make output each 100ms*/
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
cout << "\n\n\n\t\t\t the final utilization is : "
<< finalUtilization << "\n\n ";
for (int i = 0; i < numberOfNodes; i++)
{
cout << "\n power["<<i<<"] = " << currentPower[i];
}
cout << "\n\n\n finished\n\n";
}
void adp::ThreadingNodes()
{
using std::thread;
threads = new thread[numberOfNodes];
thread outputThread(outputTask);
for (int i = 0; i < numberOfNodes; i++)
{
// define each thread to run ADPNode(i)
threads[i] = thread(ADPNode, i);
}
for (int i = 0; i < numberOfNodes; i++)
{
//start threads
threads[i].join();
}
outputThread.join();
}