File: RuleTreeGen.lua

package info (click to toggle)
golly 3.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 20,176 kB
  • sloc: cpp: 72,638; ansic: 25,919; python: 7,921; sh: 4,245; objc: 3,721; java: 2,781; xml: 1,362; makefile: 530; javascript: 279; perl: 69
file content (62 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- 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)