排队论地matlab仿真(包括仿真代码)
基于MatLab的排队系统仿真研究

/(-  ̄ 1p) 一
客户到达
— — — — — — —
— — — 型 H 器 童 f l I — — — 丽辑 —圭 —塑— 1 — —4 — — ~
f ) / c 统 的 数 学 特 性 3 M M, 系 M M/ 的 排 队 系 统 有 c台服 务 器 . 台服 务 器 独 立 / e 每 工 作 . 具 有 相 同分 布 的 服 务 时 间 , 们 假 设 所 有 服 务 且 我
0 引 言
排 队论 是 研 究 系 统 由于 随 机 因 素 的干 扰 而 出 现 排 队现 象 的一 门学 科 排 队是 日常 生 活 中经 常 遇 到 的 现 象 。例 如 : 出行 坐 火 车等 待 检票 进 站 的 排 队 ; 食 堂 打 到 饭所 形成的排 队 ; 校 打预防针 , 检所 形成 的排 队 ; 学 体 看 电影 、 游 时前 往售 票 处 购 票 形 成 的排 队等 。 近 几 旅 最
服 务 效 率 与 最合 理 的 配 置 。应 用 Ma a t b对 M/ c M / / / 等排 队 系 统进 行 仿 真 , 仿 L M/ , M c N 对
真 结 果进 行 评 估 , 果 表 明 , 方 法是 切 实 可行 的。 结 该
关 键 词 :仿 真 ;MaL b¥言 ;排 队 系统 ;马可 夫过 程 ta i
图 1 单 服 务 台 排 队 系 统 的 结构 模 型
台是空 的概率 P, n所有服务 台都在忙 的概率 P , 由下面
12 排 队 系统 的数 学特 性 .
( ) 人记 号 和重 要 度 量 参 数 1引
收 稿 日期 :0 0 7 2 2 1 —0 ~ 0 修 稿 日期 :0 0 0 —1 21— 8 1
MM1排队系统仿真matlab试验报告

