%%
%基于排队论mm1m模型的仿真,五种业务,五种不同的到达率和服务率,运行结果:五种业务被服务的客户总数与member对应统计的不相符,
%%
clear all;close all;clc;
%%
% 初始参数设置:
sim_time = 50; %总仿真时间
m = 5; %缓存队列最大长度
%可能到达的顾客数
arr_num = 200;
sim_num = arr_num*5; %五种业务
%五种业务五种不同的到达率
lambda1 = 9/1;lambda2 = 5/1; lambda3 = 7/1; lambda4 = 3/1;lambda5 = 6/1;
%lambda = lambda1 + lambda2 + lambda3 + lambda4 + lambda5;%相对于系统而言,将所有种类的业务视为一类,所以其到达速率为所有业务到达速率之和。
%五种业务五种不同的服务率
mu1 = 4;mu2 = 3;mu3 = 6;mu4 = 2;mu5 = 5;
%平均到达时间
%arr_mean = 1/lambda;
arr_mean1 = 1/lambda1;arr_mean2 = 1/lambda2;arr_mean3 = 1/lambda3;arr_mean4 = 1/lambda4;arr_mean5 = 1/lambda5;%不同业务的不同到达率
%平均服务时间
%ser_mean = 1/mu; %平均服务时间
ser_mean1 = 1/mu1;ser_mean2 = 1/mu2;ser_mean3 = 1/mu3;ser_mean4 = 1/mu4;ser_mean5 = 1/mu5;%不同业务有不同的服务率
%按负指数分布产生各顾客达到时间间隔
arr_int(1,:) = exprnd(arr_mean1,1,arr_num);
arr_int(2,:) = exprnd(arr_mean2,1,arr_num);
arr_int(3,:) = exprnd(arr_mean3,1,arr_num);
arr_int(4,:) = exprnd(arr_mean4,1,arr_num);
arr_int(5,:) = exprnd(arr_mean5,1,arr_num);
%各顾客的到达时刻等于时间间隔的累积和
info_arr(1,:) = cumsum(arr_int(1,:));
info_arr(2,:) = cumsum(arr_int(2,:));
info_arr(3,:) = cumsum(arr_int(3,:));
info_arr(4,:) = cumsum(arr_int(4,:));
info_arr(5,:) = cumsum(arr_int(5,:));
%按负指数分布产生各顾客服务时间
ser_int(1,:) = exprnd(ser_mean1,1,arr_num);
ser_int(2,:) = exprnd(ser_mean2,1,arr_num);
ser_int(3,:) = exprnd(ser_mean3,1,arr_num);
ser_int(4,:) = exprnd(ser_mean4,1,arr_num);
ser_int(5,:) = exprnd(ser_mean5,1,arr_num);
%%
%声明一些需要用到的数组
info_rej = zeros(5,arr_num);
info_left = zeros(5,arr_num);
info_accept = zeros(5,arr_num);
info_wait = zeros(5,arr_num);
%%
%信息初始化
total = zeros(5,sim_num);
x = [info_arr(1,1:arr_num),info_arr(2,1:arr_num),info_arr(3,1:arr_num),info_arr(4,1:arr_num),info_arr(5,1:arr_num)];
total(1,:) = sort(x,'ascend'); %统计物种业务各顾客到达时刻,将所有业务客户的到达时间按升序排列
len_sim = sum(total(1,:)<= sim_time) %计算仿真顾客个数,即到达时刻在仿真时间内的顾客数
%%
%第一个顾客的信息
for i = 1:5
if info_arr(i,1)==total(1,1)
info_left(i,1) = info_arr(i,1) + ser_int(i,1); %该顾客离开时刻
info_accept(i,1) = 1; %该业务第一个顾客被接纳
info_wait(i,1) = 0; %该顾客无需等待
end
end
total(5,1) = 1; %该顾客肯定被接纳,此时系统内共有1 个顾客,故标志位置1
member = [1]; %其进入系统后,系统内已有成员序号为1
%%
%第i个顾客的信息
for i = 2:len_sim
if total(1,i)>sim_time %第i个顾客的到达时间超过了仿真时间,跳出循环
for j = 1:5
for k = 1:arr_num
if info_arr(j,k) == total(1,i)
info_wait(j,k) = 0;
info_accept(j,k) = 0;
end
end
end
break;
else %第i个顾客的到达时间未超过仿真时间,则计算在其到达时刻系统中已有的顾客个数
number = sum(total(4,member) > total(1,i));
end
if number >= m+1 %系统已满,则系统拒绝第i个顾客,
total(5,i) = 0;
elseif number == 0 %系统有空闲服务窗,则第i个顾客直接接受服务
for j = 1:5
for k = 1:arr_num
if info_arr(j,k) == total(1,i)
info_wait(j,k) = 0;
info_left(j,k) = info_arr(j,k) + ser_int(j,k);
info_accept(j,k) = 1;
total(4,i) = total(1,i) + ser_int(j,k);
end
end
end
total(5,i) = 1; %进入系统后,有1个顾客
member = [member,i];
else %系统有顾客正在接受服务,且等待队列未满,则第i个顾客进入系统
for j = 1:5
for k = 1:arr_num
if info_arr(j,k) == total(1,i)
%嵌套一个循环,与member(end)对应的数进行比较
for p = 1:5
for q = 1:arr_num
if info_arr(p,q) == total(member(end))
info_wait(j,k) = info_left(p,q)-total(1,i); %info_arr(j,k);%等待时间=前1个顾客的离开时刻-该顾客到达时刻
info_left(j,k) = info_left(p,q)+ser_int(j,k);
info_accept(j,k) = 1;
total(4,i)=total(4,member(end))+ser_int(j,k);%离开时刻=前1个顾客的离开时刻+该顾客服务时间
end
end
end
%
end
end
end
total(5,i) = number+1;
member = [member,i];
end
end
%%分业务统计
num=zeros(7,1);
for i=1:5
for j=1:arr_num
if info_accept(i,j)==0
num(i,1) = num(i,1)+1;
end
end
end
num(6,1) = sum(num(1:5,1));
for i=1:len_sim
if total(5,i) > 0
num(7,1)= num(7,1)+1;
end
end
%%
%输出结果
len_men = length(member)
ls = mean(total(5,member))
num