NetLogo:计算包含代理的变量时出现问题
NetLogo: Problem when counting variable containing an agent
我是 netlogo 的新手,正在研究“Follower”模型。
在扩展模型命题时,有一个“如果允许两只海龟拥有相同的领导者会怎样?”
我想知道是否有一种方法可以计算海龟中的追随者数量
(类似于
count follower
然后以如下方式使用它:
let candidate one-of (turtles-at xd yd) with [follower = nobody or count follower < 2]
在运行时出现此错误:
COUNT expected input to be an agentset but got the turtle (turtle xxxx) instead.
我用 hack 解决了这个问题(添加一个新变量 f-num,然后在新海龟附加到当前海龟时递增它,并选择条件 f-num < 2 的候选对象)但我想请问这样的问题有没有更好的办法
问题是原来设置follower
的语句是
ask candidate [set follower myself]
和myself
不是agentset,是agent。这意味着,除非该语句发生变化,否则 follower
永远不会是可数的:您可以计算一个代理集中有多少个代理(也可以是 1 或 0 的代理集),但您不能算一个代理人。
然后解决方案必须是更改该语句,将 follower
初始化为不同的对象 - 一个容纳多重性的对象(这意味着您可以在其上执行加法和可以测量的对象)。
- 代理集解决方案
我说了,agentsets是可以统计的。实际上,代理集是 count
操作的自然对象类型。因此,我们可以将 follower
初始化为一个代理集。
一种方法是使用 turtle-set:
...
ask candidate [set follower (turtle-set follower myself)]
...
在那种情况下,为了保持一致性,我也会通过以下方式更改 create-turtles population
位:
...
set follower (turtle-set)
...
这样,follower
将从一开始就成为所有海龟的代理集(从空代理集开始而不是 nobody
)。
为了一致性,我更喜欢它,但请注意,这意味着所有询问 follower = nobody
是否不再有意义的条件:空代理集不同于 nobody
!
事实上,如果您只是进行上述更改,然后在命令中心单击setup
,然后单击运行 count turtles with [follower = nobody]
,您将获得0。
运行 而不是 count turtles with [count follower = 0]
你会得到所有的海龟。
因此 nobody
的条件必须相应更改;并且,特别是,作为您问题对象的那个将变得整洁
...
let candidate one-of (turtles-at xd yd) with [count follower < 2]
...
因此,为了保持整洁,让我将这三个更改按代码内逻辑顺序排列:
; The initial declaration when you create turtles:
...
set follower (turtle-set)
...
; The condition where you test how many followers a turtle has:
...
let candidate one-of (turtles-at xd yd) with [count follower < 2]
...
; The action of adding a follower:
...
ask candidate [set follower (turtle-set follower myself)]
...
; Plus changing other instances where the original code says 'with [follower = nobody]'.
注意
正如您从 turtle-set
的描述中看到的那样,它是一个非常灵活的原语:基本上它将任何东西变成一个(乌龟)代理集。
然而,最常用的创建代理集的方法是使用 with
。逻辑是代理集的子组仍然是代理集;鉴于 with
只能在代理集上使用,它给你的只能是一个代理集。
然而,在我们的例子中,使用 with
不会是 tidy/simple。
follower
变量可以使用 with
:
很容易地初始化为代理集
ask candidate [set follower turtles with [who = [who] of myself]]
然而,这个唯一的命令每次都将 follower
设置为包含唯一具有该特定 who
的乌龟的代理集,因此无助于增加代理集的数量。
这是事情开始变得比仅使用 turtle-set
.
更复杂的时候
- 列表解决方案
另一种可以计算的对象是列表。正如您所想象的,您不计算 count
的列表,但您可以使用 length
.
在这种情况下,我们只是将 follower
初始化为一个列表,使用 lput
或 fput
将代理作为项目添加到列表中(取决于人们想如何使用列表), 检查条件时计算列表的长度。
更改反映了我们在 agentset 解决方案中所做的更改:
; The initial declaration when you create turtles:
...
set follower (list) ; or
set follower []
...
; The condition where you test how many followers a turtle has:
...
let candidate one-of (turtles-at xd yd) with [length follower < 2]
...
; The action of adding a follower:
...
ask candidate [set follower (lput myself follower)]
...
; Plus changing other instances where the original code says 'with [follower = nobody]'.
如果您想跟踪海龟跟随领导者的顺序,使用列表会很有用。事实上,虽然列表是有序对象,但代理集是无序的(字面意思是代理集中的代理 come/act 每次调用该代理集时都是随机顺序)。
我是 netlogo 的新手,正在研究“Follower”模型。 在扩展模型命题时,有一个“如果允许两只海龟拥有相同的领导者会怎样?” 我想知道是否有一种方法可以计算海龟中的追随者数量 (类似于
count follower
然后以如下方式使用它:
let candidate one-of (turtles-at xd yd) with [follower = nobody or count follower < 2]
在运行时出现此错误:
COUNT expected input to be an agentset but got the turtle (turtle xxxx) instead.
我用 hack 解决了这个问题(添加一个新变量 f-num,然后在新海龟附加到当前海龟时递增它,并选择条件 f-num < 2 的候选对象)但我想请问这样的问题有没有更好的办法
问题是原来设置follower
的语句是
ask candidate [set follower myself]
和myself
不是agentset,是agent。这意味着,除非该语句发生变化,否则 follower
永远不会是可数的:您可以计算一个代理集中有多少个代理(也可以是 1 或 0 的代理集),但您不能算一个代理人。
然后解决方案必须是更改该语句,将 follower
初始化为不同的对象 - 一个容纳多重性的对象(这意味着您可以在其上执行加法和可以测量的对象)。
- 代理集解决方案
我说了,agentsets是可以统计的。实际上,代理集是 count
操作的自然对象类型。因此,我们可以将 follower
初始化为一个代理集。
一种方法是使用 turtle-set:
...
ask candidate [set follower (turtle-set follower myself)]
...
在那种情况下,为了保持一致性,我也会通过以下方式更改 create-turtles population
位:
...
set follower (turtle-set)
...
这样,follower
将从一开始就成为所有海龟的代理集(从空代理集开始而不是 nobody
)。
为了一致性,我更喜欢它,但请注意,这意味着所有询问 follower = nobody
是否不再有意义的条件:空代理集不同于 nobody
!
事实上,如果您只是进行上述更改,然后在命令中心单击setup
,然后单击运行 count turtles with [follower = nobody]
,您将获得0。
运行 而不是 count turtles with [count follower = 0]
你会得到所有的海龟。
因此 nobody
的条件必须相应更改;并且,特别是,作为您问题对象的那个将变得整洁
...
let candidate one-of (turtles-at xd yd) with [count follower < 2]
...
因此,为了保持整洁,让我将这三个更改按代码内逻辑顺序排列:
; The initial declaration when you create turtles:
...
set follower (turtle-set)
...
; The condition where you test how many followers a turtle has:
...
let candidate one-of (turtles-at xd yd) with [count follower < 2]
...
; The action of adding a follower:
...
ask candidate [set follower (turtle-set follower myself)]
...
; Plus changing other instances where the original code says 'with [follower = nobody]'.
注意
正如您从 turtle-set
的描述中看到的那样,它是一个非常灵活的原语:基本上它将任何东西变成一个(乌龟)代理集。
然而,最常用的创建代理集的方法是使用 with
。逻辑是代理集的子组仍然是代理集;鉴于 with
只能在代理集上使用,它给你的只能是一个代理集。
然而,在我们的例子中,使用 with
不会是 tidy/simple。
follower
变量可以使用 with
:
ask candidate [set follower turtles with [who = [who] of myself]]
然而,这个唯一的命令每次都将 follower
设置为包含唯一具有该特定 who
的乌龟的代理集,因此无助于增加代理集的数量。
这是事情开始变得比仅使用 turtle-set
.
- 列表解决方案
另一种可以计算的对象是列表。正如您所想象的,您不计算 count
的列表,但您可以使用 length
.
在这种情况下,我们只是将 follower
初始化为一个列表,使用 lput
或 fput
将代理作为项目添加到列表中(取决于人们想如何使用列表), 检查条件时计算列表的长度。
更改反映了我们在 agentset 解决方案中所做的更改:
; The initial declaration when you create turtles:
...
set follower (list) ; or
set follower []
...
; The condition where you test how many followers a turtle has:
...
let candidate one-of (turtles-at xd yd) with [length follower < 2]
...
; The action of adding a follower:
...
ask candidate [set follower (lput myself follower)]
...
; Plus changing other instances where the original code says 'with [follower = nobody]'.
如果您想跟踪海龟跟随领导者的顺序,使用列表会很有用。事实上,虽然列表是有序对象,但代理集是无序的(字面意思是代理集中的代理 come/act 每次调用该代理集时都是随机顺序)。