Advertisement
makispaiktis

Course 7 - Full of inequality constraints

Aug 29th, 2023
1,480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.49 KB | None | 0 0
  1. clear all
  2. close all
  3. clc
  4.  
  5.  
  6. % 1. Load the dataset
  7. load Nutrition.mat
  8. food
  9.  
  10. % 2. Create the optimization problem
  11. prob = optimproblem("Description","An Optimal Breakfast");
  12. servings = optimvar("servings",16,"LowerBound",0);
  13. C = food.Price .* servings;
  14. prob.Objective = sum(C);
  15. cals = food.Calories .* servings;
  16. prob.Constraints.calories = sum(cals) == 350;
  17. show(prob)
  18.  
  19. % 3a. Inequality - carbs
  20. carbs = food.Carbs .* servings;
  21. totalCarbs = sum(carbs);
  22. prob.Constraints.carbs = totalCarbs >= 45;
  23. show(prob)
  24. [sol, optval] = solve(prob)
  25. bar(food.Name, sol.servings);
  26. optCarbs = evaluate(totalCarbs, sol)
  27.  
  28. % 3b. Inequality - protein
  29. protein = food.Protein .* servings;
  30. totalProtein = sum(protein)
  31. prob.Constraints.protein = totalProtein >= 15
  32. sol2 = solve(prob)
  33. bar(food.Name,sol2.servings)
  34. optProtein = evaluate(totalProtein,sol2)
  35.  
  36. % 3c. Inequality - vitaminC
  37. vitaminC = food.VitaminC .* servings;
  38. totalVitaminC = sum(vitaminC)
  39. prob.Constraints.vitaminC = totalVitaminC >= 60
  40. sol3 = solve(prob)
  41. bar(food.Name,sol3.servings)
  42. optVitaminC = evaluate(totalVitaminC,sol3)
  43.  
  44. % 4. New inequality constraints
  45.  
  46. carbs = food.Carbs .* servings;
  47. totalCarbs = sum(carbs);
  48. prob.Constraints.carbs = totalCarbs <= 30;
  49.  
  50. protein = food.Protein .* servings;
  51. totalProtein = sum(protein)
  52. prob.Constraints.protein = totalProtein >= 60;
  53.  
  54. vitaminC = food.VitaminC .* servings;
  55. totalVitaminC = sum(vitaminC)
  56. prob.Constraints.vitaminC = totalVitaminC >= 60;
  57.  
  58. show(prob);
  59. [sol4, optval] = solve(prob)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement