我如何在 Netlogo 中计算分母有时为零的百分比?

How do I calculate a percentage in Netlogo where the denominator will sometimes be zero?

我正在尝试将变量设置为代理集中的代理集百分比。因此,该程序有时会尝试除以零,我会收到一条错误消息。

我试过使用 ifelse 条件,这样只有当分母大于 0 时才运行一段代码。但是,我仍然收到错误消息。我在下面的代码中遗漏了什么吗?..

ifelse any? people with [recentvent? and trait = 1] 
[set %minority-affiliated-vents 100 * (count people with [groupid > 0 and recentvent? and trait = 1] / count people with [recentvent? and trait = 1])]
[set %minority-affiliated-vents 0]

我仍然得到除以零的运行时错误。

我看不出你的代码有什么问题。作为一般方法,您可以按如下方式修改代码 - 这减少了构建代理集所需的次数,但也确保您不会意外地在测试和用作分母之间以不同方式构建它。它也更容易阅读。

let test-agentset people with [recentvent? and trait = 1] 
ifelse any? test-agentset 
[set %minority-affiliated-vents 100 * count test-agentset with [groupid > 0] / count test-agentset]
[set %minority-affiliated-vents 0]

通过这种构造,您可以看到分母不能为零,因此错误一定是在您的代码中的其他地方。