简体   繁体   English

Netlogo:在海龟列表中搜索

[英]Netlogo: Search in a list of turtles

Let's say I have a list of turtles and each turtle in this list has a own numeric variable foo. 假设我有一个乌龟列表,列表中的每个乌龟都有一个自己的数字变量foo。 How can I elegantly extract the turtle with the lowest value stored in foo without iterating the whole list? 如何在不迭代整个列表的情况下优雅地提取foo中存储的最低值的乌龟?

Thank you in advance! 先感谢您!

Eric. 埃里克。

If you want the turtle in the list with the lowest value of foo , then you can sort the list of turtles by the foo value for each turtle and then pick off the first turtle in the list. 如果要让列表中的乌龟具有foo的最小值,则可以按每只乌龟的foo值对乌龟列表进行排序,然后从列表中选择第一只乌龟。 For NetLogo v6.0 that would be (assuming foo is a turtle-own variable and turtle-list is your list of turtles): 对于NetLogo v6.0来说(假设foo是乌龟拥有的变量,而turtle-list是您的乌龟列表):

first sort-by[[t1 t2] -> [foo] of t1 < [foo] of t2] turtle-list

Charles 查尔斯

it depends a bit on the context (how is the list structured, who owns it, how is it built and how frequently), but in general I'd save the list in it's own global/turtle-owned variable then use the primitive 'min-one-of' to query it. 它取决于上下文(列表的结构,拥有者,构建方式和频率),但总的来说,我会将列表保存在其自己的全局/龟拥有的变量中,然后使用原始的'最小一”来查询。 If you give a reproducible example I can help you apply this to code, but something to the effect of 如果您举一个可复制的示例,我可以帮助您将其应用于代码,但是有些影响

globals [foo-list] 
;make foo-list;
let small_foo min-one-of [foo-list] 
show small_foo

When you say you have a 'list' of turtles, do you actually mean the data type list, or do you really have an agentset (which is the more usual NetLogo construction of a group of turtles)? 当您说有一个“乌龟列表”时,您实际上是指数据类型列表,还是真的有一个代理集(这是一组乌龟的NetLogo构造)? If you have an agentset, look at the dictionary for the primitive with-min . 如果您有一个代理集,请使用-min查看字典中的原始with-min You will want some construction like let small_foo agentsetname with-min [foo] 您将需要一些结构,如let small_foo agentsetname with-min [foo]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM