我在Simpy面临同步问题。通过这种方式,我的意思是计算机不会按照我想要的顺序处理事件。我一直在寻找更多信息,而不是Simpy文档中关于事件如何由计算机排队,排序和处理的信息。我发现它到处都是根据它们被触发的时间排序的。在阅读剩下的内容之前,是否有人会有任何链接或文件通知我?
更具体地说,我正在尝试建模和模拟真实世界系统(PoolSystem类的一个实例),这是一个子系统池,可以进一步细分为子系统或能够失败(最后一个)系统类别称为AtomicSystem)。总而言之,PoolSystem由子系统组成,可以是PoolSystem或AtomicSystem。
例如,汽车可以是此PoolSystem类的一个实例,其中引擎作为子系统。但是发动机可以分解成几个其他子系统,例如活塞或火花塞,这实际上可能会失效。在这种情况下,引擎将被定义为PoolSystem实例,活塞和引发插件将被定义为AtomicSystem实例。
AtomicSystem和PoolSystem类基于相同的标准模型。他们都有:
我希望以下架构可以帮助您理解您刚读到的内容: The breakdown of a car defined as a Pool System
在此模式中,汽车被定义为PoolSystem实例,其子系统是引擎和轮胎。轮胎可能是汽车故障的原因,因此它被定义为AtomicSystem实例。该引擎被定义为另一个PoolSystem,其子系统是活塞和火花塞,它们可以失败,因此定义为AtomicSystem实例。
AtomicSystem类可以在下面找到:
class AtomicSystem(object):
def __init__(self, env, mtbd, backlog, user_defined_critical=True, ids=None):
self.env = env # environment()
self.mtbd = mtbd # mean time between dysfunction
self.critical = user_defined_critical # boolean
self.ids = ids # list of strings
self.ttd = self.time_to_dysfunction() # time before dysfunction
self.update_order = self.env.event()
self.dysfunction_signal = self.env.event()
self.interrupted = self.env.event()
self.update_end = self.env.event()
self.lifecycle = self.env.process(self.run(backlog))
def time_to_dysfunction(self):
return self.mtbd
def run(self, backlog):
# the atomic system starts service when its update_order event is triggered
yield self.update_order
print("t = " + str(self.env.now) + " : " + self.ids[-1] + " starts service.")
self.update_order = self.env.event()
# atomic system specifies to higher level system that it has started service
self.update_end.succeed()
self.update_end = self.env.event()
try:
# as long as the atomic system remains in this while loop, it is said to be in service.
while True:
start = self.env.now
time_out = self.env.timeout(self.ttd)
# wait for a dysfunction (time_out) or interruption (interrupted) or an update from a higher level system (update_order)
result = yield time_out | self.interrupted | self.update_order
if time_out in result:
print("t = " + str(self.env.now) + " : " + self.ids[-1] + " fails.")
# if the atomic system fails, trigger dysfunction_signal event destined to be detected by higher level system
self.dysfunction_signal.succeed()
# when the atomic system fails, its interrupted event is automatically triggered
self.interrupted.succeed()
if self.ttd > 0:
backlog.append({"Dysfunction time": self.env.now, "IDs": self.ids})
self.ttd = 0
if self.interrupted.triggered:
print("t = " + str(self.env.now) + " : " + self.ids[-1] + " interrupts service.")
if self.ttd > 0:
operation_duration = self.env.now - start
self.ttd -= operation_duration
# the atomic system waits for update_order trigger when it has been interrupted
yield self.update_order
if self.update_order.triggered:
# here, the atomic system returns to service
print("t = " + str(self.env.now) + " : " + self.ids[-1] + " is updated.")
if self.ttd > 0:
operation_duration = self.env.now - start
self.ttd -= operation_duration
self.update_end.succeed()
self.update_order = self.env.event()
self.dysfunction_signal = self.env.event()
self.interrupted = self.env.event()
self.update_end = self.env.event()
except:
# here the atomic system is terminated (end of service)
print("t = " + str(self.env.now) + " : " + self.ids[-1] + " is terminated.")
self.env.exit()
PoolSystem类可以在下面找到:
class PoolSystem(object):
def __init__(self, env, id, init_subsystems, user_defined_critical=True):
self.env = env
self.id = id
self.subsystems = init_subsystems
self.working_subsystems = [self.subsystems[key] for key in self.subsystems.keys()]
self.critical = user_defined_critical
self.update_order = self.env.event()
self.dysfunction_signal = simpy.AnyOf(self.env, [syst.dysfunction_signal for syst in self.working_subsystems])
self.interrupted = self.env.event()
self.update_end = self.env.event()
self.lifecycle = self.env.process(self.run())
def start_subsystems(self):
for key in self.subsystems.keys():
self.subsystems[key].update_order.succeed()
def run(self):
user_defined_critical = self.critical
# the pool system is started here when its update_order event is triggered
yield self.update_order
print("t = " + str(self.env.now) + " : " + self.id + " starts service.")
self.update_order = self.env.event()
# Here, the pool system starts all of its subsystems (which can be atomic and/or pool systems)
self.start_subsystems()
# here, update_end is triggered if all the update_end events of the subsystems have been triggered
self.update_end = simpy.AllOf(self.env, [self.subsystems[key].update_end for key in self.subsystems.keys()])
yield self.update_end
try:
while True:
# wait for a dysfunction (dysfunction_signal), interruption (interrupted) or an update from a higher level system (update_order)
yield self.dysfunction_signal | self.interrupted | self.update_order
if self.dysfunction_signal.triggered:
crit = []
for syst in self.working_subsystems:
if syst.dysfunction_signal.triggered:
crit.append(syst.critical)
if True in crit: # if one of the failed subsystems is critical (critical = True), then trigger interrupted event()
print("t = " + str(self.env.now) + " : " + self.id + " fails completely.")
# pool system is interrupted
self.critical = user_defined_critical
self.interrupted.succeed()
else:
# no critical subsystem has failed yet so the pool system can continue working (no interruption here)
self.critical = False
self.working_subsystems = [self.subsystems[key] for key in self.subsystems.keys() if
not self.subsystems[key].interrupted.triggered]
if len(self.working_subsystems) is not 0:
print("t = " + str(self.env.now) + " : " + self.id + " fails partially.")
self.dysfunction_signal = simpy.AnyOf(self.env, [syst.dysfunction_signal for syst in
self.working_subsystems])
else:
# pool system is interrupted if all of its subsystems have failed
print("t = " + str(self.env.now) + " : " + self.id + " fails completely (no working EUs).")
self.interrupted.succeed()
if self.interrupted.triggered:
print("t = " + str(self.env.now) + " : " + self.id + " interrupts service.")
# interrupt all subsystems
for key in self.subsystems.keys():
if not self.subsystems[key].interrupted.triggered:
self.subsystems[key].interrupted.succeed()
# waits for update_order from higher level system
yield self.update_order
if self.update_order.triggered:
print("t = " + str(self.env.now) + " : " + self.id + " is updated.")
# update_order has been troggered by higher level system
self.update_order = self.env.event()
self.start_subsystems()
self.update_end = simpy.AllOf(self.env,
[self.subsystems[key].update_end for key in self.subsystems.keys()])
# wait for the end of the update of the subsystems
yield self.update_end
print("t = " + str(self.env.now) + " : " + self.id + " receives update-end signal.")
self.working_subsystems = [self.subsystems[key] for key in self.subsystems.keys()]
self.dysfunction_signal = simpy.AnyOf(self.env,
[syst.dysfunction_signal for syst in self.working_subsystems])
self.interrupted = self.env.event()
except simpy.Interrupt:
# here the pool system is terminated, it leaves service.
for key in self.subsystems.keys():
self.subsystems[key].lifecycle.interrupt()
self.env.exit()
我已经定义了另外两个类,Eu(继承自AtomicSystem)和ModSat(继承自PoolSystem)。基本上,我正在用几个Eu对象(只有两个系统级别)构建一个modsat对象。我已经发布了以下代码:
class Eu(AtomicSystem):
def __init__(self, env, identity, mtbd, backlog, critical=True, ids=None):
self.id = identity
ids.append(self.id)
AtomicSystem.__init__(self, env, mtbd, backlog, critical, ids)
class ModSat(PoolSystem):
def __init__(self, env, digit_id, eu_mtbds_criticals, backlog, critical=True):
identity = "ModSat" + str(digit_id)
self.eus = self.initialize(env, identity, eu_mtbds_criticals, backlog)
PoolSystem.__init__(self, env, identity, self.eus, critical)
def initialize(self, env, identity, eu_mtbds_criticals, backlog):
eus = {}
for i in range(1, len(eu_mtbds_criticals) + 1):
eu_id = "EU" + str(i) + ":" + identity
eu = Eu(env, eu_id, eu_mtbds_criticals[i - 1][0], backlog, eu_mtbds_criticals[i - 1][1], [identity])
eus[eu_id] = eu
return eus
最后,我想测试ModSat对象,看看我是否可以轻松替换modsat对象的一个失败的子系统(Eu类型),而不会影响modsat的良好行为。我创建了一个模拟函数,使我能够与modsat对象进行交互。我用以下定义的2个modsat对象运行测试:
backlog = []
eu_mtbds_criticals1 = [[5, False], [11, False], [19, False]]
eu_mtbds_criticals2 = [[4, False], [27, False], [38, False]]
env = simpy.Environment()
sat1 = ModSat(env, 1, eu_mtbds_criticals1, backlog, True)
sat2 = ModSat(env, 2, eu_mtbds_criticals2, backlog, True)
constellation = {'ModSat1': sat1, 'ModSat2': sat2}
env.process(simulate(constellation, env, backlog))
env.run(until=100)
第一个测试非常简单,具有以下模拟功能:
def simulate(constellation, env, backlog):
for key in constellation.keys():
# start service of each ModSat object included in the constellation dictionary,
# by triggering their update_order event.
constellation[key].update_order.succeed()
# wait for a while to be sure that the modsat objects have been completely simulated.
yield env.timeout(50)
输出是我想要的,因为所有事件似乎都是由计算机以正确的顺序触发和处理的:
# the 1st update_order event of PoolSystem is triggered
t = 0 : ModSat1 starts service.
t = 0 : ModSat2 starts service.
# the 1st update_order event of AtomicSystem is triggered
t = 0 : EU1:ModSat1 starts service.
t = 0 : EU3:ModSat1 starts service.
t = 0 : EU2:ModSat1 starts service.
t = 0 : EU2:ModSat2 starts service.
t = 0 : EU1:ModSat2 starts service.
t = 0 : EU3:ModSat2 starts service.
# 1st failure here. Since critical attribute of EU1:ModSat2 is set to False ModSat2 is not interrupted (partial failure)
t = 4 : EU1:ModSat2 fails.
t = 4 : EU1:ModSat2 interrupts service.
t = 4 : ModSat2 fails partially.
# 2nd failure here
t = 5 : EU1:ModSat1 fails.
t = 5 : EU1:ModSat1 interrupts service.
t = 5 : ModSat1 fails partially.
t = 11 : EU2:ModSat1 fails.
t = 11 : EU2:ModSat1 interrupts service.
t = 11 : ModSat1 fails partially.
# here the last failure of ModSat1: ModSat1 is interrupted because it has no more working Eus
t = 19 : EU3:ModSat1 fails.
t = 19 : EU3:ModSat1 interrupts service.
t = 19 : ModSat1 fails completely (no working EUs).
t = 19 : ModSat1 interrupts service.
t = 27 : EU2:ModSat2 fails.
t = 27 : EU2:ModSat2 interrupts service.
t = 27 : ModSat2 fails partially.
# here the last failure of ModSat2: ModSat2 is interrupted because it has no more working Eus
t = 38 : EU3:ModSat2 fails.
t = 38 : EU3:ModSat2 interrupts service.
t = 38 : ModSat2 fails completely (no working EUs).
t = 38 : ModSat2 interrupts service.
现在,我想用以下模拟函数测试我的代码:
def simulate(constellation, env, backlog):
for key in constellation.keys():
# start service of each ModSat object included in the constellation dictionary,
# by triggering their update_order event.
constellation[key].update_order.succeed()
# detect failure
request_signal = simpy.AnyOf(env, [constellation[key].dysfunction_signal for key in constellation.keys()])
yield request_signal
# The servicer's backlog is updated with the first item of the backlog list
print("t = " + str(env.now) + " : a service request is detected.")
servicer_backlog = []
servicer_backlog.append(backlog[0])
del backlog[0]
# the next line models the servicer time of service
yield env.timeout(5)
# The servicer gets the ID of the failed Eu to replace from its backlog
sat_id = servicer_backlog[0]['IDs'][0]
eu_id = servicer_backlog[0]['IDs'][1]
failed_eu = constellation[sat_id].eus[eu_id]
# the servicer gives the values of the attributes of the failed EU to the new EU
new_eu = Eu(failed_eu.env, failed_eu.id, failed_eu.mtbd, backlog, failed_eu.critical, failed_eu.ids)
# the failed eu is terminated (its service ends)
failed_eu.lifecycle.interrupt()
# the new EU replaces the failed_eu
constellation[sat_id].eus[eu_id] = new_eu
# the modsat concerned by the replacement has its update_order event triggered
constellation[sat_id].update_order.succeed()
print("t = " + str(env.now) + " : a service is provided")
上面的模拟函数只是模拟用新的替换第一个失败的Eu。输出是:
# the 1st update_order event of PoolSystem is triggered
t = 0 : ModSat1 starts service.
t = 0 : ModSat2 starts service.
# the 1st update_order event of AtomicSystem is triggered
t = 0 : EU3:ModSat1 starts service.
t = 0 : EU2:ModSat1 starts service.
t = 0 : EU1:ModSat1 starts service.
t = 0 : EU1:ModSat2 starts service.
t = 0 : EU2:ModSat2 starts service.
t = 0 : EU3:ModSat2 starts service.
t = 0 : ModSat1 receives update-end signal.
t = 0 : ModSat2 receives update-end signal.
# the first Eu of modsat2 fails, and its failure is detected by the simulate function
t = 4 : EU1:ModSat2 fails.
t = 4 : EU1:ModSat2 interrupts service.
t = 4 : a service request is detected.
t = 4 : ModSat2 fails partially.
# HERE IS MY CONCERN: at time t = 5, EU1 of modsat1 fails and interrupts service. However, there should be a line "t = 5 : ModSat1 fails partially" which does not appear...
t = 5 : EU1:ModSat1 fails.
t = 5 : EU1:ModSat1 interrupts service.
t = 9 : a service is provided
t = 9 : EU1:ModSat2 is terminated.
t = 9 : ModSat2 is updated.
t = 9 : EU1:ModSat2 starts service.
t = 9 : EU2:ModSat2 is updated.
t = 9 : EU3:ModSat2 is updated.
t = 9 : ModSat2 receives update-end signal.
t = 11 : EU2:ModSat1 fails.
t = 11 : EU2:ModSat1 interrupts service.
t = 13 : EU1:ModSat2 fails.
t = 13 : EU1:ModSat2 interrupts service.
t = 13 : ModSat2 fails partially.
t = 19 : EU3:ModSat1 fails.
t = 19 : EU3:ModSat1 interrupts service.
t = 27 : EU2:ModSat2 fails.
t = 27 : EU2:ModSat2 interrupts service.
t = 27 : ModSat2 fails partially.
t = 38 : EU3:ModSat2 fails.
t = 38 : EU3:ModSat2 interrupts service.
t = 38 : ModSat2 fails completely (no working EUs).
t = 38 : ModSat2 interrupts service.
如上所述,在“t = 5:EU1:ModSat1中断服务”之后,“t = 5:ModSat1部分失败”应该有一行。但相反,计算机在模拟函数的“yield env.timeout(5)”之后直接跳转到第一行。
我不明白这里发生了什么,我认为这是因为我缺乏关于Simpy如何定义和排序事件队列的知识。我在网上找不到任何有关这里发生的事情的提示。我还没有在stackoverflow和其他论坛上看到过这种问题。我很乐意感谢任何帮助。
我的代码解释很长,所以我希望我发布的代码中的注释足够:\
非常感谢!