Skip to content

Commit

Permalink
make qp solver configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-sp committed Apr 17, 2024
1 parent c5c7af6 commit 9326cfb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion scripts/example_residual.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
gI = [ObjectiveOrConstraint(const, dim_out=1)]
gE = []

options = {"num_points_obj": 5, "num_points_gI": 5}
options = {"num_points_obj": 5, "num_points_gI": 5, "qp_solver": "osqp"}

problem = SQPGS(f, gI, gE, x0=None, tol=1e-10, max_iter=500, options=options, verbose=True)

Expand Down
1 change: 1 addition & 0 deletions src/ncopt/sqpgs/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Dotdict(dict):
"num_points_obj": 2,
"num_points_gI": 3,
"num_points_gE": 4,
"qp_solver": "osqp",
}

DEFAULT_ARG = Dotdict(_defaults)
Expand Down
15 changes: 12 additions & 3 deletions src/ncopt/sqpgs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def solve(self):
pE = np.repeat(pE_, self.dimE)
###############################################################

self.SP = SubproblemSQPGS(self.dim, p0, pI, pE, self.assert_tol)
self.SP = SubproblemSQPGS(self.dim, p0, pI, pE, self.assert_tol, self.options["qp_solver"])

E_k = np.inf # for stopping criterion
x_kmin1 = None # last iterate
Expand Down Expand Up @@ -507,10 +507,18 @@ def stop_criterion(gI, gE, g_k, SP, gI_k, gE_k, V_gI, V_gE, nI_, nE_):

# %%

CVXPY_SOLVER_DICT = {"osqp": cp.OSQP, "clarabel": cp.CLARABEL}


class SubproblemSQPGS:
def __init__(
self, dim: int, p0: np.ndarray, pI: np.ndarray, pE: np.ndarray, assert_tol: float
self,
dim: int,
p0: np.ndarray,
pI: np.ndarray,
pE: np.ndarray,
assert_tol: float,
solver: str,
) -> None:
"""
dim : solution space dimension
Expand All @@ -528,6 +536,7 @@ def __init__(

self.d = cp.Variable(self.dim)
self._problem = None
self._qp_solver = CVXPY_SOLVER_DICT.get(solver, cp.OSQP)

@property
def nI(self) -> int:
Expand Down Expand Up @@ -638,7 +647,7 @@ def solve(
objective = objective + cp.sum(r_E)

problem = cp.Problem(cp.Minimize(objective), constraints)
problem.solve(solver=cp.OSQP, verbose=True)
problem.solve(solver=self._qp_solver, verbose=False)

assert problem.status in {cp.OPTIMAL, cp.OPTIMAL_INACCURATE}
self._problem = problem
Expand Down

0 comments on commit 9326cfb

Please sign in to comment.