'In Simpy simulation, how can I escape from infinite loop

I have an endless loop problem in Simpy simulation.

My scenario is: After finishing triage, a patient wait until getting empty bed. However, if there is no empty bed, the patient should wait until an empty bed is available. When a patient uses a bed and leaves the hospital, the bed turns into a dirty bed. The dirty bed turns into an empty bed when the cleaner cleans it. Bed cleaning work start after getting clean request.

I used "Store" for managing beds I think I have an infinite loop when there is no empty bed and no dirty bed.

I think...

  1. Add a queue (After triage)
  2. From the queue, assign a bed based on the FIFO.
  3. If we don't have empty or dirty bed, we should wait until a dirty bed is available.

But, I don't know how to implement this idea. Please help me to solve this problem.

import simpy
import random

class Pre_Define:
    warmup_period = 1440
    sim_duration = 14400
    number_of_runs = 3  

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"

class Model:
    def __init__(self, run_number):
        self.env = simpy.Environment()
        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 = 77)
        self.bed_dirty = simpy.Store(self.env, capacity = 77)
        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(77):
            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.Process(pt))

    def Process(self, Patients):
        with self.tg.request() as req:
            yield req
            triage_service_time = random.expovariate(1.0/18)
            yield self.env.timeout(triage_service_time)

        if self.bed_clean.items != []:
            get_empty_bed_name = yield self.bed_clean.get()         
            Patients.bed_name = get_empty_bed_name
        elif self.bed_dirty.items != []:
            get_dirty_bed_name = yield self.bed_dirty.get() 
            with self.bed_cleaner.request() as req:
                yield req
                yield self.env.timeout(50)
        else:
            print("NO BED, Should Wait!!")
            no_bed = True
            while no_bed:
                #print("Waiting dirty bed")
                if self.bed_dirty.items != []:
                    get_dirty_bed_name = yield self.bed_dirty.get() 
                    print("Find dirty bed!")
                    with self.bed_cleaner.request() as req:
                        yield req
                        yield self.env.timeout(30)
                        Patients.bed_name = get_dirty_bed_name
                    no_bed = False

        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
                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
            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()


Solution 1:[1]

So you got the right idea where you use two Stores to track dirty and clean beds. The trick is to request both a clean and a dirty bed at the same time, and discard the request you do not use.

So the big changes I made was to request both a clean bed and a dirty bed and used env.any_of() to get me the first filled request. Note that both requests could get filled. Since I made two requests, that means I need to either cancel the request I do not use, or if filled, return the unused bed back to its queue. The other thing I did was to make a separate process for the cleaners. This means that the both the patients and the cleaners will be competing for dirty beds.

"""
    Quick sim model of beds in a hosptial triage

    beds have two states, clean and dirty

    When a patient arrives they are assigned to a empty bed
    If no beds are avail then the patient wait for first empty bed
    If there is both a clean bed and a empty bed, the clean bed will be assigned
    When the patient leaves triage the bed state is dirty.

    empty dirty beads are queued to be clean, after cleaning bed state is clean

    After being triaged, a patient is either admited or discharged.
    If the patient is admitted, then they wait for a IU bed befor the triage bed is empty

    Programmer: Michael R. Gibbs

"""

import simpy
import random

TRIAGE_TIME = 30
BED_CLEAN_TIME = 20

class Patient():
    """
    has id to track patient progress
    and bed when assigned to a bed
    """

    next_id = 1

    @classmethod
    def get_next_id(cls):
        id = cls.next_id
        cls.next_id += 1

        return id

    def __init__(self):
        self.id = self.get_next_id()
        self.bed = None

class Bed():
    """
    has id to track patient progress
    and bed when assigned to a bed
    """

    next_id = 1

    @classmethod
    def get_next_id(cls):
        id = cls.next_id
        cls.next_id += 1

        return id

    def __init__(self):
        self.id = self.get_next_id()
        self.bed_state = 'Clean'

def clean_beds(env, dirty_bed_q, clean_bed_q):
    """
        Sim process for cleaning dirty beds from
        the dirty queue, and putting the clean
        beds into clean queue

        A instance shoud be started for each cleaner
    """

    while True:
        bed = yield dirty_bed_q.get()
        print(f'{env.now:.2f} bed {bed.id} is being cleaned')

        # clean
        yield env.timeout(BED_CLEAN_TIME)
        bed.bed_state = "Clean"

        clean_bed_q.put(bed)
        print(f'{env.now:.2f} bed {bed.id} is clean')

def triage(env, pat, clean_bed_q, dirty_bed_q, ui_bed_q):
    """
        models the patients life cycle in triage
        stepss are:
            get bed (clean perfered)
            get triaged
            leave
        if addmited leaving is blocked until a ui bed is found
    """

    # get bed
    clean_req = clean_bed_q.get()
    dirty_req = dirty_bed_q.get()

    print(f'{env.now:.2f} patient {pat.id} has arrived')

    fired = yield env.any_of([clean_req, dirty_req])

    # see if we got a clean or dirty or both
    if clean_req in fired:
        # got a clean bead
        pat.bed = fired[clean_req]

        # need to deal with the dirty req
        if dirty_req in fired:
            # got two beds but dirty back
            dirty_bed_q.put(fired[dirty_req])
        else:
            # stil need to cancel req
            dirty_req.cancel()
    else:
        # we have a dirty bed
        pat.bed = fired[dirty_req]

        # need to deal with the dirty req
        if clean_req in fired:
            # got two beds but dirty back
            clean_bed_q.put(fired[clean_req])
        else:
            # stil need to cancel req
            clean_req.cancel()

    print(f'{env.now:.2f} {pat.bed.bed_state} bed {pat.bed.id} is occupied and dirty with patient {pat.id}')

    pat.bed.bed_state = 'Dirty'

    # triage
    yield env.timeout(TRIAGE_TIME)

    admit = (random.uniform(0,1) < 0.7)

    if admit:
        # need to get a IU bed before
        # patient gives up triage bed

        print(f'{env.now:.2f} patient {pat.id} is being admitted')
        ui_req = ui_bed_q.request()
        yield ui_req
        print(f'{env.now:.2f} patient {pat.id} got iu bed')

    # patient leaves triage
    # give up dirty bed
    dirty_bed_q.put(pat.bed)

    print(f'{env.now:.2f} bed {pat.bed.id} is empty and patient {pat.id} has left')


def gen_pats(env, clean_bed_q, dirty_bed_q, iu_bed_q):
    """
        creates the arriving patients
    """

    while True:
        yield env.timeout(random.uniform(1,8))
        
        pat = Patient()

        # not each patient gets their own process
        # so if one patient blocks while waiting for a iu bed, it does
        # not block the other patients
        env.process(triage(env, pat, clean_bed_q, dirty_bed_q, iu_bed_q))

def model():
    env = simpy.Environment()

    # make queues
    clean_bed_q = simpy.Store(env)
    dirty_bed_q = simpy.Store(env)

    iu_bed_q = simpy.Resource(env,capacity=5)

    clean_bed_q.items = [Bed() for _ in range(10)]

    # start generating patients
    env.process(gen_pats(env, clean_bed_q, dirty_bed_q, iu_bed_q))

    # star up cleaners
    for _ in range(2):
        env.process(clean_beds(env, dirty_bed_q, clean_bed_q))

    env.run(until=100)

    print('simulation finish')

model()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael