In the following code snippet I intend to do the following:
(1) Multiply each element of the identity by the d optimization variable.
(2) Sum a vector of ones to a CVXPY affine expression, which is also a vector of 24 elements.
(3) Create a constraint which compares two vectors element-wise.
import numpy as np
import cvxpy as cp
weights = cp.Variable(5)
d = cp.Variable(1)
meas = np.random.rand(8, 3)
det = np.random.rand(24, 5)
dm = d * np.eye(3) # (1)
beh = np.ones([24, 1]) + cp.reshape((dm @ meas.T).T, [24, 1]) # (2)
constrs = [beh == det @ weights] #(3)
My questions are:
Q1: Did I code what I wanted?
Q2: At (2), I get the following error:
/usr/lib/python3.8/site-packages/cvxpy/utilities/shape.py in sum_shapes(shapes)
45 # Only allow broadcasting for 0D arrays or summation of scalars.
46 if shape != t and len(squeezed(shape)) != 0 and len(squeezed(t)) != 0:
---> 47 raise ValueError(
48 "Cannot broadcast dimensions " +
49 len(shapes)*" %s" % tuple(shapes))
ValueError: Cannot broadcast dimensions (24, 1) [24, 1]
What exactly does this mean, and how do I fix it?
Q3: When I do det @ weights
, at (3), I get an Expression(AFFINE, UNKNOWN, (24,))
. In the constraint, I'll compare it with beh
, which I'm guessing will be an Expression(AFFINE, UNKNOWN, (24, 1))
. Will this comparison also bring an issue?