提问人:user_42 提问时间:10/7/2023 更新时间:10/8/2023 访问量:27
简单 - 服务时间取决于队列长度和资源可用性
Simpy - service time dependent on length of queue and resources availability
问:
我想修改此代码以解决以下问题:假设我们有两个护士。如果两个护士都有空,那么患者可以使用两个护士,治疗时间就会减少一半。只有当队列中没有任何患者等待时,患者才能同时使用两名护士。如果有两个(或更多)患者在排队,那么我们会从队列中取出 2 名患者,并将他们分配给每个护士。
import simpy
import numpy as np
np.random.seed(1)
# Define arrival and service time distributions
def draw_arrival_time():
# Use an exponential distribution for arrival times
return np.random.exponential(2.0)
def draw_service_time():
# Use a normal distribution for service times
return np.random.normal(5.0, 1.0)
# Arrivals generator function
def patient_generator(env, nurse):
patient_id = 1
while True:
# Create an instance of activity generator
patient = treatment_activity(env, nurse, patient_id)
# Run the activity generator for this patient
env.process(patient)
# Sample time until next arrival
next_arrival = draw_arrival_time()
# Freeze until that time has passed
yield env.timeout(next_arrival)
patient_id += 1
def treatment_activity(env, nurse, patient_id):
# Patient arrival
time_entered_queue = env.now
print("Patient", patient_id, "arrived at", time_entered_queue)
# Request the nurse
with nurse.request() as req:
yield req
# Calculate the time patient left the queue
time_left_queue = env.now
print("Patient", patient_id, "started treatment at", time_left_queue)
# Calculate the waiting time for this patient
waiting_time = time_left_queue - time_entered_queue
# Append the waiting time to the waiting_times list
waiting_times.append(waiting_time)
# Sample treatment time
treatment _time = draw_service_time()
yield env.timeout(treatment_time)
# Set up simulation environment
env = simpy.Environment()
# Set up resources
nurse = simpy.Resource(env, capacity=1)
# Set up parameter values
waiting_times = []
# Start patient arrivals generator
env.process(patient_generator(env, nurse))
# Run the simulation
env.run(until=90)
# Calculate and print statistics
average_waiting_time = np.mean(waiting_times)
print("Average Waiting Time:", average_waiting_time)
我尝试使用容量为 2 nurse = simpy 的资源。Resource(env, capacity=2) 但这不起作用。然后使用两名护士,每名护士可容纳 1 人,但我无法跟踪队列,也不知道如何检查他们是否都有空。
答:
0赞
Michael
10/8/2023
#1
下面是一个快速示例,患者抓住一个或两个护士,并根据护士资源请求队列大小和可用护士数量设置治疗时间。
"""
Simple simulation of nusrses treating a patient.
If there is only one patient and both nureses are available,
both nurses will treat the patient
with a half the treatment time.
If there is more then one patatient each nurse will take one patient
Programmer: Michael R. Gibbs
"""
import simpy
class Patient():
"""
Patient that needs a nurse,
has a id for tracking
"""
last_id = 0
def __init__(self):
self.__class__.last_id += +1
self.id = self.__class__.last_id
def treat_patient(env, patient, nurse_resource):
"""
Main process where a patient:
waits for a nurse (maybe 2)
gets treated
release the nurses
"""
treat_time = 6
# wait for nurse
n_req =nurse_resource.request()
yield n_req
# list of nurses to be released later
n_reqs = [n_req]
# chech if patient queue is empty
if len(nurse_resource.queue ) == 0:
#check if second nurse is available
if nurse_resource.capacity - nurse_resource.count >= 1:
# get second nurse and cut treatment time
n_req_2 = nurse_resource.request()
yield n_req_2
n_reqs.append(n_req_2)
treat_time = treat_time / 2
# start treatment (note no yield here, just drop and go)
print(f'{env.now:.1f}: patient {patient.id} is being treated by {len(n_reqs)} nurses')
yield env.timeout(treat_time)
# release all the nurses, could be 1 or 2
for req in n_reqs:
nurse_resource.release(req)
print(f'{env.now:.1f}: patient {patient.id} treatment is finished by {len(n_reqs)} nurses')
def gen_patients(env, nurse_resource):
yield env.timeout(1)
patient = Patient()
env.process(treat_patient(env, patient, nurse_resource))
print(f'{env.now:.1f}: patient {patient.id} has arrived')
yield env.timeout(1)
patient = Patient()
env.process(treat_patient(env, patient, nurse_resource))
print(f'{env.now:.1f}: patient {patient.id} has arrived')
yield env.timeout(1)
patient = Patient()
env.process(treat_patient(env, patient, nurse_resource))
print(f'{env.now:.1f}: patient {patient.id} has arrived')
yield env.timeout(30)
patient = Patient()
env.process(treat_patient(env, patient, nurse_resource))
print(f'{env.now:.1f}: patient {patient.id} has arrived')
yield env.timeout(1)
patient = Patient()
env.process(treat_patient(env, patient, nurse_resource))
print(f'{env.now:.1f}: patient {patient.id} has arrived')
env = simpy.Environment()
nurse_resource = simpy.Resource(env, capacity=2)
env.process(gen_patients(env, nurse_resource))
env.run(100)
评论