数学建模__非线性规划Python实现

使用到的是scipy库


线性规划指的是目标模型均为线性,除此以外的都是非线性规划,使用scipy提供的方法对该类问题进行求解。

py 复制代码
from scipy.optimize import minimize
import numpy as np

#定义目标函数
def fun(args):
    a,b,c,d = args
    v = lambda x: (a+x[0])/ (b+x[1]) - c*x[0] + d*x[2]
    return v

#定义约束条件
def con(args):
    # 约束条件 分为eq 和ineq
    # eq表示 函数结果等于0 ; ineq 表示 表达式大于等于0  

    x1min,x1max,x2min,x2max,x3min,x3max = args
    cons = ({'type':'ineq', 'fun': lambda x : x[0] - x1min},
            {'type':'ineq', 'fun': lambda x : -x[0] + x1max},
            {'type':'ineq', 'fun': lambda x : x[1] - x2min},
            {'type':'ineq', 'fun': lambda x : -x[1] + x2min},
            {'type':'ineq', 'fun': lambda x : x[2] - x3min},
            {'type':'ineq', 'fun': lambda x : -x[2] + x3min},

    )

    return cons

#定义常量值
args = (2,1,3,4)


#设置变量约束条件
args2 = (0.1,0.9,0.1,0.9,0.1,0.9)
cons = con(args2)


#设置初始随机值
x0 = np.asarray((0.5,0.5,0.5))
res = minimize(fun(args), x0, method='SLSQP', constraints=cons)
res
相关推荐
m0_7487080514 分钟前
C++中的观察者模式实战
开发语言·c++·算法
qq_5375626726 分钟前
跨语言调用C++接口
开发语言·c++·算法
wjs202436 分钟前
DOM CDATA
开发语言
Tingjct38 分钟前
【初阶数据结构-二叉树】
c语言·开发语言·数据结构·算法
2401_8321319539 分钟前
Python单元测试(unittest)实战指南
jvm·数据库·python
猷咪1 小时前
C++基础
开发语言·c++
IT·小灰灰1 小时前
30行PHP,利用硅基流动API,网页客服瞬间上线
开发语言·人工智能·aigc·php
快点好好学习吧1 小时前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
秦老师Q1 小时前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db
烟锁池塘柳01 小时前
解决Google Scholar “We‘re sorry... but your computer or network may be sending automated queries.”的问题
开发语言