Boids
cax.cs.boids.cs.Boids
Bases: ComplexSystem
Boids class.
Source code in src/cax/cs/boids/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 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 | |
__init__(*, dt=0.01, velocity_half_life=jnp.inf, boid_policy)
Initialize Boids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
float
|
Time step of the simulation in arbitrary time units. Smaller values produce smoother motion but require more steps for the same duration. |
0.01
|
velocity_half_life
|
float
|
Time constant for velocity decay due to friction. After this time, velocity is halved without steering input. Use jnp.inf for no friction. Smaller values create more damped, sluggish motion. |
inf
|
boid_policy
|
BoidPolicy
|
Policy defining the behavior of the boids. |
required |
Source code in src/cax/cs/boids/cs.py
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 | |
render(state, *, resolution=512, boids_size=0.01)
Render state to RGB image.
Renders boids as triangular agents pointing in their direction of motion on a white background. Each boid is drawn as a filled triangle with the tip pointing in the velocity direction, providing visual feedback about both position and heading. The visualization uses 2D coordinates in the range [0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
BoidsState
|
BoidsState containing position and velocity arrays. Position should have shape (num_boids, 2) with coordinates in [0, 1]. Velocity determines the orientation and can have arbitrary magnitude. |
required |
resolution
|
int
|
Size of the output image in pixels for both width and height. Higher values produce smoother, more detailed renderings. |
512
|
boids_size
|
float
|
Base width of each boid triangle in coordinate space [0, 1]. The triangle height is twice this value. Larger values make boids more visible but may cause overlap. |
0.01
|
Returns:
| Type | Description |
|---|---|
Array
|
RGB image with dtype uint8 and shape (resolution, resolution, 3), where boids appear as black triangles on a white background. |
Source code in src/cax/cs/boids/cs.py
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 | |
__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.
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
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 | |