QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

+ S6 u5 m3 Y3 d! I
* F( m# X  ?* j/ w4 X+ K$ Q
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab: q& Z) A# f) Z0 T* j
clc;clear;
ST_Idle=0;4 |# N9 Q9 Q; j5 Y6 b
ST_Busy=1;
EV_NULL=0;
5 J" ?4 S) u7 G* h+ G& u+ a8 `EV_Arrive=1;
4 e& Z2 Z& M4 }" b! X" q6 k5 YEV_Depart=2;9 i1 e+ j7 j5 z" Y/ I- ], j7 U7 [
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
  c& h& L  P1 A% num_custs_delayed=[];+ h6 u/ `$ o  s# j3 [) O  ^
% num_delays_required=[];2 d8 \* l- J: y" ^, e
% num_events=[];
2 @" g! F& C: B% num_in_q=[];8 S# F/ t! F* M6 V! ^- Z# [5 f6 Q$ M8 x
% server_status=[];* O6 G& {2 E# F& O
% area_num_in_q=[];* W& L+ V$ g# o8 T  T' l/ O
% area_server_status=[];
- ~9 V4 j  H% j% s% mean_interarrival=[];
% l1 u" Q$ [& z0 f% mean_service=[];; B+ P+ J) R- z# u! y
% sim_time=[];
7 l5 g: K$ \4 c- @" Z% time_last_event=[];, V6 h1 Q) Z) ~* j
% total_of_delays=[];
* B# k* x( s8 K( V' h% " V" d. q5 L6 i. e  O7 G  @
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
2 i% O4 A* a0 ^! A%
仿真参数4 L- H9 \0 K$ P; o1 Z: r& z
num_events=EV_LEN-1;
0 B3 |: S' e9 k- p! Y4 R$ Dmean_interarrival=1;
+ R  o. l: Q4 k; `/ o7 y! @; tmean_service=.5;1 S' r" A% p" O% H' |* F: Q# Z
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
3 G  P! P/ a* _fprintf(outfile, 'Single-server queueing system\n\n');
9 t: v+ g$ i5 J1 H" Vfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);3 w7 s) N7 L; }& f4 F7 ]' Q2 i
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
5 O+ S% x8 u8 J7 zfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part19 m' ]6 C6 s1 o+ G, {# i  E
sim_time=0.0;" z$ l0 @  g, N
%     /* Initialize the state variables. */
    server_status   = 0;%idle& g. |/ e9 t; U% h: x4 s
    num_in_q        = 0;2 N3 z5 b6 I/ o+ S, J( j
    time_last_event = 0.0;  z' z# u4 K+ G+ P0 P5 D
   
. m: O* g. b# c7 p%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
' I9 ?- W$ ^# W( H/ Z    total_of_delays    = 0.0;
( _' \4 `: F" z* Q  ?  E    area_num_in_q      = 0.0;
3 M2 P# ^3 B- n4 C. T    area_server_status = 0.0;) C: A' t! C( ~2 Y  K" j
    6 @' R0 Y2 X2 F+ v9 A
%     /* Initialize event list.  Since no customers are present, the departure3 ~9 K/ Z. w" \, K  Q& U
%        (service completion) event is eliminated from consideration. */8 P0 @: d7 w7 h% _
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);* r+ ?7 i4 q% h& N) N/ L
    time_next_event(EV_Depart) = 1.0e+230;
1 o  X4 Z4 {4 L
   
  K4 i7 O1 f! o    ' H/ e( c2 M" w
   
! R7 f+ B& _3 ]: s %%%%%%%%%%%%part2
* j! R. V5 R' K$ Lwhile (num_custs_delayed < num_delays_required)
& e  U" s4 c& t, K# O/ |* `6 c%Run the simulation while more delays are still needed.
* ~! G, n& v- q4 C- Q# d; _8 Q& \%/* Determine the next event. */
     min_time_next_event = 1.0e+290;- n8 h5 Y1 O9 f2 @3 F
     next_event_type = 0;
