SimPy,如何整合资源中断



目前我正在创建以下医生:

doctor = simpy.Resource(self.env, capacity=3)

这些医生被要求使用以下方法对患者流进行检查:

with self.doctor.request() as req:
yield req2
*patient encounter*

我想做的是用某种疲劳水平(和其他特征(初始化医生,比如:

# Class representing a doctor and their characteristics
class Doctor(object):
def __init__(self, env, id, exp_level, fatigue):
self.env = env
self.id = id
self.exp_level = exp_level
self.fatigue = fatigue 
self.isAvailable = True

然后在遇到这种情况时,我想联系医生以增加疲劳,例如:

with self.doctor.request() as req:
yield req2
*patient encounter*
self.doctor.fatigue += 5

一旦疲劳超过阈值,医生就会休息并重置疲劳,例如:

def monitor_break(self): 
while True:
if Doctor.fatigue > 9:
Doctor.fatigue = 0
yield self.env.timeout(15)

你知道怎么做吗?

首先,我会为您的文档使用存储区,而不是使用资源。其次,我会用get覆盖来装饰商店,以检查医生是否需要小睡

像这个

"""
A simple sim where Doctors put themselves on break when they get tired
Programmer Michael R. Gibbs
"""
import simpy
import random
class Doctor():
"""
Doctor that get tired
"""
def __init__(self, env, id):
self.env = env
self.id = id
self.fatigue = 0
class Doc_Pool():
"""
decorates a simpy store
to chekc a doc's fatigue level
and doing a timeout if fatigue is too 
high before returning the doc to the pool
"""
def __init__(self, env, num_of_docs, max_fatigue):
self.env = env
self.store = simpy.Store(env)
self.store.items = [Doctor(env, i+1) for i in range(num_of_docs)]
self.max_fatigue = max_fatigue
def get(self):
"""
get a doc from the real store
"""
return self.store.get() 
def put(self, doc):
"""
put a doc in the store
unless the doc needs a break
"""
if doc.fatigue > self.max_fatigue:
# needs a break
self.env.process(self._doc_timeout(doc))
else:
self.store.put(doc)

def _doc_timeout(self, doc):
"""
gives the doc a break, then put in store
"""
print(f'{self.env.now:.0f} doctor {doc.id} starting break with fatigue {doc.fatigue:0.2f}')
yield self.env.timeout(doc.fatigue)
doc.fatigue = 0
self.store.put(doc)
print(f'{self.env.now:.0f} doctor {doc.id} ending break')

class Patient():
"""
Simple class to track patients
"""
next_id = 1
def __init__(self):
self.id = self.get_next_id()
@classmethod
def get_next_id (cls):
id =cls.next_id
cls.next_id += 1
return id
def gen_pats(env, doc_store):
"""
Generates the arrival off patients
and queue them up for a doctor visit
"""
while True:
pat = Patient()
print(f'{env.now:.0f} patient {pat.id} has arrived')
yield env.timeout(random.uniform(1,5))
env.process(doc_visit(env, pat, doc_store))
def doc_visit(env, pat, doc_store):
"""
gets a doc, do visit, add fatigue to doc, end visit
"""
doc = yield doc_store.get()
print(f'{env.now:.0f} patient {pat.id} is visiting doctor {doc.id}')
env.timeout(random.uniform(1,6))
doc.fatigue += random.uniform(1,5)
doc_store.put(doc)
print(f'{env.now:.0f} patient {pat.id} visiting doctor {doc.id} has ended')

# boot up sim
env = simpy.Environment()
doc_store = Doc_Pool(env, 3, 10)
env.process(gen_pats(env, doc_store))
env.run(200)
print('end of simulation')

最新更新