Simply - 服务时间取决于队列长度和资源可用性

问题描述 投票:0回答:1

我想修改这段代码来解决以下问题:假设我们有两名护士。如果两名护士都有空,那么患者就可以同时使用两名护士,治疗时间就会减少一半。仅当没有任何患者在队列中等待时,患者才可以使用两名护士。如果队列中有两名(或更多)患者,那么我们会从队列中取出 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的资源护士= simpy.Resource(env,capacity = 2)但这不起作用。然后使用了两名护士,每人容量为 1,但我无法跟踪队列,也不知道如何检查两人是否都有空。

python simulation yield simpy event-simulation
1个回答
0
投票

这是一个患者抓住一名或两名护士并根据护士资源请求队列大小和可用护士数量设置治疗时间的简单示例。

"""
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)
© www.soinside.com 2019 - 2024. All rights reserved.