目录
1 概述
2 算例及Matlab代码实现
2.1 算例
2.2 Python代码实现
2.3 求解结果
规划问题的数学模型一般由三个因素构成
决策变量 目标函数 约束条件
数学规划是运筹学的一个重要分支,线性规划是数学规划的一个重要分支
线性规划即以线性函数为目标函数,线性条件为约束条件
建立线性规划模型的基本步骤
(1)分析问题,找出决策变量
(2)根据问题,找出决策变量必须满足的一组线性等式或者不等式约束,即为约束条件
(3)根据问题的目标,构造关于决策变量的一个线性函数,即为目标函数
from gurobipy import * # 在Python中调用gurobi求解包
Max_Obj=1 # 用于判定目标函数求最大值还是求最小值
M_LP=Model("LP") #线性规划问题
# 变量声明
OP =M_LP.addVar(lb=-GRB.INFINITY,ub=GRB.INFINITY, name="OP")
x1 =M_LP.addVar(lb=0,ub=5, name="x1")
x2 =M_LP.addVar(lb=0,ub=3, name="x2")
x3 =M_LP.addVar(lb=0,ub=2, name="x3")
# 对变量边界进行限定的第二种写法:以x1为例
# M_LP.addConstr(x1>=0,"Bound_Con11")
# M_LP.addConstr(x1<=5,"Bound_Con12")
# 设置目标函数
if Max_Obj==0:
M_LP.setObjective(x1+2*x2-3*x3,GRB.MINIMIZE)
else:
M_LP.setObjective(x1+2*x2-3*x3,GRB.MAXIMIZE)
# 添加约束
M_LP.addConstr(x1+2*x2<=3,"Con1")
M_LP.addConstr(x2+x3<=2,"Con2")
M_LP.addConstr(x1+x2+x3==4,"Con3")
# Optimize model
M_LP.optimize()
M_LP.write("LP.lp")
print('**************')
print(' The optimal solution ')
print('**************')
print('OP is :',M_LP.ObjVal) # 输出目标值
print('x1 is :',x1.x) # .x 用于输出 X1 的值
print('x2 is :',x2.x)
print('x3 is :',x3.x)
from gurobipy import * # 在Python中调用gurobi求解包 Max_Obj=1 # 用于判定目标函数求最大值还是求最小值 M_LP=Model("LP") #线性规划问题 # 变量声明 OP =M_LP.addVar(lb=-GRB.INFINITY,ub=GRB.INFINITY, name="OP") x1 =M_LP.addVar(lb=0,ub=5, name="x1") x2 =M_LP.addVar(lb=0,ub=3, name="x2") x3 =M_LP.addVar(lb=0,ub=2, name="x3") # 对变量边界进行限定的第二种写法:以x1为例 # M_LP.addConstr(x1>=0,"Bound_Con11") # M_LP.addConstr(x1<=5,"Bound_Con12") # 设置目标函数 if Max_Obj==0: M_LP.setObjective(x1+2*x2-3*x3,GRB.MINIMIZE) else: M_LP.setObjective(x1+2*x2-3*x3,GRB.MAXIMIZE) # 添加约束 M_LP.addConstr(x1+2*x2<=3,"Con1") M_LP.addConstr(x2+x3<=2,"Con2") M_LP.addConstr(x1+x2+x3==4,"Con3") # Optimize model M_LP.optimize() M_LP.write("LP.lp") print('**************') print(' The optimal solution ') print('**************') print('OP is :',M_LP.ObjVal) # 输出目标值 print('x1 is :',x1.x) # .x 用于输出 X1 的值 print('x2 is :',x2.x) print('x3 is :',x3.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, 4 columns and 7 nonzeros
Model fingerprint: 0xdcaef29a
Coefficient statistics:
Matrix range [1e+00, 2e+00]
Objective range [1e+00, 3e+00]
Bounds range [2e+00, 5e+00]
RHS range [2e+00, 4e+00]
Presolve removed 1 rows and 2 columns
Presolve time: 0.00s
Presolved: 2 rows, 2 columns, 4 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 1.0100000e+00 1.004000e+00 0.000000e+00 0s
1 -0.0000000e+00 0.000000e+00 0.000000e+00 0s
Solved in 1 iterations and 0.00 seconds (0.00 work units)
Optimal objective -0.000000000e+00
**************
The optimal solution
**************
OP is : -0.0
x1 is : 3.0
x2 is : 0.0
x3 is : 1.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, 4 columns and 7 nonzeros
Model fingerprint: 0xdcaef29a
Coefficient statistics:
Matrix range [1e+00, 2e+00]
Objective range [1e+00, 3e+00]
Bounds range [2e+00, 5e+00]
RHS range [2e+00, 4e+00]
Presolve removed 1 rows and 2 columns
Presolve time: 0.00s
Presolved: 2 rows, 2 columns, 4 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 1.0100000e+00 1.004000e+00 0.000000e+00 0s
1 -0.0000000e+00 0.000000e+00 0.000000e+00 0s
Solved in 1 iterations and 0.00 seconds (0.00 work units)
Optimal objective -0.000000000e+00
**************
The optimal solution
**************
OP is : -0.0
x1 is : 3.0
x2 is : 0.0
x3 is : 1.0
Process finished with exit code 0