# HG changeset patch # User Eugen Sawin # Date 1293730279 -3600 # Node ID 95ea605276a3681e6f1c6a31bbede5d2fdd63c54 # Parent cac4ae0f22f488f8efdfb558f348d6f78fd21e39 Moved cau to cau. diff -r cac4ae0f22f4 -r 95ea605276a3 cau/automat.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cau/automat.py Thu Dec 30 18:31:19 2010 +0100 @@ -0,0 +1,74 @@ +import sys +import math +import rules +import gridmarshal as marshal +from grid import Grid + +def ruleTest(): + rule = rules.Rule() + grid = Grid() + grid.set((0, 0)) + grid.set((0, 1)).set((1, 0)).set((0, -1)).set((-1, 0)) + + + print "iteration radius(diff) area(diff) pi" + + iterations = int(sys.argv[1]) + + for i in range(iterations): + A = len(grid) + r = grid.width() / 2.0 + r_ideal = math.sqrt(A / math.pi) + A_ideal = r**2 * math.pi + pi = A / r**2 + print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, + A, A - A_ideal, pi) + + grid = rule.iterate(grid) + marshal.dumpGrid(grid, "grid.cfg") + #print + +def rule2Test(): + rule = rules.Rule2() + grid = Grid() + grid.set((0, 0)).set((-1, 0)).set((0, 1)) + olda = 1 + + iterations = int(sys.argv[1]) + + for i in range(iterations): + a = (float(len(grid)) - grid.width()) * 4.0 + pi = 1.0 / ((grid.width()-1)**2 / ((a + olda) / 2.0)) + print i, grid.width(), len(grid), pi + grid = rule.iterate(grid) + olda = a + marshal.dumpGrid(grid, "grid.cfg") + +def potentialTest(): + rule = rules.PotentialGrowth() + grid = Grid() + grid.set((0, 0), 4) + + iterations = int(sys.argv[1]) + last_pi = 3.0 + for i in range(iterations): + A = len(grid) + r = grid.width() / 4.0 + grid.height() / 4.0 + r_ideal = math.sqrt(A / math.pi) + A_ideal = r**2 * math.pi + pi = A / r**2 + pi = (pi + last_pi) / 2.0 + last_pi = pi + print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, + A, A - A_ideal, pi) + + grid = rule.iterate(grid) + marshal.dumpGrid(grid, "grid.cfg") + +def main(): + #ruleTest() + #rule2Test() + potentialTest() + +if __name__ == "__main__": + main() diff -r cac4ae0f22f4 -r 95ea605276a3 cau/grid.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cau/grid.py Thu Dec 30 18:31:19 2010 +0100 @@ -0,0 +1,47 @@ +class Grid(object): + + def __init__(self): + self.cells = {} + self.valuemap = {} + self.minx = 0 + self.maxx = 0 + self.miny = 0 + self.maxy = 0 + + def set(self, pos, value=1): + if pos in self.cells and value != self.cells[pos]: + self.clear(pos) + if not pos in self.cells: + self.cells[pos] = value + if value in self.valuemap: + self.valuemap[value].append(pos) + else: + self.valuemap[value] = [pos] + self.update(pos) + return self + + def clear(self, pos): + if pos in self.cells: + self.valuemap[self.cells[pos]].remove(pos) + del self.cells[pos] + self.update(pos) + return self + + def update(self, (x, y)): + self.minx = min(x, self.minx) + self.maxx = max(x, self.maxx) + self.miny = min(y, self.miny) + self.maxy = max(y, self.maxy) + return self + + def width(self): + return self.maxx - self.minx + 1 + + def height(self): + return self.maxy - self.miny + 1 + + def __len__(self): + return len(self.cells) + + def __str__(self): + pass diff -r cac4ae0f22f4 -r 95ea605276a3 cau/gridmarshal.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cau/gridmarshal.py Thu Dec 30 18:31:19 2010 +0100 @@ -0,0 +1,37 @@ +import ConfigParser +from grid import Grid + +meta_section = "meta" +data_section = "cells" + +def dumpGrid(grid, filename): + config = ConfigParser.RawConfigParser() + + config.add_section(meta_section) + config.set(meta_section, 'min x', grid.minx) + config.set(meta_section, 'max x', grid.maxx) + config.set(meta_section, 'min y', grid.miny) + config.set(meta_section, 'max y', grid.maxy) + config.set(meta_section, 'cell no', len(grid)) + + config.add_section(data_section) + config.set(data_section, "cells", ";".join([str(pos) for pos in grid.cells.iterkeys()])) + + with open(filename, 'wb') as configfile: + config.write(configfile) + +def loadGrid(filename): + config = ConfigParser.RawConfigParser() + config.read(filename) + + grid = Grid() + grid.minx = config.getint(meta_section, "min x") + grid.maxx = config.getint(meta_section, "max x") + grid.miny = config.getint(meta_section, "min y") + grid.maxy = config.getint(meta_section, "max y") + + cells = config.get(data_section, "cells") + for cell in cells.split(";"): + cell = [int(cell.strip("(),")) for cell in cell.split()] + grid.cells[(cell[0], cell[1])] = 1 + return grid diff -r cac4ae0f22f4 -r 95ea605276a3 cau/render.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cau/render.py Thu Dec 30 18:31:19 2010 +0100 @@ -0,0 +1,49 @@ +import Tkinter as tk +from grid import Grid + +class Render(object): + def __init__(self, title, width, height): + self.title = title + self.width = width + self.height = height + self.root = tk.Tk(className=title, sync=1) + self.canvas = tk.Canvas(width=self.width, height=self.height, bg="black") + self.canvas.pack(expand=tk.YES, fill=tk.BOTH) + def render(self, grid): + offset = (self.width / 2, self.height / 2) + size = 1 + cell_ids = [] + for pos in grid.cells.iterkeys(): + x = pos[0] + offset[0] + y = pos[1] + offset[1] + cell_id = self.canvas.create_rectangle(x, y, x+size, y+size, + fill="yellow", width=0) + cell_ids.append(cell_id) + self.root.update() + for cell_id in cell_ids: + self.canvas.delete(cell_id) + +import sys +import time +import gridmarshal as marshal +import os, stat + +def main(): + filename = sys.argv[1] + print "rendering ", filename + render = Render("hope", 800, 800) + mod_time = 0 + while True: + current = os.stat(filename)[stat.ST_MTIME] + if current > mod_time: + mod_time = current + print "updating render" + try: + grid = marshal.loadGrid(filename) + render.render(grid) + except IndexError, ValueError: + print "loading error" + time.sleep(1) + +if __name__ == "__main__": + main() diff -r cac4ae0f22f4 -r 95ea605276a3 cau/rules.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cau/rules.py Thu Dec 30 18:31:19 2010 +0100 @@ -0,0 +1,76 @@ +import copy +from grid import Grid + +class Rule(object): + + def iterate(self, oldgrid): + grid = copy.deepcopy(oldgrid) + for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2): + for y in xrange(oldgrid.miny - 1, oldgrid.maxy + 2): + #print x, y, self.neighbours(oldgrid, x, y) + n = self.neighbours(oldgrid, (x, y)) + if n > 2: + grid.set((x, y)) + return grid + + def neighbours(self, grid, (testx, testy)): + n = 0 + #print "testing ", testx, testy + for x in range(testx - 1, testx + 2): + for y in range(testy - 1, testy + 2): + if (x, y) in grid.cells: + n += 1 + #print x, y, n + return n + + +class Rule2(object): + + def iterate(self, oldgrid): + grid = copy.deepcopy(oldgrid) + for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2): + for y in xrange(oldgrid.miny, oldgrid.maxy + 2): + #print "testing ", x, y, + if (x+1, y) in oldgrid.cells or (x, y-1) in oldgrid.cells: + grid.set((x, y), 1) + return grid + +import random +import marshal + +class PotentialGrowth(object): + + def __init__(self): + random.seed() + + def iterate(self, oldgrid): + grid = oldgrid + value_pos = {} + for i in range(2, 5): + if i in oldgrid.valuemap: + value_pos[i] = copy.deepcopy(oldgrid.valuemap[i]) + for i in range(2, 5): + if i in oldgrid.valuemap: + for cell in value_pos[i]: + pos = cell + value = i + new_pos, new_value = self.grow(grid, pos) + grid.set(new_pos, new_value) + grid.set(pos, value - 1) + return grid + + def grow(self, grid, (x, y)): + n1 = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)] + n2 = [(x+1, y+1), (x+1, y-1), (x-1, y-1), (x-1, y+1)] + c0 = (n1, ) + c1 = (n1, n2) + c2 = (n1, n1, n2) + neighbours = random.choice(random.choice((c0, c1, c2))) + neighbours = [n for n in neighbours if n not in grid.cells] + if len(neighbours): + pos = random.choice(neighbours) + value = 4 + else: + pos = (x, y) + value = grid.cells[pos] + return pos, value