如何模拟允许等到下一个过程解决?
<这是简短的场景> 患者到达ED(急诊科)。当患者到达ED时,他们首先从护士那里获得分类。分类后,将为患者分配一张ED床,但是如果患者没有干净的床,他们必须等到脏床。 (当患者出院或进入IU时(住院单位)时,发生脏床。系统应给出清洁顺序。 第二是,如果没有脏床,这意味着现在正在清洁肮脏的床或等到病人出院或去IU(发生脏床的情况)。由医生治疗。检查医生后,患者要么去IU或出院。当患者去IU时,如果在IU中有一张干净的床,则立即将ED床退回,但是如果没有,则必须继续在ED床上等待。 而且,如果患者从医院出院,则将退还ED床,但不会立即进行清洁。(在发出清洁命令之前,将其弄脏)根据要求清洁脏床,然后分配给新来的患者。无论如何,我现在很难实施程序的一部分是,分配ED床的部分并未按照我的意图实施。
因此,我的要点是
- 患者应清理ED床。
- 如果没有清洁床,请病人等到可用的床。 (等待患者将继续等待,但是其他患者的过程将继续。)
- 当患者离开ED时,患者使用的床仍然很脏,但是如果有清洁新患者的要求,则脏床应清洁。
我添加了“ bed_mgmt”课程来解决这个问题,但我做不到。请给我一些指导方针。
import simpy
import random
class Pre_Define:
warmup_period = 1440
sim_duration = 14400
number_of_runs = 3
n_edbed = 77
class Patients:
def __init__(self, p_id):
self.id = p_id
self.bed_name = ""
self.admission_decision = ""
def admin_decision(self):
admin_decision_prob = random.uniform(0, 1)
if admin_decision_prob <= 0.7:
self.admission_decision = "DIS"
else:
self.admission_decision = "IU"
class Bed_Mgmt:
def __init__(self, env):
self.env = env
def check_q(self, Pt_id, pt_ed_q, bed_clean, bed_dirty, bed_cleaner): #, pt_ed_q, pt_ed_q, dirty_bed_q
if pt_ed_q != [] and bed_clean != []:
Patients.bed_name = bed_clean.get()
Pt_id = pt_ed_q.get()
elif pt_ed_q != [] and bed_clean == [] and bed_dirty != []:
get_dirty_bed_name = bed_dirty.get()
with bed_cleaner.request() as req:
yield req
yield self.env.timeout(50)
Patients.bed_name = get_dirty_bed_name
else: # self.pt_ed_q != [] and self.clean_bed_q == [] and self.dirty_bed_q == []:
pass # generate patient
class Model:
def __init__(self, run_number):
self.env = simpy.Environment()
self.pt_ed_q = simpy.Store(self.env )
self.pt_counter = 0
self.tg = simpy.Resource(self.env, capacity = 4)
self.physician = simpy.Resource(self.env, capacity = 4)
self.bed_clean = simpy.Store(self.env, capacity = Pre_Define.n_edbed)
self.bed_dirty = simpy.Store(self.env, capacity = Pre_Define.n_edbed)
self.IU_bed = simpy.Resource(self.env, capacity = 50)
self.bed_cleaner = simpy.Resource(self.env, capacity = 2)
self.run_number = run_number
def generate_beds(self):
for i in range(Pre_Define.n_edbed):
yield self.env.timeout(0)
yield self.bed_clean.put(f'bed{i}')
print(self.bed_clean.items)
def generate_pt_arrivals(self):
while True:
self.pt_counter += 1
pt = Patients(self.pt_counter)
yield self.env.timeout(1/7)
self.env.process(self.ED_Process(pt))
#self.pt_ed_q.put(pt.id)
def Process(self, Patients, Bed_Mgmt):
with self.tg.request() as req:
yield req
triage_service_time = random.expovariate(1.0/18)
yield self.env.timeout(triage_service_time)
yield self.pt_ed_q.put(Patients.id)
pt_id = yield self.pt_ed_q.get()
Bed_Mgmt.check_q(pt_id, self.pt_ed_q, self.bed_clean, self.bed_dirty, self.bed_cleaner)
with self.physician.request() as req:
yield req
yield self.env.timeout(10)
Patients.admin_decision()
if Patients.admission_decision == "DIS":
with self.IU_bed.request() as req:
yield req
Bed_Mgmt.check_q(pt_id, self.pt_ed_q, self.bed_clean, self.bed_dirty, self.bed_cleaner)
yield self.env.timeout(600)
get_dirty_bed_name = Patients.bed_name
yield self.bed_dirty.put(get_dirty_bed_name)
else:
get_dirty_bed_name = Patients.bed_name
Bed_Mgmt.check_q(pt_id, self.pt_ed_q, self.bed_clean, self.bed_dirty, self.bed_cleaner)
yield self.bed_dirty.put(get_dirty_bed_name)
def run(self):
self.env.process(self.generate_pt_arrivals())
self.env.process(self.generate_beds())
self.env.run(until = Pre_Define.warmup_period + Pre_Define.sim_duration)
for run in range(Pre_Define.number_of_runs):
run_model = Model(run)
run_model.run()
print()
< This is brief scenario >
Patients arrive at ED(Emergency Department). When patients arrive at the ED, they first receive a triage from the nurse. After the triage, patients will be assigned an ED bed, but if patients don't have a clean bed, they have to wait until get a dirty bed. (Dirty beds occur when the patient is discharged or goes to the IU(Inpatient Unit).) The meaning of waiting here has two cases, the first is that when the patient arrives, if there is only a dirty bed(not cleaned), system should give an order of cleaning.
The second is that if there is no dirty bed, this means dirty beds are being cleaned now or wait until patients are discharged or goes to the IU(the event of a dirty bed occurs.) When an ED bed is assigned, the patient is treated by a doctor. After examination of doctor, the patient will either go to the IU or be discharged. When the patient goes to the IU, if there is a clean bed in IU, the ED bed is returned immediately, but if not, the patient must continue to wait in the ED bed.
And if the patient is discharged from the hospital, the ED bed will be returned, but cleaning will not proceed immediately.(Leave it dirty until the cleaning order is issued) Dirty ED beds are cleaned on request and then assigned to new arriving patients. Anyway, the part where I'm having a hard time implementing my program right now is that the part where assigning the ED bed is not being implemented as I intended.
So, my main point of the problem is that
- Patients should get cleaned ED bed.
- If there is no cleaned bed, patient wait until a cleaned bed available.
(Waiting patients will continue to wait, but the process of other patients will continue.) - When a patient leave the ED, the bed used by the patient remains dirty, but if there is a request to clean for new patient, the dirty bed should be cleaned.
I added 'Bed_Mgmt' class to solve this, but I couldn't. Please give me some guidelines.
import simpy
import random
class Pre_Define:
warmup_period = 1440
sim_duration = 14400
number_of_runs = 3
n_edbed = 77
class Patients:
def __init__(self, p_id):
self.id = p_id
self.bed_name = ""
self.admission_decision = ""
def admin_decision(self):
admin_decision_prob = random.uniform(0, 1)
if admin_decision_prob <= 0.7:
self.admission_decision = "DIS"
else:
self.admission_decision = "IU"
class Bed_Mgmt:
def __init__(self, env):
self.env = env
def check_q(self, Pt_id, pt_ed_q, bed_clean, bed_dirty, bed_cleaner): #, pt_ed_q, pt_ed_q, dirty_bed_q
if pt_ed_q != [] and bed_clean != []:
Patients.bed_name = bed_clean.get()
Pt_id = pt_ed_q.get()
elif pt_ed_q != [] and bed_clean == [] and bed_dirty != []:
get_dirty_bed_name = bed_dirty.get()
with bed_cleaner.request() as req:
yield req
yield self.env.timeout(50)
Patients.bed_name = get_dirty_bed_name
else: # self.pt_ed_q != [] and self.clean_bed_q == [] and self.dirty_bed_q == []:
pass # generate patient
class Model:
def __init__(self, run_number):
self.env = simpy.Environment()
self.pt_ed_q = simpy.Store(self.env )
self.pt_counter = 0
self.tg = simpy.Resource(self.env, capacity = 4)
self.physician = simpy.Resource(self.env, capacity = 4)
self.bed_clean = simpy.Store(self.env, capacity = Pre_Define.n_edbed)
self.bed_dirty = simpy.Store(self.env, capacity = Pre_Define.n_edbed)
self.IU_bed = simpy.Resource(self.env, capacity = 50)
self.bed_cleaner = simpy.Resource(self.env, capacity = 2)
self.run_number = run_number
def generate_beds(self):
for i in range(Pre_Define.n_edbed):
yield self.env.timeout(0)
yield self.bed_clean.put(f'bed{i}')
print(self.bed_clean.items)
def generate_pt_arrivals(self):
while True:
self.pt_counter += 1
pt = Patients(self.pt_counter)
yield self.env.timeout(1/7)
self.env.process(self.ED_Process(pt))
#self.pt_ed_q.put(pt.id)
def Process(self, Patients, Bed_Mgmt):
with self.tg.request() as req:
yield req
triage_service_time = random.expovariate(1.0/18)
yield self.env.timeout(triage_service_time)
yield self.pt_ed_q.put(Patients.id)
pt_id = yield self.pt_ed_q.get()
Bed_Mgmt.check_q(pt_id, self.pt_ed_q, self.bed_clean, self.bed_dirty, self.bed_cleaner)
with self.physician.request() as req:
yield req
yield self.env.timeout(10)
Patients.admin_decision()
if Patients.admission_decision == "DIS":
with self.IU_bed.request() as req:
yield req
Bed_Mgmt.check_q(pt_id, self.pt_ed_q, self.bed_clean, self.bed_dirty, self.bed_cleaner)
yield self.env.timeout(600)
get_dirty_bed_name = Patients.bed_name
yield self.bed_dirty.put(get_dirty_bed_name)
else:
get_dirty_bed_name = Patients.bed_name
Bed_Mgmt.check_q(pt_id, self.pt_ed_q, self.bed_clean, self.bed_dirty, self.bed_cleaner)
yield self.bed_dirty.put(get_dirty_bed_name)
def run(self):
self.env.process(self.generate_pt_arrivals())
self.env.process(self.generate_beds())
self.env.run(until = Pre_Define.warmup_period + Pre_Define.sim_duration)
for run in range(Pre_Define.number_of_runs):
run_model = Model(run)
run_model.run()
print()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,这清除了一些事情。您会在日志开始时看到,分类后立即获得SA床,因为队列中有床。 a木头的末端您会看到患者没有床,直到清洁器完成清洁床,显示干净的床队列是空的。关于simpy的好处之一。存储是管理您的请求,因此您不需要检查队列是否为空。另外, https://realpython.com/ 是学习python的好网站。
So This cleans up a few things. You will see at the start of the log that a patient get s a bed right after triage finishes, because there are beds in the queue. A the end of the log you will see patients do not get a bed until a cleaner finishes cleaning a bed, showing the clean bed queue is empty. One of the nice things about simpy.Store is it manages the requests for you so you do not need to do any checking if the queue is empty. Also https://realpython.com/ is a good site for learning python.