Javier Sandoval
Javier Sandoval

Reputation: 507

Netlogo cluster count size: stack overflow (recursion too deep)

In a land use model, I want to count the size of green and red clusters as shown in this image:

enter image description here

The code used is very similar to that found in "Patch Clusters example" from Models Library, with the only difference is that it only counts red and green patches. But when I run it, Netlogo states "stack overflow (recursion too deep) error while observer running ASK called by procedure FIND-CLUSTERS". Here is the find-clusters procedure:

to find-clusters
  loop [
    ;; pick a random patch that isn't in a cluster yet
    let seed one-of patches with [cluster = nobody and pcolor = 64 or 
pcolor = 14]
    ;; if we can't find one, then we're done!
    if seed = nobody
    [ show-clusters
      stop ]
    ;; otherwise, make the patch the "leader" of a new cluster
    ;; by assigning itself to its own cluster, then call
    ;; grow-cluster to find the rest of the cluster
    ask seed
    [ set cluster self
      grow-cluster ]
  ]
  display
end

and the grow-cluster procedure:

to grow-cluster  ;; patch procedure
  ask neighbors4 with [(cluster = nobody) and
    (pcolor = [pcolor] of myself)]
  [ set cluster [cluster] of myself
    grow-cluster ]
end

What does the message mean and how can I fix it? Thanks.

Upvotes: 0

Views: 105

Answers (1)

StephenGuerin
StephenGuerin

Reputation: 871

double-check your first if statement.

let seed one-of patches with [cluster = nobody and pcolor = 64 or 
pcolor = 14]

You will always find a patch that is "false and false or true" and will never exit the loop. Think where to put your parenthesis for order of operations.

Upvotes: 2

Related Questions