1

I am new to NetLogo and I am still struggling with the links between patches and agents. I am building a land-use change model, where the agents are farmers. The patches in my model have a "lotid-farmer" value (to know which patch belongs to which farmer; all of them together correspond to the farmer's farm) and a "land-use" value. I am trying to count how many "land-use = 1" patches I have in each "lotid-farmer" (farms) and assign that to a variable that the agents have called "forest-size". I have tried many different things, like this piece of code (which does not work):

(foreach lotid-farmer count patches [ land-use = 1 ] set forest-size )

I wonder if anyone could explain why this statement makes no sense and suggest something else that could work or a tutorial on how to loop in NetLogo with "foreach"? Thank you in advance.

TWest
  • 765
  • 2
  • 6
  • 27
  • NetLogo is a specialised ABM language and, as such, works naturally with sets of agents. What you want is `ask` - which automatically commands all the agents (in a random order) to do whatever is in [ ] – JenB Mar 24 '16 at 21:38

1 Answers1

1

lotid is a value. foreach requires a list and a command-task. Also, your set operator doesn't have a value associated with it.

Actually, I wouldn't use a foreach and just ask farmers to set the variable. I'm going to assume lotid-farmer is the who of the farmer.

ask farmers [ 
   set forest-size count patches with [land-use = 1 and lotid-farmer = myself]
]
mattsap
  • 3,790
  • 1
  • 15
  • 36
  • or ... `lot-id-farmer = myself` if the variable lotid-farmer holds the agent rather than the who value of the agent – JenB Mar 24 '16 at 21:29
  • @Jenb, I timed the difference and it seems that [who] of myself is significantly quicker than storing myself. Maybe due to the way myself is stored (requires more memory?). – mattsap Mar 24 '16 at 22:09
  • Thank you so much, it worked. Also good to know that linking it to [who] instead of a new agent variable is an easy way to make things run faster. – TWest Mar 25 '16 at 02:32
  • @mattsap I'm very skeptical of your timing data there — that should definitely not be faster, and we definitely do not recommend coding this way (using who numbers instead of storing agents directly). if you're really sure you think you're seeing it be faster with who numbers, please file a bug report. (I am the former lead developer of NetLogo.) – Seth Tisue Mar 25 '16 at 19:08
  • @SethTisue, I'm repeating 10000 times for both, the assignment of a variable to self and to who of self. Maybe this is a better discussion outside of this thread? Where to file a bug report? There may be another issue such as caching at hand, but I'll let the bug team figure that out. – mattsap Mar 25 '16 at 23:13
  • You can file a bug report at https://github.com/netlogo/netlogo/issues. Or open a new Stack Overflow question about it first? Either way. – Seth Tisue Mar 26 '16 at 00:53
  • 1
    the speed question is at http://stackoverflow.com/questions/36253959/timing-discrepancy-between-in-netlogo (summary: it isn't faster with who numbers) – Seth Tisue Mar 28 '16 at 15:50