CVXPY库可优化实用程序功能

问题描述

我正在尝试通过两个约束来最大化效用函数。但是我不确定我该在哪里修复代码。 我尝试过使矩阵的尺寸相同并在代码中指定

我已经定义了全局参数 我已经指定了约束条件

theta1 = 1
theta2 = 0.1
big_gamma1 = 1
big_gamma2= 0.5
gamma1 = 0.5
gamma2 = 0.5
z2 = 0.7*np.ones(5)
z1 = np.linspace(0.01,4,5)
xi_1 = 1/z1
xi1 = xi_1.transpose
print(z1)

z2 = 0.07*np.ones((1,5))
print(z2)
z1 = np.linspace(0.01,5)
xi_1 = 1/z1
xi1 = xi_1.transpose

r1 = cp.Variable((5,1))
r2 = cp.Variable((5,1))
f1 = cp.Variable((1,5))
f2 = cp.Variable((1,5))

constraints = [f1 + f2 ==1,f1@r1 + f2@r2 == 1]

obj = cp.Problem(cp.Minimize((f1@r1 + f2@xi1)@z1 + f2@(r2 - xi1)@(z2)),[f1 + f2 ==1,f1*r1 + f2*r2 == 1])
print("obj is DCP:",obj.is_dcp())

代码错误。

[[0.07 0.07 0.07 0.07 0.07]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-b52595a9b4b9> in <module>
     13               f1@r1 + f2@r2 == 1]
     14 
---> 15 obj = cp.Problem(cp.Minimize((f1@r1 + f2@xi1)@z1 + f2@(r2 - xi1)@(z2)),16               f1*r1 + f2*r2 == 1])
     17 print("obj is DCP:",obj.is_dcp())

~/opt/anaconda3/lib/python3.7/site-packages/cvxpy/expressions/expression.py in cast_op(self,other)
     44         """A wrapped binary operator that can handle non-Expression arguments.
     45         """
---> 46         other = self.cast_to_const(other)
     47         return binary_op(self,other)
     48     return cast_op

~/opt/anaconda3/lib/python3.7/site-packages/cvxpy/expressions/expression.py in cast_to_const(expr)
    465                         "Combine Expressions using atoms such as bmat,hstack,and vstack."
    466                     )
--> 467         return expr if isinstance(expr,Expression) else cvxtypes.constant()(expr)
    468 
    469     @staticmethod

~/opt/anaconda3/lib/python3.7/site-packages/cvxpy/expressions/constants/constant.py in __init__(self,value)
     43             self._sparse = True
     44         else:
---> 45             self._value = intf.DEFAULT_INTF.const_to_matrix(value)
     46             self._sparse = False
     47         self._imag = None

~/opt/anaconda3/lib/python3.7/site-packages/cvxpy/interface/numpy_interface/ndarray_interface.py in const_to_matrix(self,value,convert_scalars)
     48             return result
     49         else:
---> 50             return result.astype(numpy.float64)
     51 
     52     # Return an identity matrix.

TypeError: float() argument must be a string or a number,not 'builtin_function_or_method'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...