将约束应用于scipy.optimize.minimize中的目标函数?如 0 <= 目标 <= 1



我正在尝试应用scipy.optimize.minimize来解决这个问题,但我无法对目标函数应用约束。

在代码中,目标函数是 lamb(权重(。但是,其值需要介于 -1 和 0 之间。我在约束const15const16下尝试了此操作,但解决方案似乎不受影响。

谁能看出出了什么问题?

PS:我知道代码是业余的,我还在学习Python。

def lamb(weights):
theta1 = 0.1
theta2 = 0.2
theta3 = 0.7
l = weights[10]
return (-1 * ((theta1*l) + (theta2*l) + (theta3*l)))
def const1(weights):
f1 = 0
for i in range(len(m_ord)):
f1 += m_ord[i][perfil]*weights[i]
print(weights[10])
return ((f1 - lower_bounds[0])/(upper_bounds[0] - lower_bounds[0]) - theta1*weights[10])
def func2(weights):
f2 = 0
for i in range(len(m_ord)):
f2 += retorno_doze[i]*weights[i]
return f2
def const2(weights):
f2 = func2(weights)
return ((f2 - lower_bounds[1])/(upper_bounds[1] - lower_bounds[1]) - theta2*weights[10])
def const3(weights):
f2 = func2(weights)
f3 = 0
for i in range(len(m_ord)):
f3 += abs(retorno_doze[i] - f2)
f3 = f3 / (2 * num_fundos)
return ((upper_bounds[2] - f3)/(upper_bounds[2] - lower_bounds[2]) - theta3*weights[10])
def const4(weights):
return (weights[0] - (investimento_inicial[0] / financeiro))
def const5(weights):
return (weights[1] - (investimento_inicial[1] / financeiro))
def const6(weights):
return (weights[2] - (investimento_inicial[2] / financeiro))
def const7(weights):
return (weights[3] - (investimento_inicial[3] / financeiro))
def const8(weights):
return (weights[4] - (investimento_inicial[4] / financeiro))
def const9(weights):
return (weights[5] - (investimento_inicial[5] / financeiro))
def const10(weights):
return (weights[6] - (investimento_inicial[6] / financeiro))
def const11(weights):
return (weights[7] - (investimento_inicial[7] / financeiro))
def const12(weights):
return (weights[8] - (investimento_inicial[8] / financeiro))
def const13(weights):
return (weights[9] - (investimento_inicial[9] / financeiro))
#EQUALITY CONSTRAINT
def const14(weights):
return np.sum(weights[:10]) - 1
def const15(weights):
return (lamb(weights) + 1)
def const16(weights):
return (-1 * lamb(weights))
bnds = (0.0, 1.0)
bounds = tuple(bnds for fund in range(len(m_ord)+1))
x0 = (num_fundos+1)*[1./num_fundos,]
x0[10] = -0.5
cons = (
{'type':'ineq','fun':const1},
{'type':'ineq','fun':const2},
{'type':'ineq','fun':const3},
{'type':'ineq','fun':const4},
{'type':'ineq','fun':const5},
{'type':'ineq','fun':const6},
{'type':'ineq','fun':const7},
{'type':'ineq','fun':const8},
{'type':'ineq','fun':const9},
{'type':'ineq','fun':const10},
{'type':'ineq','fun':const11},
{'type':'ineq','fun':const12},
{'type':'ineq','fun':const13},
{'type':'eq','fun':const14},
{'type':'ineq','fun':const15},
{'type':'ineq','fun':const16},
)
result = sco.minimize(lamb, x0, method='CG', bounds=bounds, constraints=cons)
print(result)

我得到的解决方案值不在 -1 和 0 之间。
此外,您可以看到x[0]x[9]的值根本没有变化。

fun: -89478484.5
jac: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
message: 'Optimization terminated successfully.'
nfev: 195
nit: 1
njev: 15
status: 0
success: True
x: array([1.00000000e-01, 1.00000000e-01, 1.00000000e-01, 1.00000000e-01,
1.00000000e-01, 1.00000000e-01, 1.00000000e-01, 1.00000000e-01,
1.00000000e-01, 1.00000000e-01, 8.94784845e+07])

调试的第一步是检查文档:

约束{约束,字典}

或 {约束,字典}列表,可选

约束定义(仅适用于 COBYLA、SLSQP 和信任结构(

当您显式强制使用method=CG时,您不应该对求解器会忽略您的约束感到惊讶。

不过,它应该警告您:

# - constraints or bounds
if (meth in ('nelder-mead', 'powell', 'cg', 'bfgs', 'newton-cg', 'dogleg',
'trust-ncg') and (bounds is not None or np.any(constraints))):
warn('Method %s cannot handle constraints nor bounds.' % method,
RuntimeWarning)

相关内容

最新更新