1-d cellular automaton in a graph grammar by markgritter

View this thread on steempeak.com
· @markgritter ·
$0.48
1-d cellular automaton in a graph grammar
This is the first example I tried that really pushed my engine hard. It would be better broken up into multiple grammars, I think.  I wanted to run [Rule 30](http://mathworld.wolfram.com/Rule30.html), a 1-d cellular automata, as a graph grammar in [Soffit](https://github.com/mgritter/soffit).

I decided to split up the construction into two phases, because I tried and failed several times to make "generate a row" and "implement the transition rule" work together.  This way, the basic cell structure can be re-used for different rules fairly easily.

This grammar uses the techniques described [here](https://steemit.com/procjam/@markgritter/a-complicated-soffit-example) and [here](https://steemit.com/programming/@markgritter/controlling-graph-grammar-expansion-with-a-countdown) to grow a set of cells one row at a time.  A cursor is used to grow the cells in a deterministic fashion, and a counter is used to limit the total number of rows, and tell us when it's done.

```
    "start" : "E1->X1->E2 [h]; E1[e]; E2[e]; X1[x]; S->X->C[h]; S[start]; X[x]; C[cursor]; E1->X[v]; X1->C[v]; R[row]; RE[end]; R->R1->R2->R3->R4->R5->R6->R7->R8->R9->RE",
    
    "X[x]; P[x]; P1[x]; C[cursor]; X->C [h]; P->C[v]; P->P1[h]" :
    "X[x]; P[x]; P1[x]; C[x];      X->C [h]; P->C[v]; P->P1[h]; N[cursor]; P1->N[v]; C->N[h]",
    
    "X[x]; P[x]; P1[e]; C[cursor]; X->C [h]; P->C[v]; P->P1[h]" :
    "X[x]; P[x]; P1[e]; C[x];      X->C [h]; P->C[v]; P->P1[h]; N[cursor]; P1->N[v]; C->N[h]",
    
    "X[x]; P[e]; C[cursor]; X->C [h]; P->C[v];" :
    "X[x]; P[e]; C[x];      X->C [h]; P->C[v]; N[cursor]; C->N[h]",
    
    "S[start]; XL[x]; EL[e]; S->XL [h]; EL->XL[v]; C[cursor]; XR[x]; ER[e]; ER->XR[v]; XR->C[h]; R[row]; RN; R->RN;" :
    "S[e];     XL[x]; EL[e]; S->XL [h]; EL->XL[v]; C[e];      XR[x]; ER[e]; ER->XR[v]; XR->C[h]; RN[row]; NewS[start]; NewX[x]; NewC[cursor]; NewS->NewX->NewC [h]; S->NewX[v]; XL->NewC[v];",

    "S[start]; XL[x]; EL[e]; S->XL [h]; EL->XL[v]; C[cursor]; XR[x]; ER[e]; ER->XR[v]; XR->C[h]; R[row]; RE[end]; R->RE" :
    "S[e];     XL[x]; EL[e]; S->XL [h]; EL->XL[v]; C[e];      XR[x]; ER[e]; ER->XR[v]; XR->C[h]; Z[initialcell]",
``` 

Then we populate the original row, and convert any "edge" cells into two "0" cells so that we can apply the rules to them correctly.  This would be cleaner if we did it in a separate pass instead of having to deal with the possibility that some cells may have been filled in already.

```
    "Z[initialcell]; E1[e]; X[x]; E2[e]; E1->X->E2 [h]" :
    "Z[style=invis]; E1[e]; X[1]; E2[e]; E1->X->E2 [h]",

    "Z[style=invis];       A[e]; B[x]; A->B[h];" :
    "Z[style=invis]; N[0]; A[0]; B[x]; N->A->B[h];",

    "Z[style=invis];       A[e]; B[x]; A<-B[h];" :
    "Z[style=invis]; N[0]; A[0]; B[x]; N<-A<-B[h];",

    "Z[style=invis];       A[e]; B[0]; A->B[h];" :
    "Z[style=invis]; N[0]; A[0]; B[0]; N->A->B[h];",

    "Z[style=invis];       A[e]; B[0]; A<-B[h];" :
    "Z[style=invis]; N[0]; A[0]; B[0]; N<-A<-B[h];",

    "Z[style=invis];       A[e]; B[1]; A->B[h];" :
    "Z[style=invis]; N[0]; A[0]; B[1]; N->A->B[h];",

    "Z[style=invis];       A[e]; B[1]; A<-B[h];" :
    "Z[style=invis]; N[0]; A[0]; B[1]; N<-A<-B[h];",
```

The cellular automaton rules themselves are completely straightforward once all that setup has been done.

I used fake rules for comments.  This is one severe weakness of using JSON.  I'm thinking of scrapping it, since I don't think I'll be doing a JavaScript version of this engine.

```
    "X[rule 000 -> 0]" : "",
    
    "A[0]; B[0]; C[0]; A->B->C [h]; N[x]; B->N [v]" :
    "A[0]; B[0]; C[0]; A->B->C [h]; N[0]; B->N [v]",

    "X[rule 001 -> 1]" : "",
    
    "A[0]; B[0]; C[1]; A->B->C [h]; N[x]; B->N [v]" :
    "A[0]; B[0]; C[1]; A->B->C [h]; N[1]; B->N [v]",

    "X[rule 010 -> 1]" : "",
    
    "A[0]; B[1]; C[0]; A->B->C [h]; N[x]; B->N [v]" :
    "A[0]; B[1]; C[0]; A->B->C [h]; N[1]; B->N [v]",

    "X[rule 011 -> 1]" : "",
    
    "A[0]; B[1]; C[1]; A->B->C [h]; N[x]; B->N [v]" :
    "A[0]; B[1]; C[1]; A->B->C [h]; N[1]; B->N [v]",

    "X[rule 100 -> 1]" : "",
    
    "A[1]; B[0]; C[0]; A->B->C [h]; N[x]; B->N [v]" :
    "A[1]; B[0]; C[0]; A->B->C [h]; N[1]; B->N [v]",

    "X[rule 101 -> 0]" : "",
    
    "A[1]; B[0]; C[1]; A->B->C [h]; N[x]; B->N [v]" :
    "A[1]; B[0]; C[1]; A->B->C [h]; N[0]; B->N [v]",

    "X[rule 110 -> 0]" : "",
    
    "A[1]; B[1]; C[0]; A->B->C [h]; N[x]; B->N [v]" :
    "A[1]; B[1]; C[0]; A->B->C [h]; N[0]; B->N [v]",

    "X[rule 111 -> 0]" : "",
    
    "A[1]; B[1]; C[1]; A->B->C [h]; N[x]; B->N [v]" :
    "A[1]; B[1]; C[1]; A->B->C [h]; N[0]; B->N [v]"
```

Completely expanding this set of rules into a final graph takes about 80 seconds; the slow part is the grid expansion.  I thought that should be straightforward and efficient because it's based around the unique "cursor" node, but I'll have to look into what the constraint solver is actually doing.

After some hand-editing of the output to make 1's black squares and 0's white squares, this is the output, which graphviz *almost* displays in a triangle:

![rule-30.png](https://cdn.steemitimages.com/DQmV5eGi1cKPaw1y6QvNFcibmM4mR7mnqW5Vzfwv8ELYSAV/rule-30.png)

This is probably one of the least efficient methods of implementing a 1-d cellular automata possible.  But as a demonstration of the capabilities of a graph grammar, I think it's pretty cool.

The problem with doing the conversion from 1 and 0 to graphviz attributes inline is that there's no good way to say "OK, the CA part is done, now make it pretty" so the CA rules would have to accept the styled versions.  I started out doing that and it was horrible.  Multiple passes is a feature I'm going to add sooner rather than later. 

A more radical change to Soffit would be to allow ASCII diagrams rather than Dot notation.  My notes (like these):

```
   eXe
  SXXC
  
   eXe
  SXXXC

   eXe
  eXXXe
 SXC
 ```

are much clearer than the final rule set.  But does this only work when writing planar graphs? Would making all the edges explicit make this just as much a mess as the current format?
👍  , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id65,570,432
authormarkgritter
permlink1-d-cellular-automaton-in-a-graph-grammar
categoryprocjam
json_metadata{"links":["http:\/\/mathworld.wolfram.com\/Rule30.html","https:\/\/github.com\/mgritter\/soffit","https:\/\/steemit.com\/procjam\/@markgritter\/a-complicated-soffit-example","https:\/\/steemit.com\/programming\/@markgritter\/controlling-graph-grammar-expansion-with-a-countdown"],"app":"steemit\/0.1","format":"markdown","tags":["procjam","soffit","programming","graph","cellularautomata"],"image":["https:\/\/cdn.steemitimages.com\/DQmV5eGi1cKPaw1y6QvNFcibmM4mR7mnqW5Vzfwv8ELYSAV\/rule-30.png"]}
created2018-11-06 08:25:12
last_update2018-11-06 08:25:12
depth0
children2
net_rshares450,377,887,727
last_payout2018-11-13 08:25:12
cashout_time1969-12-31 23:59:59
total_payout_value0.372 SBD
curator_payout_value0.109 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length6,097
author_reputation7,115,775,244,714
root_title"1-d cellular automaton in a graph grammar"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (27)
@ilovecoding ·
Hello! Your post has been resteemed and upvoted by @ilovecoding because **we love coding**! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On! 
 ![](https://codingforspeed.com/images/i-love-coding.jpg) 
*Reply !stop to disable the comment. Thanks!*
👍  
properties (23)
post_id65,570,439
authorilovecoding
permlink20181106t082527021z
categoryprocjam
json_metadata{"app":"ilovecoding","tags":["ilovecoding"]}
created2018-11-06 08:25:27
last_update2018-11-06 08:25:27
depth1
children0
net_rshares359,116,239
last_payout2018-11-13 08:25:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length323
author_reputation40,842,386,526
root_title"1-d cellular automaton in a graph grammar"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@geekgirl ·
Looks like you are bored. :)
properties (22)
post_id65,575,374
authorgeekgirl
permlinkre-markgritter-1-d-cellular-automaton-in-a-graph-grammar-20181106t103723518z
categoryprocjam
json_metadata{"app":"steemit\/0.1","tags":["procjam"]}
created2018-11-06 10:37:24
last_update2018-11-06 10:37:24
depth1
children0
net_rshares0
last_payout2018-11-13 10:37:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length28
author_reputation169,390,437,178,762
root_title"1-d cellular automaton in a graph grammar"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000