Posted 27 Oct 2004 by herman
You can program event-driven simulation in Python.
Using the three basic elements (clock, simulated objects, event list) the Python implemention is straightforward.
Example: Customers Arriving to an ATM
The program shoq.py simulates customers and one server. There are three objects in the simulation:
- Customer Generator. This object, when fired, simulates the arrival of a new customer into the waiting line for the ATM (invoking the ATM's enqueue method) and also schedules itself to fire again in the future.
- Customer Server. This is the ATM object. When the "fire" method is invoked, the first customer in the queue is removed. The ATM also has an "enqueue" method that adds a customer to the ATM queue and schedules an event to fire in the future.
- Visualizer. This object has no real use for the simulation, but it is useful to periodically examine the other objects during the simulation, print values, and so on --- for debugging or for study of behavior. Interesting is that the Visualizer is nothing special so far as the simulator is concerned, it just operates like any other simulated object, and has a "fire" method which is used to reschedule visualizer events.
The program shoqave.py uses the visualizer to keep track of the average queue length associated with the ATM.
The program shoqGUI.py displays the queue lengths during the simulation graphically.