Patch Clusters Example

Patch Clusters Example preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

code example 

Tagged by Reuven M. Lerner about 11 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 2117 times • Downloaded 126 times • Run 0 times
Download the 'Patch Clusters Example' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Great - suggestions to run faster with larger grid? (Question)

Hi there, I've used this example to successfully identify "habitat" clusters in a simulated landscape. This is great, but I was wondering if anyone had suggestions for improving setup speed when the size of the world expands. Right now, the two loops take a long time to run (which makes sense) - is there an alternate route? Thanks! CD

Posted about 11 years ago

Faster startup time

Hi, Chris. The two loops you mentioned are (a) setting up the patches, and (b) repeatedly changing the patch colors based on neighbors' colors. There's no way to parallelize things in NetLogo, so the process of changing the colors is a slow one. You could potentially speed things up by reducing the number of loop iterations from 20 to something smaller. But that'll obviously mean that there is a less pronounced effect. At least, that's how it seems to me; someone else might have a better suggestion.

Posted about 11 years ago

Click to Run Model

patches-own
[
  cluster   ;; Holds a patch which is that cluster's "leader".
  ;; it's arbitrary which patch is used to identify
  ;; the cluster -- whichever one we happen to find
  ;; first will do.  If this patch hasn't been assigned
  ;; to a cluster yet, then this holds "nobody".
]

to setup
  clear-all
  ask patches
  [ ;; use dark colors so the labels are visible
    set pcolor 3 + 10 * random 14
    ;; initially, we're in no cluster
    set cluster nobody ]
  ;; by spreading colors from patch to patch, connected areas
  ;; that are all the same color will emerge
  repeat 20
  [ ask patches
    [ set pcolor [pcolor] of one-of neighbors4 ] ]
end 

to find-clusters
  loop [
    ;; pick a random patch that isn't in a cluster yet
    let seed one-of patches with [cluster = nobody]
    ;; 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 ]
  ]
end 

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

;; once all the clusters have been found, this is called
;; to put numeric labels on them so the user can see
;; that the clusters were identified correctly

to show-clusters
  let counter 0
  loop
  [ ;; pick a random patch we haven't labeled yet
    let p one-of patches with [plabel = ""]
    if p = nobody
      [ stop ]
    ;; give all patches in the chosen patch's cluster
    ;; the same label
    ask p
    [ ask patches with [cluster = [cluster] of myself]
      [ set plabel counter ] ]
    set counter counter + 1 ]
end 


; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky almost 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky over 11 years ago Updated version tag Download this version
Uri Wilensky over 12 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky almost 14 years ago Patch Clusters Example Download this version
Uri Wilensky almost 14 years ago Patch Clusters Example Download this version

Attached files

File Type Description Last updated
Patch Clusters Example.png preview Preview for 'Patch Clusters Example' about 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.