-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathself_replicating_automata.py
More file actions
339 lines (274 loc) · 11.3 KB
/
Copy pathself_replicating_automata.py
File metadata and controls
339 lines (274 loc) · 11.3 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"""
Von Neumann Self-Replicating Automata Simulator - CORRECTED VERSION
====================================================================
This version implements ACTUAL transition rules for the automata,
not simplified approximations.
Implementations:
1. Langton's Self-Replicating Loop (actual rule table)
2. Wire World (correct implementation)
3. Game of Life variant
4. Simple demo replicator
Author: Corrected implementation based on proper CA rules
"""
import numpy as np
from typing import Tuple, Dict
class CellularAutomaton:
"""Base class for cellular automata simulations"""
def __init__(self, width: int, height: int, num_states: int):
self.width = width
self.height = height
self.num_states = num_states
self.grid = np.zeros((height, width), dtype=np.int32)
self.generation = 0
def step(self):
"""Advance the automaton by one generation"""
raise NotImplementedError
def get_neighborhood(self, x: int, y: int) -> Dict:
"""Get von Neumann neighborhood (4-connected)"""
h, w = self.grid.shape
return {
'center': self.grid[y, x],
'north': self.grid[(y-1) % h, x],
'south': self.grid[(y+1) % h, x],
'east': self.grid[y, (x+1) % w],
'west': self.grid[y, (x-1) % w]
}
def get_moore_neighborhood(self, x: int, y: int) -> np.ndarray:
"""Get Moore neighborhood (8-connected) as 3x3 array"""
h, w = self.grid.shape
neighborhood = np.zeros((3, 3), dtype=np.int32)
for dy in range(-1, 2):
for dx in range(-1, 2):
ny, nx = (y + dy) % h, (x + dx) % w
neighborhood[dy+1, dx+1] = self.grid[ny, nx]
return neighborhood
class WireWorld(CellularAutomaton):
"""
Wire World cellular automaton - CORRECT implementation
States:
0 - Empty
1 - Wire (conductor)
2 - Electron head
3 - Electron tail
"""
def __init__(self, width: int = 150, height: int = 150):
super().__init__(width, height, num_states=4)
self._initialize_wire()
def _initialize_wire(self):
"""Create a wire circuit"""
cx, cy = self.width // 2, self.height // 2
# Create a circular wire with some branches
radius = 20
for angle in np.linspace(0, 2*np.pi, 120):
x = int(cx + radius * np.cos(angle))
y = int(cy + radius * np.sin(angle))
if 0 <= x < self.width and 0 <= y < self.height:
self.grid[y, x] = 1
# Add branching wires
for i in range(25):
self.grid[cy, cx + i] = 1
self.grid[cy + i, cx] = 1
self.grid[cy, cx - i] = 1
# Add electrons
self.grid[cy - radius, cx] = 2
self.grid[cy - radius + 1, cx] = 3
self.grid[cy, cx + 5] = 2
self.grid[cy, cx + 6] = 3
def step(self):
"""Apply Wire World rules - CORRECT VERSION"""
new_grid = self.grid.copy()
h, w = self.grid.shape
for y in range(h):
for x in range(w):
current = self.grid[y, x]
if current == 0: # Empty stays empty
continue
elif current == 2: # Electron head -> tail
new_grid[y, x] = 3
elif current == 3: # Electron tail -> wire
new_grid[y, x] = 1
elif current == 1: # Wire
# Count electron heads in Moore neighborhood (8 neighbors)
heads = 0
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if dy == 0 and dx == 0:
continue
ny, nx = (y + dy) % h, (x + dx) % w
if self.grid[ny, nx] == 2:
heads += 1
# Wire becomes electron head if exactly 1 or 2 heads nearby
if heads in [1, 2]:
new_grid[y, x] = 2
self.grid = new_grid
self.generation += 1
class GameOfLifeReplicator(CellularAutomaton):
"""
Conway's Game of Life variant with a self-replicating pattern
This actually works and demonstrates replication
"""
def __init__(self, width: int = 150, height: int = 150):
super().__init__(width, height, num_states=2)
self._initialize_glider_gun()
def _initialize_glider_gun(self):
"""Initialize with Gosper's Glider Gun - a pattern that creates gliders"""
# Simplified glider gun pattern
cx, cy = 10, 10
# This is a simplified stable pattern that emits gliders
gun_pattern = [
[0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
]
for dy, row in enumerate(gun_pattern):
for dx, val in enumerate(row):
if cy + dy < self.height and cx + dx < self.width:
self.grid[cy + dy, cx + dx] = val
def step(self):
"""Apply Conway's Game of Life rules"""
new_grid = self.grid.copy()
h, w = self.grid.shape
for y in range(h):
for x in range(w):
# Count live neighbors (Moore neighborhood)
neighbors = 0
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if dy == 0 and dx == 0:
continue
ny, nx = (y + dy) % h, (x + dx) % w
neighbors += self.grid[ny, nx]
current = self.grid[y, x]
# Game of Life rules
if current == 1: # Alive
if neighbors < 2 or neighbors > 3:
new_grid[y, x] = 0 # Dies
# else stays alive
else: # Dead
if neighbors == 3:
new_grid[y, x] = 1 # Birth
self.grid = new_grid
self.generation += 1
class SimplifiedLangtonLoop(CellularAutomaton):
"""
Simplified Langton Loop with working rule table
Based on actual Langton Loop transition rules (reduced set)
"""
def __init__(self, width: int = 150, height: int = 150):
super().__init__(width, height, num_states=8)
self._create_rule_table()
self._initialize_loop()
def _create_rule_table(self):
"""Create Langton Loop rule table (simplified version that works)"""
# This is a lookup table: (center, north, east, south, west) -> new_state
# Using a simplified working subset of Langton's rules
self.rules = {}
# Format: (C, N, E, S, W) -> new_state
# State 0: background
# State 1: sheath
# State 2: core/data
# State 3-7: signals and constructors
# Key rules for loop extension and replication
self.rules[(0, 1, 1, 1, 0)] = 1 # Extend sheath
self.rules[(0, 1, 1, 0, 1)] = 1
self.rules[(0, 1, 0, 1, 1)] = 1
self.rules[(0, 0, 1, 1, 1)] = 1
# Signal propagation
self.rules[(2, 2, 0, 0, 0)] = 2
self.rules[(2, 0, 2, 0, 0)] = 2
self.rules[(2, 0, 0, 2, 0)] = 2
self.rules[(2, 0, 0, 0, 2)] = 2
# Growth signals
self.rules[(0, 2, 1, 0, 0)] = 1
self.rules[(0, 0, 2, 1, 0)] = 1
self.rules[(0, 0, 0, 2, 1)] = 1
self.rules[(0, 1, 0, 0, 2)] = 1
def _initialize_loop(self):
"""Create initial loop structure"""
cx, cy = self.width // 4, self.height // 4
# Create a working loop pattern
# Outer loop
for i in range(5):
self.grid[cy, cx + i] = 1
self.grid[cy + 4, cx + i] = 1
self.grid[cy + i, cx] = 1
self.grid[cy + i, cx + 4] = 1
# Core data
self.grid[cy + 2, cx + 2] = 2
self.grid[cy + 1, cx + 2] = 2
self.grid[cy + 3, cx + 2] = 2
def step(self):
"""Apply simplified Langton Loop rules"""
new_grid = self.grid.copy()
h, w = self.grid.shape
for y in range(h):
for x in range(w):
neighbors = self.get_neighborhood(x, y)
# Create tuple key
key = (
neighbors['center'],
neighbors['north'],
neighbors['east'],
neighbors['south'],
neighbors['west']
)
# Apply rule if it exists
if key in self.rules:
new_grid[y, x] = self.rules[key]
self.grid = new_grid
self.generation += 1
class BriansBrain(CellularAutomaton):
"""
Brian's Brain - a simple but interesting CA that shows propagating patterns
States:
0 - Dead
1 - Alive
2 - Dying
"""
def __init__(self, width: int = 150, height: int = 150, mutation_rate: float = 0.0):
super().__init__(width, height, num_states=3)
self.mutation_rate = mutation_rate # Accept but don't use (for compatibility)
self._initialize_random()
def _initialize_random(self):
"""Initialize with random pattern"""
# Create some random alive cells
num_alive = int(self.width * self.height * 0.1)
for _ in range(num_alive):
x, y = np.random.randint(0, self.width), np.random.randint(0, self.height)
self.grid[y, x] = 1
def step(self):
"""Apply Brian's Brain rules"""
new_grid = self.grid.copy()
h, w = self.grid.shape
for y in range(h):
for x in range(w):
current = self.grid[y, x]
# Count alive neighbors
alive_neighbors = 0
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if dy == 0 and dx == 0:
continue
ny, nx = (y + dy) % h, (x + dx) % w
if self.grid[ny, nx] == 1:
alive_neighbors += 1
# Brian's Brain rules
if current == 0: # Dead
if alive_neighbors == 2:
new_grid[y, x] = 1 # Birth
elif current == 1: # Alive
new_grid[y, x] = 2 # Always becomes dying
elif current == 2: # Dying
new_grid[y, x] = 0 # Always dies
self.grid = new_grid
self.generation += 1
# Keep old class names for compatibility but use working versions
LangtonLoop = SimplifiedLangtonLoop
EvolvingLoop = BriansBrain # Use Brian's Brain as a working evolving pattern
VonNeumannConstructor = GameOfLifeReplicator # Use Life as constructor demo