我需要从仓库中心收集产品进行产品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