Skip to content

Conversation

@FabianHofmann
Copy link
Collaborator

Summary

This PR adds support for the Artelys Knitro solver to linopy.

Changes

  • Added automatic detection of the knitro Python package to the available solvers list
  • Implemented Knitro solver class with support for solving MPS/LP files
  • Registered Knitro capabilities in the solver registry (quadratic objectives, LP file names, no solution file needed)
  • Added tests for the Knitro solver (properly skip when package not installed)
  • Mapped Knitro status codes to linopy's Status system

Implementation Details

The Knitro solver implementation follows the existing patterns in linopy:

  • File-based solver using MPS/LP files (similar to CPLEX, COPT)
  • Proper status code mapping for optimization results
  • Solution and dual value extraction using Knitro's Python API
  • Support for solver options and logging

Testing

  • Added unit tests that verify Knitro functionality
  • Tests properly skip when Knitro package is not installed
  • All existing tests continue to pass

Notes

  • Direct API support (solve_problem_from_model) is not implemented in this initial version but can be added later
  • Knitro is a powerful nonlinear optimization solver that also handles linear and quadratic problems efficiently
  • Users need to install the knitro package separately and have a valid license to use this solver

Closes #[issue_number] (if applicable)

- Add Knitro detection to available_solvers list
- Implement Knitro solver class with MPS/LP file support
- Add solver capabilities for Knitro (quadratic, LP names, no solution file)
- Add tests for Knitro solver functionality
- Map Knitro status codes to linopy Status system
@finozzifa
Copy link
Collaborator

Great work @FabianHofmann!

@finozzifa
Copy link
Collaborator

@FabianHofmann: I have modified the code (thanks Daniele Lerede for your contributions and support).

Could you please review it and let me know if it can be merged master?

Thanks :)

finozzifa added a commit to open-energy-transition/solver-benchmark that referenced this pull request Feb 11, 2026
The goal of this pull request is add support for the `knitro` solver in
the `run_solver.py` script.

The interface between `linopy` and the `knitro` Python API is introduced
in this [pull request.](PyPSA/linopy#532)

## Changes

### `run_solver.py`

I have added a `knitro` specification for the following functions:
- `def get_solver`
- `def def is_mip_problem`
- `def get_reported_runtime`
- `def get_duality_gap`

## Test
I tested the integration between knitro and linopy and the changes to
run_solver.py on the benchmark
[pypsa-eur-elec-trex-3-12h.lp](https://storage.googleapis.com/solver-benchmarks/pypsa-eur-elec-trex-3-12h.lp).

I get:
`{"runtime": 4.476548100999992, "reported_runtime": 3.8160810470581055,
"status": "ok", "condition": "optimal", "objective": 7398198432.386027,
"duality_gap": null, "max_integrality_violation": null}`

The value of the objective function is compatible to those found
[here](https://openenergybenchmark.org/dashboard/benchmark-set/pypsa-eur-elec-trex)

---------

Co-authored-by: Daniele Lerede <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

2 participants