Introduction to Simulation Models

Math 245 - Mathematical Models
Goal: Understand how to create computer simulations.

Queuing Models

Initial setup

As discussed in the notes and in lecture, here is a queuing simulation modeling a patient's waiting room.

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];];
nwait

Collecting statistics

The above model is of no use until we collect statistics and run the model many times to investigate expected behavior.

As discussed in the notes, we can determine:
The number of patients waiting in the waiting room at noon by evaluating nwait,
The number of minutes the doctor will spend after noon by evaluating If[busy==1,endTime-180+15*nwait,0].
Wrap these in Table commands to run the model many times.

waitData=Table[
nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];];
nwait,{500}];
Histogram[waitData]
N[Mean[waitData]]

afterData=Table[
nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];];
If[busy==1,endTime-180+15*nwait,0],{500}];
Histogram[afterData]
N[Mean[afterData]]

Time dependent statistics

Alternatively we might wish to simulate a day at the doctor's office, keeping track at each time interval whether a patient arrives, whether the doctor is busy, or how many people are in the waiting room.  Define new variables patient, isBusy, and numWait to keep track of this information, and then use Table commands to put the gathered data into lists that can be plotted with ListLinePlot or displayed using TableForm.

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait=nwait-1;busy=1;endTime=i+15];
patient[i]=newPatient;
isBusy[i]=busy;
numWait[i]=nwait;]

When do patients arrive?  How many arrive?  See it visually.

patientList=Table[patient[i],{i,0,179}];
Total[patientList]
ListLinePlot[patientList,PlotStyle->Thick]

Is the doctor busy?  How much of the time is the doctor busy?  See it visually.

busyList=Table[isBusy[i],{i,0,179}];
Total[busyList]
ListLinePlot[busyList,PlotStyle->Thick]

How many patients are in the waiting room?  On average?  See it visually.

waitList=Table[numWait[i],{i,0,179}];
m=N[Mean[waitList]]
p1=ListLinePlot[waitList,PlotStyle->{Thick}];
p2=Plot[m,{x,0,179},PlotStyle->{Thick,Red}];
Show[p1,p2]

Put all three together

ListLinePlot[{waitList,busyList,patientList},PlotStyle->{{Yellow,Thick},{Blue,Thick},{Red,Thick}}]

A chart of the accumulated data from the first hour

TableForm[Table[{i,patient[i],isBusy[i],numWait[i]},{i,0,59}],TableHeadings->{None,{"Time","Patient arrives?","Doctor busy?","Patients Waiting"}}]

Comprehension Questions

1. Suppose that the doctor determines that patients are more likely to arrive in the first half of the morning (9:00 to 10:29am) than in the second half of the morning (10:30 to 11:59am).  Let the arrival probability be p=.1 for the first half and p=.05 for the second half.  Notice that the expected time spent with the doctor is still 15 minutes.  How do we modify the simulation to take this into account?  Run your simulation at least 1000 times to determine if your modification increases, decreases, or keeps the same the expected number of patients in the waiting room at noon.  How about how long the doctor will stay after noon?  Perhaps the plots of how busy the doctor is or how many patients there are during the day look different?  Discuss whether the answer you find is what you expected to find.

2. What goes wrong (if anything) if the simulation is instead: (removal of zeroing counters)

For[i=0,i<180,i++,
If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];]

3. What goes wrong (if anything) if the simulation is instead: (a reordering of lines of code)

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];
If[endTime==i,busy=0];
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];]

4. What goes wrong (if anything) if the simulation is instead: (a different reordering of lines of code)

nwait=0;busy=0;endTime=0;
For[i=0,i<180,i++,
If[busy==0&&nwait>0,nwait--;busy=1;endTime=i+15];
If[endTime==i,busy=0];
newPatient=If[RandomReal[]≤0.075,1,0];
If[newPatient==1,nwait++];]