Skip to content

Commit 276b309

Browse files
committed
Generalize Elementary CA and Life
1 parent ca301b3 commit 276b309

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+341
-143
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Designed for speed and flexibility, CAX allows you to easily experiment with sel
2222

2323
## Why CAX? 💡
2424

25-
CAX supports discrete and continuous models, including neural cellular automata, across any number of dimensions. Beyond traditional cellular automata, it also handles particle systems and more, all unified under a single, intuitive API.
25+
CAX supports discrete and continuous systems, including neural cellular automata, across any number of dimensions. Beyond traditional cellular automata, it also handles particle systems and more, all unified under a single, intuitive API.
2626

2727
### Rich 🎨
2828

2929
CAX provides a comprehensive collection of 15+ ready-to-use systems. From simulating one-dimensional [elementary cellular automata](examples/10_elementary_ca.ipynb) to training three-dimensional [self-autoencoding neural cellular automata](examples/45_self_autoencoding_mnist.ipynb), or even creating beautiful [Lenia](examples/20_lenia.ipynb) simulations, CAX provides a versatile platform for exploring the rich world of self-organizing systems.
3030

3131
### Flexible 🧩
3232

33-
CAX makes it easy to extend existing models or build custom ones from scratch for endless experimentation and discovery. Design your own experiments to probe the boundaries of artificial open-ended evolution and emergent complexity.
33+
CAX makes it easy to extend existing systems or build custom ones from scratch for endless experimentation and discovery. Design your own experiments to probe the boundaries of artificial open-ended evolution and emergent complexity.
3434

3535
### Fast 🚀
3636

@@ -40,7 +40,7 @@ CAX is built on top of the JAX/Flax ecosystem for speed and scalability. The lib
4040

4141
The library is thoroughly tested and documented with numerous examples to get you started! Our comprehensive guides walk you through everything from basic cellular automata to advanced neural implementations.
4242

43-
## Implemented Models 🦎
43+
## Implemented Systems 🦎
4444

4545
| Cellular Automata | Reference | Example |
4646
| --- | --- | --- |

