QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

7 U4 D% K- o! n$ B3 X5 v" c$ z  A' j8 d% G2 J; \
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
& A( k' @! O' j' \$ M- K- X! zclc;clear;
ST_Idle=0;8 M/ D' z2 o: e+ l5 o3 j+ H: F8 g
ST_Busy=1;
EV_NULL=0;5 H  w+ X; i7 [. @
EV_Arrive=1;
; s: p" r: j& p' U8 ~EV_Depart=2;' v7 Z- Q& t6 F: ~& ?0 \! Q  v: l# O8 E
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];+ |% h, ?% i- P/ A+ O/ B
% num_custs_delayed=[];
' E( \' m6 }7 o% num_delays_required=[];# K; W1 k; X8 I& h; e0 b! m
% num_events=[];
; @7 x7 u9 s3 p' x2 @+ D% num_in_q=[];  w* T# \/ T# r; c' r
% server_status=[];
3 f2 h! \+ T( Z/ i  z( R3 Q+ m% area_num_in_q=[];
( r: m' f5 @, {% area_server_status=[];4 w( J, g  h; A& T
% mean_interarrival=[];& l3 o/ l  b/ O! R& n' [, d
% mean_service=[];: b4 \( R! m" N. U
% sim_time=[];' ]( k; U( H8 B' G
% time_last_event=[];
  Q3 u) j) R+ X  P* U% total_of_delays=[];
1 q8 }5 z) `5 M* T, ?%
- t' @  Q1 u$ d% i" |time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
: D$ w, t6 F  P2 z% T%
仿真参数
; q6 m: _5 l+ v3 u2 s$ ]num_events=EV_LEN-1;7 V) W1 H. J2 {' T
mean_interarrival=1;9 V0 J; x3 R6 x2 s- L+ H
mean_service=.5;4 R& Y! v& O, B$ j8 x/ z; K9 i* A
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
6 S. T8 H, x! \fprintf(outfile, 'Single-server queueing system\n\n');# x, ?& h" d: f; H4 m
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
5 j5 i4 u4 z+ P: d2 B1 Tfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);
5 z# `4 D. q" d/ R4 A, hfprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1! C) B& {" W  u; y! f9 ^$ `3 t2 r% k
sim_time=0.0;3 d0 E& o! b2 o
%     /* Initialize the state variables. */
    server_status   = 0;%idle
5 r: o; L0 G: v+ c8 |) ]4 H    num_in_q        = 0;8 f& x, k6 e* U+ O2 B$ k3 I
    time_last_event = 0.0;* ]) c5 i! r: Y, v
    % r1 k- W0 N# X3 l& q( B: d: m9 G# j
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;8 W! N- t# ]8 _) k0 V) \
    total_of_delays    = 0.0;6 [$ N! |7 Z6 U- i( j
    area_num_in_q      = 0.0;" _  M# g, m" ~- V8 ~
    area_server_status = 0.0;
! x5 i4 |2 s* _, I* c3 u    9 Q% b- |. Z2 B7 L1 x
%     /* Initialize event list.  Since no customers are present, the departure
2 U9 B% I1 S$ x3 C% b( J%        (service completion) event is eliminated from consideration. */$ [3 P" R8 i& H/ T  z% p' f" I
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);; x2 C( |9 M+ p- l' ~8 d7 ]  a
    time_next_event(EV_Depart) = 1.0e+230;

. B6 ?" Q$ C& y* X2 @/ P2 \   
" F5 a4 R1 u6 Q! V  q0 ~; `   
" n+ ], N0 E; z9 B6 t/ B6 r    ' k6 W+ Z5 K# k. p* D
%%%%%%%%%%%%part2
( g% h; \% D6 P3 ^. U) bwhile (num_custs_delayed < num_delays_required)
( k  O. }* S, ]- a& D1 r%Run the simulation while more delays are still needed.
, l+ ?( C; z# w%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
3 j8 ~8 ^1 z9 s) I' a     next_event_type = 0;
6 D, O! m/ D, S     ' y6 n% p$ H5 J
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
& C; Z( v; M; B6 E        if (time_next_event(i) < min_time_next_event) & y) U# C3 o2 O: |# P- l, ]
            min_time_next_event = time_next_event(i);# i3 @9 u5 z$ [' m
            next_event_type     = i;
