I have a set of agents represented by turtles. I need to divide the turtles in group according to certain percentages. Then, I need to assign values for a turtle-owned variable (X) against the respective groups. Following is the code I developed -
ask n-of (count turtles * 0.50) turtles [
set X 5.04]
if X != 5.04 [
ask n-of (count turtles * 0.25) turtles [
set X 26.2]
]
if X != 5.04 or X != 26.2 [
ask n-of (count turtles * 0.25) turtles [
set X 58]
]
But, after implementing this code I see the number of turtles with the assigned values are nor accurate. For example, I have 200 turtles. But, the number of turtles that got X=5.04 is not 100 (50%).
I think the problem is you are not referring to turtles with the X
variable with the value you already set. That should be done with something like:
ask turtles with [X != 5.04] [
set X 26.2
]
However, as when a variable is defined its default initial value is 0, I usually do this kind of distribution like this, which I think it is cleaner and easier to work with:
ask n-of (round (0.50 * count turtles)) turtles with [X = 0] [set X 5.04]
ask n-of (round (0.25 * count turtles)) turtles with [X = 0] [set X 26.2]
ask n-of (round (0.25 * count turtles)) turtles with [X = 0] [set X 58]
ask turtles with [X = 0] [set X one-of [5.04 26.2 58]]
If you are creating just 200 turtles, round
and the last line won't be necessary. But those are useful when you are dealing with more complicated percentages or uneven groups of turtles.