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
|
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double m=5; //determine the maximum backoff window, CWmax= pow(2,m)*CWmin
//since CWmax=1024, m equals 5
double W =32; //minimum contention window
double K=7; //K is the retransmission limit
double N=5; //number of node
double M=3; //number of non-zero power avaliable
int CPascal(int n , int r) { //The nCr function
int v = 1;
for (int i = 1; i <= r; i++)
v = v * (n + 1 - i) / i;
return v;
}
void NormalizedOfferedLoad(double a){ //a is just use for identifying the study case,
//i.e. if a =1, uniform distribution, a=2, with SPR
double lamda=0;
double v1;
double v2; //store the result of the nCr function
double Lpay= 4000; //packet payload in bits
double Rdata = 11 * pow(10, 6); //Tranmission rate
double σ = 20 * pow(10, -6);
double Pb=0,Ps=0,P1=0,P2=0,PE=0;
double T,Tv=0, Ts, Tc, Tdata, Tsifs, Tack,Tdifs;
double R,b=0; //γ is the collision probability experencied by a tagged node
double z,x,c; // The probability of choosing the channel to transmit the packet
double S=0; //Average Service Time
double L =4576; //packet size in bits
double gamma=0.2; //Let 1 be the initial guess of γ
double gamma2=0.2;
double sum=0;
double tau=0;
double ATPS=0; //ATPS is the attempt rate per slot, i.e. τ ′;
double normalizedOL=0.1;
double p ;
int count=0;
Tack = 304/(11*pow(10, 6));
Tsifs = 10 * pow(10, -6);
Tdifs = 50 * pow(10, -6);
Tdata =4.16* pow(10, -4);
//Tack = 3.04 * pow(10, -4);
while(fabs(gamma2-gamma)<0.0001){
lamda =(normalizedOL * Rdata)/(N*Lpay);
gamma =gamma2;
count++;
if(a==1){
PE = 1- (M* pow((1/M), 2));}
else{
PE= 1- pow(z, 2) + pow(x, 2) + pow(c, 2);
}
v1 = CPascal(N, 1);
v2 = CPascal(N, 2);
for(int i=0;i<=K;i++){ //R(γ)
R += pow(gamma,i);
}
for(int j=0;j<=K;j++){
if (j==0)
b= W/2;
else if(j>=1 && j<=m-1)
b=pow(2, j)* (W/2)* pow(gamma,j);
else if(j>=m &&j<=K){
b = pow(2, m) * (W/2) * pow(gamma,j);}
sum =sum+b;
}
ATPS = R/sum;
Ts = Tdata + Tsifs + Tack + Tdifs;
Tc = Tdata+Tack+Tsifs+Tdifs;
Tv = ((1-Pb)* σ)+ (Pb*Ps*(Ts+σ))+(Pb*(1-Ps)*(Tc+σ));
S = sum *Tv;
p = lamda*S;
if(p>1)
{
p=1;
}
tau = p* ATPS;
P1= v1 * tau * pow((1-tau), N-1);
P2= v2 * pow(tau, 2) * pow((1-tau), (N-2)) * PE;
Pb = 1-(pow((1-tau),N));
Ps = (P1 + P2)/ Pb;
gamma2 = 1- (pow(1-tau, N-1) - (N-1) * tau *pow(1-tau, N-2)*PE);
}
cout<<"Number of iteration: "<<count<<endl;
cout<<"gamma2: "<<gamma2<<endl;
cout<<"gamma: "<<gamma<<endl;
cout<<"Tv: "<<Tv<<endl;
cout<<"P1: "<<P1<<endl;
cout<<"P2: "<<P2<<endl;
cout<<"Pb: "<<Pb<<endl;
cout<<"Ps: "<<Ps<<endl;
cout<<"S: "<<S<<endl;
cout<<"p: "<<p<<endl;
cout<<"R: "<<R<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Attempt rate per slot: "<<ATPS<<endl;
cout<<"τ: "<<tau<<endl;
T = ((L*P1) + (2*L*P2))/Tv;
cout<<"System throughput: "<<T<<" bits per seond"<<endl;
//λ =(normalizedOL * Rdata)/(N*Lpay);
cout<<"Normalized Offered Load: "<<normalizedOL<<endl;
cout<<"λ: "<<lamda <<endl;
}
int main(int argc, const char * argv[]) {
NormalizedOfferedLoad(1);
return 0;
}
|