如何在Simpy的模拟过程中,在开放时间之前模拟一个起始队列?

huangapple go评论66阅读模式
英文:

How to simulate a starting queue before opening times in a Simulation process with Simpy?

问题

我正在学习SimPy,我遇到了这个有趣的教程,它允许模拟银行的排队。

我想知道是否可能以及如何创建一个初始队列。

假设银行在09:00开门,但我们已经有20名等待服务的客户+根据定义的概率到达率将会到来的其他客户。

如何做到这一点?谢谢你的支持。

    """
    多队列银行示例
    包括:
    - 资源: Resource
    - 迭代过程
    场景:
      一个具有随机服务时间和客户到达过程的多柜台银行基于SimPy 2的TheBank教程中的程序bank10.py (KGM)
    作者Aaron Janeiro Stone
    """
    from simpy import *
    import random
    
    maxNumber = 30      # 最大客户数
    maxTime = 400.0     # 运行时间限制
    timeInBank = 20.0   # 在银行的平均时间
    arrivalMean = 10.0  # 到达过程的平均值
    seed = 12345        # 模拟的种子
    
    def Customer(env, name, counters):
        arrive = env.now
        Qlength = [NoInSystem(counters[i]) for i in range(len(counters))]
        print("%7.4f %s: 我在这里 %s" % (env.now, name, Qlength))
        for i in range(len(Qlength)):
            if Qlength[i] == 0 or Qlength[i] == min(Qlength):
                choice = i  # 选择的队列号
                break
        with counters[choice].request() as req:
            # 等待柜台
            yield req
            wait = env.now - arrive
            # 我们到达了柜台
            print('%7.4f %s: 等待了 %6.3f' % (env.now, name, wait))
            tib = random.expovariate(1.0 / timeInBank)
            yield env.timeout(tib)
            print('%7.4f %s: 完成了' % (env.now, name))
    
    def NoInSystem(R):
        """资源R中的客户总数"""
        return max([0, len(R.put_queue) + len(R.users)])
    
    def Source(env, number, interval, counters):
        for i in range(number):
            c = Customer(env, 'Customer%02d' % i, counters)
            env.process(c)
            t = random.expovariate(1.0 / interval)
            yield env.timeout(t)
    
    # 设置和启动模拟
    print('具有多个队列的银行')
    random.seed(seed)
    env = Environment()
    
    counters = [Resource(env), Resource(env)]
    env.process(Source(env, maxNumber, arrivalMean, counters))
    env.run(until=maxTime)
英文:

I am studying SimPy and I came across this interesting tutorial that allows to simulate a queue in a bank.

I wanted to know if it is possible and how to create an initial queue.

Let's assume that the bank opens at 09:00 but we have already 20 customers waiting to be served + the other that will come with the defined probabilistic arrival rate.

How to do that? Thank you for you support.

"""
Bank with multiple queues example
Covers:
- Resources: Resource
- Iterating processes
Scenario:
A multi-counter bank with a random service time and customers arrival process. Based on the
program bank10.py from TheBank tutorial of SimPy 2. (KGM)
By Aaron Janeiro Stone
"""
from simpy import *
import random
maxNumber = 30      # Max number of customers
maxTime = 400.0     # Rumtime limit
timeInBank = 20.0   # Mean time in bank
arrivalMean = 10.0  # Mean of arrival process
seed = 12345        # Seed for simulation
def Customer(env, name, counters):
arrive = env.now
Qlength = [NoInSystem(counters[i]) for i in range(len(counters))]
print("%7.4f %s: Here I am. %s" % (env.now, name, Qlength))
for i in range(len(Qlength)):
if Qlength[i] == 0 or Qlength[i] == min(Qlength):
choice = i  # the chosen queue number
break
with counters[choice].request() as req:
# Wait for the counter
yield req
wait = env.now - arrive
# We got to the counter
print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
tib = random.expovariate(1.0 / timeInBank)
yield env.timeout(tib)
print('%7.4f %s: Finished' % (env.now, name))
def NoInSystem(R):
"""Total number of customers in the resource R"""
return max([0, len(R.put_queue) + len(R.users)])
def Source(env, number, interval, counters):
for i in range(number):
c = Customer(env, 'Customer%02d' % i, counters)
env.process(c)
t = random.expovariate(1.0 / interval)
yield env.timeout(t)
# Setup and start the simulation
print('Bank with multiple queues')
random.seed(seed)
env = Environment()
counters = [Resource(env), Resource(env)]
env.process(Source(env, maxNumber, arrivalMean, counters))
env.run(until=maxTime)

答案1

得分: 1

以下是代码的翻译部分:

添加了一个在银行开门之前加载顾客队列的函数时间为0

"""
多队列银行示例
涵盖:
- 资源:Resource
- 迭代过程
场景:
一个多柜台银行,具有随机服务时间和顾客到达过程。基于SimPy 2的TheBank教程中的程序bank10.py。(KGM)
作者:Aaron Janeiro Stone
"""
from simpy import *
import random

qStartNum = 20      # 开始时队列中的人数
maxNumber = 30      # 开始后的最大顾客数
maxTime = 400.0     # 运行时间限制
timeInBank = 20.0   # 银行中的平均时间
arrivalMean = 10.0  # 到达过程的平均值
seed = 12345        # 模拟的种子


def Customer(env, name, counters):
    arrive = env.now
    Qlength = [NoInSystem(counters[i]) for i in range(len(counters))]
    print("%7.4f %s: 我在这里。%s" % (env.now, name, Qlength))
    for i in range(len(Qlength)):
        if Qlength[i] == 0 or Qlength[i] == min(Qlength):
            choice = i  # 选择的队列编号
            break
    with counters[choice].request() as req:
        # 等待柜台
        yield req
        wait = env.now - arrive
        # 我们到达了柜台
        print('%7.4f %s: 等待了 %6.3f' % (env.now, name, wait))
        tib = random.expovariate(1.0 / timeInBank)
        yield env.timeout(tib)
        print('%7.4f %s: 完成' % (env.now, name))


def NoInSystem(R):
    """资源R中的顾客总数"""
    return max([0, len(R.put_queue) + len(R.users)])


def Source(env, minNumber, maxNumber, interval, counters):
    for i in range(minNumber, maxNumber):
        c = Customer(env, '顾客%02d' % i, counters)
        env.process(c)
        t = random.expovariate(1.0 / interval)
        yield env.timeout(t)


def preSource(env, number, counters):
    """
    在银行开门之前排队的顾客
    """
    for i in range(number):
        c = Customer(env, '顾客%02d' % i, counters)
        env.process(c)


# 设置和启动模拟
print('多队列银行')
random.seed(seed)
env = Environment()

counters = [Resource(env), Resource(env)]
preSource(env, qStartNum, counters)
env.process(Source(env, qStartNum, qStartNum + maxNumber, arrivalMean, counters))
env.run(until=maxTime)
英文:

added a function to load the queues with customers before the bank opens which is time 0.

"""
Bank with multiple queues example
Covers:
- Resources: Resource
- Iterating processes
Scenario:
A multi-counter bank with a random service time and customers arrival process. Based on the
program bank10.py from TheBank tutorial of SimPy 2. (KGM)
By Aaron Janeiro Stone
"""
from simpy import *
import random
qStartNum = 20      # number of people in queue at start
maxNumber = 30      # Max number of customers after start
maxTime = 400.0     # Rumtime limit
timeInBank = 20.0   # Mean time in bank
arrivalMean = 10.0  # Mean of arrival process
seed = 12345        # Seed for simulation
def Customer(env, name, counters):
arrive = env.now
Qlength = [NoInSystem(counters[i]) for i in range(len(counters))]
print("%7.4f %s: Here I am. %s" % (env.now, name, Qlength))
for i in range(len(Qlength)):
if Qlength[i] == 0 or Qlength[i] == min(Qlength):
choice = i  # the chosen queue number
break
with counters[choice].request() as req:
# Wait for the counter
yield req
wait = env.now - arrive
# We got to the counter
print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
tib = random.expovariate(1.0 / timeInBank)
yield env.timeout(tib)
print('%7.4f %s: Finished' % (env.now, name))
def NoInSystem(R):
"""Total number of customers in the resource R"""
return max([0, len(R.put_queue) + len(R.users)])
def Source(env, minNumber, maxNumber, interval, counters):
for i in range(minNumber, maxNumber):
c = Customer(env, 'Customer%02d' % i, counters)
env.process(c)
t = random.expovariate(1.0 / interval)
yield env.timeout(t)
def preSource(env, number, counters):
"""
Queues customers before bank opens
"""
for i in range(number):
c = Customer(env, 'Customer%02d' % i, counters)
env.process(c)
# Setup and start the simulation
print('Bank with multiple queues')
random.seed(seed)
env = Environment()
counters = [Resource(env), Resource(env)]
preSource(env, qStartNum, counters)
env.process(Source(env, qStartNum, qStartNum + maxNumber, arrivalMean, counters))
env.run(until=maxTime)

huangapple
  • 本文由 发表于 2023年5月28日 01:10:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定