Search code examples
algorithmgreedyknapsack-problem

multi-dimensional knapsack with multiple constraints


i am unsure if i have even identified the problem correctly, but reading up on Knapsack problem seems the closest to what i am trying to solve:

A cook has several ingredients of varying quantities. for example:

8 eggs 3 sausages 500 mL milk 12 strawberries

There is a finite list of recipes, each consisting varying ingredients of varying quantities. The universe of ingredients is finite, as is the quantity of each ingredient in all recipes.

Each recipe may or may not contain any of the ingredients the cook has.

The cook wants to use up all his ingredients as much as possible to minimize waste on 1 recipe.

There is a case where the cook wants to use all his ingredients on 2 or 3 different recipes, with minimal leftovers.

What is his optimized solution?

EDIT: My question is a more complex version of the following knapsack problem http://www.g12.cs.mu.oz.au/wiki/doku.php?id=simple_knapsack


Solution

  • This doesn't sound like the knapsack problem if i'm not missing anything in your Q. The amount of each ingredient to go into each recipe is known so your slot size isn't a variable.

    If i read your Q correctly, all you need to do is to run the ingredients thru each recipe, see whether the amounts are sufficient, and if so, calculate the value of the ingredients to be left out on that one. the recipe with the minimum of such positive values is your answer. takes \theta(m*n) time with direct access to the ingredients list-- m & n the number of ingredients and the recipes.