page383.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 126 行

HTML
126
字号
<HTML><HEAD><TITLE>Implementation</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html5597" HREF="page384.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5595" HREF="page381.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5591" HREF="page382.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5599" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0011520000000000000000">Implementation</A></H2><P>This section presents the simulation ofa system comprised of a single queue and server as shown in Figure&nbsp;<A HREF="page382.html#figqueueing"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Program&nbsp;<A HREF="page383.html#progsimulationb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the nested class <tt>Simlation.Event</tt>which represents events in the simulation.<P><P><A NAME="27495">&#160;</A><A NAME="progsimulationb">&#160;</A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program27293" SRC="img1526.gif"  ><BR><STRONG>Program:</STRONG> <tt>Simulation.Event</tt> class.<BR><P><P>Since events will be put into a priority queue,the <tt>Event</tt> class is derived from the <tt>Association</tt> classintroduced in Section&nbsp;<A HREF="page127.html#secadtsassociations"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.An association is an ordered pair comprised of a key and a value.In the case of the <tt>Event</tt> class,the key is the <em>time</em> of the eventand the value is the <em>type</em> of the event.Therefore, the events in a priority queue are prioritized by their times.<P>Program&nbsp;<A HREF="page383.html#progsimulationa"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>run</tt> methodwhich implements the discrete event simulation.In addition to <tt>self</tt>,this method takes one argument, <tt>timeLimit</tt>,which specifies the total amount of time to be simulated.<P>The <tt>Simulation</tt> class uses several instance attributes.The first, <tt>_eventList</tt>,is a priority queue.This priority queue is used to hold the events during the courseof the simulation.<P><P><A NAME="27326">&#160;</A><A NAME="progsimulationa">&#160;</A> <IMG WIDTH=575 HEIGHT=791 ALIGN=BOTTOM ALT="program27323" SRC="img1527.gif"  ><BR><STRONG>Program:</STRONG> Application of priority queues--discrete event simulation.<BR><P><P>The state of the system being simulated is represented by the twoinstance attributes <tt>_serverBusy</tt> and <tt>_numberInQueue</tt>.The first is a <tt>bool</tt> value which indicates whether the server is busy.The second keeps track of the number of customers in the queue.<P>In addition to the state variables,there are two instances of the class <tt>ExponentialRV</tt>.The class <tt>ExponentialRV</tt> is a random number generatordefined in Section&nbsp;<A HREF="page465.html#secalgsrng"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.It extends the abstract <tt>RandomVariable</tt> classdefined in Program&nbsp;<A HREF="page468.html#prograndomVariablea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.This class defines a property called <tt>next</tt>which is used to sample the random number generator.Every time <tt>next</tt> is accessed,a different (random) result is returned.The random values are exponentially distributed around a mean valuewhich is specified in the <tt>__init__</tt> method.For example, in this case both <tt>_serviceTime</tt>and <tt>_interArrivalTime</tt>produce random distributions with the mean value of 100 (lines&nbsp;11-12).<P>It is assumed that the <tt>_eventList</tt> priority queue is initially empty.The simulation begins by enqueueing a customer arrival at time zero (line&nbsp;15).The <tt>while</tt> loop (lines&nbsp;16-39) constitutes the main simulation loop.This loop continues as long as the <tt>_eventList</tt> is not empty,i.e., as long as there is an event to be simulated<P>Each iteration of the simulation loop beginsby dequeuing the next event in the event list (line&nbsp;17).If the time of that event exceeds <tt>timeLimit</tt>,the event is discarded,the <tt>_eventList</tt> is purged,and the simulation is terminated.Otherwise, the simulation proceeds.<P>The simulation of an event depends on the type of that event.The <tt>if</tt>/<tt>elif</tt> statements (lines&nbsp;22 and&nbsp;32)invoke the appropriate code for the given event.If the event is a customer arrival and the server is not busy,<tt>_serverBusy</tt> is set to <tt>True</tt>and the <tt>_serviceTime</tt> random number generator is sampledto determine the amount of time required to service the customer.A customer departure is scheduledat the appropriate time in the future (lines&nbsp;23-27).On the other hand, if the server is already busy when the customer arrives,we add one to the <tt>_numberInQueue</tt> variable (line&nbsp;29).<P>Another customer arrival is scheduled after every customer arrival.The <tt>interArrivalTime</tt> random number generator is sampled,and the arrival is scheduledat the appropriate time in the future (lines&nbsp;30-31).<P>If the event is a customer departure and the queue is empty,the server becomes idle (lines&nbsp;33-34).When a customer departs and there are still customers in the queue,the next customer in the queue is served.Therefore, <tt>_numberInQueue</tt> is decreased by oneand the <tt>_serviceTime</tt> random number generator is sampledto determine the amount of time required to service the next customer.A customer departure is scheduledat the appropriate time in the future (lines&nbsp;36-39).<P>Clearly the execution of the <tt>run</tt> methodgiven in Program&nbsp;<A HREF="page383.html#progsimulationa"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> mimics the modeled system.Of course, the program given produces no output.For it to be of any practical value,the simulation program should be instrumentedto allow the user to study its behavior.For example, the user may be interested in knowing statisticssuch as the average queue lengthand the average waiting time that a customer waits for service.And such instrumentation can be easily incorporated into the given framework.<P><HR><A NAME="tex2html5597" HREF="page384.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5595" HREF="page381.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5591" HREF="page382.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5599" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?