{"id":963,"date":"2016-09-12T23:48:00","date_gmt":"2016-09-13T04:48:00","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=963"},"modified":"2016-09-13T12:07:48","modified_gmt":"2016-09-13T17:07:48","slug":"a-simple-discrete-event-simulation-part-8","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/09\/12\/a-simple-discrete-event-simulation-part-8\/","title":{"rendered":"A Simple Discrete-Event Simulation: Part 8"},"content":{"rendered":"<p>Today I automated the generation of entity IDs.<\/p>\n<p>First we create a basic counter.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    var globalIDCounter = 0;\r\n    function getNewID() {\r\n      return ++globalIDCounter;\r\n    }\r\n<\/pre>\n<p>Then we change the entity declarations to remove the explicit assignment of IDs as we have been doing.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    function entity1(initialTime,incrementTime,endTime) {\r\n      this.entityID = getNewID();\r\n<\/pre>\n<p>That was pretty mindless, so let&#8217;s also add a new type of entity, following the form of the original one.  Here&#8217;s the original.  It just advances its own clock and reinserts itself back into the future events queue.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    function entity1(initialTime,incrementTime,endTime) {\r\n      this.entityID = getNewID();\r\n      this.initialTime = initialTime;\r\n      this.incrementTime = incrementTime;\r\n      this.endTime = endTime;\r\n      this.nextState = \"increment\";\r\n      feq.newItem(initialTime,this);\r\n      displayProgressText(\"entity \"+this.entityID+\" created at time \"+globalSimClock+\"<br \/>\");\r\n\r\n      this.activate = function() {\r\n        if (this.nextState == \"increment\") {\r\n          displayProgressText(\"entity \"+this.entityID+\" updated at time \"+globalSimClock+\"<br \/>\");\r\n          if (globalSimClock + this.incrementTime >= this.endTime) {\r\n            this.nextState = \"destroy\";\r\n          }\r\n          advance(this.incrementTime);\r\n        } else if (this.nextState == \"destroy\") {\r\n          displayProgressText(\"entity \"+this.entityID+\" terminated at time \"+globalSimClock+\"<br \/>\");\r\n        } else {\r\n          alert(\"entity \"+this.entityID+\" went into undefined state\");\r\n          displayProgressText(\"entity \"+this.entityID+\" in undefined state at time \"+globalSimClock+\"<br \/>\");\r\n        }\r\n      };  \/\/this.activate      \r\n    };  \/\/entity\r\n<\/pre>\n<p>Here&#8217;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.<\/p>\n<p>In reality we would always create entities that carry out more interesting behaviors.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    function entity2(initialTime,initialPosition,speed,endPosition) {\r\n      this.entityID = getNewID();\r\n      this.initialTime = initialTime;\r\n      this.currentPosition = initialPosition;\r\n      this.speed = speed;\r\n      this.endPosition = endPosition;\r\n      this.nextState = \"go_forward\";\r\n      this.waitCountdown = 5;\r\n      feq.newItem(initialTime,this);\r\n      displayProgressText(\"entity \"+this.entityID+\" created at time \"+globalSimClock+\"<br \/>\");\r\n      this.activate = function() {\r\n        if (this.nextState == \"go_forward\") {\r\n          displayProgressText(\"entity \"+this.entityID+\" updated at time \"+globalSimClock+\" position: \"+this.currentPosition+\"<br \/>\");\r\n          this.currentPosition += this.speed;\r\n          if (this.currentPosition >= this.endPosition) {\r\n            if (this.currentPosition > this.endPosition) {\r\n              this.currentPosition = this.endPosition;\r\n            }\r\n            this.nextState = \"wait\";\r\n          }\r\n          advance(7.01);\r\n        } else if (this.nextState == \"wait\") {\r\n          this.waitCountdown--;\r\n          displayProgressText(\"entity \"+this.entityID+\" waiting at time \"+globalSimClock+\" wait count: \"+this.waitCountdown+\"<br \/>\");\r\n          if (this.waitCountdown <= 0.0) {\r\n            this.nextState = \"destroy\";\r\n          }\r\n          advance(7.01);\r\n        } else if (this.nextState == \"destroy\") {\r\n          displayProgressText(\"entity \"+this.entityID+\" terminated at time \"+globalSimClock+\"<br \/>\");\r\n        } else {\r\n          alert(\"entity \"+this.entityID+\" went into undefined state\");\r\n          displayProgressText(\"entity \"+this.entityID+\" in undefined state at time \"+globalSimClock+\"<br \/>\");\r\n        }\r\n      };  \/\/this.activate      \r\n    };  \/\/entity\r\n<\/pre>\n<p>Having to rely on passive garbage collection to officially deallocate or &#8220;destroy&#8221; 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.<\/p>\n<p>Here&#8217;s how the entities are initialized.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    var entityA = new entity1(11.0,10.0,100.0);\r\n    var entityB = new entity1(13.0,13.0,100.0);\r\n    var entityC = new entity2(12.0,-40,5,-1);\r\n<\/pre>\n<p>Here is the output generated:<br \/>\n<code><br \/>\n104 minutes<\/p>\n<p>entity 1 created at time 0<br \/>\nentity 2 created at time 0<br \/>\nentity 3 created at time 0<br \/>\nentity 1 updated at time 11<br \/>\nentity 3 updated at time 12 position: -40<br \/>\nentity 2 updated at time 13<br \/>\nentity 3 updated at time 19.009999999999998 position: -35<br \/>\nentity 1 updated at time 21<br \/>\nentity 2 updated at time 26<br \/>\nentity 3 updated at time 26.019999999999996 position: -30<br \/>\nentity 1 updated at time 31<br \/>\nentity 3 updated at time 33.029999999999994 position: -25<br \/>\nentity 2 updated at time 39<br \/>\nentity 3 updated at time 40.03999999999999 position: -20<br \/>\nentity 1 updated at time 41<br \/>\nentity 3 updated at time 47.04999999999999 position: -15<br \/>\nentity 1 updated at time 51<br \/>\nentity 2 updated at time 52<br \/>\nentity 3 updated at time 54.05999999999999 position: -10<br \/>\nentity 1 updated at time 61<br \/>\nentity 3 updated at time 61.069999999999986 position: -5<br \/>\nentity 2 updated at time 65<br \/>\nentity 3 waiting at time 68.07999999999998 wait count: 4<br \/>\nentity 1 updated at time 71<br \/>\nentity 3 waiting at time 75.08999999999999 wait count: 3<br \/>\nentity 2 updated at time 78<br \/>\nentity 1 updated at time 81<br \/>\nentity 3 waiting at time 82.1 wait count: 2<br \/>\nentity 3 waiting at time 89.11 wait count: 1<br \/>\nentity 2 updated at time 91<br \/>\nentity 1 updated at time 91<br \/>\nentity 3 waiting at time 96.12 wait count: 0<br \/>\nentity 1 terminated at time 101<br \/>\nentity 3 terminated at time 103.13000000000001<br \/>\nentity 2 terminated at time 104<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I automated the generation of entity IDs. First we create a basic counter. var globalIDCounter = 0; function getNewID() { return ++globalIDCounter; } Then we change the entity declarations to remove the explicit assignment of IDs as we have &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/09\/12\/a-simple-discrete-event-simulation-part-8\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[121,49],"_links":{"self":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/963"}],"collection":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/comments?post=963"}],"version-history":[{"count":1,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":964,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/963\/revisions\/964"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}