# Increase efficiency of cellular automaton and combine values in datasets

**URL:** <https://community.wolfram.com/t/increase-efficiency-of-cellular-automaton-and-combine-values-in-datasets/15734>\
**Category:** Questions\
**Tags:** physics\
**Created:** [April 13, 2020, 8:48am UTC](https://community.wolfram.com/t/increase-efficiency-of-cellular-automaton-and-combine-values-in-datasets/15734 "2020-04-13T08:48:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrickett](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jrickett](https://community.wolfram.com/u/jrickett)\
**Post date:** [April 13, 2020, 8:48am UTC](https://community.wolfram.com/t/increase-efficiency-of-cellular-automaton-and-combine-values-in-datasets/15734/1 "2020-04-13T08:48:20Z")

</div>

Hi all,

I’m working on a model of self-organised critical systems following [B. Drossel and F. Schwabl, Physical Review Letters 69, 1629 (1992)] forest fire model to create a cellular automaton which counts the total number of fires within the lattice.

Given a matrix (100 x 100) with randomly generated integer values in range [0,2], on each timestep t, the matrix values will be updated according to 3 rules which change the values within the matrix, 0 → 1 if rule1, 1 → 2 if rule 2 etc. This would run over t time steps and then I would run the code “sample” number of times to create a pdf of the total count of each site.

I have attached the nb. Advice on increasing efficiency of the calculations is also more than welcome.

What I am trying to do next is create a dataset which notes the time taken for a 2 in the matrix to change back to a 2 after a series of rules has changed it cyclically from 2-\>0-\>1-\>2. Essentially looking at the wait times between the 2’s occuring at a specific site.

To do this, I created a dataset:

```
wait = Dataset[{<|"run,i,j" -> {y, i, j}, "t" -> ii|>}];

```

Aiming to note which run of the code, where in the matrix the value 2 lies, and at what timestep it appears on. I would like to be able to append only the t values for a given value of {y, i , j} without duplication.

My first attempt to test these changes went like this:

```
n = 20;
sample = 4;
t = 10;
n = 20;
wait = Dataset[{<|"run,i,j" -> {y, i, j}, "t" -> ii|>}];
Nmat = Table[RandomInteger[{0, 1}], {n}, {n}];

For[y = 1, y < sample + 1, y++,
 
 For[ii = 1, ii < t + 1, ii++,
  
  For[i = 1, i < n + 1, i++,
   	For[j = 1, j < n + 1, j++,
    
    If[Nmat[[i, j]] == 1,
     (*Add a value into the dataset noting the run and matrix \
component*)
     If[MemberQ[waittimes[[All, 1]], {y, i, j}],
      ]
     ]
    ]
   ]
  ]
 ]

```

However I get stuck on changing the values within the specifi row of the dataset.  
For example if a 2 occurs at “run,i,j” = {1,1,1} for t = 1 and then t = 5, I would like to append only the t column within the dataset where the specific row is {1,1,1}

Any help is more than welcome!

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/5ab00649-1769-4834-8e6d-531b094bc88a)

---

<div class="post-metadata">

**Author:** ![samcarrettie](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/samcarrettie/32/29668_2.png) [@samcarrettie](https://community.wolfram.com/u/samcarrettie)\
**Post date:** [April 13, 2020, 11:31am UTC](https://community.wolfram.com/t/increase-efficiency-of-cellular-automaton-and-combine-values-in-datasets/15734/2 "2020-04-13T11:31:35Z")

</div>

Are you sure your algorithm cannot be implemented via [CellularAutomaton](http://reference.wolfram.com/language/ref/CellularAutomaton.html) function? See Details section (especially "general function to apply to each list of neighbors ") and many examples.

---

<div class="post-metadata">

**Author:** ![jrickett](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jrickett](https://community.wolfram.com/u/jrickett)\
**Post date:** [April 13, 2020, 11:35am UTC](https://community.wolfram.com/t/increase-efficiency-of-cellular-automaton-and-combine-values-in-datasets/15734/3 "2020-04-13T11:35:40Z")

</div>

I’m sure it probably could, however I have been struggling to interpret the implementation of the required rules into the CellularAutomaton function.

I labelled each site as:

```
0 - black site
1 - green site
2 - red site

```

The rules required for update at each time step are:  
i) red site becomes black  
ii) green site becomes red if one of it’s neighbors is red, otherwise it becomes red with probability f  
iii) a black site becomes green with probability p.

Any assistance with the CellularAutomaton function is welcome too though.

---

<div class="post-metadata">

**Author:** ![jrickett](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jrickett](https://community.wolfram.com/u/jrickett)\
**Post date:** [April 17, 2020, 7:08am UTC](https://community.wolfram.com/t/increase-efficiency-of-cellular-automaton-and-combine-values-in-datasets/15734/4 "2020-04-17T07:08:33Z")

</div>

Hi Sam,

I recently found some code which has helped:

```
evolve[nbhd_List, k_] := 0 /; nbhd[[2, 2]] == 2 (*burning->empty*)
evolve[nbhd_List, k_] := 2 /; nbhd[[2, 2]] == 1 && Max@nbhd == 2 (*near_burning&nonempty->burning*)
evolve[nbhd_List, k_] := RandomChoice[{f, 1 - f} -> {2, nbhd[[2, 2]]}] /; nbhd[[2, 2]] == 1 && Max@nbhd < 2 (*spontaneously combusting tree*)
evolve[nbhd_List, k_] := RandomChoice[{p, 1 - p} -> {1, nbhd[[2, 2]]}] /; nbhd[[2, 2]] == 0 (*random tree growth*)
 
r = 100; c = 100; p = 10^-2; f = 10^-4;
init = RandomInteger[BernoulliDistribution[0.05], {r, c}];
MatrixPlot[CellularAutomaton[{evolve, {}, {1, 1}}, {init, 0}, {{{300}}}], ColorRules -> {0 -> White, 1 -> Green, 2 -> Red}, Frame -> False]

```

However, this is for a 9-neighbourhood region. Do you know how I can implement a 5-neighbourhood totalistic region for this?
