数学建模__非线性规划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
相关推荐
databook6 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室6 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三8 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271111 小时前
Python之语言特点
python
刘立军11 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机15 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机16 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i17 小时前
django中的FBV 和 CBV
python·django
c8i17 小时前
python中的闭包和装饰器
python
这里有鱼汤20 小时前
小白必看:QMT里的miniQMT入门教程
后端·python