A Hospital Simulation
the previous assignments, you can work in a team of two
- Event-driven simulation
- linked data structure
- priority queue
Hospital has just been bought out by an HMO, and it's budget-crunch time.
You have been hired to model the flow of patients in their emergency room and
answer some key questions: "How long do patients wait to be treated?"
"What is the average treatment time?" "What is an optimal number of
treatment rooms that balances cost against patient service?"
You are going to design an event-driven
simulation that models the treatment of patients in a hospital emergency
room. The number of examination rooms in use will be entered by the
user. When a patient arrives, if there is an open treatment room, they go
right in. If there is no treatment room available, patients wait for an
open room. Patients are treated in order according to their urgency
rating; patients with the same urgency rating are treated in order of
arrival. Once treatment begins, the examination room is occupied until
treatment is finished, even if a more urgent patient arrives in the mean
All the relevant data about a patient (when
they arrive, the urgency of their condition, and how long treatment will take)
is generated by methods in PatientVisitGenerator.java. (You will
also need PatientVisit.java that you can
This program will be an event-driven
simulation and includes two different kinds of
- An arrival
event occurs when a patient arrives in the emergency room.
When one arrival occurs, another arrival event needs to be
scheduled. An arrival event is an external event.
- A departure
event occurs when an examination room becomes available because a
patient's treatment is finished. If there are patients waiting, the next
scheduled patient is moved into the examination room.
The simulation is to run for 10 simulated
hours. After 10 hours, no new arrival events should be generated.
Let the patients that are still being treated and any that are waiting after the
10 hour mark finish normally.
- You will need an event list for this assignment. You will model this
by implementing a priority queue.
We could use the PriorityQueue class listed in the Java API. However, for this assignment, you are asked to write your own PriorityQueue class. A priority queue behaves like a queue, except that objects are not always
added at the rear of the queue. Instead, objects are added according
to their priority. If two objects are equal, they are handled first-in,
first-out. Removal is always from the front.
- Create a PriorityQueue class using a linked list implementation based on generics.
Make the nodes (or links) doubly-linked. The type of the data stored in the nodes should be a generic type that is comparable. That is write for the class declaration:
public class PriorityQueue<E extends Comparable<E>>
- The public interface is as
- constructor - create an empty PriorityQueue.
- void insert (E e) - insert the object in the queue. Use the Comparable
method compareTo( ) to implement the ordering.
- E remove() - removes and returns the object from the front. Throw
an appropriate exception if the PriorityQueue is empty.
- E front() - returns the object at the front without changing the
PriorityQueue. Throw an appropriate exception if the PriorityQueue is
- boolean isEmpty()
- List testForwardTraversal() and List testReverseTraversal() - test
methods to be used by the unit tests; used to make sure all of the links are
correct (going forward and backward) by traversing the queue and
constructing a list containing its contents.
In your simulation, you will use one queue (= arrival queue) for the patients arriving in the ER. The patients are queued by order of urgency and by arrival time for two patients with the same urgency. If an examination room is available, a patient is dequeued from the arrival queue and transfered to the examination room. Simulate the patients being currently examined with another queue (= examination queue). Patients in the examination queue are ordered by their departure time (= time of start of examination + duration of examination). As patients are "routed" to an
examination room, have your code decide which non-busy room gets the patient
so that all rooms are equally busy. For instance, if there were 2 rooms,
you wouldn't want one to be 75% busy and the other only 15% busy (each room
has its own nurse). Have your code spread the visits between the 2 so
both work out to be equally busy (they don't have to be exactly the same, but
close). You can do so with another priority queue (= room queue). Order the available rooms in the room queue by their "busy" time. The least busy room should be at the front of the queue and is the next one that will receive a patient. Create classes as needed to model the different elements going the queues. Remember that any data put in a priority queue must be Comparable.
- The rest of the design up to you (user interface is described
below). Come up with an appropriate object-oriented design based on
examples/ideas we've discussed this quarter.
Write a class (e.g. Simulation) with a main method to run the simulation. Let the user input the number of rooms and select the type of simulation (random and preset for testing). Output the results (see below) in a nice readable format. Run the simulation by filling the arrival queue with new patients as given by the PatientVisitGenerator class. Dequeue patients from the arrival queue and route them to the examination queue. The simulation is over when the duration of the simulation (= 10 hours) has passed and the examination queue is empty. Tabulate your results as the simulation runs and report your results at the end of the simulation.
As the simulation runs, collect data to
answer these questions. Display the answers to the user in an easily
- How many examination rooms are in the system?
- For how long did the simulation run (in hours)? Note: this will be
longer than 10 hours since some patients might still be being treated.
- How many patients were treated in all?
- What was the average wait for treatment (in minutes). Note that
sometimes the wait time is zero (if the patient is moved into an examination
room without being placed in the patient queue). In addition to the
average overall wait, provide two other values:
- the average wait for patients with urgency levels from 1 to 4 (highest
- the average wait for patients with urgency levels 9 or 10 (lowest
- What was the average duration of treatment (in minutes)?
- How many patients were treated in each examination room?
- What percentage of time was each examination room busy?
As always, you will have success if you
code and test in pieces. Get your individual classes working first.
Then put them together in the simulation.
When testing, you may want to start with
simple numbers instead of the random numbers. A static method named
getNextProgrammedArrival is provided that you may find helpful. Remember
that the objective of testing is to prove that the program is working
correctly. This is easier to do with simple data.
You could run the simulation for a shorter
period of time during testing so that you don't have to examine a huge amount of
data. Though not a requirement, you might think about adding a 2nd input
field to enter the number of hours to run the simulation. This might aid
Style & Testing
- Make sure you have documented your public interfaces well. Remember,
you are building these classes from scratch. No one has any idea of what
they do except you. You need to communicate these ideas to the reader.
- Use appropriate style that we've discussed in class. This includes
(but not limited to) named constants, private methods, and throwing exceptions
- Develop unit tests for the PriorityQueue class. Be sure to include
code that checks to make sure all your links are correct (going forward and
backward), like printing out the PriorityQueue in forward and reverse
- Carefully test your code and include information about the testing you
performed in your report.
Written Report (typed and turned in as a pdf file) You must turn in a short report that discusses your program,
describes the class design, and discusses issues you encountered while working
on it. Your report
should cover the following:
- Planning: How did you plan and organize your program? What does it
do? Include user instructions if appropriate.
- Implementation: How is your program organized? What are the major
classes? How do the objects
interact? Remember, I didn't give you a design to start
with so I know nothing about your code. Explain this well.
- Testing: How did you test your code? What sort of bugs did you
encounter? Are there any unresolved problems in the code? Be sure to list any ways in
which the program falls short of the specification.
- Evaluate this project. What did you learn from it? Was it
worth the effort? This could include things you learned about
specifications and interfaces, design issues, Java language issues, debugging,
- Finally, as a result of the testing you have performed, what do you think is the
optimal number of examination rooms? Make sure to back up
your recommendation by referring to your results, including average waiting