Search code examples
anylogic

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

  • 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);