byof
Bring-Your-Own-Functions (BYOF) - Expert User Mode.
This module provides type definitions and documentation for expert users who want to bypass the symbolic layer and directly provide raw JAX functions.
Important
The unified state/control vectors include ALL states/controls in the order they were provided, plus any augmented states from CTCS constraints. You are responsible for correct indexing. Consider inspecting the symbolic problem to understand the layout.
Warning
Constraint Sign Convention: All constraints follow g(x,u) <= 0 convention.
Return negative when satisfied, positive when violated.
Example: for x <= 10 return x - 10, for x >= 5 return 5 - x.
Function Signatures
All byof functions must be JAX-compatible (use jax.numpy, avoid side effects).
-
dynamics:
(x, u, node, params) -> xdot_component- x: Full unified state vector (1D array)
- u: Full unified control vector (1D array)
- node: Integer node index
- params: Dict of parameters
- Returns: State derivative component (array matching state shape)
-
nodal_constraints:
(x, u, node, params) -> residual- Same arguments as dynamics
- Returns: Constraint residual (g <= 0: negative=satisfied, positive=violated)
-
cross_nodal_constraints:
(X, U, params) -> residual- X: State trajectory (N, n_x) where N is number of trajectory nodes, n_x is unified state dimension
- U: Control trajectory (N, n_u) where N is number of trajectory nodes, n_u is unified control dimension
- params: Dict of parameters
- Returns: Constraint residual (g <= 0: negative=satisfied, positive=violated)
-
ctcs constraint_fn:
(x, u, node, params) -> scalar- Same as nodal_constraints but MUST return scalar
- Returns: Constraint residual (g <= 0: negative=satisfied, positive=violated)
-
ctcs penalty:
(residual) -> penalty_value- residual: Scalar constraint residual
- Returns: Non-negative penalty value
Example
Basic usage mixing symbolic and byof::
import jax.numpy as jnp
import openscvx as ox
from openscvx import ByofSpec
# Define states
position = ox.State("position", shape=(2,))
velocity = ox.State("velocity", shape=(1,))
theta = ox.Control("theta", shape=(1,))
# Unified state: [position[0], position[1], velocity[0], time, augmented...]
# Unified control: [theta[0], time_dilation]
# Tip: Use the .slice property on State/Control objects for cleaner,
# more maintainable indexing instead of hardcoded indices.
byof: ByofSpec = {
"nodal_constraints": [
# Velocity bounds (applied to all nodes)
{
"constraint_fn": lambda x, u, node, params: x[velocity.slice][0] - 10.0,
},
{
"constraint_fn": lambda x, u, node, params: -x[velocity.slice][0],
},
# Velocity must be exactly 0 at start (selective enforcement)
{
"constraint_fn": lambda x, u, node, params: x[velocity.slice][0],
"nodes": [0], # Only at first node
},
],
"ctcs_constraints": [
{
"constraint_fn": lambda x, u, node, params: x[position.slice][0] - 10.0,
"penalty": "square",
"bounds": (0.0, 1e-4),
}
],
}
problem = ox.Problem(..., byof=byof)
ByofSpec
¶
Bases: TypedDict
Bring-Your-Own-Functions specification for expert users.
Allows bypassing the symbolic layer and directly providing raw JAX functions. All fields are optional - you can mix symbolic and byof as needed.
Warning
You are responsible for:
- Correct indexing into unified state/control vectors
- Ensuring functions are JAX-compatible (use jax.numpy, no side effects)
- Ensuring functions are differentiable
- Following g(x,u) <= 0 convention for constraints
Tip
Use the .slice property on State/Control objects for cleaner, more
maintainable indexing instead of hardcoded indices. For example, use
x[velocity.slice] instead of x[2:3]. The slice property is set
after preprocessing and provides the correct indices into the unified
state/control vectors.
Attributes:
| Name | Type | Description |
|---|---|---|
dynamics |
dict[str, DynamicsFunction]
|
Raw JAX functions for state derivatives. Maps state names to functions
with signature |
nodal_constraints |
List[NodalConstraintSpec]
|
Point-wise constraints applied at specific nodes.
Each item is a :class:
Follows g(x,u) <= 0 convention. |
cross_nodal_constraints |
List[CrossNodalConstraintFunction]
|
Constraints coupling multiple nodes (smoothness, rate limits).
Signature: |
ctcs_constraints |
List[CtcsConstraintSpec]
|
Continuous-time constraint satisfaction via dynamics augmentation.
Each adds an augmented state accumulating violation penalties.
See :class: |
Example
Custom dynamics and constraints::
import jax.numpy as jnp
import openscvx as ox
from openscvx import ByofSpec
# Define states and controls
position = ox.State("position", shape=(2,))
velocity = ox.State("velocity", shape=(1,))
theta = ox.Control("theta", shape=(1,))
# Custom dynamics for one state using .slice property
def custom_velocity_dynamics(x, u, node, params):
# Use .slice property for clean indexing
return params["g"] * jnp.cos(u[theta.slice][0])
byof: ByofSpec = {
"dynamics": {
"velocity": custom_velocity_dynamics,
},
"nodal_constraints": [
# Applied to all nodes (no "nodes" field)
{
"constraint_fn": lambda x, u, node, params: x[velocity.slice][0] - 10.0,
},
{
"constraint_fn": lambda x, u, node, params: -x[velocity.slice][0],
},
# Specify nodes for selective enforcement
{
"constraint_fn": lambda x, u, node, params: x[velocity.slice][0],
"nodes": [0], # Velocity must be exactly 0 at start
},
],
"cross_nodal_constraints": [
# Constrain total velocity across trajectory: sum(velocities) >= 5
# X.shape = (N, n_x), extract velocity column using slice
lambda X, U, params: 5.0 - jnp.sum(X[:, velocity.slice]),
],
"ctcs_constraints": [
{
"constraint_fn": lambda x, u, node, params: x[position.slice][0] - 5.0,
"penalty": "square",
}
],
}
Source code in openscvx/expert/byof.py
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 | |
CtcsConstraintSpec
¶
Bases: TypedDict
Specification for CTCS (Continuous-Time Constraint Satisfaction) constraint.
CTCS constraints are enforced by augmenting the dynamics with a penalty term that accumulates violations over time. Useful for path constraints that must be satisfied continuously, not just at discrete nodes.
Attributes:
| Name | Type | Description |
|---|---|---|
constraint_fn |
CtcsConstraintFunction
|
Function computing constraint residual with signature
|
penalty |
PenaltyFunction
|
Penalty function for positive residuals (violations).
Built-in options: "square" (max(r,0)^2, default), "l1" (max(r,0)),
"huber" (Huber loss). Custom: Callable |
bounds |
Tuple[float, float]
|
(min, max) bounds for augmented state accumulating penalties. Default: (0.0, 1e-4). Max acts as soft constraint on total violation. |
initial |
float
|
Initial value for augmented state. Default: bounds[0] (usually 0.0). |
over |
Tuple[int, int]
|
Node interval (start, end) where constraint is active. The constraint
is enforced for nodes in [start, end). If omitted, constraint is active
over all nodes. Matches symbolic |
idx |
int
|
Constraint group index for sharing augmented states (default: 0). All CTCS constraints (symbolic and byof) with the same idx share a single augmented state. Their penalties are summed together. Use different idx values to track different types of violations separately. |
Warning
If symbolic CTCS constraints exist with idx values [0, 1, 2], then byof idx must either:
- Match an existing idx (e.g., 0, 1, or 2) to add to that augmented state
- Be sequential after them (e.g., 3, 4, 5) to create new augmented states
You cannot use idx values that create gaps (e.g., if symbolic has [0, 1], you cannot use byof idx=3 without also using idx=2).
Example
Enforce position[0] <= 10.0 continuously::
# Assuming position = ox.State("position", shape=(2,))
ctcs_spec: CtcsConstraintSpec = {
"constraint_fn": lambda x, u, node, params: x[position.slice][0] - 10.0,
"penalty": "square",
"bounds": (0.0, 1e-4),
"initial": 0.0,
"idx": 0, # Groups with other constraints having idx=0
}
Enforce constraint only over specific node range::
ctcs_spec: CtcsConstraintSpec = {
"constraint_fn": lambda x, u, node, params: x[position.slice][0] - 10.0,
"over": (10, 50), # Active only for nodes 10-49
"penalty": "square",
}
Multiple constraints sharing an augmented state::
# If symbolic CTCS already has idx=[0, 1], then:
byof = {
"ctcs_constraints": [
# Add to existing symbolic idx=0 augmented state
{
"constraint_fn": lambda x, u, node, params: x[pos.slice][0] - 10.0,
"idx": 0, # Shares with symbolic idx=0
},
# Add to existing symbolic idx=1 augmented state
{
"constraint_fn": lambda x, u, node, params: x[vel.slice][0] - 5.0,
"idx": 1, # Shares with symbolic idx=1
},
# Create NEW augmented state (sequential after symbolic)
{
"constraint_fn": lambda x, u, node, params: x[pos.slice][1] - 8.0,
"idx": 2, # New state (symbolic has 0,1, so next is 2)
},
]
}
Source code in openscvx/expert/byof.py
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 | |
NodalConstraintSpec
¶
Bases: TypedDict
Specification for nodal constraint with optional node selection.
Nodal constraints are point-wise constraints evaluated at specific trajectory nodes. By default, constraints apply to all nodes, but you can restrict enforcement to specific nodes for boundary conditions, waypoints, or computational efficiency.
Attributes:
| Name | Type | Description |
|---|---|---|
constraint_fn |
NodalConstraintFunction
|
Constraint function with signature |
nodes |
List[int]
|
List of integer node indices where constraint is enforced. If omitted, applies to all nodes. Negative indices supported (e.g., -1 for last). Optional field. |
Example
Boundary constraint only at first and last nodes::
nodal_spec: NodalConstraintSpec = {
"constraint_fn": lambda x, u, node, params: x[velocity.slice][0],
"nodes": [0, -1], # Only at start and end
}
Waypoint constraint at middle of trajectory::
nodal_spec: NodalConstraintSpec = {
"constraint_fn": lambda x, u, node, params: jnp.linalg.norm(
x[position.slice] - jnp.array([5.0, 7.5])
) - 0.1,
"nodes": [N // 2],
}