如何在Python中为凸问题提供约束

问题描述 投票:0回答:1

我想用Python解决凸问题。但是当我想为我的问题定义约束时,它会产生错误。这是我的代码:

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 和 Y 是 2322×6561 和 2322×1 矩阵。

运行上面的代码后会出现错误(我刚刚准备了所需的代码部分。如果您认为需要知道代码前面几行,请告诉我):

    ---------------------------------------------------------------------------
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)

有人能提一下我面临的问题吗?

python dimensions convex-optimization convex
1个回答
0
投票

正如 @MichalAdamaszek 在评论中提到的,我必须使用大小为 (2322,) 的向量而不是大小为 (2322,1) 的数组。

© www.soinside.com 2019 - 2024. All rights reserved.