コード例 #1
0
def better_randfill():
    g.new('')
    g.select([-10, -10, GRID_SIZE[0], GRID_SIZE[1]])
    g.randfill(random.randrange(50))
    g.select([])
    g.autoupdate(True)

    cnt = 0
    for x in range(0, GENS):
        cnt += 1
        if not g.empty():
            g.run(1)
            if is_static():
                break
            time.sleep(0.05)
        else:
            break
        if cnt % 10 == 0:
            g.fit()
コード例 #2
0
    # now generate the ONcell HWSSs and LWSSs
    # add rest of pattern after the center area is filled in
    # and p184 HWSS guns are in the same phase (3680 = 184 * 40)
    ONcell = all + CellCenter[3680]
    
    if os.access(ONcellFileName, os.W_OK) or not os.access(ONcellFileName, os.F_OK):
       try:
          ONcell.save (ONcellFileName, "Metapixel ON cell: Brice Due, Spring 2006")
       except:
          g.show("Failed to save file version of ON cell: " + ONcellFileName)
          os.remove(ONcellFileName)
          
OFFcell += RuleBits
ONcell += RuleBits

g.autoupdate(True)
g.new(layername)   
g.setalgo("QuickLife")              # qlife's setcell is faster

for j in xrange(selheight):
   for i in xrange(selwidth):
      golly.show("Placing (" + str(i+1) + "," + str(j+1) + ") tile" +
                 " in a " + str(selwidth) + " by " + str(selheight) + " rectangle.")
      if livecell[i][j]:
         ONcell.put(2048 * i - 5, 2048 * j - 5)
      else:
         OFFcell.put(2048 * i - 5, 2048 * j - 5)
      g.fit()
   
g.show("")
g.setalgo("HashLife")               # no point running a metapattern without hashing
コード例 #3
0
ファイル: draw_golly.py プロジェクト: 0-zM/draw
import sys
import golly

visual_execution = True  #False for faster programs, True required for infinitely looping ones

file = golly.opendialog()
f = open(file).read()

golly.new("Executing " + file.split("/")[-1])
golly.setrule("B/S012345678")

golly.autoupdate(visual_execution)

commands = []

for line in f.splitlines():
    important = line.split("#")[0].strip()
    if not important:
        continue

    important = important.split()[0:5]
    commands.append((important[0], int(important[1]), int(important[2]),
                     important[3], important[4]))

pointer = [0, 0]
instruction_pointer = "start"

while True:
    command = commands[[i[0] for i in commands].index(instruction_pointer)]
    pointer = [pointer[0] + command[1], pointer[1] + command[2]]
    x = pointer[0]
コード例 #4
0
def score_pair(g, seed1, seed2, width_factor, height_factor, \
  time_factor, num_trials):
    """
  Put seed1 and seed2 into the Immigration Game g and see which 
  one wins and which one loses. Note that this function does
  not update the histories of the seeds.
  """
    #
    # Make copies of the original two seeds, so that the following
    # manipulations do not change the originals.
    #
    s1 = copy.deepcopy(seed1)
    s2 = copy.deepcopy(seed2)
    #
    # Initialize scores
    #
    score1 = 0.0
    score2 = 0.0
    #
    # Run several trials with different rotations and locations.
    #
    for trial in range(num_trials):
        #
        # Randomly rotate and flip s1 and s2
        #
        s1 = s1.random_rotate()
        s2 = s2.random_rotate()
        #
        # Switch cells in the second seed (s2) from state 1 (red) to state 2 (blue)
        #
        s2.red2blue()
        #
        # Rule file is "Immigration.rule"
        # Set toroidal universe of height yspan and width xspan
        # Base the s1ze of the universe on the s1zes of the seeds
        #
        # g = the Golly universe
        #
        [g_width, g_height, g_time] = dimensions(s1, s2, \
          width_factor, height_factor, time_factor)
        #
        # set algorithm -- "HashLife" or "QuickLife"
        #
        g.setalgo("QuickLife")  # use "HashLife" or "QuickLife"
        g.autoupdate(False)  # do not update the view unless requested
        g.new("Immigration")  # initialize cells to state 0
        g.setrule("Immigration:T" + str(g_width) + "," +
                  str(g_height))  # make a toroid
        #
        # Find the min and max of the Golly toroid coordinates
        #
        [g_xmin, g_xmax, g_ymin, g_ymax] = get_minmax(g)
        #
        # Set magnification for Golly viewer
        #
        g.setmag(set_mag(g))
        #
        # Randomly place seed s1 somewhere in the left s1de of the toroid
        #
        s1.insert(g, g_xmin, -1, g_ymin, g_ymax)
        #
        # Randomly place seed s2 somewhere in the right s1de of the toroid
        #
        s2.insert(g, +1, g_xmax, g_ymin, g_ymax)
        #
        # Run for a fixed number of generations.
        # Base the number of generations on the sizes of the seeds.
        # Note that these are generations ins1de one Game of Life, not
        # generations in an evolutionary sense. Generations in the
        # Game of Life correspond to growth and decay of a phenotype,
        # whereas generations in evolution correspond to the reproduction
        # of a genotype.
        #
        g.run(g_time)  # run the Game of Life for g_time time steps
        g.update()  # need to update Golly to get counts
        #
        # Count the populations of the two colours. State 1 = red = seed1.
        # State 2 = blue = seed2.
        #
        [count1, count2] = count_pops(g)
        #
        if (count1 > count2):
            score1 = score1 + 1.0
        elif (count2 > count1):
            score2 = score2 + 1.0
        else:
            score1 = score1 + 0.5
            score2 = score2 + 0.5
        #
    #
    # Normalize the scores
    #
    score1 = score1 / num_trials
    score2 = score2 / num_trials
    #
    return [score1, score2]
