Question: I need to gather products from warehouse centers for product bundling like this {product:quantity} center_a_products = {1: 8, 2: 4, 3: 12} Every centers has different kinds and quantities of products like this {center:{product:quantity}} product_quantities = {1: {1: 10, 2: 3, 3: 15}, 2: {1: 5, 2: 8, 3: 10}, 3: {1: 12, 2: 6}} I can only visit one center at a time but I can collect multiple product from one center. If total quantity of some product is less than center_a_products, I must visit every center containing that product and collect total quantity of that product. There are many combination meets these conditions but I need to know most efficient combination that visits the smallest number of centers I tried this but it failed... 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}")  I expected something like this 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 

Answer: The problem description lends itself to solving with a Mixed-Integer-Linear Program (MIP). A convenient library for solving mixed integer problems is PuLP that ships with the built-in Coin-OR suite and in particular the integer solver CBC. We formulate a model that describes your problem: 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()  Print solution: 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}")  Output: 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  Currently the example you provided is pretty trivial. It's easily possible to add additional constraints, but you need to provide better description of the problem and the expected output. Translating a problem description into a linear program can take some practice. The trickiest line in this program is problem += product_vars[center, product] <= center_vars[center] * product_quantities[center][product]  The objective minimizes center_vars, but the satisfaction constraint forces some of them to be 1. To get the mindset of describing problems, rather than thinking of solutions I recommend Raymond Hettinger's excellent talk on PyCon 2019 - Modern solvers: Problems well-defined are problems solved to install PuLP pip install pulp