Fuse finder scripts
Posted: January 4th, 2015, 7:49 am
I thought there was too much talk in the script itself so I decided to make a thread mainly for the script.
Here's the code.
Here's the code.
Code: Select all
# codeholic's FuseFinder script v1.02
# Creates a new layer called "FuseTestLayer"
# Writes to a FuseFinder subfolder in Golly's data directory.
# Updates screen every hundred trials (why not?)
import golly as g
import os
import time
import copy
outpath=os.path.join(g.getdir("data"),"FuseFinder")
if not os.path.isdir(outpath): os.mkdir(outpath)
FUSENAME = "BeehiveFuse"
DEBRIS = g.parse('b2o$o2bo$b2o!')
WIDTH = 4
HEIGHT = 5
DX, DY = (0, -5) #For diagonal/oblique fuses
COPIES = 100
TESTRECT = [COPIES * DX, COPIES * DY, WIDTH, HEIGHT]
RNDSIZE = 16
RNDRECT = [-7, 0, RNDSIZE, RNDSIZE]
SOUPSPERDISPLAY = 100
SQRTSPP = 10
SPP = SQRTSPP ** 2
SOUPL = 4000
def patterns_identical(cells1, cells2):
if len(cells1) != len(cells2):
return False
return set(zip(cells1[::2], cells1[1::2])) == set(zip(cells2[::2], cells2[1::2]))
for i in range(g.numlayers()):
if g.getname(i)=="FuseTestLayer":
r=g.getrect()
if r is not []:
g.select(r)
g.clear(0)
break
if not g.getname(i)=="FuseTestLayer": g.addlayer()
g.setalgo('QuickLife')
for x in xrange(SQRTSPP):
for y in xrange(SQRTSPP):
for i in xrange(1, COPIES+1):
g.putcells(DEBRIS, i * DX + x * SOUPL, i * DY + y * SOUPL)
if x==0 and y==0:
fuserect = g.getrect()
allcells=g.getcells(g.getrect())
cells = g.getcells(TESTRECT)
count, found = 0, 0
start_time = time.clock()
# For profiling
#
#init_time = 0
#gen_time = 0
#check_time = 0
while True:
#mark = time.clock()
rndrect = copy.deepcopy(RNDRECT)
testrect = copy.deepcopy(TESTRECT)
foundindex = []
g.new("FuseTestLayer")
g.putcells(allcells)
for x in xrange(SQRTSPP):
for y in xrange(SQRTSPP):
g.select(rndrect)
g.randfill(50)
rndrect[1] += SOUPL
rndrect[0] += SOUPL
rndrect[1] = RNDRECT[1]
#init_time += time.clock()-mark
#mark = time.clock()
g.setbase(16)
g.setstep(3)
g.step()
#gen_time +=time.clock()-mark
#mark = time.clock()
for x in xrange(SQRTSPP):
for y in xrange(SQRTSPP):
test = g.getcells(testrect)
for i in xrange(0, len(test), 2):
test[i] -= x * SOUPL
for i in xrange(1, len(test), 2):
test[i] -= y * SOUPL
if not patterns_identical(test, cells):
foundindex.append((x, y))
testrect[1] += SOUPL
testrect[0] += SOUPL
testrect[1] = TESTRECT[1]
if foundindex is not []:
g.reset()
for (x, y) in foundindex:
rndrect = copy.deepcopy(RNDRECT)
foundfuserect = copy.deepcopy(fuserect)
rndrect[0] += x * SOUPL
rndrect[1] += y * SOUPL
foundfuserect[0] += x * SOUPL
foundfuserect[1] += y * SOUPL
foundpattern = g.getcells(rndrect) + g.getcells(foundfuserect)
g.store(foundpattern, os.path.join(outpath, FUSENAME + str(count + 10 * y + x) + '.rle'))
g.show("Saved fuse to " + outpath + FUSENAME + str(count + 10 * y + x) + ".rle")
found += 1
count += SPP
if count % SOUPSPERDISPLAY == 0:
end_time =time.clock()
g.show('Count:' + str(count) + ' Found:' + str(found) + ' (' +\
str(round(SOUPSPERDISPLAY/(end_time - start_time), 2)) + " soups per second)")
#g.show('init: '+str(init_time*1000/count)+'ms gen: '+str(gen_time*1000/count)+'ms check: '+str(check_time*1000/count)+'ms')
start_time = end_time
g.fit()
g.update()
#check_time += time.clock()-markI think we would have to use g.getevent() to do something like that. But before that, do you have problems with that unclosed layer? I think the layer would have been 'unclosed' since the original script.HartmutHolzwart wrote:the script does not close the current layer when terminated by "escape"...