A Simple Discrete-Event Simulation: Part 1

Some months ago I discussed some internal features of a discrete-event simulation. My next series of exercises and ruminations will proceed toward building one in JavaScript, using the simple pseudo-construct I outlined in the linked post.

I’ll start with the creation of a future events queue. Since the point is to get something running in a reasonably brief series of posts I’ll keep it simple. There are many ways to make a future events queue more efficient, but for our purposes we’ll create one with a straightforward insert/pull structure. That is, it’ll be a single list of items, sorted by time (we’ll assume items occurring at the exact same time are processed in the order in which they were added to the queue, in one dimension.

Here’s a rough bit of code I worked out a while ago.

This code generates the following output to the console, which indicates that the basic insertion mechanism is working.

Number of events in queue: 0
Array size: 1
reportItemInfo: element: [object Object] index: 0 arr: [object Object]
Time: 40 ID: 333 Prev: prev1 Next: next1
Number of events in queue: 1
Array size: 2
reportItemInfo: element: [object Object] index: 0 arr: [object Object],[object Object]
Time: 30 ID: 444 Prev: prev2 Next: next2
reportItemInfo: element: [object Object] index: 1 arr: [object Object],[object Object]
Time: 40 ID: 333 Prev: prev1 Next: next1
Number of events in queue: 2
Array size: 3
reportItemInfo: element: [object Object] index: 0 arr: [object Object],[object Object],[object Object]
Time: 30 ID: 444 Prev: prev2 Next: next2
reportItemInfo: element: [object Object] index: 1 arr: [object Object],[object Object],[object Object]
Time: 40 ID: 333 Prev: prev1 Next: next1
reportItemInfo: element: [object Object] index: 2 arr: [object Object],[object Object],[object Object]
Time: 50 ID: 555 Prev: prev3 Next: next3
Number of events in queue: 3

This entry was posted in Software and tagged , . Bookmark the permalink.

Leave a Reply