我有一个模型,其中雄龟根据阈值分类如下:
ifelse(condition > threshold) [set status 0 ] [set status 1]
我想计算满足这个阈值的人群中每个女性的潜在男性伴侣。我对其进行了编码,因此女性仅限于10个补丁半径的检测。如果没有男性达到此阈值,那么FR应该为0,否则它应该是一个比例。
ask turtles with [sex = "female"] [if any? turtles with [sex = "male"]
in-radius 10 [ set potentialMates turtles with [sex = "male"] in-radius 10]
ifelse any? potentialMates with [anadromousM = 1]
[set FR count potentialMates with [anadromousM = 1] / count potentialMates ]
[set FR 0]]
当我运行此操作时,我收到第二个any?
ANY?预期的输入是一个代理集,但得到了数字0。
我哪里错了?希望你能帮忙。
答案 0 :(得分:3)
使用不同的缩进更容易看到。以下是您的代码(包含在过程名称中)。
to find-mate
ask turtles with [sex = "female"]
[ if any? turtles with [sex = "male"] in-radius 10
[ set potentialMates turtles with [sex = "male"] in-radius 10
]
ifelse any? potentialMates with [anadromousM = 1]
[ set FR count potentialMates with [anadromousM = 1] / count potentialMates ]
[ set FR 0]
]
end
如您所见,无论ifelse
的结果如何,都会对if any? turtles with [sex = "male"] in-radius 10
进行测试(对于每只雌龟)。想象一下,这第一个if
是false
,然后潜在的匹配agentset
永远不会被雄性乌龟创建。因此错误。你没有向我们展示首次实例化pontentialMates的代码 - 假设它是一个全局变量,那么它将具有值0。
我认为在半径中根本没有男性的情况下你希望FR为0。在这种情况下,试试这个。
to find-mate
ask turtles with [sex = "female"]
[ ifelse any? turtles with [sex = "male"] in-radius 10
[ set potentialMates turtles with [sex = "male"] in-radius 10
set FR count potentialMates with [anadromousM = 1] / count potentialMates
]
[ set FR 0]
]
end
如果没有anadromousM = 1的potentialMates,则分子为0,0 / N为0.