问题在Netlogo中的界面上的监视器中进行计算问题
我有一个模型,可以跟踪绵羊的能量水平,我将其定义为一个品种。我已经设置了一个监视器来计算绵羊的平均能级,但需要考虑如果某个时候没有绵羊会发生什么。我尝试了许多变体,其中最简单的是:
ifelse (any? sheep) ["NA"] [mean [energy] of sheep]
不幸的是,我一直遇到错误
预期的记者。
我可以通过在代码中创建一个新的全局变量和记者来解决这个问题,但这似乎有点浪费。我是做错了什么,还是在接口上的监视器中可以进行哪种计算有局限性?如果是这样,这些限制在哪里总结?
I have a model that tracks energy levels in sheep, which I have defined as a breed. I have set up a monitor to calculate the mean energy level for the sheep, but need to consider what happens if there are no sheep at some point. I have tried numerous variations, the simplest of which is:
ifelse (any? sheep) ["NA"] [mean [energy] of sheep]
Unfortunately, I keep getting the error
Expected reporter.
I can work around this by creating a new global variable and reporter in the code, but this seems to be a bit of a waste. Am I doing something wrong or are there limitations on what kind of calculations can be done in a monitor on the Interface? If so, where are these limitations summarized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在三个步骤中回答:
Ifelse
和Ifelse-value
您在那里遇到语法错误,因为监视器期望记者,但是
ifelse
是处理命令。ifelse> ifelse
的记者版本是ifelse-value
。如果您只是将 ifelse 更改为ifelse-value
在您的示例中,您会发现您将不再遇到任何语法错误。但是,您还将看到,如果您这样做,并使用绵羊具有其Energy
启动模型,则监视器将显示na
- 请参见下一点。正确使用
是否?
这是因为您使用
是否使用?
相反。 ,您可以看到,,如果代理不是空的,则报告为正确。这意味着以下记者:
将在模型中有绵羊时报告
“ Na”
,因为这是第一个记者块。切换到:
您拥有想要的东西。就像英语一样:“如果有任何绵羊,请进行计算,否则,这不适用”。
无论如何,这可以完成工作,但它是多余的 - 请参阅下一点。
监视器可以执行的
监视器能够处理一些记者的运行时错误。您可以简单地将绵羊的含义[能量]放在监视器中,当没有绵羊时,它将自动显示
n/a
,而无需您处理案例。Answer in three steps:
Difference between
ifelse
andifelse-value
You are getting a syntax error there because a monitor expects a reporter, but
ifelse
is made to handle commands. The reporter version ofifelse
isifelse-value
. If you just changeifelse
toifelse-value
in your example, you see that you don't get any syntax error anymore. However, you will also see that if you do so and start your model with sheep having theirenergy
, the monitor will showNA
- see next point.Correct use of
any?
This happens because you are using
any?
the other way around. As you can see,any?
reports true if the agentset is not empty. This means that the following reporter:will report
"NA"
when there are sheep in the model, because that is the first reporter block.Switch to:
and you have what you want. Just as in English: "If there are any sheep, do the calculation, else, this does not apply".
In any case, this does the job but it is superflous - see next point.
What monitors can do
Monitors are able to handle some of their reporters' runtime errors. You can simply put
mean [energy] of sheep
in your monitor and it will automatically showN/A
when there are no sheep, without the need for you to handle the case.