Cell-f assembly

Inspired by the reading of AN EVOLUTIONARY ARCHITECTURE by John Frazer and the generative toolbox.

Structure generations based on John Orton Conway’s Game of Life.  The goal is to explore the emerging resulting patterns of the genetic algorithm whilst stacking all generations. The automation requires an initial finite array of cells and a maximum number of generations. Their initial state (life or death) is randomly assigned based on an arbitrary threshold. The script calls a recursive routine that decides the state of each cell (life or death) depending on the state of their neighbours (8 in total) at the end of each iteration. In order to best approximate an infinite game board, the automation wraps at the edges.

The stacking generates unexpected & elaborate patterns in xyz directions. The search for emergent behaviours is on.

The initial script has been developed in Python for Rhino5. This version is not optimized but presents a clear function split. The sumNeighbours function is inspired from an elegant solution by algorithmicdesign.net

Code:

import rhinoscriptsyntax as rs
import math
import random

def RandomGenesis(intGridi,intGridj):
arrPt=[]
for i in range(intGridi):
arrj=[]
for j in range(intGridj):
rnd = random.random()
if rnd>0.97:# density threshold for initial cells
arrj.append(1)
else:
arrj.append(0)
arrPt.append(arrj)
return arrPt # arrPt is a list of i no. lists _ each list contains j no. elements (1s or 0s)

def BrickCell(arrCtr):# BrickCell creates a square surface based on a centre point called arrCtr
arrBrickLowPt1=[(arrCtr[0]-0.5),(arrCtr[1]-0.5),arrCtr[2]]
arrBrickLowPt2=[(arrCtr[0]+0.5),(arrCtr[1]-0.5),arrCtr[2]]
arrBrickLowPt3=[(arrCtr[0]+0.5),(arrCtr[1]+0.5),arrCtr[2]]
arrBrickLowPt4=[(arrCtr[0]-0.5),(arrCtr[1]+0.5),arrCtr[2]]
arrBrickLowPoints = [arrBrickLowPt1,arrBrickLowPt2,arrBrickLowPt3,arrBrickLowPt4]
NewCell=rs.AddSrfPt(arrBrickLowPoints)
return NewCell

def ArrayCellMaker(arrPt,z): # calls BrickCell for each point in arrPt at z height
for i in range(len(arrPt)):
for j in range(len(arrPt[i])):
if arrPt[i][j]==1:
arrCtr=[i,j,z]
newCell=BrickCell(arrCtr)
return newCell
def SumNeighbours(arrPt,i,j): #sums neighbour cells values (cellN1 to >>> cellN8) starting from bottom left corner & anti clock-wise
iOneUp=i+1
iOneDown=i-1

jOneUp=j+1
jOneDown=j-1

if i==(len(arrPt)-1):iOneUp=0 #shifts missing neighbour cell at the begining of the grid’s i axis
if i==0:iOneDown=len(arrPt)-1 #shifts missing neighbour cell at the end of the grid’s i axis

if j==(len(arrPt[i])-1):jOneUp=0 #shifts missing neighbour cell at the begining of the grid’s j axis
if j==0:jOneDown=len(arrPt[i])-1 #shifts missing neighbour cell at the end of the grid’s j axis

cellN1=arrPt[iOneDown][jOneDown]
cellN2=arrPt[i][jOneDown]
cellN3=arrPt[iOneUp][jOneDown]
cellN4=arrPt[iOneUp][j]
cellN5=arrPt[iOneUp][jOneUp]
cellN6=arrPt[i][jOneUp]
cellN7=arrPt[iOneDown][jOneUp]
cellN8=arrPt[iOneDown][j]

SumNcells = cellN1+cellN2+cellN3+cellN4+cellN5+cellN6+cellN7+cellN8

return SumNcells

def MutateArray(arrPt):
newArrPt=arrPt
for i in range(len(arrPt)):
for j in range(len(arrPt[i])):
dblNumberOfMeighbours=SumNeighbours(arrPt,i,j)
if arrPt[i][j]==0:
if dblNumberOfMeighbours ==3:newArrPt[i][j]=1
if arrPt[i][j]==1:
if dblNumberOfMeighbours >3:newArrPt[i][j]=0
return newArrPt
def RecursiveGameOfLife(arrPt,Generation,intMaxGeneration):
if Generation>intMaxGeneration:return
ArrayCellMaker(arrPt,Generation)
newArrPt=MutateArray(arrPt)
RecursiveGameOfLife(newArrPt,Generation+1,intMaxGeneration)

def Main():
rs.EnableRedraw(False)

intGridi=rs.GetInteger(“number of cells in x direction”,70,5,80)
intGridj=rs.GetInteger(“number of cells in y direction”,70,5,80)
intMaxGeneration=rs.GetInteger(“number of generations”,30,10,150)
arrPt=RandomGenesis(intGridi,intGridj)
RecursiveGameOfLife(arrPt,0,intMaxGeneration)

rs.EnableRedraw(True)
return

Main()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s