TWest
TWest

Reputation: 824

Count number of patches with specific value within specific clusters of patches

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.

Upvotes: 1

Views: 426

Answers (1)

mattsap
mattsap

Reputation: 3806

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]
]

Upvotes: 1

Related Questions