max-n-of

 

max-n-of reports an agentset that contains a specified number of agents with the highest value of a given reporter. For example, if we wanted to create a model where the largest 5 turtles are divided in half, we would write the code below:

ask max-n-of 5 turtles [size] [
    set size (size / 2)
    hatch 1
]

Things to keep in mind when using max-n-of:

In the model example below, we have some people who represent an absurdly simple economy. At each tick, a randomly picked turtle executes a trade by picking another turtle randomly and giving that turtle 5 of its money. This exchange is represented with a temporary link between the two turtles. We use max-n-of to change the richest three turtles' color to blue. We also use min-n-of to change the poorest 3 turtles' color to red.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the max-n-of primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the max-n-of primitive:
 
 
Similar primitives:
one-of

Reports one randomly picked member from a provided agentset or list.

Read more
max

Reports the highest value in a provided list.

Read more
of

Reports the value of an agent-owned variable.

Read more
count

Counts the number of agents in an agentset.

Read more
 
Learn another primitive: