Skip to content

Add support for KNITRO solver#2873

Merged
PTNobel merged 12 commits intocvxpy:masterfrom
eminyouskn:master
Aug 1, 2025
Merged

Add support for KNITRO solver#2873
PTNobel merged 12 commits intocvxpy:masterfrom
eminyouskn:master

Conversation

@eminyouskn
Copy link
Copy Markdown
Contributor

Description

This PR adds support for the KNITRO solver.
To run the tests in CI, the KNITRO_CI environment variable needs to be set. Artelys can provide a license for this purpose, but I’m not sure whom to contact to arrange that.

Type of change

  • New feature (backwards compatible)
  • New feature (breaking API changes)
  • Bug fix
  • Other (Documentation, CI, ...)

Contribution checklist

  • Add our license to new files.
  • Check that your code adheres to our coding style.
  • Write unittests.
  • Run the unittests and check that they’re passing.
  • Run the benchmarks to make sure your change doesn’t introduce a regression.

…ms (#1)

* Add KNITRO solver to the list of available solvers

* Add KNITRO QP and Conic solver interfaces

* Add KNITRO citation to the citation dictionary

* Add KNITRO QP and Conic solver interface implementation

* Add KNITRO to doc

* Add KNITRO to ci for optional solvers
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jul 24, 2025

CLA assistant check
All committers have signed the CLA.

@PTNobel
Copy link
Copy Markdown
Collaborator

PTNobel commented Jul 24, 2025

Hello, you can reach out to me cvxpydevs@gmail.com with info on us getting a license for CI.

@eminyouskn
Copy link
Copy Markdown
Contributor Author

@PTNobel thanks.

@SteveDiamond
Copy link
Copy Markdown
Collaborator

I added the license under the "KNITRO_LICENSE" secret. Unfortunately secrets can't be accessed in MRs.

@eminyouskn
Copy link
Copy Markdown
Contributor Author

Thanks @SteveDiamond. Let me then rename the secret to KNITRO_LICENSE instead of KNITRO_CI

@eminyouskn
Copy link
Copy Markdown
Contributor Author

I noticed that KNITRO wasn't installed in the test_optional_solvers jobs. I assume this is because the secret KNITRO_LICENSE is not accessible from pull requests. Do you need me to do anything about that?

P.S.: On macOS, KNITRO is only supported on the ARM architecture, which is not the default configuration tested in your CI. Would it be possible to add this OS configuration to your CI setup?

@Transurgeon
Copy link
Copy Markdown
Collaborator

I noticed that KNITRO wasn't installed in the test_optional_solvers jobs. I assume this is because the secret KNITRO_LICENSE is not accessible from pull requests. Do you need me to do anything about that?

I think the secret licenses are only accessible when the pull request gets merged to master, but I am not entirely sure. I suppose it should be similar to how we deal with the MOSEK license.

P.S.: On macOS, KNITRO is only supported on the ARM architecture, which is not the default configuration tested in your CI. Would it be possible to add this OS configuration to your CI setup?

Yes! I definitely think we should add that. Maybe having an extra entry for macOS-latest should do the job.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 30, 2025

Benchmarks that have stayed the same:

   before           after         ratio
 [07d05b53]       [19ef24f8]
      941±0ms          1.02±0s     1.08  gini_portfolio.Cajas.time_compile_problem
      1.38±0s          1.47±0s     1.07  matrix_stuffing.ParamConeMatrixStuffing.time_compile_problem
      284±0ms          295±0ms     1.04  matrix_stuffing.ParamSmallMatrixStuffing.time_compile_problem
      570±0ms          585±0ms     1.03  simple_QP_benchmarks.ParametrizedQPBenchmark.time_compile_problem
      2.35±0s          2.39±0s     1.02  simple_LP_benchmarks.SimpleFullyParametrizedLPBenchmark.time_compile_problem
      4.59±0s          4.66±0s     1.02  huber_regression.HuberRegression.time_compile_problem
      22.3±0s          22.6±0s     1.01  sdp_segfault_1132_benchmark.SDPSegfault1132Benchmark.time_compile_problem
      939±0ms          951±0ms     1.01  simple_LP_benchmarks.SimpleScalarParametrizedLPBenchmark.time_compile_problem
      236±0ms          239±0ms     1.01  gini_portfolio.Murray.time_compile_problem
      13.0±0s          13.1±0s     1.01  finance.CVaRBenchmark.time_compile_problem
      5.33±0s          5.38±0s     1.01  semidefinite_programming.SemidefiniteProgramming.time_compile_problem
      5.00±0s          5.03±0s     1.01  optimal_advertising.OptimalAdvertising.time_compile_problem
      344±0ms          346±0ms     1.01  gini_portfolio.Yitzhaki.time_compile_problem
      1.66±0s          1.67±0s     1.00  tv_inpainting.TvInpainting.time_compile_problem
      843±0ms          844±0ms     1.00  simple_QP_benchmarks.LeastSquares.time_compile_problem
      241±0ms          241±0ms     1.00  high_dim_convex_plasticity.ConvexPlasticity.time_compile_problem
      11.4±0s          11.4±0s     1.00  simple_LP_benchmarks.SimpleLPBenchmark.time_compile_problem
      1.86±0s          1.86±0s     1.00  simple_QP_benchmarks.UnconstrainedQP.time_compile_problem
      297±0ms          297±0ms     1.00  slow_pruning_1668_benchmark.SlowPruningBenchmark.time_compile_problem
      253±0ms          253±0ms     1.00  simple_QP_benchmarks.SimpleQPBenchmark.time_compile_problem
      5.22±0s          5.14±0s     0.99  svm_l1_regularization.SVMWithL1Regularization.time_compile_problem
      1.10±0s          1.08±0s     0.98  finance.FactorCovarianceModel.time_compile_problem
      731±0ms          715±0ms     0.98  matrix_stuffing.ConeMatrixStuffingBench.time_compile_problem
      2.94±0s          2.86±0s     0.97  quantum_hilbert_matrix.QuantumHilbertMatrix.time_compile_problem
     45.2±0ms         43.9±0ms     0.97  matrix_stuffing.SmallMatrixStuffing.time_compile_problem

Copy link
Copy Markdown
Collaborator

@Transurgeon Transurgeon left a comment

Choose a reason for hiding this comment

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

thanks for making this PR @eminyouskn , I left a few comments/questions above. Other than that the interface is looking pretty good!

I had a related question for you: we are currently working on adding a new IPOPT interface for NLPs and I was wondering if the KNITRO NLP interface was similar to IPOPT's? We would definitely like to support KNITRO at some point down the line!

@eminyouskn
Copy link
Copy Markdown
Contributor Author

Thanks @Transurgeon for your comments. I've addressed most of them. Let me know if there's anything else that needs attention.

Regarding the NLP interface, I'm not familiar with the IPOPT implementation. However, if you'd like to add NLP support for KNITRO, we'd be happy to assist. KNITRO, for example, uses callbacks to handle general nonlinear functions.

solution = Solution(status, obj, primal_vars, dual_vars, attr)

# Free the Knitro context.
kn.KN_free(kc)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since we're cacheing the context should we be freeing it here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree. However, we're not using the cache yet. I plan to add that in the future, but for now, I think it's safe to free the context here. By the way the same thing apply for the conic interface.

@Transurgeon
Copy link
Copy Markdown
Collaborator

@eminyouskn I just merged the master cvxpy branch to your PR, which includes the new macos-14 ARM runners. Once this PR gets merged we should be able to see that it successfully tested the KNITRO solver (with the license).

@eminyouskn
Copy link
Copy Markdown
Contributor Author

@eminyouskn I just merged the master cvxpy branch to your PR, which includes the new macos-14 ARM runners. Once this PR gets merged we should be able to see that it successfully tested the KNITRO solver (with the license).

Thanks @Transurgeon for the PR. I tested it on my fork, and the tests pass for KNITRO. However, there's still an error related to SDPA.

@Transurgeon
Copy link
Copy Markdown
Collaborator

@eminyouskn I just merged the master cvxpy branch to your PR, which includes the new macos-14 ARM runners. Once this PR gets merged we should be able to see that it successfully tested the KNITRO solver (with the license).

Thanks @Transurgeon for the PR. I tested it on my fork, and the tests pass for KNITRO. However, there's still an error related to SDPA.

yeah that's fine, the error is definitely unrelated.

Copy link
Copy Markdown
Collaborator

@Transurgeon Transurgeon left a comment

Choose a reason for hiding this comment

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

Okay, I think this PR is ready to merge. @PTNobel could you have a sanity check please?

Copy link
Copy Markdown
Collaborator

@PTNobel PTNobel left a comment

Choose a reason for hiding this comment

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

Coursery read looks fine

@PTNobel PTNobel merged commit 9009cb9 into cvxpy:master Aug 1, 2025
51 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants