Question

In: Computer Science

You are required to modify the attached simulation program. This program currently uses an array to...

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

Solutions

Expert Solution

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


Related Solutions

You are required to modify the attached simulation program. This program currently uses an array to...
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. /* This is for CSC 611 class at NSU Computer Science Department This program is a modified C++ version of the C program From the Second Edition Simulation Modeling & Analysis Averill M. Law W. David Kelton */ #include using namespace std; #include #include double time = 0;...
In this second case you will modify the simulation to make it more realistic. In the...
In this second case you will modify the simulation to make it more realistic. In the natural environment, not all genotypes have the same rate of survival; that is, the environment might favor some genotypes while selecting against others. An example is the human condition sickle-cell anemia. It is a condition caused by a mutation on one allele, in which a homozygous recessive does not survive to reproduce. For this simulation you will assume that the homozygous recessive (ff) individuals...
Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Write a program that uses an array for time and temperature. The program should contain an...
Write a program that uses an array for time and temperature. The program should contain an array with 24 elements, each of which is holding a temperature for a single hour. Your program should have a function that lists all of the temperatures that are held in the array. Temperatures should be listed to console with one entry per line. Your program should have a function that lists a single temperature entry. You should ask the user for a number...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents of a file and display each line in the file to the screen. It will also display the line number followed by a colon. You will need to change the program so that it only display 24 lines from the file and waits for the user to press the enter key to continue. Do not use the system(“pause”) statement. Download Source Lab 5 File:...
Write a program that uses an array of doubles initialized to whatever values you wish. Write...
Write a program that uses an array of doubles initialized to whatever values you wish. Write methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT