1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/caumat.py Thu Dec 30 14:22:05 2010 +0100
1.3 @@ -0,0 +1,136 @@
1.4 +import copy
1.5 +from caugrid import Grid
1.6 +
1.7 +class Rule(object):
1.8 +
1.9 + def iterate(self, oldgrid):
1.10 + grid = copy.deepcopy(oldgrid)
1.11 + for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
1.12 + for y in xrange(oldgrid.miny - 1, oldgrid.maxy + 2):
1.13 + #print x, y, self.neighbours(oldgrid, x, y)
1.14 + n = self.neighbours(oldgrid, (x, y))
1.15 + if n > 2:
1.16 + grid.set((x, y))
1.17 + return grid
1.18 +
1.19 + def neighbours(self, grid, (testx, testy)):
1.20 + n = 0
1.21 + #print "testing ", testx, testy
1.22 + for x in range(testx - 1, testx + 2):
1.23 + for y in range(testy - 1, testy + 2):
1.24 + if (x, y) in grid.cells:
1.25 + n += 1
1.26 + #print x, y, n
1.27 + return n
1.28 +
1.29 +
1.30 +class Rule2(object):
1.31 +
1.32 + def iterate(self, oldgrid):
1.33 + grid = copy.deepcopy(oldgrid)
1.34 + for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
1.35 + for y in xrange(oldgrid.miny, oldgrid.maxy + 2):
1.36 + #print "testing ", x, y,
1.37 + if (x+1, y) in oldgrid.cells or (x, y-1) in oldgrid.cells:
1.38 + grid.set((x, y), 1)
1.39 + return grid
1.40 +
1.41 +import random
1.42 +import caumarshal as marshal
1.43 +
1.44 +class PotentialGrowth(object):
1.45 +
1.46 + def __init__(self):
1.47 + random.seed()
1.48 +
1.49 + def iterate(self, oldgrid):
1.50 + grid = copy.deepcopy(oldgrid)
1.51 + for cell in oldgrid.cells.iteritems():
1.52 + pos = cell[0]
1.53 + value = cell[1]
1.54 + if value > 1:
1.55 + new_pos, new_value = self.grow(grid, pos)
1.56 + grid.set(new_pos, new_value)
1.57 + grid.set(pos, value - 1)
1.58 + return grid
1.59 +
1.60 + def grow(self, grid, (x, y)):
1.61 + neighbours = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)]
1.62 + neighbours = [n for n in neighbours if n not in grid.cells]
1.63 + try:
1.64 + pos = random.choice(neighbours)
1.65 + value = 4
1.66 + except IndexError:
1.67 + pos = (x, y)
1.68 + value = grid.cells[pos]
1.69 + return pos, value
1.70 +
1.71 +import sys
1.72 +import math
1.73 +
1.74 +def ruleTest():
1.75 + rule = Rule()
1.76 + grid = Grid()
1.77 + grid.set((0, 0))
1.78 + grid.set((0, 1)).set((1, 0)).set((0, -1)).set((-1, 0))
1.79 +
1.80 +
1.81 + print "iteration radius(diff) area(diff) pi"
1.82 +
1.83 + iterations = int(sys.argv[1])
1.84 +
1.85 + for i in range(iterations):
1.86 + A = len(grid)
1.87 + r = grid.width() / 2.0
1.88 + r_ideal = math.sqrt(A / math.pi)
1.89 + A_ideal = r**2 * math.pi
1.90 + pi = A / r**2
1.91 + print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal,
1.92 + A, A - A_ideal, pi)
1.93 +
1.94 + grid = rule.iterate(grid)
1.95 + marshal.dumpGrid(grid, "grid.cfg")
1.96 + #print
1.97 +
1.98 +def rule2Test():
1.99 + rule = Rule2()
1.100 + grid = Grid()
1.101 + grid.set((0, 0)).set((-1, 0)).set((0, 1))
1.102 + olda = 1
1.103 +
1.104 + iterations = int(sys.argv[1])
1.105 +
1.106 + for i in range(iterations):
1.107 + a = (float(len(grid)) - grid.width()) * 4.0
1.108 + pi = 1.0 / ((grid.width()-1)**2 / ((a + olda) / 2.0))
1.109 + print i, grid.width(), len(grid), pi
1.110 + grid = rule.iterate(grid)
1.111 + olda = a
1.112 + marshal.dumpGrid(grid, "grid.cfg")
1.113 +
1.114 +def potentialTest():
1.115 + rule = PotentialGrowth()
1.116 + grid = Grid()
1.117 + grid.set((0, 0), 4)
1.118 +
1.119 + iterations = int(sys.argv[1])
1.120 +
1.121 + for i in range(iterations):
1.122 + A = len(grid)
1.123 + r = grid.width() / 4.0 + grid.height() / 4.0
1.124 + r_ideal = math.sqrt(A / math.pi)
1.125 + A_ideal = r**2 * math.pi
1.126 + pi = A / r**2
1.127 + print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal,
1.128 + A, A - A_ideal, pi)
1.129 +
1.130 + grid = rule.iterate(grid)
1.131 + marshal.dumpGrid(grid, "grid.cfg")
1.132 +
1.133 +def main():
1.134 + #ruleTest()
1.135 + #rule2Test()
1.136 + potentialTest()
1.137 +
1.138 +if __name__ == "__main__":
1.139 + main()