Fixed some. Added module prefix.
authorEugen Sawin <sawine@me73.com>
Thu, 30 Dec 2010 14:22:05 +0100
changeset 389d76549ba6e
parent 2 ccbf96145796
child 4 20edf3e369a2
Fixed some. Added module prefix.
caugrid.py
caumarshal.py
caumat.py
caurender.py
cellpimat.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/caugrid.py	Thu Dec 30 14:22:05 2010 +0100
     1.3 @@ -0,0 +1,39 @@
     1.4 +class Grid(object):
     1.5 +
     1.6 +	def __init__(self):
     1.7 +		self.cells = {}
     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 +			self.cells[pos] = value
    1.16 +			self.update(pos)
    1.17 +		return self
    1.18 +
    1.19 +	def clear(self, pos):
    1.20 +		if pos in self.cells:
    1.21 +			del self.cells[pos]
    1.22 +			self.update(pos)
    1.23 +		return self
    1.24 +
    1.25 +	def update(self, (x, y)):
    1.26 +		self.minx = min(x, self.minx)
    1.27 +		self.maxx = max(x, self.maxx)
    1.28 +		self.miny = min(y, self.miny)
    1.29 +		self.maxy = max(y, self.maxy)
    1.30 +		return self
    1.31 +
    1.32 +	def width(self):
    1.33 +		return self.maxx - self.minx + 1
    1.34 +
    1.35 +	def height(self):
    1.36 +		return self.maxy - self.miny + 1
    1.37 +
    1.38 +	def __len__(self):
    1.39 +		return len(self.cells)
    1.40 +
    1.41 +	def __str__(self):
    1.42 +		pass
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/caumarshal.py	Thu Dec 30 14:22:05 2010 +0100
     2.3 @@ -0,0 +1,37 @@
     2.4 +import ConfigParser
     2.5 +from caugrid import Grid
     2.6 +
     2.7 +meta_section = "meta"
     2.8 +data_section = "cells"
     2.9 +
    2.10 +def dumpGrid(grid, filename):
    2.11 +	config = ConfigParser.RawConfigParser()
    2.12 +
    2.13 +	config.add_section(meta_section)
    2.14 +	config.set(meta_section, 'min x', grid.minx)
    2.15 +	config.set(meta_section, 'max x', grid.maxx)
    2.16 +	config.set(meta_section, 'min y', grid.miny)
    2.17 +	config.set(meta_section, 'max y', grid.maxy)
    2.18 +	config.set(meta_section, 'cell no', len(grid))	
    2.19 +	
    2.20 +	config.add_section(data_section)
    2.21 +	config.set(data_section, "cells", ";".join([str(pos) for pos in grid.cells.iterkeys()]))
    2.22 +
    2.23 +	with open(filename, 'wb') as configfile:
    2.24 +		config.write(configfile)
    2.25 +
    2.26 +def loadGrid(filename):
    2.27 +	config = ConfigParser.RawConfigParser()
    2.28 +	config.read(filename)
    2.29 +
    2.30 +	grid = Grid()
    2.31 +	grid.minx = config.getint(meta_section, "min x")
    2.32 +	grid.maxx = config.getint(meta_section, "max x")
    2.33 +	grid.miny = config.getint(meta_section, "min y")
    2.34 +	grid.maxy = config.getint(meta_section, "max y")
    2.35 +
    2.36 +	cells = config.get(data_section, "cells")
    2.37 +	for cell in cells.split(";"):
    2.38 +		cell = [int(cell.strip("(),")) for cell in cell.split()]
    2.39 +		grid.cells[(cell[0], cell[1])] = 1
    2.40 +	return grid
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/caumat.py	Thu Dec 30 14:22:05 2010 +0100
     3.3 @@ -0,0 +1,136 @@
     3.4 +import copy
     3.5 +from caugrid import Grid
     3.6 +
     3.7 +class Rule(object):
     3.8 +
     3.9 +	def iterate(self, oldgrid):
    3.10 +		grid = copy.deepcopy(oldgrid)
    3.11 +		for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
    3.12 +			for y in xrange(oldgrid.miny - 1, oldgrid.maxy + 2):
    3.13 +				#print x, y, self.neighbours(oldgrid, x, y)
    3.14 +				n = self.neighbours(oldgrid, (x, y))
    3.15 +				if n > 2:
    3.16 +					grid.set((x, y))					
    3.17 +		return grid
    3.18 +
    3.19 +	def neighbours(self, grid, (testx, testy)):
    3.20 +		n = 0
    3.21 +		#print "testing ", testx, testy
    3.22 +		for x in range(testx - 1, testx + 2):
    3.23 +			for y in range(testy - 1, testy + 2):
    3.24 +				if (x, y) in grid.cells:
    3.25 +					n += 1
    3.26 +				#print x, y, n
    3.27 +		return n 
    3.28 +	
    3.29 +
    3.30 +class Rule2(object):
    3.31 +
    3.32 +	def iterate(self, oldgrid):
    3.33 +		grid = copy.deepcopy(oldgrid)
    3.34 +		for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
    3.35 +			for y in xrange(oldgrid.miny, oldgrid.maxy + 2):
    3.36 +				#print "testing ", x, y,
    3.37 +				if (x+1, y) in oldgrid.cells or (x, y-1) in oldgrid.cells:
    3.38 +					grid.set((x, y), 1)								
    3.39 +		return grid
    3.40 +
    3.41 +import random
    3.42 +import caumarshal as marshal
    3.43 +
    3.44 +class PotentialGrowth(object):
    3.45 +		
    3.46 +	def __init__(self):
    3.47 +		random.seed()
    3.48 +
    3.49 +	def iterate(self, oldgrid):
    3.50 +		grid = copy.deepcopy(oldgrid)
    3.51 +		for cell in oldgrid.cells.iteritems():
    3.52 +			pos = cell[0]
    3.53 +			value = cell[1]
    3.54 +			if value > 1:
    3.55 +				new_pos, new_value = self.grow(grid, pos)
    3.56 +				grid.set(new_pos, new_value)
    3.57 +				grid.set(pos, value - 1)
    3.58 +		return grid
    3.59 +
    3.60 +	def grow(self, grid, (x, y)):
    3.61 +		neighbours = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)]
    3.62 +		neighbours = [n for n in neighbours if n not in grid.cells]
    3.63 +		try:
    3.64 +			pos = random.choice(neighbours)	
    3.65 +			value = 4
    3.66 +		except IndexError:
    3.67 +			pos = (x, y)
    3.68 +			value = grid.cells[pos]
    3.69 +		return pos, value
    3.70 +
    3.71 +import sys
    3.72 +import math
    3.73 +
    3.74 +def ruleTest():
    3.75 +	rule = Rule()
    3.76 +	grid = Grid()
    3.77 +	grid.set((0, 0))
    3.78 +	grid.set((0, 1)).set((1, 0)).set((0, -1)).set((-1, 0))
    3.79 +	
    3.80 +	
    3.81 +	print "iteration radius(diff) area(diff) pi"
    3.82 +
    3.83 +	iterations = int(sys.argv[1])
    3.84 +
    3.85 +	for i in range(iterations):
    3.86 +		A = len(grid)
    3.87 +		r = grid.width() / 2.0
    3.88 +		r_ideal = math.sqrt(A / math.pi)
    3.89 +		A_ideal = r**2 * math.pi
    3.90 +		pi = A / r**2
    3.91 +		print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, 
    3.92 +										A, A - A_ideal, pi)
    3.93 +
    3.94 +		grid = rule.iterate(grid)
    3.95 +		marshal.dumpGrid(grid, "grid.cfg")
    3.96 +		#print
    3.97 +
    3.98 +def rule2Test():
    3.99 +	rule = Rule2()
   3.100 +	grid = Grid()
   3.101 +	grid.set((0, 0)).set((-1, 0)).set((0, 1))
   3.102 +	olda = 1
   3.103 +
   3.104 +	iterations = int(sys.argv[1])
   3.105 +
   3.106 +	for i in range(iterations):
   3.107 +		a =  (float(len(grid)) - grid.width()) * 4.0
   3.108 +		pi = 1.0 / ((grid.width()-1)**2 / ((a + olda) / 2.0))
   3.109 +		print i, grid.width(), len(grid), pi
   3.110 +		grid = rule.iterate(grid)
   3.111 +		olda = a
   3.112 +		marshal.dumpGrid(grid, "grid.cfg")
   3.113 +
   3.114 +def potentialTest():
   3.115 +	rule = PotentialGrowth()
   3.116 +	grid = Grid()
   3.117 +	grid.set((0, 0), 4)
   3.118 +
   3.119 +	iterations = int(sys.argv[1])
   3.120 +
   3.121 +	for i in range(iterations):
   3.122 +		A = len(grid)
   3.123 +		r = grid.width() / 4.0 + grid.height() / 4.0
   3.124 +		r_ideal = math.sqrt(A / math.pi)
   3.125 +		A_ideal = r**2 * math.pi
   3.126 +		pi = A / r**2
   3.127 +		print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, 
   3.128 +										A, A - A_ideal, pi)
   3.129 +
   3.130 +		grid = rule.iterate(grid)
   3.131 +		marshal.dumpGrid(grid, "grid.cfg")
   3.132 +
   3.133 +def main():
   3.134 +	#ruleTest()
   3.135 +	#rule2Test()
   3.136 +	potentialTest()
   3.137 +
   3.138 +if __name__ == "__main__":
   3.139 +	main()
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/caurender.py	Thu Dec 30 14:22:05 2010 +0100
     4.3 @@ -0,0 +1,46 @@
     4.4 +import Tkinter as tk
     4.5 +from caugrid import Grid
     4.6 +
     4.7 +class Render(object):
     4.8 +	def __init__(self, title, width, height):
     4.9 +		self.title = title		
    4.10 +		self.width = width
    4.11 +		self.height = height
    4.12 +		self.root = tk.Tk(className=title, sync=1)
    4.13 +		self.canvas = tk.Canvas(width=self.width, height=self.height, bg="black")
    4.14 +		self.canvas.pack(expand=tk.YES, fill=tk.BOTH)
    4.15 +	def render(self, grid):
    4.16 +		offset = (self.width / 2, self.height / 2)
    4.17 +		size = 1
    4.18 +		cell_ids = []
    4.19 +		for pos in grid.cells.iterkeys():
    4.20 +			x = pos[0] + offset[0]
    4.21 +			y = pos[1] + offset[1]
    4.22 +			cell_id = self.canvas.create_rectangle(x, y, x+size, y+size, 
    4.23 +					fill="yellow", width=0)
    4.24 +			cell_ids.append(cell_id)
    4.25 +		self.root.update()
    4.26 +		for cell_id in cell_ids:
    4.27 +			self.canvas.delete(cell_id)
    4.28 +
    4.29 +import sys
    4.30 +import time
    4.31 +import caumarshal as marshal
    4.32 +import os, stat
    4.33 +
    4.34 +def main():
    4.35 +	filename = sys.argv[1]
    4.36 +	print "rendering ", filename	
    4.37 +	render = Render("hope", 800, 800)
    4.38 +	mod_time = 0
    4.39 +	while True:
    4.40 +		current = os.stat(filename)[stat.ST_MTIME]
    4.41 +		if current > mod_time:
    4.42 +			mod_time = current
    4.43 +			print "updating render"
    4.44 +			grid = marshal.loadGrid(filename)
    4.45 +			render.render(grid)
    4.46 +		time.sleep(1)
    4.47 +
    4.48 +if __name__ == "__main__":
    4.49 +	main()
     5.1 --- a/cellpimat.py	Thu Dec 30 02:16:10 2010 +0100
     5.2 +++ b/cellpimat.py	Thu Dec 30 14:22:05 2010 +0100
     5.3 @@ -1,44 +1,5 @@
     5.4 -class Grid(object):
     5.5 -
     5.6 -	def __init__(self):
     5.7 -		self.cells = {}
     5.8 -		self.minx = 0
     5.9 -		self.maxx = 0
    5.10 -		self.miny = 0
    5.11 -		self.maxy = 0
    5.12 -
    5.13 -	def set(self, pos, value=1):
    5.14 -		if not pos in self.cells or value != self.cells[pos]:
    5.15 -			self.cells[pos] = value
    5.16 -			self.update(pos)
    5.17 -		return self
    5.18 -
    5.19 -	def clear(self, pos):
    5.20 -		if pos in self.cells:
    5.21 -			del self.cells[pos]
    5.22 -			self.update(pos)
    5.23 -		return self
    5.24 -
    5.25 -	def update(self, (x, y)):
    5.26 -		self.minx = min(x, self.minx)
    5.27 -		self.maxx = max(x, self.maxx)
    5.28 -		self.miny = min(y, self.miny)
    5.29 -		self.maxy = max(y, self.maxy)
    5.30 -		return self
    5.31 -
    5.32 -	def width(self):
    5.33 -		return self.maxx - self.minx + 1
    5.34 -
    5.35 -	def height(self):
    5.36 -		return self.maxy - self.miny + 1
    5.37 -
    5.38 -	def __len__(self):
    5.39 -		return len(self.cells)
    5.40 -
    5.41 -	def __str__(self):
    5.42 -		pass
    5.43 -
    5.44  import copy
    5.45 +from caugrid import Grid
    5.46  
    5.47  class Rule(object):
    5.48  
    5.49 @@ -75,7 +36,7 @@
    5.50  		return grid
    5.51  
    5.52  import random
    5.53 -import marshal
    5.54 +import caumarshal as marshal
    5.55  
    5.56  class PotentialGrowth(object):
    5.57  		
    5.58 @@ -128,7 +89,7 @@
    5.59  										A, A - A_ideal, pi)
    5.60  
    5.61  		grid = rule.iterate(grid)
    5.62 -		dumpGrid(grid, "grid.cfg")
    5.63 +		marshal.dumpGrid(grid, "grid.cfg")
    5.64  		#print
    5.65  
    5.66  def rule2Test():
    5.67 @@ -145,7 +106,7 @@
    5.68  		print i, grid.width(), len(grid), pi
    5.69  		grid = rule.iterate(grid)
    5.70  		olda = a
    5.71 -		dumpGrid(grid, "grid.cfg")
    5.72 +		marshal.dumpGrid(grid, "grid.cfg")
    5.73  
    5.74  def potentialTest():
    5.75  	rule = PotentialGrowth()
    5.76 @@ -164,12 +125,12 @@
    5.77  										A, A - A_ideal, pi)
    5.78  
    5.79  		grid = rule.iterate(grid)
    5.80 -		dumpGrid(grid, "grid.cfg")
    5.81 +		marshal.dumpGrid(grid, "grid.cfg")
    5.82  
    5.83  def main():
    5.84 -	ruleTest()
    5.85 +	#ruleTest()
    5.86  	#rule2Test()
    5.87 -	#potentialTest()
    5.88 +	potentialTest()
    5.89  
    5.90  if __name__ == "__main__":
    5.91  	main()