import numpy as np
import matplotlib.pyplot as plt
def apply_rule(rule, state, pos):
= state[pos - 1]
left = state[pos]
center = state[(pos + 1) % len(state)]
right = rule[left * 4 + center * 2 + right]
out return out
def evolve(rule, initial_state, steps):
= initial_state.copy()
state = [state.copy()]
history
for _ in range(steps):
= np.zeros_like(state)
new_state for i in range(len(state)):
= apply_rule(rule, state, i)
new_state[i] = new_state
state
history.append(state.copy()) return history
def plot_evolution(history):
=(10, 5))
plt.figure(figsize="magma", interpolation="nearest")
plt.imshow(history, cmap"Time Step")
plt.xlabel("Cell")
plt.ylabel("Rule 30 Cellular Automaton Evolution")
plt.title(
plt.show()
# Define Rule 30
= np.array([0, 1, 1, 1, 1, 0, 0, 0], dtype=int)
rule30
# Set initial state
= np.zeros(101, dtype=int)
initial_state 50] = 1
initial_state[
# Evolve and plot
= 50
steps = evolve(rule30, initial_state, steps)
evolution_history plot_evolution(np.array(evolution_history))
Loose Entries
biology