Added Pi-approximating automatons.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/anks.py Thu Dec 30 02:16:10 2010 +0100
1.3 @@ -0,0 +1,72 @@
1.4 +class Cell(object):
1.5 + def __init__(self, value):
1.6 + self.value = value
1.7 + def __str__(self):
1.8 + if self.value:
1.9 + return chr(176)
1.10 + else:
1.11 + return chr(177)
1.12 +
1.13 +def iterate(grid, rule):
1.14 + expanded = [Cell(False), Cell(False)]
1.15 + expanded.extend(grid)
1.16 + expanded.extend([Cell(False), Cell(False)])
1.17 + new_grid = []
1.18 + for i in xrange(len(expanded)-2):
1.19 + triple = expanded[i:i+3]
1.20 + #print [t.value for t in triple]
1.21 +
1.22 + if triple[0].value and triple[1].value and triple[2].value:
1.23 + new_grid.append(Cell(rule[7]))
1.24 + elif triple[0].value and triple[1].value:
1.25 + new_grid.append(Cell(rule[6]))
1.26 + elif triple[0].value and triple[2].value:
1.27 + new_grid.append(Cell(rule[5]))
1.28 + elif triple[0].value:
1.29 + new_grid.append(Cell(rule[4]))
1.30 + elif triple[1].value and triple[2].value:
1.31 + new_grid.append(Cell(rule[3]))
1.32 + elif triple[1].value:
1.33 + new_grid.append(Cell(rule[2]))
1.34 + elif triple[2].value:
1.35 + new_grid.append(Cell(rule[1]))
1.36 + else:
1.37 + new_grid.append(Cell(rule[0]))
1.38 + return new_grid
1.39 +
1.40 +def show(grid, max_cells):
1.41 + import sys
1.42 + padding_left = int(0.5 + max_cells - len(grid)) / 2
1.43 + padding_right = (max_cells - len(grid)) / 2
1.44 + for i in xrange(padding_left):
1.45 + sys.stdout.write(chr(178))
1.46 + for cell in grid:
1.47 + sys.stdout.write(str(cell))
1.48 + for i in xrange(padding_right):
1.49 + sys.stdout.write(chr(178))
1.50 + print
1.51 +
1.52 +def gen_rule(i):
1.53 + rule = []
1.54 + v = i
1.55 + for x in range(8):
1.56 + rule.append(v - v/2*2)
1.57 + v = v/2
1.58 + return rule
1.59 +
1.60 +def main():
1.61 + import time
1.62 +
1.63 + max_iter = 150
1.64 + max_cells = max_iter * 2 + 1
1.65 + for i in xrange(255):
1.66 + rule = gen_rule(i)
1.67 + print "rule %i" % i
1.68 + grid = [Cell(True)]
1.69 + for i in range(max_iter):
1.70 + show(grid, max_cells)
1.71 + grid = iterate(grid, rule)
1.72 + #time.sleep(2)
1.73 +
1.74 +if __name__ == "__main__":
1.75 + main()
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/cellpimat.py Thu Dec 30 02:16:10 2010 +0100
2.3 @@ -0,0 +1,175 @@
2.4 +class Grid(object):
2.5 +
2.6 + def __init__(self):
2.7 + self.cells = {}
2.8 + self.minx = 0
2.9 + self.maxx = 0
2.10 + self.miny = 0
2.11 + self.maxy = 0
2.12 +
2.13 + def set(self, pos, value=1):
2.14 + if not pos in self.cells or value != self.cells[pos]:
2.15 + self.cells[pos] = value
2.16 + self.update(pos)
2.17 + return self
2.18 +
2.19 + def clear(self, pos):
2.20 + if pos in self.cells:
2.21 + del self.cells[pos]
2.22 + self.update(pos)
2.23 + return self
2.24 +
2.25 + def update(self, (x, y)):
2.26 + self.minx = min(x, self.minx)
2.27 + self.maxx = max(x, self.maxx)
2.28 + self.miny = min(y, self.miny)
2.29 + self.maxy = max(y, self.maxy)
2.30 + return self
2.31 +
2.32 + def width(self):
2.33 + return self.maxx - self.minx + 1
2.34 +
2.35 + def height(self):
2.36 + return self.maxy - self.miny + 1
2.37 +
2.38 + def __len__(self):
2.39 + return len(self.cells)
2.40 +
2.41 + def __str__(self):
2.42 + pass
2.43 +
2.44 +import copy
2.45 +
2.46 +class Rule(object):
2.47 +
2.48 + def iterate(self, oldgrid):
2.49 + grid = copy.deepcopy(oldgrid)
2.50 + for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
2.51 + for y in xrange(oldgrid.miny - 1, oldgrid.maxy + 2):
2.52 + #print x, y, self.neighbours(oldgrid, x, y)
2.53 + n = self.neighbours(oldgrid, (x, y))
2.54 + if n > 2:
2.55 + grid.set((x, y))
2.56 + return grid
2.57 +
2.58 + def neighbours(self, grid, (testx, testy)):
2.59 + n = 0
2.60 + #print "testing ", testx, testy
2.61 + for x in range(testx - 1, testx + 2):
2.62 + for y in range(testy - 1, testy + 2):
2.63 + if (x, y) in grid.cells:
2.64 + n += 1
2.65 + #print x, y, n
2.66 + return n
2.67 +
2.68 +
2.69 +class Rule2(object):
2.70 +
2.71 + def iterate(self, oldgrid):
2.72 + grid = copy.deepcopy(oldgrid)
2.73 + for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
2.74 + for y in xrange(oldgrid.miny, oldgrid.maxy + 2):
2.75 + #print "testing ", x, y,
2.76 + if (x+1, y) in oldgrid.cells or (x, y-1) in oldgrid.cells:
2.77 + grid.set((x, y), 1)
2.78 + return grid
2.79 +
2.80 +import random
2.81 +import marshal
2.82 +
2.83 +class PotentialGrowth(object):
2.84 +
2.85 + def __init__(self):
2.86 + random.seed()
2.87 +
2.88 + def iterate(self, oldgrid):
2.89 + grid = copy.deepcopy(oldgrid)
2.90 + for cell in oldgrid.cells.iteritems():
2.91 + pos = cell[0]
2.92 + value = cell[1]
2.93 + if value > 1:
2.94 + new_pos, new_value = self.grow(grid, pos)
2.95 + grid.set(new_pos, new_value)
2.96 + grid.set(pos, value - 1)
2.97 + return grid
2.98 +
2.99 + def grow(self, grid, (x, y)):
2.100 + neighbours = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)]
2.101 + neighbours = [n for n in neighbours if n not in grid.cells]
2.102 + try:
2.103 + pos = random.choice(neighbours)
2.104 + value = 4
2.105 + except IndexError:
2.106 + pos = (x, y)
2.107 + value = grid.cells[pos]
2.108 + return pos, value
2.109 +
2.110 +import sys
2.111 +import math
2.112 +
2.113 +def ruleTest():
2.114 + rule = Rule()
2.115 + grid = Grid()
2.116 + grid.set((0, 0))
2.117 + grid.set((0, 1)).set((1, 0)).set((0, -1)).set((-1, 0))
2.118 +
2.119 +
2.120 + print "iteration radius(diff) area(diff) pi"
2.121 +
2.122 + iterations = int(sys.argv[1])
2.123 +
2.124 + for i in range(iterations):
2.125 + A = len(grid)
2.126 + r = grid.width() / 2.0
2.127 + r_ideal = math.sqrt(A / math.pi)
2.128 + A_ideal = r**2 * math.pi
2.129 + pi = A / r**2
2.130 + print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal,
2.131 + A, A - A_ideal, pi)
2.132 +
2.133 + grid = rule.iterate(grid)
2.134 + dumpGrid(grid, "grid.cfg")
2.135 + #print
2.136 +
2.137 +def rule2Test():
2.138 + rule = Rule2()
2.139 + grid = Grid()
2.140 + grid.set((0, 0)).set((-1, 0)).set((0, 1))
2.141 + olda = 1
2.142 +
2.143 + iterations = int(sys.argv[1])
2.144 +
2.145 + for i in range(iterations):
2.146 + a = (float(len(grid)) - grid.width()) * 4.0
2.147 + pi = 1.0 / ((grid.width()-1)**2 / ((a + olda) / 2.0))
2.148 + print i, grid.width(), len(grid), pi
2.149 + grid = rule.iterate(grid)
2.150 + olda = a
2.151 + dumpGrid(grid, "grid.cfg")
2.152 +
2.153 +def potentialTest():
2.154 + rule = PotentialGrowth()
2.155 + grid = Grid()
2.156 + grid.set((0, 0), 4)
2.157 +
2.158 + iterations = int(sys.argv[1])
2.159 +
2.160 + for i in range(iterations):
2.161 + A = len(grid)
2.162 + r = grid.width() / 4.0 + grid.height() / 4.0
2.163 + r_ideal = math.sqrt(A / math.pi)
2.164 + A_ideal = r**2 * math.pi
2.165 + pi = A / r**2
2.166 + print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal,
2.167 + A, A - A_ideal, pi)
2.168 +
2.169 + grid = rule.iterate(grid)
2.170 + dumpGrid(grid, "grid.cfg")
2.171 +
2.172 +def main():
2.173 + ruleTest()
2.174 + #rule2Test()
2.175 + #potentialTest()
2.176 +
2.177 +if __name__ == "__main__":
2.178 + main()
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/marshal.py Thu Dec 30 02:16:10 2010 +0100
3.3 @@ -0,0 +1,36 @@
3.4 +import ConfigParser
3.5 +
3.6 +meta_section = "meta"
3.7 +data_section = "cells"
3.8 +
3.9 +def dumpGrid(grid, filename):
3.10 + config = ConfigParser.RawConfigParser()
3.11 +
3.12 + config.add_section(meta_section)
3.13 + config.set(meta_section, 'min x', grid.minx)
3.14 + config.set(meta_section, 'max x', grid.maxx)
3.15 + config.set(meta_section, 'min y', grid.miny)
3.16 + config.set(meta_section, 'max y', grid.maxy)
3.17 + config.set(meta_section, 'cell no', len(grid))
3.18 +
3.19 + config.add_section(data_section)
3.20 + config.set(data_section, "cells", ";".join([str(pos) for pos in grid.cells.iterkeys()]))
3.21 +
3.22 + with open(filename, 'wb') as configfile:
3.23 + config.write(configfile)
3.24 +
3.25 +def loadGrid(filename):
3.26 + config = ConfigParser.RawConfigParser()
3.27 + config.read(filename)
3.28 +
3.29 + grid = Grid()
3.30 + grid.minx = config.getint(meta_section, "min x")
3.31 + grid.maxx = config.getint(meta_section, "max x")
3.32 + grid.miny = config.getint(meta_section, "min y")
3.33 + grid.maxy = config.getint(meta_section, "max y")
3.34 +
3.35 + cells = config.get(data_section, "cells")
3.36 + for cell in cells.split(";"):
3.37 + cell = [int(cell.strip("(),")) for cell in cell.split()]
3.38 + grid.cells[(cell[0], cell[1])] = 1
3.39 + return grid
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/render.py Thu Dec 30 02:16:10 2010 +0100
4.3 @@ -0,0 +1,39 @@
4.4 +import Tkinter as tk
4.5 +
4.6 +class Render(object):
4.7 + def __init__(self, title, width, height):
4.8 + self.title = title
4.9 + self.width = width
4.10 + self.height = height
4.11 + self.root = tk.Tk(className=title, sync=1)
4.12 + self.canvas = tk.Canvas(width=self.width, height=self.height, bg="black")
4.13 + self.canvas.pack(expand=tk.YES, fill=tk.BOTH)
4.14 + def render(self, grid):
4.15 + offset = (self.width / 2, self.height / 2)
4.16 + size = 1
4.17 + cell_ids = []
4.18 + for pos in grid.cells.iterkeys():
4.19 + x = pos[0] + offset[0]
4.20 + y = pos[1] + offset[1]
4.21 + cell_id = self.canvas.create_rectangle(x, y, x+size, y+size,
4.22 + fill="yellow", width=0)
4.23 + cell_ids.append(cell_id)
4.24 + self.root.update()
4.25 + for cell_id in cell_ids:
4.26 + self.canvas.delete(cell_id)
4.27 +
4.28 +import sys
4.29 +import time
4.30 +import cellpimat
4.31 +
4.32 +def main():
4.33 + filename = sys.argv[1]
4.34 + print "rendering ", filename
4.35 + render = Render("hope", 800, 800)
4.36 + while True:
4.37 + grid = cellpimat.loadGrid(filename)
4.38 + render.render(grid)
4.39 + time.sleep(1)
4.40 +
4.41 +if __name__ == "__main__":
4.42 + main()