转载-scipy求有约束优化问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39575835/article/details/101173969
————————————————

问题

针对于有约束问题
在这里插入图片描述
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.])

需要注意,这个方法貌似对初值很敏感。。。

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39575835/article/details/101173969
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值