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

2024/11/20 4:57:08

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 statisticsrandom_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 = 10def 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 memorydef customer(env, name, server, system_time):#customer arrives to the system, waits and leavesarrive = env.nowprint('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 - arriveif req in results:#req will be in the server timeprint('%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 occursprint('%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.

Answer

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)
https://en.xdnf.cn/q/119871.html

Related Q&A

Python: Whats the difference between import X and from X import *? [duplicate]

This question already has answers here:Use import module or from module import?(23 answers)Closed 7 years ago.I use to think both are equal until I tried this:$python Python 2.7.13 (default, Dec 17 20…

Python iterate through pixels of image

Im trying to iterate through pixels of an image. I set the size and then use a for loop, however I get a type error: object not iterable. I have imported PIL and Imagew=100 h=200 im=im.resize((w,h), Im…

Geocoding with Geopy and big data

I have this CSV file which im feeding with this python scriptimport csv from geopy.geocoders import OpenCagegeolocator = OpenCage() #here some parameters are needed with open(/Users/Ian/Desktop/Test02/…

an error in sending json data to flask server [duplicate]

This question already has answers here:How to get POSTed JSON in Flask?(13 answers)Closed 1 year ago.I have a json data as {"age":59.0,"bp":70.0,"sg":1.01,"al":…

terminate a python program when it hanged using subprocess python

I have a main.py which open a new cmd (subprocess) when another program (test.py, in same directory) is hanged. To determining test.py is hanged or not, I used latest modified time (os.path.getmtime(te…

causes of Python IOError: [Errno 13] Permission denied

When attempting to write a file, I can get this same error when any of following conditions applies:The file exists and is marked read-only. I dont have write permission for the folder and therefore ca…

Python Invalid Syntax IF statement

Im trying to make a quiz in python but I keep getting invalid syntax errors.#This is for addition questions.if (question=add) <---- That is where i get the error for i in range(0,10):first_number_a…

record a web page using python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

returning a string from askopenfilename() to a entry box

I have seen many postings on the use of askopenfilename(), however I still cant seem to find anything to help me display the full file path in an entry box once I have selected said file. below I have…

Overflow error in Python program

Please help me to understand why this code doesnt work. I know there is something very stupid wrong. This should be an implementation of the fourth order Runge kutta algorithm to solve Lorentz system o…