unified
Unification functions for aggregating symbolic State and Control objects.
This module provides the unification layer that transforms multiple symbolic State and Control objects into unified representations for numerical optimization.
The unification process
- Collection: Gathers all State and Control objects from expression trees
- Sorting: Organizes variables (user-defined first, then augmented)
- Aggregation: Concatenates bounds, guesses, and boundary conditions
- Slice Assignment: Assigns each State/Control a slice for indexing
- Unified Representation: Creates UnifiedState/UnifiedControl objects
This separation allows users to define problems with natural variable names while maintaining efficient vectorized operations during optimization.
Example
Creating and unifying multiple states::
import openscvx as ox
from openscvx.symbolic.unified import unify_states
# Define separate symbolic states
position = ox.State("position", shape=(3,), min=-10, max=10)
velocity = ox.State("velocity", shape=(3,), min=-5, max=5)
mass = ox.State("mass", shape=(1,), min=0.1, max=10.0)
# Unify into single state vector
unified_x = unify_states([position, velocity, mass], name="x")
# Access unified properties
print(unified_x.shape) # (7,) - combined shape
print(unified_x.min) # Combined bounds: [-10, -10, -10, -5, -5, -5, 0.1]
print(unified_x.true) # Access only user-defined states
Accessing slices after unification::
# After unification, each State has a slice assigned
print(position._slice) # slice(0, 3)
print(velocity._slice) # slice(3, 6)
print(mass._slice) # slice(6, 7)
# During lowering, these slices extract values from unified vector
x_unified = jnp.array([1, 2, 3, 4, 5, 6, 7])
position_val = x_unified[position._slice] # [1, 2, 3]
See Also
- UnifiedState: Dataclass for unified state representation (in openscvx.lowered.unified)
- UnifiedControl: Dataclass for unified control representation (in openscvx.lowered.unified)
- State: Individual symbolic state variable (symbolic/expr/state.py)
- Control: Individual symbolic control variable (symbolic/expr/control.py)
UnifiedControl
dataclass
¶
Unified control vector aggregating multiple Control objects.
UnifiedControl is a drop-in replacement for individual Control objects that holds aggregated data from multiple Control instances. It maintains compatibility with optimization infrastructure while providing access to individual control components through slicing.
The unified control separates user-defined "true" controls from augmented controls added internally (e.g., for time dilation). This separation allows clean access to physical control inputs while supporting advanced features.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name identifier for the unified control vector |
shape |
tuple
|
Combined shape (total_dim,) of all aggregated controls |
min |
ndarray
|
Lower bounds for all control variables, shape (total_dim,) |
max |
ndarray
|
Upper bounds for all control variables, shape (total_dim,) |
guess |
ndarray
|
Initial guess trajectory, shape (num_nodes, total_dim) |
_true_dim |
int
|
Number of user-defined control dimensions (excludes augmented) |
_true_slice |
slice
|
Slice for extracting true controls from unified vector |
_augmented_slice |
slice
|
Slice for extracting augmented controls |
time_dilation_slice |
Optional[slice]
|
Slice for time dilation control, if present |
Properties
true: Returns UnifiedControl view containing only true (user-defined) controls augmented: Returns UnifiedControl view containing only augmented controls
Example
Creating a unified control from multiple Control objects::
from openscvx.symbolic.unified import unify_controls
thrust = ox.Control("thrust", shape=(3,), min=0, max=10)
torque = ox.Control("torque", shape=(3,), min=-1, max=1)
unified = unify_controls([thrust, torque], name="u")
print(unified.shape) # (6,)
print(unified.min) # [0, 0, 0, -1, -1, -1]
print(unified.true.shape) # (6,) - all are true controls
print(unified.augmented.shape) # (0,) - no augmented controls
Appending controls dynamically::
unified = UnifiedControl(name="u", shape=(0,), _true_dim=0)
unified.append(min=-1, max=1, guess=0.0) # Add scalar control
print(unified.shape) # (1,)
See Also
- unify_controls(): Factory function for creating UnifiedControl from Control list
- Control: Individual symbolic control variable
- UnifiedState: Analogous unified state vector
Source code in openscvx/lowered/unified.py
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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | |
augmented: UnifiedControl
property
¶
Get the augmented (internal) control variables.
Returns a view of the unified control containing only augmented controls added internally by the optimization framework (e.g., time dilation control).
Returns:
| Name | Type | Description |
|---|---|---|
UnifiedControl |
UnifiedControl
|
Sliced view containing only augmented control variables |
Example
Get augmented controls::
unified = unify_controls([thrust, time_dilation], name="u")
aug_controls = unified.augmented # Only time dilation
true: UnifiedControl
property
¶
Get the true (user-defined) control variables.
Returns a view of the unified control containing only user-defined controls, excluding internal augmented controls added for time dilation, etc.
Returns:
| Name | Type | Description |
|---|---|---|
UnifiedControl |
UnifiedControl
|
Sliced view containing only true control variables |
Example
Get true user defined controls::
unified = unify_controls([thrust, torque, time_dilation], name="u")
true_controls = unified.true # Only thrust and torque
append(other: Optional[Control | UnifiedControl] = None, *, min=-np.inf, max=np.inf, guess=0.0, augmented=False)
¶
Append another control or create a new control variable.
This method allows dynamic extension of the unified control, either by appending another Control/UnifiedControl object or by creating a new scalar control variable with specified properties. Modifies the unified control in-place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Optional[Control | UnifiedControl]
|
Control object to append. If None, creates a new scalar control variable with properties from keyword args. |
None
|
min
|
float
|
Lower bound for new scalar control (default: -inf) |
-inf
|
max
|
float
|
Upper bound for new scalar control (default: inf) |
inf
|
guess
|
float
|
Initial guess value for new scalar control (default: 0.0) |
0.0
|
augmented
|
bool
|
Whether the appended control is augmented (internal) rather than true (user-defined). Affects _true_dim tracking. Default: False |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
Modifies the unified control in-place |
Example
Appending a Control object::
unified = unify_controls([thrust], name="u")
torque = ox.Control("torque", shape=(3,), min=-1, max=1)
unified.append(torque)
print(unified.shape) # (6,) - thrust (3) + torque (3)
Creating new scalar control variables::
unified = UnifiedControl(name="u", shape=(0,), _true_dim=0)
unified.append(min=-1, max=1, guess=0.0) # Add scalar control
print(unified.shape) # (1,)
Source code in openscvx/lowered/unified.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
UnifiedState
dataclass
¶
Unified state vector aggregating multiple State objects.
UnifiedState is a drop-in replacement for individual State objects that holds aggregated data from multiple State instances. It maintains compatibility with optimization infrastructure while providing access to individual state components through slicing.
The unified state separates user-defined "true" states from augmented states added internally (e.g., for CTCS constraints or time variables). This separation allows clean access to physical states while supporting advanced features.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name identifier for the unified state vector |
shape |
tuple
|
Combined shape (total_dim,) of all aggregated states |
min |
ndarray
|
Lower bounds for all state variables, shape (total_dim,) |
max |
ndarray
|
Upper bounds for all state variables, shape (total_dim,) |
guess |
ndarray
|
Initial guess trajectory, shape (num_nodes, total_dim) |
initial |
ndarray
|
Initial boundary conditions, shape (total_dim,) |
final |
ndarray
|
Final boundary conditions, shape (total_dim,) |
_initial |
ndarray
|
Internal initial values, shape (total_dim,) |
_final |
ndarray
|
Internal final values, shape (total_dim,) |
initial_type |
ndarray
|
Boundary condition types at t0 ("Fix" or "Free"), shape (total_dim,), dtype=object |
final_type |
ndarray
|
Boundary condition types at tf ("Fix" or "Free"), shape (total_dim,), dtype=object |
_true_dim |
int
|
Number of user-defined state dimensions (excludes augmented) |
_true_slice |
slice
|
Slice for extracting true states from unified vector |
_augmented_slice |
slice
|
Slice for extracting augmented states |
time_slice |
Optional[slice]
|
Slice for time state variable, if present |
ctcs_slice |
Optional[slice]
|
Slice for CTCS augmented states, if present |
Properties
true: Returns UnifiedState view containing only true (user-defined) states augmented: Returns UnifiedState view containing only augmented states
Example
Creating a unified state from multiple State objects::
from openscvx.symbolic.unified import unify_states
position = ox.State("pos", shape=(3,), min=-10, max=10)
velocity = ox.State("vel", shape=(3,), min=-5, max=5)
unified = unify_states([position, velocity], name="x")
print(unified.shape) # (6,)
print(unified.min) # [-10, -10, -10, -5, -5, -5]
print(unified.true.shape) # (6,) - all are true states
print(unified.augmented.shape) # (0,) - no augmented states
Appending states dynamically::
unified = UnifiedState(name="x", shape=(0,), _true_dim=0)
unified.append(min=-1, max=1, guess=0.5) # Add scalar state
print(unified.shape) # (1,)
See Also
- unify_states(): Factory function for creating UnifiedState from State list
- State: Individual symbolic state variable
- UnifiedControl: Analogous unified control vector
Source code in openscvx/lowered/unified.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 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 | |
augmented: UnifiedState
property
¶
Get the augmented (internal) state variables.
Returns a view of the unified state containing only augmented states added internally by the optimization framework (e.g., CTCS penalty states, time variables).
Returns:
| Name | Type | Description |
|---|---|---|
UnifiedState |
UnifiedState
|
Sliced view containing only augmented state variables |
Example
Get augmented state::
unified = unify_states([position, ctcs_aug], name="x")
aug_states = unified.augmented # Only CTCS states
true: UnifiedState
property
¶
Get the true (user-defined) state variables.
Returns a view of the unified state containing only user-defined states, excluding internal augmented states added for CTCS, time, etc.
Returns:
| Name | Type | Description |
|---|---|---|
UnifiedState |
UnifiedState
|
Sliced view containing only true state variables |
Example
Get true user-defined state::
unified = unify_states([position, velocity, ctcs_aug], name="x")
true_states = unified.true # Only position and velocity
true_states.shape # (6,) if position and velocity are 3D each
append(other: Optional[State | UnifiedState] = None, *, min=-np.inf, max=np.inf, guess=0.0, initial=0.0, final=0.0, augmented=False)
¶
Append another state or create a new state variable.
This method allows dynamic extension of the unified state, either by appending another State/UnifiedState object or by creating a new scalar state variable with specified properties. Modifies the unified state in-place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Optional[State | UnifiedState]
|
State object to append. If None, creates a new scalar state variable with properties from keyword args. |
None
|
min
|
float
|
Lower bound for new scalar state (default: -inf) |
-inf
|
max
|
float
|
Upper bound for new scalar state (default: inf) |
inf
|
guess
|
float
|
Initial guess value for new scalar state (default: 0.0) |
0.0
|
initial
|
float
|
Initial boundary condition for new scalar state (default: 0.0) |
0.0
|
final
|
float
|
Final boundary condition for new scalar state (default: 0.0) |
0.0
|
augmented
|
bool
|
Whether the appended state is augmented (internal) rather than true (user-defined). Affects _true_dim tracking. Default: False |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
Modifies the unified state in-place |
Example
Appending a State object::
unified = unify_states([position], name="x")
velocity = ox.State("vel", shape=(3,), min=-5, max=5)
unified.append(velocity)
print(unified.shape) # (6,) - position (3) + velocity (3)
Creating new scalar state variables::
unified = UnifiedState(name="x", shape=(0,), _true_dim=0)
unified.append(min=-1, max=1, guess=0.5) # Add scalar state
unified.append(min=-2, max=2, augmented=True) # Add augmented state
print(unified.shape) # (2,)
print(unified._true_dim) # 1 (only first is true)
Note
Maintains the invariant that true states appear before augmented states in the unified vector. When appending augmented states, they are added to the end but don't increment _true_dim.
Source code in openscvx/lowered/unified.py
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 | |
unify_controls(controls: List[Control], name: str = 'unified_control') -> UnifiedControl
¶
Create a UnifiedControl from a list of Control objects.
This function is the primary way to aggregate multiple symbolic Control objects into a single unified control vector for numerical optimization. It:
- Sorts controls (user-defined first, augmented controls second)
- Concatenates all control properties (bounds, guesses)
- Assigns slices to each Control for extracting values from unified vector
- Identifies special controls (time dilation)
- Returns a UnifiedControl with all aggregated data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
controls
|
List[Control]
|
List of Control objects to unify. Can include both user-defined controls and augmented controls (names starting with '_'). |
required |
name
|
str
|
Name identifier for the unified control vector (default: "unified_control") |
'unified_control'
|
Returns:
| Name | Type | Description |
|---|---|---|
UnifiedControl |
UnifiedControl
|
Unified control object containing: - Aggregated bounds and guesses - Shape equal to sum of all control shapes - Slices for extracting individual control components - Properties for accessing true vs augmented controls |
Example
Basic unification::
import openscvx as ox
from openscvx.symbolic.unified import unify_controls
thrust = ox.Control("thrust", shape=(3,), min=0, max=10)
torque = ox.Control("torque", shape=(3,), min=-1, max=1)
unified = unify_controls([thrust, torque], name="u")
print(unified.shape) # (6,)
print(unified._true_dim) # 6 (all are user controls)
print(thrust._slice) # slice(0, 3) - assigned during unification
print(torque._slice) # slice(3, 6)
With augmented controls::
# Time-optimal problems may add time dilation control
time_dilation = ox.Control("_time_dilation", shape=(1,))
unified = unify_controls([thrust, torque, time_dilation])
print(unified._true_dim) # 6 (thrust + torque)
print(unified.true.shape) # (6,)
print(unified.augmented.shape) # (1,) - time dilation
Note
After unification, each Control object has its _slice attribute set,
which is used during JAX lowering to extract the correct values from
the unified control vector.
See Also
- UnifiedControl: Return type with detailed documentation
- unify_states(): Analogous function for State objects
- Control: Individual symbolic control variable
Source code in openscvx/symbolic/unified.py
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 420 | |
unify_states(states: List[State], name: str = 'unified_state') -> UnifiedState
¶
Create a UnifiedState from a list of State objects.
This function is the primary way to aggregate multiple symbolic State objects into a single unified state vector for numerical optimization. It:
- Sorts states (user-defined first, augmented states second)
- Concatenates all state properties (bounds, guesses, boundary conditions)
- Assigns slices to each State for extracting values from unified vector
- Identifies special states (time, CTCS augmented states)
- Returns a UnifiedState with all aggregated data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
List[State]
|
List of State objects to unify. Can include both user-defined states and augmented states (names starting with '_'). |
required |
name
|
str
|
Name identifier for the unified state vector (default: "unified_state") |
'unified_state'
|
Returns:
| Name | Type | Description |
|---|---|---|
UnifiedState |
UnifiedState
|
Unified state object containing: - Aggregated bounds, guesses, and boundary conditions - Shape equal to sum of all state shapes - Slices for extracting individual state components - Properties for accessing true vs augmented states |
Example
Basic unification::
import openscvx as ox
from openscvx.symbolic.unified import unify_states
position = ox.State("pos", shape=(3,), min=-10, max=10)
velocity = ox.State("vel", shape=(3,), min=-5, max=5)
unified = unify_states([position, velocity], name="x")
print(unified.shape) # (6,)
print(unified._true_dim) # 6 (all are user states)
print(position._slice) # slice(0, 3) - assigned during unification
print(velocity._slice) # slice(3, 6)
With augmented states::
# CTCS or other features may add augmented states
time_state = ox.State("time", shape=(1,))
ctcs_aug = ox.State("_ctcs_aug_0", shape=(2,)) # Augmented state
unified = unify_states([position, velocity, time_state, ctcs_aug])
print(unified._true_dim) # 7 (pos + vel + time)
print(unified.true.shape) # (7,)
print(unified.augmented.shape) # (2,) - only CTCS augmented
Note
After unification, each State object has its _slice attribute set,
which is used during JAX lowering to extract the correct values from
the unified state vector.
See Also
- UnifiedState: Return type with detailed documentation
- unify_controls(): Analogous function for Control objects
- State: Individual symbolic state variable
Source code in openscvx/symbolic/unified.py
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 | |