简体   繁体   English

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

[英]Apply constraints to the objective function in scipy.optimize.minimize? Such as 0 <= objective <= 1

I'm trying to apply scipy.optimize.minimize to solve this problem, but I haven't been able to apply constraints to the objective function. 我试图应用scipy.optimize.minimize来解决此问题,但是我无法将约束应用于目标函数。

In the code, objective function is lamb(weights). 在代码中,目标函数是lamb(weights)。 However, its value needs to be between -1 and 0 . 但是,其值必须在-1和0之间 I tried this with the constraints const15 and const16 , but the solution doesn't seem to be affected. 我使用约束const15const16 ,但是该解决方案似乎并未受到影响。

Can anyone see what's wrong? 谁能看到什么问题?

PS: I know the code is amateur, I'm still learning Python. 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)

The solution value I'm getting is not between -1 and 0. 我得到的解决方案值不在-1和0之间。
Also, you can see that the values of x[0] to x[9] did not change at all. 另外,您可以看到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])

The first step for debugging would be checking the docs : 调试的第一步是检查docs

constraints{Constraint, dict} or List of {Constraint, dict}, optional 约束{约束,字典}或列表{约束,字典},可选

Constraints definition ( only for COBYLA, SLSQP and trust-constr ) 约束定义( 仅适用于COBYLA,SLSQP和trust-constr

As you explicitly enforce the use of method=CG , you should not be surprised that the solver will ignore your constraints. 当您明确强制使用method=CG ,求解器将忽略您的约束,您应该不会感到惊讶。

It should warn you though: 应该警告您

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scipy.optimize.minimize目标函数ValueError - Scipy.optimize.minimize objective function ValueError 从矩阵动态编写用于scipy.optimize.minimize的目标函数和约束 - Dynamically writing the objective function and constraints for scipy.optimize.minimize from matrices scipy.optimize.minimize 跟踪目标函数 - scipy.optimize.minimize keep track of objective function Scipy.optimize.minimize 目标 function 必须返回一个标量 - Scipy.optimize.minimize Objective function must return a scalar 当我使用 scipy.optimize.minimize() 最小化它时,为什么我的目标 function 的矩阵参数发生了变化? - Why is a matrix argument of my objective function changed when I minimize it with scipy.optimize.minimize()? Scipy.optimize.minimize() 两个 arrays 用于非线性物镜 - Scipy.optimize.minimize() two arrays for non-linear objective 当你想要计算梯度和目标函数时,如何使用scipy.optimize.minimize函数? - How to use scipy.optimize.minimize function when you want to compute gradient along with the objective function? 除了自变量外,如何为scipy.optimize.minimize的目标函数提供附加输入 - How to give additional input to objective function of scipy.optimize.minimize other than independent variables 我可以将目标函数和派生函数传递给scipy.optimize.minimize作为一个函数吗? - Can I pass the objective and derivative functions to scipy.optimize.minimize as one function? 为什么scipy.optimize.minimize尝试将奇怪的参数传递给我的目标函数? - Why is scipy.optimize.minimize trying to pass in weird arguments to my objective function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM