<jupyter_start><jupyter_text>Read the given nutrition dataset into a Pandas DataFrame objectNote we are reading only the first 64 rows with `nrows=64` argument because we just want to read all the nutrients informtion and not the maximum/minimum bounds in the dataset. We will enter those bounds in the optimization problem separately.<jupyter_code>df = pd.read_excel("Data/diet - medium.xls",nrows=17)<jupyter_output><empty_output><jupyter_text>Show the dataset<jupyter_code>df<jupyter_output><empty_output><jupyter_text>Create the `PuLP` problem variable. Since it is a cost minimization problem, we need to use `LpMinimize`<jupyter_code># Create the 'prob' variable to contain the problem data
prob = LpProblem("Simple Diet Problem",LpMinimize)<jupyter_output>/home/andreas/anaconda3/envs/py38/lib/python3.8/site-packages/pulp/pulp.py:1190: UserWarning: Spaces are not permitted in the name. Converted to '_'
  warnings.warn("Spaces are not permitted in the name. Converted to '_'")<jupyter_text>Create a list of food items from the dataset<jupyter_code># Creates a list of the Ingredients
food_items = list(df['Foods'])
print("So, the food items to consdier, are\n"+"-"*100)
for f in food_items:
    print(f,end=', ')<jupyter_output>So, the food items to consdier, are
