Simulation in Python (SimPy)

Category Cross-Omics>Agent-Based Modeling/Simulation/Tools

Abstract SimPy is an object-oriented, process-based discrete-event simulation language based on standard Python and released under the GNU GPL.

It provides the modeler with components of a simulation model including processes, for active components like customers, messages, and vehicles, and resources, for passive components that form limited capacity congestion points like servers, checkout counters, and tunnels.

SimPy also provides Monitor variables to aid in gathering statistics. Random variates are provided by the standard Python random module.

Many users claim that SimPy is one of the cleanest, easiest to use discrete event simulation packages!

SimPy comes with ‘data collection’ capabilities, Graphical User Interface (GUI) and plotting packages. It can be easily interfaced to other packages, such as plotting, statistics, GUI, spreadsheets, and databases.

What SimPy can be used --

SimPy has very wide applicability. It is already being used for:

‘Modeling/simulation’ of epidemics; Traffic simulation; Air space surveillance planning; Industrial engineering;

Computer hardware performance studies; Performance modeling; Industrial process optimization;

Workflow studies; Teaching of simulation methodology; and a lot of applications that the manufacturers don't know about.

SimPy’s capabilities/features include:

The active elements (or entities) of a SimPy model are objects of a class defined by the programmer (see Processes, below...).

Each entity has a standard method, a ‘Process Execution Method’ (referred to by SimPy programmers as a PEM) which specifies its actions in detail. Each PEM runs in parallel with (and may interact with) the PEMs of other entities.

The activity of an entity may be delayed for fixed or random times, queued at resource facilities, and may be interrupted by or interact in different ways with other entities and components.

For example in a gas station model, automobile entities (objects of an Automobile Class) may have to wait at the gas station for a pump to become available. On obtaining a pump it takes time to fill the tank. The pump is then released for the next automobile in the queue if there is one.

SimPy has three (3) kinds of ‘resource facilities’ (Resources, Levels, and Stores). Each type models a congestion point where entities queue while waiting to acquire or, in some cases, to deposit a resource.

SimPy automatically handles the queuing.

1) Resources have one or more ‘identical resource’ units, each of which can be held by entities.

Extending the example above, the gas station might be modeled as a Resource with its pumps as resource units. When a car requests a pump the gas station resource automatically queues it until a pump becomes available (perhaps immediately). The car holds the pump until it finishes refueling and then releases it for use by the next car.

2) ‘Levels model’ the supply and consumption of a homogeneous undifferentiated “material”. The Level holds an amount that is fully described by a non-negative number which can be increased or decreased by entities.

For example, a gas station stores gas in large storage tanks. The tanks can be filled by tankers and emptied by cars refueling. In contrast to the operation of a Resource, a car need Not return the gas to the gas station.

3) ‘Stores model’ the production and consumption of distinguishable items. A Store holds a list of items. Entities can insert or remove items from the list and these can be of any type. They can even be SimPy process objects.

For example, the gas station holds spares of different types. A car might request a set of spares from the Store. The store is replenished by deliveries from a warehouse.

SimPy also supplies Monitors and Tallys to record simulation events.

Monitors (see below…) are used to compile summary statistics such as waiting times and queue lengths. These statistics includes simple averages and variances, time-weighted averages, or histograms. In particular, data can be gathered on the queues associated with Resources, Levels and Stores.

For example you may collect data on the average number of cars waiting at the gas station and the distribution of their waiting times.

Monitors preserve complete time-series records that may later be used for more advanced post-simulation analyses. A simpler version of a Monitor is a Tally which provides most of the statistical facilities but does Not retain a complete time-series record.

Processes --

SimPy’s active objects (entities) are process objects - instances of a class written by the user that inherits from SimPy’s Process class.

For example, if you are simulating a gas station you might model each car as an object of the class Car. A car arrives at the gas station (modeled as a Resource with pumps). It requests a pump and may need to wait for it. Then it fills its tank and when it has finished it releases the pump.

It might also buy an item from the station store. The Car class specifies the logic of these actions in its Process Execution Method (PEM). The simulation creates a number of cars as it runs and their evolutions are directed by their Car class’s PEM.

Random Number Generation --

Simulations usually need ‘random numbers’. By design, SimPy does Not provide its own random number generators, so users need to import them from some other source. Perhaps the most convenient is the standard Python random module.

It can generate random variates from the following continuous distributions: uniform, beta, exponential, gamma, normal, log-normal, Weibull, and vonMises. It can also generate random variates from some discrete distributions.

Python’s random module can be used in two (ways: you can import the methods directly or you can import the Random class and make your own random objects. In the second method, each object gives a different random number sequence, thus providing multiple random streams as in some other simulation languages such as Simscript and ModSim.

Monitors and Recording Simulation Results --

A Monitor enables you to observe a particular ‘variable of interest’ and to hold a ‘time series’ of its values. It can return a simple data summary either during or at the completion of a simulation run.

For example you might use one Monitor to record the waiting time for each of a series of customers and another to record the total number of customers in the shop.

In a ‘discrete-event system’ the number of customer’s changes only at arrival or departure events and it is at those events that the number in the shop must be observed. A Monitor provides simple statistics useful either alone or as the start of a more sophisticated statistical analysis.

When Resources are defined, a Monitor can be set up to automatically observe the lengths of each of their queues.

SimPy's extensive Documentation sections include --

SimPy Overview; Acknowledgments; Getting Started; Using SimPy; SimPy Libraries; SimPy Tutorials;

SimPy Course on the Web; Interfacing to External Packages; Cheat-sheets; SimPy Tools; and Source code Documentation.

System Requirements

You need to know how to write Python code to develop a SimPy model.

In particular, you will have to define and use classes and their objects. Python is free and open-source and is available on most platforms.

You can find out more about SimPy and download it from the manufacturer’s web-site, where there is full ‘Python web-site’ documentation and tutorials (as stated above…).

SimPy requires Python version 2.3 or later.

Manufacturer

Manufacturer Web Site Simulation in Python (SimPy)

Price Contact manufacturer.

G6G Abstract Number 20605

G6G Manufacturer Number 104205