Search code examples
anylogicevolutionary-algorithmagent-based-modelinggame-theory

How to control interactions of agents in simple evolutionary games in AnyLogic?


Learning AnyLogic agent-based modeling capabilities, I want to model a simple evolutionary game.

Setting There are N agents (even number) each having two states, i.e. Cooperate and Defect, and they may switch between states dependent on the results of an interaction in the time period. Next period (iteration, or step) they again should interact in newly matched random pairs. My guess is that population should split randomly in pairs somehow (though some people suggest to use a kind of super-agent, a broker, who is in charge of coordinating all unique pairs).

At the very moment I see examples of games in AnyLogic, like Segregation game, but the setting is different, and I've found no example model or tutorial where agents interact in random pairs (some links would be welcome).

Question: How to model such a setting to make sure that each agent interacted in pairs in each period (tact) and none of them left without interaction, and none of them took part in more then one pair. Any tips are welcome.

Note: The type of interaction in pairs (one-shot game) is not important at the moment (say, one agent sends an message to the counterpart). I'm after the logic of interactions arrangement.


Solution

  • Let's assume your agent population is of type MyAgent with a population called myAgents. Each one of these agents has a bidirectional connection called agentLink that links one agent to another of the same type as seen in this figure:

    agent link

    Then to create random pairs you can use this code:

    for(MyAgent a : myAgents){
        a.agentLink.disconnect(); // remove previous connections
    }
    for(MyAgent a : myAgents){
        if(!a.agentLink.isConnected()){ //check if there is no pair already
           MyAgent b=randomWhere(myAgents,m->!m.agentLink.isConnected() && !m.equals(a)); //find a random agent which is not equal to "a" and not connected to someone else
           a.agentLink.connectTo(b); //link them together
        }
    }
    

    All these are random connections based on nothing... but you can use randomWhere to define your own conditions to which 2 agents can be connected to each other