Simul8 Tips, Technical Corner

Using Visual Logic with work items

Are you using Visual Logic to move work items? Don’t make this common mistake!

Once you start using Visual Logic (VL) you will be tempted to do everything using VL, even the simplest of commands. That is all well and good and will make your simulations very versatile. However, if you are using VL to loop through a queue and move work items make sure your loop goes through all work items.

Let me explain; say, for example, you have a queue with 7 work items arranged as follows:

using VL for work items

The aim of the loop is to go through the work items in the queue and move WI-3, WI-4 & WI-6, to another queue. The loop counter will go through positions #1, #2, #3, …, #Queue.Count Contents. When the loop reaches position #3, it will take WI-3 out:

loop counter

This will move the work items one position down in the queue. This means that WI-4 is now in position #3. The loop will continue on to the next position, #4, skipping WI-4 which is now in position #3. To avoid this you need to tell the loop to readjust the position after moving a work item. Let’s call it CountMoves.

So, instead of the loop counter going through positions #1, #2, #3, …, #Queue.CountContents, it should go through positions #1-CountMoves, #2-CountMoves, #3-CountMoves, … #Queue.CountContents-CountMoves. In other words, the CountMoves will be zero for the first two 3 iterations, #1-0, #2-0, #3-0, then as WI-3 moves we adjust CountMoves = 1, and the 4th iteration will be #4-1, and the new sequence will be #1, #2, #3, #3. In the same way because we will move WI-4, which is now in position #3, the new CountMoves = 2, and the fifth iteration will be #5-2 and the new sequence will be #1, #2, #3, #3, #3, #4, …, #Queue.CountContents – CountMoves.

loop counter

One more thing to consider in your loop is the limit, or the number of the contents in the queue. When we started the loop the queue had 7 work items, and therefore we set the queue to go through 7 iterations. When we moved the WI-3 and WI-4 from the queue, the queue now holds 5 WI and therefore will end the loop early (after the fifth iteration), and will not move WI-6.

using VL for work items

To solve this we need to change the loop parameter to account for the moved work items, instead of saying Loop from 1 to Queue.CountContents we include Loop from 1 to Queue.CountContents+CountMoves.

This is how the code should look in VL: