运行 Simpy模拟直到给定值
Running a Simpy simulation until a given value
我是 SymPy (3.0.8) 的新手,想 运行 演示模拟 'conference attendee' 直到给定值 (250),即 运行 直到讨论 7 (3 次谈话,1 次休息,3 次谈话,1 次休息,1 次谈话)。
3*30 + 15 + 3*30 + 15 + 1*30 = 240
但是模拟在3讲1断3讲1断(210)后结束。
我想知道为什么模拟 运行 直到 'talk 7' 以及如何解决这个问题。
# import
import simpy
from random import randint
# config
TALKS_PER_SESSION = 3
TALK_LENGTH = 30
BREAK_LENGTH = 15
ATTENDEES = 3
# process function
def attendee(env, name, knowledge=0, hunger=0):
talks = 0
breaks = 0
while True:
# Visit talks
for i in range(TALKS_PER_SESSION):
knowledge += randint(0, 3) / (1 + hunger)
hunger += randint(1, 4)
talks += 1
yield env.timeout(TALK_LENGTH)
print('Attendee %s finished %d talks with knowledge %.2f and hunger %.2f.' % (name, talks, knowledge, hunger))
# Go to buffet
food = randint(3, 12)
hunger -= min(food, hunger)
breaks += 1
yield env.timeout(BREAK_LENGTH)
print('Attendee %s has finished break %d with hunger %.2f' % (name, breaks, hunger))
# setup environment and run simulation
env = simpy.Environment()
for i in range(ATTENDEES):
env.process(attendee(env, i))
env.run(until=250)
这个模拟的输出是:
Attendee 0 finished 3 talks with knowledge 2.33 and hunger 7.00.
Attendee 1 finished 3 talks with knowledge 0.60 and hunger 8.00.
Attendee 2 finished 3 talks with knowledge 3.81 and hunger 7.00.
Attendee 0 has finished break 1 with hunger 0.00
Attendee 1 has finished break 1 with hunger 0.00
Attendee 2 has finished break 1 with hunger 0.00
Attendee 0 finished 6 talks with knowledge 5.53 and hunger 6.00.
Attendee 1 finished 6 talks with knowledge 1.89 and hunger 9.00.
Attendee 2 finished 6 talks with knowledge 4.31 and hunger 5.00.
Attendee 0 has finished break 2 with hunger 0.00
Attendee 1 has finished break 2 with hunger 0.00
Attendee 2 has finished break 2 with hunger 0.00
但我预计会是:
Attendee 0 finished 3 talks with knowledge 2.33 and hunger 7.00.
Attendee 1 finished 3 talks with knowledge 0.60 and hunger 8.00.
Attendee 2 finished 3 talks with knowledge 3.81 and hunger 7.00.
Attendee 0 has finished break 1 with hunger 0.00
Attendee 1 has finished break 1 with hunger 0.00
Attendee 2 has finished break 1 with hunger 0.00
Attendee 0 finished 6 talks with knowledge 5.53 and hunger 6.00.
Attendee 1 finished 6 talks with knowledge 1.89 and hunger 9.00.
Attendee 2 finished 6 talks with knowledge 4.31 and hunger 5.00.
Attendee 0 has finished break 2 with hunger 0.00
Attendee 1 has finished break 2 with hunger 0.00
Attendee 2 has finished break 2 with hunger 0.00
Attendee 0 finished 7 talks with knowledge <...> and hunger <...>.
Attendee 1 finished 7 talks with knowledge <...> and hunger <...>.
Attendee 2 finished 7 talks with knowledge <...> and hunger <...>.
请设置 ATTENDEES = 1
并将内部 for
循环更改为:
for i in range(TALKS_PER_SESSION):
print(f'Talk {talks+1} begins at {env.now}')
knowledge += randint(0, 3) / (1 + hunger)
hunger += randint(1, 4)
talks += 1
yield env.timeout(TALK_LENGTH)
print(f'Talk {talks} ends at {env.now}')
当你让你的模拟再次运行并思考会发生什么时,你应该明白你得到的输出是正确的。 :)
我是 SymPy (3.0.8) 的新手,想 运行 演示模拟 'conference attendee' 直到给定值 (250),即 运行 直到讨论 7 (3 次谈话,1 次休息,3 次谈话,1 次休息,1 次谈话)。
3*30 + 15 + 3*30 + 15 + 1*30 = 240
但是模拟在3讲1断3讲1断(210)后结束。
我想知道为什么模拟 运行 直到 'talk 7' 以及如何解决这个问题。
# import
import simpy
from random import randint
# config
TALKS_PER_SESSION = 3
TALK_LENGTH = 30
BREAK_LENGTH = 15
ATTENDEES = 3
# process function
def attendee(env, name, knowledge=0, hunger=0):
talks = 0
breaks = 0
while True:
# Visit talks
for i in range(TALKS_PER_SESSION):
knowledge += randint(0, 3) / (1 + hunger)
hunger += randint(1, 4)
talks += 1
yield env.timeout(TALK_LENGTH)
print('Attendee %s finished %d talks with knowledge %.2f and hunger %.2f.' % (name, talks, knowledge, hunger))
# Go to buffet
food = randint(3, 12)
hunger -= min(food, hunger)
breaks += 1
yield env.timeout(BREAK_LENGTH)
print('Attendee %s has finished break %d with hunger %.2f' % (name, breaks, hunger))
# setup environment and run simulation
env = simpy.Environment()
for i in range(ATTENDEES):
env.process(attendee(env, i))
env.run(until=250)
这个模拟的输出是:
Attendee 0 finished 3 talks with knowledge 2.33 and hunger 7.00.
Attendee 1 finished 3 talks with knowledge 0.60 and hunger 8.00.
Attendee 2 finished 3 talks with knowledge 3.81 and hunger 7.00.
Attendee 0 has finished break 1 with hunger 0.00
Attendee 1 has finished break 1 with hunger 0.00
Attendee 2 has finished break 1 with hunger 0.00
Attendee 0 finished 6 talks with knowledge 5.53 and hunger 6.00.
Attendee 1 finished 6 talks with knowledge 1.89 and hunger 9.00.
Attendee 2 finished 6 talks with knowledge 4.31 and hunger 5.00.
Attendee 0 has finished break 2 with hunger 0.00
Attendee 1 has finished break 2 with hunger 0.00
Attendee 2 has finished break 2 with hunger 0.00
但我预计会是:
Attendee 0 finished 3 talks with knowledge 2.33 and hunger 7.00.
Attendee 1 finished 3 talks with knowledge 0.60 and hunger 8.00.
Attendee 2 finished 3 talks with knowledge 3.81 and hunger 7.00.
Attendee 0 has finished break 1 with hunger 0.00
Attendee 1 has finished break 1 with hunger 0.00
Attendee 2 has finished break 1 with hunger 0.00
Attendee 0 finished 6 talks with knowledge 5.53 and hunger 6.00.
Attendee 1 finished 6 talks with knowledge 1.89 and hunger 9.00.
Attendee 2 finished 6 talks with knowledge 4.31 and hunger 5.00.
Attendee 0 has finished break 2 with hunger 0.00
Attendee 1 has finished break 2 with hunger 0.00
Attendee 2 has finished break 2 with hunger 0.00
Attendee 0 finished 7 talks with knowledge <...> and hunger <...>.
Attendee 1 finished 7 talks with knowledge <...> and hunger <...>.
Attendee 2 finished 7 talks with knowledge <...> and hunger <...>.
请设置 ATTENDEES = 1
并将内部 for
循环更改为:
for i in range(TALKS_PER_SESSION):
print(f'Talk {talks+1} begins at {env.now}')
knowledge += randint(0, 3) / (1 + hunger)
hunger += randint(1, 4)
talks += 1
yield env.timeout(TALK_LENGTH)
print(f'Talk {talks} ends at {env.now}')
当你让你的模拟再次运行并思考会发生什么时,你应该明白你得到的输出是正确的。 :)