[英]Cannot get total of required 100 in Scipy linear programming
我正在尝试解决一个优化问题,当我使用太简单的数据集时,我得到准确的结果并且 res.x 的总和达到 100,这是我想要的,但是当我使用我的原始数据时,res.x 给出小于 1 或负值的变量,其次它们总和不等于 100。
更简单的版本是这样的:
from scipy.optimize import linprog
# sample data
c = [90, 190]
A = [[12, 50], [4.4, 1.2]]
b = [22, 3.6]
res = linprog(c, A_eq=A, b_eq=b)
res.x
# Output array([0.74708171, 0.26070039])
这个例子工作正常,但由于我将使用更现实的值,问题将无法正确解决。
# sample data
# costs
c = [15, 16, 22, 90]
A = [
# cp
[8, 8, 12, 41],
# me
[3.4, 3.5, 3.3, 3.5]
# can further take dm as well
]
b = [15, 2.0]
x0_bounds = (0, 50)
x1_bounds = (0, 25)
x2_bounds = (0, 10)
x3_bounds = (0, 3)
bounds=[x0_bounds, x1_bounds, x2_bounds, x3_bounds]
res = linprog(c, A_ub=A, b_ub=b, bounds=bounds)
res.x
# Output:
# array([8.51258834e-15, 4.70244892e-14, 8.70913453e-14, 2.34489024e-14])
我也尝试从类似的问题中寻求帮助,但是从这个 repo 复制相同的代码也没有帮助。 https://github.com/thotasu/scipy.optimize.linprog/blob/master/whiskas.ipynb
后来我意识到我还必须以约束的形式将总和设为 100,例如x1 + x2 = 100
,所以我不得不添加另一个等式。
c = [90, 190]
A = [[12, 50], [4.4, 1.2], [1, 1]]
b = [22, 3.6, 100]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.