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
:
min-n-of
primitive that reports an agentset that contains a specified number of agents with the lowest value of a given reporter.max-n-of
will report the entire agentset. For example, if we have only 2 turtles in a model, max-n-of 5 turtles [ size ]
would report all 2 turtles. 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.
xxxxxxxxxx
turtles-own[money]
to setup
clear-all
create-turtles 10 [
set shape "person"
set color gray
set money 100
]
layout-circle turtles 4
reset-ticks
end
to go
ask turtles [
ask my-links [ die ]
]
ask one-of turtles [
set color gray
if money > 0 [
set money money - 5
create-link-with one-of other turtles
ask link-neighbors [
set money money + 5
]
]
set label money
]
let three-richest max-n-of 3 turtles [money]
ask three-richest [
set color blue
]
let three-poorest min-n-of 3 turtles [money]
ask three-poorest [
set color red
]
tick
end
Once you mastered the max-n-of
primitive, don't stop there. Check out the resources below to improve your NetLogo skills.
max-n-of
primitive: