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

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

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

问题

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

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

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

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

  1. """
  2. 多队列银行示例
  3. 包括:
  4. - 资源: Resource
  5. - 迭代过程
  6. 场景:
  7. 一个具有随机服务时间和客户到达过程的多柜台银行基于SimPy 2TheBank教程中的程序bank10.py (KGM)
  8. 作者Aaron Janeiro Stone
  9. """
  10. from simpy import *
  11. import random
  12. maxNumber = 30 # 最大客户数
  13. maxTime = 400.0 # 运行时间限制
  14. timeInBank = 20.0 # 在银行的平均时间
  15. arrivalMean = 10.0 # 到达过程的平均值
  16. seed = 12345 # 模拟的种子
  17. def Customer(env, name, counters):
  18. arrive = env.now
  19. Qlength = [NoInSystem(counters[i]) for i in range(len(counters))]
  20. print("%7.4f %s: 我在这里 %s" % (env.now, name, Qlength))
  21. for i in range(len(Qlength)):
  22. if Qlength[i] == 0 or Qlength[i] == min(Qlength):
  23. choice = i # 选择的队列号
  24. break
  25. with counters[choice].request() as req:
  26. # 等待柜台
  27. yield req
  28. wait = env.now - arrive
  29. # 我们到达了柜台
  30. print('%7.4f %s: 等待了 %6.3f' % (env.now, name, wait))
  31. tib = random.expovariate(1.0 / timeInBank)
  32. yield env.timeout(tib)
  33. print('%7.4f %s: 完成了' % (env.now, name))
  34. def NoInSystem(R):
  35. """资源R中的客户总数"""
  36. return max([0, len(R.put_queue) + len(R.users)])
  37. def Source(env, number, interval, counters):
  38. for i in range(number):
  39. c = Customer(env, 'Customer%02d' % i, counters)
  40. env.process(c)
  41. t = random.expovariate(1.0 / interval)
  42. yield env.timeout(t)
  43. # 设置和启动模拟
  44. print('具有多个队列的银行')
  45. random.seed(seed)
  46. env = Environment()
  47. counters = [Resource(env), Resource(env)]
  48. env.process(Source(env, maxNumber, arrivalMean, counters))
  49. 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.

  1. """
  2. Bank with multiple queues example
  3. Covers:
  4. - Resources: Resource
  5. - Iterating processes
  6. Scenario:
  7. A multi-counter bank with a random service time and customers arrival process. Based on the
  8. program bank10.py from TheBank tutorial of SimPy 2. (KGM)
  9. By Aaron Janeiro Stone
  10. """
  11. from simpy import *
  12. import random
  13. maxNumber = 30 # Max number of customers
  14. maxTime = 400.0 # Rumtime limit
  15. timeInBank = 20.0 # Mean time in bank
  16. arrivalMean = 10.0 # Mean of arrival process
  17. seed = 12345 # Seed for simulation
  18. def Customer(env, name, counters):
  19. arrive = env.now
  20. Qlength = [NoInSystem(counters[i]) for i in range(len(counters))]
  21. print("%7.4f %s: Here I am. %s" % (env.now, name, Qlength))
  22. for i in range(len(Qlength)):
  23. if Qlength[i] == 0 or Qlength[i] == min(Qlength):
  24. choice = i # the chosen queue number
  25. break
  26. with counters[choice].request() as req:
  27. # Wait for the counter
  28. yield req
  29. wait = env.now - arrive
  30. # We got to the counter
  31. print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
  32. tib = random.expovariate(1.0 / timeInBank)
  33. yield env.timeout(tib)
  34. print('%7.4f %s: Finished' % (env.now, name))
  35. def NoInSystem(R):
  36. """Total number of customers in the resource R"""
  37. return max([0, len(R.put_queue) + len(R.users)])
  38. def Source(env, number, interval, counters):
  39. for i in range(number):
  40. c = Customer(env, 'Customer%02d' % i, counters)
  41. env.process(c)
  42. t = random.expovariate(1.0 / interval)
  43. yield env.timeout(t)
  44. # Setup and start the simulation
  45. print('Bank with multiple queues')
  46. random.seed(seed)
  47. env = Environment()
  48. counters = [Resource(env), Resource(env)]
  49. env.process(Source(env, maxNumber, arrivalMean, counters))
  50. env.run(until=maxTime)

答案1

得分: 1

