QQ登录

只需要一步,快速开始

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

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

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

11

主题

6

听众

51

积分

升级  48.42%

该用户从未签到

新人进步奖

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

8 g* }* \7 a, {* d
" }  U( R2 @; ]0 I
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
; L( k8 x1 }% Q. O4 ^clc;clear;
ST_Idle=0;+ |# r9 W5 k( m/ S
ST_Busy=1;
EV_NULL=0;
; O: z- s# {( w! }) ~* v+ AEV_Arrive=1;
' g. O# m- ]  qEV_Depart=2;
8 t* m$ y) ], A' TEV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
+ V+ }2 ^& A- L( X( h% num_custs_delayed=[];# |9 @  c/ v7 z1 g+ T' G
% num_delays_required=[];0 @: y/ _$ y4 @2 E+ N4 B5 q" D
% num_events=[];% Y) O/ M  n" V+ X1 p
% num_in_q=[];
# r1 U- B* z/ h% server_status=[];
  Z- d& [- R, d. Z% f7 a% area_num_in_q=[];
. p8 P! k; f) [: o, f: M( z. x  H$ ~9 R% area_server_status=[];% ], A1 V* F0 F+ Y  ~. V; L  u
% mean_interarrival=[];
4 h0 c& _! }. J. l3 d' l2 h8 E% mean_service=[];, O5 R2 A8 U" k( q) O5 J7 ]
% sim_time=[];4 ~/ s3 w+ ?! F
% time_last_event=[];
) O! q3 Z, k4 ~; O0 O6 }. W& T1 J" _% total_of_delays=[];
% H: x6 |( O1 q) `% 6 t( D3 Z( A  h! q5 w
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);) O/ E) x( Z6 w4 T# H" x2 u
%
仿真参数
. O# t5 W7 |2 F, \) [num_events=EV_LEN-1;0 U: m* V* J' K5 V  M
mean_interarrival=1;
3 s: O2 V2 _% O( m# Nmean_service=.5;3 `. f* D% r+ c0 |; i3 _* S" ~
num_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
* m- Z. u+ L0 H6 J8 \& i  H, jfprintf(outfile, 'Single-server queueing system\n\n');
% P9 p7 _/ F2 d5 @5 qfprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);% j3 a% n1 W# W% e
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);* S/ L0 C7 H8 R0 h" \! l
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1) ]8 c: O8 i: x7 c! |
sim_time=0.0;1 _+ d- ?) H, F& a' P7 c
%     /* Initialize the state variables. */
    server_status   = 0;%idle1 D2 b5 A0 E; S, J& ?# L' x
    num_in_q        = 0;
0 W4 U1 k1 }; a, |- D8 E$ {, ?; o( \6 d$ ^    time_last_event = 0.0;
8 r# G0 m' |$ ]9 A9 G    + u) A+ q6 D! A3 f* W
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;4 T2 S( W3 a  G! V& B  ?) I0 ^! g
    total_of_delays    = 0.0;1 ?  D* e1 H0 u. E- ]: M
    area_num_in_q      = 0.0;
9 U, h  g2 j. C* v    area_server_status = 0.0;. A* P+ Y8 X/ d  M, |0 S$ I6 M
    6 q8 J% y4 L0 G2 z; D/ {7 b- ~
%     /* Initialize event list.  Since no customers are present, the departure
- g; D0 @0 C0 W, w%        (service completion) event is eliminated from consideration. */' q' m4 r! ^- ^
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);" g/ P5 y7 Y9 t, J* a& D' a% }0 y
    time_next_event(EV_Depart) = 1.0e+230;
2 ]$ S& a3 [. e
    / G1 ^+ Q* I* w3 l
    & ^6 T# p% d; Q, q; ]6 N
   
+ }2 x+ H$ m) S  Y %%%%%%%%%%%%part2  A& w, q7 z! ^1 H. Q
while (num_custs_delayed < num_delays_required)
3 e0 _% t( Z4 D" d& f%Run the simulation while more delays are still needed.
3 b5 M$ c' v" r* `%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
( H& ]0 d8 m) S4 W9 p  S     next_event_type = 0;3 W: P+ E$ T5 Y; l
     
