Skip to content

feat: Quadratic constraints#523

Closed
FBumann wants to merge 0 commit into
PyPSA:masterfrom
fluxopt:master
Closed

feat: Quadratic constraints#523
FBumann wants to merge 0 commit into
PyPSA:masterfrom
fluxopt:master

Conversation

@FBumann
Copy link
Copy Markdown
Collaborator

@FBumann FBumann commented Nov 26, 2025

Closes #444

Changes proposed in this Pull Request

This PR adds comprehensive quadratic constraints support to linopy, enabling optimization problems of the form x'Qx + a'x ≤ b.

New Features

Core quadratic constraint infrastructure:

  • QuadraticConstraint class for storing and manipulating quadratic constraints (linopy/constraints.py)
  • QuadraticConstraints container class with dict-like access
  • Model.add_quadratic_constraints() method for creating constraints
  • Model.quadratic_constraints property for accessing constraints
  • Model.has_quadratic_constraints property for checking if model contains QCs

Solver support:

  • Gurobi direct API integration for quadratic constraints
  • MOSEK direct API integration for quadratic constraints (convex only)
  • Centralized solver capability checking via quadratic_constraint_solvers and nonconvex_quadratic_constraint_solvers

File I/O:

  • LP file export with QCMATRIX sections for quadratic constraints
  • MPS file export with quadratic constraints (via Gurobi)
  • NetCDF I/O for models with quadratic constraints

Matrix representations:

  • Quadratic constraint matrix accessors for solver interfaces
  • Integration with existing matrix infrastructure

Additional:

  • Dual value retrieval for quadratic constraints (convex problems)
  • Example notebook demonstrating quadratic constraint usage

Before Merging:

Remove dev-scripts

The dev-scripts/ directory contains planning documents that were committed but should be removed before merging (per CLAUDE.md, dev-scripts is for non-tracked
temporary code). Would you like me to create a commit removing those files?

Review and documentation

As this is quite a big feature, i would be happy if someone that understands the codebase more deeply than i do goes into detail and verifies my code, especially the matrix computation. Also, including examples and explanations into the docs would be appropriate

Solver capabilities

Im not entirely sure if SCIP supports MIQCQP. In my tests i got an LP read error.
Dual values can only be retrived for convex models. For gurobi, we need to set QCPDual=1. Other solvers might need other settings. this is not set automatically.

Follow Ups

  1. Constraint modification methods (modify_rhs, modify_coeffs) - Medium priority
  2. Convexity checking - Low priority
  3. Documentation - Low priority

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.

@FabianHofmann
Copy link
Copy Markdown
Collaborator

wonderful initiative! let me know when I should take a first look

@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Nov 26, 2025

@FabianHofmann Its ready for an initial review. I would be happy if you give me some quick feedback on what functionality is missing (if some), so i wont get lost in details. I will revisit the new tests tomorrow.

@FBumann FBumann marked this pull request as ready for review November 26, 2025 10:46
@FBumann FBumann changed the title Quadratic constraints feat: Quadratic constraints Nov 26, 2025
@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Nov 26, 2025

From my analysis the QuadraticConstraint class is missing several wrapped xarray methods that Constraint has:

  • sel, isel, loc (for indexing)
  • where, fillna
  • rename, rename_dims, swap_dims, set_index
  • assign, assign_attrs, assign_coords
  • broadcast_like, chunk, drop_sel, drop_isel
  • expand_dims, shift, roll, reindex, reindex_like, stack

These are definitely a todo imo

@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Nov 26, 2025

From my analysis the QuadraticConstraint class is missing several wrapped xarray methods that Constraint has:

  • sel, isel, loc (for indexing)
  • where, fillna
  • rename, rename_dims, swap_dims, set_index
  • assign, assign_attrs, assign_coords
  • broadcast_like, chunk, drop_sel, drop_isel
  • expand_dims, shift, roll, reindex, reindex_like, stack

These are definitely a todo imo

resolved

@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Nov 26, 2025

@FabianHofmann My PR includes a "fix" regarding the expression.shape attribute. It excludes the _term dim. Please verify that this is actually correct. Else i need to adjust the quadratic_expression.shape

@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Dec 3, 2025

Consider merging #528 beforehand and update this accordingly for better solver support checking and better error messages

@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Jan 19, 2026

I didnt find the the time to continue on this.
Is it a priority for any of you? Or are you hesitant?

@FabianHofmann
Copy link
Copy Markdown
Collaborator

All good! This is not pressing. But we might want to tackle it in the upcoming weeks. I'll let you know as soon as we prioritize this

@FBumann FBumann added the enhancement New feature or request label Mar 18, 2026
@FBumann FBumann closed this Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quadratic Constraints and/or objective

2 participants