----------------------------------------------------------------------------------------------------
Frozen Broccoli, Frozen Corn, Raw Lettuce Iceberg,  Baked Potatoes, Tofu, Roasted Chicken, Spaghetti W/ Sauce, Raw Apple, Banana, Wheat Bread, White Bread, Oatmeal Cookies, Apple Pie, Scrambled Eggs, Turkey Bologna, Beef Frankfurter, Chocolate Chip Cookies,<jupyter_text>Create a dictinary of costs for all food items<jupyter_code>costs = dict(zip(food_items,df['Price/Serving']))
costs<jupyter_output><empty_output><jupyter_text>Create a dictionary of calories for all food items<jupyter_code>calories = dict(zip(food_items,df['Calories']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of cholesterol for all food items<jupyter_code>cholesterol = dict(zip(food_items,df['Cholesterol (mg)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of total fat for all food items<jupyter_code>fat = dict(zip(food_items,df['Total_Fat (g)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of sodium for all food items<jupyter_code>sodium = dict(zip(food_items,df['Sodium (mg)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of carbohydrates for all food items<jupyter_code>carbs = dict(zip(food_items,df['Carbohydrates (g)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of dietary fiber for all food items<jupyter_code>fiber = dict(zip(food_items,df['Dietary_Fiber (g)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of protein for all food items<jupyter_code>protein = dict(zip(food_items,df['Protein (g)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of vitamin A for all food items<jupyter_code>vit_A = dict(zip(food_items,df['Vit_A (IU)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of vitamin C for all food items<jupyter_code>vit_C = dict(zip(food_items,df['Vit_C (IU)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of calcium for all food items<jupyter_code>calcium = dict(zip(food_items,df['Calcium (mg)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of iron for all food items<jupyter_code>iron = dict(zip(food_items,df['Iron (mg)']))<jupyter_output><empty_output><jupyter_text>Create a dictionary of food items with lower bound<jupyter_code># A dictionary called 'food_vars' is created to contain the referenced Variables
food_vars = LpVariable.dicts("Food",food_items,0,cat='Continuous')
food_vars<jupyter_output><empty_output><jupyter_text>Adding the objective function to the problem<jupyter_code># The objective function is added to 'prob' first
prob += lpSum([costs[i]*food_vars[i] for i in food_items]), "Total Cost of the balanced diet"<jupyter_output><empty_output><jupyter_text>Adding the calorie constraints to the problem<jupyter_code>prob += lpSum([calories[f] * food_vars[f] for f in food_items]) >= 800.0, "CalorieMinimum"
prob += lpSum([calories[f] * food_vars[f] for f in food_items]) <= 1300.0, "CalorieMaximum"<jupyter_output><empty_output><jupyter_text>Adding other nutrient constraints to the problem one by one...  Cholesterolprob += lpSum([cholesterol[f] * food_vars[f] for f in food_items]) >= 30.0, "CholesterolMinimum"prob += lpSum([cholesterol[f] * food_vars[f] for f in food_items]) <= 240.0, "CholesterolMaximum" Fatprob += lpSum([fat[f] * food_vars[f] for f in food_items]) >= 40.0, "FatMinimum"prob += lpSum([fat[f] * food_vars[f] for f in food_items]) <= 100.0, "FatMaximum" Sodiumprob += lpSum([sodium[f] * food_vars[f] for f in food_items]) >= 500.0, "SodiumMinimum"prob += lpSum([sodium[f] * food_vars[f] for f in food_items]) <= 2000.0, "SodiumMaximum" Carbsprob += lpSum([carbs[f] * food_vars[f] for f in food_items]) >= 130.0, "CarbsMinimum"prob += lpSum([carbs[f] * food_vars[f] for f in food_items]) <= 450.0, "CarbsMaximum" Fiberprob += lpSum([fiber[f] * food_vars[f] for f in food_items]) >= 125.0, "FiberMinimum"prob += lpSum([fiber[f] * food_vars[f] for f in food_items]) <= 250.0, "FiberMaximum" Proteinprob += lpSum([protein[f] * food_vars[f] for f in food_items]) >= 60.0, "ProteinMinimum"prob += lpSum([protein[f] * food_vars[f] for f in food_items]) <= 100.0, "ProteinMaximum" Vitamin Aprob += lpSum([vit_A[f] * food_vars[f] for f in food_items]) >= 1000.0, "VitaminAMinimum"prob += lpSum([vit_A[f] * food_vars[f] for f in food_items]) <= 10000.0, "VitaminAMaximum" Vitamin Cprob += lpSum([vit_C[f] * food_vars[f] for f in food_items]) >= 400.0, "VitaminCMinimum"prob += lpSum([vit_C[f] * food_vars[f] for f in food_items]) <= 5000.0, "VitaminCMaximum" Calciumprob += lpSum([calcium[f] * food_vars[f] for f in food_items]) >= 300.0, "CalciumMinimum"prob += lpSum([calcium[f] * food_vars[f] for f in food_items]) <= 1500.0, "CalciumMaximum" Ironprob += lpSum([iron[f] * food_vars[f] for f in food_items]) >= 10.0, "IronMinimum"prob += lpSum([iron[f] * food_vars[f] for f in food_items]) <= 40.0, "IronMaximum"<jupyter_code># Fat
prob += lpSum([fat[f] * food_vars[f] for f in food_items]) >= 20.0, "FatMinimum"
prob += lpSum([fat[f] * food_vars[f] for f in food_items]) <= 50.0, "FatMaximum"

# Carbs
prob += lpSum([carbs[f] * food_vars[f] for f in food_items]) >= 130.0, "CarbsMinimum"
prob += lpSum([carbs[f] * food_vars[f] for f in food_items]) <= 200.0, "CarbsMaximum"

# Fiber
prob += lpSum([fiber[f] * food_vars[f] for f in food_items]) >= 60.0, "FiberMinimum"
prob += lpSum([fiber[f] * food_vars[f] for f in food_items]) <= 125.0, "FiberMaximum"

# Protein
prob += lpSum([protein[f] * food_vars[f] for f in food_items]) >= 100.0, "ProteinMinimum"
prob += lpSum([protein[f] * food_vars[f] for f in food_items]) <= 150.0, "ProteinMaximum"<jupyter_output><empty_output><jupyter_text>Writing problem data to a `.lp` file<jupyter_code># The problem data is written to an .lp file
prob.writeLP("SimpleDietProblem.lp")<jupyter_output><empty_output><jupyter_text>Run the solver<jupyter_code># The problem is solved using PuLP's choice of Solver
prob.solve()<jupyter_output><empty_output><jupyter_text>Print the problem solution status `'optimal'`, `'infeasible'`, `'unbounded'` etc...<jupyter_code># The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])<jupyter_output>Status: Optimal<jupyter_text>Scan through the problem variables and print out only if the variable quanity is positive i.e. it is included in the optimal solution<jupyter_code>print("Therefore, the optimal (least cost) balanced diet consists of\n"+"-"*110)
for v in prob.variables():
    if v.varValue>0:
        print(v.name, "=", v.varValue)<jupyter_output>Therefore, the optimal (least cost) balanced diet consists of
--------------------------------------------------------------------------------------------------------------
Food_Frozen_Broccoli = 6.9242113
Food_Scrambled_Eggs = 6.060891
Food__Baked_Potatoes = 1.0806324<jupyter_text>Print the optimal diet cost<jupyter_code>print("The total cost of this balanced diet is: ${}".format(round(value(prob.objective),2)))<jupyter_output>The total cost of this balanced diet is: $5.52
StarcoderdataJupyterStructuredCleanDedup