M/M/1排队系统实验报告一、实验目的本次实验要求实现M/M/1单窗口无限排队系统的系统仿真,利用事件调度法实现离散事件系统仿真,并统计平均队列长度以及平均等待时间等值,以与理论分析结果进行对比。
二、实验原理根据排队论的知识我们知道,排队系统的分类是根据该系统中的顾客到达模式、服务模式、服务员数量以及服务规则等因素决定的。
1、顾客到达模式设到达过程是一个参数为入的Poisson过程,则长度为t的时间内到达k个呼p (t)=(入“ e 4叫的概率服从Poisson分布,即k k!,k = 01,2, .. ,其中入>0为一常数,表示了平均到达率或Poisson呼叫流的强度。
2、服务模式设每个呼叫的持续时间为二,服从参数为N的负指数分布,即其分布函数为P{ X < t } = 1 - e-k t > 03、服务规则先进先服务的规则(FIFO)4、理论分析结果入P = Q = -2—在该M/M/1系统中,设口,则稳态时的平均等待队长为Q 1-P,顾客上乙T T T T =的平均等待时间为N-九。
三、实验内容M/M/1排队系统:实现了当顾客到达分布服从负指数分布,系统服务时间也服从负指数分布,单服务台系统,单队排队,按FIFO (先入先出队列)方式服务。
四、采用的语言MatLab语言源代码:clear;clc;%M/M/1排队系统仿真SimTotal=input('请输入仿真顾客总数SimTotal='); %仿真顾客总数;Lambda=0.4; % 到达率 Lambda;Mu=0.9; % 服务率 Mu;t_Arrive=zeros(1,SimTotal);t_Leave=zeros(1,SimTotal);ArriveNum=zeros(1,SimTotal);LeaveNum=zeros(1,SimTotal);Interval_Arrive=-log(rand(1,SimTotal))/Lambda;% 到达时间间隔Interval_Serve=-log(rand(1,SimTotal))/Mu;% 服务时间 t_Arrive(1)=Interval_Arrive(1);% 顾客到达时间ArriveNum(1)=1;for i=2:SimTotalt_Arrive(i)=t_Arrive(i-1)+Interval_Arrive(i);ArriveNum(i)=i;endt_Leave(1)=t_Arrive(1)+Interval_Serve(1);% 顾客离开时间 LeaveNum(1)=1;for i=2:SimTotalif t_Leave(i-1)<t_Arrive(i)t_Leave(i)=t_Arrive(i)+Interval_Serve(i);elset_Leave(i)=t_Leave(i-1)+Interval_Serve(i);endLeaveNum(i)=i;endt_Wait=t_Leave-t_Arrive; %各顾客在系统中的等待时间 t_Wait_avg=mean(t_Wait);t_Queue=t_Wait-Interval_Serve;%各顾客在系统中的排队时间 t_Queue_avg=mean(t_Queue);Timepoint=[t_Arrive,t_Leave];%系统中顾客数随时间的变化Timepoint=sort(Timepoint);ArriveFlag=zeros(size(Timepoint));% 至 U达时间标志CusNum=zeros(size(Timepoint));temp=2;CusNum(1)=1;for i=2:length(Timepoint)if (temp<=length(t_Arrive))&&(Timepoint(i)==t_Arrive(temp)) CusNum(i)=CusNum(i-1)+1;temp=temp+1;ArriveFlag(i)=1;elseCusNum(i)=CusNum(i-1)-1;endend%系统中平均顾客数计算Time_interval=zeros(size(Timepoint));Time_interval(1)=t_Arrive(1);for i=2:length(Timepoint)Time_interval(i)=Timepoint(i)-Timepoint(i-1);endCusNum_fromStart=[0 CusNum];CusNum_avg=sum(CusNum_fromStart.*[Time_interval 0] )/Timepoint(end);QueLength=zeros(size(CusNum));for i=1:length(CusNum)if CusNum(i)>=2QueLength(i)=CusNum(i)-1;elseQueLength(i)=0;endendQueLength_avg=sum([0 QueLength].*[Time_interval 0] )/Timepoint(end);% 系统平均等待队长%仿真图figure(1);set(1,'position',[0,0,1000,700]);subplot(2,2,1);title('各顾客到达时间和离去时间’);stairs([0 ArriveNum],[0 t_Arrive],'b');hold on;stairs([0 LeaveNum],[0 t_Leave],'y');legend('到达时间‘,‘离去时间’); hold off;subplot(2,2,2);stairs(Timepoint,CusNum,'b')title('系统等待队长分布');xlabel('时间');ylabel('队长’); subplot(2,2,3);title('各顾客在系统中的排队时间和等待时间’);stairs([0 ArriveNum],[0 t_Queue],'b');hold on;stairs([0 LeaveNum],[0 t_Wait],'y');hold off;legend('排队时间‘,‘等待时间’);%仿真值与理论值比较disp(['理论平均等待时间t_Wait_avg=',num2str(1/(Mu-Lambda))]);disp(['理论平均排队时间t_Wait_avg=',num2str(Lambda/(Mu*(Mu-Lambda)))]);disp(['理论系统中平均顾客数=',num2str(Lambda/(Mu-Lambda))]);disp(['理论系统中平均等待队长=',num2str(Lambda*Lambda/(Mu*(Mu-Lambda)))]);disp(['仿真平均等待时间t_Wait_avg=',num2str(t_Wait_avg)])disp(['仿真平均排队时间t_Queue_avg=',num2str(t_Queue_avg)])disp(['仿真系统中平均顾客数=',num2str(CusNum_avg)]);disp(['仿真系统中平均等待队长=',num2str(QueLength_avg)]);五、数据结构1.仿真设计算法(主要函数)利用负指数分布与泊松过程的关系,产生符合泊松过程的顾客流,产生符合负指数分布的随机变量作为每个顾客的服务时间:Interval_Arrive=-log(rand(1,SimTotal))/Lambda;% 至U 达时间间隔,结果与调用exprnd(1/Lambda, m)函数产生的结果相同Interval_Serve=-log(rand(1,SimTotal))/Mu;% 服务时间间隔七_A「后8(1)=加七8~31_八「用8(1);%顾客到达时间时间计算t_Wait=t_Leave-t_Arrive; %各顾客在系统中的等待时间t_Queue=t_Wait-Interval_Serve; %各顾客在系统中的排队时间由事件来触发仿真时钟的不断推进。
matlab(仿真法)具体讲解

执行下面的命令:ode23(‘zjwt',[3,0.0005],0)
若想看图中点的坐标可执行下面的命令: [t,y]=ode23(‘zjwt',[3,0.0005],0) plot(t,y) 此时缉私艇的位置坐标是(0.00050000000000,1.96013657712118) 执行下面的命令: ode45(‘zjwt',[3,0.0005],0) 若想看图中点的坐标可执行下面的命令: [t,y]=ode45(‘zjwt',[3,0.0005],0) plot(t,y) 此时缉私艇的位置坐标是(0.0005,1.9675 )
例3
求微分方程组的通解. dx dt 2 x 3 y 3z dy 4 x 5 y 3z dt dz 4 x 4 y 2 z dt
解 输入命令 : [x,y,z]=dsolve('Dx=2*x-3*y+3*z','Dy=4*x5*y+3*z','Dz=4*x-4*y+2*z', 't'); x=simple(x) % 将x化简 y=simple(y) z=simple(z) 结 果 为:x = (c1-c2+c3+c2e -3t-c3e-3t)e2t y = -c1e-4t+c2e-4t+c2e-3t-c3e-3t+c1-c2+c3)e2t z = (-c1e-4t+c2e-4t+c1-c2+c3)e2t
例 29y 0 dx dx y (0) 0, y ' (0) 15
解 输入命令: y=dsolve('D2y+4*Dy+29*y=0','y(0)=0,Dy(0)=15','x') 结 果 为 : y =3e-2xsin(5x)
Matlab在交通仿真中的应用技巧

Matlab在交通仿真中的应用技巧引言近年来,交通拥堵问题日益严重,给人们的生活和经济发展带来了很大的困扰。
为了解决交通拥堵问题,提高交通效率,交通仿真成为了一种重要的工具。
而Matlab作为一种强大的数学计算软件,可以提供丰富的工具和函数,为交通仿真提供了很大的帮助。
本文将介绍一些Matlab在交通仿真中的应用技巧,包括交通流模型、交通信号灯优化、路网设计和交通预测等方面。
1. 交通流模型交通流模型是交通仿真的基础,它用于描述交通流的行为和变化。
在Matlab中,我们可以利用各种数学模型来建立和模拟交通流。
常用的交通流模型包括微观模型和宏观模型。
微观模型主要用于个体车辆行为的建模,宏观模型主要用于整个交通网络的流量分布和拥堵状况的模拟。
在建立交通流模型时,我们需要收集大量的交通数据,包括车辆的速度、密度和流量等信息。
利用Matlab的数据处理功能,我们可以轻松地对这些数据进行分析和建模。
例如,可以使用Matlab的数据统计函数来计算交通流的平均速度和流量,进而推导出交通流的密度和流量之间的关系。
2. 交通信号灯优化交通信号灯是调控交通流的重要手段。
合理地优化交通信号灯的配时方案,可以有效减少交通拥堵和减少人们的出行时间。
在Matlab中,我们可以利用优化算法来优化交通信号灯的配时方案。
常用的优化算法有遗传算法、粒子群算法等。
首先,我们需要建立交通信号灯的仿真模型,模拟交通信号灯的开关过程和车辆的行驶。
然后,利用Matlab的优化函数,设置优化目标和约束条件,进行信号灯配时方案的优化。
最后,通过仿真实验,评估不同配时方案的性能,选择最优的配时方案。
3. 路网设计路网设计是交通规划和交通工程中的重要环节。
合理地设计路网,可以提高交通的通行能力和效率。
在Matlab中,我们可以利用图论算法和网络流模型来进行路网设计。
首先,我们需要建立路网的拓扑结构,即道路和交叉口之间的连接关系。
然后,利用图论算法,计算路网的最短路径和最小生成树等信息。
数学建模:排队系统仿真

多服务台问题(每个服务台 的服务时间一样)
服务台服务时间为3
顾客到达间隔时间为1
2个服务台
输出排队长的动态 变化情况(用连续 图的形式显示输出)
多服务台问题(每个服务台 的服务时间可以分别设置)
“Stair”形式,即阶梯形式
“Stem”形式,即条状形式
单击“Signal Scope”窗口的“Axes”下拉菜单下的“AutoScale” 子菜单可以改变”Signal Scope”中的坐标
单击“Signal Scope”窗口的“Style”下拉菜单可以改变”Signal Scope”中的输出结果的线性表示形式。
双击“Times-Based Entity Generator”(基于 时间的实体产生器)模块,打开模块设置对 话框,进行模块属性设置。
Generate entities upons(实体产生): 选择”Intergeneration time from port dialog”时,在“Distribution”下拉菜单中 指定如下三种服务时间分布。
5、顾客属性分路形式:根据顾客的属 性,分不同的出口。
路径模块
顾客复制分路模块
顾客复制分路模块
双击顾客复制分路模块
1、顾客复制形式:所有的出口都畅通时,即进行 顾客复制。
2、顾客复制形式:只要有任何一个出口畅通,即 进行顾客复制。
顾客复制分路模块
双击随机数产生器中的 Distribution下来菜单选 择指定随机数。
打开“Single Server”对话框
双击“Single Server”模块
多服务台排队系统的仿真

实验3---多服务台排队系统的仿真姓名:学号:一、目标任务已知一个系统有N个服务员,能力相等,服务时间服从指数分布。
顾客的到达时间间隔服从指数分布。
用Monte-Carlo仿真,分别求按下列方案的总体平均排队时间:① M|M|N。
② N个单通道系统并列,按1/N概率分裂到达流。
③ N个单通道并列,挑选最短的队。
要求:①给出程序设计的过程。
②如果采用固定的N,则要求N>2。
③至少取ρ=0.3和ρ=0.7两种强度运行程序。
④对结果进行分析。
二、编程语言Matlab三、关键代码方案一:N = 3; % 服务员人数r = 6; % 顾客到达流强度u = 20; % 服务员服务强度T = 1000000; % 仿真运行时间avg_wait_time = []; % 平均等待时间for i=1:100% 模拟排队函数server_time = [0.0, 0.0, 0.0]; % 用来保存服务员下一空闲时间time = 0; % 绝对时钟,初始为0client_num = 0; % 顾客总数,初始为0CRTime = 0; % 顾客到达时间间隔ServeTime = 0; % 顾客服务时间server_id = 0; % 当前进入排队窗口的服务员编号total_wait_time = 0;% 系统中到达顾客的总等待时间while 1CRTime = exprnd(1/r); % 按指数分布产生顾客到达时间间隔time = time + CRTime; % 更新系统的绝对时钟if time > Tbreak;endclient_num = client_num + 1; % 顾客数加1ServeTime = exprnd(1/u); % 按指数分布产生顾客服务间隔server_id = mod(client_num, N); % 按1..N的顺序循环排入服务员窗口if server_id ==0server_id = N;endif server_time(1, server_id) <= time % 如果当前server_id号服务员空闲,则直接接收服务server_time(1, server_id) = time + ServeTime; % 服务员下一空闲时间为当前绝对时钟加上当前服务时间else % 否则所有服务员都在忙碌,顾客要排队等候total_wait_time = total_wait_time + server_time(1, server_id) - time; % 顾客排队等候时间为当前服务员下一空闲时间减去绝对时钟server_time(1, server_id) = server_time(1, server_id) + ServeTime;endendavg_wait_time = [avg_wait_time, total_wait_time/client_num];end% 计算平均等待时间mean_avg_wait_time = mean(avg_wait_time);fprintf('ρ=%2.1f平均等待时间%6.5f\n', r/u, mean_avg_wait_time); % 打印平均等待时间% 绘制每次仿真的平均等待时间和总体平均等待时间线状图x = 1:100;%plot(x, avg_wait_time, x, mean_avg_wait_time);scatter(x, avg_wait_time, '.');方案二:N = 3; % 服务员人数r = 6; % 顾客到达流强度u = 20; % 服务员服务强度T = 1000; % 仿真运行时间avg_wait_time = []; % 平均等待时间for i=1:100% 模拟排队函数server_time = [0.0, 0.0, 0.0]; % 用来保存服务员下一空闲时间time = 0; % 绝对时钟,初始为0client_num = 0; % 顾客总数,初始为0CRTime = 0; % 顾客到达时间间隔ServeTime = 0; % 顾客服务时间server_id = 0; % 当前进入排队窗口的服务员编号total_wait_time = 0;% 系统中到达顾客的总等待时间while 1CRTime = exprnd(1/r); % 按指数分布产生顾客到达时间间隔time = time + CRTime; % 更新系统的绝对时钟if time > Tbreak;endclient_num = client_num + 1; % 顾客数加1ServeTime = exprnd(1/u); % 按指数分布产生顾客服务时间间隔server_id = randi([1 N]); % 按1/N的概率排入服务员窗口if server_time(1, server_id) <= time % 如果当前server_id号服务员空闲,则直接接收服务server_time(1, server_id) = time + ServeTime; % 服务员下一空闲时间为当前绝对时钟加上当前服务时间else % 否则所有服务员都在忙碌,顾客要排队等候total_wait_time = total_wait_time + server_time(1, server_id) - time; % 顾客排队等候时间为当前服务员下一空闲时间减去绝对时钟server_time(1, server_id) = server_time(1, server_id) + ServeTime;endendavg_wait_time = [avg_wait_time, total_wait_time/client_num];end% 计算平均等待时间mean_avg_wait_time = mean(avg_wait_time);fprintf('ρ=%2.1f平均等待时间%6.5f\n', r/u, mean_avg_wait_time); % 打印平均等待时间% 绘制每次仿真的平均等待时间散点图x = 1:100;scatter(x, avg_wait_time, '.');方案三:N = 3; % 服务员人数r = 6; % 顾客到达流强度u = 20; % 服务员服务强度T = 1000; % 仿真运行时间avg_wait_time = []; % 平均等待时间for i=1:100% 模拟排队函数server_time = [0.0, 0.0, 0.0]; % 用来保存服务员下一空闲时间time = 0; % 绝对时钟,初始为0client_num = 0; % 顾客总数,初始为0CRTime = 0; % 顾客到达时间间隔ServeTime = 0; % 顾客服务时间server_id = 0; % 当前进入排队窗口的服务员编号total_wait_time = 0;% 系统中到达顾客的总等待时间while 1CRTime = exprnd(1/r); % 按指数分布产生顾客到达时间间隔time = time + CRTime; % 更新系统的绝对时钟if time > Tbreak;endclient_num = client_num + 1; % 顾客数加1ServeTime = exprnd(1/u); % 按指数分布产生顾客服务时间间隔temp = min(server_time); % 寻找排队时间最短的服务员窗口[x, y] = find(temp == min(min(server_time)));server_id = y; % 按队伍最短排入服务员窗口if server_time(1, server_id) <= time % 如果当前server_id号服务员空闲,则直接接收服务server_time(1, server_id) = time + ServeTime; % 服务员下一空闲时间为当前绝对时钟加上当前服务时间else % 否则所有服务员都在忙碌,顾客要排队等候total_wait_time = total_wait_time + server_time(1, server_id) - time; % 顾客排队等候时间为当前服务员下一空闲时间减去绝对时钟server_time(1, server_id) = server_time(1, server_id) + ServeTime;endendavg_wait_time = [avg_wait_time, total_wait_time/client_num];end% 计算平均等待时间mean_avg_wait_time = mean(avg_wait_time);fprintf('ρ=%2.1f平均等待时间%6.5f\n', r/u, mean_avg_wait_time); % 打印平均等待时间% 绘制每次仿真的平均等待时间散点图x = 1:100;scatter(x, avg_wait_time, '.');四、实验结果与分析方案一:图1 方案一仿真的平均等待时间散点图图2 方案一平均等待时间M|M|N1. 输入参数:服务员人数N,顾客到达流强度r,服务员服务强度u,仿真运行时间T;2. 各变量初始值置0:绝对时钟time,服务员下一空闲时刻数组server_time[](其中按顺序保存每一个服务员的下一空闲时刻),顾客总数client_num,顾客到达时间间隔CRTime,顾客服务时间ServeTime,当前进入排队窗口的服务员编号server_id,系统中顾客总等待时间total_wait_time;3. 按照指数分布产生下一顾客到达的时间间隔CRTime,time+=CRTime。
排队问题matlab完整程序

function shengclear allclose alltim0=720;%需要模拟的时间[num,pass]=computing(tim0);%计算模拟数据%动画制作envirment %场景title1= annotation('textbox', 'Position',[0.3377 0.8712 0.33480.07885], ...'EdgeColor','none','FitHeightToText','off','FontName','Arial','FontSize',16,...'FontWeight','bold','String',{'理发店忙闲情况分析'});time1 = annotation('textbox','Position',[0.02754 0.1019 0.09420.06538],...'EdgeColor','none','FitHeightToText','off','FontSize',14,'FontWeight',' bold',...'String',{'时间'});hour1 = annotation('textbox','Position',[0.1072 0.1038 0.075360.06346],...'EdgeColor','none','FitHeightToText','off','FontSize',14,...'FontWeight','bold','String',{'08:'});minute1 = annotation('textbox','Position',[0.1493 0.1038 0.072460.06538],...'EdgeColor','none','FitHeightToText','off', 'FontSize',14,...'FontWeight','bold','String',{'10'});counter1=1; %时间计数temp1=1; %顾客计数counter=1;counterxy=zeros(7,2);tempa=0;tempb=0;tempc=0;tempd=0;tempe=0;tempf=0;tempg=0;global man1 mana1 mana2 mana3 mana4 mana5global manb1 manb2 manb3 manb4 manb5global manc1 manc2 manc3 manc4 manc5global mand1 mand2 mand3 mand4 mand5global mane1 mane2 mane3 mane4 mane5man1(1,:)=[0.4625 0.1786 0.0982 0.1381]; %[x0 y0 x1 y1] man1(2,:)=[0.4875 0.2667 0.5018 0.2667]; %[x0 y0 x1 y1] man1(3,:)=[0.5214 0.2667 0.5339 0.2667]; %[x0 y0 x1 y1] man1(4,:)=[0.5125 0.25 0.5125 0.23]; %[x0 y0 x1 y1] man1(5,:)=[0.4968 0.2081 0.525 0.2072]; %[x0 y0 x1 y1] axis offwhile counter1<=10 %时间计数%显示时间minutex=rem(counter1,60);hourx=8+(counter1-minutex)/60;set(hour1,'String',{hourx})set(minute1,'String',{minutex})if temp1<=num%离开与删除人脸对象if tempa ~= 0if pass(tempa,6) == counter1leaveflash(1,counterxy(1,1),counterxy(1,2))tempa=0;delete(mana1,mana2,mana3,mana4,mana5);endendif tempb~=0if pass(tempb,6) == counter1leaveflash(2,counterxy(2,1),counterxy(2,2))tempb=0;delete(manb1,manb2,manb3,manb4,manb5);endendif tempc~=0if pass(tempc,6) == counter1leaveflash(3,counterxy(3,1),counterxy(3,2))tempc=0;delete(manc1,manc2,manc3,manc4,manc5);endendif tempd~=0if pass(tempd,6) == counter1leaveflash(4,counterxy(4,1),counterxy(4,2))tempd=0;delete(mand1,mand2,mand3,mand4,mand5);endendif tempe~=0if pass(tempe,6) == counter1leaveflash(5,counterxy(5,1),counterxy(5,2))tempe=0;delete(mane1,mane2,mane3,mane4,mane5);endendif tempf~=0if pass(tempf,6) == counter1leaveflash(6,counterxy(6,1),counterxy(6,2))tempf=0;delete(manf1,manf2,manf3,manf4,manf5);endendif tempg~=0if pass(tempg,6) == counter1leaveflash(7,counterxy(7,1),counterxy(7,2))tempg=0;delete(mang1,mang2,mang3,mang4,mang5);endend%产生人脸if pass(temp1,2)==counter1if tempa==0[mana1,mana2,mana3,mana4,mana5]=personcreat(pass(temp1,3)) tempa=temp1;if pass(temp1,5)==1counterxy(1,:)=[-0.027 0.055];endif pass(temp1,5)==2counterxy(1,:)=[0 0.055];endif pass(temp1,5)==3counterxy(1,:)=[0.027 0.055];endelseif tempb==0[manb1,manb2,manb3,manb4,manb5]=personcreat(pass(temp1,3))tempb=temp1;if pass(temp1,5)==1counterxy(2,:)=[-0.027 0.055];endif pass(temp1,5)==2counterxy(2,:)=[0 0.055];endif pass(temp1,5)==3counterxy(3,:)=[0.027 0.055];endtemp1=temp1+1;elseif tempc==0[manc1,manc2,manc3,manc4,manc5]=personcreat(pass(temp1,3))tempc=temp1;if pass(temp1,5)==1counterxy(3,:)=[-0.027 0.055];endif pass(temp1,5)==2counterxy(3,:)=[0 0.055];endif pass(temp1,5)==3counterxy(3,:)=[0.027 0.055];endtemp1=temp1+1;elseif tempd==0[mand1,mand2,mand3,mand4,mand5]=personcreat(pass(temp1,3))tempd=temp1;if pass(temp1,5)==1counterxy(4,:)=[-0.027 0.055];endif pass(temp1,5)==2counterxy(4,:)=[0 0.055];if pass(temp1,5)==3counterxy(4,:)=[0.027 0.055];endtemp1=temp1+1;elseif tempe==0[mane1,mane2,mane3,mane4,mane5]=personcreat(pass(temp1,3))tempe=temp1;if pass(temp1,5)==1counterxy(5,:)=[-0.027 0.055];endif pass(temp1,5)==2counterxy(5,:)=[0 0.055];endif pass(temp1,5)==3counterxy(5,:)=[0.027 0.055];endtemp1=temp1+1;elseif tempf==0[manf1,manf2,manf3,manf4,manf5]=personcreat(pass(temp1,3))tempf=temp1;if pass(temp1,5)==1counterxy(6,:)=[-0.027 0.055];endif pass(temp1,5)==2counterxy(6,:)=[0 0.055];endif pass(temp1,5)==3counterxy(6,:)=[0.027 0.055];endtemp1=temp1+1;elseif tempg==0[mang1,mang2,mang3,mang4,mang5]=personcreat(pass(temp1,3))tempg=temp1;if pass(temp1,5)==1counterxy(7,:)=[-0.027 0.055];endif pass(temp1,5)==2counterxy(7,:)=[0 0.055];endif pass(temp1,5)==3counterxy(7,:)=[0.027 0.055];endtemp1=temp1+1;endendendendendendendend%开始服务if tempa~=0if pass(tempa,2)+pass(tempa,7) == counter1serveflash(1,counterxy(1,1),counterxy(1,2))endendif tempb~=0if pass(tempb,2)+pass(tempb,7) == counter1serveflash(2,counterxy(2,1),counterxy(2,2))endendif tempc~=0if pass(tempc,2)+pass(tempc,7) == counter1serveflash(3,counterxy(3,1),counterxy(3,2))endendif tempd~=0if pass(tempd,2)+pass(tempd,7) == counter1serveflash(4,counterxy(4,1),counterxy(4,2))endendif tempe~=0if pass(tempe,2)+pass(tempe,7) == counter1serveflash(5,counterxy(5,1),counterxy(5,2))endendif tempf~=0if pass(tempf,2)+pass(tempf,7) == counter1serveflash(6,counterxy(6,1),counterxy(6,2))endendif tempg~=0if pass(tempg,2)+pass(tempg,7) == counter1serveflash(7,counterxy(7,1),counterxy(7,2))endendendcounter1=counter1+1;endiiii=1;sumtime=0;sumtimea=0;sumtimeb=0;sumtimec=0;numa=0;numb=0;numc=0;while iiii<numsumtime=sumtime+pass(iiii,3)+pass(iiii,4);if pass(iiii,5)==1sumtimea=sumtimea+pass(iiii,3)+pass(iiii,4);numa=numa+1;endif pass(iiii,5)==2sumtimeb=sumtimeb+pass(iiii,3)+pass(iiii,4);numb=numb+1;endif pass(iiii,5)==3sumtimec=sumtimec+pass(iiii,3)+pass(iiii,4);numc=numc+1;endiiii=iiii+1;endnum=num-1;freetimea=720-sumtimea;freetimeb=720-sumtimeb;freetimec=720-sumtimec;freetime=freetimea+freetimeb+freetimec;avetimea=round(1000*sumtimea/720)/1000;avetimeb=round(1000*sumtimeb/720)/1000;avetimec=round(1000*sumtimec/720)/1000;avetime=round(1000*sumtime/(3*720))/1000;scaleaa=freetimea/720;scaleab=freetimeb/720;scaleac=freetimec/720;dispdata(1,:)=[numa numb numc];dispdata(2,:)=[sumtimea sumtimeb sumtimec];dispdata(3,:)=[avetimea avetimeb avetimec];dispdata(4,:)=[freetimea freetimeb freetimec];dispdata(5,:)=[freetimea/720 freetimeb/720 freetimec/720];figure(2)subplot(2,2,1),bar(dispdata(1,:))title('服务顾客数')subplot(2,2,2),title('服务时间')bar(dispdata(2,:))subplot(2,2,3),title('平均服务时间')pie(dispdata(3,:))subplot(2,2,4),title('空闲比例')pie(dispdata(5,:))function [num,pass]=computing(tim0)seat=[0 0 0];%服务员属性pass=rand(1,4);%序号、到达时间、特殊要求时间、正常理发时间pass(5)=0;%服务员pass(6)=0;%离开时间pass(7)=0;%等待时间num=1;%服务人数tim=0;%时间计数器temp=0;%while tim<=tim0pass(num,1)=num; %装入序号pass(num,2)=rand;pass(num,3)=rand;pass(num,4)=rand;%计算顾客到达时间if pass(num,2)<=0.07temp=4;else if pass(num,2)<=0.17temp=5;else if pass(num,2)<=0.69temp=6;else if pass(num,2)<=0.89temp=7;else temp=8;endendendendtim=tim+temp; %装入顾客到达时间pass(num,2)=tim;if pass(num,3)<=0.1pass(num,3)=4; %装入需要特殊服务的时间else pass(num,3)=0;endnum=num+1;endnum=num-1;for i=1:num%计算顾客的理发席位if seat(1)<=pass(i,2)+pass(i,7)pass(i,5)=1; %由1号服务员理发temp1=timinge1(1,pass(i,4));seat(1)=pass(i,2)+pass(i,3)+temp1;pass(i,4)=temp1; %装入正常理发所需时间else if seat(2)<=pass(i,2)+pass(i,7)pass(i,5)=2; %由2号服务员理发temp1=timinge1(2,pass(i,4));seat(2)=pass(i,2)+pass(i,3)+temp1;pass(i,4)=temp1; %装入正常理发所需时间else if seat(3)<=pass(i,2)+pass(i,7)pass(i,5)=3; %由2号服务员理发temp1=timinge1(3,pass(i,4));seat(3)=pass(i,2)+pass(i,3)+temp1;pass(i,4)=temp1;else%计算等待时间x=seat(1);y=1;if x>seat(2)x=seat(2);y=2;endif x>seat(3)x=seat(3);y=3;endpass(i,5)=y;temp1=timinge1(y,pass(i,4));pass(i,7)=seat(y)-pass(i,2);seat(y)=seat(y)+temp1+pass(i,3);pass(i,4)=temp1;endendendpass(i,6)=pass(i,2)+pass(i,3)+pass(i,4);endfunction envirment%场景设置pict=figure('color',[0.75 0.75 0.75],'position',[50 50 690 520]);door = annotation('rectangle',[0.4214 0.1405 0.19460.03333],'FaceColor',[0 1 0],'EdgeColor',[0 1 0]);word1 = annotation('textbox','Position',[0.3393 0.1143 0.08750.07619],'EdgeColor','none','FitHeightToText','off','FontSize',12,'Stri ng',{'门口'});seata1= annotation('rectangle',[0.2054 0.8214 0.11070.02857],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); seata2 = annotation('rectangle',[0.2071 0.7476 0.014290.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); seata3 = annotation('rectangle',[0.3021 0.7419 0.014290.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); word2 = annotation('textbox','Position',[0.1607 0.65 0.11070.06667],'EdgeColor','none','FitHeightToText','off','String',{'SeatA'});seatb1 = annotation('rectangle',[0.4579 0.8171 0.11070.02857],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); seatb2 = annotation('rectangle',[0.4579 0.7386 0.014290.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); seatb3 = annotation('rectangle',[0.5564 0.7448 0.014290.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); word3 = annotation('textbox','Position',[0.3768 0.6381 0.10360.07381],'EdgeColor','none','FitHeightToText','off','String',{'SeatB'});seatc1 = annotation('rectangle',[0.6986 0.8157 0.11070.02857],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); seatc2 = annotation('rectangle',[0.6996 0.7343 0.014290.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); seatc3 = annotation('rectangle',[0.7964 0.7333 0.014290.1024],'FaceColor',[0.8471 0.1608 0],'EdgeColor',[0.8471 0.1608 0]); word4 = annotation('textbox','Position',[0.6429 0.6286 0.10180.07619],'EdgeColor','none','FitHeightToText','off','String',{'SeatC'});watierplace = annotation('rectangle',[0.8214 0.1762 0.019640.3857],'FaceColor',[0 1 1],'EdgeColor',[0 1 1]);word5 = annotation('textbox','Position',[0.7107 0.2 0.11790.0619],'EdgeColor','none','FitHeightToText','off','String',{'等待席'});function leaveflash(xx,yy,zz)global man1 mana1 mana2 mana3 mana4 mana5global manb1 manb2 manb3 manb4 manb5global manc1 manc2 manc3 manc4 manc5global mand1 mand2 mand3 mand4 mand5global mane1 mane2 mane3 mane4 mane5switch xxcase 1for ii = 10:-1:1face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(mana1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)]);set(mana2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);set(mana3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);set(mana4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);set(mana5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);MM(ii)=getframe;endcase 2for ii = 10:-1:1face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(manb1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)]);set(manb2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);set(manb3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);set(manb4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);set(manb5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);MM(ii)=getframe;endcase 3for ii = 10:-1:1face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(manc1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)]);set(manc2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]); set(manc3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]); set(manc4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);set(manc5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);MM(ii)=getframe;endcase 4for ii = 10:-1:1face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(mand1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)]);set(mand2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]); set(mand3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);set(mand4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);set(mand5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);MM(ii)=getframe;endcase 5for ii = 10:-1:1face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(mane1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)]);set(mane2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]); set(mane3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]); set(mane4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);set(mane5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);MM(ii)=getframe;endcase 6for ii = 10:-1:1face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(manf1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)]);set(manf2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]); set(manf3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);set(manf4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);set(manf5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);MM(ii)=getframe;endcase 7for ii = 10:-1:1face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(mang1,'position',[face(1,1) face(1,2) face(1,3)face(1,4)]);set(mang2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]);set(mang3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]);set(mang4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)]);set(mang5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]);MM(ii)=getframe;endendfunction [face1,face2,face3,face4,face5]=personcreat(vect)if vect==0face1 = annotation('ellipse',[0.4625 0.1786 0.09820.1381],'FaceColor',[0.6824 0.4667 0],'EdgeColor',[0.6824 0.4667 0]); else face1 = annotation('ellipse',[0.4625 0.1786 0.09820.1381],'FaceColor',[0.4 0.6 0],'EdgeColor',[0.6824 0.4667 0]);%[0.2096 0.6881 0.09821 0.1381]endface2 = annotation('line',[0.4875 0.5018],[0.2667 0.2667],'LineWidth',3); face3 = annotation('line',[0.5214 0.5339],[0.2667 0.2667],'LineWidth',3); face4 = annotation('line',[0.5125 0.5125],[0.25 0.2309],'LineWidth',3); face5 = annotation('line',[0.4968 0.525],[0.2081 0.2072],'LineWidth',3);function serveflash(xx,yy,zz)global man1 mana1 mana2 mana3 mana4 mana5global manb1 manb2 manb3 manb4 manb5global manc1 manc2 manc3 manc4 manc5global mand1 mand2 mand3 mand4 mand5global mane1 mane2 mane3 mane4 mane5switch xxcase 1for ii = 1:10 %开始服务动画face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(mana1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)])set(mana2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])set(mana3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])set(mana4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])set(mana5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) MM(ii)=getframe;endcase 2for ii = 1:10 %开始服务动画face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(manb1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)])set(manb2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])set(manb3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])set(manb4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])set(manb5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) MM(ii)=getframe;endcase 3for ii = 1:10 %开始服务动画face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(manc1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)])set(manc2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]) set(manc3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]) set(manc4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])set(manc5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) MM(ii)=getframe;endcase 4for ii = 1:10 %开始服务动画face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(mand1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)])set(mand2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])set(mand3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])set(mand4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])set(mand5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) MM(ii)=getframe;endcase 5for ii = 1:10 %开始服务动画face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(mane1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)])set(mane2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]) set(mane3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]) set(mane4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])set(mane5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) MM(ii)=getframe;endcase 6for ii = 1:10 %开始服务动画face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(manf1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)])set(manf2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)])set(manf3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)])set(manf4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])set(manf5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) MM(ii)=getframe;endcase 7for ii = 1:10 %开始服务动画face(:,1)=man1(:,1)+yy*(ii-1);face(:,3)=man1(:,3)+yy*(ii-1);face(:,2)=man1(:,2)+zz*(ii-1);face(:,4)=man1(:,4)+zz*(ii-1);face(1,3)=0.0982;face(1,4)=0.1381;set(mang1,'position',[face(1,1) face(1,2) face(1,3) face(1,4)])set(mang2,'X',[face(2,1);face(2,3)],'Y',[face(2,2);face(2,4)]) set(mang3,'X',[face(3,1);face(3,3)],'Y',[face(3,2);face(3,4)]) set(mang4,'X',[face(4,1);face(4,3)],'Y',[face(4,2);face(4,4)])set(mang5,'X',[face(5,1);face(5,3)],'Y',[face(5,2);face(5,4)]) MM(ii)=getframe;endendfunction xx=timinge1(vect1,vect)switch vect1case 1if vect<=0.18xx=8;else if vect<=0.4xx=9;else if vect<=0.77xx=10;else xx=11;endendendcase 2if vect<=0.18xx=10;else if vect<=0.37xx=11;else if vect<=0.72xx=12;else xx=13;endendendotherwiseif vect<=0.15xx=12;else if vect<=0.37xx=13;else if vect<=0.74xx=14;else xx=15;endendendend。
基于Matlab的排队问题仿真

