QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2009-8-12 13:02 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
- I4 T3 j' @/ [
4 h6 a. h, j+ @. m& B2 d2 U
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
4 F. w0 t+ N4 p* q2 e/ f% ?clc;clear;
ST_Idle=0;
5 E0 F5 R' w: J6 M& zST_Busy=1;
EV_NULL=0;5 z% ^, p; {' M4 M3 H( a3 c
EV_Arrive=1;" Q$ I7 c- @( Z  J- g8 y' x
EV_Depart=2;
) T- O$ d$ L7 H3 T8 ?EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
9 P  I: D5 ], J0 X" U, }( ~; f% U% num_custs_delayed=[];  O0 ~0 ]1 Y0 X. d, x& J
% num_delays_required=[];
1 w2 z* |$ `& P: b% num_events=[];
; T* s2 R# ?: y% num_in_q=[];
5 |2 r5 R# O% a/ ]% server_status=[];3 c; D" t, v, t
% area_num_in_q=[];; l  f! t5 k! ]1 {' }" @* `; z' G
% area_server_status=[];
' Z9 Z& l' _. u4 H1 k! w% mean_interarrival=[];7 H2 X0 ^( L4 E* i: X
% mean_service=[];& R4 E! C6 i# n% z
% sim_time=[];
' M* P: q6 D! p9 G& C9 Q' ~, O. l% time_last_event=[];: y8 f5 I! e) A, U$ I
% total_of_delays=[];
/ R2 |  H3 {. J% J' @3 `%
$ Q& }/ `- a/ P7 j+ j8 ]time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
  i7 m4 ?7 \7 |! a  j' O3 V+ a%
仿真参数  X' c2 z# @7 k) q" n1 j
num_events=EV_LEN-1;
& ?/ e5 R7 T! q- p6 Fmean_interarrival=1;. T5 k. }  u  N& s& a& M. f7 ^
mean_service=.5;* i. _3 T4 m; k' J9 [
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');' C7 z  u+ z  J* t- V& M# ]% o
fprintf(outfile, 'Single-server queueing system\n\n');
2 X5 |: W+ c  F0 s; y) ofprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
/ U& |; L3 M: ^  Gfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);1 i, }. C+ H9 y2 I' p" V6 r
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1- Y7 [# {1 R$ S% N# H
sim_time=0.0;2 T% M5 a$ _' R8 U( `
%     /* Initialize the state variables. */
    server_status   = 0;%idle" \% d! k- I1 s7 u7 z1 J9 z$ v
    num_in_q        = 0;
0 k% J& E7 I" N2 V8 b    time_last_event = 0.0;
# W& r# t3 F' S/ |4 I& k, L   
7 N0 l* v" r& e, U( D%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;2 {+ a: V4 T5 J5 k
    total_of_delays    = 0.0;4 F6 Q) i* k+ Q% t
    area_num_in_q      = 0.0;
' W: F  A, D7 ~    area_server_status = 0.0;! n5 w5 X5 y: F) z. X3 R
   
$ V) d. L6 P* o9 [7 ?- K%     /* Initialize event list.  Since no customers are present, the departure2 W! u, D0 Q5 s6 h7 H
%        (service completion) event is eliminated from consideration. */
" I3 G1 X, \+ ~0 ^$ G    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);) I3 A) ?( a/ a; N
    time_next_event(EV_Depart) = 1.0e+230;

& `  L% h6 v4 n1 J2 j   
* a, H6 k6 g+ F   
% o, |6 q" F9 T4 B% P) f; w   
9 S5 [# J& S3 z9 n. j5 s %%%%%%%%%%%%part2
! O5 F  d+ @0 F7 }; U* Mwhile (num_custs_delayed < num_delays_required)' l8 \1 n" `4 e- F- ~  F/ ~4 t* }
%Run the simulation while more delays are still needed.$ _0 V* o3 |. q4 B  b: y5 ]
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
: k. A2 E. e; P2 ~     next_event_type = 0;
/ L  S& r2 r" f% {& p$ \7 G/ j     6 _9 R: F6 t9 ?
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events; T" P4 e! w. N) [. v
        if (time_next_event(i) < min_time_next_event)
