solvers
Convex subproblem solvers for trajectory optimization.
This module provides implementations of convex subproblem solvers used within SCvx algorithms. At each iteration of a successive convexification algorithm, the non-convex problem is approximated by a convex subproblem, which is then solved using one of these solver backends.
All solvers inherit from :class:ConvexSolver, enabling pluggable solver
implementations and custom backends:
class ConvexSolver(ABC):
@abstractmethod
def create_variables(self, N, x_unified, u_unified, jax_constraints) -> None:
'''Create backend-specific optimization variables (called once).'''
...
@abstractmethod
def initialize(self, lowered, settings) -> None:
'''Build the convex subproblem structure (called once).'''
...
@abstractmethod
def solve(self, state, params, settings) -> Any:
'''Update parameters and solve (called each iteration).'''
...
This architecture enables users to implement custom solver backends such as:
- Direct Clarabel solver (Rust-based, GPU-capable)
- QPAX (JAX-based QP solver for end-to-end differentiability)
- OSQP direct interface (specialized for QP structure)
- Custom embedded solvers for real-time applications
- Research solvers with specialized structure exploitation
Note
Solvers own their optimization variables (e.g., CVXPySolver.ocp_vars).
The lowering process calls solver.create_variables() before constraint
lowering, then solver.initialize() after. See :mod:openscvx.solvers.base
for the interface details.
ConvexSolver
¶
Bases: ABC
Abstract base class for convex subproblem solvers.
This class defines the interface for solvers that handle the convex subproblems generated at each iteration of a successive convexification algorithm.
The solver lifecycle has two phases:
Setup (called once):
- create_variables: Create backend-specific variables
- initialize: Build the problem structure using lowered constraints
Per-iteration (called each SCP iteration):
- update_dynamics_linearization: Set linearization point and dynamics matrices
- update_constraint_linearizations: Set constraint values and gradients
- update_penalties: Set penalty weights
- solve: Solve and return results
Example
Implementing a custom solver::
class MySolver(ConvexSolver):
def create_variables(self, N, x_unified, u_unified, jax_constraints):
self._vars = create_my_variables(N, x_unified, ...)
def initialize(self, lowered, settings):
self._prob = build_my_problem(self._vars, lowered, settings)
def update_dynamics_linearization(self, **kwargs):
# Set x_bar, u_bar, A_d, B_d, etc.
...
def update_constraint_linearizations(self, **kwargs):
# Set constraint function values and gradients
...
def update_penalties(self, **kwargs):
# Set lam_prox, lam_cost, lam_vc, lam_vb
...
def solve(self):
self._prob.solve()
return MyResult(...)
Source code in openscvx/solvers/base.py
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 | |
citation() -> List[str]
abstractmethod
¶
Return BibTeX citations for this solver.
Implementations should return a list of BibTeX entry strings for the papers that should be cited when using this solver.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of BibTeX citation strings. |
Source code in openscvx/solvers/base.py
create_variables(N: int, x_unified: UnifiedState, u_unified: UnifiedControl, jax_constraints: LoweredJaxConstraints) -> None
abstractmethod
¶
Create backend-specific optimization variables.
This method creates the optimization variables (decision variables and parameters) for this solver's backend. Called once during problem setup, before constraint lowering.
The solver should store its variables on self for use in subsequent
initialize() and solve() calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
int
|
Number of discretization nodes |
required |
x_unified
|
UnifiedState
|
Unified state interface with dimensions and scaling bounds |
required |
u_unified
|
UnifiedControl
|
Unified control interface with dimensions and scaling bounds |
required |
jax_constraints
|
LoweredJaxConstraints
|
Lowered JAX constraints (for sizing linearization params) |
required |
Source code in openscvx/solvers/base.py
get_stats() -> dict
abstractmethod
¶
Get solver statistics for diagnostics and printing.
Returns:
| Type | Description |
|---|---|
dict
|
Dict containing solver statistics. Expected keys:
- |
Source code in openscvx/solvers/base.py
initialize(lowered: LoweredProblem, settings: Config) -> None
abstractmethod
¶
Build the convex subproblem structure.
This method constructs the optimization problem once, using CVXPy Parameters (or equivalent) for values that change each iteration. Called once during problem setup, not at each SCP iteration.
The solver should store its problem representation on self for use
in subsequent solve() calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lowered
|
LoweredProblem
|
Lowered problem containing:
- |
required |
settings
|
Config
|
Configuration object with solver settings |
required |
Source code in openscvx/solvers/base.py
solve() -> Any
abstractmethod
¶
Solve the convex subproblem and return results.
Called at each SCP iteration after updating linearization and penalties. Returns a solver-specific result object containing the solution.
Returns:
| Type | Description |
|---|---|
Any
|
Solver-specific result object (e.g., |
Source code in openscvx/solvers/base.py
update_boundary_conditions(**kwargs) -> None
abstractmethod
¶
Update boundary condition parameters.
Called once during algorithm initialization to set initial and terminal state constraints.
The specific parameters depend on the solver implementation. See concrete solver classes for expected arguments.
Source code in openscvx/solvers/base.py
update_constraint_linearizations(**kwargs) -> None
abstractmethod
¶
Update linearized constraint values and gradients.
Called at each SCP iteration before solve() to set constraint
function values and gradients at the current linearization point.
The specific parameters depend on the solver implementation. See concrete solver classes for expected arguments.
Source code in openscvx/solvers/base.py
update_dynamics_linearization(**kwargs) -> None
abstractmethod
¶
Update dynamics linearization point and matrices.
Called at each SCP iteration before solve() to set the current
linearization point and discretized dynamics matrices.
The specific parameters depend on the solver implementation. See concrete solver classes for expected arguments.
Source code in openscvx/solvers/base.py
update_penalties(**kwargs) -> None
abstractmethod
¶
Update SCP penalty weights.
Called at each SCP iteration before solve() to set the current
penalty weights for trust region, virtual control, and virtual buffer.
The specific parameters depend on the solver implementation. See concrete solver classes for expected arguments.
Source code in openscvx/solvers/base.py
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
CVXPy-based convex subproblem solver for the PTR algorithm.
This solver uses CVXPy's modeling language to construct and solve the convex subproblems generated at each SCP iteration. It supports multiple backend solvers (CLARABEL, ECOS, MOSEK, etc.) and optional code generation via cvxpygen for improved performance.
The solver builds the problem structure once during initialize(), using
CVXPy Parameters for values that change each iteration. The solve()
method then solves and returns a structured PTRSolveResult.
The cost and constraint formulations are defined in the cost() and
constraints() methods, which can be overridden in subclasses to
customize the convex subproblem. For example::
class MyPTRSolver(PTRSolver):
def cost(self, settings, lowered):
c = super().cost(settings, lowered)
c += my_extra_term(self._ocp_vars)
return c
Future Backend Support
When adding a new backend (QPAX, COCO, etc.), this class should be refactored:
- Rename
PTRSolvertoCVXPyPTRSolver - Extract
PTRSolveras an abstract base class defining the PTR interface (update_dynamics_linearization,update_constraint_linearizations,update_penalties,solvereturningPTRSolveResult) - Have
CVXPyPTRSolverand the new backend (e.g.,QPAXPTRSolver) inherit from the abstractPTRSolver
This keeps the algorithm backend-agnostic while allowing multiple solver implementations for the PTR formulation.
Example
Using PTRSolver with the SCP framework::
solver = PTRSolver()
solver.create_variables(N, x_unified, u_unified, jax_constraints)
solver.initialize(lowered, settings)
# Each iteration (parameter updates done by algorithm):
result = solver.solve()
x_sol = result.x # Unscaled state trajectory
Attributes:
| Name | Type | Description |
|---|---|---|
ocp_vars |
CVXPyVariables
|
The CVXPy variables and parameters (available after create_variables()) |
Source code in openscvx/solvers/ptr_solver.py
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 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 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 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 | |
ocp_vars: CVXPyVariables
property
¶
The CVXPy variables and parameters.
Returns:
| Type | Description |
|---|---|
CVXPyVariables
|
The CVXPyVariables dataclass, or None if create_variables() not called. |
__init__()
¶
Initialize PTRSolver with unset problem.
Call create_variables() then initialize() to build the problem structure.
Source code in openscvx/solvers/ptr_solver.py
citation() -> List[str]
¶
Return BibTeX citations for CVXPy.
Returns:
| Type | Description |
|---|---|
List[str]
|
List containing BibTeX entries for CVXPy and DCCP papers. |
Source code in openscvx/solvers/ptr_solver.py
constraints(settings: Config, lowered: LoweredProblem) -> list
¶
Build the constraint list for the convex subproblem.
Constructs all PTR constraints including:
- Linearized nodal constraints (from JAX-lowered nonconvex constraints)
- Linearized cross-node constraints
- Convex constraints (already lowered to CVXPy)
- Boundary conditions (fixed initial/terminal states)
- Uniform time grid constraints
- State and control deviation definitions
- Linearized dynamics
- State and control box constraints
- CTCS constraints
Override this method in subclasses to customize the constraint
formulation. Use super().constraints(settings, lowered) to include
the standard PTR constraints and extend them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Config
|
Configuration object with solver settings |
required |
lowered
|
LoweredProblem
|
Lowered problem containing lowered constraints |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of CVXPy constraints. |
Source code in openscvx/solvers/ptr_solver.py
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 | |
cost(settings: Config, lowered: LoweredProblem) -> cp.Expression
¶
Build the cost expression for the convex subproblem.
Constructs the PTR objective function including:
- Boundary condition costs (Minimize/Maximize state components)
- Trust region penalty (deviation from linearization point)
- Virtual control penalty (dynamics defect relaxation)
- Virtual buffer penalty (nonconvex constraint violation relaxation)
Override this method in subclasses to customize the cost formulation.
Use super().cost(settings, lowered) to include the standard PTR
cost terms and add to them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Config
|
Configuration object with solver settings |
required |
lowered
|
LoweredProblem
|
Lowered problem containing constraint structure |
required |
Returns:
| Type | Description |
|---|---|
Expression
|
CVXPy expression representing the total cost to minimize. |
Source code in openscvx/solvers/ptr_solver.py
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 | |
create_variables(N: int, x_unified: UnifiedState, u_unified: UnifiedControl, jax_constraints: LoweredJaxConstraints) -> None
¶
Create CVXPy optimization variables.
Creates all CVXPy Variable and Parameter objects needed for the optimal control problem. This includes state/control variables, dynamics parameters, constraint linearization parameters, and scaling matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
int
|
Number of discretization nodes |
required |
x_unified
|
UnifiedState
|
Unified state interface with dimensions and scaling bounds |
required |
u_unified
|
UnifiedControl
|
Unified control interface with dimensions and scaling bounds |
required |
jax_constraints
|
LoweredJaxConstraints
|
Lowered JAX constraints (for sizing linearization params) |
required |
Source code in openscvx/solvers/ptr_solver.py
get_stats() -> dict
¶
Get solver statistics for diagnostics and printing.
Returns:
| Type | Description |
|---|---|
dict
|
Dict containing:
- |
Source code in openscvx/solvers/ptr_solver.py
initialize(lowered: LoweredProblem, settings: Config) -> None
¶
Build the CVXPy optimal control problem.
Constructs the complete optimization problem by calling cost() and
constraints() to build the objective and constraint formulations,
then assembles them into a CVXPy Problem.
If cvxpygen is enabled in settings, generates compiled solver code for improved performance.
Note
create_variables() must be called before this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lowered
|
LoweredProblem
|
Lowered problem containing:
- |
required |
settings
|
Config
|
Configuration object with solver settings |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If create_variables() has not been called. |
Source code in openscvx/solvers/ptr_solver.py
solve() -> PTRSolveResult
¶
Solve the convex subproblem and return structured results.
Call update_dynamics_linearization(), update_constraint_linearizations(),
and update_penalties() before calling this method.
Returns:
| Type | Description |
|---|---|
PTRSolveResult
|
PTRSolveResult containing unscaled trajectories, slack variables, |
PTRSolveResult
|
cost, and solver status. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If initialize() has not been called. |
Source code in openscvx/solvers/ptr_solver.py
update_boundary_conditions(x_init: np.ndarray = None, x_term: np.ndarray = None) -> None
¶
Update boundary condition parameters.
Sets initial and/or terminal state constraints. Only sets parameters that exist in the problem (some problems may not have both).
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
¶
Update linearized constraint values and gradients.
Sets constraint function values and gradients at the current linearization point for both nodal and cross-node constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodal
|
List[dict]
|
List of dicts for nodal constraints, each containing:
- |
None
|
cross_node
|
List[dict]
|
List of dicts for cross-node constraints, 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) -> None
¶
Update dynamics linearization point and matrices.
Sets the current linearization point (previous iterate) and the discretized dynamics matrices for the convex subproblem.
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 dynamics, shape (N-1, n_states) |
required |
Source code in openscvx/solvers/ptr_solver.py
update_penalties(lam_prox: float, lam_cost: float, lam_vc: np.ndarray, lam_vb: float) -> None
¶
Update SCP penalty weights.
Sets the penalty weights that balance competing objectives in the PTR convex subproblem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lam_prox
|
float
|
Trust region weight (penalizes deviation from linearization point) |
required |
lam_cost
|
float
|
Cost function weight |
required |
lam_vc
|
ndarray
|
Virtual control penalty weights, shape (N-1, n_states) |
required |
lam_vb
|
float
|
Virtual buffer penalty weight (for constraint violations) |
required |