You are on page 1of 5

// Mohammed Taskeen

// mxt088000
#include <iostream>
#include <fstream>
#include <iomanip>
#include "rv.h"
#include "event.h"
int main()
{
using namespace std;
EventList Elist; // Create event list
enum {ARR1, ARR2, ARR3, DEP1, DEP2, DEP3}; // Define the event
types
double a1 = 1.0/2.0;
double a2 = 1.0/4.0;
double a3 = 1.0/4.0;
double r1 = 1.0/2.0;
double r2 = 2.0/3.0;
double r3 = 1.0/4.0;
double lambda = 0.0; // Arrival rate
double mu = 0.0; // Service rate
cout<< "Enter the service rate(mu): ";
cin>>mu;
double EN[3];
int N1,N2,N3;//Number of custoemrs in the queue 1,2,3
int flag; //to indicate a job coming from a different queue
int n1,n2,n3;
double random_no;
double clock; // System clock
int Ndep; // Number of departures from system
double EN1,EN2,EN3; // For calculating E[N]
double U1,U2,U3; // Utilization 1,2,3
double prev1,prev2,prev3; //to hold individual clock values
double theta1,theta2,theta3; //values of theta1,2,3
double rho1,rho2,rho3; // values of rho 1,2 and 3
ofstream ExNo1,ExNo2,ExNo3; // files to hold EN values of queue 1,2,3
ExNo1.open("ExNo1.csv");
ExNo1<<"lambda,Simulation,Theoretical"<<endl;
ExNo2.open("ExNo2.csv");
ExNo2<<"lambda,Simulation,Theoretical"<<endl;
ExNo3.open("ExNo3.csv");
ExNo3<<"lambda,Simulation,Theoretical"<<endl;
int done; // End condition satisfied?
Event* CurrentEvent;
for(int i=0;i<9;i++)
{
N1=0; N2=0; N3=0;
EN1=0.0; EN2=0.0; EN3=0.0;
clock=0.0;
prev1=0.0,prev2=0.0,prev3=0.0;
Ndep=0;
done=0;
random_no=0.0;
n1=0,n2=0,n3=0;
EN[ARR1]=0.0;EN[ARR2]=0.0;EN[ARR3]=0.0;
U1=0.0,U2=0.0,U3=0.0;
theta1=0.0,theta2=0.0,theta3=0.0;
rho1=0.0,rho2=0.0,rho3=0.0;
flag=0;
lambda = i+1;
Elist.insert(exp_rv(lambda*a1),ARR1);
Elist.insert(exp_rv(lambda*a2),ARR2);
Elist.insert(exp_rv(lambda*a3),ARR3);
while (!done)
{
CurrentEvent = Elist.get();
clock = CurrentEvent->time;
switch(CurrentEvent->type)
{
case ARR1:
n1++;
EN[ARR1] += N1*(clock-prev1);
if(N1>0)
U1+=(clock-prev1);
N1++;
if(N1==1)
{
Elist.insert(clock+exp_rv(mu),DEP1); // gene
rate its departure event
}
if(!flag)
{
Elist.insert(clock+exp_rv(lambda*a1),ARR1);
}
else
flag=0;
prev1 = clock;
break;
case ARR2:
n2++;
if(N2>0)
U2+=(clock-prev2);
EN[ARR2] += N2*(clock-prev2);
N2++;
if(N2==1)
Elist.insert(clock+exp_rv(mu),DEP2); // gene
rate its departure event
if(!flag)
Elist.insert(clock+exp_rv(lambda*a2),ARR2);
else
flag=0;
prev2 = clock;
break;
case ARR3:
n3++;
EN[ARR3] += N3*(clock-prev3);
if(N3>0)
U3+=(clock-prev3);
N3++;
if(N3==1)
Elist.insert(clock+exp_rv(mu),DEP3); // gene
rate its departure event
if(!flag)
Elist.insert(clock+exp_rv(lambda*a3),ARR3);
else
flag=0;
prev3= clock;
break;
case DEP1:
EN[ARR1] += N1*(clock-prev1);
if(N1>0)
U1+=(clock-prev1);
N1--;
Ndep++;
if(N1>0)
{
Elist.insert(clock+exp_rv(mu),DEP1);
}
random_no=uni_rv();
if((1-r1)>random_no)
{
Elist.insert(clock,ARR2);
flag=1;
}
prev1 = clock;
break;
case DEP2:
EN[ARR2] += N2*(clock-prev2);
if(N2>0)
U2+=(clock-prev2);
N2--;
Ndep++;
if(N2>0)
{
Elist.insert(clock+exp_rv(mu),DEP2);
}
random_no=uni_rv();
if((1-r2)>random_no)
{
Elist.insert(clock,ARR3);
flag=1;
}
prev2 = clock;
break;
case DEP3:
EN[ARR3] += N3*(clock-prev3);
if(N3>0)
U3+=(clock-prev3);
N3--;
Ndep++;
if(N3>0)
{
Elist.insert(clock+exp_rv(mu),DEP3);
}
random_no=uni_rv();
if((1-r3)>random_no)
{
Elist.insert(clock,ARR1);
flag=1;
}
prev3 = clock;
break;
}
delete CurrentEvent;
if (Ndep > 500000) done=1; // End condition
}
theta1 = ((lambda*a1)+(lambda*a3*(1-r3))+((1-r2)*lambda*a2*(1-r3)))/(1-(
(1-r2)*(1-r1)*(1-r3)));
theta2 = (lambda*a2) + (theta1*(1-r1));
theta3 = (lambda*a3) + (theta2*(1-r2));
rho1 = theta1/mu;
rho2 = theta2/mu;
rho3 = theta3/mu;
EN1 = rho1/(1-rho1);
EN2 = rho2/(1-rho2);
EN3 = rho3/(1-rho3);
cout<<endl;
cout<<"Lambda: "<<(int)lambda<<endl;
cout.setf(ios::fixed);
if(lambda==8)
{
cout<<"Throughput of queue 1(simulation): " <<setprecision(4)<<(doub
le)n1/prev1<<endl;
cout<<"Throughput of queue 1(theory): "<<setprecision(4)<<(doubl
e)theta1<<endl;
cout<<"Throughput of queue 2(simulation): " <<setprecision(4)<<(doub
le)n2/prev2<<endl;
cout<<"Throughput of queue 2(theory): "<<setprecision(4)<<(double)th
eta2<<endl;
cout<<"Throughput of queue 3(simulation): " <<setprecision(4)<<(
double)n3/prev3<<endl;
cout<<"Throughput of queue 3(theory): "<<setprecision(4)<<(doubl
e)theta3<<endl;
cout<<"Utilization of queue 1(simulation): "<<setprecision(4)<<(
double)U1/prev1<<endl;
cout<<"Utilization of queue 1(theory): "<<setprecision(4)<<(doub
le)rho1<<endl;
cout<<"Utilization of queue 2(simulation): "<<setprecision(4)<<(
double)U2/prev2<<endl;
cout<<"Utilization of queue 2(theory): "<<setprecision(4)<<(doub
le)rho2<<endl;
cout<<"Utilization of queue 3(simulation): "<<setprecision(4)<<(
double)U3/prev3<<endl;
cout<<"Utilization of queue 3(theory): "<<setprecision(4)<<(doub
le)rho3<<endl;
}
cout<<"Expected number of customers in queue 1 (simulation): "<<setpreci
sion(4)<<EN[ARR1]/prev1<<endl;
cout<<"Expected number of customers in queue 1 (theory): "<<setprecision
(4)<<EN1<<endl;
ExNo1<<lambda<<","<<EN[ARR1]/prev1<<","<<EN1<<endl;
cout<<"Expected number of customers in queue 2 (simulation): "<<setpreci
sion(4)<<EN[ARR2]/prev2<<endl;
cout<<"Expected number of customers in queue 2 (theory): "<<setprecision
(4)<<EN2<<endl;
ExNo2<<lambda<<","<<EN[ARR2]/prev2<<","<<EN2<<endl;
cout<<"Expected number of customers in queue 3 (simulation): "<<setpreci
sion(4)<<EN[ARR3]/prev3<<endl;
cout<<"Expected number of customers in queue 3 (theory): "<<setprecision
(4)<<EN3<<endl;
ExNo3<<lambda<<","<<EN[ARR3]/prev3<<","<<EN3<<endl;
}
cout<<"Simulation Completed"<<endl;
}

You might also like