breed [cells cell]
cells-own [black? -p- next-state nbhd] ;; -p- is just p, cached to avoid recalculating
to startup setup end
to setup
clear-all
ask patches [set pcolor grey + 1.75 ;; neutral background color
sprout-cells 1 [set black? false set nbhd no-turtles update-color]]
if neighborhood = 3 ;; wraps along a torus ONLY IF world width and height are multiples of 4
[ask cells with [ifelse-value (pycor mod 4 < 2) [even? pxcor] [odd? pxcor]] [die]
ask cells [set heading ifelse-value even? pycor [180] [0]
carefully [fd 0.125] [die]] ;; don't move past world bounds
;; this arrangement fits evenly into the square, but is not fully symmetric:
;; the links are all 1.25 long, but the hexagons are still squished!
ask cells [create-links-with other cells in-radius 1.26]]
if neighborhood = 4
[ask cells [create-links-with cells-on neighbors4]]
if neighborhood = 6 ;; requires world height to be even; squishes the triangles a bit, too
[ask cells [if even? pycor [carefully [set xcor xcor + 0.5] [die]]
create-links-with other cells in-radius 1.26]]
if neighborhood = 8
[ask cells [create-links-with cells-on neighbors]]
if neighborhood = "random"
[ask cells [setxy random-xcor random-ycor]
ask cells [create-links-with other cells in-radius 2]] ;; could parameterize this radius
ask links [set thickness 0.1 set color grey - 1]
ask cells [set nbhd other (including extent)] ;; extended nbhd!
reset-ticks
end
to show-nbhd
ask min-one-of cells
[distancexy ((max-pxcor - min-pxcor) / 2) ((max-pycor - min-pycor) / 2)]
[set color green + 1 ask nbhd [set color sky + 2]]
end
to randomize ask cells [set black? (random 2 = 0) update-color] reset-ticks end
to blank ask cells [set black? false update-color] reset-ticks end
to edit
if mouse-inside? and mouse-down?
[ask min-one-of cells [distancexy mouse-xcor mouse-ycor]
[set black? not black? update-color]
display wait 0.16]
end
to go ;; synchronized update scheme
ask cells [set -p- p
set next-state ifelse-value black? ;; first calculate
[s-min <= -p- and -p- <= s-max]
[t-min <= -p- and -p- <= t-max]]
ask cells [set black? next-state update-color] ;; and then update
tick
end
to-report p ;; the proportion of black neighbors in a cell's neighborhood
let states [black?] of nbhd let total length states
if total = 0 [report 0] ;; avoid zero division
report (length filter [?] states) / total
end
to update-color
ifelse color-by-proportion?
[set shape ifelse-value black? ["black-cell"] ["white-cell"]
set color 9.9 - 9.9 * -p- ]
[set shape "dot" set color ifelse-value black? [black] [white]]
end
to-report including [n] ;; the (recursive) neighborhood extension procedure
ifelse n = 0
[report nobody]
[report (turtle-set link-neighbors ([including (n - 1)] of link-neighbors))]
end
to-report even? [n] report n mod 2 = 0 end to-report odd? [n] report n mod 2 = 1 end
to-report proportions report sort remove-duplicates ([precision p 3] of cells) end
@#$#@#$#@
GRAPHICS-WINDOW
233
40
771
599
-1
-1
11.0
1
10
1
1
1
0
1
1
1
0
47
0
47
1
1
1
ticks
30.0
BUTTON
116
40
222
85
NIL
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
8
290
113
330
go 1
go
NIL
1
T
OBSERVER
NIL
1
NIL
NIL
1
BUTTON
115
290
221
330
NIL
go
T
1
T
OBSERVER
NIL
G
NIL
NIL
1
CHOOSER
6
40
114
85
neighborhood
neighborhood
3 4 6 8 "random"
3
SLIDER
6
87
114
120
extent
extent
1
4
1
1
1
hops
HORIZONTAL
BUTTON
116
87
222
120
NIL
show-nbhd
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
7
189
113
222
t-min
t-min
0
t-max
0.26
0.01
1
NIL
HORIZONTAL
SLIDER
115
189
220
222
t-max
t-max
t-min
1
0.49
0.01
1
NIL
HORIZONTAL
SLIDER
7
154
113
187
s-min
s-min
0
s-max
0.13
0.01
1
NIL
HORIZONTAL
SLIDER
115
154
220
187
s-max
s-max
s-min
1
0.49
0.01
1
NIL
HORIZONTAL
PLOT
9
433
222
598
proportion distribution
- p -
# cells
0.0
1.0
0.0
250.0
true
false
"set-plot-pen-mode 1\nset-histogram-num-bars 2 + [count nbhd] of max-one-of cells [count nbhd]" ""
PENS
"default" 1.0 0 -16777216 true "" "set-plot-y-range 0 100 histogram [-p-] of cells"
BUTTON
8
255
86
288
NIL
randomize
NIL
1
T
OBSERVER
NIL
R
NIL
NIL
1
BUTTON
88
255
160
288
NIL
blank
NIL
1
T
OBSERVER
NIL
B
NIL
NIL
1
BUTTON
162
255
221
288
NIL
edit
T
1
T
OBSERVER
NIL
E
NIL
NIL
1
SWITCH
9
362
222
395
color-by-proportion?
color-by-proportion?
1
1
-1000
TEXTBOX
9
10
390
37
Additive Thresholding Cellular Automata
18
0.0
1
TEXTBOX
14
126
216
144
configure the neighborhood system
11
3.0
1
TEXTBOX
14
228
224
246
set parameters of the threshold rule
11
3.0
1
TEXTBOX
11
335
222
353
control system state and advance time
11
3.0
1
TEXTBOX
16
399
213
431
display each cell's proportion of black neighbors 'p' as a grey ring?
11
3.0
1
@#$#@#$#@
## Additive thresholding cellular automata
John Conway's "Game of Life" cellular automaton is probably the most popular and most thoroughly studied CA rule. It uses an **additive** rule, with two cell states (here, "black" and "white") on a square grid, where a cell neighborhood includes the four 'diagonal' neighbors. The rule called _additive_ because it's defined in terms of the sum _n_, which is the number of black cells in the cell's outer 8-neighborhood:
* A black cell remains black on the next step
if 2 ≤ _n_ ≤ 3, otherwise becoming white, and
* A white cell becomes black on the next step
if 3 ≤ _n_ ≤ 3, otherwise remaining white
The upper and lower bounds of these two **thresholds** can be parameterized to yield an interesting family of different rules which are independent of neighborhood structure, respecting all geometric symmetries in every neighborhood, regardless of size or shape.
## How it works
There are several different options for the underlying neighborhood structure, including all three regular polygon tilings of the plane, the 8-neighborhood on the square grid, and a randomized neighborhood system, explained below. The neighborhoods can also be _extended_ to include the neighbors' neighbors, up to a given number of links distance.
Where _p_ is the **proportion**, 0 ≤ _p_ ≤ 1, of black cells in a cell's neighborhood, not including itself, on the next step
* A black cell **stays** black whenever _S__{min} ≤ _p_ ≤ _S__{max}, otherwise becoming white, and
* A white cell **turns** black whenever _T__{min} ≤ _p_ ≤ _T__{max}, otherwise remaining white
This is a _thresholding_ rule specified by two sub-intervals _S_ and _T_ of the unit interval [0,1]. If, like Conway, we were to call black cells "alive" and white cells "dead", then the intervals _S_ and _T_ can be thought of as the conditions required to "**survive**" or to "**thrive**" (in Conway's terms "non-death" and "birth"); respectively, the regimes of stability and increase.
Specifying the four parameters _S__{min}, _S__{max}, _T__{min}, _T__{max} allows you to adjust the behavior of the rule independent of the neighborhood geometry. The model also displays a histogram showing the relative frequency, over the entire grid of cells, of each of the possible values of _p_. You may choose to color cells by their _p_-values rather than just directly by their state: if **color-by-p?** is set On, then each cell is displayed inside a grey ring: darker greys correspond to higher _p_ values.
While the **edit** button is active, you can click on a cell to toggle its state between black and white. The **randomize** and **blank** buttons reset every cell's state without making a new neighborhood system.
## The neighborhood systems
The model supplies five options for a **neighborhood**. Those labeled 3, 4, and 6 correspond to triangular, square, and hexagonal tilings of the plane. By default, this model uses a torus-shaped space, which wraps top to bottom and left to right. With this rule scheme, it is also entirely possible to use a bounded space that _doesn't_ wrap around. If you want to use a torus of a different size, note well that the 3-neighborhood requires that both `world-width` and `world-height` be integer multiples of 4, while the 6-neighborhood needs `world-height` to be an even number.
The 8-neighborhood on the square tiling, which includes diagonal neighbors, is also provided. This 8-neighborhood is used in the Game of Life CA, and is sometimes called the _Moore neighborhood_. Finally, a "random" option places as many cells as there are patches (like the 4 and 8 neighborhoods), but at locations randomly chosen from a uniform probability distribution. Cells in the random neighborhood are connected with all other cells within a fixed distance, creating an irregular mesh in which cells may have different numbers of neighbors. This works because the thresholds are defined in terms of proportions, rather than fixed numbers: at any particular step, a cell with one black neighbor out of 3 has a _p_ value of 0.333, just the same as a cell with two black neighbors out of 6.
It's also possible to **extend** any of the five neighborhoods by including the "neighbors' neighbors". A two-hop extension of the 4-neighborhood has 12 _outer neighbor_ cells around the central cell, and a three-hop extension has 24 outer neighbors. The currently active neighborhood can always be seen by using the **show-nbhd** button.
## Due credit
This model was developed for the Complexity Explorer project by Max Orhai, 2012.
NetLogo is copyright Uri Wilensky and the Center for Connected Learning at Northwestern University.
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250
black-cell
false
0
Circle -16777216 true false 0 0 300
Circle -7500403 true true 15 15 270
Circle -16777216 true false 75 75 148
circle
false
0
Circle -7500403 true true 0 0 300
dot
false
0
Circle -16777216 true false 30 30 240
Circle -7500403 true true 45 45 210
white-cell
false
0
Circle -16777216 true false 0 0 300
Circle -7500403 true true 15 15 270
Circle -16777216 true false 75 75 148
Circle -1 true false 90 90 120
@#$#@#$#@
NetLogo 5.0.1
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
default
0.0
-0.2 0 0.0 1.0
0.0 1 1.0 0.0
0.2 0 0.0 1.0
link direction
true
0
Line -7500403 true 150 150 90 180
Line -7500403 true 150 150 210 180
@#$#@#$#@
0
@#$#@#$#@