% |3 U" F2 X5 f9 _* B9 ^% `- ]            min_time_next_event = time_next_event(i);
3 }4 R5 o/ ^, B1 M0 C8 N            next_event_type     = i;$ T( z& L3 U$ Z, a0 z
        end
6 `  E9 j* A( Z( w% A/ |    end% a8 V  _- h) s2 L$ W  c5 i
   
, B3 H, N( A8 a4 b% ~1 R/ {/ _%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
. b; N& `' K7 H6 b% }5 j# i        5 p( K1 l  Y& `- b$ i
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);1 b7 F) B, |% Y
        exit(1);" m& l4 l/ n" H$ X& B
    end8 M1 y# V! C. \' d# ]+ K' A
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
& c" t) r2 m; G# A
) }# V* l: j$ X& X. d   
% T- \: o) J6 ?+ f/ K%/* 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;
  e7 z; L# U0 F9 x2 n    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;5 X( Y7 W& C5 C( R  O
   
' a- F5 T" C* Z/ y0 l4 c%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
8 Q: N) g  W1 D4 A$ e2 N        double delay;
/ q) ]  R* x1 X6 R9 ^        5 z. \4 i* P: q* y
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
. q+ ^  N7 S* |7 y/ K: l3 i            
2 r: N- Y$ V) w( @%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;- Z. Z3 z1 F! f  U3 R- B
            
+ O0 [& [% k+ Y( ~- Q  o5 I%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 * y8 n% t5 |2 ?4 z0 C0 d9 {2 Q
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');7 b' ^% o0 ~$ d, u+ \
                fprintf(outfile, ' time %f', sim_time);
& @$ V7 [% ^5 K) H0 n                exit(2);
8 y5 F0 s$ Z. E/ y            end" R$ p- J0 p2 n& _4 z7 Y
%/* 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
! Z3 U6 w2 L% n  S6 p& G, g%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity 1 u/ U/ C# A6 z& m4 m
%and do not affect the results of the simulation.) */
            delay = 0.0;% R7 Q! x9 \& [! D
            total_of_delays =total_of_delays + delay;0 F% d0 {1 z1 m/ a
            
4 [: C/ h4 |) o! U) Q7 n, N7 L) X4 S%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
6 O7 N0 u3 e, Q& E5 D            server_status = ST_Busy;
& W( G! h' c' `* q9 `# h            
$ e# d3 e) D, u1 u( _; G%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);. e: Q$ i7 c0 i& I. P
        end %    if (server_status == ST_Busy) + n" j& c( B$ n& ]
%%%%%%%%depart( h7 m" l# E3 \4 @; X
    else" ]7 f  M, g3 f/ z6 }8 a" F5 p
            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;9 _$ ^6 Z$ C6 y8 |
            time_next_event(EV_Depart) = 1.0e+230;& t& {/ W' k$ x+ q) R% `
    3 A/ S1 V9 v! o# f' _( m& x
        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);
# f& A! `& v0 U* @5 p. Z            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;
8 C( I8 |8 c0 n            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));  |! x; q9 z  u9 D
            time_arrival=tempForPop;
1 f- [/ a# D. q! ^" o" J0 e        end %if (num_in_q == 0)
* j4 s" x1 X$ t( l        5 `" |+ S" v4 U3 w/ O3 b
    end %if(next_event_type==EV_Arrive)
5 {/ b- I; k5 N$ J4 S    ) a8 v( Q' r" ^$ A' L
end %while
' Z7 W7 t8 d. G. N: L               
# x0 }4 K* r/ p+ ~
%%%%%%%%%% part 3% @7 m0 o7 z  @: P/ Z1 ~- a+ _, d+ t
%/* 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);' \" x" {0 J0 l* n
    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);% ?9 b1 u2 j1 K) b# X+ h
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
3 X, E* J& g5 V7 _    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);5 _3 y/ X8 W" ^& Z' e/ y
    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-9-12 10:16 , Processed in 0.403768 second(s), 106 queries .

    回顶部