Search code examples
javaanylogicagent-based-modelinguniform-distributionevent-simulation

Generating Agents of Same Type with Different Colors by Uniform Distribution - AnyLogic


Hello there,

I am trying to add a source that generates agents with different colors that are randomly populated based on a non-uniform distribution. For example, I'd like to see this source generates 50% of the agents having orange or 10% of them lightSkyBlue color. I have this line so far that randomly selects a color from availableColors array. But I need each color to be selected based its associated probability.

availableColors = { oliveDrab, crimson, orange, lightSkyBlue, darkOrchid }

agent.favoriteColor =
    randomFrom(availableColors);

Any clue? thanks


Solution

  • Create a custom distribution from the Agent palette with the following properties. In the below, 2 represents "no color". The sum of the number of observations should always be 100 if you are using percentages.

    enter image description here

    Then create a function with the following properties and body: enter image description here

    int colorID = colorProbabilities();
    
    if( colorID == 0 ) { return orange;}
    else if (colorID == 1 ) { return lightSkyBlue;}
    else return null;
    

    Finally, you can now use:

    agent.favoriteColor = colorPicker();
    

    This allows you now to customize your distribution at any point to any number of colors and to any probabilities.