{"id":1329,"date":"2017-01-18T22:55:52","date_gmt":"2017-01-19T03:55:52","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=1329"},"modified":"2017-02-03T12:52:09","modified_gmt":"2017-02-03T17:52:09","slug":"a-simple-discrete-event-simulation-part-73","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2017\/01\/18\/a-simple-discrete-event-simulation-part-73\/","title":{"rendered":"A Simple Discrete-Event Simulation: Part 73"},"content":{"rendered":"<p><iframe loading=\"lazy\" width=\"418px\" height=\"1050px\" src=\"https:\/\/www.rpchurchill.com\/demo\/des\/discrete-event-sim_20170118.html\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Today I added the Bag component (number 36 in the frame above), which you can think of as a parking lot.  It works largely the same way as a ProcessComponent but it doesn&#8217;t store its entities in a FIFO queue internally.  Rather, it defines a list of entities with one space for each possible entity up to the defined <code>maxCapacity<\/code> of the component.  An entity enters the component, gets assigned to the lowest empty index in the list, and begins working off its defined process time.  When the process time ends the entity is moved over to an exit queue, which is another list of entities within the Bag component.  The exit queue shows which entities are waiting to leave the Bag component, which is only relevant if the next downstream component is an exclusive one that is not currently open.  I added a downstream Process component to the model in order to visually demonstrate this behavior.<\/p>\n<p>The procedure for drawing the entities in the linked <code>displayElement<\/code> is a bit different than for the queue or process components.  The designer should specify more <code>entityLocs<\/code> spaces than the capacity of the component.  The first <code>maxCapacity<\/code> spaces are used to display the entities while they are &#8220;parked&#8221;.  Any additional spaces are used to display the entities waiting in the exit queue.  The Bag component in today&#8217;s version of the example model has a capacity of twelve and an additional five spaces reserved to show the exit queue.  If there are more entities in the internal exit queue than there are spaces left to display them then the extras do not get displayed.  One trick to make this more visually interesting, if it&#8217;s appropriate to the real process being modeled, is to assign the entity locations up to the Bag&#8217;s capacity in a semi-random order.  That way the entities look like they&#8217;re parking in a more naturalistic manner rather than obediently filling spaces in regimented order.  The border modeling programs I worked with at Regal Decision Systems (BorderWizard, CanSim, and SimFronteras) all allowed for this, and the effect was particularly striking in situations where there were large numbers of vehicles in a parking area.  The commercial parking area at Ambassador Bridge in Detroit used to fill up in a big way when many trucks needed to park so the drivers could attend to customs paperwork.  (That process has since been largely obviated by automating the paperwork so it&#8217;s completed offsite before the vehicle makes the crossing.)<\/p>\n<p>Having a Bag component feed directly into another component that&#8217;s exclusive should probably be rare, but the full behavior is included.  I&#8217;m thinking of my time analyzing and collecting data at dozens of border crossings where vehicles and pedestrians were almost always able to leave a Process or Bag (parking lot, literally) and go someplace else.  There was effectively a non-exclusive queue in front of most processes and parking lots.  That said, one can easily imagine a tightly coupled manufacturing process when internal queues have been squeezed out by design or after rearrangement following a Lean analysis.<\/p>\n<p>Here&#8217;s the code for the entire Bag component.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    function BagComponent(processTime, processTimeSwitch, maxCapacity, routingTable) {\r\n      if (typeof maxCapacity === \"undefined\") {maxCapacity = 1;}\r\n      if (typeof routingTable === \"undefined\") {routingTable = [1.0];}\r\n      \/\/generally exclusive, should always be fed by a queue or at least \"protected\" by a status-based diversion component\r\n      setOfComponents.push(this);\r\n      this.componentID = getNewComponentID();\r\n      this.componentType = \"Bag\";\r\n      this.componentName = \"Bag\";\r\n      this.componentGroup = \"Bag\";\r\n      this.exclusive = true;\r\n      this.routingMethod = 1;  \/\/1: one connection, 2: distribution, 3 routing\r\n      this.previousComponentList = [];\r\n      this.previousComponentCount = 0;\r\n      this.nextComponentList = [];\r\n      this.nextComponentCount = 0;\r\n      this.nextComponentIDList = [];\r\n      this.processTime = processTime;\r\n      this.processTimeSwitch = processTimeSwitch;\r\n      this.maxCapacity = maxCapacity;\r\n      this.savedDestination = -1;\r\n      this.previousComponentIndex = 0;\r\n      this.nextComponentIndex = 0;\r\n      this.entityQueue = [];\r\n      for (var i=0; i<maxCapacity; i++) {\r\n        this.entityQueue[i] = null;\r\n      }\r\n      this.exitQueue = [];\r\n      this.routingTable = routingTable;\r\n      this.openStatus = true;\r\n      this.entryTime = \"\";\r\n      this.entryEntityID = \"\";\r\n      this.exitTime = \"\";\r\n      this.exitEntityID = \"\";\r\n      this.exitResidenceTime = \"\";\r\n      this.countInBag = 0;\r\n      this.countInProcess = 0;\r\n      this.activity = \"\";\r\n      this.endEntryDisplayTime = 0;\r\n      this.endExitDisplayTime = 0;\r\n      this.endAllDisplayTime = 0;\r\n      this.displayDelay = 0;\r\n      this.graphic = null;\r\n\r\n      this.reset = function() {\r\n        this.previousComponentIndex = this.previousComponentCount - 1;\r\n        this.nextComponentIndex = this.nextComponentCount - 1;\r\n        this.entityQueue = [];\r\n        for (var i=0; i<maxCapacity; i++) {\r\n          this.entityQueue[i] = null;\r\n        }\r\n        this.exitQueue = [];\r\n        this.openStatus = true;\r\n        this.savedDestination = -1;\r\n        this.entryTime = \"\";\r\n        this.entryEntityID = \"\";\r\n        this.exitTime = \"\";\r\n        this.exitEntityID = \"\";\r\n        this.exitResidenceTime = \"\";\r\n        this.countInBag = 0;\r\n        this.countInProcess = 0;\r\n        this.activity = \"\";\r\n        this.endEntryDisplayTime = 0;\r\n        this.endExitDisplayTime = 0;\r\n        this.endAllDisplayTime = 0;\r\n      };\r\n      this.assignPreviousComponent = function(prev) {  \/\/TODO-: implement code that makes this actually work\r\n        this.previousComponentList.push(prev);\r\n        this.previousComponentCount++;\r\n        this.previousComponentIndex = this.previousComponentCount - 1;\r\n        \/\/TODO-: assign this automatically when upstream link and exclusive paths are required?\r\n      };\r\n      this.assignNextComponent = function(next) {  \/\/BagComponent\r\n        this.nextComponentList.push(next);\r\n        this.nextComponentCount++;\r\n        this.nextComponentIndex = this.nextComponentCount - 1;\r\n        next.assignPreviousComponent(this);\r\n        \/\/TODO-: automatically assign upstream link if downstream component is exclusive?\r\n      };\r\n      this.verifyLinks = function() {\r\n        var i;\r\n        var error = \"\";\r\n        if (this.nextComponentCount > 0) {\r\n          for (i = 0; i < this.nextComponentCount; i++) {  \/\/>\r\n            if (this.nextComponentList[i]) {  \/\/link exists\r\n              if (typeof this.nextComponentList[i] === \"object\") {  \/\/link points to an object\r\n                if (\"componentType\" in this.nextComponentList[i]) {  \/\/object contains member componentType\r\n                  if ((this.nextComponentList[i].componentType == \"Arrivals\") ||\r\n                      (this.nextComponentList[i].componentType == \"Entry\")) {\r\n                    error += this.componentType + \" comp. \" + this.componentID + \" next comp. list element \" + i + \" is not an allowed comp.\\n\";\r\n                  }\r\n                } else {\r\n                  \/\/linked object does not contain member componentType\r\n                  error += this.componentType + \" comp. \" + this.componentID + \" next comp. list item \" + i + \" does not have componentType\\n\";\r\n                }\r\n              } else {\r\n                \/\/link points to something that is not an object\r\n                error += this.componentType + \" comp. \" + this.componentID + \" next comp. list item \" + i + \" is not an object\\n\";\r\n              }\r\n            } else {\r\n              \/\/link that should exist does not\r\n              error += this.componentType + \" comp. \" + this.componentID + \" next comp. list item \" + i + \" does not exist\\n\";\r\n            }\r\n          }\r\n        } else {\r\n          error += this.componentType + \" comp. \" + this.componentID + \" has index of zero next components\\n\";\r\n        }\r\n        if (this.previousComponentCount > 0) {\r\n          for (i = 0; i < this.previousComponentCount; i++) {  \/\/>\r\n            if (this.previousComponentList[i]) {  \/\/link exists\r\n              if (typeof this.previousComponentList[i] === \"object\") {  \/\/link points to an object\r\n                if (\"componentType\" in this.previousComponentList[i]) {  \/\/object contains member componentType\r\n                  if ((this.previousComponentList[i].componentType == \"Arrivals\") ||\r\n                      (this.previousComponentList[i].componentType == \"Exit\")) {\r\n                    error += this.componentType + \" comp. \" + this.componentID + \" previous comp. list element \" + i + \" is not an allowed comp.\\n\";\r\n                  }\r\n                } else {\r\n                  \/\/linked object does not contain member componentType\r\n                  error += this.componentType + \" comp. \" + this.componentID + \" previous comp. list item \" + i + \" does not have componentType\\n\";\r\n                }\r\n              } else {\r\n                \/\/link points to something that is not an object\r\n                error += this.componentType + \" comp. \" + this.componentID + \" previous comp. list item \" + i + \" is not an object\\n\";\r\n              }\r\n            } else {\r\n              \/\/link that should exist does not\r\n              error += this.componentType + \" comp. \" + this.componentID + \" previous comp. list item \" + i + \" does not exist\\n\";\r\n            }\r\n          }\r\n        } else {\r\n          error += this.componentType + \" comp. \" + this.componentID + \" has index of zero previous components\\n\";\r\n        }\r\n        return error;\r\n      };\r\n\r\n      this.getNextComponentIDs = function() {\r\n        for (var i = 0; i < this.nextComponentCount; i++) {\r\n          if (this.nextComponentList[i].getComponentType() != \"Path\") {\r\n            this.nextComponentIDList[i] = this.nextComponentList[i].getComponentID();\r\n          } else {\r\n            this.nextComponentIDList[i] = this.nextComponentList[i].passComponentID();\r\n          }\r\n        }\r\n      };\r\n\r\n      this.getComponentID = function() {\r\n        return this.componentID;\r\n      };\r\n      this.getComponentType = function() {  \/\/BagComponent\r\n        return this.componentType;\r\n      };\r\n      this.getComponentName = function() {\r\n        return this.componentName;\r\n      };\r\n      this.setComponentName = function(componentName) {\r\n        this.componentName = componentName;\r\n      };\r\n      this.getComponentGroup = function() {\r\n        return this.componentGroup;\r\n      };\r\n      this.setComponentGroup = function(componentGroup) {\r\n        this.componentGroup = componentGroup;\r\n        addToGroupStatsNameListWrapper(componentGroup);\r\n      };\r\n      this.getExclusive = function() {\r\n        return this.exclusive;\r\n      };\r\n      this.setExclusive = function(exclusive) {\r\n        this.exclusive = exclusive;\r\n      };\r\n      this.getProcessTime = function() {\r\n        return this.processTime;\r\n      };\r\n      this.getProcessTimeSwitch = function() {\r\n        return this.processTimeSwitch;\r\n      };\r\n      this.setProcessTimeSwitch = function(processTimeSwitch) {\r\n        this.processTimeSwitch = processTimeSwitch;\r\n      };\r\n      this.getMaxCapacity = function() {\r\n        return this.maxCapacity;\r\n      };\r\n      this.setMaxCapacity = function(maxCapacity) {\r\n        this.maxCapacity = maxCapacity;\r\n      };\r\n      this.getOpenStatus = function() {\r\n        return this.openStatus;\r\n      };\r\n      this.setOpenStatus = function(openStatus) {\r\n        this.openStatus = openStatus;\r\n      };\r\n      this.getForwardAttemptTime = function() {\r\n        if (this.exitQueue.length > 0) {\r\n          return this.exitQueue[this.exitQueue.length - 1].getForwardAttemptTime();\r\n        } else {\r\n          return Infinity;\r\n        }\r\n      };\r\n      this.getRoutingMethod = function() {\r\n        return this.routingMethod;\r\n      };\r\n      this.setRoutingMethod = function(routingMethod) {\r\n        this.routingMethod = routingMethod;\r\n      };\r\n      this.getEntryTime = function() {\r\n        return this.entryTime;\r\n      };\r\n      this.getEntryEntityID = function() {\r\n        return this.entryEntityID;\r\n      };\r\n      this.getExitTime = function() {\r\n        return this.exitTime;\r\n      };\r\n      this.getExitEntityID = function() {\r\n        return this.exitEntityID;\r\n      };\r\n      this.getExitResidenceTime = function() {\r\n        return this.exitResidenceTime;\r\n      };\r\n      this.getCountInBag = function() {\r\n        return this.countInBag;\r\n      };\r\n      this.getCountInProcess = function() {\r\n        return this.countInProcess;\r\n      };\r\n      this.getActivity = function() {\r\n        return this.activity;\r\n      };\r\n      this.getEndEntryDisplayTime = function() {\r\n        return this.endEntryDisplayTime;\r\n      };\r\n      this.getEndExitDisplayTime = function() {\r\n        return this.endExitDisplayTime;\r\n      };\r\n      this.getEndAllDisplayTime = function() {\r\n        return this.endAllDisplayTime;\r\n      };\r\n\r\n      this.dataGroup = new DisplayGroup1();\r\n      this.defineDataGroup = function(displayDelay, x, y, vw, bc, vc, lc) {\r\n        this.displayDelay = displayDelay;\r\n        this.dataGroup.define(this.componentID, this.componentType, x, y, vw, bc, vc, lc);\r\n      };\r\n      this.dataGroup.addValue(this.entryEntityID, \"Entry ID\", \"integer\");\r\n      this.dataGroup.addValue(this.countInBag, \"# In Bag\", \"numdec\", \"integer\");\r\n      this.dataGroup.addValue(this.exitEntityID, \"Exit ID\", \"integer\");\r\n      this.dataGroup.addValue(this.exitResidenceTime, \"Resdnce Tm\", \"numdec\", 5);\r\n      this.dataGroup.addValue(this.activity, \"Activity\", \"text\");\r\n\r\n      this.assignDisplayValues = function() {\r\n        this.dataGroup.valueList[0].value = this.entryEntityID;\r\n        this.dataGroup.valueList[1].value = this.countInBag;\r\n        this.dataGroup.valueList[2].value = this.exitEntityID;\r\n        this.dataGroup.valueList[3].value = this.exitResidenceTime;\r\n        this.dataGroup.valueList[4].value = this.activity;\r\n        if (this.exclusive) {\r\n          if (this.openStatus) {\r\n            this.dataGroup.setBorderColor(\"#00FF00\");\r\n          } else {\r\n            this.dataGroup.setBorderColor(\"#FF0000\");\r\n          }\r\n        }\r\n      };\r\n      this.drawData = function() {  \/\/BagComponent\r\n        this.assignDisplayValues();\r\n        this.dataGroup.drawBasic();\r\n      };\r\n\r\n      this.defineGraphic = function(graphic) {\r\n        this.graphic = graphic;\r\n      };\r\n      this.updateGraphic = function() {\r\n        this.graphic.setTraverseValue(this.countInProcess);\r\n        this.graphic.setCountValue(this.countInBag);\r\n        \/\/if (this.exclusive) {\r\n        \/\/  if (this.openStatus) {\r\n        \/\/    this.graphic.setBorderColor(\"#00FF00\");\r\n        \/\/  } else {\r\n        \/\/    this.graphic.setBorderColor(\"#FF0000\");\r\n        \/\/  }\r\n        \/\/}\r\n      };\r\n\r\n      this.isOpen = function() {  \/\/BagComponent\r\n        if (this.exclusive) {\r\n          if (this.currentCount() < this.maxCapacity) {\r\n            this.openStatus = true;\r\n          } else {\r\n            this.openStatus = false;\r\n          }\r\n          for (var i = 0; i < this.previousComponentCount; i++) {\r\n            if (this.previousComponentList[i].getComponentType() == \"Path\") {\r\n              this.previousComponentList[i].setPreviousStatus(this.openStatus);  \/\/this may only be needed to determine open\/closed status for display, count <=> capacity used when something is trying to enter\r\n            }\r\n          }\r\n        }\r\n        return this.openStatus;  \/\/if not exclusive should be set to true by default\r\n      };\r\n      this.clearEntryDisplay = function() {\r\n        \/\/only clear display if a new one hasn't started a new timer\r\n        if (globalSimClock >= this.endEntryDisplayTime) {\r\n          this.entryTime = \"\";\r\n          this.entryEntityID = \"\";\r\n        }\r\n        if (globalSimClock >= this.endAllDisplayTime) {\r\n          this.activity = \"\";\r\n        }\r\n        \/\/displayProgressText(\"Bag entry \"+this.componentID+\" clears at time \"+globalSimClock.toFixed(6));\r\n      };\r\n      this.clearExitDisplay = function() {\r\n        \/\/only clear display if a new one hasn't started a new timer\r\n        if (globalSimClock >= this.endExitDisplayTime) {\r\n          this.exitTime = \"\";\r\n          this.exitEntityID = \"\";\r\n          this.exitResidenceTime = \"\";\r\n        }\r\n        if (globalSimClock >= this.endAllDisplayTime) {\r\n          this.activity = \"\";\r\n        }\r\n        \/\/displayProgressText(\"Bag exit \"+this.componentID+\" clears at time \"+globalSimClock.toFixed(6));\r\n      };\r\n      this.currentCount = function() {\r\n        var count = 0;\r\n        if (this.exclusive) {\r\n          \/\/start with entities already in component\r\n          count = this.countInBag;\r\n          \/\/add entities in feeding paths\r\n          for (var i = 0; i < this.previousComponentCount; i++) {\r\n            if (this.previousComponentList[i].componentType == \"Path\") {  \/\/TODO- consider adding test for whether path is boundary component for associated exclusive group of components \/\/do this using no-time\/no-space control component to define boundary\r\n              count += this.previousComponentList[i].currentCount();\r\n            }\r\n          }\r\n        }\r\n        return count;\r\n      };\r\n      this.pullFromPrevious = function() {  \/\/BagComponent\r\n        var oldest = this.previousComponentList[0].getForwardAttemptTime();\r\n        var oldestIndex = 0;\r\n        for (var i = 1; i < this.previousComponentCount; i++) {\r\n          var age = this.previousComponentList[i].getForwardAttemptTime();\r\n          if (age < oldest) {\r\n            oldestIndex = i;\r\n          }\r\n        }\r\n        if (this.previousComponentList[oldestIndex].getComponentType() != \"Path\") {\r\n          if (this.previousComponentList[oldestIndex].getComponentType() != \"Entry\") {\r\n            \/\/TODO: this should call forward entity in a way that ensures that previous component only sends entity to where it is requested and if one is available and if this is a legitimate destination\r\n            this.previousComponentList[oldestIndex].forwardEntity(this.componentID);\r\n          }\r\n        } else {\r\n          displayProgressText(\"Bag comp. \" + this.componentID + \" pulls from previous (\" + oldestIndex + \") at time \" + globalSimClock.toFixed(6));\r\n          this.previousComponentList[oldestIndex].pullFromPrevious(this.componentID);\r\n        }\r\n      };\r\n      this.nextOpen = function() {\r\n        var startIndex = this.nextComponentIndex;\r\n        var tempIndex = startIndex;\r\n        do {\r\n          tempIndex++;\r\n          if (tempIndex >= this.nextComponentCount) {\r\n            tempIndex = 0;\r\n          }\r\n          if (this.nextComponentList[tempIndex].isOpen()) {\r\n            \/\/open link found, update and return nextComponentIndex\r\n            return tempIndex;\r\n          }\r\n        } while (tempIndex != startIndex);\r\n        return -1;  \/\/no open links found, leave nextComponentIndex unchanged\r\n      };\r\n      this.processComplete = function(entity) {  \/\/BagComponent\r\n        this.countInProcess--;   \/\/TODO: ensure handled properly if process time is zero \/\/prob. not applicable\r\n        \/\/figure out which entity just finished processing\r\n        var tempID = entity.entityID;\r\n        this.exitQueue.unshift(entity);\r\n        this.entityQueue[entity.getLocalIndex()] = null;\r\n        entity.setForwardAttemptTime(globalSimClock);\r\n        displayProgressText(\"Bag comp. \" + this.componentID + \" entity: \" + tempID + \" processed at \" + globalSimClock.toFixed(6));\r\n        this.forwardEntity();  \/\/try to forward it\r\n      };\r\n      \/\/##default parameters##\r\n      \/\/this.forwardEntity = function(destIndex = -1) {  \/\/BagComponent\r\n      \/\/this.forwardEntity = function() {  \/\/BagComponent\r\n        \/\/\/var routingTable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1;\r\n      this.forwardEntity = function(destIndex) {  \/\/BagComponent\r\n        if (typeof destIndex === \"undefined\") {destIndex = -1;}\r\n        var dest = -1;\r\n        if (destIndex >= 0) {  \/\/pull request from a specific downstream component, must send entity there\r\n          if (this.routingMethod == 1) { \/\/single connection, nothing to do\r\n            dest = 0;\r\n          } else if (this.routingMethod == 2) { \/\/distribution, send to any request\r\n            dest = 0;\r\n            while ((this.nextComponentIDList[dest] != destIndex) && (dest < this.nextComponentCount)) {  \/\/second test should not be needed, loop can't fail to return valid result\r\n              dest++;\r\n            }\r\n          } else if (this.routingMethod == 3) {  \/\/model routing logic, TODO: don't forward if not desired destination\r\n            dest = 0;\r\n            while ((this.nextComponentIDList[dest] != destIndex) &#038;&#038; (dest < this.nextComponentCount)) {  \/\/second test should not be needed, loop can't fail to return valid result\r\n              dest++;\r\n            }\r\n          }\r\n          dummy2 = 0;\r\n        } else {\r\n          if (this.routingMethod == 1) {  \/\/single connection\r\n            if (this.nextComponentList[0].isOpen()) {\r\n              dest = 0;\r\n            }\r\n          } else if (this.routingMethod == 2) {  \/\/distribution\r\n            var nextIndex = this.nextOpen();\r\n            if (nextIndex >= 0) {\r\n              dest = nextIndex;\r\n              \/\/this.nextComponentIndex = dest;\r\n            }\r\n          } else if (this.routingMethod == 3) {  \/\/model routing logic\r\n            if (this.savedDestination >= 0) {\r\n              dest = this.savedDestination;\r\n            } else {\r\n              dest = 0;\r\n              var test = Math.random();\r\n              \/\/need access to entity type but can't pop it off queue here\r\n              var index = this.countInQueue - 1;\r\n              if (index >= 0) {\r\n                index = entityDiversionPercentIndex(this.entityQueue[index]);  \/\/get head item in queue and find out what type it is\r\n              } else {\r\n                index = 0;  \/\/nothing in queue, following code will work but nothing will be popped and processed below\r\n              }\r\n              while (test > this.routingTable[index][dest]) {\r\n                dest++;\r\n              }\r\n              if (dest <= this.nextComponentCount) {\r\n                if (!this.nextComponentList[dest].isOpen()) {\r\n                  dest = -1;\r\n                }\r\n              } else {\r\n                alert(\"Bag comp. tried to assign destination with too high of an index\")\r\n              }\r\n              if (dest >= 0) {\r\n                this.savedDestination = dest;  \/\/ensure that once destination is determined for this entity that we don't keep changing it\r\n              }\r\n            }\r\n          } else {  \/\/0 uninitialized or anything else\r\n            alert(\"comp. \" + this.componentID + \" incorrect routing method: \" + this.routingMethod);\r\n          }\r\n        }\r\n        if (dest >= 0) {\r\n          if (this.countInBag > this.countInProcess) {\r\n            var entity = this.exitQueue.pop();  \/\/TODO-: are we testing to ensure the next entity is really available\r\n            if (entity) {  \/\/TODO-: since we've tested above this should not be necessary\r\n              \/\/calculate how long item was in process\r\n              this.exitResidenceTime = globalSimClock - entity.getLocalEntryTime();\r\n              this.exitTime = globalSimClock;\r\n              this.exitEntityID = entity.entityID;\r\n              this.activity = \"forward entity\";\r\n              this.endExitDisplayTime = globalSimClock + this.displayDelay;\r\n              this.endAllDisplayTime = this.endExitDisplayTime;\r\n              advance(this.displayDelay, this, \"clearExitDisplay\");\r\n              displayProgressText(\"Bag comp. \" + this.componentID + \" forwards entity: \" + this.exitEntityID + \" at time \" + globalSimClock.toFixed(6));\r\n\r\n              this.countInBag--;\r\n              \/\/should be open now\r\n              if (this.exclusive) {\r\n                displayProgressText(\"Bag comp. \" + this.componentID + \" calls pull from previous at time \" + globalSimClock.toFixed(6));\r\n                if (!this.openStatus) {\r\n                  this.pullFromPrevious(); \/\/TODO: call this with a modest (~1 sec) delay to account for reaction time? \/\/may or may not successfully get an entity but should always be called\r\n                }\r\n              }\r\n              this.isOpen();\r\n              \/\/if (this.exclusive) {\r\n              \/\/  for (var i=0; i<this.previousComponentCount; i++) {\r\n              \/\/    if (this.previousComponentList[i].getComponentType() == \"Path\") {\r\n              \/\/      this.previousComponentList[i].setPreviousStatus(this.openStatus);  \/\/this may only be needed to determine open\/closed status for display, count <=> capacity used when something is trying to enter\r\n              \/\/    }\r\n              \/\/  }\r\n              \/\/}\r\n              \/\/this.nextComponentList[this.nextComponentIndex].receiveEntity(entity);\r\n              this.nextComponentIndex = dest;\r\n              this.savedDestination = -1;  \/\/clear old choice when entity successfully forwarded\r\n              this.nextComponentList[dest].receiveEntity(entity);\r\n              \/\/record stats\r\n              \/\/TODO: test to ensure not going to another component in the same componentGroup\r\n              \/\/TODO: do the same thing for the ProcessComponent\r\n              recordGroupStatsWrapper(this.componentGroup, entity.getComponentGroupEntryTime(), entity);\r\n            }\r\n          }\r\n        }\r\n      };\r\n      this.receiveEntity = function(entity) {  \/\/BagComponent\r\n        \/\/receive the entity\r\n        entity.setLocalEntryTime();  \/\/record time entity entered bag\r\n        if (entity.getComponentGroup() != this.componentGroup) {\r\n          entity.setComponentGroup(this.componentGroup);\r\n          entity.setComponentGroupEntryTime(globalSimClock);\r\n          recordGroupStatsSystemEntryWrapper(this.componentGroup,entity);\r\n        }\r\n        \/\/figure out which parking space to use\r\n        var i = 0;\r\n        while (this.entityQueue[i] != null) {\r\n          i++;\r\n        }\r\n        if (i < this.maxCapacity) {\r\n          this.entityQueue[i] = entity;\r\n          entity.setLocalIndex(i);\r\n        } else {  \/\/this shouldn't happen\r\n          alert(\"Bag comp. \"+this.componentID+\" over capacity at time \"+this.globalSimClock.toFixed(6));\r\n        }\r\n        entity.setForwardAttemptTime(Infinity);  \/\/TODO: figure out how to handle this\r\n        entity.setPermission(false);  \/\/entity has reached end of related components group, permission no longer matters\r\n        this.countInProcess++;\r\n        this.countInBag++;  \/\/TODO: handle if process time is zero?\r\n        this.isOpen();\r\n        \/\/display what was done\r\n        this.entryTime = globalSimClock;\r\n        this.entryEntityID = entity.entityID;\r\n        this.activity = \"receive entity\";\r\n        \/\/set timer to clear the display after a bit\r\n        this.endEntryDisplayTime = globalSimClock + this.displayDelay;\r\n        this.endAllDisplayTime = this.endEntryDisplayTime;\r\n        advance(this.displayDelay, this, \"clearEntryDisplay\");\r\n        \/\/set timer for the process duration\r\n        var pTime = this.processTime[entityProcessTimeIndex(entity,this.processTimeSwitch)];\r\n        advance(pTime, this, \"processComplete\",entity);\r\n        displayProgressText(\"Bag comp. \" + this.componentID + \" receives entity: \" + this.entryEntityID + \" at time \" + globalSimClock.toFixed(6));\r\n      };\r\n      this.activate = function(nextState, entity2) {\r\n        if (nextState == \"clearEntryDisplay\") {\r\n          this.clearEntryDisplay();\r\n        } else if (nextState == \"clearExitDisplay\") {\r\n          this.clearExitDisplay();\r\n        } else if (nextState == \"processComplete\") {\r\n          this.processComplete(entity2);\r\n        } else {\r\n          errorUndefinedAdvanceState(this.entityID, this.nextState);\r\n        }\r\n      };  \/\/this.activate\r\n\r\n    }  \/\/BagComponent\r\n<\/pre>\n<p>I had to add the parameter <code>processTimeSwitch<\/code> to allow different type indices to be used for determining the process time for each entity type.  The process times are provided to the component as an array of values meant to apply to different types using the function <code>entityProcessTimeIndex(entity,this.processTimeSwitch)<\/code>.  In the primary Process components I wanted the types to be based on a combination of the entity's residency and process speed.  In the secondary group I specified the same time for all types so it didn't matter what indices were used.  I noticed the problem when I was looking at the report output for the \"Parking Lot\" group when there was no Process component in place after the Bag component.  I had defined process times of 30, 50, and 80 units envisioning that they would apply to citizens, LPRs, and visitors, respectively, but the process times reported for those types varied in ways I didn't expect.  Then I remembered how the <code>entityProcessTimeIndex<\/code> function worked and added a switch to it to allow different indices to be generated based on an entity's properties.  That index had to be added to the component definition for both the Bag and Process components.<\/p>\n<p>Since JavaScript is <em>so<\/em> modular it should be possible to supply a custom function to the <code>processTime<\/code> parameter that embodies any combination of indices and process time values.  I'll experiment with that tomorrow.<\/p>\n<p>Here's the updated code for drawing the display element.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n      this.drawEntities = function() {\r\n        var i;\r\n        var drawCount;\r\n        var drawColor;\r\n        \/\/if (this.parent.getComponentType() == \"Path\") {\r\n        if (this.isPath) {\r\n          drawCount = this.parent.entityQueue.length;\r\n          for (i=0; i<drawCount; i++) {\r\n            var location = this.parent.entityQueue[i].getLocation();\r\n            drawNode(location.x,location.y,5,this.readyColor);\r\n            drawNode(location.x,location.y,3,this.parent.entityQueue[i].getEntityColor())\r\n          }\r\n        } else if (this.parent.getComponentType() == \"Bag\") {  \/\/new section\r\n          drawCount = this.parent.maxCapacity;  \/\/should be < this.entityLocs.length\r\n          drawColor = this.waitingColor;\r\n          for (i=0; i<drawCount; i++) {\r\n            if (this.parent.entityQueue[i] != null) {\r\n              var xx = this.entityLocs[i].x;\r\n              var yy = this.entityLocs[i].y;\r\n              drawNode(this.x1+xx,this.y1+yy,5,drawColor);\r\n              drawNode(this.x1+xx,this.y1+yy,3,this.parent.entityQueue[i].getEntityColor());\r\n            }\r\n          }\r\n          drawCount = this.entityLocs.length - this.parent.maxCapacity;\r\n          if (this.parent.exitQueue.length < drawCount) {\r\n            drawCount = this.parent.exitQueue.length;\r\n          }\r\n          drawColor = this.readyColor;\r\n          for (i=0; i<drawCount; i++) {\r\n            \/\/locs for exit queue in order from maxCapacity\r\n            var xx = this.entityLocs[this.parent.maxCapacity + i].x;\r\n            var yy = this.entityLocs[this.parent.maxCapacity + i].y;\r\n            drawNode(this.x1+xx,this.y1+yy,5,drawColor);\r\n            \/\/exit queue items in reverse order\r\n            drawNode(this.x1+xx,this.y1+yy,3,this.parent.exitQueue[this.parent.exitQueue.length-1-i].getEntityColor());            \r\n          }\r\n        } else if (this.parent.getComponentType() != \"Arrivals\") {\r\n          var qCount = this.countValue - this.traverseValue;\r\n          if (this.countValue > this.locsCount) {\r\n            drawCount = this.locsCount;\r\n          } else {\r\n            drawCount = this.countValue;\r\n          }\r\n          for (i=0; i<drawCount; i++) {\r\n            if (i < qCount) {\r\n              drawColor = this.readyColor;\r\n            } else {\r\n              drawColor = this.waitingColor;\r\n            }\r\n            var xx = this.entityLocs[i].x;\r\n            var yy = this.entityLocs[i].y;\r\n            drawNode(this.x1+xx,this.y1+yy,5,drawColor);\r\n            drawNode(this.x1+xx,this.y1+yy,3,this.parent.entityQueue[i].getEntityColor());\r\n          }\r\n        }\r\n      };\r\n<\/pre>\n<p>I tested the Bag component with and without the subsequent Process component and everything worked as it should procedurally.  I also assigned the Bag and Process components to a <code>componentGroup<\/code> called \"Parking Lot\" and the reporting mechanism picked up the new group and reported it like a champ.  However, I noticed that there were negative entity counts during some time intervals in the \"Parking Lot\" section of the report for each entity type.  I traced this down to the erroneous assumption that exiting a Process or Bag component should always trigger recording an exit from a component group.  This worked fine when the groups start with a Queue component and end with a Process component, but not so well when a group contained multiple Process and Bag components in series.  I therefore have to add a test to see whether an entity is truly exiting a component group before triggering the exit reporting mechanism.  That will be another part of tomorrow's work.<\/p>\n<p>Last but not least I'll update the framework so it can be allowed to run until all entities have cleared the system, after a specified minimum running time.  This is not always the desired behavior for analysis or reporting, so a switch setting will have to be included as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I added the Bag component (number 36 in the frame above), which you can think of as a parking lot. It works largely the same way as a ProcessComponent but it doesn&#8217;t store its entities in a FIFO queue &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2017\/01\/18\/a-simple-discrete-event-simulation-part-73\/\">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,136],"_links":{"self":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1329"}],"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=1329"}],"version-history":[{"count":7,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1329\/revisions"}],"predecessor-version":[{"id":1395,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1329\/revisions\/1395"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}