scipy.minimize与SLSQP不遵守约束

时间:2017-08-07 00:23:30

标签: python numpy scipy mathematical-optimization

我正在尝试使用scipy.minimize将函数f(x) = x[0] * x[1]最小化到不等式约束系统上,并且求解器返回的值不符合所有约束。例如:

import numpy as np
from scipy.optimize import minimize
from scipy.optimize import fmin_slsqp

# small constant for enforcing strict inequalities
c = 0.000001

# Objective Function to Optimize
def objective(x):
    return x[0] * x[1]

# Constraints
def con1(x):
    return (x[0]*(1/(7**x[2]))) + 6*x[1] - c

def con2(x):
    return (x[0]*(1/(2**x[2]))) + x[1] - c

def con3(x):
    return (x[0]*(1/(3**x[2]))) + 2*x[1] - c

def con4(x):
    return (x[0]*(1/(4**x[2]))) + 3*x[1] - c

def con5(x):
    return (x[0]*(1/(7**x[2]))) - (x[0]*(1/(1**x[2]))) + 7*x[1] - c

def con6(x):
    return (x[0]*(1/(2**x[2]))) - (x[0]*(1/(1**x[2]))) + 2*x[1] - c

def con7(x):
    return (x[0]*(1/(3**x[2]))) - (x[0]*(1/(1**x[2]))) + 3*x[1] - c

def con8(x):
    return -(x[0]*(1/(1**x[2]))) + 4*x[1] - c

def con9(x):
    return (x[0]*(1/(1**x[2]))) + 2*x[1] - c

def con10(x):
    return (x[0]*(1/(7**x[2]))) + 4*x[1] - c

def con11(x):
    return (x[0]*(1/(2**x[2]))) - x[1] - c

def con12(x):
    return (x[0]*(1/(4**x[2]))) + x[1] - c

def con13(x):
    return x[0] - 1

def con14(x):
    return x[1] - 1

def con15(x):
    return x[2] - c

# Initial Guesses
x0 = [1,1,1]

# Constraint Objects
constr1 = {'type':'ineq', 'fun':con1}
constr2 = {'type':'ineq', 'fun':con2}
constr3 = {'type':'ineq', 'fun':con3}
constr4 = {'type':'ineq', 'fun':con4}
constr5 = {'type':'ineq', 'fun':con5}   
constr6 = {'type':'ineq', 'fun':con6}
constr7 = {'type':'ineq', 'fun':con7}
constr8 = {'type':'ineq', 'fun':con8}
constr9 = {'type':'ineq', 'fun':con9}
constr10 = {'type':'ineq', 'fun':con10}
constr11 = {'type':'ineq', 'fun':con11}
constr12 = {'type':'ineq', 'fun':con12}
constr13 = {'type':'ineq', 'fun':con13}
constr14 = {'type':'ineq', 'fun':con14}
constr15 = {'type':'ineq', 'fun':con15}

cons = [constr1,constr2,constr3,constr4,constr5,constr6,constr7,constr8,constr9,constr10,constr11,constr12,constr13,constr14,constr15]

solution = minimize(objective,x0,method='SLSQP',constraints=cons)

for con in cons:
    print(str(con) + str(con['fun'](solution.x)))

使用解决方案值循环约束表明,即使约束的格式为>= 0,某些约束也会评估为负值。

这是由于我在规范中出现了一些错误吗?数值精度的问题?或者这是SLSQP的问题? (参见,例如:https://github.com/scipy/scipy/issues/7618)。

如果这不是我的规范的问题,我也会接受在其他框架(最好是Python)中解决这个约束问题的公式的建议。

0 个答案:

没有答案