1、问题描述
某厂生产甲乙两种饮料,每百箱甲饮料需用原料6千克、工人10名,获利10万元;每百箱乙饮料需用原料5千克、工人20名,获利9万元。
今工厂共有原料60千克、工人150名,又由于其他条件所限甲饮料产量不超过8百箱。
(1)问如何安排生产计划,即两种饮料各生产多少使获利最大?
(2)若投资0.8万元可增加原料1千克,是否应作这项投资?投资多少合理?
(3)若每百箱甲饮料获利可增加1万元,是否应否改变生产计划?
(4)若每百箱甲饮料获利可增加1万元,若投资0.8万元可增加原料1千克,是否应作这项投资?投资多少合理?
(5)若不允许散箱(按整百箱生产),如何安排生产计划,即两种饮料各生产多少使获利最大?
2、用PuLP 库求解线性规划
2.1 问题 1
(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
(2)Python 编程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import pulp # 导入 pulp库
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)) # 输出最优解的目标函数值
# = 关注 Youcans,分享原创系列 https://blog.csdn.net/youcans =
|
(3)运行结果
1
2
3
4
|
ProbLP1
x1=6.4285714
x2=4.2857143
F1(X)=102.8571427
|
2.2 问题 2
(1)数学建模
问题建模:
决策变量:
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
(2)Python 编程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import pulp # 导入 pulp库
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)运行结果
1
2
3
4
5
|
ProbLP2
x1=8.0
x2=3.5
x3=4.4
F2(X)=107.1
|
2.3 问题 3
(1)数学建模
问题建模:
决策变量:
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
(2)Python 编程
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import pulp # 导入 pulp库
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)) # 输出最优解的目标函数值
|
(3)运行结果
1
2
3
4
|
ProbLP3
x1=8.0
x2=2.4
F3(X) = 109.6
|
2.4 问题 4
(1)数学建模
问题建模:
决策变量:
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
(2)Python 编程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import pulp # 导入 pulp库 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)) # 输出最优解的目标函数值
# = 关注 Youcans,分享原创系列 https://blog.csdn.net/youcans =
|
(3)运行结果
1
2
3
4
5
|
ProbLP4
x1=8.0
x2=3.5
x3=4.4
F4(X) = 115.1
|
2.5 问题 5:整数规划问题
(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 为整数
推导条件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
因此,0 <= x1<=8,0 <= x2<=7
说明:本题中要求饮料车辆为整百箱,即决策变量 x1,x2 为整数,因此是整数规划问题。PuLP提供了整数规划的
(2)Python 编程
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import pulp # 导入 pulp库
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)) # 输出最优解的目标函数值
|
(3)运行结果
1
2
3
4
|
ProbLP5
x1=8.0
x2=2.0
F5(X) = 98.0
|
以上就是Python数学建模PuLP库线性规划实际案例编程详解的详细内容,更多关于PuLP库线性规划实际编程案例的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/youcans/article/details/116371509