Search code examples
anylogicagent

Anylogic find agent in population and access one of its variables


I want to find the index my agent has in its population (named dullieses), which lives in my Main-agent.
I want to find one of my dullieses-agent via one of its variables (DullyID) which, in this example, shall be equal to my counter variable. Both are of type int.

I have tried the following to no avail:

int i = 0;

for ( i = 0; i < dullieses.size(); i++){
    if (Main.dullieses(i).DullyID == counter){
        traceln("I found myself! I have the index " + i);
    break;
    }
}

Error code: Cannot make a static reference to the non-static method dullies(int) from type Main.

How can I find my agent in my population and find its index?


Solution

  • I think you're probably already in main and don't need to add Main.. Anyway, I would recommend using:

    Dully d = findFirst( dullieses, d -> d.ID == counter );
    d.variable = 10;
    

    I used variable and 10 as random examples, but this should give you the idea. Also replace Dully with whatever your agent type name is.