AnyLogic - transition triggered by a condition - anylogic

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
I have 1000 agents...I expect 950 to go in state V3...yet
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"
and the result is perfect!!!
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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...

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.
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.
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).

Related

Temporary adjustment of delay time

I have the following problem which I am unable to solve:
I have a situation where a security point (added as delay) holds every half an hour a 15 min break. After the break, the security guards increase their speed till the queue is shorter than 10pp.
I wanted to model this as follows: a state chart with delay.set_capacity(0) after 30 minutes and delay.set_capacity(1) again after the 15 min break. For the increased speed after the break, I added an additional state with condition: queue.size()>10 and now I want to set the action such that the delay function changes the delay time from exponential (1/10) to exponential (1/5) as long as queue.size()>10.
Anyone experience with which function in the action box to use? Or would you suggest a different function?
Since you are using, or at least want to use a statechart I would suggest the following design, where you have composite states inside the working state to indicate if the security agent is working fast or normal and a message transition to let it move from one state to the next.
It is advised to use a message transition and trigger it as needed instead of a conditional state which gets chected for every change inside the agent since this can be a computational expensive exercise.
I assume you already implemented the correct capacity settings for the different on enter actions for working and breaking
Now you simply need to send the message every time an agent enters the queue and every time it exits the delay block, and of course, see the delay time based on the state of the statechart.
Aee screenshot below.

anylogic triggering an event

In accordance, I have a question in the modeling process. I want my agent to have an event that is triggered by time and condition, example: goToSchool if it is more than 6 am and there is a school bus. I am confused about whether to use the timeout trigger (but cannot use the condition) or the condition (but cannot use the timeout) or is there any possible alternative?
In your example, "if it is more than 6 am" is a condition and not a timeout. A timeout trigger is used when you want an event to happen at an exact time. In your case, while "more than 6 am" is time related, it is still a condition. So I would use a condition triggered event with two conditions:
getHourOfDay() > 6 && <bus condition>
getHourOfDay() function returns the hour of the day in a 24-hr format.
You need to keep in mind something important related to condition triggered events, they are only evaluated "on change". I recommend you read this carefully:
https://help.anylogic.com/index.jsp?topic=%2Fcom.anylogic.help%2Fhtml%2Fstatecharts%2Fcondition-event.html
My recommendation would be to use the onChange() function in the block controlling your bus arrival so that the condition is evaluated each time a bus arrives.

Does Apache Beam stateful Processing consider window lateness constraints (withAllowedLateness) for resetting state?

I'm trying to implement a valueState to filter records in my ParDo transformation. The high level flow is this:
Fixed-Window of 1hr size, with allowedLateness (10min)
The first message (for a given key) that is processed in the ParDo shall set the valueState(boolean) to true. Subsequent messages for the same key shall be dropped if corresponding valueState is set to true. (Allow only first message for a given key in every window).
The messages (that are not dropped in step 2) will be written out as output.
While testing this however, I see that, after the Fixed window time-period ends (1hr), the state is reset/lost. Ideally, the state should be available to process late records until allowedLateness period (10min is complete).
These parts are right:
Each 1 hour window expires when the watermark reaches the end of the hour plus 10 minutes.
For a given window, the state is cleaned up after the window expires.
Here are the parts that I have corrections
State is never reset.
Elements with timestamps in different windows are processed totally independently. Many windows may be receiving data at the same time. Each hour window happened after another, when the data was generated. But it is not processed after the other.
Allowed lateness will not cause elements from a later window to be processed using the state from the prior window. It will simply allow the state to stay longer and the elements to not be dropped.

Statechart definition of two "parallel" processes

I am trying to define two independent but related processes, criminal career and crime enforcement (imprisonment). Here is a diagram:
Crimes will be committed only if an agent has already started his criminal career through an internal transition. Desistance (to move away from crime) is a final state that can happen while an agent is imprisoned (that's why I define this problem as two independent processes).
When a crime occurs (internal transition commitCrime) the variable committedCrime is set to true. The commitCrime transition has as a guard this.ImprisonmentStateChart.isStateActive(Free), so an agent cannot commit crimes if he is imprisoned.
Every time a crime is committed, a condition transition is assessed from state Free: this.committedCrime == true. Once in prison, an agent will be eventually released.
My problem is that the commitCrime transition is not restarted after coming back to the Free state. The idea would be to restart the commitCrime transition after releaseFromPrison is triggered, but I haven't found how to do it with Anylogic 7.
Any ideas?
I think the simplest way is to remove guard and wrap the action in if statement:
if ( ImprisonmentStateChart.isStateActive(Free) )
committedCrime == true;

Long running workflow with conditional transition

I have long-running workflow service, based on state machine, that at some point has to compare two values and if they are equal it should transition to a new state.
However, once I introduce trigerless transition with compare, workflow instance never goes to idle.
What is the best approach to do this, to have conditional transition that is evaluated once per entering source state, and goes back idle if condition is false.