线性规划问题的建模与求解(例1:离散制造中的混合产品生产规划问题)
离散制造工厂生产规划问题
一家机械厂承接两种不同型号的轴承零件生产任务,这两种轴承零件的生产工艺过程和材料一致,每天生产这两种轴承的相关资料如下表所示:
轴承型号 | 消耗材料 | 加工时间 | 存储空间 | 利润值 |
---|---|---|---|---|
ZC1 | 4.5 | 25 | 3.5 | 65 |
ZC2 | 3 | 18 | 2 | 52 |
生产能力 | 300 | 480 | 250 | — |
已知生产ZC1的利润为65元每件,生成ZC2的利润为每件52元,求每天应该怎样安排生产,才能使利润最大?
分析:
设 x 1 x_1 x1和 x 2 x_2 x2分别为每天生产轴承ZC1和ZC2的数量,该问题的目标是总利润最大 Z = 65 x 1 + 52 x 2 Z=65x_1+52x_2 Z=65x1+52x2,同时还需满足以下约束条件:
- 材料约束: 4.5 x 1 + 3 x 2 ≤ 300 4.5x_1+3x_2 \leq 300 4.5x1+3x2≤300;
- 时间约束: 25 x 1 + 18 x 2 ≤ 480 25x_1+18x_2 \leq 480 25x1+18x2≤480;
- 存储约束: 3.5 x 1 + 2 x 2 ≤ 250 3.5x_1+2x_2 \leq 250 3.5x1+2x2≤250;
- 非负约束: x 1 ≥ 0 , 3 x 2 ≥ 0 x_1 \geq 0 , 3x_2 \geq 0 x1≥0,3x2≥0;
由此,得到数学模型:
m a x Z = 65 x 1 + 52 x 2 s. t. { 4.5 x 1 + 3 x 2 ≤ 300 25 x 1 + 18 x 2 ≤ 480 3.5 x 1 + 2 x 2 ≤ 250 x 1 ≥ 0 x 2 ≥ 0 max \ Z = 65x_1+52x_2 \\ \text{s. t. } \begin{cases} 4.5x_1+3x_2 \leq 300\\ 25x_1+18x_2 \leq 480\\ 3.5x_1+2x_2 \leq 250 \\ x_1 \geq 0 \\ x_2 \geq 0 \end{cases} max Z=65x1+52x2s. t. ⎩ ⎨ ⎧4.5x1+3x2≤30025x1+18x2≤4803.5x1+2x2≤250x1≥0x2≥0
使用python求解
-
首先要目标函数转换为求最小值的标准形式
-
同时将线性不等式约束也转换成小于等于形式,,即:
m i n Z = − 65 x 1 − 52 x 2 s. t. { 4.5 x 1 + 3 x 2 ≤ 300 25 x 1 + 18 x 2 ≤ 480 3.5 x 1 + 2 x 2 ≤ 250 − x 1 ≤ 0 − x 2 ≤ 0 min \ Z = -65x_1-52x_2 \\ \text{s. t. } \begin{cases} 4.5x_1+3x_2 \leq 300\\ 25x_1+18x_2 \leq 480\\ 3.5x_1+2x_2 \leq 250 \\ -x_1 \leq 0 \\ -x_2 \leq 0 \end{cases} min Z=−65x1−52x2s. t. ⎩ ⎨ ⎧4.5x1+3x2≤30025x1+18x2≤4803.5x1+2x2≤250−x1≤0−x2≤0
import numpy as np
from scipy.optimize import linprog
c = np.array([-65,-52])
A_ub = np.array([[4.5,3],
[25,18],[3.5,2],[-1,0],[0,-1]])
b_ub = np.array([300,480,250,0,0])
result = linprog(c, A_ub=A_ub, b_ub=b_ub)
print(result.message)
Optimization terminated successfully. (HiGHS Status 7: Optimal)
- 结果显示优化成功,这个模型中两个决策变量的非负约束可以通过函数中的设置变量取值范围来约束,代码如下:
- 计算结果
import numpy as np
from scipy.optimize import linprog
c = np.array([-65,-52])
A_ub = np.array([[4.5,3],
[25,18],[3.5,2]])
b_ub = np.array([300,480,250])
x0_bounds = (0, np.inf) # +/- np.inf can be used instead of None
x1_bounds = (0, None) # +/- np.inf can be used instead of None
bounds = [x0_bounds, x1_bounds]
result = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=bounds)
print(result.x)
[ 0. 26.66666667]
- 运行成功,打印结果为非整数,这是由于建模中仅考虑线性优化,没有考虑产品的整数约束;
- 增加整数约束,重新编写代码并执行:
integrality = np.ones_like(c)
b_l = np.full_like(b_ub, 0)
from scipy.optimize import LinearConstraint
constraints = LinearConstraint(A_ub, b_l, b_ub)
from scipy.optimize import milp
res = milp(c=c,constraints=constraints, integrality=integrality)
res.x
res
fun: -1365.0
message: 'Optimization terminated successfully. (HiGHS Status 7: Optimal)'
mip_dual_bound: -1365.0
mip_gap: 0.0
mip_node_count: 1
status: 0
success: True
x: array([ 1., 25.])
- 输出最优值结果为 x 1 = 1 , x 2 = 25 x_1 = 1, x_2 = 25 x1=1,x2=25,此时目标值为1365.