这是我在python 3.6中的当前代码,使用SLSQP方法优化受约束的3变量优化器,这是我优化到椭圆分布。
import numpy as np
from scipy.optimize import minimize
def func(x):
return ((x[0]*((0.2292 + x[1])/2)) + ((0.9-x[0])*((x[2] +
x[1])/2))-0.16)
def func_deriv(x):
""" Derivative of objective function """
dfdx0 = (0.2292 - x[2])/2
dfdx1 = 0.89/2
dfdx2 = (0.89 - x[0])/2
return np.array([dfdx0, dfdx1, dfdx2])
cons = ({'type': 'ineq',
'fun': lambda x: np.array([x[1]-x[2]]),
'jac': lambda x: np.array([0.0, 1.0, -1.0])},
{'type': 'ineq',
'fun': lambda x: np.array([0.2292-x[1]]),
'jac': lambda x: np.array([0.0, -1.0, 0.0])},
{'type': 'ineq',
'fun': lambda x: np.array(0.89-x[0]),
'jac': lambda x: np.array([-1.0, 0.0, 0.0])})
res = minimize(func, [0.45, 0.20, 0.10], jac=func_deriv, constraints=cons
method='SLSQP',
options={'disp': True, 'maxiter': 100})
我不断得到的错误是RunTimeWarning invalid double scalar
我不知道为什么会发生这种情况
Iteration limit exceeded (Exit mode 9)
Current function value: nan
Iterations: 101
Function evaluations: 881
Gradient evaluations: 101
C:/Users/bob19/untitled9.py:13: RuntimeWarning: overflow encountered in
double_scalars
return ((x[0]*((0.2292 + x[1])/2)) + ((0.9-x[0])*((x[2] + x[1])/2))-0.16)
C:/Users/bob19/untitled9.py:13: RuntimeWarning: invalid value encountered
in double_scalars
return ((x[0]*((0.2292 + x[1])/2)) + ((0.9-x[0])*((x[2] + x[1])/2))-0.16)
非常感谢任何帮助。
上面的错误我不再得到,而是我得到'不平等约束不兼容'
runfile('C:/Users/Robin19/opimizer v2.py', wdir='C:/Users/Robin19')
Inequality constraints incompatible (Exit mode 4)
Current function value: -2.77024369372e+24
Iterations: 8
Function evaluations: 8
Gradient evaluations: 8
fun: -2.7702436937183577e+24
jac: array([ 1.49413376e+12, 4.45000000e-01, 9.27040055e+11,
0.00000000e+00])
message: 'Inequality constraints incompatible'
nfev: 8
nit: 8
njev: 8
status: 4
success: False
x: array([ -1.85408011e+12, -3.00597522e+12, -2.98826753e+12])