【问题标题】:Apply constraints to the objective function in scipy.optimize.minimize? Such as 0 <= objective <= 1对 scipy.optimize.minimize 中的目标函数应用约束?如 0 <= 目标 <= 1
【发布时间】:2019-12-28 22:02:03
【问题描述】:

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

在代码中,目标函数是lamb(weights)。但是,它的值需要介于 -1 和 0 之间。我使用约束 const15const16 进行了尝试,但解决方案似乎没有受到影响。

谁能看出问题所在?

P.S.:我知道代码是业余的,我还在学习 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])

【问题讨论】:

    标签: python optimization scipy constraints minimize


    【解决方案1】:

    调试的第一步是检查docs:

    constraints{Constraint, dict} 或 {Constraint, dict} 列表,可选

    约束定义(仅适用于 COBYLA、SLSQP 和 trust-constr

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

    不过should warn you

    # - 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)
    

    【讨论】:

      猜你喜欢
      • 2019-06-30
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 2015-10-24
      • 2015-06-21
      相关资源
      最近更新 更多