线性规划
线性规划(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)