使用Scipy的linprog和单纯形法,我如何解决一个简单的tableau并得到预期的结果?

vdzxcuhz  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(137)

我正在尝试使用python库scipy**解决一个简单的单纯形表。

c = [7, 9, 18, 17]
A = [
    [2, 4, 5, 7],
    [ 1, 1, 2, 2],
    [ 1, 2, 3, 3]
]
b = [42, 17, 24]

from scipy.optimize import linprog

print(linprog(c, A, b, method='simplex'))

根据勒阿弗尔大学的course,这个例子表被用来介绍单纯形法,我应该得到变量**[3,0,7,0]和最小化函数**的147。

相反,我得到[0,0,0,0]和0

jljoyd4f

jljoyd4f1#

你的问题设置的优化意识是不正确的。linprog假设最小化,但教科书要求最大化。反转c的符号,问题成功:

import numpy as np
from scipy.optimize import linprog

c = -np.array((7, 9, 18, 17))
A = [
    [2, 4, 5, 7],
    [1, 1, 2, 2],
    [1, 2, 3, 3]
]
b = [42, 17, 24]

result = linprog(c=c, A_ub=A, b_ub=b, method='highs')
print(result)
message: Optimization terminated successfully. (HiGHS Status 7: Optimal)
        success: True
         status: 0
            fun: -147.00000000000006
              x: [ 3.000e+00  0.000e+00  7.000e+00  0.000e+00]
            nit: 4
          lower:  residual: [ 3.000e+00  0.000e+00  7.000e+00  0.000e+00]
                 marginals: [ 0.000e+00  2.000e+00  0.000e+00  1.000e+00]
          upper:  residual: [       inf        inf        inf        inf]
                 marginals: [ 0.000e+00  0.000e+00  0.000e+00  0.000e+00]
          eqlin:  residual: []
                 marginals: []
        ineqlin:  residual: [ 1.000e+00  0.000e+00  0.000e+00]
                 marginals: [-0.000e+00 -3.000e+00 -4.000e+00]
 mip_node_count: 0
 mip_dual_bound: 0.0
        mip_gap: 0.0

相关问题