Non-totalistic Rules and Weighted Life (Scripts)
Posted: July 20th, 2012, 3:27 pm
This script converts MCell's Weighted Life rules into Golly's Ruletree format. It will allow Golly users to experiment with their own Weighted Life rules.
Code: Select all
# Weightedlife->Ruletree(1.0).py
# by Eric Goldstein, July 20, 2012.
import golly
from glife.RuleTree import *
dialog_box_message = '''
This script will allow you to enter the same kind of Weighted Life rule specifications that are
used by Mirek Mirek Wojtowicz's MCell program. A corresponding ruletree will be created in Golly's rules folder.
An example rule is given below as a default. Separate each rule specification with a comma, a space, or
a carriage return/line feed.
Names: Rules can be named, for example, "Foo", by using the optional specification Name=Foo with no spaces.
Rule names should follow Golly's naming conventions (they should start with a capital letter and contain no spaces).
Visit http://www.mirekw.com/ca/rullex_wlif.html or search the web for "MCell weighted life" to learn more.
Rules listed on MCell's Weighted Life webpage can be cut and pasted into the space below just after the "Name=" term.
'''
# Default values
RS = []
RB = []
ME_weight = 0
NW_weight = 1
NN_weight = 1
NE_weight = 1
WW_weight = 1
EE_weight = 1
SW_weight = 1
SS_weight = 1
SE_weight = 1
CR = chr(13)
LF = chr(10)
name = "Temporary-rule-name"
WeightedRulestring = golly.getstring(dialog_box_message, "Name=Temporary-rule-name NW4,NN1,NE0,WW1, ME0,EE4,SW0,SS4, SE1,HI0,RS1,RS6,RS8,RB5,RB6")
WeightedRulestring = WeightedRulestring.replace("ame = ", "ame=")
WeightedRulestring = WeightedRulestring.replace(" ", ",")
WeightedRulestring = WeightedRulestring.replace(CR, ",")
WeightedRulestring = WeightedRulestring.replace(LF, ",")
for x in WeightedRulestring.split(","):
if x.startswith("Name=") or x.startswith("name="):
name = x.split("ame=")[1]
else:
x = x.upper()
if x.startswith("NW"):
NW_weight = int(x.split("NW")[1])
elif x.startswith("NN"):
NN_weight = int(x.split("NN")[1])
elif x.startswith("NE"):
NE_weight = int(x.split("NE")[1])
elif x.startswith("WW"):
WW_weight = int(x.split("WW")[1])
elif x.startswith("EE"):
EE_weight = int(x.split("EE")[1])
elif x.startswith("SW"):
SW_weight = int(x.split("SW")[1])
elif x.startswith("SS"):
SS_weight = int(x.split("SS")[1])
elif x.startswith("SE"):
SE_weight = int(x.split("SE")[1])
elif x.startswith("ME"):
ME_weight = int(x.split("ME")[1])
elif x.startswith("RS"):
RS.append(int(x.split("RS")[1]))
elif x.startswith("RB"):
RB.append(int(x.split("RB")[1]))
elif x.startswith("HI"):
n_states = (int(x.split("HI")[1]))
# python probably has a nicer way to say the following, using max or min
if n_states < 3:
n_states = 2
if n_states > 256:
n_states = 256
if n_states > 8:
golly.getstring('''Ruletrees with more than 8 or so states can take a long time to compute.
Save your work before continuing. Once this script starts computing your rule,
if you want to stop the calculation, you may have to abort Golly itelf.
Choose the cancel option now to stop this script.''', "Proceed")
n_neighbors = 8
def transition_function(a):
# order for 8 neighbors is NW, NE, SW, SE, N, W, E, S, C
n = NW_weight*(a[0] == 1) + NE_weight*(a[1] == 1) + SW_weight*(a[2] == 1) + SE_weight*(a[3] == 1) + NN_weight*(a[4] == 1) + WW_weight*(a[5] == 1) + EE_weight*(a[6] == 1) + SS_weight*(a[7] == 1) + ME_weight*(a[8] == 1)
if a[8] == 1 and n in RS:
return 1
if a[8] == 0 and n in RB:
return 1
if a[8] > 0 and a[8] < (n_states - 1):
return a[8] + 1
return 0
golly.show("Please wait while ruletree is being created...")
# The code below this line is copied from make-ruletree.py
try:
MakeRuleTreeFromTransitionFunction( n_states, n_neighbors, transition_function, golly.getdir("rules")+name+".tree" )
golly.setalgo("RuleTree") # in case name.table exists
golly.setrule(name)
golly.show("Created "+name+".tree in "+golly.getdir("rules"))
except:
import sys
import traceback
exception, msg, tb = sys.exc_info()
golly.warn(\
'''There was an error. Please see http://www.mirekw.com/ca/rullex_wlif.html for more information on how to specify rules for this script.'''+ '\n'.join(traceback.format_exception(exception, msg, tb)))
golly.exit()