Simulation Program Basic Design

An object oriented language is best for simulation. You will typicall need the following classes

  • An event class - with time, type, and any other variables that hold information to process that event
  • Classes corresponding to system state
  • An "eventlist" class - this is event queue, typically implemented as a priority queue whose key is the time of the event.

The basic simulation design should always  be as follows:
simulation main:
initialize simulation {
  set time=0,
  schedule initial events
(that means create Event objects, timestamp them, add any necessary information
about which entities in the actual system these events might be about.
Then add the event into the event list)
while (eventlist not empty) {
remove the nearest event (pop from priority queue, key being event timestamp, priority
queue is required because events are not added into the queue in the order that they may
happen. You may add an event e1 scheduled at time t+s, before you add an event e2
scheduled at time t.)
advance simulation clock to this event time
handle event based on event type (i.e. call event handler).
(This will usually be a switch that picks the event  handler based on the event
type. For advanced simulations, event handlers will need arguments. This
could be information that was retrieved from the event object itself. Mainly,
simulations of complex systems need information about which object of the
system is the event about. E.g. in a multiple server simulation, if you get a departure
event, you need to know which server the departure is from. Or which request
departing - server ID could be in the request)
Event Handlers:
event handlers should have logic like this:
update system state - whatever needs to be done to the state, do it.
Based on new system state, new events may get created.
Create them, add them into eventlist.
Collect statistics.
Generate Statistics At The End
In the end, after exiting main simulation loop, you will need to generate summary statistics.

  • Do not distribute events in multiple data structures. Consolidate events into one event list
  • Think about the best way of attaching information to an event, that helps you process it.