Python解决线性规划问题

线性规划

线性规划(Linear programming,简称LP),是运筹学中研究较早、发展较快、应用广泛、方法较成熟的一个重要分支,它是辅助人们进行科学管理的一种数学方法。研究线性约束条件下线性目标函数的极值问题的数学理论和方法。英文缩写LP。

线性规划模型的一般形式:
在这里插入图片描述

建立线性规划模型的一般步骤:

1.根据问题所要达到目的的因素找到决策变量;
2.由决策变量和所要达到目的之间的函数关系确定目标函数;
3.由决策变量所受的限制条件确定决策变量所要满足的约束条件。

scipy

scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method=‘simplex’, callback=None, options=None)

  • c : array_like
    Coefficients of the linear objective function to be minimized.
    最小化的线性目标函数的系数。

  • A_ub :
    2-D array which, when matrix-multiplied by x, gives the values of the upper-bound inequality constraints at x.
    上限不等式约束的值

  • b_ub : array_like
    1-D array of values representing the upper-bound of each inequality constraint (row) in A_ub.
    表示 A_ub 中每个不等式约束(行)的上限的一维值数组。

  • A_eq : array_like
    2-D array which, when matrix-multiplied by x, gives the values of the equality constraints at x.

  • b_eq : array_like
    1-D array of values representing the RHS of each equality constraint (row) in A_eq.

  • bounds : sequence, optional
    (min, max) pairs for each element in x, defining the bounds on that parameter. Use None for one of min or max when there is no bound in that direction. By default bounds are (0, None) (non-negative) If a sequence containing a single tuple is provided, then min and max will be applied to all variables in the problem.

  • method : str, optional
    Type of solver. At this time only ‘simplex’ is supported.

  • callback : callable, optional
    If a callback function is provide, it will be called within each iteration of the simplex algorithm. The callback must have the signature callback(xk, **kwargs) where xk is the current solution vector and kwargs is a dictionary containing the following:

“tableau” : The current Simplex algorithm tableau
“nit” : The current iteration.
“pivot” : The pivot (row, column) used for the next iteration.
“phase” : Whether the algorithm is in Phase 1 or Phase 2.
“basis” : The indices of the columns of the basic variables.

  • options : dict, optional
    A dictionary of solver options. All methods accept the following generic options:

  • maxiter : int
    Maximum number of iterations to perform.

  • disp : bool
    Set to True to print convergence messages.

例子

在这里插入图片描述

from scipy import optimize as op
import math
import numpy as np

#系数
beta=[3,5,4]
c=np.array(beta)
#设置约束
a1=[2,3,0]
a2=[0,2,4]
a3=[3,2,5]

#不等式约束
A_ub=np.array([a1,a2,a3])
#约束上限,默认为非负
B_ub=np.array([1500,800,2000])
#线性规划
res=op.linprog(-c,A_ub,B_ub)
#输出结果
print(res)

在这里插入图片描述

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤影墨客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值