QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

! @. u  b% K3 x
% z2 G9 F: J  B( r# L! k5 N: h0 s
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab1 C+ P% K/ w7 g" B4 D3 Z" D) F: g
clc;clear;
ST_Idle=0;- z2 @/ k3 t- W7 p2 f/ [
ST_Busy=1;
EV_NULL=0;
" {) Z2 R+ u% [; I$ UEV_Arrive=1;
6 _6 Y% q( b9 o' [. YEV_Depart=2;
1 u# F! r. C1 f/ X+ qEV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];7 V$ ?5 f! S8 T) }" H; h9 ~
% num_custs_delayed=[];
: f  D$ M( q# \% Z7 p# E% num_delays_required=[];
1 t$ Z, j# {# G5 B% num_events=[];( D" \% x0 K* k* d. U9 |3 {
% num_in_q=[];
; O% [7 u) a9 @8 i& z% server_status=[];
- {$ n9 a9 A; s' O. l4 r$ ]% area_num_in_q=[];
9 f' }5 x1 z+ n% area_server_status=[];8 Y/ d" D9 x( R; E3 I8 B
% mean_interarrival=[];: g/ W! c7 G% x1 S) t7 o
% mean_service=[];
) {+ O/ I$ w% A! `6 y% sim_time=[];
8 r+ Q( P; e  T6 u% time_last_event=[];
8 [( M9 S: m! m' ^- y# [+ N, z) j% total_of_delays=[];
  j) X. a. K2 D  R& q% , S4 ~3 D1 ?: }. w
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
! Q- l  X. x4 A%
仿真参数
# V6 z* F8 `; c- P* q4 r! B0 lnum_events=EV_LEN-1;
  Y& e3 v- _9 jmean_interarrival=1;
+ ~& T) D1 d6 s' Cmean_service=.5;6 N! @' B  j& e
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
* @2 Y: d7 W1 [3 A$ zfprintf(outfile, 'Single-server queueing system\n\n');
8 \; Z' h( L$ V5 ffprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);; d* t! X/ ]7 w, M: h7 N# o/ w
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);$ m: ?, Y2 m. |+ T% Y
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1  r3 m* X( N+ ^0 C( o' H  E9 e
sim_time=0.0;8 E* i  P6 Q# X$ F3 v
%     /* Initialize the state variables. */
    server_status   = 0;%idle) }+ j* O) G* y4 p$ p
    num_in_q        = 0;$ K: {# Z, U6 I, h& P0 L  H- o
    time_last_event = 0.0;& m" K( ?4 p5 @/ D, w
    ; I3 ~0 k0 s* V8 @" O! e1 S* ^
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
  r& `! [8 L7 T3 w2 D    total_of_delays    = 0.0;- K9 q: E+ Q" Y. E
    area_num_in_q      = 0.0;
4 \  d4 q: s! y& O; f  W* i* t3 q    area_server_status = 0.0;8 [% s$ S  z0 V8 R* R
    # W! [3 \! u8 p- i; w
%     /* Initialize event list.  Since no customers are present, the departure
# f) d& P# s) P( J%        (service completion) event is eliminated from consideration. */
1 f* S$ @% `9 d- D+ L    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);, e$ E* k5 d! |" E; q8 t
    time_next_event(EV_Depart) = 1.0e+230;

( ]4 L6 ]2 Y- D  y    . m9 T/ X. u3 a
   
