Search code examples
simulationanylogic

AnyLogic how to set attractor choice to both free and random


Is there a way to make attractor choice both free & random at the same time?

The problem I have with either on it's own is:

  • When the choice is set to Free - agents are using attractors in very predictable order based on attractor creation.
  • When the choice is set to Random - more than one agent is using an attractor at the same time, which I don't want.

Solution I found, but don't know how to implement correctly is in the following thread:

AnyLogic Attractor weird behavior

I tried to create an agent type (instead of a class) 'myAttractor' with a boolean variable inside (occupied or not occupied attractor), but I don't know how to assign that agent type to actual attractors within a node - if that is even possible?

Maybe there are other solutions to customise the attractor choice to achieve complete randomness with only one agent per attractor?

Many thanks in advance, Peter


Solution

  • That is a good question and often a problem on the animation side of things.

    One option is to create a collection, simple ArrayList will do, of all the attractors

    enter image description here

    Then in the Process Modelling Block (PML) where you setup the attractor you have a function that returns an Attractor. I supply the agent here so that we can keep track of what agent is sent to which attractor so that we can put the attractor back into the available pile once the agent leaves the attractor location.

    enter image description here

    Here is the getAttractor function

    enter image description here

    It gets a random available attractor and then also saves the agent that is taking it to a map

    Here is the setup for the map mapAgentPerAttractor

    enter image description here

    If you want to free the attractor you can simple call this at any point that the attractor is freed

    attractorsAvailable.add(mapAgentPerAttractor.get(agent));
    mapAgentPerAttractor.remove(agent);
    

    Here is the final result as well as a comparison where we are replciating the problem you described

    enter image description here

    One can see in the bottom node there are only 8 dots available as some agents are on the same attractor...