数学建模社区-数学中国

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

作者: qlau2007    时间: 2009-8-12 13:02
标题: M/M/1排队系统性能仿真代码(MATLAB)
8 \3 L( e" w8 J+ [1 l: B
7 N0 B1 y) d2 R! k
可能有点儿乱啊~~嘿嘿
%mm1 simulation in matlab
$ M1 ~. s0 m: w3 V( F/ |$ lclc;clear;
ST_Idle=0;
9 K4 m7 f& [- ?2 p$ H. v" S  L. ~ST_Busy=1;
EV_NULL=0;
9 D- O! P) F2 @EV_Arrive=1;" J& g1 y% [0 e/ w- P
EV_Depart=2;
; D6 G; B1 v5 ^EV_LEN=3;
Q_LIMIT=1000;
% next_event_type=[];
4 r, c, {9 R9 t" W- o$ z% num_custs_delayed=[];
% O3 U& h0 P: S9 P! ?# L) A% num_delays_required=[];0 Q4 ?/ O' w, c% r
% num_events=[];
$ i* e/ X# `- R6 O" N7 }; p% num_in_q=[];" Y: M2 W; o: y6 v2 J) B
% server_status=[];! }% L8 m/ G" {) ?2 @/ |! O: G
% area_num_in_q=[];
% o- f6 N- r: m# W4 D3 ?- j% area_server_status=[];
3 j  C4 @# V9 f, o5 h, u% mean_interarrival=[];
  D: X& h. |4 c# @4 D! e% mean_service=[];4 T8 }& B4 r& d( i+ [, Z) w6 _
% sim_time=[];4 @: a1 u9 g  n5 r
% time_last_event=[];8 W; t$ e" G/ k: G7 q
% total_of_delays=[];0 _; r& n: r" M2 x7 A
%
( ^: [' q* O" Ftime_arrival=[];                 %
到达时刻
time_next_event=zeros(1,EV_LEN);
6 E" U1 g* M4 m9 j9 w%
仿真参数
2 _# a5 b6 A3 @5 |num_events=EV_LEN-1;
) o/ r, T+ Z6 `0 C) o( g2 Zmean_interarrival=1;
% m3 }7 s( D5 H+ `( M" imean_service=.5;
. x# I4 c+ X1 }* X& W$ {7 }  Gnum_delays_required=2000;           %
outfile=fopen('mm1.txt','w');
8 S3 B$ g. H8 b) H# c/ L- Ffprintf(outfile, 'Single-server queueing system\n\n');+ C4 w2 k! b0 U+ ?
fprintf(outfile, 'Mean interarrival time%11.3f minutes\n\n',mean_interarrival);7 f3 u1 E; t1 R. h* o  y2 b
fprintf(outfile, 'Mean service time%16.3f minutes\n\n', mean_service);$ {/ r: R( C* O
fprintf(outfile, 'Number of customers%14d\n\n', num_delays_required);
%%%%%%%%%%%%%part1- e7 X$ [; e; s  X5 S
sim_time=0.0;+ n& v$ }* p5 S, Q# \. k! j
%     /* Initialize the state variables. */
    server_status   = 0;%idle+ p: |% b! ~. \7 }- h
    num_in_q        = 0;
' m( H: v; n: {1 e. T  C5 ^    time_last_event = 0.0;* ^  O' H) Z) Y, N
    / F9 D! H) m( h, w) P/ H) I
%     /* Initialize the statistical counters. */
    num_custs_delayed  = 0;
% P1 I2 `+ Z* R9 D& f$ z( }8 a    total_of_delays    = 0.0;$ r) D+ o# x9 Q# i3 e% U/ c
    area_num_in_q      = 0.0;
& m: u  l$ Y2 _. j# [$ t6 x  e    area_server_status = 0.0;
" B: B6 r. k' W1 B   
# X8 f" N9 G( M& c%     /* Initialize event list.  Since no customers are present, the departure
, `/ d" R8 V/ K7 L$ X. x8 M%        (service completion) event is eliminated from consideration. */
; x9 A& C1 V5 A3 ~. O    time_next_event(EV_Arrive) = sim_time + randexp(mean_interarrival);
" }2 G8 Y4 c1 o' `    time_next_event(EV_Depart) = 1.0e+230;

# n# R$ {2 J! h3 I2 }    ! R% ]+ {. x+ W3 _$ d- f% a* q
   
& ~) j% m6 J2 U/ h+ g' _1 L$ N" @   
  P0 r0 x' M' g, c4 [2 b) ^ %%%%%%%%%%%%part2
