提问人:Javad Akbari Sattar 提问时间:11/15/2023 最后编辑:Trenton McKinneyJavad Akbari Sattar 更新时间:11/15/2023 访问量:69
单通道队列系统仿真
Simulation of single-channel queue system
问:
[我写了这段代码,但计算值没有收敛,收敛图显示一些不正确的线性行为。我应该做些什么才能使用这种方法 (FEL) 正确仿真?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(42)
n=1000
min_IA= 1
max_IA= 6
min_ST= 1
max_ST= 4
event = 0
clock = 0
next_arrival = np.random.uniform (min_IA, max_IA)
next_departure = float ('inf')
server_status = 'idle'
queue = 0
data = []
total_wait_time = 0
total_system_time = 0
total_queue_time = 0
total_queue_count = 0
total_busy_time = 0
total_customers = 0
total_system_count = 0
def arrival_event ():
global event, clock, next_arrival, next_departure, server_status, queue, data, total_wait_time, total_system_count
event += 1
clock = next_arrival
total_system_count += 1
next_arrival = clock + np.random.uniform (min_IA, max_IA)
if server_status == 'idle':
server_status = 'busy'
next_departure = clock + np.random.uniform (min_ST, max_ST)
else:
queue += 1
data.append ([event, 'arrival', clock, next_arrival, next_departure, server_status, queue])
total_wait_time += len(data) - 1 - queue
# تعریف زیر برنامه برای رویداد خروج
def departure_event ():
global event, clock, next_arrival, next_departure, server_status, queue, data, total_system_time, total_queue_time, total_queue_count, total_busy_time, total_customers
event += 1
clock = next_departure
total_system_time += clock - data[-queue-1][2]
total_customers += 1
if queue == 0:
server_status = 'idle'
next_departure = clock + np.random.uniform(1/(total_customers - total_queue_count))
else:
queue -= 1
next_departure = clock + np.random.uniform (min_ST, max_ST)
total_queue_time += clock - data[-queue-1][2]
total_queue_count += 1
data.append ([event, 'departure', clock, next_arrival, next_departure, server_status, queue])
total_busy_time += data[-1][4] - data[-1][2]
while event < n:
if next_arrival < next_departure:
arrival_event ()
else:
departure_event ()
df = pd.DataFrame (data, columns = ['Event', 'Type', 'Clock', 'Next Arrival', 'Next Departure', 'Server Status', 'Queue'])
df.index += 1
df = df.round (2)
print (df)
average_wait_time = total_wait_time / n
average_system_time = total_system_time / n
average_queue_time = total_queue_time / total_queue_count
server_utilization = (total_busy_time / clock) * 100
arrival_rate = total_customers / clock
average_system_count = total_system_count / clock
average_queue_count = total_queue_count / clock
print ('Average wait time for all customers in queue:', average_wait_time)
print ('Average system time for all customers:', average_system_time)
print ('Average wait time for customers who waited in queue:', average_queue_time)
print ('Server utilization:', server_utilization , '%')
print('Arrival rate:', arrival_rate)
print('Average number of customers in the system:', average_system_count)
print('Average number of customers in the queue:', average_queue_count)
plt.figure()
plt.plot(range(1, n+1), df['Clock'].cumsum() / range(1, n+1))
plt.xlabel('Number of events')
plt.ylabel('Average wait time of all customers in queue')
plt.title('Convergence Chart - Average Wait Time of All Customers in Queue')
plt.figure()
plt.plot(range(1, n+1), df['Queue'].cumsum() / range(1, n+1))
plt.xlabel('Number of events')
plt.ylabel('Average wait time of customers in queue')
plt.title('Convergence Chart - Average Wait Time of Customers in Queue')
plt.figure()
plt.plot(range(1, n+1), df['Server Status'].apply(lambda x: 1 if x == 'busy' else 0).cumsum() / range(1, n+1))
plt.xlabel('Number of events')
plt.ylabel('Server utilization')
plt.title('Convergence Chart - Server Utilization')
plt.figure()
plt.plot(range(1, n+1), df['Clock'].cumsum() / range(1, n+1))
plt.xlabel('Number of events')
plt.ylabel('Average system time')
plt.title('Convergence Chart - Average System Time')
plt.figure()
plt.plot(range(1, n+1), df['Event'].cumsum() / df['Clock'])
plt.xlabel('Number of events')
plt.ylabel('Arrival rate')
plt.title('Convergence Chart - Arrival Rate')
plt.figure()
plt.plot(range(1, n+1), df['Clock'].cumsum() / range(1, n+1))
plt.xlabel('Number of events')
plt.ylabel('Average number of customers in the system')
plt.title('Convergence Chart - Average Number of Customers in the System')
plt.figure()
plt.plot(range(1, n+1), df['Queue'].cumsum() / range(1, n+1))
plt.xlabel('Number of events')
plt.ylabel('Average number of customers in the queue')
plt.title('Convergence Chart - Average Number of Customers in the Queue')
plt.show()
作业标题:模拟单服务器队列 使用 Python 编程语言为 1000 名客户模拟课堂上讨论的单服务器队列。假设模拟从第一个客户的到来开始。假设到达时间均匀分布在 1 到 6 分钟之间,服务时间均匀分布在 1 到 4 分钟之间。收集必要的统计数据,并据此估算队列中所有客户的平均等待时间、在队列中等待一段时间的客户的平均等待时间、服务器利用率的百分比、在系统中花费的平均时间、到达率、系统中存在的平均客户数量, 以及等待队列中的平均客户数量。
对队列中所有客户的平均等待时间应用收敛图,对在队列中等待一段时间的客户的平均等待时间应用收敛图,对服务器利用率百分比应用收敛图,对在系统中花费的平均时间应用收敛图,对到达率应用收敛图, 在此 Python 代码中,系统中存在的平均客户数的收敛图和等待队列中存在的平均客户数的收敛图,以分别绘制每个客户。
为了增加客户数量,我将其增加到十万,但整体主题没有变化。我将不胜感激您的帮助。
答: 暂无答案
评论