& y: |3 n+ {0 m- X9 u' ?, w%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
  F% ^+ `% J& U% Q6 z. R  x7 i        if (time_next_event(i) < min_time_next_event)
; |+ L- e7 o6 Y- |            min_time_next_event = time_next_event(i);* m0 ^6 r9 f' y7 a% t0 f
            next_event_type     = i;8 _- `& P* K3 u" [, ^
        end
* \9 y" [" A- F$ n    end
  u0 i  |# @4 e5 u5 L( y   
; T0 h" B5 Z% n/ o  Q1 Z" A( I& }  I%/* Check to see whether the event list is empty. */
    if (next_event_type == 0)
8 ^- ]1 G: [1 e5 N0 i' r        ; {( i+ X* L& W2 k
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);1 P% B  m/ m9 d. G$ o
        exit(1);
# \% E+ H) ?2 c0 k) L( e    end
" l7 m* [: P% K6 L* q$ z%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;' Q- [+ z4 V- k$ w
' z: x9 e: d$ N6 l- h! |0 G* Z
   
$ `# q2 n8 k7 c  w) F6 H%/* 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;* d- A+ c, j% \5 E3 W* d2 U$ Q
    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, {' R" P( v$ r( g9 A8 G
   
. _; W/ u4 I( [1 Y3 o+ O%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)* @+ K( |0 l. A- e8 f
        double delay;& q, i; f( N8 k
        
9 }7 \" o; i8 e7 Z%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy)
. K6 f5 }3 \% C' H+ n& G            * y1 c: ~9 q. m. |' s) U$ r: W
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
% z2 j% }+ B$ g- Q$ J' i            % u5 b! ^8 d* D9 k; J
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
/ k- d1 _% t  r/ }7 b%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');: d6 @% m7 r0 g
                fprintf(outfile, ' time %f', sim_time);
5 a. t6 K1 T4 S- T. b  Z                exit(2);
* T$ q2 C  ?8 J+ F2 J            end4 v  K& @' }( ?9 V( [
%/* 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 _" ?$ l' v$ s3 o( [' L: g& N%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
. [& P* A! F5 o2 [7 c/ X%and do not affect the results of the simulation.) */
            delay = 0.0;
* x( R! d! ], N5 @6 r            total_of_delays =total_of_delays + delay;$ R: L" U' Y5 i9 v5 o
            
( ^4 h# j8 [8 r' s%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;$ y4 E& Z! p6 ~# D( O- m# ~5 s
            server_status = ST_Busy;& A. c* {$ o1 X! x
            
9 E0 V8 g) ^) |# p, w8 _%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
" Q/ T4 |2 b% V( p        end %    if (server_status == ST_Busy)
) ^, t% }8 |! w% W, P%%%%%%%%depart
! |# K- i" \" B8 z3 Y+ t$ t# _    else6 A2 _5 r1 X' M, _/ A$ m! E6 V
            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;
& m6 @3 g" G% A8 t% ]$ ~            time_next_event(EV_Depart) = 1.0e+230;
' ~  k- d2 _2 J/ F8 q* ?    * k# m) {/ J2 ^* C, o  m' 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);
; ]4 ?( L$ T4 h            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;8 x9 U7 ?. s! Z4 E
            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));9 M* }- _* r# H; n( s) O
            time_arrival=tempForPop;+ F2 o4 ]/ i3 Q2 z  b0 ~
        end %if (num_in_q == 0)
8 b9 I& C+ k5 _# t% C* h; e        ( l- z5 \1 v. Q8 ^% O& [
    end %if(next_event_type==EV_Arrive): q/ b2 I5 B9 u% x3 T9 J
   
. G6 q; c6 \& I" H, |0 Qend %while+ o( s8 j: A5 q2 H
               

( t; g/ b5 {0 U%%%%%%%%%% part 3" U" n( e0 }+ v) 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);0 c2 x: @! w. d. b3 f8 k
    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);3 w2 u/ F/ j& F: i( `" G9 G; c
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
$ |7 w% H8 v' l" q7 h8 w    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
7 Z& ~6 [6 @0 F8 V    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-8-3 20:36 , Processed in 0.523341 second(s), 106 queries .

    回顶部