A Simple Discrete-Event Simulation: Part 52

Today I reworked the future events mechanism to streamline it in a big way. I knew it needed this and the essential insight about how to do it occurred to me in the shower about ten days ago. I decided to do this now because I was going cross-eyed looking at the connection logic, which itself needs one more extension.

The main insight involved the need to recycle the reference to the most recent future event item or to create a new future event item. New items are generated as needed and placed in the future events queue. Items are then pulled off the queue in time order and processed one by one. The items are stored in the feqCurrent variable, which can be updated and recycled back into the queue. The variable should then be set to null, which signals that a new event item will have to be created so it can be placed into the future events queue.

I also created standalone functions to process the reuse or generation of new future event items. The advance function defines and event that takes place by a specified offset from the current time, whenever that is. That’s the most commonly used function and models a fixed delay, process time, or event duration in general. The jump function works the same way except that the absolute time (from time zero, the beginning of the simulation) is used. This function is more likely to be used to specify a schedule of known events as the simulation is initialized.

The code is shown below in its now much abbreviated form.

Here are a couple of examples of the advance function in use. The second example shows the extra parameter being used to reference an entity that needs to be processed within a component.

This entry was posted in Simulation and tagged . Bookmark the permalink.

Leave a Reply