数学建模__非线性规划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
相关推荐
Luchang-Li1 天前
sglang pytorch NCCL hang分析
pytorch·python·nccl
一个天蝎座 白勺 程序猿1 天前
Python爬虫(47)Python异步爬虫与K8S弹性伸缩:构建百万级并发数据采集引擎
爬虫·python·kubernetes
XiaoMu_0011 天前
基于Django+Vue3+YOLO的智能气象检测系统
python·yolo·django
honder试试1 天前
焊接自动化测试平台图像处理分析-模型训练推理
开发语言·python
民乐团扒谱机1 天前
逻辑回归算法干货详解:从原理到 MATLAB 可视化实现
数学建模·matlab·分类·数据挖掘·回归·逻辑回归·代码分享
^Rocky1 天前
JavaScript性能优化实战
开发语言·javascript·性能优化
心本无晴.1 天前
Python进程,线程
python·进程
ponnylv1 天前
深入剖析Spring Boot启动流程
java·开发语言·spring boot·spring
萧邀人1 天前
第一课、Cocos Creator 3.8 安装与配置
开发语言
wheeldown1 天前
【数学建模】在烟雾导弹遮蔽模型中的实际参考文献
数学建模