Python Questions

For scripts to aid with computation or simulation in cellular automata.
User avatar
dvgrn
Moderator
Posts: 6725
Joined: May 17th, 2009, 11:00 pm
Location: Madison, WI
Contact:

Re: Python Questions

Post by dvgrn » January 5th, 2016, 9:01 am

HartmutHolzwart wrote:The XOR is an alternative way to reach the same goal. I didn't want to hash the whole pattern, but only the relevant part of it. And then compare the hash of the relevant part to the original pie sequence. I was lead to the hash function as it states that the hash is pretty unique and independent from the actual location.
True enough. Yes, that should work. If you can guarantee that you've run the pi climbers far enough that they've climbed well beyond the debris, so that there are no pieces of ash in a corner of your bounding rectangle, then golly.hash() should be fine.

I guess probably 270 ticks is enough, especially if the defined rectangle only reaches down to the front part of the last pi -- if the front part is still there at T=270, then presumably the whole thing survived intact.

I always seem to want the hash function to operate on cell lists, not rectangles. Often it's necessary to create a new layer and drop a pattern into it, just so that I can use golly.hash(). But in this case it should do just what you want.

An equivalent trick would be just to remember the original cell list -- oldpat=golly.getcells(oldrectangle) -- and then see if it's equal to a transformed copy of the new cell list: newpat=g.transform(golly.getcells(newrectangle),0,102). That would avoid any possible false matches -- though I'm pretty sure collisions in the hashing function are so rare that it's really pointless to worry about them. golly.hash() is more efficient in this case, since list comparisons have to do so much more work -- but golly.transform() is fast and useful, so seemed worth mentioning.

HartmutHolzwart
Posts: 463
Joined: June 27th, 2009, 10:58 am
Location: Germany

Re: Python Questions

Post by HartmutHolzwart » January 6th, 2016, 2:35 pm

I have some first results now, but no breakthrough on the original goal. Unfortunately, my overview pattern is too big to be directly pasted here.

User avatar
dvgrn
Moderator
Posts: 6725
Joined: May 17th, 2009, 11:00 pm
Location: Madison, WI
Contact:

Re: Python Questions

Post by dvgrn » January 6th, 2016, 3:21 pm

HartmutHolzwart wrote:I have some first results now, but no breakthrough on the original goal. Unfortunately, my overview pattern is too big to be directly pasted here.
How about an attachment? It would have to be an impressively big pattern to not fit in an attached ZIP file. Or you could upload it to Google Drive or DropBox or some such, I suppose.

HartmutHolzwart
Posts: 463
Joined: June 27th, 2009, 10:58 am
Location: Germany

Re: Python Questions

Post by HartmutHolzwart » January 9th, 2016, 7:02 pm

Here is one of the misses to illustrate what I want to do:

Code: Select all

x = 66, y = 594, rule = B3/S23
4$33bo$33bo$33bo16$32b3o16$33bo$33bo$33bo16$32b3o16$33bo$33bo$33bo16$
32b3o16$33bo$33bo$33bo16$32b3o16$33bo$33bo$33bo16$32b3o16$33bo$33bo$
33bo16$32b3o16$33bo$33bo$33bo16$32b3o16$33bo$33bo$33bo16$32b3o16$33bo$
33bo$33bo16$32b3o16$33bo$33bo$33bo16$32b3o16$33bo$33bo$33bo16$32b3o16$
33bo$33bo$33bo16$32b3o9$32b3o$33bo2$27b3o7b3o$26bobo3b3o3bobo$26bo13bo
$26b2ob2o5b2ob2o$28bo3bobo3bo$28b3o5b3o6$26b3o3b3o3b3o$23bo2b3o9b3o2bo
$23bo19bo$23bo19bo2$10b2o$11b2o$10bo$30bobobobo$32bobo$29bo2bobo2bo$
29bo7bo2$33bo$32b3o$31b2ob2o4$33bo$31bo3bo$29bo7bo$29bo7bo$29b2ob3ob2o
4$22b2o4b2o7b2o4b2o$22bobob2o11b2obobo$25b2o5bobo5b2o$27b2o3bobo3b2o$
20bo7b2o7b2o7bo$19b2o6bo4bobo4bo6b2o$19bo7b2o3bobo3b2o7bo$20bobo8bo3bo
8bobo$20bo2bo19bo2bo$21b2o21b2o3$33bo$33bo$33bo3$32b3o$31bo3bo$31b2ob
2o3$24bo17bo$22b3o3b2o7b2o3b3o$21bo4bo2bo7bo2bo4bo$21bo3bo15bo3bo$21bo
5b4o5b4o5bo$22b2o4b2o7b2o4b2o$24b3o13b3o$25b2o13b2o$26bo3bo5bo3bo2$27b
o2bo5bo2bo$28bobo5bobo2$33bo$33bo$33bo6$33bo$33bo$32bobo$28bo9bo$28bo
9bo$26b4ob2ob2ob4o$25b2o4bobobo4b2o$24bo5bo2bo2bo5bo$25b2o2b3o3b3o2b2o
$28bo2bo3bo2bo$28bobo5bobo$28bobo5bobo4$27bo5bo5bo$25b2obo4bo4bob2o$
24b3obo4bo4bob3o$22b2o2b2o11b2o2b2o15$32b3o15$33bo$32bobo$31bo3bo$30b
2obob2o$33bo$29bo7bo$29bo2bobo2bo$29bo2bobo2bo$29bo7bo$30b2o3b2o$27b2o
9b2o$27b2o9b2o$22b2ob4o9b4ob2o$21bo5bo11bo5bo$22bo21bo$20bob2o3b2ob3ob
3ob2o3b2obo$19bo8b2ob2ob2ob2o8bo$20bo2bo5bobo3bobo5bo2bo$21b2obo5bo5bo
5bob2o$23bo19bo5$33bo$33bo$33bo4$33bo$32bobo$31bo3bo$31b2ob2o$30b2obob
2o$25bo3b2o5b2o3bo$24b2o3bo2b3o2bo3b2o$23bo7bo3bo7bo$24b2o3bo7bo3b2o$
25b3o11b3o$28bo3bobo3bo$27bo2bo5bo2bo$28b3o5b3o$28bobo5bobo$29b2o5b2o
3$33bo$33bo$33bo!

