数学建模__非线性规划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
相关推荐
Warson_L16 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅16 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅16 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L16 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅17 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L17 小时前
python的类&继承
python
Warson_L17 小时前
类型标注/type annotation
python
ThreeS19 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵21 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