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()