'Problem making a calculation in a monitor on the interface in Netlogo

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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Seth Tisue