I have a project where a patient comes to a hospital for an appointment and he must be allocated a doctor from a set of doctors based on round robin logic. How would i do this? and what is the over all concept of round robin?
How to implement round robin in an application?
Collapse
X
-
-
Hey.
As I understand it, Round Robin is used for CPU scheduling, where each process is allocated CPU time in turn, with no process taking priority.
For a doctor application, I would use a FIFO (first in, first out) approach. It's the most "natural" approach. - That is; while there are more patients then there are doctors, each patient would be assigned a doctor as soon as one became available. The first patient to come in would - naturally - be the first to be assigned a doctor. - But while there are more doctors then there are patients, the first doctor to become free would be assigned the next patient to come in. Once a doctor becomes available, he is put at the end of the queue and could take a break while the doctors in front of him/her in the line are assigned the incoming patients, until it is his/her turn.
... Come to think about it, that is probably how the the "Round-Robin" CPU scheduling is implemented as well. -
but each doctor has multiple slots where in he sees a patient like every 15mins is a slot for a doctor. like this he will have around say 150 slots in a day,this condition has also to be taken care of.Comment
-
Ok, so each doctor only has a limited amount of time per patient? - That makes this even easier to schedule. You just go through the doctors in a loop, assigning the next patient to the next available slot for the next doctor in the loop.
I mean, it could be written like this, in pseudo-code:
[code=text]patient = a stream of incomming patients
doctor = a queue of doctors, each with a set number of slots
When a new patient arrives:
If next doctor in the queue has an available slot:
assign the patient to next available slot
Else
put patient on tomorrows waiting list[/code]
Note that there is no need to check the other doctors for open slots if the current doctor doesn't have one. Given that all the doctors have the same amount of slots, that situation would never present itself.
P.S.
150 slots per day at 15 minutes each = 37,5 hours... Hard-working doctors you have over there xDComment
-
how do i build the doctor queue ?
loop through each doctors slots ?
say at a give time there are 20 patients and 5 doctors , how do i make sure that each doctor has 4 patients . bcos it must be divided equally . Its bit confusing for me .Comment
-
how do i build the doctor queue ?
Like:
[code=php]$doctors = array(
array(
'name' => 'Greg',
'patients' => array()
),
array(
'name' => 'Meredith',
'patients' => array()
)
// etc...
);[/code]
loop through each doctors slots ?
say at a give time there are 20 patients and 5 doctors , how do i make sure that each doctor has 4 patients . bcos it must be divided equally . Its bit confusing for me .
You could use a foreach loop to loop through the patients. Inside that loop you would add the patient to the next doctor in the queue. To figure out which doctor is next, just create a variable that is incremented on every loop and reset it when it exceeds the number of doctors.
Again, in pseudo-code:
[code=pseudo]doctorIndex = 0
For each patient:
// Add the client to the slots for the
// doctor at the current index
doctors[doctorIndex]["slots"][] = patient
// Increment the doctor index, so the next
// doctor will be used on the next loop.
doctorIndex = doctorIndex + 1
// Make sure the index doesn't go higher than
// the total amount of doctors.
If doctorIndex > count(doctors):
doctorIndex = 0[/code]Comment
Comment