AndreA
AndreA

Reputation: 789

Netlogo - expected constant error

I have this line of code:

set SSB sum [foreach  [foglie with [ipotetico? = false]] [((([media] of self ) - media-tot ) ^ 2) * ([larg] of self * [altez] of self)]]

I cannot figure out why it gives me the error:

expected constant

I found the following loophole but it looks to me not elegant and maybe even slower:

set SSB 0
ask foglie with [ipotetico? = false] [ set SSB (SSB + (( [media] of self - media-tot) ^ 2) * ([larg] of self * [altez] of self)) ] 

Upvotes: 1

Views: 362

Answers (1)

Alan
Alan

Reputation: 9620

A good question will include a minimal example of the problem. In this case, in your first attempt above, it looks like you do not understand the difference between agentsets and lists, nor the use of foreach. So you need to read about this.

  • sum requires a list as input
  • to make a list with the bracket notation, you need to include only constants; otherwise, use list
  • putting an agentset in brackets does not produce a list, for the above reason, but even if you used list you would get a list containing the agentset as an element, not a list of the agents
  • foreach does not return a list (but map does)

Assuming media, larg, and altez are foglie attributes and media-tot is a global, you could have done the following:

sum [(media - media-tot) ^ 2 * (larg * altez)] of (foglie with [ipotetico? = false])

Upvotes: 3

Related Questions