expert
Expert-mode features for advanced users.
This module contains features for expert users who need fine-grained control and are willing to bypass higher-level abstractions.
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],
}
Source code in openscvx/expert/byof.py
apply_byof(byof: dict, dynamics: Dynamics, dynamics_prop: Dynamics, jax_constraints: LoweredJaxConstraints, x_unified: UnifiedState, x_prop_unified: UnifiedState, u_unified: UnifiedState, states: List[State], states_prop: List[State], N: int) -> Tuple[Dynamics, Dynamics, LoweredJaxConstraints, UnifiedState, UnifiedState]
¶
Apply bring-your-own-functions (byof) to augment lowered problem.
Handles raw JAX functions provided by expert users, including: - dynamics: Raw JAX functions for specific state derivatives - nodal_constraints: Point-wise constraints at each node - cross_nodal_constraints: Constraints coupling multiple nodes - ctcs_constraints: Continuous-time constraint satisfaction via dynamics augmentation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
byof
|
dict
|
Dict with keys "dynamics", "nodal_constraints", "cross_nodal_constraints", "ctcs_constraints" |
required |
dynamics
|
Dynamics
|
Lowered optimization dynamics to potentially augment |
required |
dynamics_prop
|
Dynamics
|
Lowered propagation dynamics to potentially augment |
required |
jax_constraints
|
LoweredJaxConstraints
|
Lowered JAX constraints to append to |
required |
x_unified
|
UnifiedState
|
Unified optimization state interface to potentially augment |
required |
x_prop_unified
|
UnifiedState
|
Unified propagation state interface to potentially augment |
required |
u_unified
|
UnifiedState
|
Unified control interface for validation |
required |
states
|
List[State]
|
List of State objects for optimization (with _slice attributes) |
required |
states_prop
|
List[State]
|
List of State objects for propagation (with _slice attributes) |
required |
N
|
int
|
Number of nodes in the trajectory |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Dynamics, Dynamics, LoweredJaxConstraints, UnifiedState, UnifiedState]
|
Tuple of (dynamics, dynamics_prop, jax_constraints, x_unified, x_prop_unified) |
Example
dynamics, dynamics_prop, constraints, x_unified, x_prop_unified = apply_byof( ... byof, dynamics, dynamics_prop, jax_constraints, ... x_unified, x_prop_unified, u_unified, states, states_prop, N ... )
Source code in openscvx/expert/lowering.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
validate_byof(byof: dict, states: List[State], n_x: int, n_u: int, N: int = None) -> None
¶
Validate byof function signatures and shapes.
Checks that user-provided functions have the correct signatures and return appropriate shapes. Performs validation before functions are used to provide clear error messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
byof
|
dict
|
Dictionary of user-provided functions to validate |
required |
states
|
List[State]
|
List of State objects for determining expected shapes |
required |
n_x
|
int
|
Total dimension of the unified state vector |
required |
n_u
|
int
|
Total dimension of the unified control vector |
required |
N
|
int
|
Number of nodes in the trajectory (optional). If provided, validates node indices in nodal constraints. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any function has invalid signature or returns wrong shape |
TypeError
|
If functions are not callable |
Example
validate_byof(byof, states, n_x=10, n_u=3, N=50) # Raises if invalid
Source code in openscvx/expert/validation.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |