Optimization model (1) Detailed explanation of linear programming, as well as examples, using Python's Pulp library function to solve linear programming

Mathematical modeling series of articles:

The following are some model algorithms and codes compiled by me when preparing for the National Digital Analog Competition. I will update the content when I have time:
Evaluation Model (1) Analytic Hierarchy Process (AHP) , Entropy weight method, TOPSIS analysis and its corresponding PYTHON implementation code and explanation of examples
Evaluation model (2) Principal component analysis, factor analysis, comparison between the two and their corresponding explanation of PYTHON implementation code and examples< /span>Optimization model (2) Detailed explanation of nonlinear programming, and examples, Scipy.optimize solves nonlinear programmingOptimization model (1) Detailed explanation of linear programming, and examples, Use Python's Pulp library function to solve linear programming
Optimization model (zero) Overview, classification, analysis of various optimization models and universal problem-solving steps

3.2 Linear programming Pulp library function solution

For basic knowledge about optimization problems, seeOverview

Linear programming definition:

If the objective function f(x) and the constraints are both linear expressions of the decision variables, then the mathematical programming problem at this time belongs to linear programming. It is more convenient to use Python’s pulp library to solve the linear programming problem. If it is not Linear functions are generally solved usingscipy.optimize

PuLP library solves linear programming

PuLP is an open source third-party toolkit that can solve linear programming, integer programming, and mixed integer programming problems.

The following will first introduce some basic library functions of Pulp, and then take some examples< /span>Repeated explanation

Some basic function definitions:

pulp.LpProblem("LPProbDemo1", sense=pulp.LpMaximize)

pulp.LpProblem is the constructor that defines the problem.
LPProbDemo1 is the user-defined question name (used for output information).
sense is used to specify the minimum/maximum problem. Optional parameter values: LpMinimize, LpMaximize

x1 = pulp.LpVariable('x1', lowBound=0, upBound=7, cat='Continuous')

x1is a user-defined variable name

lowBound、upBound is used to set the lower and upper bounds of decision variables; the lower and upper bounds do not need to be defined, and the default lower and upper bounds are negative infinity/positive infinity. In this example, the value range of x1, x2, x3 is [0,7].
cat is used to set the variable type, optional parameter value:

  • ‘Continuous’ represents continuous variables (default value),
  • ‘Integer’ represents discrete variables (used in integer programming problems),
  • ‘Binary’ represents a 0/1 variable (used in 0/1 planning problems).

pulp.value(MyProbLP.objective)

The maximum value of the objective function, that is, the result

pulp.LpStatus[MyProbLP.status]

Output solution status if Status: Optimal is a reasonable status

Example 1 A simple solution to linear programming problem

max		fx = 2*x1 + 3*x2 - 5*x3
s.t.	x1 + 3*x2 + x3 <= 12
		2*x1 - 5*x2 + x3 >= 10
		x1 + x2 + x3 = 7
		x1, x2, x3 >=0

Solving code:

# 导入 PuLP库函数
import pulp
# 定义一个规划问题
MyProbLP = pulp.LpProblem("LPProbDemo1", sense=pulp.LpMaximize)
'''
pulp.LpProblem 是定义问题的构造函数。
  "LPProbDemo1"是用户定义的问题名(用于输出信息)。
   sense 用来指定求最小值/最大值问题,可选参数值:LpMinimize、LpMaximize 。
'''
# 定义决策变量
x1 = pulp.LpVariable('x1', lowBound=0, upBound=7, cat='Continuous') 
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7, cat='Continuous')
x3 = pulp.LpVariable('x3', lowBound=0, upBound=7, cat='Continuous') 
'''
pulp.LpVariable 是定义决策变量的函数。
  ‘x1’ 是用户定义的变量名。
  参数 lowBound、upBound 用来设定决策变量的下界、上界;可以不定义下界/上界,默认的下界/上界是负无穷/正无穷。本例中 x1,x2,x3 的取值区间为 [0,7]。
  参数 cat 用来设定变量类型,可选参数值:
  ‘Continuous’ 表示连续变量(默认值)、
  ’Integer ’ 表示离散变量(用于整数规划问题)、
  ’Binary ’ 表示0/1变量(用于0/1规划问题)。
'''
MyProbLP += 2*x1 + 3*x2 - 5*x3  	# 设置目标函数
MyProbLP += (2*x1 - 5*x2 + x3 >= 10)  # 不等式约束
MyProbLP += (x1 + 3*x2 + x3 <= 12)  # 不等式约束
MyProbLP += (x1 + x2 + x3 == 7)  # 等式约束
MyProbLP.solve()

# 输出求解状态
print("Status:", pulp.LpStatus[MyProbLP.status]) 

# 输出每个变量的最优值
for v in MyProbLP.variables():
    print(v.name, "=", v.varValue)  
    
# 输出最优解的目标函数值 
print("F(x) = ", pulp.value(MyProbLP.objective))  


# 输出结果如下
'''
Optimal - objective value 14.571429
Optimal objective 14.57142857 - 2 iterations time 0.002
Option for printingOptions changed from normal to all
Total time (CPU seconds):   0.00   (Wallclock seconds):  0.00

Status: Optimal
x1 = 6.4285714
x2 = 0.57142857
x3 = 0.0
F(x) =  14.57142851
'''

Linear Programming Example 2 (from Qingfeng Modeling Courseware)

Insert image description here

Solving code:

# 公式就是图上的公式
import pulp
MyProbLP = pulp.LpProblem("LPProbDemo1", sense=pulp.LpMaximize)

# 定义整型变量  'Integer'  范围是(0,+inf)
x1 = pulp.LpVariable('x1', lowBound=0, cat='Integer')
x2 = pulp.LpVariable('x2', lowBound=0, cat='Integer')
x3 = pulp.LpVariable('x3', lowBound=0, cat='Integer')
x4 = pulp.LpVariable('x4', lowBound=0, cat='Integer')
x5 = pulp.LpVariable('x5', lowBound=0, cat='Integer')
x6 = pulp.LpVariable('x6', lowBound=0, cat='Integer')
x7 = pulp.LpVariable('x7', lowBound=0, cat='Integer')
x8 = pulp.LpVariable('x8', lowBound=0, cat='Integer')
x9 = pulp.LpVariable('x9', lowBound=0, cat='Integer')

# 设置目标函数
MyProbLP += 1*(x1+x2) + 1.65*x8 + 2.3*x9 - 0.05*(5*x1+10*x6)-0.0321*(7*x2+9*x7+12*x9)-0.0625*(6*x3+8*x8)-0.111857*(4*x4+11*x9)-0.05*7*x5 

# 设置约束函数
MyProbLP += (5*x1 + 10*x6  <= 6000)  # 不等式约束
MyProbLP += (7*x2+9*x7+12*x9 <= 10000)  # 不等式约束
MyProbLP += (6*x3+8*x8 <= 4000)  # 不等式约束
MyProbLP += (4*x4+11*x9 <= 7000)  # 不等式约束
MyProbLP += (7*x5 <= 4000)  # 不等式约束
MyProbLP += (x1 + x2  ==  x3 + x4 + x5)  # 等式约束
MyProbLP += (x6 + x7  ==  x8)  # 等式约束
MyProbLP.solve()

# 输出求解状态
print("Status:", pulp.LpStatus[MyProbLP.status]) 

# 输出每个变量的最优值
for v in MyProbLP.variables():
    print(v.name, "=", v.varValue)  
    
#输出最优解的目标函数值
print("F(x) = ", pulp.value(MyProbLP.objective))  

Output results

'''
Status: Optimal
x1 = 1200.0
x2 = 230.0
x3 = 0.0
x4 = 859.0
x5 = 571.0
x6 = 0.0
x7 = 500.0
x8 = 500.0
x9 = 324.0
F(x) =  1146.4152
'''

Guess you like

Origin blog.csdn.net/m0_63669388/article/details/132703024