builder
Symbolic problem preprocessing and augmentation pipeline.
This module provides the main preprocessing pipeline for trajectory optimization problems, transforming user-specified symbolic dynamics and constraints into an augmented form ready for compilation to executable code.
The preprocessing pipeline is purely symbolic - no code generation occurs here. Instead, it performs validation, canonicalization, and augmentation to prepare the problem for efficient numerical solution.
Key functionality
- Problem validation: Check shapes, variable names, constraint placement
- Time handling: Auto-create time state or validate user-provided time
- Canonicalization: Simplify expressions algebraically
- Parameter collection: Extract parameter values from expressions
- Constraint separation: Categorize constraints by type (CTCS, nodal, convex)
- CTCS augmentation: Add augmented states and time dilation for path constraints
- Propagation dynamics: Optionally extend dynamics for post-solution propagation
The preprocessing pipeline is purely symbolic - no code generation occurs here.
Pipeline stages
- Time handling & validation
- Expression validation (shapes, names, constraint structure)
- Canonicalization & parameter collection
- Constraint separation & CTCS augmentation
- Propagation dynamics creation
See preprocess_symbolic_problem() for the main entry point.
add_propagation_states(dynamics_extra: dict, states_extra: List[State], dynamics_opt: any, states_opt: List[State], controls_opt: List[Control], parameters: Dict[str, any]) -> Tuple
¶
Extend optimization dynamics with additional propagation-only states.
This function augments the optimization dynamics with extra states that are only needed for post-solution trajectory propagation and simulation. These states don't affect the optimization but are useful for computing derived quantities like distance traveled, energy consumed, or accumulated cost.
Propagation-only states are NOT part of the optimization problem - they are integrated forward after solving using the optimized state and control trajectories. This is more efficient than including them as optimization variables.
The user specifies only the ADDITIONAL states and their dynamics. These are appended after all optimization states (user states + time + CTCS augmented states).
State ordering in propagation dynamics
[user_states, time, ctcs_aug_states, extra_prop_states]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynamics_extra
|
dict
|
Dictionary mapping extra state names to dynamics expressions. Only specify NEW states, not optimization states. Example: {"distance": speed} |
required |
states_extra
|
List[State]
|
List of extra State objects for propagation only |
required |
dynamics_opt
|
any
|
Augmented optimization dynamics expression (from preprocessing) |
required |
states_opt
|
List[State]
|
Augmented optimization states (user + time + CTCS augmented) |
required |
controls_opt
|
List[Control]
|
Augmented optimization controls (user + time dilation) |
required |
parameters
|
Dict[str, any]
|
Dictionary of parameter values from optimization preprocessing |
required |
Returns:
| Type | Description |
|---|---|
Tuple
|
Tuple containing: - dynamics_prop (Expr): Extended dynamics (optimization + extra) - states_prop (List[State]): Extended states (optimization + extra) - controls_prop (List[Control]): Same as controls_opt - parameters_updated (Dict): Updated parameters including any from extra dynamics |
Raises:
| Type | Description |
|---|---|
ValueError
|
If extra states conflict with optimization state names or if validation fails |
Example
Adding distance and energy tracking for propagation::
# After preprocessing, add propagation states
import openscvx as ox
import numpy as np
# Define extra states for tracking
distance = ox.State("distance", shape=(1,))
distance.initial = np.array([0.0])
energy = ox.State("energy", shape=(1,))
energy.initial = np.array([0.0])
# Define their dynamics (using optimization states/controls)
# Assume v and u are optimization states/controls
dynamics_extra = {
"distance": ox.Norm(v), # Integrate velocity magnitude
"energy": ox.Norm(u)**2 # Integrate squared control
}
dyn_prop, states_prop, controls_prop, params = add_propagation_states(
dynamics_extra=dynamics_extra,
states_extra=[distance, energy],
dynamics_opt=dynamics_aug,
states_opt=states_aug,
controls_opt=controls_aug,
parameters=parameters
)
# Now states_prop includes all states for forward simulation
# distance and energy will be integrated during propagation
Note
The extra states should have initial conditions set, as they will be integrated from these initial values during propagation.
Source code in openscvx/symbolic/builder.py
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
preprocess_symbolic_problem(dynamics: dict, constraints: ConstraintSet, states: List[State], controls: List[Control], N: int, time: Time, licq_min: float = 0.0, licq_max: float = 0.0001, time_dilation_factor_min: float = 0.3, time_dilation_factor_max: float = 3.0, dynamics_prop_extra: dict = None, states_prop_extra: List[State] = None) -> SymbolicProblem
¶
Preprocess and augment symbolic trajectory optimization problem.
This is the main preprocessing pipeline that transforms a user-specified symbolic problem into an augmented form ready for compilation. It performs validation, canonicalization, constraint separation, and CTCS augmentation in a series of well-defined phases.
The function is purely symbolic - no code generation or compilation occurs. The output is a SymbolicProblem dataclass that can be lowered to JAX or CVXPy by downstream compilation functions.
Pipeline phases
- Time handling & validation: Auto-create or validate time state
- Expression validation: Validate shapes, names, constraints
- Canonicalization & parameter collection: Simplify and extract parameters
- Constraint separation & augmentation: Sort constraints and add CTCS states
- Propagation dynamics creation: Optionally add extra states for simulation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynamics
|
dict
|
Dictionary mapping state names to dynamics expressions. Example: {"x": v, "v": u} |
required |
constraints
|
ConstraintSet
|
ConstraintSet with raw constraints in |
required |
states
|
List[State]
|
List of user-defined State objects (should NOT include time or CTCS states) |
required |
controls
|
List[Control]
|
List of user-defined Control objects (should NOT include time dilation) |
required |
N
|
int
|
Number of discretization nodes in the trajectory |
required |
time
|
Time
|
Time configuration object specifying time bounds and constraints |
required |
licq_min
|
float
|
Minimum bound for CTCS augmented states (default: 0.0) |
0.0
|
licq_max
|
float
|
Maximum bound for CTCS augmented states (default: 1e-4) |
0.0001
|
time_dilation_factor_min
|
float
|
Minimum factor for time dilation control (default: 0.3) |
0.3
|
time_dilation_factor_max
|
float
|
Maximum factor for time dilation control (default: 3.0) |
3.0
|
dynamics_prop_extra
|
dict
|
Optional dictionary of additional dynamics for propagation-only states (default: None) |
None
|
states_prop_extra
|
List[State]
|
Optional list of additional State objects for propagation only (default: None) |
None
|
Returns:
| Type | Description |
|---|---|
SymbolicProblem
|
SymbolicProblem dataclass with: - dynamics: Augmented dynamics (user + time + CTCS penalties) - states: Augmented states (user + time + CTCS augmented) - controls: Augmented controls (user + time dilation) - constraints: ConstraintSet with is_categorized=True - parameters: Dict of extracted parameter values - node_intervals: List of (start, end) tuples for CTCS intervals - dynamics_prop: Propagation dynamics - states_prop: Propagation states - controls_prop: Propagation controls |
Raises:
| Type | Description |
|---|---|
ValueError
|
If validation fails at any stage |
Example
Basic usage with CTCS constraint::
import openscvx as ox
from openscvx.symbolic.constraint_set import ConstraintSet
x = ox.State("x", shape=(2,))
v = ox.State("v", shape=(2,))
u = ox.Control("u", shape=(2,))
dynamics = {"x": v, "v": u}
constraints = ConstraintSet(unsorted=[
(ox.Norm(x) <= 5.0).over((0, 50))
])
problem = preprocess_symbolic_problem(
dynamics=dynamics,
constraints=constraints,
states=[x, v],
controls=[u],
N=50,
time=ox.Time(initial=0.0, final=10.0)
)
assert problem.is_preprocessed
# problem.dynamics: augmented dynamics expression
# problem.states: [x, v, time, _ctcs_aug_0]
# problem.controls: [u, _time_dilation]
print([s.name for s in problem.states])
# ['x', 'v', 'time', '_ctcs_aug_0']
With propagation-only states::
distance = ox.State("distance", shape=(1,))
dynamics_extra = {"distance": ox.Norm(v)}
problem = preprocess_symbolic_problem(
dynamics=dynamics,
constraints=constraints,
states=[x, v],
controls=[u],
N=50,
time=ox.Time(initial=0.0, final=10.0),
dynamics_prop_extra=dynamics_extra,
states_prop_extra=[distance]
)
# Propagation states include distance for post-solve simulation
print([s.name for s in problem.states_prop])
Source code in openscvx/symbolic/builder.py
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 | |