You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SimPy实现M/M/s排队系统仿真结果偏误,请求代码排查

M/M/s排队系统SimPy仿真结果与理论值偏差问题调试求助

我用SimPy构建M/M/s排队系统仿真,对比理论计算值时发现,即便运行时长充足,仿真结果仍比理论值低约10%:

  • 所有客户的理论平均等待时间为5.15448,仿真结果多低于5;
  • 需等待客户的理论平均等待时间为6.0,仿真结果多低于5.8。

怀疑代码存在错误,附上代码请求调试。注:queue_entry_time为测试用冗余变量,替换为arrival_time后问题仍存在。

import simpy
import numpy as np 

params = {"arrival_rate": 4, "service_rate": (1/2.4), "num_servers": 10, "num_customers": 5000000, "stream_id": 1}
queue_length_list = []  # 存储队列长度
customer_count_list = []  # 存储系统内客户数量
waiting_time_list = []  # 存储等待时间
utilization_list = []  # 存储利用率
throughput_list = []  # 存储吞吐量

def customer(env, server, params):    
    arrival_time = env.now            
    queue_entry_time = env.now
    with server.request() as request:
        queue_length_list.append((env.now, len(server.queue)))    
        customer_count_list.append((env.now, len(server.queue) + server.count))    
        queue_entry_time = env.now
        yield request                 
        waiting_time_list.append((env.now, env.now - queue_entry_time))     
        utilization_list.append((env.now, server.count / params["num_servers"]))     
        service_time = np.random.exponential(1.0 / params["service_rate"])     
        yield env.timeout(service_time)
        throughput_list.append((env.now, len(waiting_time_list)/env.now))     

def arrivals(env, server, params):
    for _ in range(params["num_customers"]):
        yield env.timeout(np.random.exponential(1.0 / params["arrival_rate"]))
        env.process(customer(env, server, params))

env = simpy.Environment()
server = simpy.Resource(env, capacity=params["num_servers"])
env.process(arrivals(env, server, params))
env.run()

times, waiting_times = zip(*waiting_time_list)
mean_waiting_time = np.mean(waiting_times)
print(f"Mean Waiting Time:", mean_waiting_time)
max_waiting_time = np.max(waiting_times)
print(f"Max Waiting Time:", max_waiting_time)
non_zero_waiting_times = [wt for wt in waiting_times if wt > 0]
if len(non_zero_waiting_times) > 0:
    mean_non_zero_waiting_time = sum(non_zero_waiting_times) / len(non_zero_waiting_times)
    print(f"Mean Non-Zero Waiting Time:", mean_non_zero_waiting_time)
print()

内容的提问来源于stack exchange,提问作者LC007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 08:08:12