ptr_solver
Abstract base class for Penalized Trust-Region (PTR) convex subproblem solvers.
The PTR formulation — its variables, slack structure, cost terms, and linearization contract — is shared across backends. This module defines that contract; concrete backends (CVXPy, QPAX, Moreau) live in sibling modules and implement the assembly/dispatch that each backend's modeling layer requires.
Two per-iteration entry points coexist:
- The historical NumPy contract — four :meth:
PTRSolver.update_*stages followed by :meth:PTRSolver.solve, returning a :class:PTRSolveResult. Used by today's Python-side SCP loop and by direct interactive use of :class:~openscvx.solvers.cvxpy_ptr_solver.CVXPyPTRSolver. - The JAX-pure contract — :meth:
PTRSolver.iteration_callback, which returns a(state, SubproblemData) -> SubproblemSolutioncallable built once at :meth:~openscvx.solvers.base.ConvexSolver.initialize. All backends emit the same input/output pytree shape so the callable composes withjax.jit/jax.vmapand, downstream,lax.while_loop-driven SCP iteration.
Backends
:class:openscvx.solvers.cvxpy_ptr_solver.CVXPyPTRSolver
DCP graph assembled via CVXPy, dispatched to a conic solver
(QOCO, CLARABEL, ...). The JAX-pure callback wraps the host solve
in :func:jax.pure_callback (vmap_method="sequential").
:class:openscvx.solvers.qpax_ptr_solver.QPAXPTRSolver
Flat (Q, q, A, b, G, h) assembled as JAX arrays and solved with
qpax.solve_qp (NumPy path) or the differentiable
qpax.solve_qp_primal (JAX-pure path). Enables an end-to-end
JAX-differentiable SCP loop in follow-up work.
:class:openscvx.solvers.moreau_ptr_solver.MoreauPTRSolver
Sparse conic program assembled as CSR JAX arrays and solved with
moreau.jax.Solver. The NumPy path warm-starts between SCP
iterations. The JAX-pure path calls that solver's jitted kernel,
which returns (solution, info) and does not warm-start. Uses SOC
epigraphs for the L1 / pos
PTR penalties instead of QPAX-style slack expansion.
PTRSolveResult
dataclass
¶
Result from solving a PTR convex subproblem.
Contains the solution trajectories and slack variables from a single SCP iteration. All trajectories are unscaled (physical units).
Attributes:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
State trajectory, shape (N, n_states). Unscaled. |
u |
ndarray
|
Control trajectory, shape (N, n_controls). Unscaled. |
nu |
ndarray
|
Virtual control slack for dynamics defects, shape (N-1, n_states). |
nu_vb |
List[ndarray]
|
Nonconvex nodal constraint violation slacks. List of arrays, one per nodal constraint. |
nu_vb_cross |
List[float]
|
Cross-node constraint violation slacks. List of scalars, one per cross-node constraint. |
cost |
float
|
Optimal objective value. |
status |
str
|
Solver status string (e.g., "optimal", "infeasible"). |
Source code in openscvx/solvers/ptr_solver.py
PTRSolver
¶
Bases: ConvexSolver
Abstract base class for Penalized Trust-Region convex subproblem solvers.
Defines the contract every PTR backend must satisfy: per-iteration entry
points for updating the linearization point, constraint gradients, penalty
weights, and boundary conditions, plus a solve() returning a
:class:PTRSolveResult. The set of variables (state, control, virtual
control nu, per-constraint virtual buffer nu_vb, cross-node slack
nu_vb_cross) is fixed by the PTR formulation; how each backend
realizes those variables is an implementation detail.
Subclasses must additionally implement :meth:create_variables and
:meth:initialize from :class:ConvexSolver.
Attributes:
| Name | Type | Description |
|---|---|---|
SUPPORTED_CONE_TYPES |
FrozenSet[Type[ConeConstraint]]
|
The set of
:class: |
Source code in openscvx/solvers/ptr_solver.py
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 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 | |
exportable: bool
property
¶
Whether this backend's solve survives jax.export serialization.
:meth:~openscvx.problem.Problem.solve_batched exports the whole
vmapped SCP loop to disk under save_compiled=True, which requires
every op in the loop — the backend solve included — to lower to
serializable XLA. The pure-JAX backends (QPAX, Moreau) qualify, so the
base default is True. CVXPy's :meth:iteration_callback is a
:func:jax.pure_callback, whose host call jax.export cannot
serialize, so it overrides this to False and solve_batched
raises a teaching error rather than silently degrading to an in-process
solve.
iteration_callback() -> Callable[..., SubproblemSolution]
abstractmethod
¶
Return a JAX-friendly (state, SubproblemData) -> SubproblemSolution.
Built once at :meth:initialize; called once per SCP iteration. The
callable consumes the algorithm-state pytree plus an assembled
:class:SubproblemData, performs one assemble → solve → unpack
cycle on the JAX boundary, and returns a :class:SubproblemSolution.
Replaces the four update_* methods + :meth:solve for the
JAX-pure SCP path. All backends share the same input/output pytree
shape so the SCP loop downstream can wrap the callback in
lax.while_loop and compose with jax.jit / jax.vmap.
Source code in openscvx/solvers/ptr_solver.py
lower_convex_constraints(constraints: ConstraintSet, parameters: Optional[Dict[str, Any]] = None) -> Tuple[List[Any], Dict[str, Any]]
¶
Lower user .convex() constraints into cone constraints.
Separates auto-generated impulsive zero-pin equalities from genuine
user constraints, canonicalises the latter via
:func:~openscvx.symbolic.canonicalize.canonicalize_nodal_constraint,
and verifies each cone type against :attr:SUPPORTED_CONE_TYPES.
Cross-node convex constraints are not yet supported by the JAX
backends and raise :class:NotImplementedError.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
For cross-node convex constraints or unsupported cone types (with a message listing which backends do support the cone type). |
ValueError
|
If a constraint is not affine in state/control. |
Source code in openscvx/solvers/ptr_solver.py
solve() -> PTRSolveResult
abstractmethod
¶
Solve the convex subproblem and return a :class:PTRSolveResult.
Call the four update_* methods first to set the linearization
point, constraint gradients, penalties, and boundary conditions.
Source code in openscvx/solvers/ptr_solver.py
update_boundary_conditions(x_init: np.ndarray = None, x_term: np.ndarray = None) -> None
abstractmethod
¶
Update initial and/or terminal state parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_init
|
ndarray
|
Initial state vector, shape (n_states,). Optional. |
None
|
x_term
|
ndarray
|
Terminal state vector, shape (n_states,). Optional. |
None
|
Source code in openscvx/solvers/ptr_solver.py
update_constraint_linearizations(nodal: List[dict] = None, cross_node: List[dict] = None) -> None
abstractmethod
¶
Update linearized constraint values and gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodal
|
List[dict]
|
List of dicts, one per nodal constraint, each containing:
|
None
|
cross_node
|
List[dict]
|
List of dicts, one per cross-node constraint, each
containing: |
None
|
Source code in openscvx/solvers/ptr_solver.py
update_dynamics_linearization(x_bar: np.ndarray, u_bar: np.ndarray, A_d: np.ndarray, B_d: np.ndarray, C_d: np.ndarray, x_prop: np.ndarray, x_prop_plus: np.ndarray | None = None, D_d: np.ndarray | None = None, E_d: np.ndarray | None = None) -> None
abstractmethod
¶
Update dynamics linearization point and discrete-time matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_bar
|
ndarray
|
Previous state trajectory, shape (N, n_states). |
required |
u_bar
|
ndarray
|
Previous control trajectory, shape (N, n_controls). |
required |
A_d
|
ndarray
|
Discretized state Jacobian, shape (N-1, n_states, n_states). |
required |
B_d
|
ndarray
|
Discretized control Jacobian (current node), shape (N-1, n_states, n_controls). |
required |
C_d
|
ndarray
|
Discretized control Jacobian (next node), shape (N-1, n_states, n_controls). |
required |
x_prop
|
ndarray
|
Propagated state from continuous dynamics, shape (N-1, n_states). |
required |
x_prop_plus
|
ndarray | None
|
Optional impulsive/discrete propagated state, shape (N, n_states). |
None
|
D_d
|
ndarray | None
|
Optional impulsive/discrete Jacobian wrt state, shape (N, n_states, n_states). |
None
|
E_d
|
ndarray | None
|
Optional impulsive/discrete Jacobian wrt control, shape (N, n_states, n_controls). |
None
|
Source code in openscvx/solvers/ptr_solver.py
update_penalties(lam_prox: np.ndarray, lam_cost: Union[float, np.ndarray], lam_vc: np.ndarray, lam_vb_nodal: np.ndarray, lam_vb_cross: np.ndarray) -> None
abstractmethod
¶
Update SCP penalty weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lam_prox
|
ndarray
|
Trust region weights, shape (N, n_states + n_controls). |
required |
lam_cost
|
Union[float, ndarray]
|
Cost function weight. Scalar or shape (n_states,). |
required |
lam_vc
|
ndarray
|
Virtual control penalty weights, shape (N-1, n_states). |
required |
lam_vb_nodal
|
ndarray
|
Virtual buffer penalty weights for nodal constraints, shape (N, n_nodal_constraints). |
required |
lam_vb_cross
|
ndarray
|
Virtual buffer penalty weights for cross-node constraints, shape (n_cross_node_constraints,). |
required |
Source code in openscvx/solvers/ptr_solver.py
StatusCode
¶
Bases: IntEnum
Subproblem solve outcome, encoded as int32 for JAX traceability.
iteration_callback returns a numeric status on the JAX side rather than
a Python string so the result pytree stays valid under jax.jit /
jax.vmap. The SCP loop maps the code back to a label via
:func:status_code_to_str only on the Python-loop printing path.
Source code in openscvx/solvers/ptr_solver.py
SubproblemData
dataclass
¶
JAX-pure inputs to :meth:PTRSolver.iteration_callback.
Packs the per-iteration linearization, penalty weights, and boundary conditions into a single registered pytree so a single SCP iteration — assemble → solve → unpack — happens on the JAX boundary instead of bouncing through NumPy each step.
Attributes:
| Name | Type | Description |
|---|---|---|
x_bar |
ndarray
|
Previous nodal state, shape |
u_bar |
ndarray
|
Previous nodal control, shape |
A_d, |
(B_d, C_d)
|
Continuous-step discretized Jacobians,
shapes |
x_prop |
ndarray
|
Propagated state, shape |
x_prop_plus |
ndarray
|
Impulsive propagated state, shape |
D_d, |
E_d
|
Impulsive Jacobians, shapes |
nodal_g |
ndarray
|
Nodal constraint values stacked to fixed shape
|
nodal_grad_x |
ndarray
|
Nodal constraint state gradients, shape
|
nodal_grad_u |
ndarray
|
Nodal constraint control gradients, shape
|
cross_g |
ndarray
|
Cross-node constraint values, shape |
cross_grad_X |
ndarray
|
Cross-node state gradients, shape |
cross_grad_U |
ndarray
|
Cross-node control gradients, shape |
lam_prox |
ndarray
|
Trust-region weights, shape |
lam_cost |
ndarray
|
Cost weight, scalar or shape |
lam_vc |
ndarray
|
Virtual-control penalty weights, shape |
lam_vb_nodal |
ndarray
|
Nodal virtual-buffer weights, shape |
lam_vb_cross |
ndarray
|
Cross-node virtual-buffer weights, shape |
x_init |
ndarray
|
Initial state, shape |
x_term |
ndarray
|
Terminal state, shape |
params |
dict
|
Runtime parameters dict from the outer solve loop. Threaded
here so JAX-pure assembly functions see the per-batch vmapped
parameter values (e.g. |
Source code in openscvx/solvers/ptr_solver.py
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 | |
SubproblemSolution
dataclass
¶
JAX-pure output of :meth:PTRSolver.iteration_callback.
All backends produce structurally-identical pytrees so the result composes
with lax.while_loop in the downstream batchable-problem work.
Attributes:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
Nodal state, shape |
u |
ndarray
|
Nodal control, shape |
nu |
ndarray
|
Virtual-control slack, shape |
nu_vb |
ndarray
|
Nodal virtual-buffer slacks stacked to |
nu_vb_cross |
ndarray
|
Cross-node virtual-buffer slacks, shape |
cost |
ndarray
|
Optimal objective value (scalar). |
status_code |
ndarray
|
:class: |
Source code in openscvx/solvers/ptr_solver.py
status_code_to_str(code: Union[int, jnp.ndarray, np.ndarray]) -> str
¶
Map a :class:StatusCode value (int or 0-d array) to its label.
status_str_to_code(status: str) -> StatusCode
¶
Map a backend-emitted status string to a :class:StatusCode.
Used by :class:CVXPyPTRSolver.iteration_callback to coerce CVXPy's
problem.status into the int32 form the JAX-pure result pytree
requires. Only the three definite outcomes ("optimal",
"infeasible", "unbounded") are recognized — every other label
CVXPy emits ("optimal_inaccurate", "solver_error", ...) collapses
to :attr:StatusCode.UNKNOWN.