我需要从仓库中心收集产品进行产品Bundle 像这样{产品:数量} center_a_products = {1: 8, 2: 4, 3: 12}

每个中心都有不同种类和数量的产品 像这样的{Center:{product:Quantity}}product_quantities = {1: {1: 10, 2: 3, 3: 15}, 2: {1: 5, 2: 8, 3: 10}, 3: {1: 12, 2: 6}}

我一次只能参观一个中心,但我可以从一个中心收集多个产品. 如果某个产品的总数量少于center_a_products个,我必须走访每个包含该产品的中心,收集该产品的总数量. 有很多组合满足这些条件,但我需要知道访问最少中心的最有效组合

我试过了,但失败了.

from itertools import combinations

# Define the centers
centers = [1, 2, 3]

# Define the products and quantities needed in Center A
center_a_products = {
    1: 8,
    2: 4,
    3: 12
}

# Define the products and quantities for each center
product_quantities = {
    1: {1: 10, 2: 3, 3: 15},
    2: {1: 5, 2: 8, 3: 10},
    3: {1: 12, 2: 6}
}

# Function to check if a product combination meets the requirements
def meets_requirements(combination):
    product_counts = {product: 0 for product in center_a_products.keys()}
    for center, products in combination:
        for product, quantity in products.items():
            product_counts[product] += quantity
    for product, required_quantity in center_a_products.items():
        if product_counts[product] < required_quantity:
            return False
    return True

# Find all combinations of products
combinations_to_move = []
for r in range(1, len(centers) + 1):
    for combination in combinations(zip(centers, [product_quantities[c] for c in centers]), r):
        if meets_requirements(combination):
            combinations_to_move.append(combination)

# Print the combinations
for idx, combination in enumerate(combinations_to_move, start=1):
    print(f"Combination {idx}:")
    for center, products in combination:
        for product, quantity in products.items():
            print(f"Move {quantity} units of product {product} from Center {center}")

我预料到会有这样的事情

Move 8 units of product 1 from Center 1
Move 3 units of product 2 from Center 1
Move 12 units of product 3 from Center 1
Move 1 units of product 3 from Center 2

推荐答案

问题描述给出了Mixed-Integer-Linear Program (MIP)分,有助于解决问题.

用于解决混合整数问题的一个方便的库是PuLP,它随内置的Coin-OR suite一起提供,特别是整数求解器CBC.

我们制定了一个模型来描述您的问题:

import pulp


center_a_products = {
    1: 8,
    2: 4,
    3: 12
}

product_quantities = {
    1: {1: 10, 2: 5, 3: 15},
    2: {1: 5, 2: 8, 3: 10},
    3: {1: 12, 2: 6, 3: 20}
}

problem = pulp.LpProblem("CenterSatisfactionProblem", pulp.LpMinimize)

# Center vars are binary variables that we minimize in the objective. They indicate whether a center was visited or not
center_vars = {center: pulp.LpVariable(f"Center_{center}", cat="Binary") for center in product_quantities}
product_vars = {(center, product): pulp.LpVariable(f"Center_{center}_Product_{product}", lowBound=0, cat="Integer")
                for center in product_quantities
                for product in product_quantities[center]}


# Satisfy all demand
for product in center_a_products:
    problem += pulp.lpSum(product_vars[center, product] for center in product_quantities) == center_a_products[product]

# We can only take stuff from a center if we visit it 
# Also: can't take more stuff from a center than is in stock

for center in product_quantities:
    for product in product_quantities[center]:
        problem += product_vars[center, product] <= center_vars[center] * product_quantities[center][product]
# Minimize amount of centers visited
problem += pulp.lpSum(center_vars.values())
problem.solve()

打印解决方案:

for center in product_quantities:
    if center_vars[center].varValue == 1:
        for product in product_quantities[center]:
            quantity = product_vars[center, product].varValue
            if quantity > 0:
                print(f"Move {quantity} units of product {product} from center {center}")

输出:

Move 8.0 units of product 1 from center 3
Move 4.0 units of product 2 from center 3
Move 12.0 units of product 3 from center 3

目前,您提供的示例非常简单.可以很容易地添加额外的约束,但您需要更好地描述问题和预期输出.

将问题描述转换为线性规划需要一些练习.这个节目中最棘手的一句话是

problem += product_vars[center, product] <= center_vars[center] * product_quantities[center][product]

目标最小化为center_vars,但满意度约束迫使其中一些目标为1.

为了获得描述问题而不是思考解决方案的心态,我推荐Raymond Hettinger关于PyCon 2019-Modern solvers: Problems well-defined are problems solved的精彩演讲

要安装PuLP

pip install pulp

Python相关问答推荐

如何在Python中使用ijson解析SON期间检索文件位置?

Python无法在已导入的目录中看到新模块

如何处理嵌套的SON?

Chatgpt API不断返回错误:404未能从API获取响应

Python Hashicorp Vault库hvac创建新的秘密版本,但从先前版本中删除了密钥

比较两个数据帧并并排附加结果(获取性能警告)

Pystata:从Python并行运行stata实例

将两只Pandas rame乘以指数

如何找到满足各组口罩条件的第一行?

如果满足某些条件,则用另一个数据帧列中的值填充空数据帧或数组

不允许访问非IPM文件夹

在单个对象中解析多个Python数据帧

如何从列表框中 Select 而不出错?

* 动态地 * 修饰Python中的递归函数

如何在海上配对图中使某些标记周围的黑色边框

如何在Great Table中处理inf和nans

如何合并具有相同元素的 torch 矩阵的行?

为什么在Python中00是一个有效的整数?

在一个数据帧中,我如何才能发现每个行号是否出现在一列列表中?

如何在Polars中创建条件增量列?