Langton's Ant
cax.cs.langton_ant.cs.LangtonAnt
Bases: ComplexSystem[LangtonAntState, Array]
Langton's Ant and multi-color generalizations.
A two-dimensional cellular automaton with a mobile agent that traverses a grid of colored cells. At each step the ant turns according to the color of its current cell, advances the cell to the next color in a cyclic sequence, and moves forward one step. The rule is specified as a sequence of turn directions — one per color — encoded as a string over the alphabet {R, L, N, U}.
Classic examples include "RL" (Langton's original ant, produces a highway), "LLRR" (symmetric growth filling a square), and "LRRRRRLLR" (triangle-building ant).
Source code in src/cax/cs/langton_ant/cs.py
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 | |
__init__(*, turns, rngs)
Initialize Langton's Ant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
turns
|
Array
|
Array of shape (num_colors,) with integer values in {0, 1, 2, 3} encoding turn directions for each cell color. Values correspond to clockwise rotation in multiples of 90 degrees: 0=no turn, 1=right, 2=u-turn, 3=left. |
required |
rngs
|
Rngs
|
rng key. |
required |
Source code in src/cax/cs/langton_ant/cs.py
37 38 39 40 41 42 43 44 45 46 47 48 49 | |
turns_from_rule_string(rule_string)
classmethod
Create turns array from a rule string.
Parses a rule string in the standard Langton's Ant notation where each character specifies the turn direction for the corresponding cell color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule_string
|
str
|
String of characters from {R, L, N, U}. For example, "RL" for classic Langton's Ant. The length of the string determines the number of cell colors. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape (num_colors,) with integer turn values where 0=no turn, 1=right (90 degrees clockwise), 2=u-turn (180 degrees), 3=left (90 degrees counter-clockwise). |
Source code in src/cax/cs/langton_ant/cs.py
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 | |
render(state)
Render state to RGB image.
Visualizes the grid by mapping each cell color to a distinct hue. For 2-color rules the rendering is grayscale (black for color 0, white for color 1). For multi-color rules, color 0 (unvisited) is black and colors 1 through num_colors-1 are evenly spaced around the HSV hue wheel at full saturation and value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
LangtonAntState
|
Langton's Ant state with grid of shape (height, width, 1). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
RGB image with dtype uint8 and shape (height, width, 3). |
Source code in src/cax/cs/langton_ant/cs.py
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 | |
__call__(state, input=None, *, num_steps=1, input_in_axis=None, sow=False)
Step the system for multiple time steps.
This method wraps _step inside a JAX scan for efficiency and JIT-compiles the loop.
If input is time-varying, set input_in_axis to the axis containing the time
dimension so that each step receives the corresponding slice of input.
When remat is enabled, the scan body is wrapped with nnx.remat to reduce memory
usage during backpropagation at the cost of recomputing intermediates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Current state. |
required |
input
|
Input | None
|
Optional input. |
None
|
num_steps
|
int
|
Number of steps. |
1
|
input_in_axis
|
int | None
|
Axis for input if provided for each step. |
None
|
sow
|
bool
|
Whether to sow intermediate values. |
False
|
Returns:
| Type | Description |
|---|---|
State
|
Final state after |
Source code in src/cax/core/cs.py
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 | |