自 10 9 9年 E L N R A G开创 性 的论 文 发表 以 来 , 队论 理 论 广 泛 应 用 于通 信 、 事 、 输 、 排 军 运 维
第3 卷 第 6 2 期
21 00年 1 2月
武 汉 理 工 大 学 学 报 ・信 息 与 管 理 工 程 版
J U N L O T I F R TO O R A FWU (N O MA IN& M N G ME TE G N E IG) A A E N N IE R N
Vo _ 2 . l3 No 6 De 2 0 c. 01
应结果 。
统 内有 n个 顾客 的概 率 P () t满足 方程 :
收稿 日期 :00— 6— 2 21 0 2.
作者简介: 李字光 (9 6一) 男 , 16 , 湖北天 门人 , 武汉理工大学理学 院讲师
第3 2卷
第 6期
李 宇光 : 基于 M t b的排队问题仿真 aa l
而某 阶段 服务员 又空 闲无 事 的情 形 。服务 系统 的 服务 能力一 方 面取决 于服 务员 的数量 和服 务员 的 能力 ; 另一 方 面也 取 决 于 顾 客 流 的性 质 。排 队 问 题就 是要 寻求顾 客流 、 务 能 力 和 服务 系统 效益 服
足 这种假 设 , 因此 需要 有更 一般 的方法 。
理 包括 线性 、 非线 性 系统 、 离散 、 连续 及混 合 、 任 单
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Wireless NetworkExperiment Three: Queuing TheoryABSTRACTThis experiment is designed to learn the fundamentals of the queuing theory. Mainly about the M/M/S and M/M/n/n queuing models.KEY WORDS: queuing theory, M/M/s, M/M/n/n, Erlang B, Erlang C.INTRODUCTIONA queue is a waiting line and queueing theory is the mathematical theory of waiting lines. More generally, queueing theory is concerned with the mathematical modeling and analysis of systems that provide service to random demands. In communication networks, queues are encountered everywhere. For example, the incoming data packets are randomly arrived and buffered, waiting for the router to deliver. Such situation is considered as a queue. A queueing model is an abstract description of such a system. Typically, a queueing model represents (1) the system's physical configuration, by specifying the number and arrangement of the servers, and (2) the stochastic nature of the demands, by specifying the variability in the arrival process and in the service process. The essence of queueing theory is that it takes into account the randomness of the arrival process and the randomness of the service process. The most common assumption about the arrival process is that the customer arrivals follow aPoisson process, where the times between arrivals are exponentially distributed. The probability of the exponential distribution function is λλ.●Erlang B modelOne of the most important queueing models is the Erlang B model (i.e., M/M/n/n). It assumes that the arrivals follow a Poisson process and have a finite n servers. In Erlang B model, it assumes that the arrival customers are blocked and cleared when all the servers are busy. The blocked probability of a Erlang B model is given by the famous Erlang B formula,w here n is the number of servers and A=λμ is the offered load in Erlangs,λ is the arrival rate and μ is the average service time. Formula (1.1) ishard to calculate directly from its right side when n and A are large. However, it is easy to calculate it using the following iterative scheme:●Erlang C modelThe Erlang delay model (M/M/n) is similar to Erlang B model, except that now it assumes that the arrival customers are waiting in a queue for a server to become available without considering the length of the queue. The probability of blocking (all the servers are busy) is given by the Erlang C formula,Where ρ if and ρ if . The quantity ρindicates theserver utilization. The Erlang C formula (1.3) can be easily calculated by the following iterative schemewhere is defined in Eq.(1.1).DESCRIPTION OF THE EXPERIMENTSing the formula (1.2), calculate the blocking probability of the ErlangB model. Draw the relationship of the blocking probability PB(n,A) andoffered traffic A with n = 1,2, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.Compare it with the table in the text book (P.281, table 10.3).From the introduction, we know that when the n and A are large, it is easy to calculate the blocking probability using the formula 1.2 as follows.it use the theory of recursion for the calculation. But the denominator and the numerator of the formula both need to recurs() when doing the matlab calculation, it waste time and reduce the matlab calculation efficient. So we change the formula to be :Then the calculation only need recurs once time and is more efficient.The matlab code for the formula is: erlang_b.m%**************************************% File: erlanb_b.m% A = offered traffic in Erlangs.% n = number of truncked channels.% Pb is the result blocking probability.%**************************************function [ Pb ] = erlang_b( A,n )if n==0Pb=1; % P(0,A)=1elsePb=1/(1+n/(A*erlang_b(A,n-1))); % use recursion "erlang(A,n-1)"endendAs we can see from the table on the text books, it uses the logarithm coordinate, so we also use the logarithm coordinate to plot the result. We divide the number of servers(n) into three parts, for each part we can define a interval of the traffic intensity(A) based on the figure on the text books :1. when 0<n<10, 0.1<A<10.2. when 10<n<20, 3<A<20.3. when 30<n<100, 13<A<120.For each part, use the “erlang_b” function to calculate and then use “loglog”function to figure the logarithm coordinate.The matlab code is :%*****************************************% for the three parts.% n is the number servers.% A is the traffic indensity.% P is the blocking probability.%*****************************************n_1 = [1:2];A_1 = linspace(0.1,10,50); % 50 points between 0.1 and 10.n_2 = [10:10:20];A_2 = linspace(3,20,50);n_3 = [30:10:100];A_3 = linspace(13,120,50);%*****************************************% for each part, call the erlang_b() function.%*****************************************for i = 1:length(n_1)for j = 1:length(A_1)p_1(j,i) = erlang_b(A_1(j),n_1(i));endendfor i = 1:length(n_2)for j = 1:length(A_2)p_2(j,i) = erlang_b(A_2(j),n_2(i));endendfor i = 1:length(n_3)for j = 1:length(A_3)p_3(j,i) = erlang_b(A_3(j),n_3(i));endend%*****************************************% use loglog to figure the result within logarithm coordinate. %*****************************************loglog(A_1,p_1,'k-',A_2,p_2,'k-',A_3,p_3,'k-');xlabel('Traffic indensity in Erlangs (A)')ylabel('Probability of Blocking (P)')axis([0.1 120 0.001 0.1])text(.115, .115,'n=1')text(.6, .115,'n=2')text(7, .115,'10')text(17, .115,'20')text(27, .115,'30')text(45, .115,'50')text(100, .115,'100')The figure on the text books is as follow:We can see from the two pictures that, they are exactly the same with each other except that the result of the experiment have not considered the situation with n=3,4,5,…,12,14,16,18.ing the formula (1.4), calculate the blocking probability of the ErlangC model. Draw the relationship of the blocking probability PC(n,A) andoffered traffic A with n = 1,2, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100. From the introduction, we know that the formula 1.4 is :Since each time we calculate the , we need to recurs n times, so the formula is not efficient. We change it to be:Then we only need recurs once. is calculated by the “erlang_b” function as step 1.The matlab code for the formula is : erlang_c.m%**************************************% File: erlanb_b.m% A = offered traffic in Erlangs.% n = number of truncked channels.% Pb is the result blocking probability.% erlang_b(A,n) is the function of step 1.%**************************************function [ Pc ] = erlang_c( A,n )Pc=1/((A/n)+(n-A)/(n*erlang_b(A,n)));endThen to figure out the table in the logarithm coordinate as what shown in the step 1.The matlab code is :%*****************************************% for the three parts.% n is the number servers.% A is the traffic indensity.% P_c is the blocking probability of erlangC model.%*****************************************n_1 = [1:2];A_1 = linspace(0.1,10,50); % 50 points between 0.1 and 10.n_2 = [10:10:20];A_2 = linspace(3,20,50);n_3 = [30:10:100];A_3 = linspace(13,120,50);%*****************************************% for each part, call the erlang_c() function.%*****************************************for i = 1:length(n_1)for j = 1:length(A_1)p_1_c(j,i) = erlang_c(A_1(j),n_1(i));%µ÷Óú¯Êýerlang_cendendfor i = 1:length(n_2)for j = 1:length(A_2)p_2_c(j,i) = erlang_c(A_2(j),n_2(i));endendfor i = 1:length(n_3)for j = 1:length(A_3)p_3_c(j,i) = erlang_c(A_3(j),n_3(i));endend%*****************************************% use loglog to figure the result within logarithm coordinate. %*****************************************loglog(A_1,p_1_c,'g*-',A_2,p_2_c,'g*-',A_3,p_3_c,'g*-');xlabel('Traffic indensity in Erlangs (A)')ylabel('Probability of Blocking (P)')axis([0.1 120 0.001 0.1])text(.115, .115,'n=1')text(.6, .115,'n=2')text(6, .115,'10')text(14, .115,'20')text(20, .115,'30')text(30, .115,'40')text(39, .115,'50')text(47, .115,'60')text(55, .115,'70')text(65, .115,'80')text(75, .115,'90')text(85, .115,'100')The result of blocking probability table of erlang C model.Then we put the table of erlang B and erlang C in the one figure, to comparetheir characteristic.The line with ‘ * ’ is the erlang C model, the line without ‘ * ’ is the erlang B model. We can see from the picture that, for a constant traffic intensity (A), the erlang C model has a higher blocking probability than erlang B model. The blocking probability is increasing with traffic intensity. The system performs better when has a larger n.ADDITIONAL BONUSWrite a program to simulate a M/M/k queue system with input parameters of lamda, mu, k.In this part, we will firstly simulate the M/M/k queue system use matlab to get the figure of the performance of the system such as the leave time of each customer and the queue length of the system.About the simulation, we firstly calculate the arrive time and the leave time for each customer. Then analysis out the queue length and the wait time for each customer use “for” loops.Then we let the input to be lamda = 3, mu = 1 and S = 3, and analysis performance of the system for the first 10 customers in detail.Finally, we will do two test to compared the performance of the system with input lamda = 1, mu = 1 and S = 3 and the input lamda = 4, mu = 1 and S = 3.The matlab code is:mms_function.mfunction[block_rate,use_rate]=MMS_function(mean_arr,mean_serv,peo_num,serve r_num) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%first part: compute the arriving time interval, service time%interval,waiting time, leaving time during the whole service interval %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%state=zeros(5,peo_num);%represent the state of each customer by%using a 5*peo_num matrix%the meaning of each line is: arriving time interval, service time%interval, waiting time, queue length when NO.ncustomer%arrive, leaving timestate(1,:)=exprnd(1/mean_arr,1,peo_num);%arriving time interval between each%customer follows exponetial distributionstate(2,:)=exprnd(1/mean_serv,1,peo_num);%service time of each customer follows exponetial distribution for i=1:server_numstate(3,1:server_num)=0;endarr_time=cumsum(state(1,:));%accumulate arriving time interval to compute%arriving time of each customerstate(1,:)=arr_time;state(5,1:server_num)=sum(state(:,1:server_num));%compute living time of first NO.server_num%customer by using fomular arriving time + service timeserv_desk=state(5,1:server_num);%create a vector to store leaving time of customers which is in servicefor i=(server_num+1):peo_numif arr_time(i)>min(serv_desk)state(3,i)=0;elsestate(3,i)=min(serv_desk)-arr_time(i);%when customer NO.i arrives and the%server is all busy, the waiting time can be compute by%minus arriving time from the minimum leaving timeendstate(5,i)=sum(state(:,i));for j=1:server_numif serv_desk(j)==min(serv_desk)serv_desk(j)=state(5,i);breakend%replace the minimum leaving time by the first waitingcustomer's leaving timeendend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%second part: compute the queue length during the whole service interval %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%zero_time=0;%zero_time is used to identify which server is emptyserv_desk(1:server_num)=zero_time;block_num=0;block_line=0;for i=1:peo_numif block_line==0find_max=0;for j=1:server_numif serv_desk(j)==zero_timefind_max=1; %means there is empty serverbreakelse continueendendif find_max==1%update serv_deskserv_desk(j)=state(5,i);for k=1:server_numif serv_desk(k)<arr_time(i) %before the NO.i customer actuallyarrives there maybe somecustomer leaveserv_desk(k)=zero_time;else continueendendelseif arr_time(i)>min(serv_desk)%if a customer will leave before the NO.i%customer arrivefor k=1:server_numif arr_time(i)>serv_desk(k)serv_desk(k)=state(5,i);breakelse continueendendfor k=1:server_numif arr_time(i)>serv_desk(k)serv_desk(k)=zero_time;else continueendendelse %if no customer leave before the NO.i customer arriveblock_num=block_num+1;block_line=block_line+1;endendelse %the situation that the queue length is not zeron=0;%compute the number of leaing customer before the NO.i customer arrives for k=1:server_numif arr_time(i)>serv_desk(k)n=n+1;serv_desk(k)=zero_time;else continueendendfor k=1:block_lineif arr_time(i)>state(5,i-k)n=n+1;else continueendendif n<block_line+1% n<block_line+1 means the queue length is still not zero block_num=block_num+1;for k=0:n-1if state(5,i-block_line+k)>arr_time(i)for m=1:server_numif serv_desk(m)==zero_timeserv_desk(m)=state(5,i-block_line+k)breakelse continueendendelsecontinueendendblock_line=block_line-n+1;else %n>=block_line+1 means the queue length is zero%update serv_desk and queue lengthfor k=0:block_lineif arr_time(i)<state(5,i-k)for m=1:server_numif serv_desk(m)==zero_timeserv_desk(m)=state(5,i-k)breakelse continueendendelsecontinueendendblock_line=0;endendstate(4,i)=block_line;endplot(state(1,:),'*-');figureplot(state(2,:),'g');figureplot(state(3,:),'r*');figureplot(state(4,:),'y*');figureplot(state(5,:),'*-');Since the system is M/M/S which means the arriving rate and service rate follows Poisson distribution while the number of server is S and the buffer length is infinite, we can compute all the arriving time, service time, waiting time and leaving time of each customer.We can test the code with input lamda = 3, mu = 1 and S = 3. Figures are below.Arriving time of each customerService time of each customerWaiting time of each customerQueue length when each customer arriveLeaving time of each customerAs lamda == mu*server_num, the load of the system could be very high.Then we will zoom in the result pictures to analysis the performance of the system for the firstly 10 customer.The first customer enterthe system at about 1s.Arriving time of first 10 customerThe queue length is 1for the 7th customer.Queue length of first 10 customerThe second customerleaves the system atabout 1.3sLeaving time of first 10 customer1.As we have 3 server in this test, the first 3 customer will be served withoutany delay.2.The arriving time of customer 4 is about 1.4 and the minimum leaving timeof customer in service is about 1.2. So customer 4 will be served immediately and the queue length is still 0.3.Customer 1, 4, 3 is in service.4.The arriving time of customer 5 is about 1.8 and the minimum leaving timeof customer in service is about 1.6. So customer 5 will be served immediately and the queue length is still 0.5.Customer 1, 5 is in service.6.The arriving time of customer 6 is about 2.1 and there is a empty server.So customer 6 will be served immediately and the queue length is still 0.7.Customer 1, 5, 6 is in service.8.The arriving time of customer 7 is about 2.2 and the minimum leaving timeof customer in service is about 2.5. So customer 7 cannot be served immediately and the queue length will be 1.9.Customer 1, 5, 6 is in service and customer 7 is waiting.10.The arriving time of customer 8 is about 2.4 and the minimum leaving timeof customer in service is about 2.5. So customer 8 cannot be served immediately and the queue length will be 2.11.Customer 1, 5, 6 is in service and customer 7, 8 is waiting.12.The arriving time of customer 9 is about 2.5 and the minimum leaving timeof customer in service is about 2.5. So customer 7 can be served and the queue length will be 2.13.Customer 1, 7, 6 is in service and customer 8, 9 is waiting.14.The arriving time of customer 10 is about 3.3 and the minimum leaving timeof customer in service is about 2.5. So customer 8, 9, 10 can be served and the queue length will be 0.15.Customer 7, 9, 10 is in service.Test 2:lamda = 1, mu = 1 and S = 3Waiting time of customerQueue length when each customer arriveAs lamda < mu*server_num, the performance of the system is much better.Test 3:lamda = 4, mu = 1 and S = 3Waiting time of customerQueue length when each customer arriveAs lamda > mu*server_num, system will crash as the waiting time and queue length increases as new customer arrives. For the situation of lamda<mu*server_num, the system performs better when mu and server_num are both not small. If the mu is smaller than lamda or we only have one server though with large mu, the system works not very good. It is may be because that the server time for each customer is a Poisson distribution, it may be a large time though the mu is large enough, so the more number of server, the better of the performance of the system.CONCLUSTIONAfter the experiment, we have a deeply understanding of the queuing theory, including the erlang B model and the erlang C model. What’s more, we are familiar with how to simulate the queue system for M/M/s. Through the simulation, we have known how the queue system works and the performance of the system with different input parameter.。