{"id":1000,"date":"2016-09-22T23:16:13","date_gmt":"2016-09-23T04:16:13","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=1000"},"modified":"2016-09-23T23:55:21","modified_gmt":"2016-09-24T04:55:21","slug":"a-simple-discrete-event-simulation-part-14","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/09\/22\/a-simple-discrete-event-simulation-part-14\/","title":{"rendered":"A Simple Discrete-Event Simulation: Part 14"},"content":{"rendered":"<p>Today I modified the entry component so it creates new entities according to a supplied schedule.  This can be made as complex as we&#8217;d like but let&#8217;s keep it simple for now:  the input parameters are the duration of each scheduled block (in minutes) and the array of arrivals itself.<\/p>\n<p>The entry object keeps a running index into the schedule array, a new value of which is read every 30 minutes.  The <code>increment<\/code> method generates new entities spread even across the time span.  It does this by dividing the block duration by the number of arrivals plus one, and then generating a new arrival on each increment (one arrival leads to two spans which leads to an arrival at 15 minutes, two arrivals leads to three spans which leads to arrivals at ten and twenty minutes, three arrivals leads to four spans which lead to arrivals every 7.5 minutes).  No arrivals happen at the beginning or end of the schedule block.<\/p>\n<p>Here&#8217;s the code for the new entry component:<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    \/\/Entry component\r\n    function entryComponent(scheduleBlockMinutes,scheduleArray) {\r\n      \/\/initially assume it just generates entity1s that disappear after a single cycle\r\n      this.entityID = getNewID();\r\n\r\n      this.initialTime = 0.0;\r\n      this.incrementTime = scheduleBlockMinutes;\r\n      this.arrayIndex = 0;\r\n      this.endTime = endSimTime;\r\n      this.nextState = \"increment\";\r\n      feq.newItem(globalSimClock,this);  \/\/assume all components created at time zero\r\n      this.generateNewEntity = function(start,increment,end) {\r\n        var newEntity = new entity1(start,increment,end);\r\n        setOfEntities.push(newEntity);\r\n      }\r\n      this.increment = function() {  \/\/should be called at the beginning of every half-hour block\r\n        \/\/get arrival count per array index\r\n        var arrivals = scheduleArray[this.arrayIndex];\r\n        if (arrivals > 0) {\r\n          \/\/distribute arrivals evenly across the time span\r\n          var increment = scheduleBlockMinutes \/ (arrivals + 1);\r\n          var arrivalBump = 0.0;\r\n          for (var i=0; i<arrivals; i++) {\r\n            arrivalBump += increment;\r\n            this.generateNewEntity(globalSimClock + arrivalBump, 2.0, globalSimClock + arrivalBump + 1.0);\r\n          }\r\n        }\r\n        this.arrayIndex++;\r\n        displayProgressText(\"Entry component \"+this.entityID+\" generates \"+arrivals+\" new entities 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      }\r\n      this.destroy = function() {\r\n        \/\/this.flagPointer.setFlag(true);\r\n        displayProgressText(\"Entry component \"+this.entityID+\" terminated at time \"+globalSimClock+\"<br \/>\");\r\n      }\r\n      this.activate = function() {\r\n        if (this.nextState == \"increment\") {\r\n          this.increment();\r\n        } else if (this.nextState == \"destroy\") {\r\n          this.destroy();\r\n        } else {\r\n          errorUndefinedAdvanceState(this.entityID,this.nextState);\r\n        }      \r\n      }\r\n    };  \/\/entryComponent\r\n<\/pre>\n<p>And here&#8217;s the array definition and the call to create the entry component.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    \/\/time to end the entire simulation, defined before the entry component\r\n    var endSimTime = 360.0;  \/\/time increments in minutes, run for 6 hours\r\n\r\n    \/\/schedule for six hours of arrivals in half-hour blocks\r\n    var arrivalSchedule = [0,0,1,3,4,4,5,6,3,2,1,0]; \r\n    \r\n    var entry1 = new entryComponent(30.0,arrivalSchedule);\r\n<\/pre>\n<p>This works as expected, as demonstrated by the output:<\/p>\n<p><code><br \/>\n360.000000 minutes<\/p>\n<p>Entry component 1 generates 0 new entities at time 0<br \/>\nEntry component 1 generates 0 new entities at time 30<br \/>\nentity 2 created at time 60<br \/>\nEntry component 1 generates 1 new entities at time 60<br \/>\nentity 2 updated at time 75<br \/>\nentity 2 terminated at time 77<br \/>\nentity 3 created at time 90<br \/>\nentity 4 created at time 90<br \/>\nentity 5 created at time 90<br \/>\nEntry component 1 generates 3 new entities at time 90<br \/>\nentity 3 updated at time 97.5<br \/>\nentity 3 terminated at time 99.5<br \/>\nentity 4 updated at time 105<br \/>\nentity 4 terminated at time 107<br \/>\nentity 5 updated at time 112.5<br \/>\nentity 5 terminated at time 114.5<br \/>\nentity 6 created at time 120<br \/>\nentity 7 created at time 120<br \/>\nentity 8 created at time 120<br \/>\nentity 9 created at time 120<br \/>\nEntry component 1 generates 4 new entities at time 120<br \/>\nentity 6 updated at time 126<br \/>\nentity 6 terminated at time 128<br \/>\nentity 7 updated at time 132<br \/>\nentity 7 terminated at time 134<br \/>\nentity 8 updated at time 138<br \/>\nentity 8 terminated at time 140<br \/>\nentity 9 updated at time 144<br \/>\nentity 9 terminated at time 146<br \/>\nentity 10 created at time 150<br \/>\nentity 11 created at time 150<br \/>\nentity 12 created at time 150<br \/>\nentity 13 created at time 150<br \/>\nEntry component 1 generates 4 new entities at time 150<br \/>\nentity 10 updated at time 156<br \/>\nentity 10 terminated at time 158<br \/>\nentity 11 updated at time 162<br \/>\nentity 11 terminated at time 164<br \/>\nentity 12 updated at time 168<br \/>\nentity 12 terminated at time 170<br \/>\nentity 13 updated at time 174<br \/>\nentity 13 terminated at time 176<br \/>\nentity 14 created at time 180<br \/>\nentity 15 created at time 180<br \/>\nentity 16 created at time 180<br \/>\nentity 17 created at time 180<br \/>\nentity 18 created at time 180<br \/>\nEntry component 1 generates 5 new entities at time 180<br \/>\nentity 14 updated at time 185<br \/>\nentity 14 terminated at time 187<br \/>\nentity 15 updated at time 190<br \/>\nentity 15 terminated at time 192<br \/>\nentity 16 updated at time 195<br \/>\nentity 16 terminated at time 197<br \/>\nentity 17 updated at time 200<br \/>\nentity 17 terminated at time 202<br \/>\nentity 18 updated at time 205<br \/>\nentity 18 terminated at time 207<br \/>\nentity 19 created at time 210<br \/>\nentity 20 created at time 210<br \/>\nentity 21 created at time 210<br \/>\nentity 22 created at time 210<br \/>\nentity 23 created at time 210<br \/>\nentity 24 created at time 210<br \/>\nEntry component 1 generates 6 new entities at time 210<br \/>\nentity 19 updated at time 214.28571428571428<br \/>\nentity 19 terminated at time 216.28571428571428<br \/>\nentity 20 updated at time 218.57142857142858<br \/>\nentity 20 terminated at time 220.57142857142858<br \/>\nentity 21 updated at time 222.85714285714286<br \/>\nentity 21 terminated at time 224.85714285714286<br \/>\nentity 22 updated at time 227.14285714285714<br \/>\nentity 22 terminated at time 229.14285714285714<br \/>\nentity 23 updated at time 231.42857142857142<br \/>\nentity 23 terminated at time 233.42857142857142<br \/>\nentity 24 updated at time 235.71428571428572<br \/>\nentity 24 terminated at time 237.71428571428572<br \/>\nentity 25 created at time 240<br \/>\nentity 26 created at time 240<br \/>\nentity 27 created at time 240<br \/>\nEntry component 1 generates 3 new entities at time 240<br \/>\nentity 25 updated at time 247.5<br \/>\nentity 25 terminated at time 249.5<br \/>\nentity 26 updated at time 255<br \/>\nentity 26 terminated at time 257<br \/>\nentity 27 updated at time 262.5<br \/>\nentity 27 terminated at time 264.5<br \/>\nentity 28 created at time 270<br \/>\nentity 29 created at time 270<br \/>\nEntry component 1 generates 2 new entities at time 270<br \/>\nentity 28 updated at time 280<br \/>\nentity 28 terminated at time 282<br \/>\nentity 29 updated at time 290<br \/>\nentity 29 terminated at time 292<br \/>\nentity 30 created at time 300<br \/>\nEntry component 1 generates 1 new entities at time 300<br \/>\nentity 30 updated at time 315<br \/>\nentity 30 terminated at time 317<br \/>\nEntry component 1 generates 0 new entities at time 330<br \/>\nEntry component 1 terminated at time 360<br \/>\n<\/code><\/p>\n<p>Again, there are a lot of ways to make this mechanism more complicated.  We could define several types of arrivals, which would involve a more complex data structure and more involved calculations.  We could define different ways to distribute the arrivals over each time span (which I will discuss next week).  We could have this component generate entries for the entire simulation and then use a different data structure to define how arrivals are distributed over multiple locations where the entries actually occur.  Conversely, we could have a separate entry component and arrival definition for each entry location, individually.  The possibilities are endless.<\/p>\n<p>The main thing to note is that, depending on how it&#8217;s implemented, the entry component may be less a part of the process being modeled then it is a meta-component of the simulation.  The mechanisms can and usually do end up being co-mingled.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I modified the entry component so it creates new entities according to a supplied schedule. This can be made as complex as we&#8217;d like but let&#8217;s keep it simple for now: the input parameters are the duration of each &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/09\/22\/a-simple-discrete-event-simulation-part-14\/\">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\/1000"}],"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=1000"}],"version-history":[{"count":1,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1000\/revisions"}],"predecessor-version":[{"id":1001,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1000\/revisions\/1001"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}