![](https://csdnimg.cn/release/download_crawler_static/9871868/bg9.jpg)
4.2. SIMULATING THE M (T )/M/∞ QUEUE 9
class G:
maxTime = 24.0 # hours
arrivalrate = 100 # per hour
parkingtime = 2.0 # hours
parkedcars = 0
seedVal = 9999
Figure 4.3: Global declarations.
Global declarations are in Fig. 4.3. While Python allows these to be in the
global namespace, it is preferred that these be put into a class created for the
purpose as shown here. In particular, this is generally used as a central location
for parameters of the model.
The main simulation class is shown in Fig. 4.4. The simulation class Parkingsim
is a subclass of Sim.Simulation. While it can have other functions, the central
function is the run(self) function. Note that the first argument of a member
function of a class is self, indicating that the function has access to the other
functions and variables of the class. The run function takes one argument, the
random number seed. Sim.initialize() sets all Monitors and the simulation
clock. Then processes and resources are created. In this simulation there is one
process, the generation of arriving cars in Arrival. Note that at this point,
there are no Car’s, as the Arrival process will create the cars. This simulation
does not have any resources; but if, for example, the parking lot had a limited
capacity of 20 spaces, it could have been created here by:
self.parkinglot = Sim.Resource(capacity = 20, name=’Parking lot’,
unitName=’Space’, monitored=True, sim=self)
This creates the parking lot with 20 spaces. By default, resources are a
FIFO, non-priority queue with no preemption. In addition to the capacity of
the Resource (number of servers), there are two queues (lists) associated with
the resource. First is the activeQ, which is the list of process objects currently
using the resource. Second is the waitQ, which is the number of processes that
have requested but have not yet received a unit of the resource. If when creating
the resource the option monitored=True is set, then a Monitor is created for
each queue, and statistics can be collected on resource use and the wait queue for
further analysis. The last option is sim=self, which declares that the Process
or Resource is part of the simulation defined in the current class derived from
Sim.Simulation. If the simulation was defined in the global namespace (not
inside of a class), then this option would not be needed.
After Processes and Resources are created, any additional Monitors are cre-
ated here. Note that Processes, Resources, and Monitors are created using
self. constructs. This classifies the object as part of the class, and not only
local to the run() function. Other classes and methods that are part of this
simulation (declared using sim = self) can refer to any object created here
using self.sim.objectname. This includes Monitors as well as variables that
need to be updated from anywhere in the simulation. For example, the vari-
able self.parkedcars can be updated from the Car class in Fig. 4.2 using