在Simpy中,我面临一个同步问题。我的意思是,事件不是按照我想要的顺序由计算机处理的。我一直在寻找比Simpy文档中更多的关于事件是如何由计算机排队、排序和处理的信息。我在任何地方都发现,它是按照它们必须在何时触发的时间来排序的。在阅读剩下的文章之前,是否有人会有任何链接或文件给我提供建议?
更具体地说,我试图建模和模拟现实世界系统( PoolSystem类的一个实例),这是一个子系统池,可以进一步细分为子子系统,也可以失败(最后一类系统称为AtomicSystem)。总之,PoolSystem是由可以是PoolSystem或AtomicSystem的子系统组成的。
例如,一个car可以是这个PoolSystem类的一个实例,其中一个引擎作为子系统。但是,发动机可以分解成其他几个子系统,如活塞或火花塞,这实际上可能会失败。在这种情况下,引擎将定义为PoolSystem实例,活塞和火花塞定义为AtomicSystem实例。
AtomicSystem和PoolSystem类基于相同的标准模型。他们都有:
我希望下面的模式可以帮助您理解您刚才读到的内容:被定义为池系统的汽车的故障
在这个模式中,car被定义为一个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")上面的模拟函数只是将第一个失败的欧盟替换为一个新的欧盟。产出如下:
# 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中断服务”之后,应该有一行“t=5:ModSat1故障部分失败”。相反,计算机直接跳到模拟函数的“env.timeout(5)”之后的第一行。
我不明白这里发生了什么,我认为这是因为我对如何用Simpy定义和排序事件队列缺乏了解。我在网上找不到任何关于这里发生的事情的提示。在堆叠式论坛和其他论坛上,我没有看到任何这样的问题。我很高兴能得到任何帮助。
我的代码很长,所以我希望我发布的代码中的注释足够:\
非常感谢!
发布于 2016-12-12 11:07:48
我(终于)在SimPy上开始了一本关于时间的指南。它仍然是WIP,但您可以继续讨论这里。
https://stackoverflow.com/questions/40444437
复制相似问题