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:
- In the agent type that your Source block creates, add a shape (rectangle...) named
myShapeHavingColor
- In the Source block, ensure it creates agents of that type
- In the Source block, in the "on at Exit" code box, you can now change colors from your desired distributions, for example:
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.
Then create a function with the following properties and body:
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: