0

I'm creating a program which calculates products that can be made from available ingredients based on different recipes. User can add new recipes, ingredients and their quantities. Recipes may contains same ingredients.

I have a List from recipes and a list from ingredients(from which recipes are made). I need to calculate the optimal variation of maximum number of products which can be made among all variations. The constraints are the quantity of ingredients that I have available and the storage space.

When I find the optimal variation and made products I need to decrease quantities in available ingredients with the used quantities for the products.

These are part of my classes for Recipes and Ingredients (all quantities are in grams)

public class Ingredient
    {
        public Ingredient() { }
        public Ingredient(string name, int quantity)
        {
            Name = name;
            Quantity = quantity;
        }         

     public string Name { get; set; }        
     public int Quantity { get; set; }   
     
    } 

public class Recipe 
    {        
        public Recipe(string recipeName, ObservableCollection<Ingredient> ingredients)
        {
            RecipeName = recipeName;
            Ingredients = new ObservableCollection<Ingredient>();
        }         

        public string RecipeName { get; set; }
        public ObservableCollection<Ingredient> Ingredients { get; set; }               
    }

I'm not sure how to calculate all variations when there are more than 2 recipes.

Update:

This is some sample data:

recipe1 Ingredient - "Peppers", Quantity - 300 Ingredient - "Tomatoes", Quantity - 300 Ingredient - "Carrots", Quantity - 100 Ingredient - "Salt", Quantity - 50 Ingredient - "Sugar", Quantity - 50

recipe2
Ingredient - "Peppers", Quantity - 200 Ingredient - "Tomatoes", Quantity - 200 Ingredient - "Eggplant", Quantity - 300 Ingredient - "Salt", Quantity - 25 Ingredient - "Onion", Quantity - 50 Ingredient - "Garlic", Quantity - 25

recipe3
Ingredient - "Potatoes", Quantity - 200 Ingredient - "Tomatoes", Quantity - 200 Ingredient - "Eggplant", Quantity - 300 Ingredient - "Salt", Quantity - 25 Ingredient - "Onion", Quantity - 50 Ingredient - "Garlic", Quantity - 25

This needs to work with random quantities from available ingredients.

Fallon
  • 11
  • 2
  • 1
    Sounds like a knapsack problem. – Dmitry Bychenko Mar 01 '23 at 11:05
  • Please also post your data; otherwise it will be difficult to reproduce your results – Reinderien Mar 01 '23 at 14:27
  • The straight forward (but with a certain amount of recipies also slowest) approach would be creating the [powerset](https://en.wikipedia.org/wiki/Power_set) **P(R)** of all recipies. Then for each set **S in P(R)**, check whether it is possible to create all contained recipies with the available ingredients. Finally return the biggest set **S** for which this is possible ... – derpirscher Mar 02 '23 at 10:42
  • But as SO is not a code-writing service, and you should show some research and coding effort yourself, I will leave it at that ... If you have problems with implementing this approach, feel free to edit your question, show your efforts and ask more specific questions ... – derpirscher Mar 02 '23 at 10:46
  • @derpirscher I have tried many things, but since I'm brand new in this is difficult for me to determine if I'm not doing sth right or the approach is not correct. The last thing have tried is google.OrTools.LinearSolver but I'm not sure how to proceed. I read that I need to define: variables for each recipe and and ingredient; objective function(I think that this have to be "maximizing the total number of products"); constraints(ingredients quantities and available storage).. I tried a few things but still nothing.. and desided to ask here if there is easier way to calculate this.. – Fallon Mar 02 '23 at 12:31
  • @derpirscher First I tied another approach - to find the limiting factor for each recipe, then I can calculate the optimal number of products but this worked only for 2 recipes.. – Fallon Mar 02 '23 at 12:35
  • @DmitryBychenko yes, I read about knapsack problem, but I'm not sure how to implement it in my project. Can you give me some directions how to start? Thank you in advance! – Fallon Mar 08 '23 at 12:18

0 Answers0