QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

4 r& ]) h  I2 T+ c& J6 f. V7 W  [6 E) _6 f, R9 z" W3 A& E, q
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
, C' B' t2 B7 z% b- Fclc;clear;
ST_Idle=0;
' ]' g% y& g' u/ ~1 T$ y$ HST_Busy=1;
EV_NULL=0;
. V* H1 k" D+ k, P+ dEV_Arrive=1;
6 `- K+ ?# |* u% K* e. e6 tEV_Depart=2;
8 z' T- J: |! r4 y6 `( e$ ^EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
" R9 Q& m* N* g. J) z' X% num_custs_delayed=[];1 }  `/ ~) y( c/ n# u- F
% num_delays_required=[];+ y5 u" s- {; @
% num_events=[];' D4 f2 i- U0 u- E4 z$ C
% num_in_q=[];, p2 N( b, a. X0 `3 g* o
% server_status=[];
: s: s$ Q$ `% X0 |0 x6 k* c% area_num_in_q=[];: e/ B4 ~, @/ t' b' a3 G+ v) m
% area_server_status=[];% A4 d( R5 A0 U2 x, r
% mean_interarrival=[];
% t0 p6 w! U3 g/ I0 {: G& z# M; L% A% mean_service=[];( b8 [: }2 q0 \1 k& b% z2 [3 A
% sim_time=[];
$ e# v4 U2 Z/ S/ Z# E* ?; j/ v, J% time_last_event=[];! B9 r; x/ m& p  L; M3 [8 A
% total_of_delays=[];
- c- T5 t3 m3 V: V% 2 r2 \# Y& p) Q! \/ L5 j" {( t
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
( B1 R+ v0 N% x, _%
仿真参数
. S+ _& e" ?4 U* onum_events=EV_LEN-1;
  F7 a2 B* `& R8 [& j: j5 v, Y  t- Imean_interarrival=1;/ L+ K. {; w; C6 h
mean_service=.5;
! f+ V' |  H; `3 j- M- U. pnum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');- w- m! H+ _/ B( H+ \
fprintf(outfile, 'Single-server queueing system\n\n');
( l( w0 G+ {; B5 f/ o! Wfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
- a7 U; e$ K! \$ U$ B+ ~fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
, G# l7 y$ L) P* xfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
# m, J; z/ F. S# L$ vsim_time=0.0;
: F. t6 W, i8 a! b/ ^" [%     /* Initialize the state variables. */
    server_status   = 0;%idle- \, _. ?7 C9 g8 ?& \4 @0 u
    num_in_q        = 0;
7 [* c0 c; s7 Y: G6 _    time_last_event = 0.0;
# j4 c+ S  x- }  q$ e0 K' q5 X   
, W" Q' K8 |7 p3 s2 U2 M%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
0 s+ i+ W7 E8 J( K( ^2 T& \& o    total_of_delays    = 0.0;
6 i' R! b) N6 H7 d( O+ s% P' G    area_num_in_q      = 0.0;
$ Z4 m' [' g/ I! M: b* X% q+ Z' h    area_server_status = 0.0;
8 {0 j! G- R. K   
/ e2 N5 W; M2 w%     /* Initialize event list.  Since no customers are present, the departure
  x1 U/ ]+ V9 m6 i/ R$ D6 g; ~# r%        (service completion) event is eliminated from consideration. */
. k" I& `) z0 [2 M' a! x( x0 m    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
) ]/ w& Q- [: x1 t' L    time_next_event(EV_Depart) = 1.0e+230;

: t9 O) D# z  D  i8 F9 {   
2 ^$ }  R# W8 g5 W% h! }7 p0 x' s    ! r( c7 I% e+ `! U7 \& \1 p6 R
    9 @. \2 s+ [3 n0 O- K
%%%%%%%%%%%%part2; f  _+ Y! f6 o2 P( X
while (num_custs_delayed < num_delays_required)
2 B- p+ u$ x, N1 E) q4 d# K2 l%Run the simulation while more delays are still needed.
' H: Y3 j( B* O1 U+ x+ @; x9 a%/* Determine the next event. */
     min_time_next_event = 1.0e+290;. }  Q7 k$ ^% O8 K
     next_event_type = 0;4 s7 L% ]5 x1 b6 i3 v
     1 N; |0 N, u( o% F! A5 d
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
% [8 ^% g$ _/ F5 K" ^) n: u        if (time_next_event(i) < min_time_next_event)
6 {) m5 \% f4 I" @            min_time_next_event = time_next_event(i);
, |2 |0 {: }( O: t            next_event_type     = i;
% v; r, P, D/ d" _; C; i# t        end
5 J: w0 e0 S3 R* k6 l    end
2 p/ s. f! Z' c$ N    , ?  I; W5 s+ P7 [$ M
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
3 c8 |- i6 y, |1 ^6 f        ! B2 q5 c% {/ b. \0 \( B9 p' M8 P
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
; L- _8 m' Z: {" `: X0 f, h        exit(1);: D8 O) h8 H$ R' Z2 p/ m
    end
; m! y) e3 F) o# ?* M%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;* v7 ~6 F3 o6 X8 Y
) T5 E1 P/ P* t* o
    8 t. b* \$ a. u5 L2 V( e. z
%/* 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;4 ?! k0 u5 `- p' b3 t1 o6 V7 L
    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;. n5 y5 ]* R" M
    / w3 _' r' o5 {8 t
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)+ v6 q# q+ d2 V) ~8 {
        double delay;. S# X4 I) a7 \& j6 A/ g$ w, a# S
        
( E  B' H6 U# k+ W" g3 b%/* 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 P  ^# H; m# I; p8 P3 z" N
            
0 {' c% Q9 m) N8 A; q2 ^6 ~+ O%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;! @( u! w$ x& `3 R; O
            0 G! ~9 E# g/ G
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
% |- _% x# w: o% I) Y%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
9 C8 N# a$ O' H6 k                fprintf(outfile, ' time %f', sim_time);
6 t. ~5 F6 b% r( a; }                exit(2);. v. K+ L2 F5 M: y
            end9 e# O. w! X! z) s7 t
%/* 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 E; f( U; J- t6 }% b%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity 8 Q9 w2 J- f- R9 e* R& `
%and do not affect the results of the simulation.) */
            delay = 0.0;
" N! J- c. E0 ^+ v; {2 d2 a            total_of_delays =total_of_delays + delay;
0 Z/ R* d* Q9 E) ]$ Z0 ^            
4 v7 [+ P+ v5 j- Q: k; O  c3 m3 s%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;' y8 ^* R* P6 x; p% w/ U# G
            server_status = ST_Busy;9 c# F2 R  z4 G! w4 r: F
            
* s% S. q0 s/ {% W* o2 i%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);" n4 n& p; j& I# ]9 V
        end %    if (server_status == ST_Busy) . X/ m  k. q. z  R
%%%%%%%%depart5 x) u, P; G7 V0 e9 t& u% c
    else) Z3 `0 ]3 r5 I' e) d8 c
            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;
. c; \  G# m1 K+ c: E1 c, |            time_next_event(EV_Depart) = 1.0e+230;
8 [! b% T3 o" {( S0 X3 {   
- x% l. ]) O, V6 Q% y        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);
  X/ _( l) C8 R6 v4 d            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;, r$ t. U6 T' K; y; U9 u
            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));+ R$ G" f% W  Y1 U, z! o- c
            time_arrival=tempForPop;
% }( ]; q1 ]# K* I  _7 S        end %if (num_in_q == 0)
( q. D4 b5 ?9 A3 y& Z        
2 h9 H2 a$ a6 E- g# Z2 T6 d( Q    end %if(next_event_type==EV_Arrive)
. V! R/ l- f# j, g, i; C   
. t8 l. u1 N3 Z5 i4 Oend %while
' R9 z+ H' W/ S2 r               

9 U. R- ^0 C3 A" k' g%%%%%%%%%% part 3
; v( b4 L4 p, i# }0 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$ E) l0 b* F( Y: {    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);; G/ j; x" ^# b' J5 j7 U0 b% }3 c6 B9 d
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
" R( X2 s: p4 r$ W) g    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);4 ~( S8 {& {. c, T) M: t) h( [
    fclose(outfile);
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
chuan0810        

0

主题

4

听众

12

积分

升级  7.37%

该用户从未签到

新人进步奖

回复

使用道具 举报

jamesgeng        

0

主题

4

听众

6

积分

升级  1.05%

该用户从未签到

新人进步奖

回复

使用道具 举报

GunBreaK        

0

主题

4

听众

32

积分

升级  28.42%

  • TA的每日心情
    奋斗
    2014-5-29 09:00
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    新人进步奖

    回复

    使用道具 举报

    3

    主题

    3

    听众

    41

    积分

    升级  37.89%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

    2

    主题

    2

    听众

    406

    积分

    升级  35.33%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

    2

    主题

    4

    听众

    253

    积分

    升级  76.5%

    该用户从未签到

    群组数学建模

    群组数学趣味、游戏、IQ等

    回复

    使用道具 举报

    leeeff        

    0

    主题

    3

    听众

    85

    积分

    升级  84.21%

    该用户从未签到

    回复

    使用道具 举报

    2

    主题

    5

    听众

    152

    积分

    升级  26%

    该用户从未签到

    回复

    使用道具 举报

    rockhuman        

    2

    主题

    4

    听众

    415

    积分

    升级  38.33%

    该用户从未签到

    新人进步奖

    回复

    使用道具 举报

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

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

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

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

    蒙公网安备 15010502000194号

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

    GMT+8, 2026-6-18 19:24 , Processed in 0.453055 second(s), 107 queries .

    回顶部