コード例 #5
0
def score_pair(g, seed1, seed2, width_factor, height_factor, \
  time_factor, num_trials):
    """
  Put seed1 and seed2 into the Immigration Game g and see which 
  one wins and which one loses. Note that this function does
  not update the histories of the seeds. For updating histories,
  use update_history().
  """
    #
    # Make copies of the original two seeds, so that the following
    # manipulations do not change the originals.
    #
    s1 = copy.deepcopy(seed1)
    s2 = copy.deepcopy(seed2)
    #
    # Check the number of living cells in the seeds. If the number
    # is zero, it is probably a mistake. The number is initially
    # set to zero and it should be updated when the seed is filled
    # with living cells. We could use s1.count_ones() here, but
    # we're trying to be efficient by counting only once and
    # storing the count.
    #
    assert s1.num_living > 0
    assert s2.num_living > 0
    #
    # Initialize scores
    #
    score1 = 0.0
    score2 = 0.0
    #
    # Run several trials with different rotations and locations.
    #
    for trial in range(num_trials):
        #
        # Randomly rotate and flip s1 and s2
        #
        s1 = s1.random_rotate()
        s2 = s2.random_rotate()
        #
        # Switch cells in the second seed (s2) from state 1 (red) to state 2 (blue)
        #
        s2.red2blue()
        #
        # Rule file
        #
        rule_name = "Immigration"
        #
        # Set toroidal universe of height yspan and width xspan
        # Base the s1ze of the universe on the s1zes of the seeds
        #
        # g = the Golly universe
        #
        [g_width, g_height, g_time] = dimensions(s1, s2, \
          width_factor, height_factor, time_factor)
        #
        # set algorithm -- "HashLife" or "QuickLife"
        #
        g.setalgo("QuickLife")  # use "HashLife" or "QuickLife"
        g.autoupdate(False)  # do not update the view unless requested
        g.new(rule_name)  # initialize cells to state 0
        g.setrule(rule_name + ":T" + str(g_width) + "," +
                  str(g_height))  # make a toroid
        #
        # Find the min and max of the Golly toroid coordinates
        #
        [g_xmin, g_xmax, g_ymin, g_ymax] = get_minmax(g)
        #
        # Set magnification for Golly viewer
        #
        g.setmag(set_mag(g))
        #
        # Randomly place seed s1 somewhere in the left s1de of the toroid
        #
        s1.insert(g, g_xmin, -1, g_ymin, g_ymax)
        #
        # Randomly place seed s2 somewhere in the right s1de of the toroid
        #
        s2.insert(g, +1, g_xmax, g_ymin, g_ymax)
        #
        # Run for a fixed number of generations.
        # Base the number of generations on the sizes of the seeds.
        # Note that these are generations ins1de one Game of Life, not
        # generations in an evolutionary sense. Generations in the
        # Game of Life correspond to growth and decay of a phenotype,
        # whereas generations in evolution correspond to the reproduction
        # of a genotype.
        #
        g.run(g_time)  # run the Game of Life for g_time time steps
        g.update()  # need to update Golly to get counts
        #
        # Count the populations of the two colours. State 1 = red = seed1.
        # State 2 = blue = seed2.
        #
        [count1, count2] = count_pops(g)
        #
        # We need to make an adjustment to these counts. We don't want to
        # use the total count of living cells; instead we want to use
        # the increase in the number of living cells over the course of
        # the contest between the two organisms. The idea here is that
        # we want to reward seeds according to their growth during the
        # contest, not according to their initial states. This should
        # avoid an evolutionary bias towards larger seeds simply due
        # to size rather than due to functional properties. It should
        # also encourage efficient use of living cells, as opposed to
        # simply ignoring useless living cells.
        #
        # s1.num_living = initial number of living cells in s1
        # s2.num_living = initial number of living cells in s2
        #
        if (s1.num_living < count1):
            count1 = count1 - s1.num_living
        else:
            count1 = 0
        #
        if (s2.num_living < count2):
            count2 = count2 - s2.num_living
        else:
            count2 = 0
        #
        # Now we are ready to determine the winner.
        #
        if (count1 > count2):
            score1 = score1 + 1.0
        elif (count2 > count1):
            score2 = score2 + 1.0
        else:
            score1 = score1 + 0.5
            score2 = score2 + 0.5
        #
    #
    # Normalize the scores
    #
    score1 = score1 / num_trials
    score2 = score2 / num_trials
    #
    return [score1, score2]
