17
Recitation 9: Computer Assignment 2: M/M/m/N Queueing System Simulation Hung-Bin (Bing) Chang 1 and Yu-Yu Lin 2 [email protected] 1 and [email protected] 2 Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 1/9

Recitation 9

Embed Size (px)

DESCRIPTION

ee

Citation preview

Page 1: Recitation 9

Recitation 9: Computer Assignment 2: M/M/m/NQueueing System Simulation

Hung-Bin (Bing) Chang1 and Yu-Yu Lin2

[email protected] and [email protected]

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 1 / 9

Page 2: Recitation 9

Outline

1 Admin

2 M/M/1 Queueing System (QS)

3 M/M/m/N Queueing System (QS)

4 Sample Code for M/M/1 QS

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 2 / 9

Page 3: Recitation 9

Admin

Admin

Computer Assignment #2Submit a WORD or pdf file that contains graphs, results, and (e.g.,Matlab) code into our CCLE.Specify the software/language used (e.g., MATLAB).Deadline: 12/10/14 (Wed.) Midnight

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 3 / 9

Page 4: Recitation 9

M/M/1 Queueing System (QS)

Illustration: M/M/1 Queueing System (QS)

Arrival processMessage (customer) inter-arrival times are i .i .d . random variablesthat are governed by an exponential distribution with parameter λ.

Service TimesMessage (customer) service times are i .i .d . random variables thatfollow an exponential distribution with parameter µ.

Single server (m = 1)Buffer capacity: unlimited (infinite)Service policy: FCFS (FIFO)

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 4 / 9

Page 5: Recitation 9

M/M/1 Queueing System (QS)

M/M/1 QS Process

t

X

3

2

1

Queue size areaService area

ttotal

Figure : M/M/1 QS Process

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 5 / 9

Page 6: Recitation 9

M/M/1 Queueing System (QS)

M/M/1 QS Process - Performance metrics

System size area = queue size area + service areaPerformance metrics include:

Performance Metrics DescriptionMean System Size (E [X ]) System size area/ttotal

Mean Queueing Size (E [Q]) Queue size area/ttotalMean Delay Time (E [D]) total delay time/Ntotal

Mean Waiting Time (E [W ]) total wait time/Ntotal

Table : Performance metrics in a queueing system

Number of message (Ntotal ) = total number of messages servedduring the simulation run.Total delay time = cumulative sum of the delays incurred bymessages served by the system during the simulation runTotal wait time = cumulative sum of the waiting times incurred bymessages served by the system during the simulation run

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 6 / 9

Page 7: Recitation 9

M/M/1 Queueing System (QS)

M/M/1 QS - Summary of Analytical Results

λi = λ,∀i ≥ 0.µi = µ,∀i ≥ 1.ρ = λ

µ < 1

P(j) = (1− ρ)ρj , ∀j ≥ 0.λD = λ

E [X ] =∑∞

j=0 jP(j) = ρ1−ρ ,

E [D] = λ−1D E [X ] = 1

µ(1−ρ) ,

E [W ] = E [D]− 1µ = ρ

µ(1−ρ) ,

E [Q] = λDE [W ] = ρ2

1−ρ .

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 7 / 9

Page 8: Recitation 9

M/M/m/N Queueing System (QS)

M/M/m/N QS - Summary of Analytical Results

λi =

