1

我正在尝试解决 Saul Grass 在他的书“线性规划图解指南”第 12 页的运输问题中描述的问题。

冰箱必须按
以下数量(10、8、7)运送到 3 家商店(S1、S2、S3)
从工厂 F1、F2 到商店的运输成本为:
F1(8、6、10)= 11(总计F1)
F2 (9,5,7) = 14 (F2 总出货量)

Saul Grass 给出了最小化的目标函数:

8x_11 + 6x_12 + 10x_13 + 9x_21 + 5x_22 + 7x_23

和约束 c 为:
x_11 + x_12 + x_13 + 0x_21 + 0x_22 + 0x_23 = 11
0x_11 + 0x_12 + 0x_13 + x_21 + x_22 + x_23 = 14
x_11 + 0x_12 + 0x_13 + x_21 + 0x_22 + 0x_23 = 20
x_1110 + x_1 + 0x_21 + x_22 + 0x_23 = 8
0x_11 + 0x_12 + x_13 + 0x_21 + 0x_22 + x_23 = 7

他最好的解决方案是 [10,1,0,0,7,7] :

10 x 8x_11 + 1 x 6x_12 + 0 x 10x_13 + 0 x 9x_21 + 7 x 5x_22 + 7 x 7x_23 = 170

我试图用 scipy 解决这个问题,但得到的结果不如 Saul Grass 的解决方案(204 vs. 170)。我的解决方案有什么问题?

我的代码:

import numpy as np
from scipy.optimize import linprog

c = [-8,-6,-10,-9,-5,-7] 
A = [[1,1,1,0,0,0],[0,0,0,1,1,1],[1,0,0,1,0,0],[0,1,0,0,1,0],[0,0,1,0,0,1]] 
b = [11,14,10,8,7]
x0_bounds = (0, None)
x1_bounds = (0, None)
x2_bounds = (0, None)
x3_bounds = (0, None)
x4_bounds = (0, None)
x5_bounds = (0, None)

res = linprog(c, A_ub=A, b_ub=b,  bounds=(x0_bounds, x1_bounds,x2_bounds,x3_bounds, x4_bounds,x5_bounds), method='simplex', options={"disp": True})
print(res)

我的结果:

Optimization terminated successfully.
         Current function value: -204.000000 
         Iterations: 4
     fun: -204.0
 message: 'Optimization terminated successfully.'
     nit: 4
   slack: array([0., 0., 0., 0., 0.])
  status: 0
 success: True
       x: array([ 0.,  4.,  7., 10.,  4.,  0.])
4

1 回答 1

2

当给出相等约束时,应使用,和参数的文档scipy.optimize.linprog。并且应该是,不是,因为最小化目标函数。A_eqb_eqc[8, 6, 10, 9, 5, 7][-8, -6, -10, -9, -5, -7]scipy.optimize.linprog

因此,您可以执行以下操作:

from scipy.optimize import linprog

c = [8, 6, 10, 9, 5, 7]
A = [[1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1], [1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1]]
b = [11, 14, 10, 8, 7]
x0_bounds = (0, None)
x1_bounds = (0, None)
x2_bounds = (0, None)
x3_bounds = (0, None)
x4_bounds = (0, None)
x5_bounds = (0, None)

res = linprog(c, A_eq=A, b_eq=b, bounds=(x0_bounds, x1_bounds, x2_bounds, x3_bounds, x4_bounds, x5_bounds),
              method='simplex', options={"disp": True})
print(res)

哪个打印

Optimization terminated successfully.
         Current function value: 170.000000  
         Iterations: 6
     con: array([0., 0., 0., 0., 0.])
     fun: 170.0
 message: 'Optimization terminated successfully.'
     nit: 6
   slack: array([], dtype=float64)
  status: 0
 success: True
       x: array([10.,  1.,  0.,  0.,  7.,  7.])
于 2020-08-02T08:30:05.370 回答