目录
1 概述
2 算例实现
3 Python+Gurobi求解线性规划
3.1 求解步骤
3.2 Python代码
3.3 求解结果
在人们的生产实践中,经常会遇到如何利用现有资源来安排生产,以取得最大经济效益的问题。此类问题构成了运筹学的一个重要分支—数学规划,而线性规划(Linear Programming 简记LP)则是数学规划的一个重要分支。自从1947年G.B.Dantzig提出求解线性规划的单纯形方法以来,线性规划在理论上趋向成熟,在实用中日益广泛与深入。特别是在计算机能处理成千上万个约束条件和决策变量的线性规划问题之后,线性规划的适用领域更为广泛了,已成为现代管理中经常采用的基本方法之一。
线性规划前面已经总结很多,文章链接:
线性规划(Python各种包实现及Yalmip+Cplex)
'''1导入优化包'''
from gurobipy import * # 在Python中调用gurobi求解包
'''2 模型建立'''
#=============2.1变量声明=============
LP=Model("LP_Exam") # LP_Exam线性规划模型,直接套用就行
OP =LP.addVar(lb=-GRB.INFINITY,ub=GRB.INFINITY, name="OP")
x1 =LP.addVar(lb=-GRB.INFINITY,ub=GRB.INFINITY, name="x1")
x2 =LP.addVar(lb=-3,ub=GRB.INFINITY, name="x2")
#===========2.2设置目标函数=========
LP.setObjective(-2*x1+4*x2,GRB.MINIMIZE)
#===========2.3添加约束===========
LP.addConstr(-3*x1+x2<=6,"Con1")
LP.addConstr(x1+2*x2>=4,"Con2")
LP.addConstr(x1+3*x2==4,"Con3")
'''3模型求解'''
LP.optimize()
print("输出名为‘LP_Expression’的 .lp文件")
LP.write("LP_Expression.lp")
# 输出结果
print('=========================')
print('====最优解为========')
print('===========================')
print('OP is :',LP.ObjVal) # 输出目标值
print('x1 is :',x1.x) # 输出 X1 的值
print('x2 is :',x2.x)
'''1导入优化包''' from gurobipy import * # 在Python中调用gurobi求解包 '''2 模型建立''' #=============2.1变量声明============= LP=Model("LP_Exam") # LP_Exam线性规划模型,直接套用就行 OP =LP.addVar(lb=-GRB.INFINITY,ub=GRB.INFINITY, name="OP") x1 =LP.addVar(lb=-GRB.INFINITY,ub=GRB.INFINITY, name="x1") x2 =LP.addVar(lb=-3,ub=GRB.INFINITY, name="x2") #===========2.2设置目标函数========= LP.setObjective(-2*x1+4*x2,GRB.MINIMIZE) #===========2.3添加约束=========== LP.addConstr(-3*x1+x2<=6,"Con1") LP.addConstr(x1+2*x2>=4,"Con2") LP.addConstr(x1+3*x2==4,"Con3") '''3模型求解''' LP.optimize() print("输出名为‘LP_Expression’的 .lp文件") LP.write("LP_Expression.lp") # 输出结果 print('=========================') print('====最优解为========') print('===========================') print('OP is :',LP.ObjVal) # 输出目标值 print('x1 is :',x1.x) # 输出 X1 的值 print('x2 is :',x2.x)
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (win64)
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads
Optimize a model with 3 rows, 3 columns and 6 nonzeros
Model fingerprint: 0x0b0b64fc
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [2e+00, 4e+00]
Bounds range [3e+00, 3e+00]
RHS range [4e+00, 6e+00]
Presolve removed 3 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 -3.8000000e+01 0.000000e+00 2.000000e-06 0s
Solved in 0 iterations and 0.01 seconds (0.00 work units)
Optimal objective -3.800000000e+01
输出名为‘LP_Expression’的 .lp文件
=========================
====最优解为========
===========================
OP is : -38.0
x1 is : 13.0
x2 is : -3.0
Process finished with exit code 0
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (win64)
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads
Optimize a model with 3 rows, 3 columns and 6 nonzeros
Model fingerprint: 0x0b0b64fc
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [2e+00, 4e+00]
Bounds range [3e+00, 3e+00]
RHS range [4e+00, 6e+00]
Presolve removed 3 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 -3.8000000e+01 0.000000e+00 2.000000e-06 0s
Solved in 0 iterations and 0.01 seconds (0.00 work units)
Optimal objective -3.800000000e+01
输出名为‘LP_Expression’的 .lp文件
=========================
====最优解为========
===========================
OP is : -38.0
x1 is : 13.0
x2 is : -3.0
Process finished with exit code 0