QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

+ T$ Y0 Q+ M4 X4 f
5 T# f5 w! V4 t5 Y: q
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab$ G+ F" S3 {. s
clc;clear;
ST_Idle=0;
& ]! ^4 S5 `- p5 s- ~9 GST_Busy=1;
EV_NULL=0;
6 x  E6 {# ~6 e# \EV_Arrive=1;
5 U, f8 k6 A; ^$ }* c7 IEV_Depart=2;/ s' T/ Z) b5 T, x$ J- w# E, g
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];" V) D& n8 B+ M/ F
% num_custs_delayed=[];2 l3 S9 I+ y  d* ~: M9 z
% num_delays_required=[];& |! F9 V! \$ M- M3 |
% num_events=[];
3 |* Q' e/ X6 @! Z/ y; m, l# _% num_in_q=[];9 ~5 \  @/ J/ J2 b- t5 H5 @& g, D
% server_status=[];
. ^( {3 `) ?3 w) L- n% area_num_in_q=[];# \. ?+ q2 F( ~, @
% area_server_status=[];5 ?* Z7 b6 N  ]" H( U9 Z: l; d  v
% mean_interarrival=[];
" |) W; Y" @+ N1 K; ^% mean_service=[];. O0 E' R, P. e# b0 Z
% sim_time=[];
: r9 F6 f- w$ @4 }" E" O& E) f4 H% time_last_event=[];
- z' A& D2 _6 U1 H, |" I  b% total_of_delays=[];2 M9 Q5 |" E& Q9 o) w8 E8 D- ^
%
" A2 C2 E5 |( \3 `, F* Atime_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
! i" K2 L5 r* h%
仿真参数
. J8 n! F8 t/ ^, K" d" Gnum_events=EV_LEN-1;
5 u( E9 J: m1 Emean_interarrival=1;! f: s9 @$ a. C/ u' W
mean_service=.5;8 E3 r" c  R9 V0 j  J
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');3 L2 F' A, l; j( u0 }5 f
fprintf(outfile, 'Single-server queueing system\n\n');* h# j" J9 L  x8 H
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
8 i4 u% Y# K6 z  U. s5 [fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
" @5 A: [9 h# M! R$ rfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1$ H! W1 C4 }. h. a# r  F9 ?0 r0 J+ \
sim_time=0.0;' \! i/ H% O8 j" }4 S. C
%     /* Initialize the state variables. */
    server_status   = 0;%idle
0 \( N/ t. P8 j    num_in_q        = 0;' ?, @9 H* U  p8 P& M6 n/ [
    time_last_event = 0.0;- g5 I0 L5 A% [# N3 \4 A5 n. d
   
( W7 e' O5 }# f6 C0 _: C6 x%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
( B& R2 }1 d$ v; H    total_of_delays    = 0.0;
0 o3 c0 c  C! J: L4 m- S    area_num_in_q      = 0.0;
  ?7 z) U1 _- Y" ~- M    area_server_status = 0.0;" N6 i# w1 _2 ]: o" j, Y" G9 I
    - s5 o; K& d; q0 F; a" F
%     /* Initialize event list.  Since no customers are present, the departure+ c* w' j, }. [
%        (service completion) event is eliminated from consideration. */
& N* Q7 P$ y# S' N% a    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
5 ?) g: I% A4 ?$ F, L2 ]% U% L    time_next_event(EV_Depart) = 1.0e+230;
' a7 j. k, g) c- C7 Y6 {/ b' I1 S
   
5 o5 Y; m6 c. Z+ [/ ?6 }) @4 g1 s    / J9 n! H' U, [  ?# F+ v* L. G! J
   
