variablesparametersbranchanylogicstatechart

Anylogic: how to compare parameter and variable value within statechart?


In my Anylogic model I have a hub which can store 5 containers. So it has a capacity parameter with value 5. I also have given it a variable with the numberOfContainers stored at the hub at that moment. When I run the model I see that the variable works (it changes over time to the number of containers that is stored at that moment).

Now I want another agent in my model to make a decision based on whether the capacity of the hub is reached at that moment (within its statechart). I tried to create a branche with the following condition: main.hub.numberOfContainers > main.hub.capacity but it doesn't work, the statechart acts like the capacity is never reached, even if the number of containers is much higher than the capacity. Does anybody know how to make this work?


Solution

  • Typically, condition branches are tricky because the condition may not be evaluated at the time you want it to be. Here is an example.

    The model may have missed evaluating the condition at time (n+1) which is why your transition would not be triggered.

    To address this issue, I have 3 possible suggestions:

    1. Do not use a condition transition. Instead, use a message. For example, if you are storing the containers in a queue, then, on the "On Enter" and "On Exit" fields of the queue, add the condition:

       if(queue.size >= main.hub.numberOfContainers)
         <send msg to the statechart>
      
    2. Use a cyclic event to check if the condition is met every second or millisecond or whatever time period makes sense to you. When the condition is met, send a message to trigger the transition. But the problem with this method is that it might slow down your model with poor performance.

    3. Use the onChange() function. This function is used to signal to your model that a change happened and the condition trigger needs to be evaluated. So, you need to make sure to place onChange() whenever a change that might cause the condition to become true happens. In the example provided under option 1 above, that would be in the fields of the queue "On Enter" and "On Exit".