为什么我用 scipy.optimize 得不到我想要的值。 (线性规划问题)
Why am I not getting the values I want with scipy.optimize. (linear programming problems)
import numpy as np
from scipy.optimize import linprog
b_ub = [74, 40, 36]
b_eq = [20, 45, 30]
A = np.array([[7, 3, 6], [4, 8, 2], [1, 5, 9]])
m, n = A.shape
c = list(np.reshape(A, n * m)) # Convert matrix A to list c.
A_ub = np.zeros([m, m * n])
for i in np.arange(0, m,
1): # Filling in the matrix of conditions-inequalities.
for j in np.arange(0, n * m, 1):
if i * n <= j <= n + i * n - 1:
A_ub[i, j] = 1
A_eq = np.zeros([m, m * n])
for i in np.arange(0, m, 1): # Filling in the matrix of conditions-equalities
k = 0
for j in np.arange(0, n * m, 1):
if j == k * n + i:
A_eq[i, j] = 1
k = k + 1
print(linprog(c, A_ub, b_ub, A_eq, b_eq))
我明白了
而不是
我认为这是默认解算器造成的。交换求解器给出了您要寻找的解决方案。请参阅 docs 并评估使用每个求解器的权衡以确定您要使用的内容。
print(linprog(c, A_ub, b_ub, A_eq, b_eq, method='highs-ds'))
con: array([0., 0., 0.])
crossover_nit: 0
fun: 215.0
message: 'Optimization terminated successfully.'
nit: 3
slack: array([29., 10., 16.])
status: 0
success: True
x: array([ 0., 45., 0., 0., 0., 30., 20., 0., 0.])
import numpy as np
from scipy.optimize import linprog
b_ub = [74, 40, 36]
b_eq = [20, 45, 30]
A = np.array([[7, 3, 6], [4, 8, 2], [1, 5, 9]])
m, n = A.shape
c = list(np.reshape(A, n * m)) # Convert matrix A to list c.
A_ub = np.zeros([m, m * n])
for i in np.arange(0, m,
1): # Filling in the matrix of conditions-inequalities.
for j in np.arange(0, n * m, 1):
if i * n <= j <= n + i * n - 1:
A_ub[i, j] = 1
A_eq = np.zeros([m, m * n])
for i in np.arange(0, m, 1): # Filling in the matrix of conditions-equalities
k = 0
for j in np.arange(0, n * m, 1):
if j == k * n + i:
A_eq[i, j] = 1
k = k + 1
print(linprog(c, A_ub, b_ub, A_eq, b_eq))
我明白了
而不是
我认为这是默认解算器造成的。交换求解器给出了您要寻找的解决方案。请参阅 docs 并评估使用每个求解器的权衡以确定您要使用的内容。
print(linprog(c, A_ub, b_ub, A_eq, b_eq, method='highs-ds'))
con: array([0., 0., 0.])
crossover_nit: 0
fun: 215.0
message: 'Optimization terminated successfully.'
nit: 3
slack: array([29., 10., 16.])
status: 0
success: True
x: array([ 0., 45., 0., 0., 0., 30., 20., 0., 0.])