Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to set up a simple quadratic program optimisation using cvxpy:

import numpy as np
import cvxpy as cp

sigma = np.array([[0.000234, 0.000167],
                  [0.000167, 0.00030]])

ret = np.array([0.06, 0.04])

x = cp.Variable(2)

prob = cp.Problem(cp.Maximize(ret.T @ x - (1/2) * cp.quad_form(x, sigma)), [cp.sum(x) == 1, x >= 0])
prob.solve()

However, I'm getting the following error:

Traceback (most recent call last):
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagesIPythoncoreinteractiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-82-4582a2312a0a>", line 12, in <module>
    prob.solve()
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagescvxpyproblemsproblem.py", line 395, in solve
    return solve_func(self, *args, **kwargs)
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagescvxpyproblemsproblem.py", line 744, in _solve
    solver, gp, enforce_dpp)
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagescvxpyproblemsproblem.py", line 524, in get_problem_data
    data, inverse_data = solving_chain.apply(self)
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagescvxpy
eductionschain.py", line 71, in apply
    problem, inv = r.apply(problem)
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagescvxpy
eductionsqp2quad_formqp_matrix_stuffing.py", line 263, in apply
    problem, extractor)
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagescvxpy
eductionsqp2quad_formqp_matrix_stuffing.py", line 247, in stuffed_objective
    params_to_P, params_to_q = extractor.quad_form(expr)
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagescvxpyutilitiescoeff_extractor.py", line 198, in quad_form
    P = sp.block_diag([P, coeffs[var_id]['P']])
  File "C:UsersfidahurAppDataLocalProgramsPythonPython37libsite-packagesscipysparseconstruct.py", line 693, in block_diag
    data = np.concatenate(data)
  File "<__array_function__ internals>", line 6, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)

Any help would be really appreciated. Thanks.

question from:https://stackoverflow.com/questions/65945243/dimension-error-when-setting-up-a-quadratic-program-optimisation-using-cvxpy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
293 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...