Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solver 'CVXOPT' failed when solving LP #2417

Open
GrahamHulsey opened this issue Apr 18, 2024 · 3 comments
Open

Solver 'CVXOPT' failed when solving LP #2417

GrahamHulsey opened this issue Apr 18, 2024 · 3 comments

Comments

@GrahamHulsey
Copy link

GrahamHulsey commented Apr 18, 2024

CVXPY is unable to solve a simple LP with solver set to CVXOPT
I am running cvxpy and cvxopt in a virtual environment on Ubuntu 22.04.4. After successfully pip installing both cvxpy and cvxopt, I ran the following model:

import cvxpy as cp
import numpy as np
import cvxopt


n = 2
x = cp.Variable(n)

prob = cp.Problem(cp.Maximize(cp.sum(x)), [cp.sum(x) <= 10])
prob.solve(solver=cp.CVXOPT, verbose=True)

print("\nThe optimal value is", prob.value)
print("A solution x is")
print(x.value)
print("A dual solution is")
print(prob.constraints[0].dual_value)

This gives the following output:

CVXPY                                     
v1.4.3                                    

(CVXPY) Apr 17 09:46:13 PM: Your problem has 2 variables, 1 constraints, and 0 parameters.
(CVXPY) Apr 17 09:46:13 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Apr 17 09:46:13 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Apr 17 09:46:13 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
(CVXPY) Apr 17 09:46:13 PM: Your problem is compiled with the CPP canonicalization backend.

Compilation                                  

(CVXPY) Apr 17 09:46:13 PM: Compiling problem (target solver=CVXOPT).
(CVXPY) Apr 17 09:46:13 PM: Reduction chain: FlipObjective -> Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> CVXOPT
(CVXPY) Apr 17 09:46:13 PM: Applying reduction FlipObjective
(CVXPY) Apr 17 09:46:13 PM: Applying reduction Dcp2Cone
(CVXPY) Apr 17 09:46:13 PM: Applying reduction CvxAttr2Constr
(CVXPY) Apr 17 09:46:13 PM: Applying reduction ConeMatrixStuffing
(CVXPY) Apr 17 09:46:13 PM: Applying reduction CVXOPT
(CVXPY) Apr 17 09:46:13 PM: Finished problem compilation (took 2.648e-03 seconds).

Numerical solver                               

(CVXPY) Apr 17 09:46:13 PM: Invoking solver CVXOPT  to obtain a solution.
Traceback (most recent call last):
  File "/home/me/test2.py", line 10, in <module>
    prob.solve(solver=cp.CVXOPT, verbose=True)
  File "/home/me/venv/lib/python3.10/site-packages/cvxpy/problems/problem.py", line 503, in solve
    return solve_func(self, *args, **kwargs)
  File "/home/me/venv/lib/python3.10/site-packages/cvxpy/problems/problem.py", line 1086, in _solve
    self.unpack_results(solution, solving_chain, inverse_data)
  File "/home/me/venv/lib/python3.10/site-packages/cvxpy/problems/problem.py", line 1411, in unpack_results
    raise error.SolverError(
cvxpy.error.SolverError: Solver 'CVXOPT' failed. Try another solver, or solve with verbose=True for more information.

When the same model is run with the default ECOS solver, the correct answer is returned.

Looking through the traceback in cvxpy/problems/problem.py the error is being raised beginning at line 1410 in the problem.py file:

if solution.status in s.ERROR:
            raise error.SolverError(
                    "Solver '%s' failed. " % chain.solver.name() +
                    "Try another solver, or solve with verbose=True for more "
                    "information.")

I am unsure what is causing this, but suspect it is either a syntax issue in the model formulation or a default setting on the CVXOPT solver, but it seems that either documentation is missing, a default setting is insufficient for this use case, or both, and that should be flagged for users in the documentation.

EDIT: adding that the CVXOPT solver works in this environment, for example solving the cvxpy LP example with solver=cp.CVXOPT returns correct answer.

@chofchof
Copy link
Contributor

Your example will be reduced to the following CVXOPT linear cone program.

from cvxopt import matrix, solvers

c = matrix(-1.0, (2, 1))
G = matrix(1.0, (1, 2))
h = matrix(10.0)
solvers.conelp(c, G, h)

However it raises ValueError: Rank(A) < p or Rank([G; A]) < n. Here, $A$ is empty so that the rank of $[G; A]$ is less than $n=2$.

If you replace x = cp.Variable(n) with x = cp.Variable(n, nonneg=True), you will get a result.

-------------------------------------------------------------------------------
                                Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Apr 18 01:59:31 PM: Invoking solver CVXOPT  to obtain a solution.
     pcost       dcost       gap    pres   dres   k/t
 0: -6.6667e+00 -2.0000e+01  1e+01  0e+00  2e-16  1e+00
 1: -8.7325e+00 -1.0100e+01  1e+00  4e-16  1e-16  1e-01
 2: -9.9873e+00 -1.0005e+01  2e-02  1e-16  2e-16  2e-03
 3: -9.9999e+00 -1.0000e+01  2e-04  5e-16  4e-16  2e-05
 4: -1.0000e+01 -1.0000e+01  2e-06  3e-16  3e-16  2e-07
Optimal solution found.
-------------------------------------------------------------------------------
                                    Summary
-------------------------------------------------------------------------------
(CVXPY) Apr 18 01:59:31 PM: Problem status: optimal
(CVXPY) Apr 18 01:59:31 PM: Optimal value: 1.000e+01
(CVXPY) Apr 18 01:59:31 PM: Compilation took 3.182e-03 seconds
(CVXPY) Apr 18 01:59:31 PM: Solver (including time spent in interface) took 2.314e-03 seconds

The optimal value is 9.999998732501222
A solution x is
[4.99999937 4.99999937]
A dual solution is
1.0000000462353087

@GrahamHulsey
Copy link
Author

Interesting, thanks for your digging. I believe this issue is essentially a duplicate of #1588 , as reporting the CVXOPT ValueError would be helpful for debugging. I will mark as duplicate

@GrahamHulsey
Copy link
Author

Duplicate of #1588

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

No branches or pull requests

3 participants