GhettiG
GhettiG

Reputation: 91

AnyLogic - transition triggered by a condition

i'm new to AnyLogic...but it is driving me crazy!!!

It is super basic...in an AB model I want a percentage of agents to go from one state to another...but it happens something that I cannot understand :S

enter image description here

I have 1000 agents...I expect 950 to go in state V3...yet enter image description here

Only 889 agents change status :(
It seems that the condition is evaluated twice...indeed 0.95*0.95=0.90..that is exactly the percentage of agents changing status :(

Not satisfied I tried to put the contidion in the "guard" enter image description here

and the result is perfect!!!
enter image description here

Can anyone help me understand this? :( I have to revise the entire model...when do I have to put "conditions" and when "guards"?

Thanks everyone!!

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

from @sdaza model

enter image description here enter image description here

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

AnyLogic Support Team answer

_The condition is evaluated twice in case if it returns true initially. Initial chec is performed when agent enters in state that has outcoming conditional transition(s). If certain condition of a transition returns true, engine tries to trigger it. Here the condition is checked again. If the condition returns false, it will be cancelled, agent remains in the state. In other words, condition should return true twice to be executed.

In your case only the agents that returned "true" twice executed the transition, and probability matches with result: 0.95 * 0.95 = 0.9. This is actual probability.

I hope it will help_

I didn't get the reason why it is so though...

Upvotes: 3

Views: 2827

Answers (1)

Stuart Rossiter
Stuart Rossiter

Reputation: 2517

Some background and an example model which 'opens up' a bit what is happening behind the scenes should help you understand the AnyLogic response.

Background

AnyLogic conditions are checked at every step of the model (i.e., at model start and after every event created explicitly by the developer or implicitly via the AnyLogic elements used). AnyLogic also uses timeout 0 events under the covers to effect state transitions; i.e., if it decides a transition of any sort is due, it does not do it immediately but schedules an event for the same simulation time to do so, which will be triggered at the next step of the model if there are no other events at the same simulation time. If you have the Professional edition, you can see this at model runtime via the Events Viewer (see later).

Condition Transitions

Because of the above, the condition is evaluated at the start of the model. If it is true, a timeout 0 event is scheduled to effect the transition. When this fires, the condition is checked again (because it is possible it is no longer true due to intervening events at the same simulation time). If it is true this second time, the transition goes ahead.

Example Model

I adapted your example. I just have a Main with a statechart with two states and two transitions: a condition one (with your randomTrue(0.95) condition) from state 1 to state 2, and a timeout 1 one which sends state 2 back to state 1. By putting the condition inside a function (check), I can add some extra traceln statements so we can better see what's going on.

Example model setup

Run this (setting to Run Until time 0 so it pauses at model startup) with the Event Viewer shown. The condition evaluated true so you can see the timeout 0 transition event set up.

enter image description here

I also added some traceln messages for the condition transition occurring and the transition back to state 1. Here's a sample from a run. (The transitions will stop once the condition doesn't evaluate true twice in a row, and so will depend on the random seed chosen for the run.)

Checking condition at time 0.0: sampled true
Checking condition at time 0.0: sampled true
Probabilistically transitioned to state 2
Going back to state 1
Checking condition at time 1.0: sampled true
Checking condition at time 1.0: sampled true
Checking condition at time 1.0: sampled true
Probabilistically transitioned to state 2
Going back to state 1
Checking condition at time 2.0: sampled false
Checking condition at time 2.0: sampled true
Checking condition at time 2.0: sampled true
Probabilistically transitioned to state 2
Going back to state 1
Checking condition at time 3.0: sampled true
Checking condition at time 3.0: sampled true
Checking condition at time 3.0: sampled true
Probabilistically transitioned to state 2
Going back to state 1
Checking condition at time 4.0: sampled true
Checking condition at time 4.0: sampled false

Notice that there are three condition evaluations each time after the first transition. I presume this is because the condition is also evaluated when the state 2 --> state 1 transition event fires (after its action code completes but before the transition is actually completed). Whether it evaluates true or false at these points is irrelevant since the Agent is not in state 1 yet and so the state 2 transition is not 'active'. (This does seem slightly strange, since it would be more efficient for conditions only to be checked if the Agent is in a state where the condition transition is active. However, I have no other explanation for this extra evaluation otherwise.)

It then arrives in state 1 and so the condition is checked (immediately, without an event) and, if true, a state 2 transition event is setup (causing the second check when it fires).

Upvotes: 1

Related Questions