* R4 e4 e6 L: x6 Cwhile (num_custs_delayed < num_delays_required)
9 H: r0 Z+ ^- y9 L( w+ i  v. y4 ?%Run the simulation while more delays are still needed.+ g( n! j, w$ }0 D
%/* Determine the next event. */
     min_time_next_event = 1.0e+290;# [: V. i6 e$ ^
     next_event_type = 0;
( p! B7 N% s# w( \+ Y       {& w4 n" Z. }8 H' ?* z, d. {
%/* Determine the event type of the next event to occur. */
    for i = 1: num_events3 _% u& H6 ~; z7 p3 ]; j6 H% K& e
        if (time_next_event(i) < min_time_next_event)
5 m/ u9 \; o& X3 y, Z            min_time_next_event = time_next_event(i);
7 I; Q9 g5 m: d4 b0 x* {7 e1 c4 `            next_event_type     = i;
3 H0 Z0 o) O6 ]1 l& j        end4 v5 Z0 {- r4 P6 s9 s& L+ H
    end# ?5 _5 {. J! k) b: b
    1 m6 b9 H  E5 P
%/* Check to see whether the event list is empty. */
    if (next_event_type == 0) , J* f# @! m- i3 H
        
* k$ h4 P4 z8 R/ M8 Z%/* The event list is empty, so stop the simulation. */
        fprintf(outfile, '\nEvent list empty at time %f', sim_time);7 ]" _8 s- p" Z  h
        exit(1);: G0 u# t& ?4 e' w+ R5 H9 B1 G
    end
! C5 h- {* K; z%/* The event list is not empty, so advance the simulation clock. */
    sim_time = min_time_next_event;
1 P$ |; G4 a7 T: O ) y5 P8 H# F0 [: B' l
    ; t  T2 E/ H" s- r8 X
%/* 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;
: r5 S, z* u. ~8 K7 i    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;" z( t7 G( a' r) S& a3 J& f7 G2 n
    & A0 x+ I+ ?, x5 I# H3 y8 q  `! D& w
%/* Invoke the appropriate event function. */
%%%%%%%%%%%%%arrival
    if(next_event_type==EV_Arrive): G  E1 c* g  o! b3 `
        double delay;
+ L% ~) a" K8 v0 d8 q1 k        
+ ^; Q; w5 H3 P%/* Schedule next arrival. */
        time_next_event(1) = sim_time + randexp(mean_interarrival);
%/* Check to see whether server is busy. */
        if (server_status == ST_Busy) . t0 N& A* P4 `/ a5 ]/ p! R% s' G/ H
            
: U( K& p  n0 M%/* Server is busy, so increment number of customers in queue. */
            num_in_q=1+num_in_q;! G7 T+ N4 o- g3 E% N; `' h
            1 E1 ]5 H8 X* \
%/* Check to see whether an overflow condition exists. */
            if (num_in_q > Q_LIMIT)                 % ?9 J# L! F' k  Z) }
%/* The queue has overflowed, so stop the simulation. */
                fprintf(outfile, '\nOverflow of the array time_arrival at');' W. [- o) I: z* z- }
                fprintf(outfile, ' time %f', sim_time);
: ~$ x: A8 m3 I0 K0 i. ], H1 m+ V) o9 w                exit(2);; k. N( L! p& g3 k5 ^2 ?) K
            end# j' [! d" T' C. L: O, E
%/* 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
! p3 {2 b- Y7 Z9 Y%/* Server is idle, so arriving customer has a delay of zero.  (The following two statements are for program clarity
0 o) {* ?/ c: |/ W. q%and do not affect the results of the simulation.) */
            delay = 0.0;/ I, [. P' Z3 a1 F8 b
            total_of_delays =total_of_delays + delay;
- D# i  \- m& G" P$ Z            " x. M& z: n9 x  ~
%/* Increment the number of customers delayed, and make server busy. */
            num_custs_delayed = 1 + num_custs_delayed;
. h9 H* F; t2 {, {1 R            server_status = ST_Busy;) B8 d- @5 \+ G, _+ C8 M
            , @( u  r* v) ]" ?* }' ?/ q
%/* Schedule a departure (service completion). */
            time_next_event(EV_Depart) = sim_time + randexp(mean_service);
1 }/ \* j; ~7 {7 K        end %    if (server_status == ST_Busy)
. L' }* J, W$ s: _* n) t; r' |%%%%%%%%depart
% r& p2 k5 e# E0 u' V    else
2 T$ @& o) z5 P- d            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;" I+ ~6 ~0 O" j: [' {- T
            time_next_event(EV_Depart) = 1.0e+230;
1 ^  S$ F% [6 C$ F/ r) N# i& Y    3 ^- e9 q0 P+ m8 ?' n# b; T
        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);
9 _8 @" m3 k8 p- E, ?            total_of_delays =total_of_delays + delay;
%/* Increment the number of customers delayed, and schedule departure. */
            num_custs_delayed = 1 + num_custs_delayed;" s) v, N. i- g9 w! A/ C) W0 v
            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));
6 c9 ?$ s7 A" D- o1 E5 z            time_arrival=tempForPop;
$ R8 L& E* U9 q( c, g( L" ~# S        end %if (num_in_q == 0)
9 ]" r7 d9 C# f' V  }8 {( J        
% t9 B: G9 I1 m  a/ ]" _( p    end %if(next_event_type==EV_Arrive)
3 O: W& D! \- {; h% [8 n    ! l, v, |+ o6 t  Q4 x: {) k
end %while* K/ h6 j% V8 z8 _  W" W" k# C5 c
               
# F! E* f+ L! W4 ]% O
%%%%%%%%%% part 3
7 a* w9 q' P% z: L%/* 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);
$ i: i  t" g0 w! a6 w, V6 L" I    fprintf(outfile, 'Average number in queue%10.3f\n\n',area_num_in_q / sim_time);# v: k7 V* s# C* o4 _; H5 m
    fprintf(outfile, 'Server utilization%15.3f\n\n',area_server_status / sim_time);
- n" p! x( G" i8 T, J; o0 q    fprintf(outfile, 'Time simulation ended%12.3f minutes', sim_time);
! g4 m( C; ^. y6 F9 g    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