Simpy - 如何启动一个时钟来跟踪工作班次和后台工作日?

时间:2017-04-28 17:27:24

标签: python simpy

嗨,我是新手使用Simpy。

我为那些工作各种班次的工作人员进行了模拟,我想跟踪现在正在发生的转变,以及它基于env.now的哪一天,但我遇到了麻烦。

我希望我的模拟能够运行,但我想要某种形式的时钟打印出一个班次已经结束或者一天结束等等。

def do_task(env):
    yield env.timeout(30) # each task takes 30 minutes to complete
    print("Completed task")

def start(env):
    while True:
        # at the end of 24 hours (24 * 60)...
        if env.now != 0 and env.now % 1440 == 0:
            print("Day is done")
        # shift is 4 hours (4 * 60)..
        elif env.now != 0 and env.now % 240 == 0:
            print("Shift is done")

        yield env.process(do_task(env)) # I want this to be running with the above print statements printing stuff at the right times.


env = simpy.Environment()
env.process(start(env))
env.run(until = 7200) # 3 days (3 * 24 * 60 minutes)

上面的代码当然不起作用。声明env.now % 1440 == 0并不是很有效,因为如果超时过程没有完全考虑到1440,它就不会打印当天就完成了。我如何实现我想要的行为?

2 个答案:

答案 0 :(得分:0)

也许这太简单了,但这对我有用。

def ShiftClock(env):
shift=1
while True:
    if env.now % 8*3600 == 0:
        shift+=1
        if shift>3:shift=1
    print(shift)
env=simpy.Environment()
TimeClock=env.process(ShiftClock(env))
env.run(until=7*24*3600)

答案 1 :(得分:0)

我认为你过度复杂了。你需要的是一个简单的过程,超时到1440:

def day_timer(env):
    while True:
        yield env.timeout(1440)
        print(print("Day is done"))

如果您希望在当天结束或轮班结束时实际中断当前进程,您的代码将变得更加复杂。假设它到了一天结束,你希望你的工人停止工作并回家(即使在工作中)。在这种情况下,请参考进程被中断的machine shop example

class Machine(object):
    """A machine produces parts and my get broken every now and then.

    If it breaks, it requests a *repairman* and continues the production
    after the it is repaired.

    A machine has a *name* and a numberof *parts_made* thus far.

    """
    def __init__(self, env, name, repairman):
        self.env = env
        self.name = name
        self.parts_made = 0
        self.broken = False

        # Start "working" and "break_machine" processes for this machine.
        self.process = env.process(self.working(repairman))
        env.process(self.break_machine())

    def working(self, repairman):
        """Produce parts as long as the simulation runs.

        While making a part, the machine may break multiple times.
        Request a repairman when this happens.

        """
        while True:
            # Start making a new part
            done_in = time_per_part()
            while done_in:
                try:
                    # Working on the part
                    start = self.env.now
                    yield self.env.timeout(done_in)
                    done_in = 0  # Set to 0 to exit while loop.

                except simpy.Interrupt:
                    self.broken = True
                    done_in -= self.env.now - start  # How much time left?

                    # Request a repairman. This will preempt its "other_job".
                    with repairman.request(priority=1) as req:
                        yield req
                        yield self.env.timeout(REPAIR_TIME)

                    self.broken = False

            # Part is done.
            self.parts_made += 1

    def break_machine(self):
        """Break the machine every now and then."""
        while True:
            yield self.env.timeout(time_to_failure())
            if not self.broken:
                # Only break the machine if it is currently working.
                self.process.interrupt()