In: Computer Science
You are required to modify the attached simulation program. This program currently uses an array to implement the queue. You will modify the program so that it uses the STL queue instead.
#include <iostream> using namespace std; #include <math.h> #include<fstream> double time = 0; int seed = 1; /* External definitions for single-server queeing system. */ const int Q_LIMIT = 100000; /* Limit on queue length. */ const int BUSY = 1; /* Mnemonics for server's being busy */ const int IDLE = 0; /* and idel. */ const double infinity = 1.0e+29; const int max_int = 2147483647; //Define generators ifstream infile; ofstream outfile; int next_event_type, num_custs_delayed, num_delays_required, num_events, num_in_q, server_status; double area_num_in_q, area_server_status, mean_interarrival, mean_service, clock, time_arrival[Q_LIMIT + 1], time_last_event, time_next_event[3], total_of_delays, arrivaltime, lambda; double asum = 0; //accumulates arrival times double ssum = 0; //accumulates service time long int num_arrived = 0; //number of arrivals long int num_serviced = 0; //number of customers serviced //Function Prototypes void initialize(); void timing(); void arrive(); void depart(); void report(); void update_time_avg_stats(); double arrival_time(double tau); // generate time of next arrival double service(double s);// : real; { generate a service time } //Main function int main() /* Main function. */ { /* Open input and output files. */ infile.open("mm1.in"); outfile.open("mm1.out"); /* Specify the number of events for the timimg function. */ num_events = 2; /* Read input parameters. */ cout << "please enter the mean interarrival time >>"; cin >>mean_interarrival; cout << "please enter the mean service time >>"; cin>>mean_service; cout << "please enter the number of customers>>"; cin >> num_delays_required; lambda = 1 /mean_interarrival; /* Write report heading and input parameters. */ outfile << "Single-server queueing system "<<endl<<endl; outfile << "Mean interarrival time" << mean_interarrival << "minutes "<<endl<<endl; outfile << "Mean service time "<< mean_service << " minutes" << endl<<endl; outfile << "Number of customers "<< num_delays_required <<endl<<endl; /* Initialize the simulation. */ initialize(); /* Run the simulation while more delays are still needed. */ for(int i = 0; i < num_delays_required;i++) { /* Determine the next event. */ timing(); /* Update time-average statistical accumulators. */ update_time_avg_stats(); /* Invoke the appropriate event funtions. */ switch (next_event_type) { case 1: arrive(); break; case 2: depart(); break; } } /* Invoke the report generator and eand the simulation. */ report(); infile.close(); outfile.close(); return 0; } void initialize() /* Initialization function. */ { /* Initialize the simulation clock. */ time = 0.0; /* Initialize the state variables. */ server_status = IDLE; num_in_q = 0; time_last_event = 0.0; /* Initialize the statistical counters. */ num_custs_delayed = 0; total_of_delays = 0.0; area_num_in_q = 0.0; area_server_status = 0.0; /* Initialize event list. Since no customers are present, the departure (service completion) event is eliminated from consideration. */ time_next_event[1] = time + arrival_time(mean_interarrival);//schedule first arrival time_next_event[2] = infinity; //first event cannot be departure } void timing() /* Timing function. */ { int i; double min_time_next_event = infinity; next_event_type = 0; /* Determine the event type of the next event to occur. */ for (i = 1; i<= num_events; ++i) { if (time_next_event[i] < min_time_next_event) { min_time_next_event = time_next_event[i]; next_event_type = i; } } /* Check to see whether the event list is empty. */ if (next_event_type ==0) { /* The event list is empty, so stop the simulation. */ outfile << endl <<"Event list empty at time "<< time; exit(1); } /* The event list is not empty, so advance the simulation clock. */ time = min_time_next_event; } void arrive() /* Arrival event functin. */ { double delay; /* Schedule next arrival. */ time_next_event[1] = time + arrival_time(mean_interarrival);//expon(mean_interarrival) /* Check to see whether server is busy. */ if (server_status == BUSY) { /* Serer is busy, so increment number of customers in queue. */ ++num_in_q; /* Check to see whether an overflow condition exists. */ if (num_in_q > Q_LIMIT) { /* The queue has overflowed, so stop the simulation. */ outfile<<endl<< "Overflow of the array time_arrival at"; outfile<<" time "<< time<< endl; exit(2); } /* There is still room in the queue, so store the time of arrival of the arriving customer at the (new) end of time_arrival. */ time_arrival[num_in_q] = time; } else { /* Server is idle, so arriving customer has a delay of zero. (The following two statements are for program clarity and do not affect the results of the simulaiton.) */ delay = 0.0; total_of_delays += delay; /* Increment the number of customers delayed, and make server busy. */ ++num_custs_delayed; server_status = BUSY; /* Schedule a departure (service completion). */ time_next_event[2] = time + service(mean_service); } } void depart() /* Departure event function. */ { int i; double delay; /* Check to see whether the queue is empty. */ if (num_in_q == 0) { /* The queue is empty so make the server idle and eliminate the departure (service completion) event form consideration. */ server_status = IDLE; time_next_event[2] = 1.0e+30; } else { /* The queue is nonempty, so decrement the number customers in queue. */ --num_in_q; /* Compute the delay of the customer who is beginning service and update the total delay accumulator. */ delay = time - time_arrival[1]; total_of_delays += delay; /* Increment the number of customers delayed, and schedule departure. */ ++num_custs_delayed; time_next_event[2] = time + service(mean_service); /* Move each customer in queue (if any) up one place. */ for (i = 1; i <= num_in_q; ++i) time_arrival[i] = time_arrival[i + 1]; } } void report() /* Report generator function. */ { /* Compute and write estimates of desired measures of performance. */ cout <<"number of customers delayed = "<<num_custs_delayed<<endl; outfile << endl<<endl<<"Average delay in queue "<< total_of_delays / num_custs_delayed << "minutes " <<endl<<endl; outfile << "Average number in queue " << area_num_in_q / time<<endl<<endl; outfile << "Server utilization " << area_server_status / time << endl<<endl; outfile << "Time simulation ended " << time << endl; outfile << "Average arrival time = "<< asum/num_arrived<<endl; outfile << "Average service time = "<< ssum/num_serviced<<endl; outfile << "Total number of arrivals = " << num_arrived <<endl; outfile << "Total number serviced = " << num_serviced <<endl; outfile << endl<<endl<<"Theoretical results "<<endl<<endl<<endl; double rho = mean_service/mean_interarrival; outfile << "Average delay in queue " <<(rho * rho) / ( lambda * (1 - rho)) <<endl; outfile << "Average number in queue " <<(rho * rho) / ( 1 - rho) <<endl; outfile << "Server utilization "<<rho <<endl; outfile << "Average number in system" << rho / (1 - rho) <<endl; outfile << "The total wait time = " << (mean_service * mean_interarrival)/ (mean_interarrival- mean_service) << endl; } void update_time_avg_stats() /* Update area accumulators for time-average statistics. */ { double time_since_last_event; /* Compute time since last event, and updat last-event-time marker. */ time_since_last_event = time - time_last_event; time_last_event = time; /* Update area under number-in-queue function. */ area_num_in_q += num_in_q * time_since_last_event; /* Update area under server-busy indicator function. */ area_server_status += server_status * time_since_last_event; } //Generates a random number between 0 and 1 double random() { long int a = 16807; long int q = 127773; long int r = 2836; long int lo, hi, t ; hi = seed / q; lo = seed - q * hi; t = a * lo - r * hi; if (t > 0) seed = t; else seed = t + max_int; return (double)seed / max_int; }//end; {random} //Generates an exponentially distributed variable with mean mu double exponential(double mu) // returns an exponentially distributed random deviate { double u; double temp; u = random(); temp = -mu * log(1 - u); return temp; } //generates an exponentially distributed service time with mean //function service : real; { generate a service time } double service(double s)// : real; { generate a service time } { double stime; stime = exponential(s); ssum += stime; num_serviced ++; return stime; } //generates an exponentially distributed arrival time with mean "tau" } double arrival_time(double tau) // generate time of next arrival { double atime; atime = exponential(tau); asum += atime; num_arrived ++; return atime;; // get the next arrival time }//arrival_time
I have commented all the old codes for array based queue.
time variable is changed to time_d
clock variable is changed to clock_d
#include <iostream>
using namespace std;
#include <math.h>
#include<fstream>
#include<queue>
double time_d = 0;
int seed = 1;
/* External definitions for single-server queeing system. */
const int Q_LIMIT = 100000; /* Limit on queue length. */
const int BUSY = 1; /* Mnemonics for server's being busy */
const int IDLE = 0; /* and idel. */
const double infinity = 1.0e+29;
const int max_int = 2147483647;
//Define generators
ifstream infile;
ofstream outfile;
int next_event_type, num_custs_delayed, num_delays_required,
num_events, num_in_q, server_status;
double area_num_in_q, area_server_status, mean_interarrival,
mean_service, clock_d,
time_last_event, time_next_event[3], total_of_delays, arrivaltime,
lambda;
double asum = 0; //accumulates arrival times
double ssum = 0; //accumulates service time
long int num_arrived = 0; //number of arrivals
long int num_serviced = 0; //number of customers serviced
queue<double> time_arrival;
//Function Prototypes
void initialize();
void timing();
void arrive();
void depart();
void report();
void update_time_avg_stats();
double arrival_time(double tau); // generate time of next arrival
double service(double s);// : real; { generate a service time }
//Main function
int main() /* Main function. */
{
/* Open input and output files. */
infile.open("mm1.in");
outfile.open("mm1.out");
/* Specify the number of events for the timimg function. */
num_events = 2;
/* Read input parameters. */
cout << "please enter the mean interarrival time >>";
cin >>mean_interarrival;
cout << "please enter the mean service time >>";
cin>>mean_service;
cout << "please enter the number of customers>>";
cin >> num_delays_required;
lambda = 1 /mean_interarrival;
/* Write report heading and input parameters. */
outfile << "Single-server queueing system "<<endl<<endl;
outfile << "Mean interarrival time" << mean_interarrival << "minutes "<<endl<<endl;
outfile << "Mean service time "<< mean_service << " minutes" << endl<<endl;
outfile << "Number of customers "<< num_delays_required <<endl<<endl;
/* Initialize the simulation. */
initialize();
/* Run the simulation while more delays are still needed. */
for(int i = 0; i < num_delays_required;i++)
{
/* Determine the next event. */
timing();
/* Update time-average statistical accumulators. */
update_time_avg_stats();
/* Invoke the appropriate event funtions. */
switch (next_event_type)
{
case 1:
arrive();
break;
case 2:
depart();
break;
}
}
/* Invoke the report generator and eand the simulation. */
report();
infile.close();
outfile.close();
return 0;
}
void initialize() /* Initialization function. */
{
/* Initialize the simulation clock. */
time_d = 0.0;
/* Initialize the state variables. */
server_status = IDLE;
num_in_q = 0;
time_last_event = 0.0;
/* Initialize the statistical counters. */
num_custs_delayed = 0;
total_of_delays = 0.0;
area_num_in_q = 0.0;
area_server_status = 0.0;
/* Initialize event list. Since no customers are present, the
departure (service completion) event is eliminated from
consideration. */
time_next_event[1] = time_d + arrival_time(mean_interarrival);//schedule first arrival
time_next_event[2] = infinity; //first event cannot be departure
}
void timing() /* Timing function. */
{
int i;
double min_time_next_event = infinity;
next_event_type = 0;
/* Determine the event type of the next event to occur. */
for (i = 1; i<= num_events; ++i)
{
if (time_next_event[i] < min_time_next_event)
{
min_time_next_event = time_next_event[i];
next_event_type = i;
}
}
/* Check to see whether the event list is empty. */
if (next_event_type ==0)
{
/* The event list is empty, so stop the simulation. */
outfile << endl <<"Event list empty at time "<< time_d;
exit(1);
}
/* The event list is not empty, so advance the simulation clock. */
time_d = min_time_next_event;
}
void arrive() /* Arrival event functin. */
{
double delay;
/* Schedule next arrival. */
time_next_event[1] = time_d +
arrival_time(mean_interarrival);//expon(mean_interarrival)
/* Check to see whether server is busy. */
if (server_status == BUSY)
{
time_arrival.push(time_d);
// /* Serer is busy, so increment number of customers in queue.
// */
//
// ++num_in_q;
//
// /* Check to see whether an overflow condition exists. */
//
// if (num_in_q > Q_LIMIT)
// {
//
// /* The queue has overflowed, so stop the simulation. */
//
// outfile<<endl<< "Overflow of the array time_arrival at";
// outfile<<" time "<< time_d<< endl;
// exit(2);
// }
//
// /* There is still room in the queue, so store the time of
// arrival of the arriving customer at the (new) end of
// time_arrival. */
//
// time_arrival[num_in_q] = time_d;
}
else {
/* Server is idle, so arriving customer has a delay of zero.
(The following two statements are for program clarity and do
not affect the results of the simulaiton.) */
delay = 0.0;
total_of_delays += delay;
/* Increment the number of customers delayed, and make server
busy. */
++num_custs_delayed;
server_status = BUSY;
/* Schedule a departure (service completion). */
time_next_event[2] = time_d + service(mean_service);
}
}
void depart() /* Departure event function. */
{
// int i;
double delay;
/* Check to see whether the queue is empty. */
if (num_in_q == 0)
{
/* The queue is empty so make the server idle and eliminate the
departure (service completion) event form consideration. */
server_status = IDLE;
time_next_event[2] = 1.0e+30;
}
else
{
/* The queue is nonempty, so decrement the number customers
in queue. */
// --num_in_q;
/* Compute the delay of the customer who is beginning service
and update the total delay accumulator. */
delay = time_d - time_arrival.front();
total_of_delays += delay;
/* Increment the number of customers delayed, and schedule
departure. */
++num_custs_delayed;
time_next_event[2] = time_d + service(mean_service);
/* Move each customer in queue (if any) up one place. */
time_arrival.pop();
// for (i = 1; i <= num_in_q; ++i)
// time_arrival[i] = time_arrival[i + 1];
}
}
void report() /* Report generator function. */
{
/* Compute and write estimates of desired measures of performance.
*/
cout <<"number of customers delayed = "<<num_custs_delayed<<endl;
outfile << endl<<endl<<"Average delay in queue "<< total_of_delays /
num_custs_delayed
<< "minutes " <<endl<<endl;
outfile << "Average number in queue " << area_num_in_q / time_d<<endl<<endl;
outfile << "Server utilization " << area_server_status / time_d <<
endl<<endl;
outfile << "Time simulation ended " << time_d << endl;
outfile << "Average arrival time = "<< asum/num_arrived<<endl;
outfile << "Average service time = "<< ssum/num_serviced<<endl;
outfile << "Total number of arrivals = " << num_arrived <<endl;
outfile << "Total number serviced = " << num_serviced <<endl;
outfile << endl<<endl<<"Theoretical results "<<endl<<endl<<endl;
double rho = mean_service/mean_interarrival;
outfile << "Average delay in queue " <<(rho * rho) / ( lambda * (1 -
rho)) <<endl;
outfile << "Average number in queue " <<(rho * rho) / ( 1 - rho)
<<endl;
outfile << "Server utilization "<<rho <<endl;
outfile << "Average number in system" << rho / (1 - rho)
<<endl;
outfile << "The total wait time = " << (mean_service * mean_interarrival)/
(mean_interarrival- mean_service) << endl;
}
void update_time_avg_stats() /* Update area accumulators for
time-average statistics. */
{
double time_since_last_event;
/* Compute time since last event, and updat last-event-time
marker. */
time_since_last_event = time_d - time_last_event;
time_last_event = time_d;
/* Update area under number-in-queue function. */
area_num_in_q += num_in_q * time_since_last_event;
/* Update area under server-busy indicator function. */
area_server_status += server_status * time_since_last_event;
}
//Generates a Random number between 0 and 1
double Random()
{
long int a = 16807;
long int q = 127773;
long int r = 2836;
long int lo, hi, t ;
hi = seed / q;
lo = seed - q * hi;
t = a * lo - r * hi;
if (t > 0)
seed = t;
else
seed = t + max_int;
return (double)seed / max_int;
}//end; {Random}
//Generates an exponentially distributed variable with mean mu
double exponential(double mu) // returns an exponentially distributed Random deviate
{
double u;
double temp;
u = Random();
temp = -mu * log(1 - u);
return temp;
}
//generates an exponentially distributed service time with mean
//function service : real; { generate a service time }
double service(double s)// : real; { generate a service time }
{
double stime;
stime = exponential(s);
ssum += stime;
num_serviced ++;
return stime;
}
//generates an exponentially distributed arrival time with mean "tau" }
double arrival_time(double tau) // generate time of next arrival
{
double atime;
atime = exponential(tau);
asum += atime;
num_arrived ++;
return atime;; // get the next arrival time
}//arrival_time