caumat.py
author Eugen Sawin <sawine@me73.com>
Thu, 30 Dec 2010 15:19:36 +0100
changeset 5 dd036093fc09
parent 4 20edf3e369a2
child 6 cac4ae0f22f4
permissions -rw-r--r--
More efficient grid iteration.
     1 import copy
     2 from caugrid import Grid
     3 
     4 class Rule(object):
     5 
     6 	def iterate(self, oldgrid):
     7 		grid = copy.deepcopy(oldgrid)
     8 		for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
     9 			for y in xrange(oldgrid.miny - 1, oldgrid.maxy + 2):
    10 				#print x, y, self.neighbours(oldgrid, x, y)
    11 				n = self.neighbours(oldgrid, (x, y))
    12 				if n > 2:
    13 					grid.set((x, y))					
    14 		return grid
    15 
    16 	def neighbours(self, grid, (testx, testy)):
    17 		n = 0
    18 		#print "testing ", testx, testy
    19 		for x in range(testx - 1, testx + 2):
    20 			for y in range(testy - 1, testy + 2):
    21 				if (x, y) in grid.cells:
    22 					n += 1
    23 				#print x, y, n
    24 		return n 
    25 	
    26 
    27 class Rule2(object):
    28 
    29 	def iterate(self, oldgrid):
    30 		grid = copy.deepcopy(oldgrid)
    31 		for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
    32 			for y in xrange(oldgrid.miny, oldgrid.maxy + 2):
    33 				#print "testing ", x, y,
    34 				if (x+1, y) in oldgrid.cells or (x, y-1) in oldgrid.cells:
    35 					grid.set((x, y), 1)								
    36 		return grid
    37 
    38 import random
    39 import caumarshal as marshal
    40 
    41 class PotentialGrowth(object):
    42 		
    43 	def __init__(self):
    44 		random.seed()
    45 
    46 	def iterate(self, oldgrid):
    47 		grid = oldgrid
    48 		value_pos = {}
    49 		for i in range(2, 5):
    50 			if i in oldgrid.valuemap:
    51 				value_pos[i] = copy.deepcopy(oldgrid.valuemap[i])
    52 		for i in range(2, 5):
    53 			if i in oldgrid.valuemap:
    54 				for cell in value_pos[i]:
    55 					pos = cell
    56 					value = i
    57 					new_pos, new_value = self.grow(grid, pos)
    58 					grid.set(new_pos, new_value)
    59 					grid.set(pos, value - 1)				
    60 		return grid
    61 
    62 	def grow(self, grid, (x, y)):
    63 		n1 = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)]
    64 		n2 = [(x+1, y+1), (x+1, y-1), (x-1, y-1), (x-1, y+1)]
    65 		neighbours = random.choice((n1, n1, n1, n1, n1, n1, n2, n2, n2))
    66 		neighbours = [n for n in neighbours if n not in grid.cells]
    67 		if len(neighbours):
    68 			pos = random.choice(neighbours)	
    69 			value = 4
    70 		else:
    71 			pos = (x, y)
    72 			value = grid.cells[pos]
    73 		return pos, value
    74 
    75 import sys
    76 import math
    77 
    78 def ruleTest():
    79 	rule = Rule()
    80 	grid = Grid()
    81 	grid.set((0, 0))
    82 	grid.set((0, 1)).set((1, 0)).set((0, -1)).set((-1, 0))
    83 	
    84 	
    85 	print "iteration radius(diff) area(diff) pi"
    86 
    87 	iterations = int(sys.argv[1])
    88 
    89 	for i in range(iterations):
    90 		A = len(grid)
    91 		r = grid.width() / 2.0
    92 		r_ideal = math.sqrt(A / math.pi)
    93 		A_ideal = r**2 * math.pi
    94 		pi = A / r**2
    95 		print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, 
    96 										A, A - A_ideal, pi)
    97 
    98 		grid = rule.iterate(grid)
    99 		marshal.dumpGrid(grid, "grid.cfg")
   100 		#print
   101 
   102 def rule2Test():
   103 	rule = Rule2()
   104 	grid = Grid()
   105 	grid.set((0, 0)).set((-1, 0)).set((0, 1))
   106 	olda = 1
   107 
   108 	iterations = int(sys.argv[1])
   109 
   110 	for i in range(iterations):
   111 		a =  (float(len(grid)) - grid.width()) * 4.0
   112 		pi = 1.0 / ((grid.width()-1)**2 / ((a + olda) / 2.0))
   113 		print i, grid.width(), len(grid), pi
   114 		grid = rule.iterate(grid)
   115 		olda = a
   116 		marshal.dumpGrid(grid, "grid.cfg")
   117 
   118 def potentialTest():
   119 	rule = PotentialGrowth()
   120 	grid = Grid()
   121 	grid.set((0, 0), 4)
   122 
   123 	iterations = int(sys.argv[1])
   124 	last_pi = 3.0
   125 	for i in range(iterations):
   126 		A = len(grid)
   127 		r = grid.width() / 4.0 + grid.height() / 4.0
   128 		r_ideal = math.sqrt(A / math.pi)
   129 		A_ideal = r**2 * math.pi
   130 		pi = A / r**2
   131 		pi = (pi + last_pi) / 2.0
   132 		last_pi = pi
   133 		print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, 
   134 										A, A - A_ideal, pi)
   135 
   136 		grid = rule.iterate(grid)
   137 		marshal.dumpGrid(grid, "grid.cfg")
   138 
   139 def main():
   140 	#ruleTest()
   141 	#rule2Test()
   142 	potentialTest()
   143 
   144 if __name__ == "__main__":
   145 	main()