QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

( P/ G/ g0 k5 j0 n* h1 ]6 E' L2 H: d: j. l
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab* S8 R! j7 Q+ q: U5 `' e
clc;clear;
ST_Idle=0;
# q- R  H( T% {* WST_Busy=1;
EV_NULL=0;2 ^2 }0 ]. D) U- z
EV_Arrive=1;
, u  m- S4 F: s' F" S7 CEV_Depart=2;
0 c/ y; y* B( |" ?* M/ B6 cEV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
6 m) T/ x2 f# M% P) L$ u; R, ?9 p+ n  D% num_custs_delayed=[];
+ z; L0 k  ~5 b/ @* m9 I% num_delays_required=[];
6 n  R0 r/ z$ D$ S7 v8 y& H: W% num_events=[];9 x" ?& h$ E1 A' F3 @$ A# y
% num_in_q=[];6 p7 a0 A' B% T! X! W6 z
% server_status=[];) W* O9 I6 t" C0 X, e& a- o3 f
% area_num_in_q=[];2 e$ |9 G; N# L" r+ r
% area_server_status=[];3 r6 k; l1 y. N' o6 F* d$ Q* X
% mean_interarrival=[];
) z1 r( b9 l4 D6 O+ r  F+ W% mean_service=[];5 M+ t% c' c* n: ]7 b; E+ N
% sim_time=[];: I1 A+ p; j1 F: Y: H5 Q! R) X
% time_last_event=[];
! x% ]: K% \, i% total_of_delays=[];
% j0 o/ I% X2 X: [; x% 2 [$ }/ T) O* J' i4 _' r
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
0 ]- a5 }  y/ O: E%
仿真参数
+ l: h$ n$ Y3 R/ v% ?: i. P9 hnum_events=EV_LEN-1;
/ r& x5 E7 s* O! amean_interarrival=1;
% K5 q0 j" X4 @6 E" ]$ N) emean_service=.5;
0 N( {4 p* o6 @- q: K. tnum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');( f# T/ r2 X) W" U. ^% R$ f
fprintf(outfile, 'Single-server queueing system\n\n');' Y0 d. k$ O1 w3 U
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
$ Y4 s/ j% U. R" ]6 d; ufprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
% k' r! h+ c$ F8 y3 K4 r1 kfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
4 x( N; t# C9 {$ ~8 }, [$ L, msim_time=0.0;
7 q1 i5 i% @5 l%     /* Initialize the state variables. */
    server_status   = 0;%idle
* K0 E) l' U6 ?# W. f    num_in_q        = 0;; t/ V" P" E5 w/ ^( R: m
    time_last_event = 0.0;9 N6 s# |2 ?: N! y
   
+ P* \; h. D9 E0 ^%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
: P: A$ Q4 i# S9 o8 e2 d    total_of_delays    = 0.0;2 q9 I4 e& V5 b4 Y1 s
    area_num_in_q      = 0.0;
2 \9 ^0 J, z- Z, d; W" x( d- _    area_server_status = 0.0;
2 L( }/ z8 Q( x    # j# L: [, X% K! w) z: f! i
%     /* Initialize event list.  Since no customers are present, the departure8 ~$ d7 \* w; s+ v. p7 q' M. t
%        (service completion) event is eliminated from consideration. */
2 _; h0 k( Y% C! l: m    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);" d/ `) x- |# M' s
    time_next_event(EV_Depart) = 1.0e+230;

2 ?: i: i- k" v# u! V5 [   
. N3 k5 \5 D' Z' X- A; C# I   
# X+ {4 `. z: E9 U) K* e& D   
. C! l' R- l1 y, `( }' g %%%%%%%%%%%%part2
+ i) M( o, d+ ?$ cwhile (num_custs_delayed < num_delays_required)* W7 s/ f9 C6 G+ T
%Run the simulation while more delays are still needed.
" T2 }+ x; z0 ^  ]- m* W%/* Determine the next event. */
     min_time_next_event = 1.0e+290;0 k0 z/ W, z; l2 C4 f
     next_event_type = 0;
9 k) |+ R& o) Y       I6 n6 ^4 v8 I/ ^# ?, ^
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events7 i/ k- z/ d/ X' {) x
        if (time_next_event(i) < min_time_next_event) 1 q! f7 L4 y: u+ z9 a+ d0 P1 l6 a
            min_time_next_event = time_next_event(i);
