NetLogo:询问有限数量的海龟,按变量值排序
NetLogo: Asking a limited number of turtles, ranked by the value of a variable
我正在尝试在 NetLogo(5.3.1,在 Mac Sierra 上)中对容量限制和优先级进行建模。它归结为要求有限数量的海龟(比如 n
)做某些事情,并选择这些海龟,以便(1)它们满足特定条件并且(2)它们是 n
海龟变量的最大值 my-variable
。
我正在尝试通过以下方式做到这一点:
let subset-of-turtles turtles with [ condition-variable = some-value ]
ask max-n-of n subset-of-turtles [ my-variable ] [< do something >]
但它有多个问题。
首先,如果没有乌龟满足[ condition-variable = some-value ]
的条件,NetLogo会抛出一个错误
Requested n random agents from a set of only 0 agents.
我尝试通过在 ask
命令之前插入一行来解决:
let subset-of-turtles turtles with [ condition-variable = some-value ]
if subset-of-turtles != nobody [
ask max-n-of n subset-of-turtles [ my-variable ] [< do something >]
]
但是不行:
observer> show turtles with [ condition-variable = some-value ]
observer: (agentset, 0 turtles)
observer> let subset-of-turtles turtles with [ condition-variable = some-value ] show subset-of-turtles != nobody
observer: true
NetLogo认为一个空的agentset仍然是一个agentset,所以它会通过不同于nobody
的测试。
其次,即使有一些海龟满足条件,如果少于n
,NetLogo 也会抛出同样的错误。我的模型是一个增长模型,在这个模型中,能力意味着一开始就足够了,然后就会达到约束。因此,这将发生在模型的每个 运行 中。
我希望 NetLogo 最多执行 ask
块中的命令 n
次。假设有m
只海龟满足条件:
1.如果m <= n
,对所有m
只海龟执行命令
2. If m > n
,执行my-variable
值最高的n
只海龟的命令。有人可以建议吗?
假设此设置:
turtles-own [
condition-variable
my-variable
]
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
set condition-variable random 2
set my-variable 1 + random 10
]
end
首先,您可以使用 count
快速检查代理集是否为空,并将其用作您的条件。
不要这样做;正如@JenB 在接受的答案中所建议的那样,改用 any?
原语。
其次,你可以用ifelse
来表达"If there are fewer turtles in the subset than n, only ask the turtles in the subset. Else, ask n turtles in the subset."
to go
let n 5
let subset-of-turtles turtles with [ condition-variable = 1 ]
;; obtain the number of turtles in the subset
let subcount count subset-of-turtles
;; If there are more than 0 turtles in the subset, do the following
if subcount > 0 [
;; If n is greater than the number in the subset, only ask the subset
ifelse n >= subcount [
ask max-n-of subcount subset-of-turtles [ my-variable ] [
set size 2
]
]
;; If n is NOT greater than the number in the subset, ask n of the subset
[
ask max-n-of n subset-of-turtles [ my-variable ][
set size 2
]
]
]
end
一般来说,我不建议测试是否count <agentset> = 0
,因为NetLogo仍然需要构造agentset来统计它。但是,有一个非常方便的 any?
记者可以完成这项任务。因此,尝试对原始代码进行以下修改:
let subset-of-turtles turtles with [ condition-variable = some-value ]
if any? subset-of-turtles [
ask max-n-of n subset-of-turtles [ my-variable ] [< do something >]
]
我正在尝试在 NetLogo(5.3.1,在 Mac Sierra 上)中对容量限制和优先级进行建模。它归结为要求有限数量的海龟(比如 n
)做某些事情,并选择这些海龟,以便(1)它们满足特定条件并且(2)它们是 n
海龟变量的最大值 my-variable
。
我正在尝试通过以下方式做到这一点:
let subset-of-turtles turtles with [ condition-variable = some-value ]
ask max-n-of n subset-of-turtles [ my-variable ] [< do something >]
但它有多个问题。
首先,如果没有乌龟满足[ condition-variable = some-value ]
的条件,NetLogo会抛出一个错误
Requested n random agents from a set of only 0 agents.
我尝试通过在 ask
命令之前插入一行来解决:
let subset-of-turtles turtles with [ condition-variable = some-value ]
if subset-of-turtles != nobody [
ask max-n-of n subset-of-turtles [ my-variable ] [< do something >]
]
但是不行:
observer> show turtles with [ condition-variable = some-value ]
observer: (agentset, 0 turtles)
observer> let subset-of-turtles turtles with [ condition-variable = some-value ] show subset-of-turtles != nobody
observer: true
NetLogo认为一个空的agentset仍然是一个agentset,所以它会通过不同于nobody
的测试。
其次,即使有一些海龟满足条件,如果少于n
,NetLogo 也会抛出同样的错误。我的模型是一个增长模型,在这个模型中,能力意味着一开始就足够了,然后就会达到约束。因此,这将发生在模型的每个 运行 中。
我希望 NetLogo 最多执行 ask
块中的命令 n
次。假设有m
只海龟满足条件:
1.如果m <= n
,对所有m
只海龟执行命令
2. If m > n
,执行my-variable
值最高的n
只海龟的命令。有人可以建议吗?
假设此设置:
turtles-own [
condition-variable
my-variable
]
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
set condition-variable random 2
set my-variable 1 + random 10
]
end
首先,您可以使用 count
快速检查代理集是否为空,并将其用作您的条件。
不要这样做;正如@JenB 在接受的答案中所建议的那样,改用 any?
原语。
其次,你可以用ifelse
来表达"If there are fewer turtles in the subset than n, only ask the turtles in the subset. Else, ask n turtles in the subset."
to go
let n 5
let subset-of-turtles turtles with [ condition-variable = 1 ]
;; obtain the number of turtles in the subset
let subcount count subset-of-turtles
;; If there are more than 0 turtles in the subset, do the following
if subcount > 0 [
;; If n is greater than the number in the subset, only ask the subset
ifelse n >= subcount [
ask max-n-of subcount subset-of-turtles [ my-variable ] [
set size 2
]
]
;; If n is NOT greater than the number in the subset, ask n of the subset
[
ask max-n-of n subset-of-turtles [ my-variable ][
set size 2
]
]
]
end
一般来说,我不建议测试是否count <agentset> = 0
,因为NetLogo仍然需要构造agentset来统计它。但是,有一个非常方便的 any?
记者可以完成这项任务。因此,尝试对原始代码进行以下修改:
let subset-of-turtles turtles with [ condition-variable = some-value ]
if any? subset-of-turtles [
ask max-n-of n subset-of-turtles [ my-variable ] [< do something >]
]