-
Notifications
You must be signed in to change notification settings - Fork 0
/
elementary_CA.py
76 lines (59 loc) · 2.17 KB
/
elementary_CA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import matplotlib.pyplot as plt
def apply_rule(rule, iterations):
if rule > 255 or rule < 0:
print("No such rule.")
return
binary = format(rule, "0>8b") # convert rule to binary
binary = (list(map(int,binary))) # convert binary num to list of int
patterns = [ # list of patterns as tuples
(1,1,1),
(1,1,0),
(1,0,1),
(1,0,0),
(0,1,1),
(0,1,0),
(0,0,1),
(0,0,0)
]
pattern_rules_dict = dict(zip(patterns, binary)) # assigns each pattern its respective value from the binary num
row_length = iterations * 2 - 1
first_row_start_index = int(row_length / 2 + 1) # index of the first block
initial_row = [0] * (row_length + 2) # board consists of 0s initially, need 2 extra 0s to determine next row
initial_row[first_row_start_index] = 1 # set first block
board = [] # create matrix
board.append(initial_row)
curr_row = initial_row
for i in range(0, iterations-1):
next_row = [0] * row_length # create next row with initially all 0s
stop_index = len(curr_row) - 2
# Get pattern to create next row
for j in range(0, len(curr_row)):
if(j < stop_index): # should stop when there are less than 3 values left in the board
pattern_tuple = tuple(curr_row[j:j+3]) # get patterns to create next row
value = pattern_rules_dict[pattern_tuple]
next_row[j] = value
board.append(next_row)
curr_row = next_row
curr_row.insert(0, 0)
curr_row.append(0)
return board
def show_board(board, rule):
plt.figure(figsize=(10,5), facecolor="pink")
plt.imshow(board, cmap="Greys")
plt.axis("off")
plt.title(rule, fontsize=20)
plt.show()
plt.close()
def run_all_rules(iterations):
for i in range(0, 256):
board = apply_rule(i, iterations)
show_board(board, i)
# Instructions to Run
# 1. Set number of iterations
iterations = 100
# 2. Run 1 rule or all of them
# 2a. Run 1 rule - Set rule & uncomment show_board()
rule = 62
show_board(apply_rule(rule, iterations), rule)
# 2b. uncomment run_all_rules()
# run_all_rules(iterations)