cvxpy / cvxpy

A Python-embedded modeling language for convex optimization problems.
https://www.cvxpy.org
Apache License 2.0
5.22k stars 1.05k forks source link

Strange error of CVXPY 1.5.2 #2495

Open Yingrui-Z opened 4 days ago

Yingrui-Z commented 4 days ago

I am running my code with cvxpy1.5.2, and I get the following error:

self.prob.solve(solver=CP_solver, verbose=solver_verbose)
  File "C:\Users\X\AppData\Local\Programs\Python\Python310\lib\site-packages\cvxpy\problems\problem.py", line 503, in solve
    return solve_func(self, *args, **kwargs)
  File "C:\Users\X\AppData\Local\Programs\Python\Python310\lib\site-packages\cvxpy\problems\problem.py", line 1086, in _solve
    solution = solving_chain.solve_via_data(
  File "C:\Users\X\AppData\Local\Programs\Python\Python310\lib\site-packages\cvxpy\reductions\solvers\solving_chain.py", line 473, in solve_via_data
    return self.solver.solve_via_data(data, warm_start, verbose,
  File "C:\Users\X\AppData\Local\Programs\Python\Python310\lib\site-packages\cvxpy\reductions\solvers\qp_solvers\gurobi_qpif.py", line 232, in solve_via_data
    model.setMObjective(0.5 * P, q, 0.0)
  File "src\\gurobipy\\model.pxi", line 4400, in gurobipy.Model.setMObjective
gurobipy.GurobiError: Q and x dimensions do not match

But when I use cvxpy 1.4.2 without any changes on the code, this error is not showing.

Transurgeon commented 4 days ago

@Yingrui-Z thank you for raising this error! If you able to do so, could you share a minimal example which reproduces the error?

The main changes from cvxpy 1.4.2 to 1.5.2 were the NumPy migration changes, so that could be the reason this is happening. Although the specific error you showed hasn't been encountered before.

Yingrui-Z commented 3 days ago

Thanks for your quick response! I have been using cvxpy for 4 years, and this is the first time I have encountered this error. The code is for an academic paper in progress, and it's a big project, so I am sorry for not being able to share the whole project. I will try to extract a small optimization case that reproduces the error. Besides, I have seen another strange error while using cvxpy 1.5.2.

  File "C:\Users\X\Desktop\CVXPY debug1.5.2\test.py", line 165, in DN_ES_time_test
    prob.solve(solver=cp.GUROBI, verbose=False)
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\problems\problem.py", line 503, in solve
    return solve_func(self, *args, **kwargs)
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\problems\problem.py", line 1073, in _solve
    data, solving_chain, inverse_data = self.get_problem_data(
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\problems\problem.py", line 696, in get_problem_data
    data, inverse_data = solving_chain.apply(self, verbose)
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\reductions\chain.py", line 76, in apply
    problem, inv = r.apply(problem)
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\reductions\solvers\qp_solvers\gurobi_qpif.py", line 69, in apply
    data, inv_data = super(GUROBI, self).apply(problem)
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\reductions\solvers\qp_solvers\qp_solver.py", line 78, in apply
    P, q, d, AF, bg = problem.apply_parameters()
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\reductions\qp2quad_form\qp_matrix_stuffing.py", line 184, in apply_parameters
    self.reduced_P.cache(keep_zeros)
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\reductions\utilities.py", line 141, in cache
    canonInterface.reduce_problem_data_tensor(
  File "C:\Users\X\Desktop\CVXPY debug1.5.2\.venv\lib\site-packages\cvxpy\cvxcore\python\canonInterface.py", line 130, in reduce_problem_data_tensor
    n_constr = A.shape[0] // (n_cols)
OverflowError: Python int too large to convert to C long

Thanks again for addressing these questions!

Transurgeon commented 2 days ago

Hi @Yingrui-Z , for the second issue you just posted, I think you can try to use the SCIPY canon backend as a parameter to the solve function. i.e problem.solve(canon_backend=cp.SCIPY_CANON_BACKEND). This would allow you to avoid the cvxcore pathway and could possibly resolve your issue.

Otherwise, I would suggest you to stick with 1.4.2 if you are noticing any issues as 1.5.2 didn't bring any new performance improvements and hence is not mandatory.