Skip to content

Conversation

@FBumann
Copy link
Contributor

@FBumann FBumann commented Dec 3, 2025

Add a registry for solver features in preparation for both quadratic constraints and SOS constraints

Also for future proofing and documentation

This is a proof of concept and should be discussed regarding the design and how far we should go regarding solver features (only features needed in linopy or more)

Look into the dev-scripts/ for details and usage for now

Important catch:
We should separate Solver supported features and if we implemented them. DIRECT_API might be better located in the IO of each solver, as its more of an implementation thing.

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

@FBumann FBumann mentioned this pull request Dec 3, 2025
4 tasks
@FBumann FBumann mentioned this pull request Dec 3, 2025
4 tasks
@FBumann
Copy link
Contributor Author

FBumann commented Dec 3, 2025

AI Based research

Solver Feature Research: SOS and Quadratic Constraints

This document summarizes research on solver support for SOS (Special Ordered Sets) and quadratic constraints.

SOS1/SOS2 Support

Special Ordered Sets are used in branch and bound methods for branching on sets of variables, rather than individual variables. They help speed up the search procedure by giving the algorithm more intelligent ways to explore the solution space.

  • SOS1: At most one variable in the set can be non-zero
  • SOS2: At most two variables can be non-zero, and if two are non-zero, they must be consecutive in the ordering

File Format Support

Both LP and MPS file formats support SOS constraints:

LP Format (Gurobi LP format):

SOS
sos1: S1 :: x1:1 x2:2 x3:3
sos2: S2 :: x4:8.5 x5:10.2 x6:18.3

MPS Format (CPLEX MPS format):

  • SOS section follows the BOUNDS section
  • S1 or S2 in columns 2-3 specifies the type
  • Variables and weights on subsequent lines

Solver Support Matrix

Solver SOS1 SOS2 Direct API LP File MPS File Notes
Gurobi Full support, PreSOS1/2Encoding parameters
CPLEX Full support
Xpress Full support
HiGHS Direct API only (cvt:sos option via AMPL)
SCIP Full support via reader_lp/reader_mps
CBC Full support via CbcSOS class
GLPK Must reformulate manually
MOSEK Must reformulate manually
COPT Direct API confirmed, file format unclear
MindOpt Direct API confirmed, file format unclear

Sources


Quadratic Constraints (QCP/QCQP)

Quadratically Constrained Programs (QCP) and Quadratically Constrained Quadratic Programs (QCQP) involve constraints with quadratic terms. Support varies significantly:

  • Convex QC: Constraints where the quadratic form is convex (e.g., x'Qx ≤ b where Q is positive semidefinite)
  • Non-convex QC: General quadratic constraints including bilinear terms (e.g., x*y ≤ b)
Solver Convex QC Non-convex QC Notes
Gurobi Global optimality for non-convex via NonConvex parameter
CPLEX Convex QCP only
Xpress Native support for MIQCP
MOSEK Excellent for conic/convex problems, uses QCP-based branch-and-bound
SCIP Performance varies, less competitive on large instances
HiGHS No quadratic constraint support (LP/QP/MIP only)
CBC LP/MIP only
GLPK LP/MIP only
COPT Convex QCP supported
MindOpt Limited information available

Performance Notes

From benchmark studies:

  • MOSEK and Gurobi solved most QCP instances within time limits
  • MOSEK uses QCP-based branch-and-bound resulting in very tight bounds
  • Xpress solved almost half of test instances
  • SCIP performance worsens on medium to large instances

Sources


Current Linopy Status

As of now, linopy does not support quadratic constraints. The QuadraticExpression.to_constraint() method raises NotImplementedError:

def to_constraint(self, sign: SignLike, rhs: SideLike) -> NotImplementedType:
    raise NotImplementedError(
        "Quadratic expressions cannot be used in constraints."
    )

SOS constraints are also not currently implemented in linopy.


Recommended Registry Features

Based on this research, the following features should be added to the solver capabilities registry:

SOS Constraints

SOS1 = auto()  # Special Ordered Set Type 1
SOS2 = auto()  # Special Ordered Set Type 2

Quadratic Constraints

QUADRATIC_CONSTRAINTS = auto()        # Convex quadratic constraints (QCP)
NONCONVEX_QUADRATIC_CONSTRAINTS = auto()  # Non-convex/bilinear constraints

Summary Table for Registry

Solver SOS1 SOS2 QC (convex) QC (non-convex)
gurobi
highs
glpk
cbc
cplex
xpress
scip
mosek
copt
mindopt

Note: ❓ indicates features that need verification before adding to the registry.

Copy link
Collaborator

@FabianHofmann FabianHofmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FBumann wonderful. this is why I love open-source. I really like that abstraction. let's pull it in as soon as you are ready (you can give me a go and I have a final look)

Comment on lines +1198 to +1201
if (
solver_supports(solver_name, SolverFeature.SOLUTION_FILE_NOT_NEEDED)
and not keep_files
):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess here we could initialize the solver instance and support the features functions on it, instead of relying on helper functions with solver_name passed. but happy to keep it like this for now.

@FabianHofmann FabianHofmann marked this pull request as ready for review December 8, 2025 16:45
@FabianHofmann FabianHofmann merged commit a1a2814 into PyPSA:master Dec 8, 2025
22 of 23 checks passed
@coroa
Copy link
Member

coroa commented Dec 9, 2025

Thank you, i was just not getting around to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants