提问人:mohammad rezza 提问时间:10/15/2023 更新时间:10/26/2023 访问量:29
如何在Python中为凸问题提供约束
how to provide constraint for convex problem in python
问:
我想解决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)
谁能提到我面临的问题?
答:
0赞
mohammad rezza
10/26/2023
#1
正如评论中提到的@MichalAdamaszek,我必须使用大小为 (2322,) 的向量而不是大小为 (2322,1) 的数组。
评论