1

I want to know if it's possible to time how long the system has been in a specific state? And when it leaves the state, the "stopwatch" stops and then carries on when the state is once again entered?

If so, how?

  • This question is perfectly clear if you have basic knowledge of AnyLogic (except that 'the system' should really be 'an agent'); please remove the hold. – Stuart Rossiter Nov 14 '19 at 22:09

1 Answers1

1

"Continous" measurement (not recommended)

A live counter can be implemented with the help of a cyclic transition inside the state of which you want to measure the time. This transition records cyclicly a discrete amount of time spent in this state:

Statechart state time measurement

However this is not really a good solution because:

  • it takes relatively high computing ressource (smaller timesteps -> more events -> more ressources)
  • the resoultion of the measurement might not be accurate enough (bigger timesteps -> lower resolution measurement result)

Discrete Measurement

If you do not need to see it live counting, it would be a better idea to use the code fields when the state is entered or left, save a timestamp and just add the difference to your counter once everytime that you leave the state. Both variables are of type double.

Discrete measurement screenshot

Keep in mind however, that (as Benjamin already pointed out in his comment), the value is only up to date when the state is not currently active.

Florian
  • 962
  • 1
  • 5
  • 17
  • 1
    Just to reiterate: the internal transition is a really bad idea for this, as Florian said. Create a variable "timeInStateX". OnEnter of the state, write "timeInStateX = time()" and OnExit write "timeInStateX = time() - timeInStateX". Note that your variable will only hold the correct duration once your agent exited the state, though ;-) – Benjamin Nov 14 '19 at 19:42
  • 1
    A note to the code of Benjamin: it only works to record the time spent of the last occurance of the state, it will not add up several times spent in the state. For this you will have to introduce another variable to save the last entry timestamp, see my updated answer above. – Florian Nov 15 '19 at 09:18