我试图弄清楚什么是错我的执行意想不到的解决方案,我希望得到的结果是[5, 10]
,我不明白它是如何得到[7.5, 7.5]
,x1
应该是一半x2
。从笔者线性规划,等式约束
from scipy.optimize import linprog
import numpy as np
c = [-1, -1]
A_eq = np.array([
[1, 0.5],
[1, -0.5],
])
b_eq = [15, 0]
x0_bounds = (0, None)
x1_bounds = (0, None)
res = linprog(
c,
A_eq=A_eq.transpose(),
b_eq=b_eq,
bounds=(x0_bounds, x1_bounds),
options={"disp": True})
print res.x
# =>
# Optimization terminated successfully.
# Current function value: -15.000000
# Iterations: 2
# [ 7.5 7.5]
更新:
正如有人说这里不需要矩阵转置。 问题是在基质本身中,为了获得期望的结果,这是[5, 10]
,它必须是:
A_eq = np.array([
[1, 1],
[1, -0.5],
])
谢谢,原来的问题是没有矩阵换位,这样做可能是因为晚上不知不觉,可能是因为晚了,基本上找到了如何达到预期结果,矩阵的第一行必须是'[1,1]',再次感谢。 –
@RustyRobot很高兴听到您发现问题出在哪里。不用谢! :) –