コード例 #6
0
#
current_cpu_id = cparams.current_cpu_id
#
# read the rule list and extract rules that match the current
# run's CPU id
#
rule_list = cfuncs.tsv_BS_rule_cpu(rule_file_name, current_cpu_id)
#
# Golly screen magnification
#
screen_mag = cparams.screen_mag
#
# initialize Golly
#
g.setalgo("QuickLife")  # select the algorithm for Golly
g.autoupdate(False)  # do not update the view unless requested
g.new("Classification")  # create an empty universe
g.setmag(screen_mag)  # screen magnification
#
# show parameter settings in the log file
#
parameter_settings = cfuncs.show_parameters()
log_handle.write("\nParameter Settings\n\n")
for setting in parameter_settings:
    log_handle.write(setting + "\n")
log_handle.write("\n")
#
# write a header line for the list of results
#
columns = ["rule", \
           "prob pop incr", \
コード例 #7
0
ファイル: countNHD.py プロジェクト: shouldsee/golly_utils
import os
import golly as g
#g.select(g.getrect())
oldrule=g.getrule()
i=0
g.autoupdate(0)
while i==0:
	g.run(1)
	g.save('temp','rle')
	g.setrule('S_input')
	g.run(1)
	cellindex=g.getlayer()
	execfile("histogram_2002_r75.py")
	histindex=g.getlayer()
	g.setlayer(cellindex)
	g.open('temp')
	g.setlayer(histindex)
	g.fit()
	g.update()
	g.setlayer(cellindex)
	i=+1
コード例 #8
0
    # and p184 HWSS guns are in the same phase (3680 = 184 * 40)
    ONcell = all + CellCenter[3680]

    if os.access(ONcellFileName,
                 os.W_OK) or not os.access(ONcellFileName, os.F_OK):
        try:
            ONcell.save(ONcellFileName,
                        "Metapixel ON cell: Brice Due, Spring 2006")
        except:
            g.show("Failed to save file version of ON cell: " + ONcellFileName)
            os.remove(ONcellFileName)

OFFcell += RuleBits
ONcell += RuleBits

g.autoupdate(True)
g.new(layername)
g.setalgo("QuickLife")  # qlife's setcell is faster

for j in xrange(selheight):
    for i in xrange(selwidth):
        golly.show("Placing (" + str(i + 1) + "," + str(j + 1) + ") tile" +
                   " in a " + str(selwidth) + " by " + str(selheight) +
                   " rectangle.")
        if livecell[i][j]:
            ONcell.put(2048 * i - 5, 2048 * j - 5)
        else:
            OFFcell.put(2048 * i - 5, 2048 * j - 5)
        g.fit()

g.show("")
コード例 #9
0
        d = 0
    else:
        d = float(g.getpop()) / float(bbox.wd*bbox.ht)
    return d


grid_size = int(g.getstring("Tamanho do tabuleiro:", "3"))
max_iter = grid_size * 500

number_of_tests = 2 ** (grid_size**2)
name = "%dx%d" % (grid_size, grid_size)
out_filename = "output/results_%s.csv" % (name)
patterns_filename = "patterns/patterns_%s.csv" % (name)
rlepatterns_filename = "patterns/patterns_" + name + "-%d.rle"

g.autoupdate(False)
f = open(out_filename, 'w')
f.write("id,number_of_iterations,initial_pop,final_pop,initial_density,final_density,end_status,elapsed_time\n")
#f2 = open(patterns_filename, 'w')
#f2.write("id, initial_cell_list\n")

trial_number = 1
for test_list in create_cell_list(grid_size):
    iterations = 0
    status = ''
    o.clear()
    g.new(name)
    g.reset()
    g.putcells(test_list)

    initial_pop = g.getpop()