iteration
One fused, JAX-pure SCP iteration body.
Historically a single SCP step was stitched together in Python: Problem
exported three separate JAX callables (continuous discretization, impulsive
discretization, propagation) and :meth:PenalizedTrustRegion._subproblem
invoked each one, copied NumPy out of every result, pushed the data through
the solver's update_* methods, and finally called solver.solve(). Every
step crossed the NumPy↔JAX boundary several times, and there was no batch axis
anywhere.
This module collapses that stitching into one function. :func:make_scp_iteration
returns a (state, params) -> (state, IterationDiagnostics) callable that, on
the JAX side end to end, discretizes the current iterate, linearizes the
constraints, packs a :class:~openscvx.solvers.ptr_solver.SubproblemData, hands
it to the backend's :meth:~openscvx.solvers.ptr_solver.PTRSolver.iteration_callback,
and folds the returned :class:~openscvx.solvers.ptr_solver.SubproblemSolution
through the autotuner — producing the next :class:AlgorithmState. The
:class:IterationDiagnostics riding alongside carry the host-side bookkeeping
(raw discretization matrices, trust-region / virtual-control matrices, cost,
status) that the Python-loop step() records into AlgorithmHistory and
emits, exactly as the legacy path did. Because state is a registered pytree, the
body composes with jax.jit and jax.vmap.
:func:~openscvx.algorithms.loop.make_solve_loop wraps that body in a
lax.while_loop for the JAX-pure solve_jax / solve_batched paths;
Problem.solve() instead drives the body from a Python while loop so it
can record the :class:IterationDiagnostics into AlgorithmHistory each
iteration.
IterationDiagnostics
dataclass
¶
Host-side bookkeeping produced alongside the next :class:AlgorithmState.
These ride beside the state out of :func:make_scp_iteration so the
Python-loop step() can populate :class:AlgorithmHistory and the
emitter exactly as the legacy _subproblem did — the data they carry is
not on the JAX-traceable state pytree (raw discretization matrices are
large and host-only; cost / status / J_lin come from the subproblem
solve). :func:make_solve_loop projects them away.
Attributes:
| Name | Type | Description |
|---|---|---|
cost |
ndarray
|
Boundary-weighted objective at the candidate (scalar) — the
|
status |
ndarray
|
Subproblem :class: |
J_lin |
ndarray
|
Subproblem optimal (linearized) cost (scalar). |
V |
ndarray
|
Raw continuous discretization matrix of the candidate. |
W |
ndarray
|
Raw impulsive discretization matrix of the candidate. |
TR |
ndarray
|
Scaled trust-region step matrix, shape |
VC |
ndarray
|
Scaled virtual-control matrix, shape |
Source code in openscvx/algorithms/scvx/iteration.py
make_scp_iteration(dis_continuous: Callable, dis_impulsive: Callable, jax_constraints: LoweredJaxConstraints, solver_callback: Callable[[AlgorithmState, SubproblemData], SubproblemSolution], autotuner: AutotuningBase, settings: Config) -> Callable[[AlgorithmState, dict], Tuple[AlgorithmState, IterationDiagnostics]]
¶
Build one JAX-pure SCP iteration body.
The returned iteration_fn(state, params) performs a single SCP step and
returns (next_state, diagnostics). It is the fused mirror of the legacy
discretize → linearize → solve → autotune pipeline:
- Discretize the continuous dynamics about the current iterate
(
state.x/state.u) →A_d, B_d, C_d, x_prop. - Discretize the impulsive/discrete dynamics about the propagated nodes →
x_prop_plus, D_d, E_d. - Evaluate every nodal / cross-node constraint and its gradients.
- Pack the linearization, penalty weights, and boundary pins into a
:class:
SubproblemData. - Hand it to
solver_callback(the backend'siteration_callback), which returns a :class:SubproblemSolution. - Compute the
J_tr / J_vb / J_vcmetrics and fold the candidate through the autotuner to produce the next state, alongside an :class:IterationDiagnostics.
The current-iterate discretization (steps 1–2) is recomputed every call
rather than read from a carried state.x_prop: the candidate is
re-discretized next iteration instead of carrying its discretization on
:class:AlgorithmState. This trades roughly 2× discretization work per
iteration for a smaller loop carry and a simpler accept/reject rule in the
autotuner — the next iterate is a pure function of state.x / state.u,
so acceptance copies only the trajectory, never a discretization. The
discretization solvers are built by the caller (Problem) and
captured here so the caching policy stays in its own layer; the constraint
linearizers, per-backend solver callback, autotuner, and settings are
likewise closure constants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dis_continuous
|
Callable
|
Continuous discretization solver,
|
required |
dis_impulsive
|
Callable
|
Impulsive discretization solver,
|
required |
jax_constraints
|
LoweredJaxConstraints
|
Lowered nodal / cross-node JAX constraints. |
required |
solver_callback
|
Callable[[AlgorithmState, SubproblemData], SubproblemSolution]
|
|
required |
autotuner
|
AutotuningBase
|
Weight-update strategy applied to the candidate. |
required |
settings
|
Config
|
Problem configuration (scaling matrices, boundary types). |
required |
Returns:
| Type | Description |
|---|---|
Callable[[AlgorithmState, dict], Tuple[AlgorithmState, IterationDiagnostics]]
|
|
Source code in openscvx/algorithms/scvx/iteration.py
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 | |