Application of python in linear programming

Python learning

从入门学习到机器学习、从简单绘制折线图到随机绘制各种图,附往期目录:

Basic knowledge of getting started with python
python draws a simple line chart
python reads data in excel and draws multiple subgraphs and multiple groups of graphs on one canvas
python draws a histogram with error bars
python draws multiple subgraphs and displays them separately
python reads excel data And draw multi-y-axis images
using Python to batch adjust fonts, paragraph spacing and formatting in Word documents
Python draws a histogram with error bars


从本篇博文开始,开启学习一些优化算法和求解方法


Definition of Linear Programming

提示:这里可以添加本文要记录的大概内容:

Linear Programming (LP) comes from the decision-making problem of industrial production organization management. Mathematically, it is used to determine the optimal value of a multi-variable linear function when the variables satisfy linear constraints. The model usually consists of three elements - decision variables , objective function and constraints.


线性规划的一般模型:
m a x ( m i n ) z = ∑ j = 1 n c i x i ; max(min) z = \sum_{j=1}^{n} c_{i}x_{i}; max(min)z=j=1ncixi;

s . t . { ∑ j = 1 n a i j x j ≤ ( ≥ , = ) b i , i = 1 , 2 , 3 , 4 , ⋅ ⋅ ⋅ , m    x j > 0 , j = 1 , 2 , 3 , 4 , ⋅ ⋅ ⋅ , n s.t. \begin{cases} \sum\limits_{j=1}^na_{ij}x_{j}≤(≥,=)b_{i}, & i=1,2,3,4,···,m \\\\\ \ x_{j}>0,& j=1,2,3,4,···,n \\ \end{cases} s.t. j=1naijxj(,=)bi,  xj>0,i=1,2,3,4,⋅⋅⋅,mj=1,2,3,4,⋅⋅⋅,n
Based on the model, the objective function of linear programming can be a maximization problem or a minimization problem; some constraints are "≥", some are "≤", and they can also be "=".

1. Solution to Linear Programming

When a linear programming problem has an optimal solution, the value must be obtained at a certain vertex of the feasible region. When there is a unique solution, the optimal solution is a certain vertex of the feasible region. When there are infinitely many optimal solutions, at least one of them is a vertex of the feasible region.

2. Python solution to linear programming

SciPy's scipy.optimize module provides a function linprog for solving linear programming. The function concentrates on common algorithms for solving linear programming, such as the simplex method and the interior point method. It will solve the problem according to the scale of the problem or the user's specified selection algorithm.

1. Import the library

from scipy.optimize import linprog

2. Standard form of linear programming in SciPy

m i n   z = c T x ; min \ z = c^{T} x; min z=cTx;

s . t . {   A ⋅ x ≤ b A e q ⋅ x = b e q   L b ≤ U b s.t. \begin{cases} \ A·x≤b \\ Aeq·x=beq \\ \ L_{b}≤U_{b} \\ \end{cases} s.t.  AxbAeqx=b e q LbUb
Among them, c corresponds to the target vector in the above standard type,
A and b correspond to the inequality constraint, Aeq and beq correspond to the equal sign constraint, and
bounds is a tuple of n elements composed of the lower bound vector and the upper bound vector of the decision vector. The default lower bound of bounds is 0 and the upper bound is ∞;
the return value res.x is the optimal solution obtained, and res.fun is the optimal value of the objective function.
Corresponding function calling mode:

#linprog的基本调用格式为:
from scipy.optimize import linprog
res=linprog(c, A, b, Aeq, beq)  #默认每个决策变量下界为0,上界为 
res=linprog(c, A=None, b=None, Aeq=None, beq=None, bounds=None, method='simplex') 
print(res.fun)   #显示目标函数最小值
print(res.x)     #显示最优解

3. Training case 1

m i n   z = − x 1 + 4 x 2 ; min \ z = -x_{1}+4x_{2}; minz =x1+4x _2;
s . t . {   − 3 x 1 + x 2 ≤ 6 x 1 + 2 x 2 ≤ 4   x 2 ≥ − 3 s.t. \begin{cases} \ -3x_{1}+x_{2} ≤6 \\ x_{1}+2x_{2} ≤4 \\ \ x_{2}≥-3 \\ \end{cases} s.t.  3x _1+x26x1+2x _24 x23

The code is as follows (example):

from scipy.optimize import linprog
c = [-1, 4]; 
A = [[-3, 1], [1, 2]]
b = [6, 4]; 
#bounds是决策向量的下界向量和上界向量所组成的 n个元素的元组bounds的默认取值下界都是0,上界都是∞ 
bound=((None,None),(-3,None))

res=linprog(c,A,b,None,None,bound)
print("目标函数的最小值:",res.fun)
print("最优解为:",res.x)
#运行结果: 目标函数的最小值: -22.0
# 最优解为: [10. -3.]

4. Training case 2

m a x   z = x 1 − 2 x 2 − 3 x 3 ; max\ z = x_{1}-2x_{2}-3x_{3}; max z=x12x _23x _3;
s . t . {   − 2 x 1 + x 2 + x 3 ≤ 9 − 3 x 1 + x 2 + 2 x 3 ≥ 4 4 x 1 − 2 x 2 x 3 = − 6   x 1 ≥ − 10 , x 2 ≥ 0 s.t. \begin{cases} \ -2x_{1}+x_{2} +x_{3} ≤9 \\ -3x_{1}+x_{2} +2x_{3} ≥4\\ 4x_{1}-2x_{2} x_{3}=-6\\ \ x_{1}≥-10, x_{2}≥0\\ \end{cases} s.t.  2 x1+x2+x393x _1+x2+2x _344x _12x _2x3=6 x110,x20

4.1 Solving process

(1) First convert it into the standard form min z = − x 1 + 2 x 2 + 3 x 3 in SciPy ; min\ z = -x_{1}+2x_{2}+3x_{3};
min z=x1+2x _2+3x _3;
s . t . {   − 2 x 1 + x 2 + x 3 ≤ 9 3 x 1 − x 2 − 2 x 3 ≤ − 4 4 x 1 − 2 x 2 x 3 = − 6   x 1 ≥ − 10 , x 2 ≥ 0 s.t. \begin{cases} \ -2x_{1}+x_{2} +x_{3} ≤9 \\ 3x_{1}-x_{2} -2x_{3} ≤-4\\ 4x_{1}-2x_{2} x_{3}=-6\\ \ x_{1}≥-10, x_{2}≥0\\ \end{cases} s.t.  2 x1+x2+x393x _1x22x _344x _12x _2x3=6 x110,x20

from scipy.optimize import linprog
c=[-1, 2, 3]; 

A = [[-2, 1, 1], [3, -1, -2]]
b=[[9], [-4]]

Aeq=[[4, -2, -1]]
beq=[-6]
LB=[-10, 0, None]

UB=[None]*len(c)  #生成3个None的列表
bound = tuple(zip(LB, UB))   #生成决策向量界限的元组
res=linprog(c,A,b,Aeq,beq,bound)
print("目标函数的最小值:",res.fun)
print("最优解为:",res.x)

# 求解结果:目标函数的最小值: 0.399999999999999
# 最优解为: [-1.6  0.  -0.4]

Guess you like

Origin blog.csdn.net/m0_58857684/article/details/130649850