Calculating Average Performance Measures in M/M/1 System Using SimPy

Question:

I am seeking to calculate the average waiting times, and average service times in the following M/M/1 queueing system, but I am not able to calculate the averages. It writes down to the console each customer’s arrival, waiting and service times, but I also want to calculate the average of all. I tried several techniques such as implementing the statistics library and trying to calculate the means. I will be pleased if you are able to assist me, thank you.

      import random
import simpy
import numpy
from random import randint
from random import seed
from random import expovariate
import math
import statistics


random_seed = 42 #for seed of other random generators
new_customers = 20  # Total number of customers in the system
interarrival = numpy.random.poisson(randint(0,20), size=None) # Generate new customers roughly every x seconds
#servicetime = numpy.random.exponential(randint(0,20), size=None)
min_priority = 1
max_priority = 10

def generator(env, number, interval, server): #customer generator with interarrival times.
    """generator generates customers randomly"""
    for i in range(number):
        c = customer(env, 'Customer%02d' % i, server, system_time=15)
        env.process(c)
        t = random.expovariate(1.0 / interval)
        yield env.timeout(t) #adds time to the counter, does not delete from the memory

def customer(env, name, server, system_time):
    #customer arrives to the system, waits and leaves
    arrive = env.now
    print('Arrival time of Customer %s is: %7.4f' % (name, arrive))

    with server.request() as req:
        priority = random.uniform(min_priority, max_priority)
        results = yield req | env.timeout(priority)
        
        waiting_time = env.now - arrive

        if req in results:
            #req will be in the server time
            print('%s waited %6.3f seconds in the queue' % (name, waiting_time))
            systime = random.expovariate(1.0 / system_time)
            yield env.timeout(systime)
            print('%7.4f %s: Finished' % (env.now, name))
            print('Customer %s spent %7.4f time in the server' %(name,env.now-arrive))
            print('%s waited %6.3f seconds' % (name, waiting_time))

        else:
            #reneging occurs
            print('%7.4f %s: Reneging Time %6.3f' % (env.now, name, waiting_time))


random.seed(random_seed)
env = simpy.Environment()
seed(29384) #for seed of randint function
server = simpy.Resource(env, capacity = 1) #capacity changes the number of generators in the system.
env.process(generator(env,new_customers, interarrival, server))
env.run()
#print('%s waited %6.3f seconds' % (name, waiting_time))

Plus, I tried

print("The average waiting time is %7.4f seconds", %(statistics.mean(waiting_time))

in the latest lines of my code, and it said that waiting_time is not defined. Moreover, when I tested inside of the functions, it did not estimate the average value, it also gives int or float error. I consider it was an arraywise problem since I did not specify the array for the times, it does not store it properly.

Asked By: dozgunay

||

Answers:

Just initilize an array of waiting times at the beginning of your initialization and append it with already calculated waiting_time every time you calculate for each customer. Then you can compute statistics.

If you like, import statistics module for automating your computations.

#----
min_priority = 1
max_priority = 10
waitingTimes = []
#---
waiting_time = env.now - arrive
waitingTimes.append(waiting_time)
#---last line
average_waitingTime = statistics.mean(waitingTimes)
Answered By: cemal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.