'how to get remaining time of agent in delay block in AnyLogic?

I have three agents in delay block and i want to get remaining time of the agent which have maximum remaining time left at particular state/interval, i want to use getremainingtime() method but i dont know how to use it if delay block contains more than one agent. Further details and names can be seen in attached picture. please refer to this image.

i am expecting remaingtime of single agent from delay block whereas delay block contains multiple agent.



Solution 1:[1]

You can get the agent with the max remaining time in the delay by using the following code snippet

double maxRemainingTime = 0;
Agent maxRemainingAgent = null;
for(int i = 0; i < delay.size(); i ++){
    if (delay.getRemainingTime(delay.get(i)) > maxRemainingTime) {
        maxRemainingAgent = delay.get(i);
        maxRemainingTime = delay.getRemainingTime(maxRemainingAgent);
    }
}

Or if you know a thing or two about streams and you only want the max time value and not the agent, you can get everything in a single line of code

double x = findAll(delay, e -> true).stream().mapToDouble(e -> delay.getRemainingTime(e)).max().orElse(0);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1