# HG changeset patch # User Eugen Sawin # Date 1293715325 -3600 # Node ID 89d76549ba6e65e9ce241055aadf0b7c0eb8c428 # Parent ccbf96145796d147af856d3f61c9bc43ffd2127f Fixed some. Added module prefix. diff -r ccbf96145796 -r 89d76549ba6e caugrid.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/caugrid.py Thu Dec 30 14:22:05 2010 +0100 @@ -0,0 +1,39 @@ +class Grid(object): + + def __init__(self): + self.cells = {} + self.minx = 0 + self.maxx = 0 + self.miny = 0 + self.maxy = 0 + + def set(self, pos, value=1): + if not pos in self.cells or value != self.cells[pos]: + self.cells[pos] = value + self.update(pos) + return self + + def clear(self, pos): + if pos in self.cells: + 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 ccbf96145796 -r 89d76549ba6e caumarshal.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/caumarshal.py Thu Dec 30 14:22:05 2010 +0100 @@ -0,0 +1,37 @@ +import ConfigParser +from caugrid 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 ccbf96145796 -r 89d76549ba6e caumat.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/caumat.py Thu Dec 30 14:22:05 2010 +0100 @@ -0,0 +1,136 @@ +import copy +from caugrid 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 caumarshal as marshal + +class PotentialGrowth(object): + + def __init__(self): + random.seed() + + def iterate(self, oldgrid): + grid = copy.deepcopy(oldgrid) + for cell in oldgrid.cells.iteritems(): + pos = cell[0] + value = cell[1] + if value > 1: + 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)): + neighbours = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)] + neighbours = [n for n in neighbours if n not in grid.cells] + try: + pos = random.choice(neighbours) + value = 4 + except IndexError: + pos = (x, y) + value = grid.cells[pos] + return pos, value + +import sys +import math + +def ruleTest(): + rule = 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 = 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 = PotentialGrowth() + grid = Grid() + grid.set((0, 0), 4) + + iterations = int(sys.argv[1]) + + 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 + 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 ccbf96145796 -r 89d76549ba6e caurender.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/caurender.py Thu Dec 30 14:22:05 2010 +0100 @@ -0,0 +1,46 @@ +import Tkinter as tk +from caugrid 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 caumarshal 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" + grid = marshal.loadGrid(filename) + render.render(grid) + time.sleep(1) + +if __name__ == "__main__": + main() diff -r ccbf96145796 -r 89d76549ba6e cellpimat.py --- a/cellpimat.py Thu Dec 30 02:16:10 2010 +0100 +++ b/cellpimat.py Thu Dec 30 14:22:05 2010 +0100 @@ -1,44 +1,5 @@ -class Grid(object): - - def __init__(self): - self.cells = {} - self.minx = 0 - self.maxx = 0 - self.miny = 0 - self.maxy = 0 - - def set(self, pos, value=1): - if not pos in self.cells or value != self.cells[pos]: - self.cells[pos] = value - self.update(pos) - return self - - def clear(self, pos): - if pos in self.cells: - 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 - import copy +from caugrid import Grid class Rule(object): @@ -75,7 +36,7 @@ return grid import random -import marshal +import caumarshal as marshal class PotentialGrowth(object): @@ -128,7 +89,7 @@ A, A - A_ideal, pi) grid = rule.iterate(grid) - dumpGrid(grid, "grid.cfg") + marshal.dumpGrid(grid, "grid.cfg") #print def rule2Test(): @@ -145,7 +106,7 @@ print i, grid.width(), len(grid), pi grid = rule.iterate(grid) olda = a - dumpGrid(grid, "grid.cfg") + marshal.dumpGrid(grid, "grid.cfg") def potentialTest(): rule = PotentialGrowth() @@ -164,12 +125,12 @@ A, A - A_ideal, pi) grid = rule.iterate(grid) - dumpGrid(grid, "grid.cfg") + marshal.dumpGrid(grid, "grid.cfg") def main(): - ruleTest() + #ruleTest() #rule2Test() - #potentialTest() + potentialTest() if __name__ == "__main__": main()