pythondimensionsconvex-optimizationconvex

how to provide constraint for convex problem in python


I want to solve a convex problem in python. But when I want to define constraints for my problem, it produces an error. here is my code:

delta = 10;
A = pandas.read_excel(r"C:\Users\mohammad\Desktop\feko1\feko\A.xlsx")
Y = pandas.read_excel(r"C:\Users\mohammad\Desktop\feko1\feko\Y.xlsx")
A = numpy.array(A)
Y = numpy.array(Y)
s_L1 = cvxpy.Variable(6561)
constraints = [cvxpy.norm(A*s_L1 - Y,2) <= delta]

A and Y are 2322-by-6561 and 2322-by-1 matrix.

Error will be shown after running the above code (I have just prepared the required part of the code. If you think you need to know the previous lines of the code, please let me know):

    ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_7148\2562544661.py in <module>
----> 1 constraints = [cp.norm(A*s_L1 - Y,2) <= delta]
      2 
      
~\AppData\Roaming\Python\Python39\site-packages\cvxpy\expressions\expression.py in cast_op(self, other)
     48         """
     49         other = self.cast_to_const(other)
---> 50         return binary_op(self, other)
     51     return cast_op
     52 

~\AppData\Roaming\Python\Python39\site-packages\cvxpy\expressions\expression.py in __sub__(self, other)
    582         """Expression : The difference of two expressions.
    583         """
--> 584         return self + -other
    585 
    586     @_cast_other

~\AppData\Roaming\Python\Python39\site-packages\cvxpy\expressions\expression.py in cast_op(self, other)
     48         """
     49         other = self.cast_to_const(other)
---> 50         return binary_op(self, other)
     51     return cast_op
     52 

~\AppData\Roaming\Python\Python39\site-packages\cvxpy\expressions\expression.py in __add__(self, other)
    568             return self
    569         self, other = self.broadcast(self, other)
--> 570         return cvxtypes.add_expr()([self, other])
    571 
    572     @_cast_other

~\AppData\Roaming\Python\Python39\site-packages\cvxpy\atoms\affine\add_expr.py in __init__(self, arg_groups)
     32         # For efficiency group args as sums.
     33         self._arg_groups = arg_groups
---> 34         super(AddExpression, self).__init__(*arg_groups)
     35         self.args = []
     36         for group in arg_groups:

~\AppData\Roaming\Python\Python39\site-packages\cvxpy\atoms\atom.py in __init__(self, *args)
     49         self.args = [Atom.cast_to_const(arg) for arg in args]
     50         self.validate_arguments()
---> 51         self._shape = self.shape_from_args()
     52         if len(self._shape) > 2:
     53             raise ValueError("Atoms must be at most 2D.")

~\AppData\Roaming\Python\Python39\site-packages\cvxpy\atoms\affine\add_expr.py in shape_from_args(self)
     40         """Returns the (row, col) shape of the expression.
     41         """
---> 42         return u.shape.sum_shapes([arg.shape for arg in self.args])
     43 
     44     def expand_args(self, expr):

~\AppData\Roaming\Python\Python39\site-packages\cvxpy\utilities\shape.py in sum_shapes(shapes)
     48         # Only allow broadcasting for 0D arrays or summation of scalars.
     49         if shape != t and len(squeezed(shape)) != 0 and len(squeezed(t)) != 0:
---> 50             raise ValueError(
     51                 "Cannot broadcast dimensions " +
     52                 len(shapes)*" %s" % tuple(shapes))

ValueError: Cannot broadcast dimensions  (2322,) (2322, 1)

can anyone mention the problem which I'm facing with?


Solution

  • As @MichalAdamaszek mentioned in the comment, I have to use a vector of size (2322,) instead of an array of size (2322,1).