Python Simpy-我如何启动一个时钟,在后台跟踪工作班次和天数?
嗨,我是新来使用Simpy的 我有一个针对不同班次工作的工人的模拟,我想根据Python Simpy-我如何启动一个时钟,在后台跟踪工作班次和天数?,python,simpy,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(en
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中,它就不会打印一天已经结束。如何实现我想要的行为?也许这太简单了,但这对我来说很有效
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)
我觉得你把事情复杂化了。您需要的是一个简单的进程,该进程将超时到1440:
def day_timer(env):
while True:
yield env.timeout(1440)
print(print("Day is done"))
如果您想在一天结束或轮班结束时实际中断当前进程,那么您的代码将变得更加复杂。说一天结束,你希望你的员工停止工作,回家(即使是在工作中)。在这种情况下,请参阅,其中进程被中断:
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()