运行Simpy模拟直到给定值

时间:2017-02-22 11:44:09

标签: python-3.x simulation simpy

我是SymPy(3.0.8)的新手,希望运行一个演示模拟'会议与会者'直到给定值(250),即直到谈话7(3次谈话,1次休息,3次谈话,1次休息,1次谈话)。

3 * 30 + 15 + 3 * 30 + 15 + 1 * 30 = 240

但模拟在3次会谈,1次休息,3次会谈和1次休息(210)之后结束。

我想知道为什么模拟直到'谈话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 <...>.

0 个答案:

没有答案