CS301 Lec10
CS301 Lec10
CS301 Lec10
10
Data Structures
Dr. Sohail Aslam
Simulation of a Bank
A customer enters the bank at a specific
time (t1) desiring to conduct a transaction.
Any one of the four tellers can attend to
the customer.
The transaction (withdraw, deposit) will
take a certain period of time (t2).
If a teller is free, the teller can process the
customers transaction immediately and
the customer leaves the bank at t1+t2.
Simulation of a Bank
It is possible that none of the four tellers is
free in which case there is a line of
customers are each teller.
An arriving customer proceeds to the back
of the shortest line and waits for his turn.
The customer leaves the bank at t2 time
units after reaching the front of the line.
The time spent at the bank is t2 plus time
waiting in line.
Simulation of a Bank
teller 1
teller 2
teller 3
teller 4
Simulation of a Bank
teller 1
teller 2
teller 3
teller 4
Simulation of a Bank
teller 1
teller 2
teller 3
teller 4
Simulation of a Bank
teller 1
teller 2
teller 3
teller 4
Simulation of a Bank
teller 1
teller 2
teller 3
teller 4
Simulation of a Bank
teller 1
teller 2
teller 3
teller 4
Simulation of a Bank
teller 1
teller 2
teller 3
teller 4
Simulation Models
Two common models of simulation are
time-based simulation and event-based
simulation.
In time-based simulation, we maintain a
timeline or a clock.
The clock ticks. Things happen when the
time reaches the moment of an event.
C1 in
10
11
12
C1 out
C2 in
C2 out
C3 in
13
14
15
C1 in
10
11
12
C1 out
C2 in
Event 1:
Event 2:
Event 3:
Event 4:
Event 5:
C2 out
2 mins
4 mins
6 mins
10 mins
12 mins
C1 in
C2 in
C1 out
C2 out
C3 in
C3 in
13
14
15
Simulation Procedure
The first event to occur is the arrival of the
first customer.
This event placed in the priority queue.
Initially, the four teller queues are empty.
The simulation proceeds are follows:
When an arrival event is removed from the
priority queue, a node representing the
customer is placed on the shortest teller
queue.
Simulation Procedure
If that customer is the only one on a teller
queue, a event for his departure is placed
on the priority queue.
At the same time, the next input line is
read and an arrival event is placed in the
priority queue.
When a departure event is removed from
the event priority queue, the customer
node is removed from the teller queue.
Simulation Procedure
The total time spent by the customer is computed:
it is the time spent in the queue waiting and the
time taken for the transaction.
This time is added to the total time spent by all
customers.
At the end of the simulation, this total time divided
by the total customers served will be average time
spent by customers.
The next customer in the queue is now served by
the teller.
A departure event is placed on the event queue.
"Customer.cpp"
"Queue.h"
"PriorityQueue.cpp"
"Event.cpp"
Queue q[4];
// teller queues
PriorityQueue pq; //eventList;
int totalTime;
int count = 0;
int customerNo = 0;