Neighborhoods Example
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
This code example shows how to use the basic neighborhood primitives. These include neighbors
, neighbors4
, in-radius
, and at-points
:
neighbors
: reports the patches surrounding a patch or turtle both patches that share a side and those that share a corner. If the world is a torus every patch will have eight neighbors, if wrapping is not allowed in either direction the edge patches will have fewer neighbors.neighbors4
: reports the same patches, excluding the ones at the corners. On a torus there will always be four neighbors.<agentset> in-radius <number>
: reports those agents in agentset whose distance from the caller is less than or equal to n.<agentset> at-points <list>
: reports those agents in agentset at the given points relative to the caller, wherelist
is a list of two-number lists.
Although in this example, we only change the color of the reported patches, you can of course do more complicated operations, including assigning that agentset to a variable. This is particularly useful in cellular automata models.
RELATED MODELS
Moore & Von Neumann Example shows how to use square (Moore) or diamond (Von Neumann) shaped neighborhoods of any size.
To see neighbors
and neighbors4
in the sample models see Shepherds, Altruism, GenDrift P local, Ising, Crystallization models, CA models, Fire, Rumor Mill, Segregation, and Voting.
To see in-radius
in the sample models see Fireflies, Flocking, and Simple Kinetics 3.
To see at-points
in the sample models see the Crystallization models, and Wave Machine.
Comments and Questions
;; This procedure creates 8 turtles in puts them in a circle, ;; and moves three to the edges of the world to setup ca crt 8 [ fd 10 ] ask turtle 0 [ fd 7 ] ask turtle 1 [ fd 14 ] ask turtle 2 [ fd 7 ] end ;; This procedure colors all the patches neighboring each turtle. ;; neighbors reports the eight patches surrounding the turtle. ;; Note that patches have neighbors as well, and can also use the neighbors reporter. to paint-neighbors clear-patches ask turtles [ paint-agents neighbors ] end ;; This procedure is similar to paint-neighbors, but uses the neighbors4 reporter. ;; neighbors4 reports only the four patches adjacent to the current patch, and does not include ;; the four neighbors which are only diagonally touching the current patch. ;; As with neighbors, both turtles and patches can use neighbors4. to paint-neighbors4 clear-patches ask turtles [ paint-agents neighbors4 ] end ;; This procedure uses in-radius to paint a "circle" around each turtle. ;; "agentset in-radius n" reports those agents in agentset whose distance from the current agent ;; is less than or equal to n. For patches, distance is measured from the center of the patch. ;; Note that the caller can be a turtle or a patch, and agentset can be a set of turtles or a set of ;; patches, so you can also use in-radius to locate turtles within a given radius ;; of another turtle or patch. Also notice that the reported agentset includes the patch that the ;; turtle is currently on. to paint-in-radius clear-patches ask turtles [ paint-agents patches in-radius radius ] end ;; This procedure uses the at-points reporter to paint an arbitrary neighborhood. ;; at-points lets you specify groups of agents on individual patches, using points ;; relative to the calling agent (so [0 0] indicates the patch the agent is on). ;; As with the above primitives, at-points can be used by both turtles and patches. to paint-at-points clear-patches ask turtles [ if points = "corners" [ paint-agents patches at-points [[1 1] [-1 1] [-1 -1] [1 -1 ]] ] if points = "left" [ paint-agents patches at-points [[-1 1] [-1 0] [-1 -1]] ] if points = "L-shape" [ paint-agents patches at-points [[-1 1] [-1 0] [-1 -1] [0 -1] [1 -1 ]] ] if points = "line-up" [ paint-agents patches at-points [[0 1] [0 2] [0 3] [0 4]] ] ] end ;; This is a helper procedure, used to set the color of a set of patches. to paint-agents [agents] ask agents [ set pcolor [color] of myself - 2 ] 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.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Neighborhoods Example.png | preview | Preview for 'Neighborhoods Example' | over 12 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.