我有一个要解决的最大化问题:
为此,我期待使用minimize
库中的scipy
函数。
在尝试之前,我将定义每个变量:
mu=[[-0.241035],
[ 0.03557551],
[-0.00410642],
[-0.43985304],
[-0.24741543]]
landa= 42.74650697 # is a scalar
E =[[0.000167,0.000032,0.000082,0.000055,0.000055],
[0.000032,0.000131,0.000019,0.000043,0.000032],
[0.000082,0.000019,0.000273,0.000110,0.000086],
[0.000055,0.000043,0.000110,0.000229,0.000131],
[0.000055,0.000032,0.000086,0.000131,0.000165]]
在funct0
中,设置出现在图像中的“最大化”函数并定义权重矩阵。
def funct0(x):
x0,x1,x2,x3,x4=x
weights= np.array([x0,x1,x2,x3,x4])
return -1*(np.matmul(weights.T , mu) - np.matmul(np.matmul (landa*weights.T, E ),weights) /2)
在funct1
中设置边界和约束,因为我希望权重x0,x1,x2,x3,x4
中的变量总和为1。
def funct1():
x0=np.array([1,1,1,1,1])
cons = ({'type': 'eq', 'fun': lambda x: sum(x) - 1})
res=minimize(funct0, x0, bounds=[[0,None] for i in range(len(x0))],options={"disp": False}, constraints=cons)
return res.x
print(funct1())
执行此脚本时,它输出
[ 0 1 0 0 0]
我不确定最大化是否正确,因为它将全部分配给x1
变量,而其余x0
x2
x3
x4
被分配了0个值
如果在funct0
中设置的函数是w
和mu
的线性函数,这对我来说很有意义,因为这样做会将所有值分配给对应的x1
达到mu
考虑到函数的非线性,我想我的输出将是矩阵中w
变量之间值的更“多样化”分配。
是否可能在funct0
中错误地设置了功能?
我可能使用了错误的优化吗?
我们非常感谢您的帮助。