-
Recent Posts
Recent Comments
- R.P. Churchill on TWSL Series 07: Discovery and Data Collection
- R.P. Churchill on A Simulationist’s Framework for Business Analysis: Round Two
- LN on A Simulationist’s Framework for Business Analysis: Round Two
- R.P. Churchill on Starting to Learn About the Java Memory Model
- R.P. Churchill on Multidimensional Arrays in Javascript
Categories
Meta
October 2025 M T W T F S S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Tag Archives: JavaScript
A Simple Discrete-Event Simulation: Part 12
Today I wanted to start constructing an Entry component. I created a very simple on that generates modeled entities are regular, defined intervals over the duration of the simulation run. Here’s the relevant code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//time to end the entire simulation var endSimTime = 1440.0; //time increments in minutes, run for 24 hours //collection of all entities in the system var setOfEntities = new Array(); //Entry component function entryComponent(arrivalsPerHour) { //initially assume it just generates entity1s that disappear after a single cycle this.entityID = getNewID(); this.initialTime = 0.0; this.incrementTime = 60.0 / arrivalsPerHour; this.endTime = endSimTime; this.nextState = "increment"; feq.newItem(globalSimClock,this); //assume all components created at time zero this.generateNewEntity = function() { var newEntity = new entity1(globalSimClock,2.0,globalSimClock+1.0); setOfEntities.push(newEntity); } this.activate = function() { if (this.nextState == "increment") { this.generateNewEntity(); //<<<< this one line does all the meaningful work <<<< displayProgressText("Entry component "+this.entityID+" generates new entity at time "+globalSimClock+"<br />"); if (globalSimClock + this.incrementTime >= this.endTime) { this.nextState = "destroy"; } advance(this.incrementTime); } else if (this.nextState == "destroy") { displayProgressText("Entry component "+this.entityID+" terminated at time "+globalSimClock+"<br />"); } else { alert("Entry component "+this.entityID+" went into undefined state"); displayProgressText("entity "+this.entityID+" in undefined state at time "+globalSimClock+"<br />"); } } }; //entryComponent var entry1 = new entryComponent(2.0); |
This generates the following output. … Continue reading
A Simple Discrete-Event Simulation: Part 10
Today I defined some new entities that better illustrate the operation of the wait mechanism and the current events queue, which I have modified so it scans the list of current items repeatedly until no conditions are met. Here’s the … Continue reading
A Simple Discrete-Event Simulation: Part 9
It’s one thing when you place an entity back in the future events queue at a specific absolute or relative time, but it’s quite another if you want an entity to become active when a certain condition–or set of conditions–becomes … Continue reading
A Simple Discrete-Event Simulation: Part 8
Today I automated the generation of entity IDs. First we create a basic counter.
1 2 3 4 |
var globalIDCounter = 0; function getNewID() { return ++globalIDCounter; } |
Then we change the entity declarations to remove the explicit assignment of IDs as we have been doing.
1 2 |
function entity1(initialTime,incrementTime,endTime) { this.entityID = getNewID(); |
That was pretty mindless, so let’s … Continue reading
A Simple Discrete-Event Simulation: Part 7
Today we’ll do some more streamlining. We’ll start by adding a new function to the futureEventsQueue object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
this.newItem = function(time,entity) { //create futureEventItem var feqItem = new futureEventItem(time,"absolute",entity); this.insertTime = feqItem.getActivationTime(); globalInsertTime = this.insertTime; if (this.feqSize == 0) { this.feq[0] = feqItem; this.feqSize++; console.log("Array size: "+this.feq.length); } else { //find index of feq item to insert before var insertIndex = this.feq.findIndex(this.findLaterTime); //insert the element if (insertIndex < 0) { insertIndex = this.feq.length; } this.feq.splice(insertIndex,0,feqItem); this.feqSize++; console.log("Array size: "+this.feq.length); } }; |
Then, instead of having to specify the “absolute” (vs. “advance”) flag in the initialization of a new entity object, we know we’ll … Continue reading
A Simple Discrete-Event Simulation: Part 6
Now that I’m sure the basics are working the way I’d like I want to see if I can streamline things some more. The first thing to do is recognize that the next item being pulled off the future events … Continue reading
A Simple Discrete-Event Simulation: Part 5
Adding a second entity works correctly.
1 2 3 |
var entityA = new entity(1,11.0,10.0,100.0); var entityB = new entity(2,13.0,13.0,100.0); |
104 minutes entity 1 created at time 0 entity 2 created at time 0 entity 1 updated at time 11 entity 2 updated at time 13 entity 1 updated at time 21 entity … Continue reading
A Simple Discrete-Event Simulation: Part 4
Here is code that actually runs. It creates a single entity, then updates it in a loop until the ending time is exceeded. The next steps will be to see how it works with multiple entities of the same kind … Continue reading
A Simple Discrete-Event Simulation: Part 3
Here is an entity object that knows how to be created and activated from external calls. It updates itself and puts itself back into the future events queue is appropriate. This is in keeping with my original concept of how … Continue reading
A Simple Discrete-Event Simulation: Part 2
Today I’ve added the ability to retrieve the first item from the future events queue, which is the next item to process and that with the lowest activation time. In this case I just update the item’s time when it’s … Continue reading