MassiveCoding
MassiveCoding

Reputation: 53

Discrete-event Simulation Algorithm 1.2.1 in C++

I'm currently trying to work and extend on the Algorithm given in "Discrete-event Simulation" text book pg 15. My C++ knowledge is limited, It's not homework problem just want to understand how to approach this problem in C++ & understand what going. I want to be able to compute 12 delays in a single-server FIFO service node.

Algorithm in the book is as follow:

Co = 0.0;  //assumes that a0=0.0
i = 0; 
while (more jobs to process) {
    i++;
    a_i = GetArrival ();
    if (a_i < c_i - 1)
        d_i = c_i - 1 - a_i;  //calculate delay for job i
    else
        d_i = 0.0;            // job i has no delay
    s_i = GetService ();
    c_i = a_i + d_i + s_i;    // calculate departure time for job i
}

n = i; 
return d_1, d_2,..., d_n

The GetArrival and GetService procedures read the next arrival and service time from a file.

Upvotes: 0

Views: 844

Answers (2)

triple_r
triple_r

Reputation: 1047

Just looking at the pseudo-code, it seems that you just need one a which is a at step i, one c which is c at step i-1, and an array of ds to store the delays. I'm assuming the first line in your pseudo-code is c_0 = 0 and not Co = 0, other wise the code doesn't make a lot of sense.

Now here is a C++-ized version of the pseudo-code:

std::vector<int> d;
int c = 0;
int a, s;
while(!arrivalFile.eof() && !serviceFile.eof())
{
    arrivalFile >> a;
    int delay = 0;
    if (a < c)
        delay = c - a;
    d.push_back(delay);
    serviceFile >> s;
    c = a + delay + s;
}

return d;

Upvotes: 2

Walid Ibrahim
Walid Ibrahim

Reputation: 81

If I understand the code right, d_1, d_2, ..., d_n are the delays you have, number of delays depends on number of jobs to process. while (more jobs to process)

thus if you have 12 processes you will have 12 delays.

In general if arrival time is less than previous departure time then the delay is the previous departure time - current arrival time

if (a_i < c_i-1)
  d_i = c_i-1 - a_i; 

the first departure time is set to zero

if something is not clear let me know

Upvotes: 0

Related Questions