Scipy优化scipy.optimize.minimize

如果喜欢请点赞,收藏,谢谢!
要解决一个优化问题

m i n i m i z e x [ 0 ] , x [ 1 ] \mathrm{minimize}_{x[0],x[1]} minimizex[0],x[1] log ⁡ 2 ( 1 + x [ 0 ] 2 3 ) + log ⁡ 2 ( 1 + x [ 1 ] 3 4 ) \log_{2}(1+\frac{x[0]2}{3})+\log_{2}(1+\frac{x[1]3}{4}) log2(1+3x[0]2)+log2(1+4x[1]3)

c o n s t r a i n t s \mathrm{constraints} constraints

log ⁡ 2 ( 1 + x [ 0 ] 2 5 ) ≥ 5 \log2(1+\frac{x[0]2}{5})\geq5 log2(1+5x[0]2)5
log ⁡ 2 ( 1 + x [ 1 ] 6 4 ) ≥ 5 \log2(1+\frac{x[1]6}{4})\geq5 log2(1+4x[1]6)5

程序示例

# coding=utf-8
from scipy.optimize import minimize
from scipy.optimize import NonlinearConstraint
import numpy as np


# 目标函数
def fun(a,b,c,d):
    def v(x):
        return np.log2(1+x[0]*a/b)+np.log2(1+x[1]*c/d)
    return v
#限制条件函数
def con(a,b,i):
    def v(x):
        return np.log2(1 + x[i] * a / b)-5
    return v



if __name__ == "__main__":
    # 定义常量值
    args = [2, 1, 3, 4]  # a,b,c,d
    args1 = [2, 5, 6, 4] 
    # 设置初始猜测值
    x0 = np.asarray((0.5, 0.5))
    #设置限制条件
    '''Equality constraint means that the constraint function result is
     to be zero whereas inequality means that it is to be non-negative'''
    cons = ({'type': 'ineq', 'fun': con(args1[0],args1[1],0)},
            {'type': 'ineq', 'fun': con(args1[2],args1[3],1)},
            )

    res = minimize(fun(args[0],args[1],args[2],args[3]), x0, constraints=cons)
    print(res.fun)
    print(res.success)
    print(res.x)

参考文献
scipy,optimize.minimize
ronaldo_liu2018的博客: python 非线性规划(scipy.optimize.minimize)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值