我在 Netlogo 中遇到了一个奇怪的问题,其中包含 ifelse 语句



我在做一个项目时询问网络徽标的问题。我正在做一个集群模拟,但是当我试图使用 ifelse 语句刺穿其他行为时,但是当我将 ifelse 语句放入其中时,它们不遵循任何行为,而只是移动。

代码如下:

breed [Birds Bird] breed [Hawks Hawk]
to Setup   clear-all   reset-ticks   create-Birds Number_of_Birds[
setxy random-xcor random-ycor]   create-Hawks Number_of_Hawks[
setxy random-xcor random-ycor] end
to Start   ask Birds[
set color white
ifelse (Hawks in-radius Reaction_Distance = 0)
[
set heading Migration_Direction
let closest-Birds max-n-of Target_Group_Size (other Birds) [distance myself]
let Group_Heading mean [heading] of closest-Birds
let centroidx mean [xcor] of closest-Birds
let centroidy mean [ycor] of closest-Birds
set heading (Migration_Direction +( attraction * (Group_Heading)))
fd 1
set heading ( attraction * (towardsxy centroidx centroidy) )
fd 1
]
[
let Closest_Hawks max-n-of 1 (Hawks) [distance myself]
set heading (mean [heading] of Closest_Hawks + 180)
fd 1
]   ] end

让我们来看看 NetLogo 字典中in-radius的定义:

报告一个代理集,该代理集仅包含原始代理集中与调用方的距离小于或等于数字的代理。

它说in-radius报告一个代理集

现在让我们来看看您的ifelse状况:

ifelse (Hawks in-radius Reaction_Distance = 0)

该定义告诉我们,Hawks in-radius Reaction_Distance部分报告一个代理集(即半径内的所有鹰(。然后,=符号将该代理集与数字0进行比较。但代理集不是一个数字!它永远不能等于零。

我想你想要的是将半径内的鹰数量0的数量进行比较.

一种方法是使用count原语,它报告代理集中的代理数:

ifelse (count Hawks in-radius Reaction_Distance = 0)

那会起作用,但我不会那样写。NetLogo具有更好的any?原始语,您可以像这样使用它:

ifelse (not any? Hawks in-radius Reaction_Distance)

这以更清晰的方式表达了您的意图。

您还可以反转ifelse子句的顺序以避免not

ifelse (any? Hawks in-radius Reaction_Distance)
[
; get away from hawks...
]
[
; flock normally...
] 

最新更新