数学建模__非线性规划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
相关推荐
又是忙碌的一天8 分钟前
Java IO流
java·开发语言
fish_study_csdn11 分钟前
Python内存管理机制
开发语言·python·c python
ghie90902 小时前
MATLAB/Simulink水箱水位控制系统实现
开发语言·算法·matlab
cs麦子2 小时前
C语言--详解--指针--上
c语言·开发语言
java1234_小锋2 小时前
[免费]基于Python的农产品可视化系统(Django+echarts)【论文+源码+SQL脚本】
python·信息可视化·django·echarts
像风一样自由20202 小时前
Go语言入门指南-从零开始的奇妙之旅
开发语言·后端·golang
Danceful_YJ2 小时前
31.注意力评分函数
pytorch·python·深度学习
程序员三藏3 小时前
快速弄懂POM设计模式
自动化测试·软件测试·python·selenium·测试工具·设计模式·职场和发展
CoderYanger4 小时前
前端基础——CSS练习项目:百度热榜实现
开发语言·前端·css·百度·html·1024程序员节
虾..4 小时前
C++ 哈希
开发语言·c++·哈希算法