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

Time:06-24

Hello there,

I am trying to add a source that generates agents with different colors where colors 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

CodePudding user response:

You should do it the other way around:

  1. In the agent type that your Source block creates, add a shape (rectangle...) named myShapeHavingColor
  2. In the Source block, ensure it creates agents of that type
  3. In the Source block, in the "on at Exit" code box, you can now change colors from your desired distributions, for example:

enter image description here

CodePudding user response:

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.

CodePudding user response:

Another simple answer if you're not comfortable with coding is to use the select output block. Something as simple as this: enter image description here

  • Related