Javier Sandoval
Javier Sandoval

Reputation: 507

Netlogo: asking "up to a maximum number"

Is there a way to ask up to a certain number of patches? For example, ask up to 100 patches but there are only 50 available, so the action takes on this 50 patches. Thanks.

Upvotes: 2

Views: 532

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

The way to do this at the moment would be something like:

to-report at-most [n agents]
  report ifelse-value (n <= count agents) [ agents ] [ n-of n agents ]
end

You can then say ask at-most 100 patches [ ... ] and you will get what you want.

Note that this doesn't work if there is a chance that your variable contains nobody instead of an agentset. In that case, you can convert nobody to an agentset using patch-set, turtle-set or link-set, depending on the type of agent you expect it to contain. For example:

ask one-of turtle-set other turtles-here [ ... ]

Note that the need to jump through all these hoops might disappear in the near future. There is currently an open proposal to add a primitive to NetLogo for handling these cases: https://github.com/NetLogo/NetLogo/issues/1594.

Upvotes: 3

Related Questions