Skip to content

Commit 386e471

Browse files
authored
Make solver_path fall back on non-optimal statuses (cvxpy#3324)
* Make solver_path reject non-optimal statuses * Address solver_path review feedback
1 parent 59cbc6c commit 386e471

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

cvxpy/problems/problem.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,14 @@ def _solve_solver_path(self, solve_func, solvers:list[tuple[str, dict] | str],
575575
self, *args, solver=solver_name, **solver_kwargs, **kwargs)
576576
else:
577577
raise ValueError(ENTRY_ERROR_MSG)
578-
s.LOGGER.info("Solver %s succeeds", solver_name)
578+
if self.status == s.OPTIMAL:
579+
s.LOGGER.info("Solver %s succeeds", solver_name)
580+
else:
581+
s.LOGGER.info("Solver %s returned non-optimal status %s",
582+
solver_name,
583+
self.status
584+
)
585+
continue
579586
return solution
580587
except error.SolverError as e:
581588
s.LOGGER.info("Solver %s failed: %s", solver_name, e)

cvxpy/tests/test_problem.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,15 +1542,18 @@ def test_solve_solver_path(self) -> None:
15421542
self.assertIsNotNone(Problem(cp.Minimize(
15431543
cp.sum_squares(cp.matmul(A, cp.Variable(40)) - b))).solve(
15441544
solver_path=solvers))
1545-
1545+
# valid input, non-optimal first solver falls back to next solver
1546+
problem = Problem(cp.Minimize(
1547+
cp.sum_squares(cp.matmul(A, cp.Variable(40)) - b)))
1548+
problem.solve(solver_path=[(s.OSQP, {'max_iter': 1}), s.CLARABEL])
1549+
self.assertEqual(problem.solver_stats.solver_name, s.CLARABEL)
1550+
self.assertEqual(problem.status, s.OPTIMAL)
15461551
# valid input, raise SolverError
15471552
solvers = [(s.OSQP, {'max_iter':1})]
1548-
15491553
with self.assertRaises(SolverError):
15501554
Problem(cp.Minimize(cp.quad_form(cp.Variable(1) + 1, np.array([[-1]]), True))).solve(
15511555
solver_path=solvers
15521556
)
1553-
15541557
# invalid input, raise ValueError
15551558
solvers_invalid_inner_input = [{'str':{}}, 'str', [], [1], [()], [(1)], [(1,{})],
15561559
[(s.OSQP,[])], [(s.OSQP,)]]

0 commit comments

Comments
 (0)