How to select the highest and lowest value of a patch variable?

130 views
Skip to first unread message

Karina Santos

unread,
Mar 11, 2022, 2:31:14 PM3/11/22
to netlogo-users
Hello everyone,

I have a question. I have a template that has a patch size of 1000x1000. There is also a patch variable called (food-turtle). Each patch has its own food-turtle value.

I would like to select the highest and lowest existing value of this food-turtle variable in the world. I thought about making a list, but that would take too long as my world is relatively large. Does anyone have an idea how I could solve this?

Thank you

Wade Schuette

unread,
Mar 11, 2022, 5:24:46 PM3/11/22
to Karina Santos, netlogo-users
check out "max-one-of" and "min-one-of" in the dictionary.

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/CABCtG4Hdn2tVgC0O6u-WYq35fBytJYV50HhUGWp2fcrudzg9NA%40mail.gmail.com.

James Steiner

unread,
Mar 11, 2022, 6:48:29 PM3/11/22
to netlogo-users
Luckily, NetLogo has lots of built-in commands (called "primitives") that do for us many of the things that typical programming courses lead us to believe require building arrays and looping with FOR and WHILE.  Commands like (as Wade points out) "max-one-of" that returns a patch that has the maximum value for an expression.  

Besides MIN-ONE-OF and MAX-ONE-OF, that return an AGENT with the minimum or maximum value, there's also plain-old MIN and MAX that return the VALUE from a list.
They go along with OF, which when given an agentset, returns a list of VALUEs. 

;; patch with biggest value (if a tie, chooses on at random)
PRINT MAX-ONE-OF [ PXCOR + PYCOR ] PATCHES

;; biggest value from the patches
PRINT MAX [ PXCOR + PYCOR ] OF PATHES

If you are concerned about execution speed, you may need to put thought into how to reduce the total number of operations. But remember to always test any theory of what might be faster.

~~James
TurtleZero


--
Reply all
Reply to author
Forward
0 new messages