I'm starting to play with Golly's tree generator. I've chosen Lua, but there's also Python and others. Here is a generator provided in the zip as an example:
-- This Lua script can be used to create tree data for inclusion in a Golly .rule file.
-- (Run it from a command line, not from within Golly.)
--------------------------------------------------------------------------------
function GenerateRuleTree(numStates, numNeighbors, transitionFunc)
local numParams = numNeighbors + 1
local world = {}
local r = {}
local nodeSeq = 0
local params = {}
for i = 0, numParams-1 do params[i] = 0 end
local function getNode(n)
if world[n] then return world[n] end
local new_node = nodeSeq
nodeSeq = nodeSeq + 1
r[#r+1] = n
world[n] = new_node
return new_node
end
local function recur(at)
if at == 0 then return transitionFunc(params) end
local n = tostring(at)
for i = 0, numStates-1 do
params[numParams-at] = i
n = n.." "..recur(at-1)
end
return getNode(n)
end
recur(numParams)
print("num_states="..numStates)
print("num_neighbors="..numNeighbors)
print("num_nodes="..#r)
for i = 1, #r do print(r[i]) end
end
--------------------------------------------------------------------------------
-- define your own transition function here:
function my_transition_function(a)
-- this code is for B3/S23
local n = a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7]
if n == 2 and a[8] ~= 0 then
return 1
end
if n == 3 then
return 1
end
return 0
end
--------------------------------------------------------------------------------
-- call the rule tree generator with your chosen parameters:
local n_states = 2
local n_neighbors = 8
GenerateRuleTree(n_states, n_neighbors, my_transition_function)
What it does is memoizing (https://en.wikipedia.org/wiki/Memoization) the behavior of the CA. You can comfortably define the behavior in your language of choice, then build a tree out of it. The speed of the CA you get is unbelievable.
Now I'll try to make a simple 2-layers B3/S23 (the typical GOL).
Proof of concept works perfectly! We now have 2 independent layers of game of life, on the same grid!! The code looks like this (quick&dirty):
-- This Lua script can be used to create tree data for inclusion in a Golly .rule file.
-- (Run it from a command line, not from within Golly.)
--------------------------------------------------------------------------------
function GenerateRuleTree(numStates, numNeighbors, transitionFunc)
local numParams = numNeighbors + 1
local world = {}
local r = {}
local nodeSeq = 0
local params = {}
for i = 0, numParams-1 do params[i] = 0 end
local function getNode(n)
if world[n] then return world[n] end
local new_node = nodeSeq
nodeSeq = nodeSeq + 1
r[#r+1] = n
world[n] = new_node
return new_node
end
local function recur(at)
if at == 0 then return transitionFunc(params) end
local n = tostring(at)
for i = 0, numStates-1 do
params[numParams-at] = i
n = n.." "..recur(at-1)
end
return getNode(n)
end
recur(numParams)
print("num_states="..numStates)
print("num_neighbors="..numNeighbors)
print("num_nodes="..#r)
for i = 1, #r do print(r[i]) end
end
--------------------------------------------------------------------------------
-- define your own transition function here:
function my_transition_function(a)
local result = 0
local n1 = (a[0]&1) + (a[1]&1) + (a[2]&1) + (a[3]&1) + (a[4]&1) + (a[5]&1) + (a[6]&1) + (a[7]&1)
if n1 == 2 and (a[8]&1) ~= 0 then
result = result + 1
end
if n1 == 3 then
result = result + 1
end
local n2 = (a[0]&2) + (a[1]&2) + (a[2]&2) + (a[3]&2) + (a[4]&2) + (a[5]&2) + (a[6]&2) + (a[7]&2)
if n2 == 4 and (a[8]&2) ~= 0 then
result = result + 2
end
if n2 == 6 then
result = result + 2
end
return result
end
--------------------------------------------------------------------------------
-- call the rule tree generator with your chosen parameters:
local n_states = 4
local n_neighbors = 8
GenerateRuleTree(n_states, n_neighbors, my_transition_function)
Luckily, Wireworld (https://en.wikipedia.org/wiki/Wireworld) is 4 states only! I'm starting to implement:
- 1 layer Game of life
- 1 layer Wireworld
Edit: done! it works. 8)
function my_transition_function(a)
local result = 0
-- Game of life layer
-- 1 full
-- 0 empty
local n1 = (a[0]&1) + (a[1]&1) + (a[2]&1) + (a[3]&1) + (a[4]&1) + (a[5]&1) + (a[6]&1) + (a[7]&1)
if n1 == 2 and (a[8]&1) ~= 0 then
result = result + 1
end
if n1 == 3 then
result = result + 1
end
-- Wireworld layer
-- 00 empty
-- 01 electron head
-- 10 electron tail
-- 11 conductor
local cell = a[8] & 6
if cell == 2 then -- if head
result = result + 4 -- then tail
elseif cell == 4 then -- if tail
result = result + 6 -- then conductor
elseif cell == 6 then -- if conductor
-- count electron heads around
local n2 = 0
if a[0]&6 == 2 then n2 = n2 + 1 end
if a[1]&6 == 2 then n2 = n2 + 1 end
if a[2]&6 == 2 then n2 = n2 + 1 end
if a[3]&6 == 2 then n2 = n2 + 1 end
if a[4]&6 == 2 then n2 = n2 + 1 end
if a[5]&6 == 2 then n2 = n2 + 1 end
if a[6]&6 == 2 then n2 = n2 + 1 end
if a[7]&6 == 2 then n2 = n2 + 1 end
if n2 == 1 or n2 == 2 then
result = result + 2
else
result = result + 6
end
end
return result
end
Cyan cells are Game of life, yellow is Wireworld conductor, red is electron heads and tails. Now this is where the fun begins: connecting them so they sometimes interact...
The nice thing about it is when you memoize the rule, you have to wait for more than 1 minute for the function to "compile" the behavior tree... feels like good old compilation sessions from the past, when you had to wait, without breathing, for the compiler to proceed.
First test:
When it's an electron tail without any electron head around, then create a GOL cell.
Edit:
Works like a charm! I put a stable GOL structure at the end of a wire, then send an electron through the wire and the structure is activated.
That the "write" part. Now need a "read" part.
New rule:
When 3 GOL cells around here, if there's only 1 conductor around, create an electron head.
Edit:
Done!
function my_transition_function(a)
local result = 0
local gol = 0
local babyElectron = 0
-- count electron heads around
local ne = 0
if a[0]&6 == 2 then ne = ne + 1 end
if a[1]&6 == 2 then ne = ne + 1 end
if a[2]&6 == 2 then ne = ne + 1 end
if a[3]&6 == 2 then ne = ne + 1 end
if a[4]&6 == 2 then ne = ne + 1 end
if a[5]&6 == 2 then ne = ne + 1 end
if a[6]&6 == 2 then ne = ne + 1 end
if a[7]&6 == 2 then ne = ne + 1 end
-- count conductor around
local nc = 0
if a[0]&6 == 6 then nc = nc + 1 end
if a[1]&6 == 6 then nc = nc + 1 end
if a[2]&6 == 6 then nc = nc + 1 end
if a[3]&6 == 6 then nc = nc + 1 end
if a[4]&6 == 6 then nc = nc + 1 end
if a[5]&6 == 6 then nc = nc + 1 end
if a[6]&6 == 6 then nc = nc + 1 end
if a[7]&6 == 6 then nc = nc + 1 end
-- Wireworld cell
local cell = a[8] & 6
-- Game of life layer
-- 1 full
-- 0 empty
local n1 = (a[0]&1) + (a[1]&1) + (a[2]&1) + (a[3]&1) + (a[4]&1) + (a[5]&1) + (a[6]&1) + (a[7]&1)
if n1 == 2 and (a[8]&1) ~= 0 then
result = result + 1
gol = 1
end
if n1 == 3 then
result = result + 1
gol = 1
if nc == 1 then
babyElectron = 1
end
end
if gol == 0 and cell == 4 and ne == 0 then
result = result + 1
gol = 1
end
-- Wireworld layer
-- 00X empty
-- 01X electron head
-- 10X electron tail
-- 11X conductor
if cell == 2 then -- if head
result = result + 4 -- then tail
elseif cell == 4 then -- if tail
result = result + 6 -- then conductor
elseif cell == 6 then -- if conductor
if ne == 1 or ne == 2 or babyElectron > 0 then
result = result + 2
else
result = result + 6
end
end
return result
end
What you see here is a unstable structure on the left feeding a stable structure on the right.
In case someone wanna try it, paste this in a file named Foo.rule in the Rules folder of Golly, then draw wires with state n°6 and GOL cell with states n°1. Electron heads are states n°2.
@RULE Foo
@COLORS
0 47 47 47
1 0 127 255
2 255 255 0
3 0 127 255
4 255 0 0
5 0 127 255
6 127 63 0
7 0 127 255
@TREE
num_states=8
num_neighbors=8
num_nodes=236
1 0 0 4 4 7 7 6 6
1 0 0 4 4 6 6 2 2
2 0 0 1 1 0 0 0 0
1 0 1 4 5 7 7 6 7
1 0 1 4 5 6 7 2 3
2 0 3 1 4 0 3 0 3
2 1 1 1 1 1 1 1 1
2 1 4 1 4 1 4 1 4
3 2 5 6 7 2 5 2 5
1 1 1 5 5 7 7 7 7
1 1 1 5 5 7 7 3 3
2 3 9 4 10 3 9 3 10
2 4 10 4 10 4 10 4 10
2 3 10 4 10 3 10 3 9
3 5 11 7 12 5 11 5 13
1 0 0 4 4 6 6 6 6
2 1 1 15 15 1 1 1 1
1 0 1 4 5 6 7 6 7
2 1 4 15 17 1 4 1 4
3 6 7 16 18 6 7 6 7
2 4 10 17 9 4 10 4 10
3 7 12 18 20 7 12 7 12
2 3 9 4 10 3 9 3 9
3 5 13 7 12 5 13 5 22
4 8 14 19 21 8 14 8 23
2 9 0 10 1 9 0 10 0
2 10 1 10 1 10 1 10 1
2 10 0 10 1 10 0 9 0
3 11 25 12 26 11 25 13 27
2 10 1 9 15 10 1 10 1
3 12 26 20 29 12 26 12 26
2 9 0 10 1 9 0 9 0
3 13 27 12 26 13 27 22 31
4 14 28 21 30 14 28 23 32
2 15 15 15 15 15 15 15 15
2 15 17 15 17 15 17 15 17
3 16 18 34 35 16 18 16 18
2 17 9 17 9 17 9 17 10
2 4 10 17 10 4 10 4 10
3 18 20 35 37 18 20 18 38
3 7 12 18 38 7 12 7 12
4 19 21 36 39 19 21 19 40
2 9 15 9 15 9 15 10 15
2 10 1 10 15 10 1 10 1
3 20 29 37 42 20 29 38 43
3 12 26 38 43 12 26 12 26
4 21 30 39 44 21 30 40 45
3 5 22 7 12 5 22 5 22
4 8 23 19 40 8 23 8 47
3 22 31 12 26 22 31 22 31
4 23 32 40 45 23 32 47 49
5 24 33 41 46 24 33 48 50
3 25 2 26 6 25 2 27 2
3 26 6 29 16 26 6 26 6
3 27 2 26 6 27 2 31 2
4 28 52 30 53 28 52 32 54
3 29 16 42 34 29 16 43 16
3 26 6 43 16 26 6 26 6
4 30 53 44 56 30 53 45 57
3 31 2 26 6 31 2 31 2
4 32 54 45 57 32 54 49 59
5 33 55 46 58 33 55 50 60
3 34 35 34 35 34 35 34 35
2 17 10 17 10 17 10 17 9
3 35 37 35 37 35 37 35 63
3 18 38 35 63 18 38 18 20
4 36 39 62 64 36 39 36 65
2 10 15 10 15 10 15 9 15
3 37 42 37 42 37 42 63 67
3 38 43 63 67 38 43 20 29
4 39 44 64 68 39 44 65 69
4 19 40 36 65 19 40 19 21
4 40 45 65 69 40 45 21 30
5 41 46 66 70 41 46 71 72
3 42 34 42 34 42 34 67 34
3 43 16 67 34 43 16 29 16
4 44 56 68 74 44 56 69 75
4 45 57 69 75 45 57 30 53
5 46 58 70 76 46 58 72 77
4 8 47 19 21 8 47 8 47
4 47 49 21 30 47 49 47 49
5 48 50 71 72 48 50 79 80
4 49 59 30 53 49 59 49 59
5 50 60 72 77 50 60 80 82
6 51 61 73 78 51 61 81 83
3 2 2 6 6 2 2 2 2
3 6 6 16 16 6 6 6 6
4 52 85 53 86 52 85 54 85
3 16 16 34 34 16 16 16 16
4 53 86 56 88 53 86 57 86
4 54 85 57 86 54 85 59 85
5 55 87 58 89 55 87 60 90
3 34 34 34 34 34 34 34 34
4 56 88 74 92 56 88 75 88
4 57 86 75 88 57 86 53 86
5 58 89 76 93 58 89 77 94
4 59 85 53 86 59 85 59 85
5 60 90 77 94 60 90 82 96
6 61 91 78 95 61 91 83 97
2 17 9 17 9 17 9 17 9
3 35 63 35 63 35 63 35 99
4 62 64 62 64 62 64 62 100
2 9 15 9 15 9 15 9 15
3 63 67 63 67 63 67 99 102
4 64 68 64 68 64 68 100 103
3 18 20 35 99 18 20 18 20
4 36 65 62 100 36 65 36 105
3 20 29 99 102 20 29 20 29
4 65 69 100 103 65 69 105 107
5 66 70 101 104 66 70 106 108
3 67 34 67 34 67 34 102 34
4 68 74 68 74 68 74 103 110
3 29 16 102 34 29 16 29 16
4 69 75 103 110 69 75 107 112
5 70 76 104 111 70 76 108 113
4 19 21 36 105 19 21 19 21
4 21 30 105 107 21 30 21 30
5 71 72 106 108 71 72 115 116
4 30 53 107 112 30 53 30 53
5 72 77 108 113 72 77 116 118
6 73 78 109 114 73 78 117 119
4 74 92 74 92 74 92 110 92
4 75 88 110 92 75 88 112 88
5 76 93 111 121 76 93 113 122
4 53 86 112 88 53 86 53 86
5 77 94 113 122 77 94 118 124
6 78 95 114 123 78 95 119 125
5 79 80 115 116 79 80 79 80
5 80 82 116 118 80 82 80 82
6 81 83 117 119 81 83 127 128
5 82 96 118 124 82 96 82 96
6 83 97 119 125 83 97 128 130
7 84 98 120 126 84 98 129 131
4 85 85 86 86 85 85 85 85
4 86 86 88 88 86 86 86 86
5 87 133 89 134 87 133 90 133
4 88 88 92 92 88 88 88 88
5 89 134 93 136 89 134 94 134
5 90 133 94 134 90 133 96 133
6 91 135 95 137 91 135 97 138
4 92 92 92 92 92 92 92 92
5 93 136 121 140 93 136 122 136
5 94 134 122 136 94 134 124 134
6 95 137 123 141 95 137 125 142
5 96 133 124 134 96 133 96 133
6 97 138 125 142 97 138 130 144
7 98 139 126 143 98 139 131 145
3 35 99 35 99 35 99 35 99
4 62 100 62 100 62 100 62 147
3 99 102 99 102 99 102 99 102
4 100 103 100 103 100 103 147 149
5 101 104 101 104 101 104 148 150
3 102 34 102 34 102 34 102 34
4 103 110 103 110 103 110 149 152
5 104 111 104 111 104 111 150 153
4 36 105 62 147 36 105 36 105
4 105 107 147 149 105 107 105 107
5 106 108 148 150 106 108 155 156
4 107 112 149 152 107 112 107 112
5 108 113 150 153 108 113 156 158
6 109 114 151 154 109 114 157 159
4 110 92 110 92 110 92 152 92
5 111 121 111 121 111 121 153 161
4 112 88 152 92 112 88 112 88
5 113 122 153 161 113 122 158 163
6 114 123 154 162 114 123 159 164
5 115 116 155 156 115 116 115 116
5 116 118 156 158 116 118 116 118
6 117 119 157 159 117 119 166 167
5 118 124 158 163 118 124 118 124
6 119 125 159 164 119 125 167 169
7 120 126 160 165 120 126 168 170
5 121 140 121 140 121 140 161 140
5 122 136 161 140 122 136 163 136
6 123 141 162 172 123 141 164 173
5 124 134 163 136 124 134 124 134
6 125 142 164 173 125 142 169 175
7 126 143 165 174 126 143 170 176
6 127 128 166 167 127 128 127 128
6 128 130 167 169 128 130 128 130
7 129 131 168 170 129 131 178 179
6 130 144 169 175 130 144 130 144
7 131 145 170 176 131 145 179 181
8 132 146 171 177 132 146 180 182
5 133 133 134 134 133 133 133 133
5 134 134 136 136 134 134 134 134
6 135 184 137 185 135 184 138 184
5 136 136 140 140 136 136 136 136
6 137 185 141 187 137 185 142 185
6 138 184 142 185 138 184 144 184
7 139 186 143 188 139 186 145 189
5 140 140 140 140 140 140 140 140
6 141 187 172 191 141 187 173 187
6 142 185 173 187 142 185 175 185
7 143 188 174 192 143 188 176 193
6 144 184 175 185 144 184 144 184
7 145 189 176 193 145 189 181 195
8 146 190 177 194 146 190 182 196
4 62 147 62 147 62 147 62 147
4 147 149 147 149 147 149 147 149
5 148 150 148 150 148 150 198 199
4 149 152 149 152 149 152 149 152
5 150 153 150 153 150 153 199 201
6 151 154 151 154 151 154 200 202
4 152 92 152 92 152 92 152 92
5 153 161 153 161 153 161 201 204
6 154 162 154 162 154 162 202 205
5 155 156 198 199 155 156 155 156
5 156 158 199 201 156 158 156 158
6 157 159 200 202 157 159 207 208
5 158 163 201 204 158 163 158 163
6 159 164 202 205 159 164 208 210
7 160 165 203 206 160 165 209 211
5 161 140 161 140 161 140 204 140
6 162 172 162 172 162 172 205 213
5 163 136 204 140 163 136 163 136
6 164 173 205 213 164 173 210 215
7 165 174 206 214 165 174 211 216
6 166 167 207 208 166 167 166 167
6 167 169 208 210 167 169 167 169
7 168 170 209 211 168 170 218 219
6 169 175 210 215 169 175 169 175
7 170 176 211 216 170 176 219 221
8 171 177 212 217 171 177 220 222
6 172 191 172 191 172 191 213 191
6 173 187 213 191 173 187 215 187
7 174 192 214 224 174 192 216 225
6 175 185 215 187 175 185 175 185
7 176 193 216 225 176 193 221 227
8 177 194 217 226 177 194 222 228
7 178 179 218 219 178 179 178 179
7 179 181 219 221 179 181 179 181
8 180 182 220 222 180 182 230 231
7 181 195 221 227 181 195 181 195
8 182 196 222 228 182 196 231 233
9 183 197 223 229 183 197 232 234