More efficient grid iteration.
authorEugen Sawin <sawine@me73.com>
Thu, 30 Dec 2010 15:19:36 +0100
changeset 5dd036093fc09
parent 4 20edf3e369a2
child 6 cac4ae0f22f4
More efficient grid iteration.
caugrid.py
caumat.py
     1.1 --- a/caugrid.py	Thu Dec 30 14:36:25 2010 +0100
     1.2 +++ b/caugrid.py	Thu Dec 30 15:19:36 2010 +0100
     1.3 @@ -2,19 +2,27 @@
     1.4  
     1.5  	def __init__(self):
     1.6  		self.cells = {}
     1.7 +		self.valuemap = {}
     1.8  		self.minx = 0
     1.9  		self.maxx = 0
    1.10  		self.miny = 0
    1.11  		self.maxy = 0
    1.12  
    1.13  	def set(self, pos, value=1):
    1.14 -		if not pos in self.cells or value != self.cells[pos]:
    1.15 +		if pos in self.cells and value != self.cells[pos]:
    1.16 +			self.clear(pos)
    1.17 +		if not pos in self.cells:
    1.18  			self.cells[pos] = value
    1.19 +			if value in self.valuemap:
    1.20 +				self.valuemap[value].append(pos)
    1.21 +			else:
    1.22 +				self.valuemap[value] = [pos]
    1.23  			self.update(pos)
    1.24  		return self
    1.25  
    1.26  	def clear(self, pos):
    1.27 -		if pos in self.cells:
    1.28 +		if pos in self.cells: 
    1.29 +			self.valuemap[self.cells[pos]].remove(pos)
    1.30  			del self.cells[pos]
    1.31  			self.update(pos)
    1.32  		return self
     2.1 --- a/caumat.py	Thu Dec 30 14:36:25 2010 +0100
     2.2 +++ b/caumat.py	Thu Dec 30 15:19:36 2010 +0100
     2.3 @@ -44,20 +44,25 @@
     2.4  		random.seed()
     2.5  
     2.6  	def iterate(self, oldgrid):
     2.7 -		grid = copy.deepcopy(oldgrid)
     2.8 -		for cell in oldgrid.cells.iteritems():
     2.9 -			pos = cell[0]
    2.10 -			value = cell[1]
    2.11 -			if value > 1:
    2.12 -				new_pos, new_value = self.grow(grid, pos)
    2.13 -				grid.set(new_pos, new_value)
    2.14 -				grid.set(pos, value - 1)
    2.15 +		grid = oldgrid
    2.16 +		value_pos = {}
    2.17 +		for i in range(2, 5):
    2.18 +			if i in oldgrid.valuemap:
    2.19 +				value_pos[i] = copy.deepcopy(oldgrid.valuemap[i])
    2.20 +		for i in range(2, 5):
    2.21 +			if i in oldgrid.valuemap:
    2.22 +				for cell in value_pos[i]:
    2.23 +					pos = cell
    2.24 +					value = i
    2.25 +					new_pos, new_value = self.grow(grid, pos)
    2.26 +					grid.set(new_pos, new_value)
    2.27 +					grid.set(pos, value - 1)				
    2.28  		return grid
    2.29  
    2.30  	def grow(self, grid, (x, y)):
    2.31  		n1 = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)]
    2.32  		n2 = [(x+1, y+1), (x+1, y-1), (x-1, y-1), (x-1, y+1)]
    2.33 -		neighbours = random.choice((n1, n1, n2))
    2.34 +		neighbours = random.choice((n1, n1, n1, n1, n1, n1, n2, n2, n2))
    2.35  		neighbours = [n for n in neighbours if n not in grid.cells]
    2.36  		if len(neighbours):
    2.37  			pos = random.choice(neighbours)	
    2.38 @@ -116,13 +121,15 @@
    2.39  	grid.set((0, 0), 4)
    2.40  
    2.41  	iterations = int(sys.argv[1])
    2.42 -
    2.43 +	last_pi = 3.0
    2.44  	for i in range(iterations):
    2.45  		A = len(grid)
    2.46  		r = grid.width() / 4.0 + grid.height() / 4.0
    2.47  		r_ideal = math.sqrt(A / math.pi)
    2.48  		A_ideal = r**2 * math.pi
    2.49  		pi = A / r**2
    2.50 +		pi = (pi + last_pi) / 2.0
    2.51 +		last_pi = pi
    2.52  		print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, 
    2.53  										A, A - A_ideal, pi)
    2.54