QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 14224|回复: 10
打印 上一主题 下一主题

M/M/1排队系统性能仿真代码(MATLAB)

[复制链接]
字体大小: 正常 放大
qlau2007        

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2009-8-12 13:02 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

% j9 V2 `( R# `( f- O# ]
5 J' w9 R, t7 m+ ?: c
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab; m! U% L. S, x+ t7 |: ?1 [
clc;clear;
ST_Idle=0;
5 z: Z  Q3 d! M" D- G$ ~( lST_Busy=1;
EV_NULL=0;
/ M; O2 Z2 |8 d, _1 o$ w, vEV_Arrive=1;4 n+ x# @4 C2 s" O
EV_Depart=2;' a1 W2 ~1 e* T# l
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];# |; ]3 I  Z0 x; o6 Y2 F
% num_custs_delayed=[];. w3 ?9 s2 a& e/ \/ u
% num_delays_required=[];
0 _6 m- p$ _( n9 g- |% E& J# W% num_events=[];
8 U; H+ ?3 d' c+ y9 ]& F. p* p% num_in_q=[];
% X: J' N' \$ D! A% server_status=[];# O. M6 w! a0 W9 W$ t3 L9 S' s; j+ ~% E
% area_num_in_q=[];
% s: d  ]% H/ \, V% area_server_status=[];  v( t- W6 v& o/ N2 K
% mean_interarrival=[];# x3 f% E2 d8 \/ d; e0 |4 l
% mean_service=[];
" h+ l5 \/ }$ u" W# w5 w% sim_time=[];
6 |' G) A$ ~3 Q8 K% time_last_event=[];$ R+ R* x& i( u1 N9 v. W; M
% total_of_delays=[];7 g! b$ F$ z: d8 a6 I5 c% ~
%
; i  @. ]' \7 P9 Ctime_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
! U$ M* Q; D" X# H$ `" W%
仿真参数3 m" @0 U8 \. L9 Q% u; \0 i$ B4 T, P
num_events=EV_LEN-1;
8 {( t$ [2 @( h/ T8 t9 R* c/ h% \- Mmean_interarrival=1;( }' g  s' E  Q# a1 |. @
mean_service=.5;
* V, q" u* X: l: B: o" s; pnum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');% [1 r8 S& E! |6 s# A" y  o: z
fprintf(outfile, 'Single-server queueing system\n\n');
# U' V! z( o& J& ]1 q; mfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);8 x9 t3 |  \8 d2 I2 u; e
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);0 u7 T7 }9 Y7 q+ w/ I- q
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
& _. \7 e. @+ ]% Lsim_time=0.0;
. g4 L+ f' b1 i: z) ^$ F9 F%     /* Initialize the state variables. */
    server_status   = 0;%idle
2 y" X7 K7 A. E& [    num_in_q        = 0;
+ U" K# v' @; D  T9 ]  ^    time_last_event = 0.0;
6 m+ F% e6 d; v  C   
$ N5 p# E5 O6 e%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
7 M7 \: O3 T+ a    total_of_delays    = 0.0;' a4 {/ ^$ B5 N8 N- S0 ^
    area_num_in_q      = 0.0;
, F) {! l# k) W    area_server_status = 0.0;
3 T: d3 |. `) K* O   
; O% C% D% P+ m, `* E* q%     /* Initialize event list.  Since no customers are present, the departure
% w: T4 {, O  P%        (service completion) event is eliminated from consideration. */# v- h* F" Z" h( T; e7 T" C# D
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
: U0 v8 J: h3 V5 ^- t    time_next_event(EV_Depart) = 1.0e+230;

1 b& v3 B% Z; h' K3 c    + z; {6 X) `2 Q/ D3 ^
   
/ `$ z1 p1 U3 W) F! e$ I    / z5 _5 M5 t  w6 q6 }
%%%%%%%%%%%%part2
# }4 w# l+ L2 y. |* B4 k/ ]while (num_custs_delayed < num_delays_required)- e4 W% q: }. P* @6 i
%Run the simulation while more delays are still needed.
1 w( Z7 m( K4 v- I9 ~& {%/* Determine the next event. */
     min_time_next_event = 1.0e+290;( Z( W( c8 b4 X  F) L
     next_event_type = 0;% C+ S6 @% q" {' c9 u
       Y& ~3 W: J; U1 J! R
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
! F+ a. t% s% y. _7 V        if (time_next_event(i) < min_time_next_event)
) |! n$ W( c) z" b            min_time_next_event = time_next_event(i);* K1 t, f0 H! t
            next_event_type     = i;$ f+ f+ s7 |; P5 v$ A1 x
        end* Z) n) k0 F; W& F: U6 i" N
    end% R' f7 ^  z. M( x1 Y1 J
   
  h2 W0 ^4 r/ r( a/ J# d3 f2 P%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
7 H7 ^# C( N2 F; [: C        
9 K7 W1 e" R# h' |%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);; H5 k# E3 S- Z1 F" @
        exit(1);4 W- D5 J! V6 i# v
    end) ?- s& Q9 t4 Z  f& i
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;9 f3 d* q% c" |  S4 `7 [! L

6 r$ }: j. Q" y/ {   
$ ^0 z' m7 P; E( o%/* Update time-average statistical accumulators. */
    double time_since_last_event;
%/* Compute time since last event, and update last-event-time marker. */
    time_since_last_event = sim_time - time_last_event;0 v' P2 a* ]0 b9 N/ e3 l7 S
    time_last_event       = sim_time;
%/* Update area under number-in-queue function. */
    area_num_in_q=area_num_in_q +  num_in_q * time_since_last_event;
%/* Update area under server-busy indicator function. */
    area_server_status =area_server_status + server_status * time_since_last_event;8 m  f* @; m# ?; x" d
   
+ G) Y* L, h3 {5 h%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)- ]" G$ Q: C0 K- f: `
        double delay;9 Q! h9 i# U6 r
        ( X" E- M# T& d4 O* ^8 q
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) 1 W0 u7 V( H! n5 e- a% l, E3 X
            ( [3 k7 x4 M1 U9 a" n$ v
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;5 H4 x+ v& F2 W4 t& `, O
            4 t" y: e6 R+ I
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 % e: H  L$ v4 _& C* \/ D
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');) g3 C1 c, e1 _+ N: i; L  l4 J* o
                fprintf(outfile, ' time %f', sim_time);  w2 N* f3 ^* ^: S% |* E
                exit(2);
0 W) b3 h3 g- h& \' _  C            end
' Y  U0 Z  y: ?6 t1 O7 }( q%/* There is still room in the queue, so store the time of arrival of the arriving customer at the (new) end of time_arrival. */
            time_arrival(length(time_arrival)+1)=sim_time;
        else
5 e0 C% a' @! S# i! @% h%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
) t3 c& O2 E% Y+ T: D" i) k. Z% E%and do not affect the results of the simulation.) */
            delay = 0.0;6 s; o' K# o, x
            total_of_delays =total_of_delays + delay;" e. r8 j0 [6 Y7 h7 t
            & ^- N$ w& O1 c$ ]% y3 D  k
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;$ t; Q+ M! v* N0 |# x, ~
            server_status = ST_Busy;
  x& i3 v8 W/ ?! s( }/ l            , X6 `$ l7 M% U( {  g- r9 w1 E+ [6 h& d
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);1 Y* W% s# r7 s' J' k
        end %    if (server_status == ST_Busy) " G' r4 _8 L2 L5 z* @
%%%%%%%%depart1 E' i; B# V# \: m9 O
    else. i$ i1 P# b* ^! D& Z6 J
            double delay;
%/* Check to see whether the queue is empty. */
        if (num_in_q == 0)
% /* The queue is empty so make the server idle and eliminate the departure (service completion) event from consideration. */
            server_status      = ST_Idle;: D) Z9 Y: X( [
            time_next_event(EV_Depart) = 1.0e+230;' z# o! c6 ]4 q. g" c1 D4 F3 U
    7 f% b$ `3 A, e+ @
        else
%/* The queue is nonempty, so decrement the number of customers in queue. */
            num_in_q=num_in_q-1;
%/* Compute the delay of the customer who is beginning service and update the total delay accumulator. */
            delay = sim_time - time_arrival(1);
4 B; o( n1 j' A5 q2 {            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;7 m! P: P2 \4 L% s; E3 |2 q3 ]4 s
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
%/* Move each customer in queue (if any) up one place. */
            tempForPop=time_arrival(2:length(time_arrival));7 V3 x' G) i! }+ V# I
            time_arrival=tempForPop;/ u5 W. I$ u- v# \, J
        end %if (num_in_q == 0)4 X% F' K' e% }( G6 C% K
        6 [/ v0 G" h% F9 p$ J/ N
    end %if(next_event_type==EV_Arrive)
5 \6 F* I, b1 P9 f& l  e    ) X9 q% E; A; [/ Q* r. V8 B
end %while* F0 n! a8 |" Z$ J0 l2 q
               
  _( G$ ]. s3 D5 W
%%%%%%%%%% part 35 R; k1 k# T' ~% A7 ?  |! W
%/* Invoke the report generator and end the simulation. */
    fprintf(outfile, '\n\nAverage delay in queue%11.3f minutes\n\n',total_of_delays / num_custs_delayed);
, l9 {: w0 E' {  k: F. _$ p    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
& M1 a: b9 g+ x7 s    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);: X0 B1 }3 Y# g# Z
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);6 O" n6 F  Y+ J
    fclose(outfile);
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
liuyaliss        

0

主题

3

听众

439

积分

升级  46.33%

该用户从未签到

新人进步奖

回复

使用道具 举报

rockhuman        

2

主题

4

听众

415

积分

升级  38.33%

该用户从未签到

新人进步奖

回复

使用道具 举报

2

主题

5

听众

152

积分

升级  26%

该用户从未签到

回复

使用道具 举报

leeeff        

0

主题

3

听众

85

积分

升级  84.21%

该用户从未签到

回复

使用道具 举报

2

主题

4

听众

253

积分

升级  76.5%

该用户从未签到

群组数学建模

群组数学趣味、游戏、IQ等

回复

使用道具 举报

2

主题

2

听众

406

积分

升级  35.33%

该用户从未签到

新人进步奖

回复

使用道具 举报

3

主题

3

听众

41

积分

升级  37.89%

该用户从未签到

新人进步奖

回复

使用道具 举报

GunBreaK        

0

主题

4

听众

32

积分

升级  28.42%

  • TA的每日心情
    奋斗
    2014-5-29 09:00
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    新人进步奖

    回复

    使用道具 举报

    jamesgeng        

    0

    主题

    4

    听众

    6

    积分

    升级  1.05%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-6-17 21:55 , Processed in 0.480953 second(s), 106 queries .

    回顶部