- V  B7 x! J- j7 }7 w) ^ %%%%%%%%%%%%part2
1 S5 V* F; t) A9 g* b1 R4 Rwhile (num_custs_delayed < num_delays_required)
  S& f8 R/ Y$ v' N& k%Run the simulation while more delays are still needed., {* G2 `- n3 |5 t/ D, e
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
: L, v8 K0 l5 v0 N; ^# ?. A9 s     next_event_type = 0;
% }) ~1 Z5 e* F     
0 ?2 ]  {$ D# E( K. k%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
; G! f9 @0 E% R- R8 {5 L4 X        if (time_next_event(i) < min_time_next_event) 5 u% N1 R& q; S% d  M
            min_time_next_event = time_next_event(i);- {( X9 P% q  q8 L" Y: B( S
            next_event_type     = i;
3 b% p& _7 Q$ U9 T        end0 I) u* H  ^0 I1 s
    end
, x9 H9 E* v$ m, P   
8 ~) t2 {4 A7 ]9 v: w+ o. d%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
6 O( S4 c, H) H( ^, e) I  t        6 A0 w( ]" v7 k/ Y) m% ?. K% [
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);, U3 n& I+ S4 b7 G
        exit(1);
6 X  [2 Y/ T- d7 n6 z2 H0 ~    end
1 X$ R* L3 v) T& C; |" Q%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
' b. m, Q+ [0 @: g+ K8 \) f5 d9 ~ , e5 J. f4 k& e" H
    - ^+ `. _7 h9 S- O# l, ]# r
%/* 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;
+ k% _, h% x7 V- ^( B    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;
, W; a+ i6 d6 N   
3 D; j$ }! l& v8 l& G%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)0 ?) Y( C+ y# [9 B- X
        double delay;
6 w5 O: Q) z1 g: G1 ?) h        
  F3 r* |2 v5 _9 K. |%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) 9 t( ?1 U# q1 @% |
            
$ n: a. r/ G8 H7 P8 j$ _, U%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
6 W+ I) ~9 `- B( J: o, X5 F- |            
) G$ t  R4 Q5 Q4 I8 J' W+ ?%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 1 h4 e9 L! a5 l! _, ]
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');0 o1 q- w5 |4 @; g& Y
                fprintf(outfile, ' time %f', sim_time);  M+ f+ f( z' k! K1 j" z+ T- H
                exit(2);, E8 X$ p+ E& l( I
            end: P1 b+ k# h: Q* w) u; \
%/* 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
/ G3 x: |$ c: k1 E) n%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity * I, I4 }0 o* `
%and do not affect the results of the simulation.) */
            delay = 0.0;
4 j! g9 R6 L- {# |! K$ _            total_of_delays =total_of_delays + delay;
  o0 X; _8 y4 G# w2 V            & F; j( Z; a, e8 B- {$ }
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;/ W2 o' B( C' {" \# b
            server_status = ST_Busy;
( R1 d' h: x  D4 U1 d  ~            : J' C' e3 I1 S
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);6 b' n8 q, d' W1 m/ z- A, r5 p1 C
        end %    if (server_status == ST_Busy) ) L6 {& t, h" Z. h8 m& u6 s( c& [% R0 ]
%%%%%%%%depart
. l4 t0 J9 H& S! @7 }  }% T6 i9 O, Y    else8 ^4 m0 \) d  a4 w' U
            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;! O! p# X0 z7 n! V+ H
            time_next_event(EV_Depart) = 1.0e+230;
1 o% @; i' d- K  _) y, R    ( S( w+ s3 _9 l, D6 W1 J9 o2 J8 Y9 j
        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 Y0 g: t5 ]- R( I# D
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;8 j6 v& F, J, L
            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));
" t! f# s8 b8 G. M/ q' d            time_arrival=tempForPop;
" |1 ]  s5 M+ x  l' F        end %if (num_in_q == 0)
6 x5 h2 U6 a. Y  h          M- ~- |- x% S: B2 `4 s$ V5 n
    end %if(next_event_type==EV_Arrive)  K8 l' E! O$ ]# U6 P( `# C* s
    6 ]9 K$ R+ ?! t9 B
end %while
5 f5 Z" t1 O* K( H+ {               

6 i+ x5 ~) o9 B8 C& _%%%%%%%%%% part 3
: w5 w9 \" y  Q* Q) O1 D, `5 R) 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);
" o" o  z7 b- v  q    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);) F* S+ J, O, J% N0 F9 e0 M1 M0 g
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);8 Q1 U" I7 \' C; q/ R; X# d7 f/ d/ b
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
9 h5 S1 u6 w0 G& C( \5 `: }    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%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

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

    fastpost qq
    收缩
    • 电话咨询

    • 04714969085

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

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

    蒙公网安备 15010502000194号

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

    GMT+8, 2026-11-23 06:02 , Processed in 0.266009 second(s), 106 queries .

    回顶部