: }; q( R  A& N1 n% v        end
- Q8 R- B1 @+ y0 m; e    end
: ~$ Z7 d% `" E) L+ W1 X" K- d, a   
8 ~1 A  G. V2 A( N9 m%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
/ {& D, z6 B. A! B' g4 p! I        
5 s( I5 p) B8 D% y%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
' @9 A$ h1 G1 @4 ^  |        exit(1);3 k6 P0 i  N) }3 ]3 ~
    end
5 z: \! e7 O6 |%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
& C& f4 @+ d! q   r& g8 V( j, {: b1 p
   
# h8 `: B& f: F- o" t, F%/* 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;
& I4 `/ ~  `" `) S    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;! m( j! ?, `' ^0 l3 q
    1 r4 ~, x, {- U: ^
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
( e; D$ F% `; A" q: a  K        double delay;+ d! |4 ]  S9 O: S- a
        
4 @+ G& P8 Z6 G+ Q%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
. K0 Q7 @5 z/ y3 ~# b            
0 D* E5 V/ K4 x1 R6 g%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
0 O3 s8 G7 d  i6 l/ L            ) `0 A9 N5 d/ N: `
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
$ W! u7 v# h3 D' {%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');
% j. K- {9 b1 T+ ^2 V' l' l4 {                fprintf(outfile, ' time %f', sim_time);& h- f9 X, l' Q% e* @
                exit(2);
/ s6 w& f$ ]0 W3 ^. x) f+ Z, n  X            end
; U6 j; T( s/ s) Q$ l7 T: Y# b%/* 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
+ Y/ w- F) S7 [! i$ H%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity $ _3 i. N) @; m5 q
%and do not affect the results of the simulation.) */
            delay = 0.0;+ \& y5 d6 F# X! C
            total_of_delays =total_of_delays + delay;2 a; O$ E  r4 P. B; }' @
            
; C9 G; c* l3 w0 [1 l5 o%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
5 T1 ~5 k* @6 X7 p            server_status = ST_Busy;1 V- I0 m+ Q, f5 P7 Z% t) G* g/ p
            : \, y% [! I. Q4 d. T* f( z
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);" z' ]. R5 k! K8 Z+ _% Z7 c0 W
        end %    if (server_status == ST_Busy)
2 }% _  v; r% P! {. A1 Y%%%%%%%%depart
( |- b+ K  J8 c8 w0 [    else
; o3 k8 W, B/ W# X            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;" ?) }" }0 }6 }8 {' ]& b
            time_next_event(EV_Depart) = 1.0e+230;% G  {3 D9 M# h; t1 b
   
. Y* s2 |2 [8 m- }        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);8 k9 G$ e: @' N# G
            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;" ]' ?0 V: x* 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));7 F6 I% h9 d& w! P0 T& ^; ]
            time_arrival=tempForPop;2 p8 E5 L7 j3 B* {* w
        end %if (num_in_q == 0)1 F4 k! g8 Q$ T( h- E4 z9 i) e0 I, _
        
+ K7 J8 _/ N- `9 T# O9 N2 ^    end %if(next_event_type==EV_Arrive)
& i/ l( G4 ~6 |3 }* [$ T6 p    4 p/ k; @' Z+ d
end %while) D# w; e" E- h5 K; V
               
3 |& s- u) f# @4 z2 ]  R$ T& f
%%%%%%%%%% part 30 q; v' {2 S4 k9 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);
3 K; l" [5 j- s4 @    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
) r- ~3 i/ s8 c/ r1 k    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);* ^9 E, x6 u3 a3 @& h8 _+ a& J
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);- }, E' m3 z, s% C* M, Q  z7 l" t
    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-10-9 09:05 , Processed in 1.531568 second(s), 106 queries .

    回顶部