history
CPU-side append-only iteration log and discretization read helpers for SCP.
:class:AlgorithmHistory is the mutable, host-side container the SCP loop grows
after each iteration via meth:
~AlgorithmHistory.record_iteration — it
never crosses the JAX boundary. :class:DiscretizationResult unpacks a raw
discretization matrix once so history reads are trivial slicing-free access.
:class:MultishotPropagation complements :class:DiscretizationResult by
exposing the full substep state trajectories stored during multishot
discretization.
Last in the algorithms import order: it depends on
:mod:openscvx.algorithms.state and nothing here depends on it.
AlgorithmHistory
dataclass
¶
Append-only iteration log, populated by the SCP loop.
Mirrors the lists that previously lived directly on AlgorithmState.
Never appears on the JAX boundary; the SCP loop appends to it after each
iteration via meth:
record_iteration. Purely diagnostic — the
subproblem solver consumes SubproblemData in-trace, not the history.
Source code in openscvx/algorithms/history.py
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 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 | |
V_history: List[np.ndarray]
property
¶
View of the raw discretization matrices recorded so far.
from_settings(settings: 'Config') -> 'AlgorithmHistory'
classmethod
¶
Construct an empty history sized to the problem.
record_iteration(state: AlgorithmState, *, V: Optional[np.ndarray] = None, W: Optional[np.ndarray] = None, VC: Optional[np.ndarray] = None, TR: Optional[np.ndarray] = None, J_lin: Optional[float] = None, record_diagnostics: bool = True) -> Tuple[dict, np.ndarray]
¶
Append per-iteration data based on state.adaptive_state_code.
The accepted iterate (x / u / weights / convergence scalars) is
read off state; the raw host-side diagnostics the SCP loop already
synced — discretization matrices (V / W), virtual control
(VC), trust region (TR), and the linearized cost (J_lin) —
come in as explicit keyword arrays so the generic history never has to
know the algorithm's diagnostics type.
Reproduces the old accept_solution / reject_solution behavior:
- REJECT: append
lam_prox(the bumped weight that drives the next subproblem), plus the predicted/actual/acceptance diagnostics whenrecord_diagnosticsis set. - INITIAL: append every trajectory / weight history entry, but skip the predicted/actual/acceptance diagnostics (the autotuner didn't compute them on iter 1).
- ACCEPT_*: append everything, including diagnostics when
record_diagnosticsis set (the SCP loop turns this off for autotuners that don't compute them — :class:ConstantProximalWeight, :class:RampProximalWeight).
Every device-resident leaf this method needs is pulled with one
jax.device_get call (a single CPU<->device round trip) and the
host-side numpy arrays are then partitioned onto the history lists.
The SCP loop also reads the same scalars for printing + convergence,
so the bundle is returned alongside the lam_prox array to avoid a
second sync.
Returns:
| Type | Description |
|---|---|
dict
|
|
ndarray
|
( |
Tuple[dict, ndarray]
|
|
Tuple[dict, ndarray]
|
|
Tuple[dict, ndarray]
|
the numpy copy of |
Tuple[dict, ndarray]
|
these directly without re-syncing. |
Source code in openscvx/algorithms/history.py
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 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 | |
DiscretizationResult
dataclass
¶
Unpacked discretization data from a multi-shot discretization matrix.
The discretization solver returns a matrix V that stores multiple blocks
(propagated state and linearization matrices) across nodes/time. Historically,
we stored the raw V matrices and re-unpacked them repeatedly via slicing.
This dataclass unpacks once and makes access trivial.
Source code in openscvx/algorithms/history.py
from_V(V: np.ndarray, n_x: int, n_u: int, N: int) -> 'DiscretizationResult'
classmethod
¶
Unpack the final timestep of a raw discretization matrix V.
Source code in openscvx/algorithms/history.py
from_VW(V: np.ndarray, W: np.ndarray, n_x: int, n_u: int, N: int) -> 'DiscretizationResult'
classmethod
¶
Unpack continuous and impulsive discretization blocks from V and W.
Source code in openscvx/algorithms/history.py
MultishotPropagation
dataclass
¶
Unpacked SCP multishot integration matrix V.
V shape: (n_segments * segment_size, n_substeps) where
segment_size = n_x + n_x**2 + 2 * n_x * n_u.
Source code in openscvx/algorithms/history.py
chronological() -> tuple[np.ndarray, np.ndarray]
¶
Time-ordered stitched full states and times.
Skips duplicated segment-boundary samples (j0 = 0 on seg 0, else 1).
Returns:
| Type | Description |
|---|---|
ndarray
|
|
ndarray
|
|
Source code in openscvx/algorithms/history.py
segment_states(seg_idx: int) -> np.ndarray
¶
Integrated states for one segment, shape (n_substeps, n_x).
Source code in openscvx/algorithms/history.py
segments() -> list[np.ndarray]
¶
slice_states(state_slice: slice) -> tuple[np.ndarray, np.ndarray]
¶
chronological() then apply state_slice to the state dimension.
state(name_or_state: StateLike) -> tuple[np.ndarray, np.ndarray]
¶
Chronological trajectory for one symbolic state.
name_or_state may be a :class:~openscvx.symbolic.expr.state.State
instance or a string name matching State.name in self.states.
Source code in openscvx/algorithms/history.py
segment_size(n_x: int, n_u: int) -> int
¶
unpack_multishot_V(V: np.ndarray, *, n_x: int, n_u: int, t_nodes: np.ndarray, states: Sequence = ()) -> MultishotPropagation
¶
Validate V layout and return :class:MultishotPropagation.
Raises:
| Type | Description |
|---|---|
ValueError
|
invalid |