examples/00_getting_started.ipynb

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
},
158158
{
159159
"cell_type": "code",
160-
"execution_count": null,
160+
"execution_count": 1,
161161
"metadata": {},
162162
"outputs": [],
163163
"source": [
@@ -178,7 +178,7 @@
178178
"cell_type": "markdown",
179179
"metadata": {},
180180
"source": [
181-
"In this section, we'll demonstrate the basic usage of CAX with pre-implemented cellular automata. We'll instantiate Conway's Game of Life and visualize a glider pattern, showing how easily you can get started with existing models in the library."
181+
"In this section, we'll demonstrate the basic usage of CAX with pre-implemented cellular automata. We'll instantiate Conway's Game of Life and visualize a glider pattern, showing how easily you can get started with existing systems in the library."
182182
]
183183
},
184184
{
@@ -215,7 +215,7 @@
215215
"cell_type": "markdown",
216216
"metadata": {},
217217
"source": [
218-
"### Model"
218+
"### System"
219219
]
220220
},
221221
{
@@ -231,11 +231,20 @@
231231
"metadata": {},
232232
"outputs": [],
233233
"source": [
234-
"from cax.models.life import Life\n",
234+
"from cax.systems.life import Life\n",
235235
"\n",
236236
"ca = Life(rngs=rngs)"
237237
]
238238
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": null,
242+
"metadata": {},
243+
"outputs": [],
244+
"source": [
245+
"ca.update.update_birth_survival_from_string(\"B3/S23\")"
246+
]
247+
},
239248
{
240249
"cell_type": "markdown",
241250
"metadata": {},
@@ -247,7 +256,7 @@
247256
"cell_type": "markdown",
248257
"metadata": {},
249258
"source": [
250-
"Then, we define a function to sample an initial state, which is essential for running a model."
259+
"Then, we define a function to sample an initial state, which is essential for running a system."
251260
]
252261
},
253262
{
@@ -282,7 +291,7 @@
282291
"cell_type": "markdown",
283292
"metadata": {},
284293
"source": [
285-
"Given an initial state and the model, we can simulate for `num_steps`."
294+
"Given an initial state and the system, we can simulate for `num_steps`."
286295
]
287296
},
288297
{
@@ -308,7 +317,7 @@
308317
"source": [
309318
"Finally, we can visualize the trajectory of states.\n",
310319
"\n",
311-
"All models should include a `render` method to convert a state into an RGB frame. For the Game of Life, a ready-to-use `render` method is provided, allowing you to easily generate a frame with a simple call: `frame = ca.render(state)`.\n",
320+
"All systems should include a `render` method to convert a state into an RGB frame. For the Game of Life, a ready-to-use `render` method is provided, allowing you to easily generate a frame with a simple call: `frame = ca.render(state)`.\n",
312321
"\n",
313322
"Enjoy! 👾"
314323
]
@@ -349,9 +358,9 @@
349358
"cell_type": "markdown",
350359
"metadata": {},
351360
"source": [
352-
"While visualizing the trajectory of states can produce captivating simulations, we often also want to track additional metrics to better understand how the model evolves over time.\n",
361+
"While visualizing the trajectory of states can produce captivating simulations, we often also want to track additional metrics to better understand how the system evolves over time.\n",
353362
"\n",
354-
"By default, the metrics function in CAX returns the states encountered during the model’s rollout. However, this behavior is highly customizable. In this section, we’ll explore how to create a tailored metrics function to log custom metrics suited to your needs.\n",
363+
"By default, the metrics function in CAX returns the states encountered during the system’s rollout. However, this behavior is highly customizable. In this section, we’ll explore how to create a tailored metrics function to log custom metrics suited to your needs.\n",
355364
"\n",
356365
"A metrics function must accept the next state, the current state, the perception, and the input as its parameters. For the Game of Life, however, the input parameter is not utilized, and can safely be ignored."
357366
]
@@ -388,6 +397,15 @@
388397
"ca = Life(rngs=rngs, metrics_fn=custom_metrics_fn)"
389398
]
390399
},
400+
{
401+
"cell_type": "code",
402+
"execution_count": null,
403+
"metadata": {},
404+
"outputs": [],
405+
"source": [
406+
"ca.update.update_birth_survival_from_string(\"B3/S23\")"
407+
]
408+
},
391409
{
392410
"cell_type": "markdown",
393411
"metadata": {},
@@ -410,7 +428,7 @@
410428
"cell_type": "markdown",
411429
"metadata": {},
412430
"source": [
413-
"Let's run the model:"
431+
"Let's run the system:"
414432
]
415433
},
416434
{
@@ -510,7 +528,7 @@
510528
"cell_type": "markdown",
511529
"metadata": {},
512530
"source": [
513-
"When using `nnx.Module`s with rngs in the state, we need to use `nnx.split_rngs` to properly vectorize over rngs state across parallel operations. For Conway's Game of Life specifically, the model doesn't use randomness during execution, so we could use `nnx.vmap` directly. However, we'll demonstrate the more general approach with `nnx.split_rngs` below, which works for any model that maintains rngs state."
531+
"When using `nnx.Module`s with rngs in the state, we need to use `nnx.split_rngs` to properly vectorize over rngs state across parallel operations. For Conway's Game of Life specifically, the system doesn't use randomness during execution, so we could use `nnx.vmap` directly. However, we'll demonstrate the more general approach with `nnx.split_rngs` below, which works for any systems that maintain rngs state."
514532
]
515533
},
516534
{
@@ -614,7 +632,7 @@
614632
"cell_type": "markdown",
615633
"metadata": {},
616634
"source": [
617-
"CAX provides a set of perceive models. In this notebook, we will use a simple convolution perceive module."
635+
"CAX provides a set of perceive modules. In this notebook, we will use a simple convolution perceive module."
618636
]
619637
},
620638
{
@@ -643,7 +661,7 @@
643661
"cell_type": "markdown",
644662
"metadata": {},
645663
"source": [
646-
"CAX provides a set of update models. In this notebook, we will use a residual MLP update module."
664+
"CAX provides a set of update modules. In this notebook, we will use a residual MLP update module."
647665
]
648666
},
649667
{
@@ -685,8 +703,8 @@
685703
"metadata": {},
686704
"outputs": [],
687705
"source": [
688-
"from cax.core.ca import CA\n",
689-
"from cax.utils.render import clip_and_uint8, rgba_to_rgb\n",
706+
"from cax.core import CA\n",
707+
"from cax.utils import clip_and_uint8, rgba_to_rgb\n",
690708
"\n",
691709
"\n",
692710
"class MyCustomCA(CA):\n",
@@ -856,7 +874,7 @@
856874
"name": "python",
857875
"nbconvert_exporter": "python",
858876
"pygments_lexer": "ipython3",
859-
"version": "3.12.8"
877+
"version": "3.13.3"
860878
}
861879
},
862880
"nbformat": 4,

examples/10_elementary_ca.ipynb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@
5555
},
5656
{
5757
"cell_type": "code",
58-
"execution_count": null,
58+
"execution_count": 1,
5959
"metadata": {},
6060
"outputs": [],
6161
"source": [
6262
"import jax.numpy as jnp\n",
6363
"import mediapy\n",
6464
"from flax import nnx\n",
6565
"\n",
66-
"from cax.models.elementary import ElementaryCA"
66+
"from cax.systems.elementary import ElementaryCA"
6767
]
6868
},
6969
{
@@ -102,7 +102,16 @@
102102
"metadata": {},
103103
"outputs": [],
104104
"source": [
105-
"ca = ElementaryCA(rngs=rngs, wolfram_code=wolfram_code)"
105+
"ca = ElementaryCA(rngs=rngs)"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 6,
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"ca.update.update_wolfram_code_from_string(wolfram_code)"
106115
]
107116
},
108117
{
@@ -114,7 +123,7 @@
114123
},
115124
{
116125
"cell_type": "code",
117-
"execution_count": 4,
126+
"execution_count": 7,
118127
"metadata": {},
119128
"outputs": [],
120129
"source": [
@@ -150,7 +159,7 @@
150159
},
151160
{
152161
"cell_type": "code",
153-
"execution_count": 7,
162+
"execution_count": 9,
154163
"metadata": {},
155164
"outputs": [
156165
{
@@ -190,7 +199,7 @@
190199
"name": "python",
191200
"nbconvert_exporter": "python",
192201
"pygments_lexer": "ipython3",
193-
"version": "3.12.8"
202+
"version": "3.13.3"
194203
}
195204
},
196205
"nbformat": 4,

0 commit comments

Comments
 (0)