以下是代码的翻译部分:

  1. 添加了一个在银行开门之前加载顾客队列的函数时间为0
  2. """
  3. 多队列银行示例
  4. 涵盖:
  5. - 资源:Resource
  6. - 迭代过程
  7. 场景:
  8. 一个多柜台银行,具有随机服务时间和顾客到达过程。基于SimPy 2的TheBank教程中的程序bank10.py。(KGM)
  9. 作者:Aaron Janeiro Stone
  10. """
  11. from simpy import *
  12. import random
  13. qStartNum = 20 # 开始时队列中的人数
  14. maxNumber = 30 # 开始后的最大顾客数
  15. maxTime = 400.0 # 运行时间限制
  16. timeInBank = 20.0 # 银行中的平均时间
  17. arrivalMean = 10.0 # 到达过程的平均值
  18. seed = 12345 # 模拟的种子
  19. def Customer(env, name, counters):
  20. arrive = env.now
  21. Qlength = [NoInSystem(counters[i]) for i in range(len(counters))]
  22. print("%7.4f %s: 我在这里。%s" % (env.now, name, Qlength))
  23. for i in range(len(Qlength)):
  24. if Qlength[i] == 0 or Qlength[i] == min(Qlength):
  25. choice = i # 选择的队列编号
  26. break
  27. with counters[choice].request() as req:
  28. # 等待柜台
  29. yield req
  30. wait = env.now - arrive
  31. # 我们到达了柜台
  32. print('%7.4f %s: 等待了 %6.3f' % (env.now, name, wait))
  33. tib = random.expovariate(1.0 / timeInBank)
  34. yield env.timeout(tib)
  35. print('%7.4f %s: 完成' % (env.now, name))
  36. def NoInSystem(R):
  37. """资源R中的顾客总数"""
  38. return max([0, len(R.put_queue) + len(R.users)])
  39. def Source(env, minNumber, maxNumber, interval, counters):
  40. for i in range(minNumber, maxNumber):
  41. c = Customer(env, '顾客%02d' % i, counters)
  42. env.process(c)
  43. t = random.expovariate(1.0 / interval)
  44. yield env.timeout(t)
  45. def preSource(env, number, counters):
  46. """
  47. 在银行开门之前排队的顾客
  48. """
  49. for i in range(number):
  50. c = Customer(env, '顾客%02d' % i, counters)
  51. env.process(c)
  52. # 设置和启动模拟
  53. print('多队列银行')
  54. random.seed(seed)
  55. env = Environment()
  56. counters = [Resource(env), Resource(env)]
  57. preSource(env, qStartNum, counters)
  58. env.process(Source(env, qStartNum, qStartNum + maxNumber, arrivalMean, counters))
  59. env.run(until=maxTime)
英文:

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

  1. """
  2. Bank with multiple queues example
  3. Covers:
  4. - Resources: Resource
  5. - Iterating processes
  6. Scenario:
  7. A multi-counter bank with a random service time and customers arrival process. Based on the
  8. program bank10.py from TheBank tutorial of SimPy 2. (KGM)
  9. By Aaron Janeiro Stone
  10. """
  11. from simpy import *
  12. import random
  13. qStartNum = 20 # number of people in queue at start
  14. maxNumber = 30 # Max number of customers after start
  15. maxTime = 400.0 # Rumtime limit
  16. timeInBank = 20.0 # Mean time in bank
  17. arrivalMean = 10.0 # Mean of arrival process
  18. seed = 12345 # Seed for simulation
  19. def Customer(env, name, counters):
  20. arrive = env.now
  21. Qlength = [NoInSystem(counters[i]) for i in range(len(counters))]
  22. print("%7.4f %s: Here I am. %s" % (env.now, name, Qlength))
  23. for i in range(len(Qlength)):
  24. if Qlength[i] == 0 or Qlength[i] == min(Qlength):
  25. choice = i # the chosen queue number
  26. break
  27. with counters[choice].request() as req:
  28. # Wait for the counter
  29. yield req
  30. wait = env.now - arrive
  31. # We got to the counter
  32. print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
  33. tib = random.expovariate(1.0 / timeInBank)
  34. yield env.timeout(tib)
  35. print('%7.4f %s: Finished' % (env.now, name))
  36. def NoInSystem(R):
  37. """Total number of customers in the resource R"""
  38. return max([0, len(R.put_queue) + len(R.users)])
  39. def Source(env, minNumber, maxNumber, interval, counters):
  40. for i in range(minNumber, maxNumber):
  41. c = Customer(env, 'Customer%02d' % i, counters)
  42. env.process(c)
  43. t = random.expovariate(1.0 / interval)
  44. yield env.timeout(t)
  45. def preSource(env, number, counters):
  46. """
  47. Queues customers before bank opens
  48. """
  49. for i in range(number):
  50. c = Customer(env, 'Customer%02d' % i, counters)
  51. env.process(c)
  52. # Setup and start the simulation
  53. print('Bank with multiple queues')
  54. random.seed(seed)
  55. env = Environment()
  56. counters = [Resource(env), Resource(env)]
  57. preSource(env, qStartNum, counters)
  58. env.process(Source(env, qStartNum, qStartNum + maxNumber, arrivalMean, counters))
  59. 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:

确定