Cvxpy: OSQP solver: Problem contains unspecified parameters.

Created on 20 Nov 2018  路  11Comments  路  Source: cvxgrp/cvxpy

def _lstsq_initial(self, z):
        fodf_sh = csd._solve_cholesky(self._P, z)
        s = np.dot(self._reg, fodf_sh)
        fodf_sh_ = cvx.Variable(fodf_sh.shape)
        fodf_sh_.value = fodf_sh
        s_ = cvx.Variable(s.shape)
        s_.value = np.clip(s, 1e-10, None)
        init = {'x': fodf_sh_,
                's': s_}
        return init

def __init__(self, X, reg):

        # self._P_init = np.dot(X[:, :N].T, X[:, :N])
        self._P = P = np.dot(X.T, X)
        self._X = X

        # No super res for now.
        assert _rank(P) == P.shape[0]

        self._reg = reg

        # Make cvxpy matrix types for later re-use.
        self._P_mat = cvx.Parameter(P.shape)
        self._P_mat.value = P
        self._x_mat = cvx.Parameter(P.shape[0], 1)
        self._reg_mat = cvx.Parameter(reg.shape)
        self._reg_mat.value = -reg
        self._h_mat = cvx.Parameter(reg.shape[0], 1)
        h_ = np.zeros((reg.shape[0]))
        self._h_mat.value = h_

def __call__(self, signal):
        z = np.dot(self._X.T, signal)
        init = self._lstsq_initial(z)
        z_mat = cvx.Variable(z.shape[0], 1)
        z_mat.value = z
        constraints = [self._reg_mat * self._x_mat >= self._h_mat]
        objMin = cvx.Problem(cvx.Minimize(0.5 * cvx.quad_form(self._x_mat, self._P_mat) + \
                                         (z_mat.T * self._x_mat)), constraints)
        r = objMin.solve(solver=cvx.OSQP)
        fodf_sh = r['x']
        fodf_sh = np.array(fodf_sh)[:, 0]
        return fodf_sh

I am constructing the OSQP problem as above but am getting the error: 'Problem contains unspecified parameters'. Does this mean that the quad_form has been written wrongly with 2 parameters?

I am relatively new to CVXPY and any help would be appreciated!
Thanks!

All 11 comments

Haven't run the code but it looks like _x_mat parameter is not set. Did you forget to set its value?

Thank you @bstellato ! Can you also point me to some example/ tutorial that solves some QP problem in a similar manner? I am unable to find some using OSQP...

All the cvxpy problems that can be cast as QPs are solved by default with OSQP. For additional ones, you can check out the osqp docs.

The same problem (mentioned above) was formulated and was working in CVXOPT as:

def _lstsq_initial(self, z):
        fodf_sh = csd._solve_cholesky(self._P, z)
        s = np.dot(self._reg, fodf_sh)
        init = {'x': cvx.matrix(fodf_sh),
                's': cvx.matrix(s.clip(1e-10))}
        return init

def __init__(self, X, reg):
        self._P = P = np.dot(X.T, X)
        self._X = X

        # No super res for now.
        assert _rank(P) == P.shape[0]

        self._reg = reg
        # self._P_init = np.dot(X[:, :N].T, X[:, :N])

        # Make cvxopt matrix types for later re-use.
        self._P_mat = cvx.matrix(P)
        self._reg_mat = cvx.matrix(-reg)
        self._h_mat = cvx.matrix(0., (reg.shape[0], 1))

def __call__(self, signal):
        z = np.dot(self._X.T, signal)
        init = self._lstsq_initial(z)

        z_mat = cvx.matrix(-z)
        qp = cvx.solvers.qp
        r = qp(self._P_mat, z_mat, self._reg_mat, self._h_mat, initvals=init)
        fodf_sh = r['x']
        fodf_sh = np.array(fodf_sh)[:, 0]
        return fodf_sh

I intend to just port this code to CVXPY.. Just facing a hard time in understanding how to!

Thank you @bstellato ! Can you also point me to some example/ tutorial that solves some QP problem in a similar manner? I am unable to find some using OSQP...

I want self._x_mat to be my optimization variable. But when I set it up as:
self._x_mat = cvx.Variable(P.shape[0], 1)
It gives me a dcp error. As in:

objMin = cvx.Problem(cvx.Minimize(cvx.quad_form(self._x_mat, self._P_mat)))
objMin.is_dcp()

returns False. Am I doing something wrong in setting up the problem? Because in CVXOPT I did not have to explicitly set up the optimization variable and the interface was a little simpler to understand.

Any help would be super useful!
Thanks!

@bstellato also, how can I set initvals in CVXPY as in CVXOPT? Is there an equivalent function to do so?

You cannot set initvals in CVXPY. It shouldn't matter too much for a QP, I would think.

The only way you could get objMin.is_dcp() to be False is if self._P_mat is not PSD, assuming everything has the correct dimensions.

The only way you could get objMin.is_dcp() to be False is if self._P_mat is not PSD, assuming everything has the correct dimensions.

Hi Steven,

Thank you for your response! I think I have corrected this, by specifying the values in setting up the problem as follows:
objMin = cvx.Problem(cvx.Minimize(0.5 * cvx.quad_form(self._x_mat, self._P_mat.value) + z_mat.value.T * self._x_mat)), constraints)

Does this seem correct to you?

On doing the above, objMin.is_dcp() is returning True!

You cannot set initvals in CVXPY. It shouldn't matter too much for a QP, I would think.

Thank you! I actually need this for another open-source project: DIPY (Diffusion Imaging in Python). The model needs to start the QP from a Cholesky Decomposition's initvals generated by another algorithm (Constrained Spherical Deconvolution).

I read that I could set the Variable.value to my primal initial point. Is there a way that I can make use of this to set the initvals as 'x' and 's' like in CVXOPT: QP ?

Thanks again :)

Unfortunately cvxpy does not yet support setting initial values. You can add it as a requested feature, though it's probably already in the TODOs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dave31415 picture dave31415  路  7Comments

angeris picture angeris  路  8Comments

aliirmak picture aliirmak  路  5Comments

bstellato picture bstellato  路  5Comments

GiorgioBalestrieri picture GiorgioBalestrieri  路  10Comments