什么是线性规划
线性规划(Linear programming),在线性等式或不等式约束条件下求解线性目标函数的极值问题,常用于解决资源分配、生产调度和混合问题。
线性规划问题的建模和求解,通常按照以下步骤进行:
PuLP 库求解线性规划
PuLP是一个开源的第三方工具包,可以求解线性规划、整数规划、混合整数规划问题。下面以该题为例讲解 PuLP 求解线性规划问题的步骤:
实例
某厂生产甲乙两种饮料,每百箱甲饮料需用原料6千克、工人10名,获利10万元;每百箱乙饮料需用原料5千克、工人20名,获利9万元。
今工厂共有原料60千克、工人150名,又由于其他条件所限甲饮料产量不超过8百箱。
问题1建模:
决策变量:
x1:甲饮料产量(单位:百箱)
x2:乙饮料产量(单位:百箱)
目标函数:
max fx = 10*x1 + 9*x2
约束条件:
6*x1 + 5*x2 <= 60
10*x1 + 20*x2 <= 150
取值范围:
给定条件:x1, x2 >= 0,x1 <= 8
推导条件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
因此,0 <= x1<=8,0 <= x2<=7.5
ProbLP1 = pulp.LpProblem("ProbLP1", sense=pulp.LpMaximize) # 定义问题 1,求最大值
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous') # 定义 x1
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous') # 定义 x2
ProbLP1 += (10*x1 + 9*x2) # 设置目标函数 f(x)
ProbLP1 += (6*x1 + 5*x2 <= 60) # 不等式约束
ProbLP1 += (10*x1 + 20*x2 <= 150) # 不等式约束
ProbLP1.solve()
print(ProbLP1.name) # 输出求解状态
print("Status:", pulp.LpStatus[ProbLP1.status]) # 输出求解状态
for v in ProbLP1.variables():
print(v.name, "=", v.varValue) # 输出每个变量的最优值
print("F1(x)=", pulp.value(ProbLP1.objective)) # 输出最优解的目标函数值
问题2建模:
决策变量:
x1:甲饮料产量(单位:百箱)
x2:乙饮料产量(单位:百箱)
x3:增加投资(单位:万元)
目标函数:
max fx = 10*x1 + 9*x2 - x3
约束条件:
6*x1 + 5*x2 <= 60 + x3/0.8
10*x1 + 20*x2 <= 150
取值范围:
给定条件:x1, x2 >= 0,x1 <= 8
推导条件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
因此,0 <= x1<=8,0 <= x2<=7.5
ProbLP2 = pulp.LpProblem("ProbLP2", sense=pulp.LpMaximize) # 定义问题 2,求最大值
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous') # 定义 x1
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous') # 定义 x2
x3 = pulp.LpVariable('x3', cat='Continuous') # 定义 x3
ProbLP2 += (10*x1 + 9*x2 - x3) # 设置目标函数 f(x)
ProbLP2 += (6*x1 + 5*x2 - 1.25*x3 <= 60) # 不等式约束
ProbLP2 += (10*x1 + 20*x2 <= 150) # 不等式约束
ProbLP2.solve()
print(ProbLP2.name) # 输出求解状态
print("Status:", pulp.LpStatus[ProbLP2.status]) # 输出求解状态
for v in ProbLP2.variables():
print(v.name, "=", v.varValue) # 输出每个变量的最优值
print("F2(x)=", pulp.value(ProbLP2.objective)) # 输出最优解的目标函数值
问题3建模:
决策变量:
x1:甲饮料产量(单位:百箱)
x2:乙饮料产量(单位:百箱)
目标函数:
max fx = 11*x1 + 9*x2
约束条件:
6*x1 + 5*x2 <= 60
10*x1 + 20*x2 <= 150
取值范围:
给定条件:x1, x2 >= 0,x1 <= 8
推导条件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
因此,0 <= x1<=8,0 <= x2<=7.5
ProbLP3 = pulp.LpProblem("ProbLP3", sense=pulp.LpMaximize) # 定义问题 3,求最大值
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous') # 定义 x1
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous') # 定义 x2
ProbLP3 += (11 * x1 + 9 * x2) # 设置目标函数 f(x)
ProbLP3 += (6 * x1 + 5 * x2 <= 60) # 不等式约束
ProbLP3 += (10 * x1 + 20 * x2 <= 150) # 不等式约束
ProbLP3.solve()
print(ProbLP3.name) # 输出求解状态
print("Status:", pulp.LpStatus[ProbLP3.status]) # 输出求解状态
for v in ProbLP3.variables():
print(v.name, "=", v.varValue) # 输出每个变量的最优值
print("F3(x) =", pulp.value(ProbLP3.objective)) # 输出最优解的目标函数值
问题4建模:
决策变量:
x1:甲饮料产量(单位:百箱)
x2:乙饮料产量(单位:百箱)
x3:增加投资(单位:万元)
目标函数:
max fx = 11*x1 + 9*x2 - x3
约束条件:
6*x1 + 5*x2 <= 60 + x3/0.8
10*x1 + 20*x2 <= 150
取值范围:
给定条件:x1, x2 >= 0,x1 <= 8
推导条件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
因此,0 <= x1<=8,0 <= x2<=7.5
ProbLP4 = pulp.LpProblem("ProbLP4", sense=pulp.LpMaximize) # 定义问题 2,求最大值
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous') # 定义 x1
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous') # 定义 x2
x3 = pulp.LpVariable('x3', cat='Continuous') # 定义 x3
ProbLP4 += (11 * x1 + 9 * x2 - x3) # 设置目标函数 f(x)
ProbLP4 += (6 * x1 + 5 * x2 - 1.25 * x3 <= 60) # 不等式约束
ProbLP4 += (10 * x1 + 20 * x2 <= 150) # 不等式约束
ProbLP4.solve()
print(ProbLP4.name) # 输出求解状态
print("Status:", pulp.LpStatus[ProbLP4.status]) # 输出求解状态
for v in ProbLP4.variables():
print(v.name, "=", v.varValue) # 输出每个变量的最优值
print("F4(x) = ", pulp.value(ProbLP4.objective)) # 输出最优解的目标函数值
问题5建模:
决策变量:
x1:甲饮料产量,正整数(单位:百箱)
x2:乙饮料产量,正整数(单位:百箱)
目标函数:
max fx = 10*x1 + 9*x2
约束条件:
6*x1 + 5*x2 <= 60
10*x1 + 20*x2 <= 150
取值范围:
给定条件:x1, x2 >= 0,x1 <= 8,x1, x2 为整数
推导条件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
因此,0 <= x1<=8,0 <= x2<=7
说明:本题中要求饮料车辆为整百箱,即决策变量 x1,x2 为整数,因此是整数规划问题。
ProbLP5 = pulp.LpProblem("ProbLP5", sense=pulp.LpMaximize) # 定义问题 1,求最大值
x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Integer') # 定义 x1,变量类型:整数
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Integer') # 定义 x2,变量类型:整数
ProbLP5 += (10 * x1 + 9 * x2) # 设置目标函数 f(x)
ProbLP5 += (6 * x1 + 5 * x2 <= 60) # 不等式约束
ProbLP5 += (10 * x1 + 20 * x2 <= 150) # 不等式约束
ProbLP5.solve()
print(ProbLP5.name) # 输出求解状态
print("Status:", pulp.LpStatus[ProbLP5.status]) # 输出求解状态
for v in ProbLP5.variables():
print(v.name, "=", v.varValue) # 输出每个变量的最优值
print("F5(x) =", pulp.value(ProbLP5.objective)) # 输出最优解的目标函数值