Search code examples
javaperformancesimulationanylogicagent-based-modeling

AnyLogic: Improving computational performance of a network model


I'm working with an agent-based model of an epidemic. The idea is that individual agents make decisions based on what they observe in their networks (distance-based). I have several functions within each agent that dynamically update counts of infected contacts, contacts showing a particular behaviour etc.

The code below is for counting infected contacts within an agent's network.

int infectedConnections = 0;

if (getConnections() != null)
    for (Agent a : this.getConnections())
        {
        Person p = (Person) a;

        if (p.IsCurrentlyInfected()) 
            infectedConnections++;
            }

return infectedConnections ;

There will be at least 3 more such functions that keep counts of other agents expressing other features within an agent's network. Now, this seems to run Okay when I have <500 agents, but when I increase the agent population to about 1,000 or so, the model becomes extremely slow. I'm looking to simulate at least 5,000 agents, and at this point, the model doesn't even initialise.

Is there a more computationally efficient way to track network statistics in Anylogic for larger populations?


Solution

  • Your result that things bog down somewhere between 1000 and 5000 is pretty common with the agent-based models I've seen. It's a basic computational complexity issue. With N agents, the number of 2-way interactions is N.choose.2, which is O(N^2). 5000 agents is approximately 25 times as much work as 1000 agents.

    You can pull some stunts with localization. Basically, divide your sandbox into different playing areas based on the fact that agents in a particular area can't interact with agents in other areas, so you only need to check for a subset of the interactions. Dividing the N agents into k independent groupings, if possible, will yield an O(k)-fold improvement in run times.

    Another alternative might be to move away from a time-step framework and work out an event-based design for your problem. You can find an example of this approach in this paper.