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

Vb/issue95 #97

Merged
merged 5 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=45", "wheel", "oldest-supported-numpy", "scipy>=0.13.2", "setuptools_scm>=6.2", "cmake>=3.18"]
requires = ["setuptools>=64", "wheel", "oldest-supported-numpy", "scipy>=0.13.2", "setuptools_scm>=6.2", "cmake>=3.18"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
Expand Down
10 changes: 10 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def run(self):


class CmdCMakeBuild(build_ext):
def run(self):
super().run()
# For editable installs, after the extension(s) have been built, copy the 'codegen_src' folder
# from the temporary build folder to the source folder
if self.editable_mode:
codegen_target_folder = os.path.join('src', 'osqp', 'codegen', 'codegen_src')
if os.path.exists(codegen_target_folder):
shutil.rmtree(codegen_target_folder)
shutil.copytree(os.path.join(self.build_temp, 'codegen_src'), codegen_target_folder)

def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
thisdir = os.path.dirname(os.path.abspath(__file__))
Expand Down
9 changes: 9 additions & 0 deletions src/bindings.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ PYBIND11_MODULE(@OSQP_EXT_MODULE_NAME@, m) {
.value("OSQP_UNSOLVED", OSQP_UNSOLVED)
.export_values();

py::enum_<osqp_capabilities_type>(m, "osqp_capabilities_type", py::module_local())
.value("OSQP_CAPABILITY_DIRECT_SOLVER", OSQP_CAPABILITY_DIRECT_SOLVER)
.value("OSQP_CAPABILITY_INDIRECT_SOLVER", OSQP_CAPABILITY_INDIRECT_SOLVER)
.value("OSQP_CAPABILITY_CODEGEN", OSQP_CAPABILITY_CODEGEN)
.value("OSQP_CAPABILITY_UPDATE_MATRICES", OSQP_CAPABILITY_UPDATE_MATRICES)
.value("OSQP_CAPABILITY_DERIVATIVES", OSQP_CAPABILITY_DERIVATIVES);

m.def("osqp_capabilities", &osqp_capabilities);

py::class_<CSC>(m, "CSC", py::module_local())
.def(py::init<py::object>())
.def_readonly("m", &CSC::m)
Expand Down
23 changes: 19 additions & 4 deletions src/osqp/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ def __init__(self, *args, **kwargs):
def __str__(self):
return f'OSQP with algebra={self.algebra}'

@property
def capabilities(self):
return int(self.ext.osqp_capabilities())

def has_capability(self, capability: str):
try:
cap = int(self.ext.osqp_capabilities_type.__members__[capability])
except KeyError:
raise RuntimeError(f'Unrecognized capability {capability}')

return (self.capabilities & cap) != 0

@property
def solver_type(self):
return (
Expand Down Expand Up @@ -230,9 +242,9 @@ def update(self, **kwargs):

q, l, u = kwargs.get('q'), kwargs.get('l'), kwargs.get('u')
if l is not None:
l = np.maximum(l, -constant('OSQP_INFTY'))
l = np.maximum(l, -self.constant('OSQP_INFTY'))
if u is not None:
u = np.minimum(u, constant('OSQP_INFTY'))
u = np.minimum(u, self.constant('OSQP_INFTY'))

if q is not None or l is not None or u is not None:
self._solver.update_data_vec(q=q, l=l, u=u)
Expand Down Expand Up @@ -288,11 +300,11 @@ def solve(self, raise_error=False):
self._solver.solve()

info = self._solver.info
if info.status_val == constant('OSQP_NON_CVX', algebra=self.algebra):
if info.status_val == self.constant('OSQP_NON_CVX'):
info.obj_val = np.nan
# TODO: Handle primal/dual infeasibility

if info.status_val != constant('OSQP_SOLVED') and raise_error:
if info.status_val != self.constant('OSQP_SOLVED') and raise_error:
raise ValueError('Problem not solved!')

# Create a Namespace of OSQPInfo keys and associated values
Expand Down Expand Up @@ -332,6 +344,7 @@ def codegen(
compile=False,
):

assert self.has_capability('OSQP_CAPABILITY_CODEGEN'), 'This OSQP object does not support codegen'
assert parameters in ('vectors', 'matrices'), 'Unknown parameters specification'

defines = self.ext.OSQPCodegenDefines()
Expand Down Expand Up @@ -375,6 +388,8 @@ def adjoint_derivative(self, dx=None, dy_u=None, dy_l=None, as_dense=True, dP_as
Compute adjoint derivative after solve.
"""

assert self.has_capability('OSQP_CAPABILITY_DERIVATIVES'), 'This OSQP object does not support derivatives'

try:
results = self._derivative_cache['results']
except KeyError:
Expand Down