司守奎《数学建模算法与应用》第二版 第一章线性规划习题答案
1.1
from scipy.optimize import linprog
import numpy as np
if __name__ == "__main__":
c = np.array([-3, 1, 1])
A = np.array([[1, -2, 1], [4, -1, -2]])
b = np.array([[11, -3]]).T
Aeq = np.array([[-2, 0, 1]])
beq = 1
lb = np.zeros(3)
ub = np.array([[None] * 3])
bound = np.vstack((lb, ub)).T
res = linprog(c, A, b, Aeq, beq, bound)
print("最优值为", -res.fun)
print("最优解为", res.x)
# output:最优值为 2.0
# 最优解为 [4. 1. 9.]
1.2
做变换
u
i
=
∣
x
i
∣
+
x
i
2
,
v
i
=
∣
x
i
∣
−
x
i
2
,
i
=
1
,
2
,
3
,
4
u_i=\frac{|x_i|+x_i}{2},v_i=\frac{|x_i|-x_i}{2},i=1,2,3,4
ui=2∣xi∣+xi,vi=2∣xi∣−xi,i=1,2,3,4,可以得到
min
z
=
u
1
+
v
1
+
2
(
u
2
+
v
2
)
+
3
(
u
3
+
v
3
)
+
4
(
u
4
+
v
4
)
s
.
t
.
{
u
1
−
v
1
−
(
u
2
−
v
2
)
−
(
u
3
−
v
3
)
+
u
4
−
v
4
=
0
u
1
−
v
1
−
(
u
2
−
v
2
)
+
u
3
−
v
3
−
3
(
u
4
−
v
4
)
=
1
u
1
−
v
1
−
(
u
2
−
v
2
)
−
2
(
u
3
−
v
3
)
+
3
(
u
4
−
v
4
)
=
−
1
2
u
i
,
v
i
≥
0
,
i
=
1
,
2
,
3
,
4
\min\ z=u_1+v_1+2(u_2+v_2)+3(u_3+v_3)+4(u_4+v_4)\\ \begin{equation} s.t. \begin{cases} u_1-v_1-(u_2-v_2)-(u_3-v_3)+u_4-v_4=0\\ u_1-v_1-(u_2-v_2)+u_3-v_3-3(u_4-v_4)=1\\ u_1-v_1-(u_2-v_2)-2(u_3-v_3)+3(u_4-v_4)=-\frac{1}{2}\\ u_i,v_i\geq 0,&i=1,2,3,4 \end{cases} \end{equation}
min z=u1+v1+2(u2+v2)+3(u3+v3)+4(u4+v4)s.t.⎩
⎨
⎧u1−v1−(u2−v2)−(u3−v3)+u4−v4=0u1−v1−(u2−v2)+u3−v3−3(u4−v4)=1u1−v1−(u2−v2)−2(u3−v3)+3(u4−v4)=−21ui,vi≥0,i=1,2,3,4
from scipy.optimize import linprog
import numpy as np
if __name__ == "__main__":
c = np.array([1, 2, 3, 4])
c = np.append(c, c, axis=0)
Aeq = np.array([[1, -1, -1, 1], [1, -1, 1, -3], [1, -1, -2, 3]])
Aeq = np.append(Aeq, -Aeq, axis=1)
beq = np.array([[0, 1, -1 / 2]]).T
lb = np.zeros(8)
ub = np.array([[None] * 8])
bound = np.vstack((lb, ub)).T
res = linprog(c, A_ub=None, b_ub=None, A_eq=Aeq, b_eq=beq, bounds=bound)
print("最优值为", res.fun)
print("最优解为", res.x[:4] - res.x[4:])
# output:最优值为 1.25
# 最优解为 [ 0.25 0. 0. -0.25]
1.3
假设
I
I
I产品需要
A
1
A_1
A1工序
x
1
x_1
x1次,
A
2
A_2
A2工序
x
2
x_2
x2次,需要
B
1
B_1
B1工序
x
3
x_3
x3次,
B
2
B_2
B2工序
x
4
x_4
x4次,
B
3
B_3
B3工序
x
5
x_5
x5次;
I
I
II
II差产品需要
A
1
A_1
A1工序
x
6
x_6
x6次,
A
2
A_2
A2工序
x
7
x_7
x7次,
B
1
B_1
B1工序
x
8
x_8
x8次;
I
I
I
III
III产品需要
A
2
A_2
A2工序
x
9
x_9
x9次,
B
2
B_2
B2工序
x
10
x_{10}
x10次。则由题意可得
max
总利润
=
(
1.25
−
0.25
)
(
x
1
+
x
2
)
+
(
2
−
0.35
)
x
8
+
(
2.8
−
0.5
)
x
9
−
300
6000
(
5
x
1
+
10
x
6
)
−
321
10000
(
7
x
2
+
9
x
7
+
12
x
9
)
−
250
4000
(
6
x
3
+
8
x
8
)
−
783
7000
(
4
x
4
+
11
x
10
)
s
.
t
.
{
5
x
1
+
10
x
6
≤
6000
7
x
2
+
9
x
7
+
12
x
9
≤
10000
6
x
3
+
8
x
8
≤
4000
4
x
4
+
11
x
10
≤
7000
7
x
5
≤
4000
x
1
+
x
2
≥
1
x
8
≥
1
x
9
=
x
10
≥
1
x
i
≥
0
,
i
=
1
,
2
,
3
,
4
,
5
,
6
,
7
x
1
+
x
2
=
x
3
+
x
4
+
x
5
x
6
+
x
7
=
x
8
\max\ 总利润=(1.25-0.25)(x_1+x_2)+(2-0.35)x_8+(2.8-0.5)x_9-\frac{300}{6000}(5x_1+10x_6)-\frac{321}{10000}(7x_2+9x_7+12x_9)-\frac{250}{4000}(6x_3+8x_8)-\frac{783}{7000}(4x_4+11x_{10})\\ \begin{equation} s.t. \begin{cases} 5x_1+10x_6\leq 6000\\ 7x_2+9x_7+12x_9\leq 10000\\ 6x_3+8x_8\leq 4000\\ 4x_4+11x_{10}\leq 7000\\ 7x_5\leq 4000\\ x_1+x_2\geq 1\\ x_8\geq 1\\ x_9=x_{10}\geq 1\\ x_i\geq 0,&i=1,2,3,4,5,6,7\\ x_1+x_2=x_3+x_4+x_5\\ x_6+x_7=x_8\\ \end{cases} \end{equation}
max 总利润=(1.25−0.25)(x1+x2)+(2−0.35)x8+(2.8−0.5)x9−6000300(5x1+10x6)−10000321(7x2+9x7+12x9)−4000250(6x3+8x8)−7000783(4x4+11x10)s.t.⎩
⎨
⎧5x1+10x6≤60007x2+9x7+12x9≤100006x3+8x8≤40004x4+11x10≤70007x5≤4000x1+x2≥1x8≥1x9=x10≥1xi≥0,x1+x2=x3+x4+x5x6+x7=x8i=1,2,3,4,5,6,7
from scipy.optimize import linprog
import numpy as np
if __name__ == "__main__":
x1 = (1.25 - 0.25) - 300 * 5 / 6000
x2 = (1.25 - 0.25) - 321 * 7 / 10000
x3 = -250 * 6 / 4000
x4 = -783 * 4 / 7000
x5 = -200 * 7 / 4000
x6 = -300 * 10 / 6000
x7 = -321 * 9 / 10000
x8 = (2 - 0.35) - 250 * 8 / 4000
x9 = (2.8 - 0.5) - 321 * 12 / 10000
x10 = -783 * 11 / 7000
c = np.array(
[-x1, -x2, -x3, -x4, -x5, -x6, -x7, -x8, -x9, -x10]
) # 由于求的是最大值,所以需要取相反数
A = np.array(
[
[5, 0, 0, 0, 0, 10, 0, 0, 0, 0],
[0, 7, 0, 0, 0, 0, 9, 0, 12, 0],
[0, 0, 6, 0, 0, 0, 0, 8, 0, 0],
[0, 0, 0, 4, 0, 0, 0, 0, 0, 11],
[0, 0, 0, 0, 7, 0, 0, 0, 0, 0],
[-1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
]
)
b = np.array([[6000, 10000, 4000, 7000, 4000, -1]]).T
Aeq = np.array(
[
[1, 1, -1, -1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, -1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, -1],
]
)
beq = np.array([[0, 0, 0]]).T
lb = np.array([0, 0, 0, 0, 0, 0, 0, 1, 1, 1])
ub = np.array([[None] * 10])
bound = np.vstack((lb, ub)).T
res = linprog(
c, A, b, Aeq, beq, bound, integrality=1
) # linprog也可以求整数规划问题,其中参数integrality=1表示决策变量必须为整数
print("最优值为", -res.fun)
print("最优解为", res.x)
# output:最优值为 1146.4142
# 最优解为 [1200. 230. 0. 859. 571. 0. 500. 500. 324. 324.]
1.4
假设前舱装了货物1
x
1
x_1
x1,货物2
x
2
x_2
x2,货物3
x
3
x_3
x3,货物4
x
4
x_4
x4;中仓装了货物1
x
5
x_5
x5,货物2
x
6
x_6
x6,货物3
x
7
x_7
x7,货物4
x
8
x_8
x8;后舱装了货物1
x
9
x_9
x9,货物2
x
10
x_{10}
x10,货物3
x
11
x_{11}
x11,货物4
x
12
x_{12}
x12。由题意可得
max
总利润
=
3100
(
x
1
+
x
5
+
x
9
)
+
3800
(
x
2
+
x
6
+
x
10
)
+
2500
(
x
3
+
x
7
+
x
11
)
+
2850
(
x
4
+
x
8
+
x
12
)
s
.
t
.
{
x
1
+
x
2
+
x
3
+
x
4
≤
10
x
5
+
x
6
+
x
7
+
x
8
≤
16
x
9
+
x
10
+
x
11
+
x
12
≤
8
480
x
1
+
650
x
2
+
580
x
3
+
390
x
4
≤
6800
480
x
5
+
650
x
6
+
580
x
7
+
390
x
8
≤
8700
480
x
9
+
650
x
10
+
580
x
11
+
390
x
12
≤
5300
x
1
+
x
5
+
x
9
≤
18
x
2
+
x
6
+
x
10
≤
15
x
3
+
x
7
+
x
11
≤
23
x
4
+
x
8
+
x
12
≤
12
x
1
+
x
2
+
x
3
+
x
4
10
=
x
5
+
x
6
+
x
7
+
x
8
16
=
x
9
+
x
10
+
x
11
+
x
12
8
x
i
≥
0
,
i
=
1
,
2
,
.
.
.
,
12
\max\ 总利润=3100(x_1+x_5+x_9)+3800(x_2+x_6+x_{10})+2500(x_3+x_7+x_{11})+2850(x_4+x_8+x_{12})\\ \begin{equation} s.t. \begin{cases} x_1+x_2+x_3+x_4\leq 10\\ x_5+x_6+x_7+x_8\leq 16\\ x_9+x_{10}+x_{11}+x_{12}\leq 8\\ 480x_1+650x_2+580x_3+390x_4\leq 6800\\ 480x_5+650x_6+580x_7+390x_8\leq 8700\\ 480x_9+650x_{10}+580x_{11}+390x_{12}\leq 5300\\ x_1+x_5+x_9\leq 18\\ x_2+x_6+x_{10}\leq 15\\ x_3+x_7+x_{11}\leq 23\\ x_4+x_8+x_{12}\leq 12\\ \frac{x_1+x_2+x_3+x_4}{10}=\frac{x_5+x_6+x_7+x_8}{16}=\frac{x_9+x_{10}+x_{11}+x_{12}}{8}\\ x_i\geq 0,& i=1,2,...,12 \end{cases} \end{equation}
max 总利润=3100(x1+x5+x9)+3800(x2+x6+x10)+2500(x3+x7+x11)+2850(x4+x8+x12)s.t.⎩
⎨
⎧x1+x2+x3+x4≤10x5+x6+x7+x8≤16x9+x10+x11+x12≤8480x1+650x2+580x3+390x4≤6800480x5+650x6+580x7+390x8≤8700480x9+650x10+580x11+390x12≤5300x1+x5+x9≤18x2+x6+x10≤15x3+x7+x11≤23x4+x8+x12≤1210x1+x2+x3+x4=16x5+x6+x7+x8=8x9+x10+x11+x12xi≥0,i=1,2,...,12
from scipy.optimize import linprog
import numpy as np
if __name__ == "__main__":
c = np.array([-3100, -3800, -3500, -2850] * 3)
A = np.array(
[
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[480, 650, 580, 390, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 480, 650, 580, 390, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 480, 650, 580, 390],
]
)
b = np.array([[10, 16, 8, 18, 15, 23, 12, 6800, 8700, 5300]]).T
Aeq = np.array(
[
[16, 16, 16, 16, -10, -10, -10, -10, 0, 0, 0, 0],
[0, 0, 0, 0, 8, 8, 8, 8, -16, -16, -16, -16],
[8, 8, 8, 8, 0, 0, 0, 0, -10, -10, -10, -10],
]
)
beq = np.array([[0, 0, 0]]).T
lb = np.zeros(12)
ub = np.array([[None] * 12])
bound = np.vstack((lb, ub)).T
res = linprog(c, A, b, Aeq, beq, bound)
print("最优解为", res.x)
print("最优值为", -res.fun)
# output:最优解为 [ 0. 7. 3. 0. 0. 0.
# 12.94736842 3.05263158 0. 8. 0. 0. ]
# 最优值为 121515.78947368421