9 [  h6 y8 ~" @: I1 `2 D     
5 J5 P' L5 I/ W# v+ ~# p%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
1 c7 E$ J' ]' P9 ]) f+ v        if (time_next_event(i) < min_time_next_event) # V% o7 D: L- t# U% q4 ^
            min_time_next_event = time_next_event(i);/ Q+ Q) X7 Z. I* [
            next_event_type     = i;: J% r" x+ X  Z4 Y
        end
9 Y1 P' C1 W5 M" t) O8 q) Q# L    end( N; _, T/ A  ?! i
   
/ s% y. W0 F+ J3 Z! d/ `' s$ p%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) # q, ?, k/ J+ K* p% e* C6 }& [' _' t
        ' o# w+ e" `' b. k
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);5 h% v6 u  `, n) T5 v
        exit(1);( x# p. K3 Y8 P1 G$ t& F( [2 z
    end/ W% B' M/ o7 O+ t8 ]
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
! d( b5 y# ?8 f+ Q ( A! U( s5 J" X# E) ^; M( A% }$ N* s
    ) s5 Y$ e# N4 D1 _- 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;
; M* |1 v6 z6 G( ^4 H, d    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;
% d+ y' p4 D0 `# l  b8 Z   
% }9 q5 H  C9 X# V3 t  u+ G. v( W% L. n%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
+ q( R0 U# |. \1 U' n6 T% [        double delay;2 }3 Z2 c% J1 L; j
        ! Q+ j' ]; v1 K6 {2 Q$ u
%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) . f0 o9 E, D2 N5 H
            
" g0 m/ v" k$ ~2 v7 L%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;' I1 _. m. ?- l6 D
            3 L3 L. n& Y' ]0 g/ b9 E+ J
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 : S* N! ~2 c: y4 X1 s- r
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');  ^2 S) _- t( F+ ~1 u* w: _2 z' U
                fprintf(outfile, ' time %f', sim_time);
% [+ X8 \! K. X& L" Y3 {                exit(2);
0 r0 U2 P$ G/ C/ q9 p. c7 B2 w            end. |+ u* s; z3 i* P9 P8 ]
%/* 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
+ }* u% L9 W. v7 q1 k+ p%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
% i- `9 o2 s) K2 ?5 y%and do not affect the results of the simulation.) */
            delay = 0.0;! S7 T9 O! X5 r( V7 [+ e
            total_of_delays =total_of_delays + delay;% ^9 `$ T! B' {5 W& c2 w0 Y' O
            ; U5 Y( E! w9 m4 i1 E
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;/ x: ~0 r5 N8 l' x0 L/ o
            server_status = ST_Busy;
, B- s6 S9 k& M: n7 y* {2 Y8 z! j6 V            ! V$ p8 d- F9 s" @9 `$ f
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
8 R) j4 R2 k! v) B' A        end %    if (server_status == ST_Busy) , K" V$ b' k& `0 M5 ?. {
%%%%%%%%depart# v1 J! y# @* g& S$ f. W  J
    else
( c) m% K+ v8 P; 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;! Z( c2 S' l% T
            time_next_event(EV_Depart) = 1.0e+230;  {  H* z. z& r( L
    : L+ P/ o! |/ N" {/ V+ ]6 s- ^0 ~
        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);, _$ R/ ?! X  c7 D$ i0 K/ x4 w
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;6 ?/ c( }  p: o, u# j
            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));
+ O  l  T" j. [& J' G            time_arrival=tempForPop;
1 A! i5 y) U/ J& D) ~- v        end %if (num_in_q == 0)1 R, }- A0 e! a2 A) V0 H3 T
        
: O3 F& D1 H$ E% }1 m0 H5 V! O    end %if(next_event_type==EV_Arrive)
) N. T: _  l$ R% P; P    ) }4 O7 }" f. }# d2 P
end %while
) U/ J3 x7 x: D* ?               
# V  d' c; S/ Z7 I2 ?" i5 ?( Q
%%%%%%%%%% part 35 |* D, Z2 T8 T( i5 w" 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);4 e# @( x7 a7 I4 R! i# z1 r0 H8 s
    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);* r: U+ L1 _1 c% V; G$ [% |
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
; O' D9 `6 P2 K3 _' s8 |- C& S" d    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);  Y% ~8 Z5 c/ B  h
    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-7-9 18:19 , Processed in 0.526105 second(s), 106 queries .

    回顶部