any?
is used to determine if there are at least one agent within a given agentset. If there are, it will return true, otherwise it will return false.
For example, the following code would repeat running the go
procedure on a repeated basis as long as there is at least 1 turtle in the model. If, let's say, there were many turtles but they could die
, the model would stop when all the turtles die.
to go
if not any? turtles [
stop
]
tick
end
We can combine any?
with a conditional reporter using the with
primitive such as the following example that stops the model if any of the turtles in the model are smaller than 1 unit:
if any? turtles with [size < 1] [stop]
Things to keep in mind in mind when using any?
:
?
) at the end. not
primitive, like in the example above, to reverse the outcome of any?
. any?
itself does not do anything; you should use it within a conditional statement such as if
and if-else
or other primitives that require true-false values such as while
. The model example below represents the spread of a contagious disease within a healthy population. We use any?
primitive to run the model as long as there is at least one healthy (green) individual in the model. Once all of the individuals turn red, the model automatically stops.
Once you mastered the any?
primitive, don't stop there. Check out the resources below to improve your NetLogo skills.
any?
primitive:if
Carries out a provided set of rules (code) if a given condition is true. Does nothing if a given condition is false.