Python 3.x 包cvxpy新语法错误求和条目

Python 3.x 包cvxpy新语法错误求和条目,python-3.x,optimization,numpy-ndarray,cvxpy,Python 3.x,Optimization,Numpy Ndarray,Cvxpy,我是cvxpy软件包的新手。我尝试使用它来处理以下博客中的一个示例: 我们试图优化发送给客户的营销渠道组合 最近对cvxpy包进行了一些更改,当我尝试运行sum_entries步骤(在最新版本中已更改为cvxpy.sum)时,出现以下错误 我认为问题来自于“选择”和“变压器”不兼容的维度,但我对cvxpy软件包还不够熟悉。任何提示都非常感谢 Code: test_probs.shape (200, 8) Code: # selection = cvxpy.Bool(*test_pr

我是cvxpy软件包的新手。我尝试使用它来处理以下博客中的一个示例:

我们试图优化发送给客户的营销渠道组合

最近对cvxpy包进行了一些更改,当我尝试运行sum_entries步骤(在最新版本中已更改为cvxpy.sum)时,出现以下错误

我认为问题来自于“选择”和“变压器”不兼容的维度,但我对cvxpy软件包还不够熟悉。任何提示都非常感谢

Code:

test_probs.shape

(200, 8)



Code:

# selection = cvxpy.Bool(*test_probs.shape) # syntax changed in latest version

selection = cvxpy.Variable(*test_probs.shape, boolean=True)



# constraints

# Constant matrix that counts how many of each 
# material we sent to each customer

TRANSFORMER = np.array([[1,0,0],
                        [0,1,0],
                        [0,0,1],
                        [1,1,0],
                        [1,0,1],
                        [0,1,1],
                        [1,1,1],
                        [0,0,0]])


# can't send customer more promotion than there is supply

# note: sum_entries changed to sum in latest cvxpy version

supply_constraint = cvxpy.sum(selection * TRANSFORMER, axis=0) <= supply


Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-47-f2ebf41a00af> in <module>()
     18 # note: sum_entries changed to sum in latest cvxpy version
     19 
---> 20 supply_constraint = cvxpy.sum(selection * TRANSFORMER, axis=0) <= supply
     21 
     22 

~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/expressions/expression.py in cast_op(self, other)
     47         """
     48         other = self.cast_to_const(other)
---> 49         return binary_op(self, other)
     50     return cast_op
     51 

~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/expressions/expression.py in __mul__(self, other)
    385             return cvxtypes.multiply_expr()(self, other)
    386         elif self.is_constant() or other.is_constant():
--> 387             return cvxtypes.mul_expr()(self, other)
    388         else:
    389             warnings.warn("Forming a nonconvex expression.")

~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/atoms/affine/binary_operators.py in __init__(self, lh_exp, rh_exp)
     41 
     42     def __init__(self, lh_exp, rh_exp):
---> 43         super(BinaryOperator, self).__init__(lh_exp, rh_exp)
     44 
     45     def name(self):

~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/atoms/atom.py in __init__(self, *args)
     42         self.args = [Atom.cast_to_const(arg) for arg in args]
     43         self.validate_arguments()
---> 44         self._shape = self.shape_from_args()
     45         if len(self._shape) > 2:
     46             raise ValueError("Atoms must be at most 2D.")

~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/atoms/affine/binary_operators.py in shape_from_args(self)
    107         """Returns the (row, col) shape of the expression.
    108         """
--> 109         return u.shape.mul_shapes(self.args[0].shape, self.args[1].shape)
    110 
    111     def is_atom_convex(self):

~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/utilities/shape.py in mul_shapes(lh_shape, rh_shape)
    140     lh_old = lh_shape
    141     rh_old = rh_shape
--> 142     lh_shape, rh_shape, shape = mul_shapes_promote(lh_shape, rh_shape)
    143     if lh_shape != lh_old:
    144         shape = shape[1:]

~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/utilities/shape.py in mul_shapes_promote(lh_shape, rh_shape)
    107     if lh_mat_shape[1] != rh_mat_shape[0]:
    108         raise ValueError("Incompatible dimensions %s %s" % (
--> 109             lh_shape, rh_shape))
    110     if lh_shape[:-2] != rh_shape[:-2]:
    111         raise ValueError("Incompatible dimensions %s %s" % (

ValueError: Incompatible dimensions (1, 200) (8, 3)
现在,当我运行下面代码的supply_约束部分时,我得到了新的错误

代码:

#约束
#常数矩阵,计算每个
#我们发送给每位客户的材料
TRANSFORMER=np.数组([[1,0,0],
[0,1,0],
[0,0,1],
[1,1,0],
[1,0,1],
[0,1,1],
[1,1,1],
[0,0,0]])
#无法向客户发送超过供应量的促销活动
#注意:在最新的cvxpy版本中,sum_条目更改为sum
电源约束=cvxpy.sum(选择*变压器,轴=0)20电源约束=cvxpy.sum(选择*变压器,轴=0)49返回二进制运算(自身,其他)
50回投
51
~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/expressions/expression.py in(self,other)
370“表达式:两个表达式的差异。
371         """
-->372返回自我+-其他
373
374(u cast)u其他
cast_op中的~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/expressions/expression.py(self,other)
47         """
48其他=自投至常数(其他)
--->49返回二进制运算(自身、其他)
50回投
51
~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/expressions/expression.py in\uuuu添加(self,other)
358“表达式:两个表达式之和。
359         """
-->360返回cvxtypes.add_expr()([self,other])
361
362(u cast)u其他
~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/atoms/affine/add\u expr.py in\uuuuuuuu init\uuuuuuuuu(self,arg\u组)
34#对于效率组,参数为总和。
35自组=自组
--->36 super(加法表达式,self)。\uuuu init\uuuuu(*arg\u组)
37 self.args=[]
38对于arg_组中的组:
~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/atoms/atom.py in uuu__________(self,*args)
42 self.args=[Atom.cast_to_const(arg)for args中的arg]
43自我验证_参数()
--->44 self.\u shape=self.shape\u from_args()
45如果透镜(自身形状)>2:
46 raise VALUE ERROR(“原子最多只能是2D”)
~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/atoms/affine/add_expr.py in shape_from_args(self)
42“返回表达式的(行、列)形状。
43         """
--->44返回u.shape.sum_shapes([arg.shape代表self.args中的arg])
45
46 def扩展参数(自身、扩展):
~/anaconda2/envs/py36/lib/python3.6/site-packages/cvxpy/utilities/shape.py in sum_shapes(shapes)
50升值错误(
51“无法广播维度”+
--->52个len(形状)*%s“%tuple(形状))
53
54如果长度(形状)>=长度(t)否则t,则更长=形状
ValueError:无法广播维度(3,)(1,3)

当您创建
选择变量时,您的问题就出现了。您正在将shape元组解压为多个参数。第一个参数应该是一个shape。因此正确的构造是:

selection = cvxpy.Variable(test_probs.shape, boolean=True)
可以通过检查“形状”属性来验证这一点是否正确:

selection.shape
现在应该给出:

(200, 8)

感谢您回复我。我在尝试您建议的原始帖子中添加了一个更新,现在运行代码的supply_constraint部分时出现了一个新错误。与以下内容相关的内容:“ValueError:无法广播维度(3),)(1,3)“尝试将
supply
展平为一维。事实上,我认为真正的问题是,这篇博文使用的是cvxpy 1.0之前的旧版本。您和我正在使用1.0或更高版本。如果您想将cvxpy降级或将其示例转换为1.0,则由您决定
selection = cvxpy.Variable(test_probs.shape, boolean=True)
selection.shape
(200, 8)