" c- |& O* ~$ B   
) J9 B$ F( h" B* \ %%%%%%%%%%%%part2, l  V& a3 A* T7 z) t6 q3 |- T
while (num_custs_delayed < num_delays_required)# l$ J0 l  Q8 ^6 Y; S9 Z* v! ^+ O
%Run the simulation while more delays are still needed." J  \: X$ D7 }- E9 ^8 U7 M3 F; p
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;2 W* e7 t, I% Y/ R( U) T
     next_event_type = 0;
. |5 r# ?% f& A9 [2 i  n8 O     : |" \# f; ~# ^" c& P
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
1 P7 r- i9 ?; z( N2 G# @9 h        if (time_next_event(i) < min_time_next_event)
9 M/ N: k# V6 Q" h/ }            min_time_next_event = time_next_event(i);
+ [" E" j( \) {: e+ M  p  ^9 e, o$ {% U            next_event_type     = i;9 `- }5 q) ?8 ~: a; F) N
        end
. k* d- _1 M; C" K* s  p    end
1 X# @1 i! L) x) R- y    - }8 w: p- D2 R; Z* N. n
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) % g$ C$ Q" k( w3 ]) n5 k
        
$ }% K0 G+ _; i2 b%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);+ \0 g5 R' n& _
        exit(1);
' y& r# ~1 d+ @7 b/ {6 a* p  X! V8 o    end
0 I) z2 P! O" o! q%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
! Y7 N: p8 b# j; @) s) b4 I- W) C+ T ' D8 ?3 c  I9 O' F/ }
   
# L3 D0 k; j' q% s6 O9 j/ d1 {+ A%/* 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;
5 l0 W  t' p! J/ 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;
) z& j8 M5 d0 s- ?. _; ]* F5 {* @9 y2 l    2 l& \1 \1 H; }' r. H" O& \
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
) Z  p: o/ L8 s6 w. p, i        double delay;
9 T+ S6 |  P' l6 a( n2 `        
% A! A8 m+ G$ j' 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)
' ?) J: f" u* @/ K! S/ Q            2 n# [- @* W8 {3 E8 _7 \  m7 B1 y
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;& C, V6 d8 s5 _
            ( j6 Z' O2 v, a+ r. h! L
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 : T! H" U; O, f& |9 m; ]) C
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');" @: \' I7 V! q" ~0 m; m
                fprintf(outfile, ' time %f', sim_time);
% @" v- U$ `2 g- Q" x3 B0 ^* o% L                exit(2);
0 N1 ^7 Z! H' r: r( e3 f$ f            end
( G4 U7 R" {- i2 d5 W8 z%/* 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
4 {1 I7 G3 D' ~- y& i* j3 T%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
) J+ Z& s- s! r%and do not affect the results of the simulation.) */
            delay = 0.0;% N. g( F" P, j* \: y6 q5 a
            total_of_delays =total_of_delays + delay;& f- ~1 K, e! k# k# E  i
            
( R  n1 o& O* S& {%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
% I- c, S8 h1 ?8 c% [! |            server_status = ST_Busy;
. {+ r% w! e+ e, j            
* I0 W4 D$ `  I: h%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
) X( N- O1 Q" j4 @* T- q        end %    if (server_status == ST_Busy)
- L" |8 L6 ?% h2 K! l$ f%%%%%%%%depart
1 c! H/ v$ Q+ m6 Q$ I, v" R    else9 }  B8 R6 X! V- F
            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;# p$ r! o" ^" e1 u
            time_next_event(EV_Depart) = 1.0e+230;
$ S: s0 _/ a- q5 F- }3 y    * I7 ^* p6 K4 ~) j$ v$ c
        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);
! d' z! S& ~. P1 L            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;
" r# F9 D; ^: V( w            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));
$ q2 ]7 ?4 [  `+ M            time_arrival=tempForPop;
, A9 U# N3 y) O( q        end %if (num_in_q == 0); T' ~/ _" R" Q( e
        
0 r: p3 l, I9 O; o( t1 c    end %if(next_event_type==EV_Arrive)0 f+ g0 i' b* d/ ~* w  A0 d! O
   
2 C+ g& z" Z* Cend %while
. s6 l9 G7 w7 L2 S/ E               
1 u( q/ @* }  G" `- ?' [
%%%%%%%%%% part 3
  u* }2 y! V4 H0 M9 v%/* 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);6 |8 H! |! f9 J; Q) K
    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
4 w2 U$ L! S2 O* q    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);, B+ m- L$ G! t- G* F- t" X+ P" Q
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);9 N9 g$ ]' Y5 T2 p* z  w" _
    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-4-11 11:22 , Processed in 0.481355 second(s), 106 queries .

    回顶部