Skip to content

Create one_dimensional.py #1905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 27, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions cellular_automata/one_dimensional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'''
Returns an image of 16 generations of
one-dimensional cellular automata
based on a given ruleset number
https://mathworld.wolfram.com/ElementaryCellularAutomaton.html
'''
# Formats inputted ruleset
def format_ruleset(RULE_NUM):
'''
>>> format_ruleset(11100)
[0,0,0,1,1,1,0,0]
>>> format_ruleset(0)
[0,0,0,0,0,0,0,0]
>>> format_ruleset(11111111)
[1,1,1,1,1,1,1,1]
'''
RULE = [int(a) for a in RULE_NUM]
while True:
if len(RULE) == 8:
break
else:
RULE.insert(0, 0)
#returns formatted ruleset
return(RULE)

# Defines the first generation of cells

CELLS = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,]]

# Mainloop

def new_generation(CELLS,RULE,time):
new_row = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add type hints and doctests.


for i in range(31):

# Gets neighbors of a cell

# If leftmost cell is in consideration

if i == 0:
left_neighbor = 0
right_neighbor = CELLS[time][i + 1]
elif i == 30:

# If rightmost cell is in consideration

left_neighbor = CELLS[time][i - 1]
right_neighbor = 0
else:

# All other cells

left_neighbor = CELLS[time][i - 1]
right_neighbor = CELLS[time][i + 1]

# Defines new cell in and adds it to the new generation

SITUATION = 7 - int(str(left_neighbor) + str(CELLS[time][i])
+ str(right_neighbor), 2)
new_row.append(RULE[SITUATION])
#returns new generation
return new_row

def generate_image(CELLS):
# Creates the outputting image

RESULT = Image.new('RGB', (31, 16))
PIXELS = RESULT.load()

# Generates image
for w in range(31):
for h in range(16):
color = 255 - 255 * CELLS[h][w]
PIXELS[w, h] = (color, color, color)

# Uncomment for saving the image
# RESULT.save('RULE '+str(RULE_NUM))

# Shows the image
RESULT.show()

if __name__ == '__main__':
from PIL import Image

RULE_NUM = bin(int(input('Rule\n')))[2:]
RULE = format_ruleset(RULE_NUM)

for time in range(16):
CELLS.append(new_generation(CELLS,RULE,time))

generate_image(CELLS)