473,809 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python and cellular automata (It works this time!)

I thought people would be interested in this little script I wrote to
reproduce the 256 simple automata that is shown in the first chapters
of "A New Kind of Science". You can see a few results without running
the script, at http://cooper-j.blogspot.com . And here is the code (You
will need PIL (Python Imaging Library)):
<code>
import Image

# Contract:
# To simulate simple cellular automata.

class calculations:
def __init__(self,w hich):
self.against = self.array_make r_2()[which]
self.check = self.array_make r_1()
self.array = self.array_make r_3(311)
self.calculator ()

def binary(self, n, size): ## This is the Int -> str(BINARY)
converter
assert n >= 0
bits = []
while n:
bits.append('01 '[n&1])
n >>= 1
bits.reverse()
result = ''.join(bits) or '0'
for iteration in range(len(resul t),size):
result = "0" + result
return result

def array_maker_1(s elf): # This makes the array that represents the
8 different permutations of 3 cells. Itself, its left and its right.
return [self.binary(n, 3) for n in range(8)]

def array_maker_2(s elf): # This makes the array that represents
every single different rule. If for instance the second element in one
# of these rules is 1, then the corresponding permutation that may
be found in the result array (array_maker_3) , will be 1 (black).
return [self.binary(n, 8) for n in range(256)]

def array_maker_3(s elf, y): # This is the array for all the
results. The automaton starts from the middle of the first row
x = [["0" for x in range((2*y)+1)] for n in range(y)]
x[0][(2*y+1)/2] = "1"
return x

def calculator(self ): # This cycles over all of the cells, and
scans one row at a time, and changes the next row according to the
current cell.
self.buff_resul t = ["0","0","0"] # This is the current permutation
buffer to be checked against the corresponding arrays.
for i in range(len(self. array)-1):
for j in range(1, len(self.array[0])-1):
self.step1(j,i)
self.step2(j,i)
self.step3(j,i)
y = self.check.inde x(''.join(self. buff_result))
self.array[i+1][j] = self.against[y]

# The steps update the result buffer.
def step1(self, step, y):
self.buff_resul t[0] = self.array[y][step-1]

def step2(self, step, y):
self.buff_resul t[1] = self.array[y][step]

def step3(self, step, y):
self.buff_resul t[2] = self.array[y][step+1]

for number in range(256):
objo = calculations(nu mber)
x = objo.array
y = []
for num,zo in enumerate(x):
for com,wo in enumerate(zo):
x[num][com] = int(wo)

nim = Image.new("1", (623,311))

for n in x: #converting the array of arrays into a single array so
putdata can take it.
for p in n:
y.append(p)
nim.putdata(y)
nim.resize((623 0/2,3110/2)).save("outpu t" + str(number) + ".png")
print number
</code>

Jun 24 '06 #1
5 2136

defcon8 wrote:
I thought people would be interested in this little script I wrote to
reproduce the 256 simple automata that is shown in the first chapters
of "A New Kind of Science". You can see a few results without running
the script, at http://cooper-j.blogspot.com . And here is the code (You
will need PIL (Python Imaging Library)):
<code>
import Image

# Contract:
# To simulate simple cellular automata.

class calculations:
def __init__(self,w hich):
self.against = self.array_make r_2()[which]
self.check = self.array_make r_1()
self.array = self.array_make r_3(311)
self.calculator ()

def binary(self, n, size): ## This is the Int -> str(BINARY)
converter
assert n >= 0
bits = []
while n:
bits.append('01 '[n&1])
n >>= 1
bits.reverse()
result = ''.join(bits) or '0'
for iteration in range(len(resul t),size):
result = "0" + result
return result

def array_maker_1(s elf): # This makes the array that represents the
8 different permutations of 3 cells. Itself, its left and its right.
return [self.binary(n, 3) for n in range(8)]

def array_maker_2(s elf): # This makes the array that represents
every single different rule. If for instance the second element in one
# of these rules is 1, then the corresponding permutation that may
be found in the result array (array_maker_3) , will be 1 (black).
return [self.binary(n, 8) for n in range(256)]

def array_maker_3(s elf, y): # This is the array for all the
results. The automaton starts from the middle of the first row
x = [["0" for x in range((2*y)+1)] for n in range(y)]
x[0][(2*y+1)/2] = "1"
return x

def calculator(self ): # This cycles over all of the cells, and
scans one row at a time, and changes the next row according to the
current cell.
self.buff_resul t = ["0","0","0"] # This is the current permutation
buffer to be checked against the corresponding arrays.
for i in range(len(self. array)-1):
for j in range(1, len(self.array[0])-1):
self.step1(j,i)
self.step2(j,i)
self.step3(j,i)
y = self.check.inde x(''.join(self. buff_result))
self.array[i+1][j] = self.against[y]

# The steps update the result buffer.
def step1(self, step, y):
self.buff_resul t[0] = self.array[y][step-1]

def step2(self, step, y):
self.buff_resul t[1] = self.array[y][step]

def step3(self, step, y):
self.buff_resul t[2] = self.array[y][step+1]

for number in range(256):
objo = calculations(nu mber)
x = objo.array
y = []
for num,zo in enumerate(x):
for com,wo in enumerate(zo):
x[num][com] = int(wo)

nim = Image.new("1", (623,311))

for n in x: #converting the array of arrays into a single array so
putdata can take it.
for p in n:
y.append(p)
nim.putdata(y)
nim.resize((623 0/2,3110/2)).save("outpu t" + str(number) + ".png")
print number
</code>


Ah, celular automata! Another of my on-off interest! I shall run this
as soon as posible.

Jun 24 '06 #2
Sorry about the code. It seems to have been truncated. I have it hosted
at

http://xahlee.org/x/realautomata.py

Thanks to Xah Lee.

Jun 24 '06 #3
Few coding suggestions:
- Don't mix spaces and tabs;
- Don't write line (comments) too much long;
- Don't post too much code here;
- For this program maybe Pygame is more fit (to show the images in real
time) instead of PIL;
- Maybe Psyco can help speed up this program;
- Maybe ShedSkin will support part of the cimg library, such kind of
programs is fit for it :-)

Bye,
bearophile

Jun 24 '06 #4

"defcon8" <de*****@gmail. com> wrote in message
news:11******** **************@ y41g2000cwy.goo glegroups.com.. .
I thought people would be interested in this little script I wrote to
reproduce the 256 simple automata that is shown in the first chapters
of "A New Kind of Science". You can see a few results without running
the script, at http://cooper-j.blogspot.com


404 not found. typo?


Jun 24 '06 #5
blog-of-justin.blogspot .com

Sorry for the error.

Jun 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1743
by: Hugh Macdonald | last post by:
I've got a slight problem... and I'm stuck as to where to go with it... I'm running on Redhat7.2, using Python 2.2.2 I've got a compiled module that I wrote almost a year ago - it works fine, and I've never had any problems with it... I also did an extension to a plugin (Shake) using Python, so the plugin loads a python module and calls various functions in there.... all in all, fine - I thought I had my head around the system, and...
17
4015
by: los | last post by:
Hi, I'm trying to create a program similar to that of Google's desktop that will crawl through the hard drive and index files. I have written the program and as of now I just put the thread to sleep for 1 second after indexing a couple of files. I'm wondering if anyone knows of a way that I could make so that the program will run at full speed only runs after the computer has been idle for a while. I've looked at the "nice" command...
0
1163
by: randall_burns | last post by:
I'd like to find a web framework that works with IIS and SQL Server on Windows(I know-but I didn't make that decision). Anyhow, I've looked at Turbogears, Django, subway and didn't see any evidence that anyone had made these work in that configuration. Any suggestions?
1
1268
by: walterbyrd | last post by:
As I understand it, django works with fcgi, but it's a pain to setup. Are there any python MVCs that are optimized to work without mod_python. Or any other module that isn't likely to be loaded by standard python hosters?
7
2874
by: defcon8 | last post by:
Hello. I have recently been experimenting with cellular automata and I would like to know how I could convert a 2d list of 0's and 1's into white and black squares on an image. I have tried to install matplotlib and also NumTut but both to no avail. There seem to be bugs in their installation and I have not been able to figure out how to resolve them. I would be happy for someone to suggest a library and maybe give a simple example of how...
2
1583
by: John Nagle | last post by:
(Was prevously posted as a followup to something else by accident.) I'm running a website page through BeautifulSoup. It parses OK with Python 2.4, but Python 2.5 fails with an exception: Traceback (most recent call last): File "./sitetruth/InfoSitePage.py", line 268, in httpfetch self.pagetree = BeautifulSoup.BeautifulSoup(sitetext) # parse into tree form File "./sitetruth/BeautifulSoup.py", line 1326, in __init__...
2
2264
by: vmars956 | last post by:
Greetings; I am hoping to find a 'Python GUI that works on both Win32-XP and Mac OSX 10.5' . Is there such an animal? For instance, is there a mxPython (for mac) that corresponds to wvPython on Windows? Thanks in advance! ...Vernon
6
3455
by: HypeBeast McStreetwear | last post by:
Hi, I've been doin a project for my C++ class pertaining to Cellular Automata. I'm having a little trouble so before I start I'll give you guys all the details so what I know, you'll know. In a nutshell I know I have to start with an array in "Generation 0" and the code essentially will create a copy but I don't know how to start. Can you guys help me? I mostly just need a template to create the 2D array and a template to pass in a...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.