Scipy是一个用于数学、科学、工程领域的常用软件包,可以处理插值、积分、优化、图像处理、常微分方程
问题
针对于有约束问题
python的scipy库给出了Sequential Least SQuares Programming (SLSQP) Algorithm。
实例
- SLSQP解决模板
对于下面的问题:
>>> from scipy.optimize import minimize
>>> import numpy as np
# 定义目标函数
>>> def rosen(x):
... """The Rosenbrock function"""
... return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)
# 约束项
# 变量范围约束
>>> from scipy.optimize import Bounds
>>> bounds = Bounds([0, -0.5], [1.0, 2.0])
# 不等式约束
>>> ineq_cons = {'type': 'ineq',
... 'fun' : lambda x: np.array([1 - x[0] - 2*x[1],
... 1 - x[0]**2 - x[1],
... 1 - x[0]**2 + x[1]]),
# jac 是fun的梯度矩阵
... 'jac' : lambda x: np.array([[-1.0, -2.0],
... [-2*x[0], -1.0],
... [-2*x[0], 1.0]])}
>>> eq_cons = {'type': 'eq',
... 'fun' : lambda x: np.array([2*x[0] + x[1] - 1]),
... 'jac' : lambda x: np.array([2.0, 1.0])}
# 初始值
>>> x0 = np.array([0.5, 0])
>>> res = minimize(rosen, x0, method='SLSQP', jac=rosen_der,
... constraints=[eq_cons, ineq_cons], options={'ftol': 1e-9, 'disp': True},
... bounds=bounds)
# may vary
Optimization terminated successfully. (Exit mode 0)
Current function value: 0.342717574857755
Iterations: 5
Function evaluations: 6
Gradient evaluations: 5
>>> print(res.x)
例题
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 22 20:14:59 2019
@author: Mingxing Li
"""
from scipy.optimize import minimize
from scipy.optimize import Bounds
import numpy as np
def fun(x):
return 10-x[1:]**2.0-x[:-1]**2.0
# 约束项
# 不等式约束
ineq_cons = {"type": "ineq",
"fun": lambda x:np.array([-x[0]**2+x[1]])}
# 等式约束
eq_cons = {"type": "eq",
"fun": lambda x: np.array([x[0]+x[1]])}
# 初始值
x0 = [-1, 0]
res = minimize(fun, x0, method='SLSQP',
constraints=[eq_cons, ineq_cons], options={'ftol': 1e-9, 'disp': True})
print(res)
运行结果
Optimization terminated successfully. (Exit mode 0)
Current function value: 8.0
Iterations: 2
Function evaluations: 8
Gradient evaluations: 2
fun: 8.0
jac: array([ 2., -2.])
message: 'Optimization terminated successfully.'
nfev: 8
nit: 2
njev: 2
status: 0
success: True
x: array([-1., 1.])
需要注意,这个方法貌似对初值很敏感。。。