File: FredkinModN-gen.py

package info (click to toggle)
golly 3.2-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 19,516 kB
  • sloc: cpp: 69,819; ansic: 25,894; python: 7,921; sh: 4,267; objc: 3,721; java: 2,781; xml: 1,362; makefile: 530; perl: 69
file content (72 lines) | stat: -rw-r--r-- 2,486 bytes parent folder | download | duplicates (5)
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
63
64
65
66
67
68
69
70
71
72
import golly
import random
import time
from glife.EmulateHexagonal import *
from glife.EmulateTriangular import *
from glife.RuleTree import *

# AKT: itertools.product is new in Python 2.6
try:
    from itertools import product
except ImportError:
    # see http://docs.python.org/library/itertools.html#itertools.product
    def product(*args, **kwds):
        pools = map(tuple, args) * kwds.get('repeat', 1)
        result = [[]]
        for pool in pools:
            result = [x+[y] for x in result for y in pool]
        for prod in result:
            yield tuple(prod)

spec = golly.getstring(
'''This script will write and select the rule "Fredkin-mod-n", for a given N.

This rule is the Winograd extension of the Fredkin Replicator rule (the parity rule):
  c,{s} -> sum(s)%N    (where N=2 for the original parity rule)

If N is prime, this will result in any pattern being replicated at N^m timesteps
(if the pattern is small enough that it doesn't overlap its copies).

Specify the neighborhood (N = von Neumann, M = Moore, T = triangular von Neumann,
H = hexagonal, TM = triangular Moore) and the value of N (2-255). e.g. H2, N7

(Larger values of N can take a long time.)
''', 'N3', 'Enter specification:')

# work out what the string meant
nhood = ''
nhoods = {'N':'vonNeumann','TM':'triangularMoore','M':'Moore',
          'T':'triangularVonNeumann','H':'hexagonal'}
for nh in nhoods.keys():
    if nh in spec:
        nhood = nhoods[nh]
        n = int(spec.replace(nh,''))
        break
if nhood=='':
    golly.exit('Unsupported string: '+spec)
if n<2 or n>255:
    golly.exit('Value of N must lie between 2 and 255.')

# assemble the transitions
nbors = {'vonNeumann':4,'Moore':8,'hexagonal':6,'triangularVonNeumann':3,
         'triangularMoore':12}
transitions = []
for sl in product(range(n),repeat=nbors[nhood]):
    transitions += [ [range(n)] + [[s] for s in sl] + [[sum(sl)%n]] ]
rule_name = 'Fredkin_mod'+str(n)+'_'+nhood

Converters = {
    "vonNeumann":ConvertRuleTableTransitionsToRuleTree,
    "Moore":ConvertRuleTableTransitionsToRuleTree,
    "triangularVonNeumann":EmulateTriangular,
    "triangularMoore":EmulateTriangular,
    "hexagonal":EmulateHexagonal,
}

golly.show("Building rule...")
rule_name = Converters[nhood]( nhood,
                               n,
                               transitions,
                               rule_name+'.tree' )
golly.setrule(rule_name)
golly.show('Created '+rule_name+'.rule and selected that rule.')