{λ , 0 ≤ i ≤ N − 1,0 , i = N.

µi = µ×min(i ,m), i ≥ 1.f = λ

µ , ρ = λmµ .

P(j) = ajP(0), j ≥ 0.

P(0) =(∑N

j=0 aj

)−1

aj =f j

j!,0 < j < m,

f j

m!mj−m ,m ≤ j ≤ N.a0 = 1.

PB = P(N) = P(0) f N

m!mN−m .λD = λ(1− PB).

E [D] = E [X ]λD

.

E [W ] = E [D]− 1µ

E [X ] =∑N

j=0 jP(j),E [Q] = λDE [W ].

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 8 / 9

Page 9: Recitation 9

Sample Code for M/M/1 QS

Illustrative discrete event simulation of M/M/1 QS

Global parametersDefinitions and calculation routines of the system states, variablesand their parameters.

InitializeSetting the initial values of system states and parameters

Main and called routinesDefining the flow of the iterative progress of the simulation programfrom start to end (when the termination conditions are met)At each step, a cited routine is called; when a result is returned bythis routine, the proper next routine, if applicable, is called; or thesimulation is terminated, at which time performance metrics areupdated.

ReportCollect simulation data to compute and present performancemetrics.

Prof. Izhak Rubin (UCLA) EE 132B 2014 Fall 9 / 9

Page 10: Recitation 9

M/M/1 Simulation: Global Parameters

time_next_event(1:2) = 0; % Events = {arrivals, departures} % Identifier of arrival event = 1; Identifier of departure event = 2 % Refers to class 1 and 2 event times, which are initially set to 0.

num_events = 2; % The state evolution of a single node queueing system is described through the iterative computation of

2 types of events (arrival and departure times) % Number of events = 2 = Arrival and departure events

mean_interarrival = 2; % Average interarrival time between packet arrivals [sec] % 1/λ = 2 sec (illustrative case)

mean_service_time = 1; % Average packet service time [sec] % 1/μ = 1 sec (illustrative case)

limit_packets = 1e6; (illustrative case) % Maximum number of packets that arrive to the system (whether admitted or blocked) which induce,

when met, termination of the simulation run

© 2013 by Izhak Rubin 1

Page 11: Recitation 9

Initialization sim_time = 0.0; % Initializes simulation time server_status = 0;

% server is initially idle. Recall that status = 0 implies the system to be idle and status =1 indicates the server to be busy.

system_size = 0; % number of packets in the system (or system

size, denoted as X) queueing_size = 0;

% number of packets in the queue (wait size, denoted as Q)

time_last_event = 0; % the time of the latest recorded event

% Initialize the statistical counters: num_packets_delay = 0;

% cumulative number of packets that have experienced delay up to this time

num_packets_wait = 0; % cumulative number of packets that have

experienced waiting time up to this time total_of_wait_time = 0;

% cumulative sum of the waiting times experience by all packets served by the system up to this time

total_of_delay_time = 0; % cumulative sum of the delay times experience

by all packets served by the system up to thistime

system_size_area = 0; % cumulative system size area up to this time

queueing_size_area = 0; % cumulative queue size area up to this time

service_area = 0; % cumulative service area up to this time

% Initialize event list. Initially, no packets are present. % departure (service completion) events are not yet

involved. time_next_event(1) = sim_time +

exprnd(mean_interarrival); % time of next arrival event % Matlab defines exprnd(mean_interarrival) =

exponentially distributed RV with this specified mean

time_next_event(2) = 1e30; % time of next departure event; 1e30 denotes

infinite

© 2013 by Izhak Rubin 2

Page 12: Recitation 9

main while total_of_packets-1 < limit_packets

% checks that the max number of served packets is below the specified limit and calls the timing( ) routine.

% The program should also include termination conditions that involve maximum a simulation run time and queue-size limits to assure timely completion and stable operation.

timing(); % Determines the next event type and updates the current simulation time.

update_time_avg_stats(); % updates_the values of the computed system state / performance statistical measures.

switch next_event_type % iterative progress of the simulation process as it goes through successive arrival or departure events.

case 1 arrive();

case 2 depart();

end end report(); % report subroutine is called to generate performance exhibits.

© 2013 by Izhak Rubin 3

Page 13: Recitation 9

timing() min_time_next_event = 1.0e+30; % infinity next_event_type = 0;

% Initial default value for k = 1:num_events

% class-k event; k =1 designates an arrival event; k =2 designates a departure event if time_next_event(k) < min_time_next_event

min_time_next_event = time_next_event(k); next_event_type = k;

end end sim_time = min_time_next_event;

© 2013 by Izhak Rubin 4

Page 14: Recitation 9

update_time_avg_stats() time_since_last_event = sim_time - time_last_event; time_last_event = sim_time;

system_size_area = system_size_area + system_size*time_since_last_event; % Update area under number in-system function

queueing_size_area = queueing_size_area + queueing_size*time_since_last_event; % Update area under number in-queue function

service_area = service_area + server_status*time_since_last_event; % Update area under server-busy indicator function.

© 2013 by Izhak Rubin 5

Page 15: Recitation 9

arrive() time_next_event(1) = sim_time +

exprnd(mean_interarrival); system_size = system_size + 1; time_arrival_system(system_size) = sim_time;

% time arrival system (i) = time of arrival of packet-i

% time arrival system (system_size) = time of arrival of the last packet to arrive

if server_status == 1 % busy status is 1 and idle status is 0 % Server is busy, so increment number of

packets in queue queueing_size = queueing_size + 1; time_arrival_queue(queueing_size) = sim_time;

else % i.e., server_status == 0; Server is idle, a new packet arrives and is

admitted into service. % Arriving packet has wait time of zero. % The following two statements are for

program clarity and they do not affect the results of the simulation.

wait = 0; total_of_wait_time = total_of_wait_time + wait; % Increment the number of waiting packets

and identify the server to be in busy status. num_packets_wait = num_packets_wait + 1; server_status = 1; % server is busy % Schedule a departure (service completion). time_next_event(2) = sim_time +

exprnd(mean_service_time); end

© 2013 by Izhak Rubin 6

Page 16: Recitation 9

departure() system_size = system_size - 1; delay = sim_time - time_arrival_system(1);

% delay experienced by the departing packet

% time_arrival_system(1) identifies the time of arrival of the head-of-the-line (HOL) packet.

total_of_delay_time = total_of_delay_time + delay; % Increment the total by adding the delay

incurred by the currently departing packet num_packets_delay = num_packets_delay + 1; for k = 1 : system_size

time_arrival_system(k) = time_arrival_system(k+1);

% pushes up packet arrival times in the array so that the current HOL packet arrival time is at entry 1

end if queueing_size == 0

% The queue becomes empty, the server is set to idle and we eliminate the next departure event from consideration.

server_status = 0; time_next_event(2) = 1.0e+30;

else % The queue is nonempty, so decrement the

number of packets in queue. queueing_size = queueing_size - 1; % Compute the wait of the customer that is

starting service and update the total wait accumulator.

wait = sim_time - time_arrival_queue(1); total_of_wait_time = total_of_wait_time + wait; % Increment the number of waiting packets

and schedule the next departure. num_packets_wait = num_packets_wait + 1; time_next_event(2) = sim_time +

exprnd(mean_service_time); % Move each customer in queue up one place

upwards (FIFO service assumed): for k = 1:queueing_size

time_arrival(k) = time_arrival(k+1); end

end

© 2013 by Izhak Rubin 7

Page 17: Recitation 9

report() display(['E[D] = 'num2str(total_of_delay_time/num_packets_delay)]);

% Average delay in the system E[D] display(['E[X] = 'num2str(system_size_area /sim_time)]);

% Average number in the system E[X] display(['E[W] = 'num2str(total_of_wait_time/num_packets_wait)]);

% Average delay in the queue E[W] display(['E[Q] = 'num2str(queueing_size_area /sim_time)]);

% Average number in the queue E[Q] display(['Server utilization = 'num2str(service_area/sim_time)]);

% Server utilization = fraction of time that the service channel is busy; % At steady state, for M/M/1 system, it will be equal to ρ = λ/μ.

© 2013 by Izhak Rubin 8