数学建模社区-数学中国

标题: M/M/1排队系统性能仿真代码(MATLAB) [打印本页]

作者: qlau2007    时间: 2009-8-12 13:02
标题: M/M/1排队系统性能仿真代码(MATLAB)

( U' n( X% z$ _( X
+ L& T/ c, J2 x) n/ y
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab& f) U6 g5 |$ b, s* f+ L* `, A
clc;clear;
ST_Idle=0;4 {& \, o+ @# L; a- E5 ]; s0 Z( p
ST_Busy=1;
EV_NULL=0;
6 n% F# ], l. o7 {2 Q+ K% Y, oEV_Arrive=1;/ G. @8 ?9 P1 V
EV_Depart=2;; ]. X( x) ?8 B" d4 T
EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];" S; F) O( Z' F0 W
% num_custs_delayed=[];# r3 s1 i$ Q: F6 C7 x$ P
% num_delays_required=[];, ^. Z6 J, ?) |( q& z. i( }8 }, ^
% num_events=[];
$ w  H0 F/ u6 g! \. @" T% num_in_q=[];6 v) d2 Y# m4 H! I: J$ c0 {
% server_status=[];8 _& E/ K/ a7 j8 y* W  F- G
% area_num_in_q=[];/ ?* w4 m; A* O1 S
% area_server_status=[];# ^/ ^! k8 o" E5 c" i
% mean_interarrival=[];$ @. L3 u6 ]/ g& m
% mean_service=[];
8 s3 X: {. |& e) e. {% sim_time=[];/ Q; [+ n& W. q5 q( V( L, i# Q
% time_last_event=[];
$ Z$ e: N' V: o7 z+ F+ O/ V% total_of_delays=[];3 S  X3 N" ]( e6 l; c( Z3 ~; }
% 4 S: \0 R; f6 S+ m
time_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
: b, _+ @" Y, _. X! @%
仿真参数  L$ A7 \2 u$ u
num_events=EV_LEN-1;
1 k$ T2 p- W" jmean_interarrival=1;2 D6 t( g' g. s( e3 S0 ^) B: |6 ?
mean_service=.5;
% h0 w2 Y5 S1 P! ^& X& W: Nnum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
7 p6 B$ b" T$ L3 S$ d. C" wfprintf(outfile, 'Single-server queueing system\n\n');) h( t5 q4 E: G
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);
; ]: B( ?& i7 {& `$ q! Q* K* o+ ^3 xfprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);* t+ ~9 c- P3 {% R+ n7 j
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1
, ]9 H. E3 Q  M6 @/ W2 ?0 |/ vsim_time=0.0;
6 B4 @' ?+ _4 r# F- Q% P7 a' h5 J%     /* Initialize the state variables. */
    server_status   = 0;%idle$ P' a+ L7 m0 a
    num_in_q        = 0;9 B5 ]" C" K, M2 r7 S6 H2 T
    time_last_event = 0.0;7 }! H8 ?( Y3 w5 I* o. o& ?
   
9 }7 R  L% x! {1 u/ i7 w. c%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;0 M' u: P7 E* @
    total_of_delays    = 0.0;$ @8 w" g2 y: G6 p& s5 u( a" H- Q
    area_num_in_q      = 0.0;7 y: c! t5 S: z% @4 b0 \/ u6 |" S
    area_server_status = 0.0;; l2 Z" W2 ~5 A& V
   
5 N7 c) F% c2 k& `% U%     /* Initialize event list.  Since no customers are present, the departure
/ d# k! e) ]0 o4 n" z. y4 `%        (service completion) event is eliminated from consideration. */% j- a& q7 h3 k0 r! h" L  R
    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);6 C1 n/ z7 l5 P* w0 B
    time_next_event(EV_Depart) = 1.0e+230;
; T; [; h, [! ^. _6 u8 S
    # }" h9 Q; C9 b9 M' i) y3 O
    " u6 S9 l4 ?# U1 M9 D( |
   
+ x% N1 X% w# V, g- k %%%%%%%%%%%%part2
, s! j" f  g2 ^2 a, Y9 T. _while (num_custs_delayed < num_delays_required)6 p5 x' `( H% K- l" _: W; i
%Run the simulation while more delays are still needed.
* q6 X/ g7 n# P+ A& Z& h4 _%/* Determine the next event. */
     min_time_next_event = 1.0e+290;
, ]1 s/ ]8 [$ t, s     next_event_type = 0;
) G. D  O7 g1 e6 U     2 m$ c# h& s  o# W: x
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events
0 ^5 \) O, ~3 n6 Y7 l+ W        if (time_next_event(i) < min_time_next_event) & q! \$ E6 d! Y
            min_time_next_event = time_next_event(i);
$ V5 V) _9 O4 M            next_event_type     = i;3 J2 k) o+ `, Y2 s7 n5 G! s) J
        end
' K; ~: r( V5 w; W3 o1 ^    end7 o; l1 |) |/ g, p$ J& Z
   
1 o2 N+ J' T6 r; C! ]%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) / P" v1 t/ S4 i6 F) ]2 K
        ! w: m8 s) z% }- \/ }
%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);
1 ^9 U' d3 D% Y" R* r        exit(1);
+ t3 R& x7 A) M; r0 l    end! q0 i$ Z! v4 K8 M% ?) }5 f
%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;& q, N% C% R3 L$ n- e/ i

( ~; n0 A) ?. d" y' k+ G3 `    - _0 v% ~: a# c( M7 }; v0 u
%/* 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;
/ W, }$ M8 g6 b/ T* l+ v  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;
3 W' T: Y. x. z    6 F* h. }& m6 `; u# B) `) Q
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive)
  \: V' I, w4 b  P* D        double delay;/ R" @8 X6 P8 D" c7 d2 c  m( C/ H
        
4 R' }1 h! S. 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) % F7 c: E6 ?. Z
            ! S( }: I- B: m1 G) @. E
%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;
9 @. S+ ]/ L: D3 x4 I8 n4 L            
. l+ c! G* @4 m! {/ T%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 
$ O- w* p* X+ h" {* M% {%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');' v! @* G' q' r! _
                fprintf(outfile, ' time %f', sim_time);5 p  |% N' ?5 D7 Y
                exit(2);- z* V% |7 b; X4 s4 O
            end% e: I6 \( F# e' k: x0 Y$ L
%/* 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
6 s3 S* t* I9 b7 f* |  `! \! K%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity / e5 c3 v3 i* u: i
%and do not affect the results of the simulation.) */
            delay = 0.0;1 l( Z: f$ u: Z6 m$ j" H
            total_of_delays =total_of_delays + delay;
; w' |' d. W: u% w& O1 |! M            
* X4 G! }' |: {% I' b%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
/ q) f' W9 S. v+ I: s            server_status = ST_Busy;
8 K& W- v1 P+ s+ _' c' W0 b            
9 q: X# E. Y5 f) d% V+ M%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
4 b* T; ^$ m. ~  q" V) F+ P        end %    if (server_status == ST_Busy)
; w# m) f, `6 y0 @6 R%%%%%%%%depart
& b" v/ S2 F  v    else5 w/ R! O. B/ e7 Z8 `$ _
            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;
- K) v$ ?1 E" ?. Y3 }  J2 l( v/ {            time_next_event(EV_Depart) = 1.0e+230;, {% ]/ O2 r! v  w' f
   
2 ]6 d' X2 F8 E" R! n        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 a8 J+ k# m+ P- l/ m# O  O            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;4 M7 v' G" F% \8 S, I% q
            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));1 k. b9 u% T9 P+ {- ~) E2 w9 ]; t" J
            time_arrival=tempForPop;
0 _9 @4 @5 l/ D7 y) q' S7 ^( z        end %if (num_in_q == 0)/ r. p3 \9 L7 {& d5 j
        # P7 U% z& g5 x) S
    end %if(next_event_type==EV_Arrive)/ }* O9 F! E; l
   
! x* N  o' h# P: f8 E+ Send %while0 ]+ c) L, _+ N2 O! `* n
               

, I9 Y/ u( ]: W%%%%%%%%%% part 3
8 s$ l' D  m4 ?5 w" O- w%/* 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 v& O8 j/ i  S/ u2 D6 M! x/ Z: _    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);
* a9 A- C) f' h, E( n$ c    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);/ d' B4 ^2 ^* ]2 e0 R4 h# I
    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);; Z2 P5 j3 z  d% s. }% d2 H, Y
    fclose(outfile);

作者: liuyaliss    时间: 2009-8-22 09:28
看看~~~~谢谢楼主
作者: rockhuman    时间: 2009-8-22 09:41
资料发上来 最好加上 一点解释  让大家都那能理解一下
作者: purplelesly    时间: 2009-8-29 12:52
谢谢楼主 ~~学习学习
作者: leeeff    时间: 2009-8-29 18:24
dddddddddddddddddddddddddddddddddddddddddddddd
作者: norah8291829    时间: 2009-9-3 12:27
看不懂~
作者: perfectgreen    时间: 2009-9-11 13:57
啊!!很好很强大啊!感谢楼主。。
作者: JocelynHUI    时间: 2009-9-11 14:14
高手啊!请教一下,B题第一问怎么做呀?
作者: GunBreaK    时间: 2009-9-11 16:20
没有矩阵币怎么办?。。。
作者: jamesgeng    时间: 2009-9-11 23:05
好东西啊,就是要用仿真的
作者: chuan0810    时间: 2009-9-11 23:46
有用没有那个也是!




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5