Script for a certain kind of 1D cellular automata

For scripts to aid with computation or simulation in cellular automata.
Post Reply
User avatar
_zM
Posts: 186
Joined: June 26th, 2016, 3:07 pm

Script for a certain kind of 1D cellular automata

Post by _zM » September 25th, 2016, 3:34 pm

This is not a Golly script. It is designed to be run from command line.
EDIT: This script is
I wrote a Python script that lets you explore an as of yet unnamed class of one-dimensional cellular automata.

These cellular automata are defined as follows:

If the value of a cell, its left and its right neighbor combined fall between c and d, that cell's value is increased by a, but if it is more than b, it gets decreased to 0. Otherwise, the value is decreased by 1, unless the current value is 0, in which case nothing happens.

The script requires Python 2.7 and pygame, and supports viewing in real time as well as saving the result as an image. The values. a,b,c and d must be supplied, and must be supplied in that order. Use the optional flag --save_as <file_path> to save an image.

Code: Select all

import pygame
import argparse
import random
import math
#import os
pygame.init()

parser = argparse.ArgumentParser(description="Run a 1D cellular automaton defined by various values.")

parser.add_argument("Increase", type = int)
parser.add_argument("Maximum", type = int)
parser.add_argument("thresholdLow", type = int)
parser.add_argument("thresholdHigh", type = int)
parser.add_argument("--save_as", dest="file")

ya = parser.parse_args()
increase = ya.Increase
maximum = ya.Maximum
thresholdLow = ya.thresholdLow
thresholdHigh = ya.thresholdHigh
try:
	filepath = ya.file
except:
	pass

xsize = 512
ysize = 512

celllist = []
newcells = []

r = 0

surface = pygame.display.set_mode((xsize,ysize))
pixarray = pygame.PixelArray(surface)
random.seed()

for i in range(xsize):
	celllist.append(math.floor(random.random()*maximum))
	newcells.append(0)
	random.seed()
for i in range(1):
	for i in range(ysize):

		for i in range(len(celllist)):

			if thresholdLow <= celllist[(i-1) % xsize]+celllist[(i+1) % xsize]+celllist[i] <= thresholdHigh:
				newcells[i] = (celllist[i] + increase)
				if newcells[i] > maximum:
					newcells[i] = 0

			elif celllist[i] == 0:
				pass

			else:
				newcells[i] = celllist[i] - 1

			value = newcells[i]*255/maximum
			pixarray[i,r]=(value,value,value)
			print(i)

		r += 1
		pygame.display.flip()
		celllist=newcells
	pygame.image.save(pixarray.make_surface(),filepath)
	pygame.quit()
Example:

Code: Select all

python ca.py 13 25 10 17
If the value of a cell, its left and its right neighbor combined fall between 10 and 17, that cell's value is increased by 13, with a cap at 25. Otherwise, the value is decreased by 1, unless the current value is 0, in which case nothing happens.
Last edited by _zM on September 26th, 2016, 2:05 pm, edited 2 times in total.
moment

Post Reply