In: Statistics and Probability
write a program in matlab to produce a discrete event simulation of a switching element with 10 inputs and 3 outputs. Time is slotted on all inputs and outputs. Each input packet follows a Bernoulli process. In a given slot, the independent probability that a packet arrives in a slot is p and the probability that a slot is empty is (1– p). One packet fills one slot. For a switching element if 3 or less packets arrives to some inputs, they are forwarded to the switching element outputs without a loss. If more than 3 packets arrive to the inputs of the switching element, only 3 packets are randomly chosen to be forwarded to the switching element outputs and the remaining ones are discarded. In your simulation the program will mimic the operation of the switch and collect statistics. That is, in each time slot the program randomly generates packets for all inputs of the switching element and counts how many packets can be passed to the output of the switching element (causing throughput) and, alternatively counts how many packets are dropped (when the switching element has more than 3 input packets at a given time slot) . Your task is to collect throughput statistics for different values of p (p = 0.05, 0.1 up to 1.0 in steps of 0.05), by running the procedure described above for each value of p and for many slots (at least a thousand slots per value of p). The more simulated slots, the more accurate the results will be. Based on this statistics, plot two graphs: 1) the average number of busy outputs versus p, and 2) the average number of dropped packets versus p.
outputs:
code in text format:
//Include the required header files.
#include <iostream>
#include <random>
#include <iomanip>
#include<time.h>
using namespace std;
//Define the array and variables
float p1[10]={};
float d1[10]={};
int l=0;
//Define the function BernoulliProcess()
int BernoulliProcess(float p, int packetGen)
{
//Define the variables
int passed =0;
int count=0, drop =0;
//Use the bernoulli's distribution.
default_random_engine generator;
bernoulli_distribution distribution(p);
//Iterate and generate the distribution.
//Increase the count.
for (int i=0; i<packetGen; ++i)
{
if (distribution(generator))
{
++count;
}
}
//Evaluate the passed packets from the count.
if(count >3)
passed = 3;
else
passed =count;
//Evaluate the dropped packets from the passed packets.
drop = packetGen-passed;
//Display the Probability, passed and dropped packets.
cout<<"Probability:"<<p<<endl;
cout<<"passed:"<<passed<<endl;
cout<<"Drop:"<<drop<<endl<<endl;
//Return the passed packets.
return passed;
}
//Define the main function.
int main()
{
//Generate the numbewr of packets in range 1 to 10.
srand(time(NULL));
int packetGen = rand() % (10 - 1 + 1) + 1;
//Dispaly the packet generated.
cout<<"Packet generated:"<<packetGen<<endl<<endl;
//Define the variables and array.
float p =0.05,d=0.5;
int p2[50]={},d2[50]={};
//collect throughput statistics for different values
//of p (p = 0.05, 0.1 up to 1.0 in steps of 0.05),
for(int j=0; j<50;j++)
{
//Evaluate the passed and dropped packets.
//and Probabilities.
p2[j]=BernoulliProcess(p, packetGen);
d2[j]= 10-p2[j];
p= p+0.05;
d =1-p;
}
int k=0;
//Display the average number of busy outputs versus p.
cout<<"The average number of busy outputs versus p:"<<endl;
for (float i=0.05; i<0.95; i=i+0.05)
{
k++;
cout <<setprecision(2)<<fixed<< i << "-" << (i+0.05) << ": ";
for(int j=0;j<p2[k];j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
k=0;
//Display the average number of dropped packets versus p.
cout<<"The average number of dropped packets versus p:"<<endl;
for (float i=0.05; i<0.95; i=i+0.05 )
{
k++;
cout <<setprecision(2)<<fixed<< i << "-" << (i+0.05) << ": ";
for(int j=0;j<d2[k];j++)
cout<<"*";
cout<<endl;
}
//Return the value 0.
return 0;
}