CIS 22 - Simulations: Events


   A simulation can be approached in many ways. One way could be using event queues.  Another could be a time-based approach. I did a lot of research on event-based simulations, and to explain how to do them would be way too complex. Time-based is a little simpler to explain and implement. Events still play a role, but an incrementing timer determines when events occur. To illustrate the events, I'll use our three simulations:

        1)    A student registration scenario using regular queues

        2)    A student registration scenario using priority queues

        3)    A supermarket scenario

    A vital part of programming a simulation is to do a kind-of role playing with all of the subjects (actors). In our first simulation, there are two people; students and registration station servers. If you look at the simulation from the point of view of a server, you will repeatedly wait for a student to come to you and serve that student until he/she is finished, at which time you will wave goodbye and repeat. From the point of view of a student, you'll arrive in the building at a certain time, get on the back of shortest line, move up in the line until you are next to be served, get off line and head to the registration station, be served, and leave. This process isn't repeated for the individual student. Basically, those are the events in the first simulation. At various points, values are calculated, and snapshots are taken, but in essence, these events repeat until all students are served. This role playing will help you design your classes and variables. For example, a student will have to know:

        1)    When he/she will arrive

        2)    How long he/she will take to register

    Certain values don't have to be stored, but can be arrived at by math. For example, to find out how long a student was on line, all you have to do is take the timer value when the student was finished and subtract the his/her arrival time and serving time.

    A server will have to know:

        1)    How long he/she has been idle (nobody at station or on queue)

        2)    How long he/she has been serving a student

    For the priority-queue example, much is the same, except for how the student gets on line. In a sense, higher-ranking students (ie. seniors) can butt in line. Other than that, the events are identical. However, you will probably want to tabulate wait times by rank, so your main program will have to store these variables.

    Now, for our supermarket example, much is the same. The only difference is an extra step between arriving and getting on the register queue. A given shopper will know how many items he/she will buy, and this, coupled with an average item-buy time, will determine a new shopper variable called "shopping time". I have all shoppers get on the shopping queue (priority based on shopping time where shortest time is at the front), and when the timer matches the shopping time plus arrival time, the shopper will get on the shortest line, and the rest of the simulation will be just like the others. Aside from some additional variables, the events are identical except for an extra step for the shopper. The events of the server will be exactly the same.

    Basically, a timer will start incrementing from zero, and certain values will be compared with it to determine exactly when given events should occur.


Back to CIS 22 home page

Thomas Erno, 2003