User avatar
Scorbie
Posts: 1445
Joined: December 7th, 2013, 1:05 am

Re: Scripting questions

Post by Scorbie » June 9th, 2016, 10:38 pm

Now that we have lua, I changed the title slightly.
Consider getting the states of cells in a blob of near cells.
I am curious whether it is faster to get the cells manually or parsing the cell list. Both in python and Lua.

P.S. after looking at lua scripts I am glad to see that the transition from python to lua doesnt seem too hard.
Best wishes to you, Scorbie

User avatar
Scorbie
Posts: 1445
Joined: December 7th, 2013, 1:05 am

Re: Scripting Questions

Post by Scorbie » January 15th, 2017, 8:36 pm

Haven't written a script in quite a while.
Is there a function to get an rle of a cell list? I can't find any on the golly help - lua/python scripting page.
Best wishes to you, Scorbie

User avatar
dvgrn
Moderator
Posts: 6725
Joined: May 17th, 2009, 11:00 pm
Location: Madison, WI
Contact:

Re: Scripting Questions

Post by dvgrn » January 19th, 2017, 10:39 pm

Scorbie wrote:Is there a function to get an rle of a cell list? I can't find any on the golly help - lua/python scripting page.
I keep not being able to find that when I want it. Turned out to be buried inside another script on the old Script Request thread. Thanks again, Nathaniel!

Code: Select all

# Python function to convert a cell list to RLE
# Author: Nathaniel Johnston (nathaniel@nathanieljohnston.com), June 2009.
#          DMG: Refactored slightly so that the function input is a simple cell list.
#               No error checking added.
#               TBD:  check for multistate rule, show appropriate warning.
# --------------------------------------------------------------------

def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]

# --------------------------------------------------------------------

def giveRLE(clist):
   clist_chunks = list (chunks (g.evolve(clist,0), 2))
   mcc = min(clist_chunks)
   rl_list = [[x[0]-mcc[0],x[1]-mcc[1]] for x in clist_chunks]
   rle_res = ""
   rle_len = 1
   rl_y = rl_list[0][1] - 1
   rl_x = 0
   for rl_i in rl_list:
      if rl_i[1] == rl_y:
         if rl_i[0] == rl_x + 1:
            rle_len += 1
         else:
            if rle_len == 1: rle_strA = ""
            else: rle_strA = str (rle_len)
            if rl_i[0] - rl_x - 1 == 1: rle_strB = ""
            else: rle_strB = str (rl_i[0] - rl_x - 1)

            rle_res = rle_res + rle_strA + "o" + rle_strB + "b"
            rle_len = 1
      else:
         if rle_len == 1: rle_strA = ""
         else: rle_strA = str (rle_len)
         if rl_i[1] - rl_y == 1: rle_strB = ""
         else: rle_strB = str (rl_i[1] - rl_y)
         if rl_i[0] == 1: rle_strC = "b"
         elif rl_i[0] == 0: rle_strC = ""
         else: rle_strC = str (rl_i[0]) + "b"
         
         rle_res = rle_res + rle_strA + "o" + rle_strB + "$" + rle_strC
         rle_len = 1

      rl_x = rl_i[0]
      rl_y = rl_i[1]
   
   if rle_len == 1: rle_strA = ""
   else: rle_strA = str (rle_len)
   rle_res = rle_res[2:] + rle_strA + "o"
   
   return rle_res+"!"
Instead of that, you could create a new layer, drop in the cell list, save to a temporary file, and then open the file and read back the RLE text. That's just a few lines of code and probably executes faster than this script, but it just seems wrong to me somehow.

If anyone feels like adding some decent error-checking to this Official Cell List To RLE Script (future Google search term) I'll be happy to update the above code block...!

User avatar
Scorbie
Posts: 1445
Joined: December 7th, 2013, 1:05 am

Re: Python Questions

Post by Scorbie » January 20th, 2017, 10:03 pm

Whoops, I was scripting in Lua, but I'm glad I know now where to find when I need this. So there isn't a golly function, I guess.
What I didn't realize is that I needed a 2-state cell list from a multistate pattern (only counting state 1), so I'll happily tweak it for my needs (with credits, of course) Thanks!
Best wishes to you, Scorbie

Post Reply