Single Server Queuing System - MatLab, C, Java code Implementation
Maniruzzaman Akash
December 04, 2017
Computer Programming
,
Mathematics
,
MatLab Code
,
Simulation and Modeling
1 comment
Single Server Queuing System - MatLab and C code Implementation
What:
A single server queuing system is the waiting lines or queues in that system. A single server queuing system can tell us the following things-- How many times a user need to wait in waiting & Total waiting time
- How many times user take in service time & Total service time
- How many users are in the Queue & Total queue time
- How many users has completed their work in that system yet.
Where Single server queuing system is used
Single server queuing system is applied almost all the fields in real life. Like,- In banking system, taking money from the bank. User needs to stand in a line and take money one by one
- A Bus, plane ticketing system
- Any system with line and who needs to put user in the queue
Real time example of a server queuing system - Patient Data:
Single Server Queuing Example of patient and doctor |
Single Server Queuing MatLab Code implementation without input (with figure):
Code
total=0; busy=0;
%a=randi([0 8],1,8);
a=[.4 1.6 2.1 3.8 4.0 5.6 5.8 7.2];
a_length=length(a);
arr=zeros(a_length);
%d=randi([0 9],1,5);
d=[2.4 3.1 3.3 4.9 8.6];
b=union(a,d);
l=length(b);
a_t=0;d_t=0;q=0;
axis([0 b(length(b))+1 0 length(a)]);
%Queue delay Time
figure(1);
title('Queue Delay Time');
for i=1:l-1
a_m=ismember(a,b(i));
d_m=ismember(d,b(i));
if sum(a_m)>=1
a_t=a_t+1;
end
if sum(d_m)>=1
d_t=d_t+1;
end
dif=a_t-d_t;
if dif>1
rectangle('Position',[b(i) 0 b(i+1)-b(i) dif-1],'FaceColor',[0 .5 .5]);
arr(dif)=arr(dif)+(b(i+1)-b(i));
end
if dif>0
busy=busy+(b(i+1)-b(i));
end
end
%Server Busy Time
b_t=0;
figure(2);
title('Server Busy Time');
axis([0 b(length(b))+1 0 length(a)]);
for i=1:l-1
a_m=ismember(a,b(i));
d_m=ismember(d,b(i));
if sum(a_m)>=1
b_t=b_t+1;
end
if sum(d_m)>=1
b_t=b_t-1;
end
if b_t>0
rectangle('Position',[b(i) 0 b(i+1)-b(i) 1],'FaceColor',[0 .5 .5]);
end
end
for i=1:a_length
total=total+arr(i)*(i-1);
end
disp(total);
disp(total/d(length(d)));
disp(busy);
disp(busy/d(length(d)));
Output with Figure
(Run the code and wait. Figure will open after 2/3 seconds)
Matlab Complaete Example - Single Server Queuing system |
Single Server Queuing MatLab Code implementation with input:
N = input('Enter the Value of N: ');
% Variable Declaration
AT = [];
ST = [];
WT = [];
QL = [];
IDT = [];
CAT = [];
CDT = [];
CLK = 0;
% Initialization
AT(1) = 0;
for k = 2:N
AT(k) = input('Enter interarrival time : ');
end
for k = 1:N
ST(k) = input('Enter Service time : ');
end
CAT(1) = AT(1);
CDT(1) = ST(1);
for k = 1:N
WT(k) = 0;
IDT(k) = AT(1);
QL(k) = 0;
end
% Calculation
for i = 2:N
CAT(i) = CAT(i-1) + AT(i);
WT(i) = CDT(i-1) - CAT(i);
if WT(i) < 0
WT(i) = 0;
end
DIF = CAT(i) - CDT(i-1);
if DIF < 0
CDT(i) = CDT(i-1) + ST(i) ;
if i>2
if (CAT(i) < CDT(i-2) || QL(i-1) == 0)
QL(i) = QL(i-1) + 1;
else
QL(i) = QL(i-1);
end
else
QL(i) = QL(i-1) + 1;
end
elseif DIF > 0
CDT(i) = CAT(i) + ST(i) ;
if QL(i-1) > 0
QL(i) = QL(i-1) - 1;
end
else
QL(i) = QL(i-1);
end
if QL(i) == 0
IDT(i) = CAT(i) - CDT(i-1);
end
end
% Display Results
CAT
CDT
WT
IDT
QL
Input data / Output Example for this single server queuing system
Enter the Value of N: 8 Enter interarrival time : 10 Enter interarrival time : 15 Enter interarrival time : 35 Enter interarrival time : 30 Enter interarrival time : 10 Enter interarrival time : 5 Enter interarrival time : 5 Enter Service time : 20 Enter Service time : 15 Enter Service time : 10 Enter Service time : 5 Enter Service time : 15 Enter Service time : 15 Enter Service time : 10 Enter Service time : 10 CAT = 0 10 25 60 90 100 105 110 CDT = 20 35 45 65 105 120 130 140 WT = 0 10 10 0 0 5 15 20 IDT = 0 0 0 15 25 0 0 0 QL = 0 1 1 0 0 1 1 2 >>
Single Server Queuing C Code implementation:
Single Server Queuing system in Java language:
import java.util.Random;
/**
*
* @author Hizbul Bahar
*/
public class SingleServer {
static int Queue_size = 32000;
static int next_event_type;
static int num_custs_delayed;
static int num_events=2;
static int num_in_q;
static int server_status;
static int num_delays_required;
static double area_num_in_q;
static double area_server_status;
static double sim_time;
static double time_last_event;
static double total_of_delays;
static double mean_interarrival;
static double mean_service;
static double[] time_arrival = new double[Queue_size];
static double[] time_next_event=new double[3];
static Random random = new Random(10000);
static void initialize()
{
sim_time = 0;
server_status = 0;
num_in_q = 0;
time_last_event = 0;
num_custs_delayed = 0;
total_of_delays = 0;
area_num_in_q = 0;
area_server_status = 0;
time_next_event[1] = sim_time + expon(mean_interarrival);
time_next_event[2] = 1.0e+30;
}
static void timing()
{
if (time_next_event[1] < time_next_event[2])
next_event_type = 1;
else
next_event_type = 2;
sim_time = time_next_event[next_event_type];
}
static void arrive()
{
double delay;
time_next_event[1] = sim_time + expon(mean_interarrival);
if (server_status == 1)
{
num_in_q++;
time_arrival[num_in_q] = sim_time;
}
else
{
delay = 0;
total_of_delays += delay;
num_custs_delayed++;
server_status = 1;
time_next_event[2] = sim_time + expon(mean_service);
}
}
static void depart()
{
if (num_in_q == 0)
{
server_status = 0;
time_next_event[2] = 1.0e+30;
}
else
{
num_in_q--;
num_custs_delayed++;
time_next_event[2] = sim_time + expon(mean_service);
for (int i = 1; i <= num_in_q; i++)
time_arrival[i] = time_arrival[i+1];
}
}
static void report()
{
System.out.println( "TOtal customer uses this server " + num_custs_delayed + "\n");
System.out.println( "Average delay in queue minutes " + total_of_delays / num_custs_delayed + "\n");
System.out.println( "Average number in queue " + area_num_in_q / sim_time + "\n");
System.out.println( "Server utilization " + area_server_status / sim_time + "\n");
}
static void update_time_avg_stats()
{
double time_since_last_event;
time_since_last_event = sim_time - time_last_event;
time_last_event = sim_time;
area_num_in_q += num_in_q * time_since_last_event;
area_server_status += server_status * time_since_last_event;
}
static double expon(double mean)
{
return -mean * Math.log(random.nextDouble());
}
public static void main(String[] args) {
timing();
update_time_avg_stats();
switch (next_event_type)
{
case 1: arrive();
break;
case 2: depart();
}
}
}
Links following to learn single server queuing system
- Wikipedia - Queuing Theory
- A single Server Queuing system.pdf
Tags:
Single Server queuing system example, Single Server Queuing System - MatLab, C, Java code Implementation, Single Server Queuing System Matlab code, Single Server Queuing System C code, Single Server Queuing System java code, Single Server Queuing System matlab code implementation, single server queue, discrete simulation example, single server code and algorithm
Subscribe to:
Post Comments
(
Atom
)
Why the code in Java did not output anything?
ReplyDelete