: S- e/ U) M$ w! Z4 |; _$ L7 {            next_event_type     = i;
6 d3 G: n8 J9 ~& k# [( r5 l& q        end. f) b% D3 r5 }$ f( j- O& h6 V
    end; s1 v; q8 y* I. Y
    # ^, l  z; h1 N. o* F
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) - f1 a+ x2 C1 {
        
; c5 t+ H$ y; [- F%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);/ S% n1 c+ t' ?. c, Z9 N+ N8 U
        exit(1);
# M* ?* ~1 J+ i5 x    end
/ G% E  C1 _0 f# h$ X%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
# r7 r+ h, Z5 Q2 c1 e6 A) T
% i" e1 @& J5 ~! S7 G   
' H; o6 `" H8 n9 u1 I! C9 _$ m, |%/* 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;, @- Y$ ^* |8 M, `# r
    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;
  I6 L4 u# s* o! N" T; A7 a1 v   
, [7 ]+ L7 O' j/ k%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
! g1 S' D- @9 r# y$ R/ L        double delay;4 s7 [) [/ s0 v) N% U  `- u  h$ c
        
' ~+ _% \9 y# s/ |( v( {%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) ) t2 w. x& {; c, L+ j% J
              l% D9 O8 |" ?2 C- {& N4 c
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;$ y" c" w, v/ P/ M7 }% K- z
            
. p, b. H6 _" M: s; j%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 ! n. `, A8 D! ]; w9 e
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
/ W. i/ K" G, P                fprintf(outfile, ' time %f', sim_time);
* j& s. p8 z& V; H+ F: ]9 C                exit(2);( L1 y# Z* ^+ n
            end
, p* n# @% r4 H/ I%/* 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 - K2 X% ~" v" ]  ?
%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
" n! X- y1 c$ ]8 ?6 @6 `9 ~# C" \%and do not affect the results of the simulation.) */
            delay = 0.0;+ @! p/ I- W* _3 ^, N
            total_of_delays =total_of_delays + delay;
- g5 S  X1 N5 `9 a! o8 [, H            4 y/ T) n$ @8 z( H, }
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;+ ~) @9 [# ?( g7 |: I% ?
            server_status = ST_Busy;
1 J% L' V4 r6 m: H2 e; d  P1 E            
: {  U8 C7 N1 w- i, g( q1 h& T%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);# c  L  G2 J" N' D5 V
        end %    if (server_status == ST_Busy)
* z# c: B5 X) K( r9 v* T4 O%%%%%%%%depart3 o8 I! e: j/ A& K6 f1 l
    else
- k  e' F' R! z9 }  w            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;- j6 E' D  w0 S+ h) x& T
            time_next_event(EV_Depart) = 1.0e+230;
  ^1 o* v5 b6 @' L2 o6 y: H   
  c, ~" a/ ^+ D" G7 W# T- b        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);, O1 T: C0 w8 X! H
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;
) o2 o7 d  V. C9 p            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));) [3 Q7 e! u4 `5 x/ \
            time_arrival=tempForPop;7 w; c- {# Y, z7 l5 ~6 e
        end %if (num_in_q == 0)
( Q9 F) R3 i7 [  a  h3 `        " b' w( V9 y- a" d5 a8 U+ l
    end %if(next_event_type==EV_Arrive). M; s3 H2 A# T
    ' {$ h2 D$ x) p4 S) ~1 F5 M2 q5 v
end %while
  n, @0 b, g" z3 \$ Q$ o9 L3 x               

6 n: J! f6 m( b" z7 v. `/ v%%%%%%%%%% part 3( g" B7 o( h- K/ F. C5 w% D
%/* 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);; I# o& D, q# u( J! r
    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);- k) K  I" L. P
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
1 L. s3 F) R- B( R8 D' z% i    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);9 B" X! Z  j3 S' {; 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-9-12 08:18 , Processed in 0.429641 second(s), 106 queries .

    回顶部