Moved cau to cau.
authorEugen Sawin <sawine@me73.com>
Thu, 30 Dec 2010 18:31:19 +0100
changeset 795ea605276a3
parent 6 cac4ae0f22f4
child 8 0db344245ac2
Moved cau to cau.
cau/__init__.py
cau/automat.py
cau/grid.py
cau/gridmarshal.py
cau/render.py
cau/rules.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cau/automat.py	Thu Dec 30 18:31:19 2010 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +import sys
     1.5 +import math
     1.6 +import rules
     1.7 +import gridmarshal as marshal
     1.8 +from grid import Grid
     1.9 +
    1.10 +def ruleTest():
    1.11 +	rule = rules.Rule()
    1.12 +	grid = Grid()
    1.13 +	grid.set((0, 0))
    1.14 +	grid.set((0, 1)).set((1, 0)).set((0, -1)).set((-1, 0))
    1.15 +	
    1.16 +	
    1.17 +	print "iteration radius(diff) area(diff) pi"
    1.18 +
    1.19 +	iterations = int(sys.argv[1])
    1.20 +
    1.21 +	for i in range(iterations):
    1.22 +		A = len(grid)
    1.23 +		r = grid.width() / 2.0
    1.24 +		r_ideal = math.sqrt(A / math.pi)
    1.25 +		A_ideal = r**2 * math.pi
    1.26 +		pi = A / r**2
    1.27 +		print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, 
    1.28 +										A, A - A_ideal, pi)
    1.29 +
    1.30 +		grid = rule.iterate(grid)
    1.31 +		marshal.dumpGrid(grid, "grid.cfg")
    1.32 +		#print
    1.33 +
    1.34 +def rule2Test():
    1.35 +	rule = rules.Rule2()
    1.36 +	grid = Grid()
    1.37 +	grid.set((0, 0)).set((-1, 0)).set((0, 1))
    1.38 +	olda = 1
    1.39 +
    1.40 +	iterations = int(sys.argv[1])
    1.41 +
    1.42 +	for i in range(iterations):
    1.43 +		a =  (float(len(grid)) - grid.width()) * 4.0
    1.44 +		pi = 1.0 / ((grid.width()-1)**2 / ((a + olda) / 2.0))
    1.45 +		print i, grid.width(), len(grid), pi
    1.46 +		grid = rule.iterate(grid)
    1.47 +		olda = a
    1.48 +		marshal.dumpGrid(grid, "grid.cfg")
    1.49 +
    1.50 +def potentialTest():
    1.51 +	rule = rules.PotentialGrowth()
    1.52 +	grid = Grid()
    1.53 +	grid.set((0, 0), 4)
    1.54 +
    1.55 +	iterations = int(sys.argv[1])
    1.56 +	last_pi = 3.0
    1.57 +	for i in range(iterations):
    1.58 +		A = len(grid)
    1.59 +		r = grid.width() / 4.0 + grid.height() / 4.0
    1.60 +		r_ideal = math.sqrt(A / math.pi)
    1.61 +		A_ideal = r**2 * math.pi
    1.62 +		pi = A / r**2
    1.63 +		pi = (pi + last_pi) / 2.0
    1.64 +		last_pi = pi
    1.65 +		print "%i %f(%f) %i(%i) %f" % (i, r, r - r_ideal, 
    1.66 +										A, A - A_ideal, pi)
    1.67 +
    1.68 +		grid = rule.iterate(grid)
    1.69 +		marshal.dumpGrid(grid, "grid.cfg")
    1.70 +
    1.71 +def main():
    1.72 +	#ruleTest()
    1.73 +	#rule2Test()
    1.74 +	potentialTest()
    1.75 +
    1.76 +if __name__ == "__main__":
    1.77 +	main()
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/cau/grid.py	Thu Dec 30 18:31:19 2010 +0100
     2.3 @@ -0,0 +1,47 @@
     2.4 +class Grid(object):
     2.5 +
     2.6 +	def __init__(self):
     2.7 +		self.cells = {}
     2.8 +		self.valuemap = {}
     2.9 +		self.minx = 0
    2.10 +		self.maxx = 0
    2.11 +		self.miny = 0
    2.12 +		self.maxy = 0
    2.13 +
    2.14 +	def set(self, pos, value=1):
    2.15 +		if pos in self.cells and value != self.cells[pos]:
    2.16 +			self.clear(pos)
    2.17 +		if not pos in self.cells:
    2.18 +			self.cells[pos] = value
    2.19 +			if value in self.valuemap:
    2.20 +				self.valuemap[value].append(pos)
    2.21 +			else:
    2.22 +				self.valuemap[value] = [pos]
    2.23 +			self.update(pos)
    2.24 +		return self
    2.25 +
    2.26 +	def clear(self, pos):
    2.27 +		if pos in self.cells: 
    2.28 +			self.valuemap[self.cells[pos]].remove(pos)
    2.29 +			del self.cells[pos]
    2.30 +			self.update(pos)
    2.31 +		return self
    2.32 +
    2.33 +	def update(self, (x, y)):
    2.34 +		self.minx = min(x, self.minx)
    2.35 +		self.maxx = max(x, self.maxx)
    2.36 +		self.miny = min(y, self.miny)
    2.37 +		self.maxy = max(y, self.maxy)
    2.38 +		return self
    2.39 +
    2.40 +	def width(self):
    2.41 +		return self.maxx - self.minx + 1
    2.42 +
    2.43 +	def height(self):
    2.44 +		return self.maxy - self.miny + 1
    2.45 +
    2.46 +	def __len__(self):
    2.47 +		return len(self.cells)
    2.48 +
    2.49 +	def __str__(self):
    2.50 +		pass
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/cau/gridmarshal.py	Thu Dec 30 18:31:19 2010 +0100
     3.3 @@ -0,0 +1,37 @@
     3.4 +import ConfigParser
     3.5 +from grid import Grid
     3.6 +
     3.7 +meta_section = "meta"
     3.8 +data_section = "cells"
     3.9 +
    3.10 +def dumpGrid(grid, filename):
    3.11 +	config = ConfigParser.RawConfigParser()
    3.12 +
    3.13 +	config.add_section(meta_section)
    3.14 +	config.set(meta_section, 'min x', grid.minx)
    3.15 +	config.set(meta_section, 'max x', grid.maxx)
    3.16 +	config.set(meta_section, 'min y', grid.miny)
    3.17 +	config.set(meta_section, 'max y', grid.maxy)
    3.18 +	config.set(meta_section, 'cell no', len(grid))	
    3.19 +	
    3.20 +	config.add_section(data_section)
    3.21 +	config.set(data_section, "cells", ";".join([str(pos) for pos in grid.cells.iterkeys()]))
    3.22 +
    3.23 +	with open(filename, 'wb') as configfile:
    3.24 +		config.write(configfile)
    3.25 +
    3.26 +def loadGrid(filename):
    3.27 +	config = ConfigParser.RawConfigParser()
    3.28 +	config.read(filename)
    3.29 +
    3.30 +	grid = Grid()
    3.31 +	grid.minx = config.getint(meta_section, "min x")
    3.32 +	grid.maxx = config.getint(meta_section, "max x")
    3.33 +	grid.miny = config.getint(meta_section, "min y")
    3.34 +	grid.maxy = config.getint(meta_section, "max y")
    3.35 +
    3.36 +	cells = config.get(data_section, "cells")
    3.37 +	for cell in cells.split(";"):
    3.38 +		cell = [int(cell.strip("(),")) for cell in cell.split()]
    3.39 +		grid.cells[(cell[0], cell[1])] = 1
    3.40 +	return grid
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/cau/render.py	Thu Dec 30 18:31:19 2010 +0100
     4.3 @@ -0,0 +1,49 @@
     4.4 +import Tkinter as tk
     4.5 +from grid 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 gridmarshal 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 +			try:
    4.45 +				grid = marshal.loadGrid(filename)
    4.46 +				render.render(grid)
    4.47 +			except IndexError, ValueError:
    4.48 +				print "loading error"
    4.49 +		time.sleep(1)
    4.50 +
    4.51 +if __name__ == "__main__":
    4.52 +	main()
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/cau/rules.py	Thu Dec 30 18:31:19 2010 +0100
     5.3 @@ -0,0 +1,76 @@
     5.4 +import copy
     5.5 +from grid import Grid
     5.6 +
     5.7 +class Rule(object):
     5.8 +
     5.9 +	def iterate(self, oldgrid):
    5.10 +		grid = copy.deepcopy(oldgrid)
    5.11 +		for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
    5.12 +			for y in xrange(oldgrid.miny - 1, oldgrid.maxy + 2):
    5.13 +				#print x, y, self.neighbours(oldgrid, x, y)
    5.14 +				n = self.neighbours(oldgrid, (x, y))
    5.15 +				if n > 2:
    5.16 +					grid.set((x, y))					
    5.17 +		return grid
    5.18 +
    5.19 +	def neighbours(self, grid, (testx, testy)):
    5.20 +		n = 0
    5.21 +		#print "testing ", testx, testy
    5.22 +		for x in range(testx - 1, testx + 2):
    5.23 +			for y in range(testy - 1, testy + 2):
    5.24 +				if (x, y) in grid.cells:
    5.25 +					n += 1
    5.26 +				#print x, y, n
    5.27 +		return n 
    5.28 +	
    5.29 +
    5.30 +class Rule2(object):
    5.31 +
    5.32 +	def iterate(self, oldgrid):
    5.33 +		grid = copy.deepcopy(oldgrid)
    5.34 +		for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
    5.35 +			for y in xrange(oldgrid.miny, oldgrid.maxy + 2):
    5.36 +				#print "testing ", x, y,
    5.37 +				if (x+1, y) in oldgrid.cells or (x, y-1) in oldgrid.cells:
    5.38 +					grid.set((x, y), 1)								
    5.39 +		return grid
    5.40 +
    5.41 +import random
    5.42 +import marshal
    5.43 +
    5.44 +class PotentialGrowth(object):
    5.45 +		
    5.46 +	def __init__(self):
    5.47 +		random.seed()
    5.48 +
    5.49 +	def iterate(self, oldgrid):
    5.50 +		grid = oldgrid
    5.51 +		value_pos = {}
    5.52 +		for i in range(2, 5):
    5.53 +			if i in oldgrid.valuemap:
    5.54 +				value_pos[i] = copy.deepcopy(oldgrid.valuemap[i])
    5.55 +		for i in range(2, 5):
    5.56 +			if i in oldgrid.valuemap:
    5.57 +				for cell in value_pos[i]:
    5.58 +					pos = cell
    5.59 +					value = i
    5.60 +					new_pos, new_value = self.grow(grid, pos)
    5.61 +					grid.set(new_pos, new_value)
    5.62 +					grid.set(pos, value - 1)				
    5.63 +		return grid
    5.64 +
    5.65 +	def grow(self, grid, (x, y)):
    5.66 +		n1 = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)]
    5.67 +		n2 = [(x+1, y+1), (x+1, y-1), (x-1, y-1), (x-1, y+1)]
    5.68 +		c0 = (n1, )
    5.69 +		c1 = (n1, n2)
    5.70 +		c2 = (n1, n1, n2)
    5.71 +		neighbours = random.choice(random.choice((c0, c1, c2)))
    5.72 +		neighbours = [n for n in neighbours if n not in grid.cells]
    5.73 +		if len(neighbours):
    5.74 +			pos = random.choice(neighbours)	
    5.75 +			value = 4
    5.76 +		else:
    5.77 +			pos = (x, y)
    5.78 +			value = grid.cells[pos]
    5.79 +		return pos, value