How to specify nonlinear constraints in CVXPY?

1.4k views Asked by At

I am new to the CVXPY package and am attempting to port legacy Matlab optimization code that uses fmincon(x) with nonlinear constraints to CVXPY. In so doing I continue to obtain various error exceptions. when I have used various combinations of cp.Variable() and cp.Parameter().

As such, I am at a loss at what I am doing incorrectly, therefore I was hoping that someone could provide me with advice on how to achieve my objective.

Lastly, please note I did not design or implement the original Matlab code I inherited the code base and have been asked to convert it to Python. Below is both the MATLAB code and corresponding CVXPY code:

Matlab code snippets:

  % Third: define the nonlinear constraint function:
    function [cin, ceq] = nonlcon(x)
        cin = -1;
        ceq = x'*H*x - sd.^2;   % where H and sd are global.
    end

...

[O(4:k+3, i), fval, O(k+4, i)] = ...
     fmincon(@objfun, O(4:k+3, i-1), ABar, uBar, PHI, Nu, cBar, bBar, @nonlcon, options);

CVXPY code snippets:

def nonlcon(x, H, sd):
    # Algorithmic definition of the nonlinear constraint function
    cin = -1.
    ceq = (x.T @ H @ x) - sd**2   # where H and sd are global.

    out_params = (cin, ceq)

    return(out_params)

...

if callable(nonlcon):
    if len(nonlcon_args) == 2:
        (H, sd) = nonlcon_args
        cin = lambda x: np.negative(nonlcon(x, H, sd)[0])
        ceq = lambda x: nonlcon(x, H, sd)[1]
    else:
        cin = lambda x: np.negative(nonlcon(x)[0])
        ceq = lambda x: nonlcon(x)[1]

...

  if callable(nonlcon):
        c_in = cp.Variable()
        c_in.value = np.array(cin(x_hat))

        c_eq = cp.Variable()
        c_eq.value = np.array(ceq(x_hat))

        constraints += [c_in <= 0,
                                c_eq == 0]

    prob = cp.Problem(cp.Minimize(obj_f(x_hat)),
                                   constraints)

Latest CVXPY error message:

TypeError: bad operand type for abs(): 'AddExpression'

Results of various implementation combinations of specifying the nonlinear constraints, along with the resulting error message:

Observed CVXPY nonlinear constraint errors:

Using implementation:

 c_in = cp.Parameter()
 c_in.value = cin(x_hat)

 c_eq = cp.Variable((1,))
 c_eq.value = ceq(x_hat)

TypeError: is not a valid type for a Constant value.

Using implementation:

 c_in = cp.Variable((1,))
 c_in.value = cin(x_hat)

 c_eq = cp.Variable((1,))
 c_eq.value = ceq(x_hat)

ValueError: Invalid dimensions () for Variable value.

Using implementation:

 c_in = cp.Parameter()
 c_in.value = cin(x_hat)

 c_eq = cp.Parameter()
 c_eq.value = ceq(x_hat)

TypeError: is not a valid type for a Constant value.

Using implementation:

 c_in = cp.Parameter()
 c_in.value = cin(x_hat)

 c_eq = cp.Variable()
 c_eq.value = ceq(x_hat)

TypeError: is not a valid type for a Constant value.

Using implementation:

 c_in = cp.Variable()
 c_in.value = cin(x_hat)

 c_eq = cp.Variable()
 c_eq.value = ceq(x_hat)

TypeError: is not a valid type for a Constant value.

Using implementation:

 c_in = cp.Variable()
 c_in.value = np.array(cin(x_hat))

 c_eq = cp.Variable()
 c_eq.value = np.array(ceq(x_hat))

TypeError: bad operand type for abs(): 'AddExpression'

Using implementation:

c_in = cp.Parameter()
c_in.value = cin(x_hat)

# c_eq = cp.Variable()
# c_eq.value = ceq(x_hat)

constraints += [c_in <= 0,
                ceq(x_hat) == 0]

DCPError: Problem does not follow DCP rules. Specifically: The following constraints are not DCP:

Using implementation:

# c_in = cp.Parameter()
# c_in.value = cin(x_hat)

# c_eq = cp.Variable()
# c_eq.value = ceq(x_hat)

constraints += [cin(x_hat) <= 0,
                ceq(x_hat) == 0]

AttributeError: 'numpy.bool_' object has no attribute 'variables'

Really hoping someone can explain to me what I a doing incorrectly.

Thanks in advance...

Chris

0

There are 0 answers