0

I tried to solve a convex problem with cvxpy as below.

        import cvxpy as cp
        import numpy as np

        # Problem data.
        Q = np.array([[13, 12, -2], [12, 17, 6], [-2, 6, 12]])
        q = np.array([[-22, -14.5, 13]])
        r = 1

        # Construct the problem.
        x = cp.Variable((3,1))
        objective = cp.Minimize(np.dot(np.dot(x.T, Q), x) + np.dot(q, x) + r)

        constraints = [0 <= x[0:], x[0:] <= 1]
        prob = cp.Problem(objective, constraints)

        # The optimal objective value is returned by `prob.solve()`.
        result = prob.solve()
        # The optimal value for x is stored in `x.value`.
        print(x.value)
        # The optimal Lagrange multiplier for a constraint is stored in
        # `constraint.dual_value`.
        print(constraints[0].dual_value)

However, I get this error:

        ValueError: setting an array element with a sequence.

I don't know why this error occurs because everything else seems to work.

Edit: Please let me know if the problem statement is needed.

3
  • The error is incomplete and the stacktrace probably even shows the problematic line. Furthermore you either did something wrong when copy-pasting that code or your cvxpy version is old as cp.Variable(3,1) should not do. Commented Apr 21, 2020 at 16:28
  • Your problem in general howewer is that you are using cvxpy in a wrong way: cvxpy provides atomic functions and overloads some operators such that it can generate some expression-graph internally. Using non-cvxpy stuff in expressions passed to cvxpy does not allow that. np.dot should not be there. Reason 1: cvxpy does not know what to do with it (it's external stuff) Reason 2: np.dot() is a less algebraic expression, while cvxpy allows allows algebraic expr like A*x (mat-vec mul), for example, where numpy would need dot. Reason 3: You want cvxpy's quad_form expression i suppose. Commented Apr 21, 2020 at 16:36
  • @sascha Thank you for your comment. I have missed out another pair of brackets around x = cp.Variable(3,1). I have since added them in for proper debugging. Additionally, I will look into the reasons you provided to debug. Commented Apr 21, 2020 at 16:40

1 Answer 1

1

See comments above:

import cvxpy as cp
import numpy as np

# Problem data.
Q = np.array([[13, 12, -2], [12, 17, 6], [-2, 6, 12]])
q = np.array([[-22, -14.5, 13]])
r = 1

# Construct the problem.
x = cp.Variable((3,1))

# WE CAN'T USE NUMPY'S DOT
# ALSO: WE WANT TO EXPRESS AS MUCH STRUCTURE AS POSSIBLE -> cp.quad_form()!
# q*x is cvxpy-compatible expression -> quite algebraic compared to numpy
# -------------------------------------------------------------------------
objective = cp.Minimize(cp.quad_form(x, Q) + q*x + r)

# ORIGINAL SLICING IS A NO-OP
# ---------------------------
constraints = [0 <= x, x <= 1]

prob = cp.Problem(objective, constraints)

        # The optimal objective value is returned by `prob.solve()`.
result = prob.solve()
        # The optimal value for x is stored in `x.value`.
print(x.value)
        # The optimal Lagrange multiplier for a constraint is stored in
        # `constraint.dual_value`.
print(constraints[0].dual_value)

Output:

[[ 8.46153846e-01]
[-6.34467676e-25]
[-1.92032635e-25]]
[[0.        ]
[5.80769231]
[9.61538462]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.