A Simple Discrete-Event Simulation: Part 8

Today I automated the generation of entity IDs.

First we create a basic counter.

Then we change the entity declarations to remove the explicit assignment of IDs as we have been doing.

That was pretty mindless, so let’s also add a new type of entity, following the form of the original one. Here’s the original. It just advances its own clock and reinserts itself back into the future events queue.

Here’s the new one. It also updates its clock and inserts itself back into the future events queue, but in this case it implements a more interesting (though still mindlessly simple) behavior. It increments a location property which itself gets incremented, until it reaches a final offset, whereupon it counts down through a five-step wait process. Then it stops reinserting itself back into the future events queue.

In reality we would always create entities that carry out more interesting behaviors.

Having to rely on passive garbage collection to officially deallocate or “destroy” objects in JavaScript is kind of annoying but it is what it is. Doing demos in this language makes it easy to post working demos.

Here’s how the entities are initialized.

Here is the output generated:

104 minutes

entity 1 created at time 0
entity 2 created at time 0
entity 3 created at time 0
entity 1 updated at time 11
entity 3 updated at time 12 position: -40
entity 2 updated at time 13
entity 3 updated at time 19.009999999999998 position: -35
entity 1 updated at time 21
entity 2 updated at time 26
entity 3 updated at time 26.019999999999996 position: -30
entity 1 updated at time 31
entity 3 updated at time 33.029999999999994 position: -25
entity 2 updated at time 39
entity 3 updated at time 40.03999999999999 position: -20
entity 1 updated at time 41
entity 3 updated at time 47.04999999999999 position: -15
entity 1 updated at time 51
entity 2 updated at time 52
entity 3 updated at time 54.05999999999999 position: -10
entity 1 updated at time 61
entity 3 updated at time 61.069999999999986 position: -5
entity 2 updated at time 65
entity 3 waiting at time 68.07999999999998 wait count: 4
entity 1 updated at time 71
entity 3 waiting at time 75.08999999999999 wait count: 3
entity 2 updated at time 78
entity 1 updated at time 81
entity 3 waiting at time 82.1 wait count: 2
entity 3 waiting at time 89.11 wait count: 1
entity 2 updated at time 91
entity 1 updated at time 91
entity 3 waiting at time 96.12 wait count: 0
entity 1 terminated at time 101
entity 3 terminated at time 103.13000000000001
entity 2 terminated at time 104

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

Leave a Reply