问题在Netlogo中的界面上的监视器中进行计算问题

发布于 2025-01-23 15:00:34 字数 330 浏览 5 评论 0原文

我有一个模型,可以跟踪绵羊的能量水平,我将其定义为一个品种。我已经设置了一个监视器来计算绵羊的平均能级,但需要考虑如果某个时候没有绵羊会发生什么。我尝试了许多变体,其中最简单的是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2025-01-30 15:00:34

在三个步骤中回答:

IfelseIfelse-value

您在那里遇到语法错误,因为监视器期望记者,但是ifelse是处理命令。 ifelse> ifelse 的记者版本是ifelse-value。如果您只是将 ifelse 更改为ifelse-value在您的示例中,您会发现您将不再遇到任何语法错误。但是,您还将看到,如果您这样做,并使用绵羊具有其Energy启动模型,则监视器将显示na - 请参见下一点。

正确使用是否?

这是因为您使用是否使用?相反。 ,您可以看到,,如果代理不是空的,则报告为正确。这意味着以下记者:

ifelse-value (any? sheep) ["NA"] [mean [energy] of sheep]

将在模型中有绵羊时报告“ Na”,因为这是第一个记者块。

切换到:

ifelse-value (any? sheep) [mean [energy] of sheep] ["NA"]

您拥有想要的东西。就像英语一样:“如果有任何绵羊,请进行计算,否则,这不适用”。

无论如何,这可以完成工作,但它是多余的 - 请参阅下一点。

监视器可以执行的

监视器能够处理一些记者的运行时错误。您可以简单地将绵羊的含义[能量]放在监视器中,当没有绵羊时,它将自动显示n/a,而无需您处理案例。

Answer in three steps:

Difference between ifelse and ifelse-value

You are getting a syntax error there because a monitor expects a reporter, but ifelse is made to handle commands. The reporter version of ifelse is ifelse-value. If you just change ifelse to ifelse-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 their energy, the monitor will show NA - 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:

ifelse-value (any? sheep) ["NA"] [mean [energy] of sheep]

will report "NA" when there are sheep in the model, because that is the first reporter block.

Switch to:

ifelse-value (any? sheep) [mean [energy] of sheep] ["NA"]

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 show N/A when there are no sheep, without the need for you to handle the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文