Excel 在货架上寻找最佳产品组合

时间:2021-02-06 21:11:21

标签: python excel

在 Excel 中,

假设我已经找到了给定产品的最佳排名,我需要找到将产品与约束混合的最佳解决方案,一个独特的项目只能有 6 个,所有项目最多有 15 个可能的组合并且必须至少有其中之一,我知道,对于我展示的示例,您可以推断出最佳组合是。 6+6+1+1+1 用于排名 1 到 5,但此练习对于大型数据集确实很有帮助。

提前谢谢你,如果有办法在 python 中做到这一点,我会很高兴听到它

enter image description here

Image tiny example

1 个答案:

答案 0 :(得分:0)

问题需要重写以具有适当的数学结构。特别考虑解决的方法。我将使用 linprog 函数以便最好地查看他们的示例和信息 here。 我将使用 x1、x2、x3、x4、x5 代替 a、b、c、d、e。

这是因为函数的符号可能会令人困惑。 根据我们的案例改编一个例子:

from scipy.optimize import linprog
c = [1, 2, 3, 4, 5]
A = [[-1, -1, -1, -1, -1]]
b = [-15]

x1_bounds = (1, 6)
x2_bounds = (1, 6)
x3_bounds = (1, 6)
x4_bounds = (1, 6)
x5_bounds = (1, 6)

res = linprog(c, A_ub=A, b_ub=b,
        bounds=[x1_bounds, x2_bounds,
        x3_bounds, x4_bounds,
        x5_bounds],method='revised simplex')

输出如下:

print(res.x)
[6. 6. 1. 1. 1.]

解释:最好的选择是选择 a 中的 6 个,b 中的 6 个,其余各选择 1 个。

参考资料

  1. https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linprog.html
  2. https://towardsdatascience.com/linear-programming-and-discrete-optimization-with-python-using-pulp-449f3c5f6e99
相关问题