Simulations
Simulations are used to try to predict events.
They rely on accurate data to feed in to the computer. For our purposes, this “data” will be in a file. It is our job to write a program that uses that data to simulate an outcome.
An simple example first: A bank is wondering if they are losing customers because they have to wait in line to be served too long. We are given as data arrival times of customers, and how long each transaction takes, and asked to tell the bank the average time a customer had to wait in line.
Simulations can be either time-driven simulations, or event-driven. Often using simulated times and event-driven simulations is simpler and just as accurate.
The data: customer entry time transaction time
A 20 5
B 22 4
C 23 2
D 28 3
We want to know what the average time a customer stands in line is and the max time a customer has to stand in line.
Figure it out. What is the average time? I get 15/4 or almost 4 minutes
We need a structured way to figure it out, then to figure out the data structures that will make it easiest to have the computer figure it out.
Time Event
20 A gets in line and is served immediately; (departs 25)
22 B gets in line
23 C gets in line
25 A departs; B served (departs 29)
28 D gets in line
29 B departs; C served (departs 31)
31 C departs; D served (departs 34)
What events do we need to keep track of to generate the data wanted?
Time a customer gets in line, the time they are served, and the time they depart.
Which events correspond to entering and leaving the queue?
What data structures will make it easiest to program this?
We need a clock and a queue and
An event list to keep track of arrival and departure events
EventList consists of events waiting to be processed
The clock is reset with the earliest time in the event list
When an item is put in the Q, it is taken out of the Event list, and another even is read in
When an item is taken out of the Q (served), the departure event can be set.
Here is another similar problem
A 5 9
B 7 5
C 14 5
D 30 5
E 32 5
F 34 5
Here is a more complex problem–Two queues, one teller; the FirstClass queue is considered more important, so two customers are taken from it for every one from the Regular class
The data
A FC 0 1
B R 0 1
C FC 1 1
D R 1 1
E R 2 1
F FC 3 3
G R 3 1
H R 4 2
I FC 5 2